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

  1. return a >>= k = k a
  2. m >>= return = m
  3. m >>= (\x -> k x >>= h) = (m >>= k) >>= h

results matching ""

    No results matching ""