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

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.


    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 eg 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 ie

    data Maybe a = Just a | Nothing
    

    The return function constructs a monadic value from a 'plain' value eg

    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 eg ( IO , Maybe , [] ) which is used to construct types, while return is a function which constructs a monadic value of type ma from a value of type a .

    For the monad instance of Maybe , return constructs a value of Maybe a with just ie

    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 ma for some monad m .


    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...

    链接地址: http://www.djcxy.com/p/7510.html

    上一篇: 从文件加载纯全局变量

    下一篇: monad的类型构造函数和返回函数之间的区别(在Haskell中)