Commit 4b388f9a authored by Mart Lubbers's avatar Mart Lubbers

Add alternative instance to Control.Monad.State

parent 89b34b8e
Pipeline #8947 passed with stage
in 1 minute and 38 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