Overview
- 2 basic operations
bind
return
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
do pattern <- exp
morelines
-- converted to:
exp >>= (\pattern -> do morelines)
do return exp
-- simplified to:
return exp
Parser Monad
data Parser a = P (String -> [(a, String)])
instance Monad Parser where
return v = P (\inp -> [(v, inp)])
p >>= f = P (\inp -> case parse p inp of
[] -> []
[(v, out)] -> parse (f v) out)
Monad laws
return a >>= k = k a
m >>= return = m
m >>= (\x -> k x >>= h) = (m >>= k) >>= h