[英]Bind operator for non-monadic functions
I more or less wrapped my head around monads, but i can't deduct how expression我或多或少地把头绕在单子上,但我无法推断出表达方式
(>>=) id (+) 3
evaluates to 6. It just seems the expression somehow got simplified to计算结果为 6。似乎表达式以某种方式简化为
(+) 3 3
but how?但如何? How is 3 applied twice?
3如何应用两次? Could someone explain what is happening behind the scenes?
有人可以解释幕后发生的事情吗?
This follows from how >>=
is defined for the ((->) r)
types:这遵循如何为
((->) r)
类型定义>>=
:
(f =<< g) x = f (g x) x
Thus因此
(>>=) id (+) 3
=
(id >>= (+)) 3
=
((+) =<< id) 3
=
(+) (id 3) 3
=
3 + 3
see the types:查看类型:
> :t let (f =<< g) x = f (g x) x in (=<<)
let (f =<< g) x = f (g x) x in (=<<)
:: (t1 -> (t2 -> t)) -> (t2 -> t1) -> (t2 -> t)
> :t (=<<)
(=<<) :: Monad m => (a -> m b) -> m a -> m b
The types match with类型匹配
t1 ~ a
(t2 ->) ~ m -- this is actually ((->) t2)`
t ~ b
Thus the constraint Monad m
here means Monad ((->) t2)
, and that defines the definition of =<<
and >>=
which get used.因此,这里的约束
Monad m
表示Monad ((->) t2)
,它定义了=<<
和>>=
的定义,它们会被使用。
If you want to deduce the definition from the type,如果你想从类型推导出定义,
(>>=) :: Monad m => m a -> (a -> m b) -> m b
m ~ ((->) r)
(>>=) :: (r -> a) -> (a -> r -> b) -> (r -> b)
(>>=) f g r = b
where
a = f r
rb = g a
b = rb r
which after the simplification becomes the one we used above.简化后成为我们上面使用的那个。
And if you want to understand it "with words",如果你想“用文字”来理解它,
(=<<) :: (Monad m, m ~ ((->) r)) => (a -> m b) -> m a -> m b
(f =<< g) x = f (g x) x
g
is a "monadic value" that " can compute" an " a
", represented as r -> a
g
是“可以计算”“ a
”的“一元值”,表示为r -> a
fa
calculates a "monadic value" that " can compute" a " b
", represented as r -> b
, fa
计算“可以计算”a“ b
”的“一元值”,表示为r -> b
,\x -> f (gx) x
is a monadic value that " can compute" a " b
", given an " r
".\x -> f (gx) x
是一个“可以计算”a“ b
”的一元值,给定一个“ r
”。 So these "non-monadic functions" are, in fact, monadic values, which happen to be functions.
所以这些“非单子函数”实际上是单子值,它们恰好是函数。
Thus in your example, g = id
, f = (+)
, and因此,在您的示例中,
g = id
、 f = (+)
和
id
is a "monadic value" that " can compute" an " a
", an a -> a
id
是一个“一元值”,“可以计算”一个“ a
”,一个a -> a
(+) a
calculates a "monadic value" that " can compute" a " b
", an a -> b
, which b
is actually also an a
, (+) a
计算一个“可以计算” a“ b
”的“一元值”,一个a -> b
,其中b
实际上也是一个a
,\x -> (+) (id x) x
is a monadic value that " can compute" an " a
", given an " a
":\x -> (+) (id x) x
是一个“可以计算”一个“ a
”的一元值,给定一个“ a
”:(>>=) id (+)
=
((+) =<< id)
=
\x -> (+) (id x) x
=
\x -> (+) x x
Adding some colour to Will's excellent answer.为威尔的出色回答增添了一些色彩。
If we look at the source , we have this:如果我们查看source ,我们有这个:
instance Monad ((->) r) where f >>= k = \ r -> k (fr) r
If we rearrange the input expression slightly, we get (id >>= (+)) 3
.如果我们稍微重新排列输入表达式,我们会得到
(id >>= (+)) 3
。 This now resembles the form shown above.这现在类似于上面显示的形式。 Now fitting the input into the above 'template', we can rewrite the input as
\ r -> (+) (id r) r
现在将输入拟合到上述“模板”中,我们可以将输入重写为
\ r -> (+) (id r) r
This is the same expression we arrived at for the final evaluation ie (+) (id 3) 3
这与我们为最终评估得出的表达式相同,即
(+) (id 3) 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.