A simple example showing that IO doesn't satisfy the monad laws?

前端 未结 4 706
野性不改
野性不改 2020-12-23 09:43

I\'ve seen mentioned that IO doesn\'t satisfy the monad laws, but I didn\'t find a simple example showing that. Anybody knows an example? Thanks.

4条回答
  •  無奈伤痛
    2020-12-23 10:09

    m >>= return ≡ m
    

    is broken:

    sequence_ $ take 100000 $ iterate (>>=return) (return ()) :: IO ()
    

    clutters memory and increases computation time, while

    sequence_ $ take 100000 $ iterate (>>=return) (return ()) :: Maybe ()
    

    does not.

    AFAIR there is a Monad Transformer which solves this problem; if I guess right, it is the Codensity Monad Transformer.

提交回复
热议问题