Hacker News new | ask | show | jobs
by tel 1990 days ago
The trivial implementation isn't law abiding. This law doesn't hold (written in Kleisli form for simplicity)

    pure >=> f == f  (left identity)
There are no other monads for Maybe. First, any definition of pure must be Just as Nothing doesn't work because of left identity and parametricity prevents any other funny business. Now, by law we know

    pure a >>= f == f a
Thus, we must define

    Just a >>= f = f a
So the only variable is what (Nothing >>= f) does. For (f: A -> B) we must end up with a Maybe B. We don't have one to start and we can produce Maybe values only via Nothing and Just. So, either >>= is the standard definition or we have to do

    Nothing >>= f = Just (_: B)     -- we can achieve a B only via use of f, so
    Nothing >>= f = Just (f (_: A)) -- now we are stuck, there are no values of A
Thus, we must define

    pure a = Just a
    
    Just a >>= f  = f a
    Nothing >>= f = Nothing
1 comments

yeah, and as others pointed out it fails right-identity as well. had a brain fart recapping the laws, though i think i got associativity right at least!