f(3)
return?
f(3)
return?
f
has the same type as in the previous example.
g()
return?
f
has the same type as in the previous examples.
newPage
changes the state
(has both extra input and extra output).
startNewPage
changes the state and performs IO operations.
f
has the same type as in the previous examples.
Pure function: | Input -> Output |
Extra input: | Input -> Extra -> Output |
Extra output: | Input -> (Extra,Output) |
Changing the state: | Input -> State -> (State,Output) |
IO operations: | Input -> IO Output |
Sometimes no result: | Input -> Maybe Output |
Many results: | Input -> [Output] |
h
that combines two functions
f
and g
that both have additional effects.
-- Pure functions: h x = g (f x) -- Extra input: h x e = g (f x e) e
-- Extra output h x = (o1<>o2,y2) where (o1,y1) = f x (o2,y2) = g y1 -- Changing the state: h x s0 = (s2,y2) where (s1,y1) = f x s0 (s2,y2) = g y1 s1
-- IO operations: h x = do y1 <- f x y2 <- g y1 return y2 -- Sometimes no result: h x = case f x of Nothing -> Nothing Just y -> g y -- Many results: h x = [y2 | y1 <- f x, y2 <- g y1]
h x = g (f x)
h x = (o1<>o2,y2) where (o1,y1) = f x (o2,y2) = g y1
=<<
h x = g =<< f x
h x = g (f x)
h x = (o1<>o2,y2) where (o1,y1) = f x (o2,y2) = g y1
=<<
h x = g =<< f x
g =<< fx = (o1<>o2,y2) where (o1,y1) = fx (o2,y2) = g y1
=<<
=<<
Type of effect | Type |
---|---|
Pure function: | Input -> Output |
Extra input: | Input -> Extra -> Output |
Extra output: | Input -> (Extra,Output) |
Changing the state: | Input -> State -> (State,Output) |
IO operations: | Input -> IO Output |
Sometimes no result: | Input -> Maybe Output |
Many results: | Input -> [Output] |
Type of effect | The common pattern | The difference |
---|---|---|
Pure function: | Input -> M Output | M a a |
Extra input: | Input -> M Output | M a Extra -> a |
Extra output: | Input -> M Output | M a (Extra,a) |
Changing the state: | Input -> M Output | M a State->(State,a) |
IO operations: | Input -> M Output | M a IO a |
Sometimes no result: | Input -> M Output | M a Maybe a |
Many results: | Input -> M Output | M a [a] |
Input -> M Output
M
M
(=<<) :: (a -> M b) -> M a -> M b
($) :: (a -> b) -> a -> b
=<<
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a->m b) -> m b
-- ...
(=<<) :: Monad m => (a->m b) -> m a -> m b
f=<<x = x>>=f
m
do
>>=
do x <- m1; y <- m2 x; return (x+y)
m1 >>= (\x -> do y <- m2 x; return (x+y))
m1 >>= (\x -> m2 x >>= (\y -> return (x+y)))
instance Monad IO -- predefined instance Monad Gen -- from QuickCheck instance Monad Parser -- from the Parsing module
instance Monad Maybe where return x = Just x Just x >>= f = f x Nothing >>= _ = Nothing
return :: a -> Maybe a (>>=) :: Maybe a -> (a->Maybe b) -> Maybe b
instance Monad [] where return x = [x] xs >>= f = [y | x<-xs, y<-f x]
return :: a -> [a] (>>=) :: [a] -> (a->[b]) -> [b]
=<<
do
Operator | Function | Argument | Result |
---|---|---|---|
$ | a->b | a | b |
<$> | a->b | m a | m b |
<*> | m (a->b) | m a | m b |
=<< | a->m b | m a | m b |
<$>
<*>
class Functor f where fmap :: (a->b) -> f a -> f b f <$> x = fmap f x
map
map :: (a->b) -> [a] -> [b] fmap :: Functor f => (a->b) -> f a -> f b
instance Functor IO
instance Functor Gen -- from QuickCheck
instance Functor Parser
fmap :: (a->b) -> IO a -> IO b fmap :: (a->b) -> Gen a -> Gen b fmap :: (a->b) -> Parser a -> Parser b
instance Functor Maybe where fmap f Nothing = Nothing fmap f (Just x) = Just (f x)
fmap :: (a->b) -> Maybe a -> Maybe b
instance Functor [] where fmap = map
fmap :: (a->b) -> [a] -> [b]
class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a->b) -> f a -> f b
Applicative
Monad
<*>
=<<
pure
return
instance Applicative IO
instance Applicative Gen -- from QuickCheck
instance Applicative Parser
(<*>) :: IO (a->b) -> IO a -> IO b (<*>) :: Gen (a->b) -> Gen a -> Gen b (<*>) :: Parser (a->b) -> Parser a -> Parser b
instance Applicative [] where pure x = [x] fs <*> xs = [f x | f<-fs, x<-xs]
pure :: a -> [a] (<*>) :: [a->b] -> [a] -> [b]
instance Applicative Maybe where pure x = Just x Just f <*> Just x = Just (f x) _ <*> _ = Nothing
pure :: a -> Maybe a (<*>) :: Maybe (a->b) -> Maybe a -> Maybe b
rCard :: Gen Card rCard = do s <- rSuit r <- rRank return (Card s r)
rCard = Card <$> rSuit <*> rRand
oneOrMore :: Parser item -> Parser [item] oneOrMore item = do i <- item is <- zeroOrMore item return (i:is)
oneOrMore item = (:) <$> item <*> zeroOrMore item
Applicative
Monad
class Applicative m => Monad m where -- ...
Monad
Applicative
Functor
pure
return
(<*>)
ap
fmap
liftM