简体   繁体   English

在数据类型构造函数的上下文中了解不同的monadic和应用绑定/组合器?

[英]Understanding different monadic and applicative binds/combinators in the context of a data type constructor?

I've been given the following piece of code, which I am trying to get my head around: 我已经得到了以下代码,我正在努力使自己的头脑:

data MyExample e i = MyExample (CustomMonad e i)
   | forall b. MyExample e b :>>= (b -> CustomMonad e b)
   | forall b. (MyExample e (b -> a)) :<*> (MyExample e b)
   | forall b. (b -> a) :<$> (MyExample e b)

1) What do :>>= , :<*> and :<$> do differently, as opposed to 1) :>>= :<*>:<$>什么不同,而不是

Monadic bind >>= 单声道绑定>>=

exampleFunction :: Int -> Maybe Int
exampleFunction el = Just (el + 100)
main = do 
  result <- exampleFunction >>= exampleFunction 21

Applicative combinators <*> and <$> 适用的组合器<*><$>

exampleFunction :: Int -> Maybe Int
exampleFunction el = Just el
main = do 
 result <- pure exampleFunction <$> (+) <*> (ExampleType 2) <*> (ExampleType 4)

2) Am I right in saying the following: 2)我说的对吗?

a) MyExample (CustomMonad ei) is constructing the CustomMonad type with e and i, then wrapping this in the MyExample context? a) MyExample (CustomMonad ei)正在使用e和i构造CustomMonad类型,然后将其包装在MyExample上下文中?

b) forall b. MyExample eb :>>= (b -> CustomMonad eb) b) forall b. MyExample eb :>>= (b -> CustomMonad eb) forall b. MyExample eb :>>= (b -> CustomMonad eb) is taking a (MyExample ei) then taking b and inputting this into a function ( b -> CustomMonad eb ) that constructs a (CustomMonad eb) ? forall b. MyExample eb :>>= (b -> CustomMonad eb)取一个(MyExample ei)然后取b并将其输入到构造一个(CustomMonad eb)的函数( b -> CustomMonad eb )中?

c) forall b. (MyExample e (b -> a)) :<*> (MyExample eb) c) forall b. (MyExample e (b -> a)) :<*> (MyExample eb) forall b. (MyExample e (b -> a)) :<*> (MyExample eb) is taking a MyExample constructed with a value e and a function (b -> a) and perform some kind of applicative combinator operation with a MyExample eb ? forall b. (MyExample e (b -> a)) :<*> (MyExample eb)正在使用由值e和函数(b -> a)构造的MyExample ,并使用MyExample eb执行某种应用组合器操作。

d) forall b. (b -> a) :<$> (MyExample eb) d) forall b. (b -> a) :<$> (MyExample eb) forall b. (b -> a) :<$> (MyExample eb) is passing some result from (MyExample eb) to a function (b -> a) forall b. (b -> a) :<$> (MyExample eb)将结果从(MyExample eb)传递给函数(b -> a)

Additionally, am I right in saying the use of forall b is ensuring that b is the same type throughout the operation? 另外,我是说对forall b来确保b在整个操作中是同一类型吗?

:>>= doesn't actually do anything with its arguments; :>>=实际上并不对其参数做任何事情; it simply creates a new value of type MyExample ei that you could later interpret as a call to >>= , but you could do practically anything you want with it using a different interpreter. 它只是创建一个MyExample ei类型的新值,您以后可以将其解释为对>>=的调用,但实际上,您可以使用其他解释器来对其进行任何处理。

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

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