简体   繁体   English

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

[英]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.我试图找出 Haskell 中的 monad,但还没有走得太远。

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.我找到了https://en.wikibooks.org/wiki/Haskell/Understanding_monads#cite_note-1和其他几个教程/解释,但似乎没有解释类型构造函数和返回函数之间的区别。

As I understood the正如我所理解的

  • type constructor constructs a monad from a given value of the basic data type.类型构造函数从基本数据类型的给定值构造一个 monad。 So it's sort of a normal constructor like in Java, which builds from the given parameter a new instance.所以它有点像 Java 中的普通构造函数,它从给定的参数构建一个新实例。
  • return function applies the type constructor on the given value of the basic data type and returns the constructed monad. return函数对基本数据类型的给定值应用类型构造函数并返回构造的 monad。

So what's the point of having two functions doing basically the same?那么让两个函数做基本相同的事情有什么意义呢?

EDIT So using the example of a Maybe-monad, the编辑所以使用一个Maybe-monad的例子,

  • country = Just "China" : (constructor) creates the monad for the value "China". country = Just "China" : (构造函数) 为值 "China" 创建 monad。
  • return "China" : returns the monad which is corresponding to the value of China, so it's basically the monad containing the "China" value. return "China" : 返回对应于China值的monad,所以基本上就是包含"China"值的monad。

Generally speaking I understand a monad as a container for values.一般来说,我将 monad 理解为值的容器。 One usage of monads is to combine simple/existing computations to more complex computations. monad 的一种用法是将简单/现有的计算与更复杂的计算结合起来。

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. Maybe是一个类型构造函数,它接受一个类型参数并返回一个类型,例如Maybe StringMaybe Int等。

data constructors are used to create values of a particular type.数据构造函数用于创建特定类型的值。 For some type Maybe a these constructors are Just and Nothing ie对于某些类型, Maybe a这些构造函数是JustNothing ie

data Maybe a = Just a | Nothing

The return function constructs a monadic value from a 'plain' value eg return函数从“普通”值构造一元值,例如

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

So in the definition of the Monad class所以在Monad类的定义中

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 . m是一个类型构造器,例如 ( IO , Maybe , [] ) 用于构造类型,而return是一个函数,它从 a 类型a值构造ma类型的 monadic 值。

For the monad instance of Maybe , return constructs a value of Maybe a with just ie对于Maybe的 monad 实例, return构造了一个Maybe a的值,只用 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.所以如果你知道你正在处理Maybe你使用哪个并不重要。 However, return has a more general type since it can be used to construct an arbitrary value ma for some monad m .但是, return具有更通用的类型,因为它可以用于为某些 monad m构造任意值ma

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.在 Haskell 中, []是一个类型构造函数。 When applied to a type, say Int , it makes another type [Int] .当应用于一种类型时,比如Int ,它会生成另一种类型[Int]

Incidentally, in Java [] is a type constructor too.顺便说一句,在 Java 中[]也是一个类型构造函数。 It can make a new type Int[] out of existing type Int .它可以从现有类型Int创建一个新类型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 .事实上, []也是一个数据构造函数(不同于拼写为[]的类型构造函数)并且在某些上下文中它等价于return Why do we need return then?那我们为什么需要return呢? return works for any monad and can be used to write generic monadic code that works for any monad. return适用于任何 monad,可用于编写适用于任何 monad 的通用 monadic 代码。 It is a generalization of [] and Just and Left and...它是[]JustLeft和...的概括

In

  (>>=) :: Monad m => m a -> (a -> m b) -> m b
  • m is a constructor of a type that is instance of the Monad class m是一个类型的构造函数,它是Monad类的实例
  • ma is NOT a constructor application but a pattern matching to extract m and a ma不是构造函数应用程序,而是用于提取ma的模式匹配
  • a->mb is a pattern against a function of mb . a->mb是针对mb函数的模式。 Let's call it k .我们称之为k
  • >>= needs to map to what k maps to ('returns'), ie apply ka >>=需要映射到k映射到的('返回'),即应用ka

The implementation from Maybe来自Maybe的实现

    (Just x) >>= k      = k x

In a dodo

  • >>= binds someMonad <- mConstructorSmartOrNot someParam to the next line >>=someMonad <- mConstructorSmartOrNot someParam到下一行
  • >> binds mConstructorSmartOrNot someParam to the next line >>mConstructorSmartOrNot someParam绑定到下一行
    return :: Monad m => a -> m a

return is basically the same as m , but since m can be any constructor (of a value of a variable of an m -constructed type), we can refer to it generally with this one name. return是基本相同m ,但由于m能(一个的变量的值的任何构造m -constructed类型),我们可以参考它通常与这一个名称。

A monad constructs and forwards context. monad 构造并转发上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM