Skip to content

Commit b309a62

Browse files
committed
Add some standard functions on state transformers
1 parent ae1c193 commit b309a62

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Control/Monad/Indexed/State.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE RecordWildCards #-}
12
-----------------------------------------------------------------------------
23
-- |
34
-- Module : Control.Monad.Indexed.State
@@ -90,6 +91,18 @@ instance IxMonadFix IxState where
9091

9192
newtype IxStateT m i j a = IxStateT { runIxStateT :: i -> m (a, j) }
9293

94+
-- | Lift a computation from the inner monad.
95+
lift :: Monad m => m a -> IxStateT m i i a
96+
lift ma = IxStateT $ (\i -> ma >>= \a -> return (a, i))
97+
98+
-- | Evaluate a computation in the monad from some initial state. Returns the resulting value.
99+
evalIxStateT :: Monad m => IxStateT m i j a -> i -> m a
100+
evalIxStateT IxStateT{..} i = runIxStateT i >>= return . fst
101+
102+
-- | Evaluate a computation in the monad from some initial state. Returns the final state.
103+
execIxStateT :: Monad m => IxStateT m i j a -> i -> m j
104+
execIxStateT IxStateT{..} i = runIxStateT i >>= return . snd
105+
93106
instance Monad m => Functor (IxStateT m i j) where
94107
fmap = imap
95108

0 commit comments

Comments
 (0)