iteratee Iteratee breaks monadic laws

Michael Baikov manpacket at gmail.com
Wed Feb 15 04:47:04 GMT 2012


There are three known monadic laws which every monad must obey:

"Left identity":      return a >>= f  ≡  f a
"Right identity":    m >>= return  ≡  m
"Associativity":    (m >>= f) >>= g  ≡  m >>= (\x -> f x >>= g)

Iteratee breaks the third one: "do { a; b; c }" gets desugared to this:
"a >> (b >> c)" plain usage of "a >> b >> c" is the same as "(a >> b) >> c",
since (>>)  is defined with infixl.

Here is a simple example which works with do notation, but fails to
work with (>>)

import qualified Data.Iteratee  as I
import Data.Iteratee.Iteratee

inject :: Int -> Iteratee [Int] IO ()
inject x = idone () (Chunk [x])

test' :: Iteratee [Int] IO [Int]
test' = do
    inject 2
    inject 1
    inject 0
    I.stream2list

test'' :: Iteratee [Int] IO [Int]
test'' = inject 2 >> inject 1 >> inject 0 >> I.stream2list

main = do
    run test' >>= print  -- [0, 1, 2]
    run test'' >>= print -- [2]



More information about the Iteratee mailing list