aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD/Control
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/JLD/Control')
-rw-r--r--src/Data/JLD/Control/Monad/RES.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/Data/JLD/Control/Monad/RES.hs b/src/Data/JLD/Control/Monad/RES.hs
new file mode 100644
index 0000000..1c96d46
--- /dev/null
+++ b/src/Data/JLD/Control/Monad/RES.hs
@@ -0,0 +1,35 @@
1module Data.JLD.Control.Monad.RES (
2 REST,
3 runREST,
4 evalREST,
5 withEnvRES,
6 withErrorRES,
7 withErrorRES',
8 withStateRES,
9) where
10
11import Data.JLD.Prelude
12
13import Control.Monad.Except (mapExceptT)
14
15type REST r e s m = ReaderT r (ExceptT e (StateT s m))
16
17runREST :: r -> s -> REST r e s m a -> m (Either e a, s)
18runREST env st = flip runReaderT env .> runExceptT .> flip runStateT st
19
20evalREST :: Monad m => r -> s -> REST r e s m a -> m (Either e a)
21evalREST env st = flip runReaderT env .> runExceptT .> flip evalStateT st
22
23withEnvRES :: (r -> r') -> REST r' e s m a -> REST r e s m a
24withEnvRES fn (ReaderT m) = ReaderT <| fn .> m
25
26withErrorRES :: Functor m => (e' -> e) -> REST r e' s m a -> REST r e s m a
27withErrorRES fn (ReaderT m) = ReaderT <| m .> mapExceptT (fmap <| first fn)
28
29withErrorRES' :: Monad m => (e' -> REST r e s m a) -> REST r e' s m a -> REST r e s m a
30withErrorRES' fn (ReaderT m) =
31 ReaderT <| \r -> m r |> mapExceptT \m' -> m' >>= either (fn .> flip runReaderT r .> runExceptT) (Right .> pure)
32
33withStateRES :: Monad m => (s -> s') -> (s -> s' -> s) -> REST r e s' m a -> REST r e s m a
34withStateRES fin fout (ReaderT m) =
35 ReaderT \env -> m env |> mapExceptT \st -> StateT \s -> second (fout s) <$> runStateT st (fin s)