为什么 Haskell monadic bind 是左关联的?

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 的脱糖方式与固定性定义的自然运作方式不同,这令人惊讶。

>>= 肯定是 left-associative.

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

>>= 是 left-associative 因为它很方便。我们希望 m >>= f1 >>= f2 被解析为 (m >>= f1) >>= f2,而不是 m >>= (f1 >>= f2),这可能不会像评论中指出的那样进行类型检查。

然而,>> 的结合性只是 >>= 的镜像。这可能是为了一致性,因为我们可以通过第三个单子定律证明 >> 是结合的:(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-符号de-sugars不同,因为它有不同的目标。本质上,由于 do-notation 本质上是写出一个 lambda,因此需要 right-association。这是因为m >>= (\v -> (...))写成了do {v <- m; (...)}。和前面一样,这里>>的de-sugaring为了前后一致,好像跟在>>=后面。