简体   繁体   English

为什么Haskell monadic绑定左关联?

[英]Why is Haskell monadic bind left-associative?

The >>= and >> operators are both infixl 1 . >>=>>运算符都是infixl 1 Why the left-associativity? 为什么左关联?

In particular, I observe the equivalences: 特别是,我观察到等价:

(do a; b; c ) == (a >> (b >> c))   -- Do desugaring
(a >> b >> c) == ((a >> b) >> c)   -- Fixity definition

So do is desugared differently to how the fixity definition naturally works, which is surprising. 对于固定性定义如何自然地起作用,这样do是不同的,这是令人惊讶的。

>>= must surely be 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)

And >> pretty much follows >>= ; 并且>>几乎遵循>>= ; if it had another fixity it would not only feel weird as Lennart said, it would also prevent you from using both operators in a chain: 如果它有另一个固定性,它不仅像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

>>= is left-associative because it's convenient. >>=是左关联的,因为它很方便。 We want m >>= f1 >>= f2 to be parsed as (m >>= f1) >>= f2 , not as m >>= (f1 >>= f2) , which would likely not type check, as pointed out in the comments. 我们希望m >>= f1 >>= f2被解析为(m >>= f1) >>= f2 ,而不是m >>= (f1 >>= f2) ,这可能不会像指向那样进行类型检查在评论中。

The associativity of >> however, is simply a mirror of >>= . 然而, >>的相关性只是>>=的镜像。 This is likely for the sake of consistency, since we can prove that >> is associative via the third monad law: (m >>= f) >>= g ≡ m >>= ( \\x -> fx >>= g ) . 这可能是为了保持一致,因为我们可以证明>>通过第三个monad定律是关联的: (m >>= f) >>= g ≡ m >>= ( \\x -> fx >>= g ) That is to say, its associativity doesn't theoretically matter. 也就是说,它的相关性在理论上并不重要。 Here is the proof: 这是证明:

-- 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 de-sugars differently because it has a different goal. do -notation不同,因为它有不同的目标。 Essentially, since do-notation is essentially writing out a lambda, right-association is needed. 从本质上讲,由于do-notation本质上是写出lambda,因此需要正确的关联。 This is because m >>= (\\v -> (...)) is written as do {v <- m; (...)} 这是因为m >>= (\\v -> (...))写成do {v <- m; (...)} do {v <- m; (...)} . do {v <- m; (...)} As earlier, the de-sugaring of >> here seems to follow >>= for the sake of consistency. 如前所述,为了保持一致性, >>的脱糖似乎遵循>>=

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

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