Why is the monad method not used for `>>`, defined as `flip const`?

Is there any reason why Prelude doesn't define a list monad like that? (Note the custom implementation >>.)

instance  Monad []  where
    m >>= k          = concat (map k m)
    m >> k           = k                 -- a.k.a. flip const
    return x         = [x]
    fail s           = []

I tried to test it against the laws of the monad, but they do not mention >>. The class definition Monadis as follows:

m >> k = m >>= \_ -> k

which in the instance []would translate to this:

concat (map (\_ -> k) m)

which, of course, is not the same flip const- they give is obviously different results, for example [1..5] >> return 1. But it is not clear to me whether this definition by default is the law that the instances must comply with Monad, or is it just a default implementation that satisfies another law that would also satisfy the implementation flip const.

, ( " " ), , >> , , . , , . , flip const ?

: ehird answer , , [] >> k, [], k. , , :

[] >> k = []
_ >> k = k
+3
3

a >> b a >>= const b; Monad, ( ) . : , (, fail).

, , , , (>>) Monad.

, , (>>) . [] , [] >> m []; , ! , :

do { m; ... }
do { _ <- m; ... }

, (>>), - (>>=). (. Haskell 2010.)

+14

ma >> mb ma >>= \_ -> mb. >> flip const , ma >> mb ma, ma - , .

, flip const, >> , , .

+4

Your definition >>violates the law of associativity Monad:

newtype B a = B { unB :: [a] }

instance Monad B where
  m >>= f = B . concatMap (unB.f) $ unB m
  (>>) = flip const
  return a = B [a]

case1 = B [1,2,3] >>= (\_ -> B [4,5,6] >> return 1)
case2 = (B [1,2,3] >>= \_ -> B [4,5,6]) >> return 1

main = do
  print $ unB case1
  print $ unB case2

The two cases above are distinguished by their associativity, but give different results.

+2
source

All Articles