Commit 160639cf authored by Camil Staps's avatar Camil Staps

Merge branch 'add-alternative-instance-to-control.Monad.state' into 'master'

Add alternative instance to Control.Monad.State

See merge request !98
parents 89b34b8e 4b388f9a
Pipeline #8948 passed with stage
in 1 minute and 35 seconds
definition module Control.Monad.State
from Control.Monad import class Monad
from Control.Applicative import class Applicative
from Control.Applicative import class Applicative, class Alternative
from Data.Functor import class Functor
from Data.Functor.Identity import :: Identity
from Control.Monad.Trans import class MonadTrans
......@@ -28,5 +28,6 @@ withStateT :: (s -> s) .(StateT s m c) -> StateT s m c
instance Functor (StateT s m) | Monad m
instance Applicative (StateT s m) | Monad m
instance Alternative (StateT s m) | Alternative m
instance Monad (StateT s m) | Monad m
instance MonadTrans (StateT s)
......@@ -16,6 +16,10 @@ instance Applicative (StateT s m) | Monad m where
pure a = state (\s -> (a, s))
(<*>) sf sa = ap sf sa
instance Alternative (StateT s m) | Alternative m where
empty = StateT (const empty)
(<|>) fa fb = StateT \cs->runStateT fa cs <|> runStateT fb cs
instance Monad (StateT s m) | Monad m where
bind m k = StateT (\s -> (runStateT m s >>= \(a, s`) -> runStateT (k a) s`))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment