[英]Why is Haskell monadic bind left-associative?
>>=
和>>
运算符都是infixl 1
。 为什么左关联?
特别是,我观察到等价:
(do a; b; c ) == (a >> (b >> c)) -- Do desugaring
(a >> b >> c) == ((a >> b) >> c) -- Fixity definition
对于固定性定义如何自然地起作用,这样do
是不同的,这是令人惊讶的。
>>=
肯定是左联想的。
Prelude> ["bla","bli di","blub"] >>= words >>= reverse
"albilbidbulb"
Prelude> ["bla","bli di","blub"] >>= (words >>= reverse)
<interactive>:3:30: error:
• Couldn't match expected type ‘[[b0]]’
with actual type ‘String -> [String]’
• Probable cause: ‘words’ is applied to too few arguments
In the first argument of ‘(>>=)’, namely ‘words’
In the second argument of ‘(>>=)’, namely ‘(words >>= reverse)’
In the expression:
["bla", "bli di", "blub"] >>= (words >>= reverse)
并且>>
几乎遵循>>=
; 如果它有另一个固定性,它不仅会像Lennart所说的那样感到怪异 ,它还会阻止你在链中使用这两个运算符:
Prelude> ["bla","bli di","blub"] >>= words >> "Ha"
"HaHaHaHa"
Prelude> infixr 1 ⬿≫; (⬿≫) = (>>)
Prelude> ["bla","bli di","blub"] >>= words ⬿≫ "Ha"
<interactive>:6:1: error:
Precedence parsing error
cannot mix ‘>>=’ [infixl 1] and ‘⬿≫’ [infixr 1] in the same infix expression
>>=
是左关联的,因为它很方便。 我们希望m >>= f1 >>= f2
被解析为(m >>= f1) >>= f2
,而不是m >>= (f1 >>= f2)
,这可能不会像指向那样进行类型检查在评论中。
然而, >>
的相关性只是>>=
的镜像。 这可能是为了保持一致,因为我们可以证明>>
通过第三个monad定律是关联的: (m >>= f) >>= g ≡ m >>= ( \\x -> fx >>= g )
。 也就是说,它的相关性在理论上并不重要。 这是证明:
-- Definition:
a >> b ≡ a >>= (\_ -> b)
-- Proof: (a >> b) >> c ≡ a >> (b >> c)
(a >> b) >> c
≡ (a >>= (\_ -> b)) >> c -- [Definition]
≡ (a >>= (\_ -> b)) >>= (\_ -> c) -- [Definition]
≡ a >>= (\x -> (\_ -> b) x >>= (\_ -> c)) -- [Monad law]
≡ a >>= (\_ -> b >>= (\_ -> c)) -- [Beta-reduction]
≡ a >>= (\_ -> b >> c) -- [Definition]
≡ a >> (b >> c) -- [Definition]
∎
do
-notation不同,因为它有不同的目标。 从本质上讲,由于do-notation本质上是写出lambda,因此需要正确的关联。 这是因为m >>= (\\v -> (...))
写成do {v <- m; (...)}
do {v <- m; (...)}
。 如前所述,为了保持一致性, >>
的脱糖似乎遵循>>=
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.