@@ -29,9 +29,6 @@ module Control.Monad.Trans.Ether.State
29 , liftCallCC'
29 , liftCallCC'
30 , liftListen
30 , liftListen
31 , liftPass
31 , liftPass
32− -- * Wrapping operations
37import Data.Proxy (Proxy(Proxy))
34import Data.Proxy (Proxy(Proxy))
@@ -42,7 +39,7 @@ import Control.Monad (MonadPlus)
42import Control.Monad.Fix (MonadFix)
39import Control.Monad.Fix (MonadFix)
43import Control.Monad.Trans.Class (MonadTrans, lift)
40import Control.Monad.Trans.Class (MonadTrans, lift)
44import Control.Monad.IO.Class (MonadIO)
41import Control.Monad.IO.Class (MonadIO)
45−import Control.Ether.Tags (Tags)
42+import Control.Ether.Tags (Taggable(..), Tagged(..), Tags)
47import qualified Control.Monad.Signatures as Sig
44import qualified Control.Monad.Signatures as Sig
48import qualified Control.Monad.Trans.State.Lazy as Trans
45import qualified Control.Monad.Trans.State.Lazy as Trans
@@ -72,84 +69,86 @@ newtype StateT tag s m a = StateT (Trans.StateT s m a)
72 deriving ( Functor, Applicative, Alternative, Monad, MonadPlus
69 deriving ( Functor, Applicative, Alternative, Monad, MonadPlus
73 , MonadFix, MonadTrans, MonadIO )
70 , MonadFix, MonadTrans, MonadIO )
75−type instance Tags (StateT tag r m) = tag ': Tags m
77−tagWrap :: proxy tag -> Trans.StateT s m a -> StateT tag s m a
73+instance Taggable (StateT tag s m) where
78−tagWrap _ = StateT
74+ type Tag (StateT tag s m) = 'Just tag
75+ type Tags' (StateT tag s m) = Tags m
77+instance Tagged (StateT tag s m) tag where
80−tagUnwrap :: proxy tag -> StateT tag s m a -> Trans.StateT s m a
78+ type Untagged (StateT tag s m) = Trans.StateT s m
81−tagUnwrap _ (StateT a) = a
80+ untagged _ (StateT a) = a
83-- | Constructor for computations in the state monad transformer.
82-- | Constructor for computations in the state monad transformer.
84stateT :: proxy tag -> (s -> m (a, s)) -> StateT tag s m a
83stateT :: proxy tag -> (s -> m (a, s)) -> StateT tag s m a
85−stateT t = tagWrap t . Trans.StateT
84+stateT t = tagged t . Trans.StateT
87-- | Constructor for computations in the state monad
86-- | Constructor for computations in the state monad
88-- (the inverse of 'runState').
87-- (the inverse of 'runState').
89state :: Monad m => proxy tag -> (s -> (a, s)) -> StateT tag s m a
88state :: Monad m => proxy tag -> (s -> (a, s)) -> StateT tag s m a
90−state t = tagWrap t . Trans.state
89+state t = tagged t . Trans.state
92-- | Runs a 'StateT' with the given initial state
91-- | Runs a 'StateT' with the given initial state
93-- and returns both the final value and the final state.
92-- and returns both the final value and the final state.
94runStateT :: proxy tag -> StateT tag s m a -> s -> m (a, s)
93runStateT :: proxy tag -> StateT tag s m a -> s -> m (a, s)
95−runStateT t = Trans.runStateT . tagUnwrap t
94+runStateT t = Trans.runStateT . untagged t
97-- | Runs a 'StateT' with the given initial state
96-- | Runs a 'StateT' with the given initial state
98-- and returns the final value, discarding the final state.
97-- and returns the final value, discarding the final state.
99evalStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m a
98evalStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m a
100−evalStateT t = Trans.evalStateT . tagUnwrap t
99+evalStateT t = Trans.evalStateT . untagged t
102-- | Runs a 'StateT' with the given initial state
101-- | Runs a 'StateT' with the given initial state
103-- and returns the final state, discarding the final value.
102-- and returns the final state, discarding the final value.
104execStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m s
103execStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m s
105−execStateT t = Trans.execStateT . tagUnwrap t
104+execStateT t = Trans.execStateT . untagged t
107-- | Runs a 'State' with the given initial state
106-- | Runs a 'State' with the given initial state
108-- and returns both the final value and the final state.
107-- and returns both the final value and the final state.
109runState :: proxy tag -> State tag s a -> s -> (a, s)
108runState :: proxy tag -> State tag s a -> s -> (a, s)
110−runState t = Trans.runState . tagUnwrap t
109+runState t = Trans.runState . untagged t
112-- | Runs a 'State' with the given initial state
111-- | Runs a 'State' with the given initial state
113-- and returns the final value, discarding the final state.
112-- and returns the final value, discarding the final state.
114evalState :: proxy tag -> State tag s a -> s -> a
113evalState :: proxy tag -> State tag s a -> s -> a
115−evalState t = Trans.evalState . tagUnwrap t
114+evalState t = Trans.evalState . untagged t
117-- | Runs a 'State' with the given initial state
116-- | Runs a 'State' with the given initial state
118-- and returns the final state, discarding the final value.
117-- and returns the final state, discarding the final value.
119execState :: proxy tag -> State tag s a -> s -> s
118execState :: proxy tag -> State tag s a -> s -> s
120−execState t = Trans.execState . tagUnwrap t
119+execState t = Trans.execState . untagged t
122-- | Transform the computation inside a 'StateT'.
121-- | Transform the computation inside a 'StateT'.
124-- * @'runStateT' tag ('mapStateT' tag f m) = f . 'runStateT' tag m@
123-- * @'runStateT' tag ('mapStateT' tag f m) = f . 'runStateT' tag m@
125mapStateT :: proxy tag -> (m (a, s) -> n (b, s)) -> StateT tag s m a -> StateT tag s n b
124mapStateT :: proxy tag -> (m (a, s) -> n (b, s)) -> StateT tag s m a -> StateT tag s n b
126−mapStateT t f m = tagWrap t $ Trans.mapStateT f (coerce m)
125+mapStateT t f m = tagged t $ Trans.mapStateT f (coerce m)
128-- | Fetch the current value of the state within the monad.
127-- | Fetch the current value of the state within the monad.
129get :: Monad m => proxy tag -> StateT tag s m s
128get :: Monad m => proxy tag -> StateT tag s m s
130−get t = tagWrap t Trans.get
129+get t = tagged t Trans.get
132-- | Set the value of the state within the monad.
131-- | Set the value of the state within the monad.
133put :: Monad m => proxy tag -> s -> StateT tag s m ()
132put :: Monad m => proxy tag -> s -> StateT tag s m ()
134−put t = tagWrap t . Trans.put
133+put t = tagged t . Trans.put
136-- | Lift a @catchE@ operation to the new monad.
135-- | Lift a @catchE@ operation to the new monad.
137liftCatch :: proxy tag -> Sig.Catch e m (a, s) -> Sig.Catch e (StateT tag s m) a
136liftCatch :: proxy tag -> Sig.Catch e m (a, s) -> Sig.Catch e (StateT tag s m) a
138−liftCatch t f m h = tagWrap t $ Trans.liftCatch f (coerce m) (coerce h)
137+liftCatch t f m h = tagged t $ Trans.liftCatch f (coerce m) (coerce h)
140-- | Lift a @listen@ operation to the new monad.
139-- | Lift a @listen@ operation to the new monad.
141liftListen :: Monad m => proxy tag -> Sig.Listen w m (a, s) -> Sig.Listen w (StateT tag s m) a
140liftListen :: Monad m => proxy tag -> Sig.Listen w m (a, s) -> Sig.Listen w (StateT tag s m) a
142−liftListen t listen m = tagWrap t $ Trans.liftListen listen (coerce m)
141+liftListen t listen m = tagged t $ Trans.liftListen listen (coerce m)
144-- | In-situ lifting of a @callCC@ operation to the new monad.
143-- | In-situ lifting of a @callCC@ operation to the new monad.
145-- This version uses the current state on entering the continuation.
144-- This version uses the current state on entering the continuation.
146-- It does not satisfy the uniformity property (see "Control.Monad.Signatures").
145-- It does not satisfy the uniformity property (see "Control.Monad.Signatures").
147liftCallCC' :: proxy tag -> Sig.CallCC m (a, s) (b, s) -> Sig.CallCC (StateT tag s m) a b
146liftCallCC' :: proxy tag -> Sig.CallCC m (a, s) (b, s) -> Sig.CallCC (StateT tag s m) a b
148−liftCallCC' t callCC f = tagWrap t $ Trans.liftCallCC' callCC (coerce f)
147+liftCallCC' t callCC f = tagged t $ Trans.liftCallCC' callCC (coerce f)
150-- | Lift a @pass@ operation to the new monad.
149-- | Lift a @pass@ operation to the new monad.
151liftPass :: Monad m => proxy tag -> Sig.Pass w m (a,s) -> Sig.Pass w (StateT tag s m) a
150liftPass :: Monad m => proxy tag -> Sig.Pass w m (a,s) -> Sig.Pass w (StateT tag s m) a
152−liftPass t pass m = tagWrap t $ Trans.liftPass pass (coerce m)
151+liftPass t pass m = tagged t $ Trans.liftPass pass (coerce m)
154-- Instances for mtl classes
153-- Instances for mtl classes