Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should bind result in a Monad?

Tags:

haskell

monads

The following is an example from a Haskell tutorial

instance Monad Maybe where  
    return x = Just x  
    Nothing >>= f = Nothing  
    Just x >>= f  = f x  
    fail _ = Nothing  

However, I am confused by the Just x... line. Should the result not be a monad? I would expect the line to be

Just x >>= f = Just (f x)
like image 315
cammil Avatar asked Jan 05 '23 13:01

cammil


1 Answers

Yes! You're right about the result being a monad, but remember the type of the >>= operator: m a -> (a -> m b) -> m b. Then, we guess that f has type a -> m b, so applying f to x returns a monad as a result.

like image 74
Hackerman Avatar answered Jan 07 '23 02:01

Hackerman