on master help sign in
The MonadExcept class
5289d99Index Int
ether.cabal+1
@@ -31,6 +31,7 @@ library
31 Control.Monad.Trans.Ether.Except
31 Control.Monad.Trans.Ether.Except
32 Control.Monad.Ether.Reader
32 Control.Monad.Ether.Reader
33 Control.Monad.Ether.State
33 Control.Monad.Ether.State
34+ Control.Monad.Ether.Except
34 Control.Monad.Ether.Implicit.Reader
35 Control.Monad.Ether.Implicit.Reader
35 Control.Monad.Ether.Implicit.State
36 Control.Monad.Ether.Implicit.State
36
37
src/Control/Monad/Ether/Except.hsnew file+109
@@ -0,0 +1,109 @@
1+{-# LANGUAGE MultiParamTypeClasses #-}
2+{-# LANGUAGE TypeFamilies #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE FlexibleContexts #-}
5+{-# LANGUAGE UndecidableInstances #-}
6+{-# LANGUAGE DataKinds #-}
7+{-# LANGUAGE ConstraintKinds #-}
8+{-# LANGUAGE TypeOperators #-}
9+{-# LANGUAGE FunctionalDependencies #-}
10+{-# LANGUAGE ScopedTypeVariables #-}
11+{-# LANGUAGE RankNTypes #-}
12+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13+module Control.Monad.Ether.Except
14+ (
15+ -- * MonadExcept class
16+ MonadExcept(..)
17+ -- * The Except monad
18+ , Except
19+ , runExcept
20+ -- * The ExceptT monad transformer
21+ , ExceptT
22+ , exceptT
23+ , runExceptT
24+ , mapExceptT
25+ ) where
26+
27+import Data.Proxy (Proxy(Proxy))
28+import Control.Monad.Trans (lift)
29+
30+import qualified Control.Monad.Trans.Ether.Reader as Reader
31+import qualified Control.Monad.Trans.Ether.State as State
32+import Control.Monad.Trans.Ether.Except
33+
34+-- for mtl instances
35+import qualified Control.Monad.Trans.Except as Trans (ExceptT(..), runExceptT)
36+import qualified Control.Monad.Trans.Identity as Trans.I
37+import qualified Control.Monad.Trans.List as Trans.L
38+import qualified Control.Monad.Trans.Maybe as Trans.M
39+import qualified Control.Monad.Trans.Reader as Trans.R
40+import qualified Control.Monad.Trans.State.Lazy as Trans.S.Lazy
41+import qualified Control.Monad.Trans.State.Strict as Trans.S.Strict
42+import qualified Control.Monad.Trans.Writer.Lazy as Trans.W.Lazy
43+import qualified Control.Monad.Trans.Writer.Strict as Trans.W.Strict
44+
45+class Monad m => MonadExcept tag e m | m tag -> e where
46+
47+ throw :: proxy tag -> e -> m a
48+
49+ catch :: proxy tag -> m a -> (e -> m a) -> m a
50+
51+instance {-# OVERLAPPING #-} Monad m => MonadExcept tag e (ExceptT tag e m) where
52+ throw proxy = exceptT proxy . return . Left
53+ catch proxy m h
54+ = exceptT proxy
55+ $ runExceptT proxy m
56+ >>= either (runExceptT proxy . h) (return . Right)
57+
58+instance MonadExcept tag e m => MonadExcept tag e (ExceptT tag' e' m) where
59+ throw proxy = lift . throw proxy
60+ catch proxy = liftCatch Proxy (catch proxy)
61+
62+-- Instances for other tagged transformers
63+
64+instance MonadExcept tag e m => MonadExcept tag e (Reader.ReaderT tag' r m) where
65+ throw proxy = lift . throw proxy
66+ catch proxy = Reader.liftCatch Proxy (catch proxy)
67+
68+instance MonadExcept tag e m => MonadExcept tag e (State.StateT tag' s m) where
69+ throw proxy = lift . throw proxy
70+ catch proxy = State.liftCatch Proxy (catch proxy)
71+
72+
73+-- Instances for mtl transformers
74+
75+instance MonadExcept tag e m => MonadExcept tag e (Trans.ExceptT e' m) where
76+ throw proxy = lift . throw proxy
77+ catch proxy m h = Trans.ExceptT $ catch proxy (Trans.runExceptT m) (Trans.runExceptT . h)
78+
79+instance MonadExcept tag e m => MonadExcept tag e (Trans.I.IdentityT m) where
80+ throw proxy = lift . throw proxy
81+ catch proxy = Trans.I.liftCatch (catch proxy)
82+
83+instance MonadExcept tag e m => MonadExcept tag e (Trans.L.ListT m) where
84+ throw proxy = lift . throw proxy
85+ catch proxy = Trans.L.liftCatch (catch proxy)
86+
87+instance MonadExcept tag e m => MonadExcept tag e (Trans.M.MaybeT m) where
88+ throw proxy = lift . throw proxy
89+ catch proxy = Trans.M.liftCatch (catch proxy)
90+
91+instance MonadExcept tag e m => MonadExcept tag e (Trans.R.ReaderT r m) where
92+ throw proxy = lift . throw proxy
93+ catch proxy = Trans.R.liftCatch (catch proxy)
94+
95+instance MonadExcept tag e m => MonadExcept tag e (Trans.S.Lazy.StateT s m) where
96+ throw proxy = lift . throw proxy
97+ catch proxy = Trans.S.Lazy.liftCatch (catch proxy)
98+
99+instance MonadExcept tag e m => MonadExcept tag e (Trans.S.Strict.StateT s m) where
100+ throw proxy = lift . throw proxy
101+ catch proxy = Trans.S.Strict.liftCatch (catch proxy)
102+
103+instance (Monoid w, MonadExcept tag e m) => MonadExcept tag e (Trans.W.Lazy.WriterT w m) where
104+ throw proxy = lift . throw proxy
105+ catch proxy = Trans.W.Lazy.liftCatch (catch proxy)
106+
107+instance (Monoid w, MonadExcept tag e m) => MonadExcept tag e (Trans.W.Strict.WriterT w m) where
108+ throw proxy = lift . throw proxy
109+ catch proxy = Trans.W.Strict.liftCatch (catch proxy)
src/Control/Monad/Ether/Reader.hs+8−3
@@ -22,13 +22,14 @@ module Control.Monad.Ether.Reader
22 , ReaderT
22 , ReaderT
23 , readerT
23 , readerT
24 , runReaderT
24 , runReaderT
25− , mapStateT
25+ , mapReaderT
26 ) where
26 ) where
27
27
28import Data.Proxy (Proxy(Proxy))
28import Data.Proxy (Proxy(Proxy))
29import Control.Monad.Trans (lift)
29import Control.Monad.Trans (lift)
30
30
31−import Control.Monad.Trans.Ether.State (StateT, mapStateT)
31+import Control.Monad.Trans.Ether.State (StateT , mapStateT)
32+import Control.Monad.Trans.Ether.Except (ExceptT, mapExceptT)
32import Control.Monad.Trans.Ether.Reader
33import Control.Monad.Trans.Ether.Reader
33
34
34-- for mtl instances
35-- for mtl instances
@@ -81,7 +82,7 @@ instance {-# OVERLAPPING #-} Monad m => MonadReader tag r (ReaderT tag r m) wher
81 ask proxy = readerT proxy return
82 ask proxy = readerT proxy return
82 local proxy f m = readerT proxy (runReaderT proxy m . f)
83 local proxy f m = readerT proxy (runReaderT proxy m . f)
83
84
84−instance (MonadReader tag r m) => MonadReader tag r (ReaderT tag' r' m) where
85+instance MonadReader tag r m => MonadReader tag r (ReaderT tag' r' m) where
85 ask proxy = lift (ask proxy)
86 ask proxy = lift (ask proxy)
86 local proxy = mapReaderT Proxy . local proxy
87 local proxy = mapReaderT Proxy . local proxy
87
88
@@ -91,6 +92,10 @@ instance (MonadReader tag r m) => MonadReader tag r (StateT tag' s m) where
91 ask proxy = lift (ask proxy)
92 ask proxy = lift (ask proxy)
92 local proxy = mapStateT Proxy . local proxy
93 local proxy = mapStateT Proxy . local proxy
93
94
95+instance (MonadReader tag r m) => MonadReader tag r (ExceptT tag' e m) where
96+ ask proxy = lift (ask proxy)
97+ local proxy = mapExceptT Proxy . local proxy
98+
94-- Instances for mtl transformers
99-- Instances for mtl transformers
95
100
96instance MonadReader tag r m => MonadReader tag r (Trans.ContT r' m) where
101instance MonadReader tag r m => MonadReader tag r (Trans.ContT r' m) where
src/Control/Monad/Ether/State.hs+7−1
@@ -33,6 +33,7 @@ module Control.Monad.Ether.State
33import Control.Monad.Trans (lift)
33import Control.Monad.Trans (lift)
34
34
35import Control.Monad.Trans.Ether.Reader (ReaderT)
35import Control.Monad.Trans.Ether.Reader (ReaderT)
36+import Control.Monad.Trans.Ether.Except (ExceptT)
36import Control.Monad.Trans.Ether.State
37import Control.Monad.Trans.Ether.State
37
38
38-- for mtl instances
39-- for mtl instances
@@ -79,7 +80,7 @@ instance {-# OVERLAPPING #-} Monad m => MonadState tag s (StateT tag s m) where
79 get proxy = stateT proxy (\s -> return (s, s))
80 get proxy = stateT proxy (\s -> return (s, s))
80 put proxy s = stateT proxy (\_ -> return ((), s))
81 put proxy s = stateT proxy (\_ -> return ((), s))
81
82
82−instance (MonadState tag s m) => MonadState tag s (StateT tag' s' m) where
83+instance MonadState tag s m => MonadState tag s (StateT tag' s' m) where
83 get proxy = lift (get proxy)
84 get proxy = lift (get proxy)
84 put proxy = lift . put proxy
85 put proxy = lift . put proxy
85 state proxy = lift . state proxy
86 state proxy = lift . state proxy
@@ -91,6 +92,11 @@ instance (MonadState tag s m) => MonadState tag s (ReaderT tag' r m) where
91 put proxy = lift . put proxy
92 put proxy = lift . put proxy
92 state proxy = lift . state proxy
93 state proxy = lift . state proxy
93
94
95+instance (MonadState tag s m) => MonadState tag s (ExceptT tag' e m) where
96+ get proxy = lift (get proxy)
97+ put proxy = lift . put proxy
98+ state proxy = lift . state proxy
99+
94-- Instances for mtl transformers
100-- Instances for mtl transformers
95
101
96instance MonadState tag s m => MonadState tag s (Trans.ContT r m) where
102instance MonadState tag s m => MonadState tag s (Trans.ContT r m) where
src/Control/Monad/Trans/Ether/Except.hs+1
@@ -20,6 +20,7 @@ module Control.Monad.Trans.Ether.Except
20 , liftCallCC
20 , liftCallCC
21 , liftListen
21 , liftListen
22 , liftPass
22 , liftPass
23+ , liftCatch
23 ) where
24 ) where
24
25
25import Data.Proxy (Proxy(Proxy))
26import Data.Proxy (Proxy(Proxy))