0 | module Control.Monad.State.State
2 | import Control.Monad.Identity
3 | import Control.Monad.Trans
11 | record StateT (stateType : Type) (m : Type -> Type) (a : Type) where
13 | runStateT' : stateType -> m (stateType, a)
18 | runStateT : stateType -> StateT stateType m a -> m (stateType, a)
19 | runStateT s act = runStateT' act s
22 | public export %inline
23 | evalStateT : Functor m => stateType -> StateT stateType m a -> m a
24 | evalStateT s = map snd . runStateT s
27 | public export %inline
28 | execStateT : Functor m => stateType -> StateT stateType m a -> m stateType
29 | execStateT s = map fst . runStateT s
33 | public export %inline
34 | mapStateT : (m (s, a) -> n (s, b)) -> StateT s m a -> StateT s n b
35 | mapStateT f m = ST $
f . runStateT' m
43 | State : (stateType : Type) -> (ty : Type) -> Type
44 | State = \s, a => StateT s Identity a
47 | public export %inline
48 | runState : stateType -> State stateType a -> (stateType, a)
49 | runState s act = runIdentity (runStateT s act)
52 | public export %inline
53 | evalState : stateType -> State stateType a -> a
54 | evalState s = snd . runState s
57 | public export %inline
58 | execState : stateType -> State stateType a -> stateType
59 | execState s = fst . runState s
63 | public export %inline
64 | mapState : ((s, a) -> (s, b)) -> State s a -> State s b
65 | mapState f = mapStateT $
\(Id p) => Id (f p)
72 | implementation Functor f => Functor (StateT stateType f) where
73 | map f (ST g) = ST $
\st => map f <$> g st
76 | implementation Monad f => Applicative (StateT stateType f) where
77 | pure x = ST $
\st => pure (st, x)
86 | implementation Monad m => Monad (StateT stateType m) where
93 | implementation MonadTrans (StateT stateType) where
94 | lift x = ST $
\st => (st,) <$> x
97 | implementation (Monad f, Alternative f) => Alternative (StateT st f) where
99 | ST f <|> mg = ST $
\st => f st <|> runStateT' mg st
102 | implementation HasIO m => HasIO (StateT stateType m) where
103 | liftIO io = ST $
\s => liftIO $
io_bind io $
\a => pure (s, a)