Monad laws for fail

Ulf Norell ulfn at cs.chalmers.se
Mon Jan 29 10:21:29 EST 2007


I tried out the recently released and very nice binary library and  
tripped up by laziness. Muchly simplified my problem was:

data Bad = Bad

instance Binary Bad where
   put = return ()
   get = do
     fail "this is bad"
     return Bad

Here I expected decoding to Bad to fail, but to my surprise:

ghci> decode (encode Bad) :: Bad
Bad

 From the example, it is clear that my brain has the following law  
for fail built in:

   fail err >> m  ===  fail err

Some testing reveals that this law is violated not only by the Get  
monad, but also by State, Reader, and Writer.
(There was a discussion about the strictness of the state monad a  
while back, I can't recall if this came up...)

So, where am I going wrong? Am I mixing up fail and mzero? Are there  
other laws for fail that actually hold?

/ Ulf


More information about the Libraries mailing list