0 | module Control.Monad.Indexed.State
2 | import Control.Functor.Indexed
3 | import Control.Applicative.Indexed
4 | import Control.Monad.Indexed
5 | import Control.Monad.Indexed.Do
14 | record IndexedStateT stateType x y (m : Type -> x -> y -> Type) a (i : x) (j : y) where
16 | runStateT' : stateType -> m (stateType, a) i j
20 | runStateT : stateType -> IndexedStateT stateType x y m a i j -> m (stateType, a) i j
21 | runStateT s act = runStateT' act s
25 | evalStateT : IndexedFunctor x y m => stateType -> IndexedStateT stateType x y m a i j -> m a i j
26 | evalStateT s = map snd . runStateT s
30 | execStateT : IndexedFunctor x y m => stateType -> IndexedStateT stateType x y m a i j -> m stateType i j
31 | execStateT s = map fst . runStateT s
35 | mapStateT : ({0 i : x} -> {0 j : y} -> m (s, a) i j -> n (s, b) i j) -> IndexedStateT s x y m a i j -> IndexedStateT s x y n b i j
36 | mapStateT f m = ST $
f . runStateT' m
39 | lift : IndexedFunctor x y m => m a i j -> IndexedStateT stateType x y m a i j
40 | lift z = ST $
\st => map (st,) z
42 | implementation IndexedFunctor x y f => IndexedFunctor x y (IndexedStateT stateType x y f) where
43 | map f' (ST runStateT') = ST $
\st => map (mapSnd f') $
runStateT' st
46 | IndexedMonad z m => IndexedApplicative z (IndexedStateT stateType z z m) where
47 | pure x = ST $
\st => pure (st, x)
49 | (ST ff) `ap` (ST fx) = ST $
\st =>
50 | ff st >>>= \(st', f) =>
51 | fx st' >>>= \(st'', x) =>
55 | IndexedMonad z m => IndexedMonad z (IndexedStateT stateType z z m) where
56 | bind (ST f) g = ST $
\st =>
57 | f st >>>= \(st', x) =>
61 | get : IndexedApplicative z m => IndexedStateT stateType z z m stateType i i
62 | get = ST $
\st => pure (st, st)
65 | gets : IndexedApplicative z m => (stateType -> a) -> IndexedStateT stateType z z m a i i
66 | gets f = ST $
\st => pure (st, f st)
69 | put : IndexedApplicative z m => stateType -> IndexedStateT stateType z z m () i i
70 | put st = ST $
\_ => pure (st, ())
73 | modify : IndexedMonad z m => (stateType -> stateType) -> IndexedStateT stateType z z m () i i