Search code examples
haskellmonadscategory-theory

What are the identities categorical monads enforce that Haskell monads don't?


http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html

writes:

If you hadn't guessed, this is about monads as they appear in pure functional programming languages like Haskell. They are closely related to the monads of category theory, but are not exactly the same because Haskell doesn't enforce the identities satisfied by categorical monads.

Are these the identities that the above text is talking about?

Are monad laws enforced in Haskell?

return a >>= k  =  k a
m >>= return  =  m
m >>= (\x -> k x >>= h)  =  (m >>= k) >>= h

Solution

  • Constrasting with the accepted answer to the linked question, consider this instance.

    newtype List a = List [a] deriving (Functor, Applicative)
    
    instance Monad List where
        return _ = List []
        m >>= f = List []
    

    A compiler will accept this definition, but it does not obey the monad laws. Specifically, try to confirm that

    m >>= return == m
    

    for both [] and List:

    -- Correct
    Prelude> [1,2,3] >>= return
    [1,2,3]
    
    -- Not correct, should be List [1,2,3]
    Prelude> List [1,2,3] >>= return
    List []