Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between type constructor and return function of a monad (in Haskell)

Tags:

haskell

monads

I'm trying to figure out monads in Haskell but didn't get too far yet.

I found https://en.wikibooks.org/wiki/Haskell/Understanding_monads#cite_note-1 and several other tutorials/explanations, but none seems to be explaining the difference between the type constructor and the return function.

As I understood the

  • type constructor constructs a monad from a given value of the basic data type. So it's sort of a normal constructor like in Java, which builds from the given parameter a new instance.
  • return function applies the type constructor on the given value of the basic data type and returns the constructed monad.

So what's the point of having two functions doing basically the same?

EDIT So using the example of a Maybe-monad, the

  • country = Just "China": (constructor) creates the monad for the value "China".
  • return "China": returns the monad which is corresponding to the value of China, so it's basically the monad containing the "China" value.

Generally speaking I understand a monad as a container for values. One usage of monads is to combine simple/existing computations to more complex computations.

like image 645
Jonas Avatar asked Apr 16 '26 12:04

Jonas


2 Answers

Type constructors are type-level functions which return a type. Maybe is a type constructor which takes a single type parameter and returns a type e.g. Maybe String, Maybe Int etc.

data constructors are used to create values of a particular type. For some type Maybe a these constructors are Just and Nothing i.e.

data Maybe a = Just a | Nothing

The return function constructs a monadic value from a 'plain' value e.g.

return 1 :: Maybe Int
return "s" :: [String]

So in the definition of the Monad class

class Monad m where
  return :: a -> m a

m is a type constructor e.g. (IO, Maybe, []) which is used to construct types, while return is a function which constructs a monadic value of type m a from a value of type a.

For the monad instance of Maybe, return constructs a value of Maybe a with just i.e.

instance Monad Maybe where
  return x = Just x

so if you know you are dealing with Maybe it doesn't matter which you use. However, return has a more general type since it can be used to construct an arbitrary value m a for some monad m.

like image 117
Lee Avatar answered Apr 19 '26 15:04

Lee


A type constructor constructs a type out of other types. It is not a function and has nothing to do with values.

In Haskell, [] is a type constructor. When applied to a type, say Int, it makes another type [Int].

Incidentally, in Java [] is a type constructor too. It can make a new type Int[] out of existing type Int.

Perhaps you wanted to ask about data constructors. Indeed, [] is also a data constructor (different from the type constructor spelled []) and in certain contexts it is equivalent to return. Why do we need return then? return works for any monad and can be used to write generic monadic code that works for any monad. It is a generalization of [] and Just and Left and...

like image 23
n. 1.8e9-where's-my-share m. Avatar answered Apr 19 '26 16:04

n. 1.8e9-where's-my-share m.