on master help sign in
Update documentation
commit7a1d203dd1b052a2d66c08fd10aa2d337e7650a5 authorIndex Int <vlad.z.4096@gmail.com> dateThu 11 Jun 2015, 21:31 (11 years ago) parentbd5c1c9
6 files changed+107−35
src/Control/Ether/TH.hs+8 src/Control/Ether/Tags.hs+15−1 src/Control/Monad/Ether/Reader.hs+2 src/Control/Monad/Ether/State.hs+21−14 src/Control/Monad/Trans/Ether/Reader.hs+5−3 src/Control/Monad/Trans/Ether/State.hs+56−17
src/Control/Ether/TH.hs+8
@@ -17,6 +17,14 @@ proxySimple name ty = do
1717 val <- funSimple name [e| Proxy |]
1818 return (sig, val)
1919
20+-- |
21+-- Creates a tag and a value-level proxy for it.
22+--
23+-- @'ethereal' \"Foo\" \"foo\"@ generates the following code:
24+--
25+-- > data Foo
26+-- > foo :: Proxy Foo
27+-- > foo = Proxy
2028ethereal :: String -> String -> TH.DecsQ
2129ethereal strTagName strTagProxyName = do
2230 let tagName = TH.mkName strTagName
src/Control/Ether/Tags.hs+15−1
@@ -30,7 +30,10 @@ import qualified Control.Monad.Trans.State.Strict as Trans.Strict
3030import qualified Control.Monad.Trans.Writer.Lazy as Trans.Lazy
3131import qualified Control.Monad.Trans.Writer.Strict as Trans.Strict
3232
33−-- Never declare instances for this class
33+-- |
34+-- The main purpose of the 'UniqueTag' class is to provide clear error
35+-- messages when the tag uniqueness property is violated. You should never
36+-- provide instances for it unless you know what you're doing.
3437class UniqueTag a
3538
3639type family IsUnique (x :: k) (as :: [k]) :: Constraint where
@@ -42,12 +45,23 @@ type family Unique (as :: [k]) :: Constraint where
4245 Unique '[] = ()
4346 Unique (a ': as) = (IsUnique a as, Unique as)
4447
48+-- |
49+-- The 'UniqueTags` constraint placed on a type variable representing a
50+-- monad transformer stack ensures that every tag in the stack appears
51+-- only once.
4552type family UniqueTags (m :: * -> *) :: Constraint where
4653 UniqueTags m = Unique (Tags m)
4754
55+-- |
56+-- Type-restricted version of 'id' that adds a 'UniqueTags' constraint to
57+-- the provided monadic value.
4858ensureUniqueTags :: UniqueTags m => m a -> m a
4959ensureUniqueTags = id
5060
61+-- |
62+-- The 'Tags' type family returns a type-level list of all tags of its argument
63+-- @m@. Simple monads have no tags, monad transformers propagate the tags of
64+-- their inner monads, and tagged monad transformers also add their own tag.
5165type family Tags (m :: * -> *) :: [*]
5266
5367type instance Tags IO = '[]
src/Control/Monad/Ether/Reader.hs+2
@@ -20,7 +20,9 @@ module Control.Monad.Ether.Reader
2020 , runReader
2121 -- * The ReaderT monad transformer
2222 , ReaderT
23+ , readerT
2324 , runReaderT
25+ , mapStateT
2426 ) where
2527
2628import Data.Proxy (Proxy(Proxy))
src/Control/Monad/Ether/State.hs+21−14
@@ -11,21 +11,23 @@
1111{-# LANGUAGE RankNTypes #-}
1212{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1313module Control.Monad.Ether.State
14− ( StateT
14+ (
15+ -- * MonadState class
16+ MonadState(..)
17+ , modify
18+ , gets
19+ -- * The State monad
1520 , State
16− , runStateT
1721 , runState
18− , evalStateT
1922 , evalState
20− , execStateT
2123 , execState
22− --
23− , MonadState
24− , get
25− , gets
26− , put
27− , state
28− , modify
24+ -- * The StateT monad transformer
25+ , StateT
26+ , stateT
27+ , runStateT
28+ , evalStateT
29+ , execStateT
30+ , mapStateT
2931 ) where
3032
3133import Control.Monad.Trans (lift)
@@ -49,12 +51,15 @@ class Monad m => MonadState tag s m | m tag -> s where
4951
5052 {-# MINIMAL state | get, put #-}
5153
54+ -- | Return the state from the internals of the monad.
5255 get :: proxy tag -> m s
5356 get proxy = state proxy (\s -> (s, s))
5457
58+ -- | Replace the state inside the monad.
5559 put :: proxy tag -> s -> m ()
5660 put proxy s = state proxy (\_ -> ((), s))
5761
62+ -- | Embed a simple state action into the monad.
5863 state :: proxy tag -> (s -> (a, s)) -> m a
5964 state proxy f = do
6065 s <- get proxy
@@ -62,12 +67,14 @@ class Monad m => MonadState tag s m | m tag -> s where
6267 put proxy s'
6368 return a
6469
65−gets :: MonadState tag s m => proxy tag -> (s -> a) -> m a
66−gets proxy f = fmap f (get proxy)
67−
70+-- | Modifies the state inside a state monad.
6871modify :: MonadState tag s m => proxy tag -> (s -> s) -> m ()
6972modify proxy f = state proxy $ \ s -> ((), f s)
7073
74+-- | Gets specific component of the state, using a projection function supplied.
75+gets :: MonadState tag s m => proxy tag -> (s -> a) -> m a
76+gets proxy f = fmap f (get proxy)
77+
7178instance {-# OVERLAPPING #-} Monad m => MonadState tag s (StateT tag s m) where
7279 get proxy = stateT proxy (\s -> return (s, s))
7380 put proxy s = stateT proxy (\_ -> return ((), s))
src/Control/Monad/Trans/Ether/Reader.hs+5−3
@@ -63,15 +63,17 @@ type instance Tags (ReaderT tag r m) = tag ': Tags m
6363readerT :: proxy tag -> (r -> m a) -> ReaderT tag r m a
6464readerT _proxy = ReaderT . Trans.ReaderT
6565
66−-- | Runs a 'Reader' and extracts the final value from it.
66+-- | Runs a 'ReaderT' with the given environment
67+-- and returns the vinal value.
6768runReaderT :: proxy tag -> ReaderT tag r m a -> r -> m a
6869runReaderT _proxy (ReaderT (Trans.ReaderT f)) = f
6970
70−-- | Runs a 'Reader' and extracts the final value from it.
71+-- | Runs a 'ReaderT' with the given environment
72+-- and returns the vinal value.
7173runReader :: proxy tag -> Reader tag r a -> r -> a
7274runReader proxy m r = runIdentity (runReaderT proxy m r)
7375
74−-- | Transform the computation inside a @ReaderT@.
76+-- | Transform the computation inside a 'ReaderT'.
7577--
7678-- * @'runReaderT' tag ('mapReaderT' tag f m) = f . 'runReaderT' tag m@
7779mapReaderT :: proxy tag -> (m a -> n b) -> ReaderT tag r m a -> ReaderT tag r n b
src/Control/Monad/Trans/Ether/State.hs+56−17
@@ -7,16 +7,20 @@
77{-# LANGUAGE TypeOperators #-}
88{-# LANGUAGE GeneralizedNewtypeDeriving #-}
99module Control.Monad.Trans.Ether.State
10− ( StateT
11− , State
12− , runStateT
10+ (
11+ -- * The State monad
12+ State
1313 , runState
14− , evalStateT
1514 , evalState
16− , execStateT
1715 , execState
16+ -- * The StateT monad transformer
17+ , StateT
1818 , stateT
19+ , runStateT
20+ , evalStateT
21+ , execStateT
1922 , mapStateT
23+ -- * Litfing other operations
2024 , liftCatch
2125 , liftCallCC'
2226 , liftListen
@@ -42,47 +46,82 @@ import qualified Control.Monad.State.Class as Class
4246import qualified Control.Monad.Writer.Class as Class
4347import qualified Control.Monad.Error.Class as Class
4448
49+
50+-- | The parametrizable state monad.
51+--
52+-- Computations have access to a mutable state.
53+--
54+-- The 'return' function leaves the state unchanged, while '>>=' uses
55+-- the final state of the first computation as the initial state of
56+-- the second.
57+type State tag r = StateT tag r Identity
58+
59+-- | The state monad transformer.
60+--
61+-- The 'return' function leaves the state unchanged, while '>>=' uses
62+-- the final state of the first computation as the initial state of
63+-- the second.
4564newtype StateT tag s m a = StateT (Trans.StateT s m a)
4665 deriving ( Functor, Applicative, Alternative, Monad, MonadPlus
4766 , MonadFix, MonadTrans, MonadIO )
4867
4968type instance Tags (StateT tag r m) = tag ': Tags m
5069
51−type State tag r = StateT tag r Identity
52−
70+-- | Constructor for computations in the state monad transformer.
5371stateT :: proxy tag -> (s -> m (a, s)) -> StateT tag s m a
5472stateT _proxy = StateT . Trans.StateT
5573
74+-- | Runs a 'StateT' with the given initial state
75+-- and returns both the final value and the final state.
5676runStateT :: proxy tag -> StateT tag s m a -> s -> m (a, s)
5777runStateT _proxy (StateT (Trans.StateT f)) = f
5878
59−runState :: proxy tag -> State tag s a -> s -> (a, s)
60−runState proxy m s = runIdentity (runStateT proxy m s)
61−
79+-- | Runs a 'StateT' with the given initial state
80+-- and returns the final value, discarding the final state.
6281evalStateT :: Functor m => proxy tag -> StateT tag s m a -> s -> m a
6382evalStateT proxy m s = fmap fst (runStateT proxy m s)
6483
65−evalState :: proxy tag -> State tag s a -> s -> a
66−evalState proxy m s = fst (runState proxy m s)
67−
84+-- | Runs a 'StateT' with the given initial state
85+-- and returns the final state, discarding the final value.
6886execStateT :: Functor m => proxy tag -> StateT tag s m a -> s -> m s
6987execStateT proxy m s = fmap snd (runStateT proxy m s)
7088
89+-- | Runs a 'State' with the given initial state
90+-- and returns both the final value and the final state.
91+runState :: proxy tag -> State tag s a -> s -> (a, s)
92+runState proxy m s = runIdentity (runStateT proxy m s)
93+
94+-- | Runs a 'State' with the given initial state
95+-- and returns the final value, discarding the final state.
96+evalState :: proxy tag -> State tag s a -> s -> a
97+evalState proxy m s = runIdentity (evalStateT proxy m s)
98+
99+-- | Runs a 'State' with the given initial state
100+-- and returns the final state, discarding the final value.
71101execState :: proxy tag -> State tag s a -> s -> s
72−execState proxy m s = snd (runState proxy m s)
102+execState proxy m s = runIdentity (execStateT proxy m s)
73103
104+-- | Transform the computation inside a 'StateT'.
105+--
106+-- * @'runStateT' tag ('mapStateT' tag f m) = f . 'runStateT' tag m@
74107mapStateT :: proxy tag -> (m (a, s) -> n (b, s)) -> StateT tag s m a -> StateT tag s n b
75108mapStateT _proxy f m = StateT $ Trans.mapStateT f (coerce m)
76109
110+-- | Lift a @catchE@ operation to the new monad.
77111liftCatch :: proxy tag -> Sig.Catch e m (a, s) -> Sig.Catch e (StateT tag s m) a
78112liftCatch _proxy f m h = StateT $ Trans.liftCatch f (coerce m) (coerce h)
79113
80−liftCallCC' :: proxy tag -> Sig.CallCC m (a, s) (b, s) -> Sig.CallCC (StateT tag s m) a b
81−liftCallCC' _proxy callCC f = StateT $ Trans.liftCallCC' callCC (coerce f)
82−
114+-- | Lift a @listen@ operation to the new monad.
83115liftListen :: Monad m => proxy tag -> Sig.Listen w m (a, s) -> Sig.Listen w (StateT tag s m) a
84116liftListen _proxy listen m = StateT $ Trans.liftListen listen (coerce m)
85117
118+-- | In-situ lifting of a @callCC@ operation to the new monad.
119+-- This version uses the current state on entering the continuation.
120+-- It does not satisfy the uniformity property (see "Control.Monad.Signatures").
121+liftCallCC' :: proxy tag -> Sig.CallCC m (a, s) (b, s) -> Sig.CallCC (StateT tag s m) a b
122+liftCallCC' _proxy callCC f = StateT $ Trans.liftCallCC' callCC (coerce f)
123+
124+-- | Lift a @pass@ operation to the new monad.
86125liftPass :: Monad m => proxy tag -> Sig.Pass w m (a,s) -> Sig.Pass w (StateT tag s m) a
87126liftPass _proxy pass m = StateT $ Trans.liftPass pass (coerce m)
88127