>>=
和>>
运算符都是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)
>>
几乎遵循>>=
;如果它有另一个固定性,它不仅会像伦纳特所说的那样感觉很奇怪,还会阻止你在链中使用两个运算符:
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 -> f x >>= 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
符号脱糖的方式不同,因为它有不同的目标。从本质上讲,由于do符号本质上是写出一个lambda,因此需要右关联。这是因为m >>= (v -> (...))
写成do {v <- m; (...)}
.如前所述,为了保持一致性,这里>>
的脱糖似乎遵循>>=
。