on master help sign in
EtherStateT and minor fixes
e2b72eaIndex Int
ether.cabal+6−2
@@ -2,13 +2,15 @@ name: ether
2version: 0.1.0.0
2version: 0.1.0.0
3synopsis: Monad transformers and classes
3synopsis: Monad transformers and classes
4description:
4description:
5− Ether provides an easy way to create mtl-compatible effects
5+ Ether is a Haskell library that extends @mtl@ and @transformers@ with
6− that do not require you to write O(n^2) instances.
6+ tagged monad transformers and classes in a compatible way.
7+
7category: Control
8category: Control
8license: BSD3
9license: BSD3
9license-file: LICENSE
10license-file: LICENSE
10author: Index Int
11author: Index Int
11maintainer: Index Int <vlad.z.4096@gmail.com>
12maintainer: Index Int <vlad.z.4096@gmail.com>
13+homepage: https://int-index.github.io/ether/
12bug-reports: https://github.com/int-index/ether/issues
14bug-reports: https://github.com/int-index/ether/issues
13build-type: Simple
15build-type: Simple
14cabal-version: >=1.22
16cabal-version: >=1.22
@@ -25,6 +27,8 @@ library
25 Control.Ether.TH
27 Control.Ether.TH
26 Control.Ether.Wrapped
28 Control.Ether.Wrapped
27 Control.Monad.Trans.Ether.Reader
29 Control.Monad.Trans.Ether.Reader
30+ Control.Monad.Trans.Ether.State
31+ Control.Monad.Trans.Ether.State.Lazy
28 Control.Monad.Ether.Reader
32 Control.Monad.Ether.Reader
29
33
30 build-depends: base >=4.8 && <4.9
34 build-depends: base >=4.8 && <4.9
src/Control/Monad/Trans/Ether/Reader.hs+3−3
@@ -53,7 +53,7 @@ etherReaderT :: proxy tag -> (r -> m a) -> EtherReaderT tag r m a
53etherReaderT _proxy = EtherReaderT . R.ReaderT
53etherReaderT _proxy = EtherReaderT . R.ReaderT
54
54
55runEtherReaderT :: proxy tag -> EtherReaderT tag r m a -> r -> m a
55runEtherReaderT :: proxy tag -> EtherReaderT tag r m a -> r -> m a
56−runEtherReaderT _proxy (EtherReaderT (R.ReaderT s)) = s
56+runEtherReaderT _proxy (EtherReaderT (R.ReaderT f)) = f
57
57
58runEtherReader :: proxy tag -> EtherReader tag r a -> r -> a
58runEtherReader :: proxy tag -> EtherReader tag r a -> r -> a
59runEtherReader proxy m r = runIdentity (runEtherReaderT proxy m r)
59runEtherReader proxy m r = runIdentity (runEtherReaderT proxy m r)
@@ -68,13 +68,13 @@ liftCallCC :: proxy tag -> Sig.CallCC m a b -> Sig.CallCC (EtherReaderT tag r m)
68liftCallCC _proxy callCC f = EtherReaderT $ R.liftCallCC callCC (coerce f)
68liftCallCC _proxy callCC f = EtherReaderT $ R.liftCallCC callCC (coerce f)
69
69
70type ReaderT' r = EtherReaderT r r
70type ReaderT' r = EtherReaderT r r
71−type Reader' r = EtherReader r r
71+type Reader' r = EtherReader r r
72
72
73runReaderT' :: ReaderT' r m a -> r -> m a
73runReaderT' :: ReaderT' r m a -> r -> m a
74runReaderT' = runEtherReaderT Proxy
74runReaderT' = runEtherReaderT Proxy
75
75
76runReader' :: Reader' r a -> r -> a
76runReader' :: Reader' r a -> r -> a
77−runReader' m r = runIdentity (runEtherReaderT Proxy m r)
77+runReader' m r = runIdentity (runReaderT' m r)
78
78
79-- Instances for mtl classes
79-- Instances for mtl classes
80
80
src/Control/Monad/Trans/Ether/State.hsnew file+5
@@ -0,0 +1,5 @@
1+module Control.Monad.Trans.Ether.State
2+ ( module Control.Monad.Trans.Ether.State.Lazy
3+ ) where
4+
5+import Control.Monad.Trans.Ether.State.Lazy
src/Control/Monad/Trans/Ether/State/Lazy.hsnew file+110
@@ -0,0 +1,110 @@
1+{-# LANGUAGE MultiParamTypeClasses #-}
2+{-# LANGUAGE TypeFamilies #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE FlexibleContexts #-}
5+{-# LANGUAGE UndecidableInstances #-}
6+{-# LANGUAGE DataKinds #-}
7+{-# LANGUAGE TypeOperators #-}
8+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9+module Control.Monad.Trans.Ether.State.Lazy
10+ ( EtherStateT
11+ , EtherState
12+ , StateT'
13+ , State'
14+ , runEtherStateT
15+ , runEtherState
16+ , runStateT'
17+ , runState'
18+ --
19+ , etherStateT
20+ , mapEtherStateT
21+ , liftCatch
22+ , liftCallCC'
23+ , liftListen
24+ , liftPass
25+ ) where
26+
27+import Data.Proxy (Proxy(Proxy))
28+import Data.Functor.Identity (Identity(..))
29+import Data.Coerce (coerce)
30+import Control.Applicative (Alternative)
31+import Control.Monad (MonadPlus)
32+import Control.Monad.Fix (MonadFix)
33+import Control.Monad.Trans.Class (MonadTrans, lift)
34+import Control.Monad.IO.Class (MonadIO)
35+import Control.Ether.Core
36+
37+import qualified Control.Monad.Signatures as Sig
38+import qualified Control.Monad.Trans.State.Lazy as S
39+
40+import qualified Control.Monad.Cont.Class as Class
41+import qualified Control.Monad.Reader.Class as Class
42+import qualified Control.Monad.State.Class as Class
43+import qualified Control.Monad.Writer.Class as Class
44+import qualified Control.Monad.Error.Class as Class
45+
46+newtype EtherStateT tag s m a = EtherStateT (S.StateT s m a)
47+ deriving ( Functor, Applicative, Alternative, Monad, MonadPlus
48+ , MonadFix, MonadTrans, MonadIO )
49+
50+type instance EtherTags (EtherStateT tag r m) = tag ': EtherTags m
51+
52+type EtherState tag r = EtherStateT tag r Identity
53+
54+etherStateT :: proxy tag -> (s -> m (a, s)) -> EtherStateT tag s m a
55+etherStateT _proxy = EtherStateT . S.StateT
56+
57+runEtherStateT :: proxy tag -> EtherStateT tag s m a -> s -> m (a, s)
58+runEtherStateT _proxy (EtherStateT (S.StateT f)) = f
59+
60+runEtherState :: proxy tag -> EtherState tag s a -> s -> (a, s)
61+runEtherState proxy m s = runIdentity (runEtherStateT proxy m s)
62+
63+mapEtherStateT :: proxy tag -> (m (a, s) -> n (b, s)) -> EtherStateT tag s m a -> EtherStateT tag s n b
64+mapEtherStateT _proxy f m = EtherStateT $ S.mapStateT f (coerce m)
65+
66+liftCatch :: proxy tag -> Sig.Catch e m (a, s) -> Sig.Catch e (EtherStateT tag s m) a
67+liftCatch _proxy f m h = EtherStateT $ S.liftCatch f (coerce m) (coerce h)
68+
69+liftCallCC' :: proxy tag -> Sig.CallCC m (a, s) (b, s) -> Sig.CallCC (EtherStateT tag s m) a b
70+liftCallCC' _proxy callCC f = EtherStateT $ S.liftCallCC' callCC (coerce f)
71+
72+liftListen :: Monad m => proxy tag -> Sig.Listen w m (a, s) -> Sig.Listen w (EtherStateT tag s m) a
73+liftListen _proxy listen m = EtherStateT $ S.liftListen listen (coerce m)
74+
75+liftPass :: Monad m => proxy tag -> Sig.Pass w m (a,s) -> Sig.Pass w (EtherStateT tag s m) a
76+liftPass _proxy pass m = EtherStateT $ S.liftPass pass (coerce m)
77+
78+type StateT' s = EtherStateT s s
79+type State' s = EtherState s s
80+
81+runStateT' :: StateT' s m a -> s -> m (a, s)
82+runStateT' = runEtherStateT Proxy
83+
84+runState' :: State' s a -> s -> (a, s)
85+runState' m s = runIdentity (runStateT' m s)
86+
87+-- Instances for mtl classes
88+
89+instance Class.MonadCont m => Class.MonadCont (EtherStateT tag s m) where
90+ callCC = liftCallCC' Proxy Class.callCC
91+
92+instance Class.MonadReader r m => Class.MonadReader r (EtherStateT tag s m) where
93+ ask = lift Class.ask
94+ local = mapEtherStateT Proxy . Class.local
95+ reader = lift . Class.reader
96+
97+instance Class.MonadState s' m => Class.MonadState s' (EtherStateT tag s m) where
98+ get = lift Class.get
99+ put = lift . Class.put
100+ state = lift . Class.state
101+
102+instance Class.MonadWriter w m => Class.MonadWriter w (EtherStateT tag s m) where
103+ writer = lift . Class.writer
104+ tell = lift . Class.tell
105+ listen = liftListen Proxy Class.listen
106+ pass = liftPass Proxy Class.pass
107+
108+instance Class.MonadError e m => Class.MonadError e (EtherStateT tag s m) where
109+ throwError = lift . Class.throwError
110+ catchError = liftCatch Proxy Class.catchError