on master help sign in
Update documentation
commitbccf61c155be131a0d445654240f31e478ca32f5 authorIndex Int <vlad.z.4096@gmail.com> dateSat 13 Jun 2015, 23:07 (11 years ago) parentb7754db
27 files changed+210−16
src/Control/Ether/TH.hs+11 src/Control/Ether/Tagged.hs+6−1 src/Control/Ether/Wrapped.hs+27−4 src/Control/Monad/Ether/Except.hs+3 src/Control/Monad/Ether/Except/Class.hs+5 src/Control/Monad/Ether/Implicit/Except.hs+13 src/Control/Monad/Ether/Implicit/Except/TH.hs+10 src/Control/Monad/Ether/Implicit/Reader.hs+15−1 src/Control/Monad/Ether/Implicit/State.hs+3 src/Control/Monad/Ether/Implicit/State/Lazy.hs+20 src/Control/Monad/Ether/Implicit/State/Strict.hs+20 src/Control/Monad/Ether/Implicit/Writer.hs+17 src/Control/Monad/Ether/Reader.hs+3 src/Control/Monad/Ether/Reader/Class.hs+4 src/Control/Monad/Ether/State.hs+3 src/Control/Monad/Ether/State/Class.hs+4 src/Control/Monad/Ether/State/Lazy.hs+3 src/Control/Monad/Ether/State/Strict.hs+3 src/Control/Monad/Ether/Writer.hs+3 src/Control/Monad/Ether/Writer/Class.hs+7 src/Control/Monad/Trans/Ether/Except.hs+5 src/Control/Monad/Trans/Ether/Reader.hs+3 src/Control/Monad/Trans/Ether/State.hs+3 src/Control/Monad/Trans/Ether/State/Lazy.hs+3 src/Control/Monad/Trans/Ether/State/Strict.hs+3 src/Control/Monad/Trans/Ether/Writer.hs+6 test/Regression.hs+7−10
src/Control/Ether/TH.hs+11
@@ -1,4 +1,7 @@
11{-# LANGUAGE TemplateHaskell #-}
2+
3+-- | Template haskell utilities.
4+
25module Control.Ether.TH
36 ( ethereal
47 , fmapN
@@ -38,10 +41,18 @@ ethereal strTagName strTagProxyName = do
3841 (tagProxySig, tagProxyVal) <- proxySimple tagProxyName tag
3942 return [tagDecl, tagProxySig, tagProxyVal]
4043
44+-- |
45+-- Compose 'fmap' @n@ times.
46+--
47+-- @$('fmapN' 5) = fmap.fmap.fmap.fmap.fmap@
4148fmapN :: Int -> TH.ExpQ
4249fmapN 0 = [|id|]
4350fmapN n = TH.infixApp [|fmap|] [|(.)|] (fmapN (n - 1))
4451
52+-- |
53+-- 'fmap' a function @n@ levels deep.
54+--
55+-- @$('deepN' 3 [| f |]) = $('fmapN' 3) f ($('fmapN' 2) f ($('fmapN' 1) f id))@
4556deepN :: Int -> TH.ExpQ -> TH.ExpQ
4657deepN 0 _ = [|id|]
4758deepN n f = [| $(fmapN n) $f $(deepN (n-1) f) |]
src/Control/Ether/Tagged.hs+6−1
@@ -9,6 +9,9 @@
99{-# LANGUAGE MultiParamTypeClasses #-}
1010{-# LANGUAGE FunctionalDependencies #-}
1111{-# LANGUAGE DefaultSignatures #-}
12+
13+-- | Type-level machinery for tag manipulation.
14+
1215module Control.Ether.Tagged
1316 ( Taggable(..)
1417 , Tagged(..)
@@ -43,7 +46,7 @@ import qualified Control.Monad.Trans.Writer.Strict as Trans.Strict
4346-- |
4447-- The main purpose of the 'UniqueTag' class is to provide clear error
4548-- messages when the tag uniqueness property is violated. You should never
46−-- provide instances for it unless you know what you're doing.
49+-- write instances for it unless you know what you're doing.
4750class UniqueTag a
4851
4952type family IsUnique (x :: k) (as :: [k]) :: Constraint where
@@ -91,6 +94,8 @@ type family Inners' m where
9194 Inners' 'Nothing = '[]
9295 Inners' ('Just n) = n ': Inners n
9396
97+-- | The 'Inners' type function recursively applies 'Inner' and returns the
98+-- results in a type-level list.
9499type Inners m = Inners' (Inner m)
95100
96101instance Taggable IO
src/Control/Ether/Wrapped.hs+27−4
@@ -4,7 +4,28 @@
44{-# LANGUAGE UndecidableInstances #-}
55{-# LANGUAGE ScopedTypeVariables #-}
66{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7−module Control.Ether.Wrapped where
7+
8+{- |
9+Annotating monads with tags to turn untagged constraints into tagged ones.
10+
11+> import qualified Control.Monad.State as T
12+> import Control.Ether.TH (ethereal)
13+> import Control.Monad.Ether.State (MonadState)
14+> import Control.Ether.Wrapped (ethered)
15+>
16+> ethereal "Foo" "foo"
17+>
18+> f :: T.MonadState Int m => m String
19+> f = fmap show T.get
20+>
21+> g :: MonadState Foo Int m => m String
22+> g = ethered foo f
23+-}
24+
25+module Control.Ether.Wrapped
26+ ( WrappedEther(..)
27+ , ethered
28+ ) where
829
930import Data.Proxy (Proxy(Proxy))
1031import Control.Applicative (Alternative)
@@ -21,10 +42,15 @@ import qualified Control.Monad.State as Class
2142import qualified Control.Monad.Except as Class
2243import qualified Control.Monad.Writer as Class
2344
45+-- | Wrap a monad to attach a tag to it.
2446newtype WrappedEther tag m a = WrapEther { unwrapEther :: m a }
2547 deriving ( Functor, Applicative, Alternative, Monad, MonadPlus
2648 , MonadFix, MonadIO )
2749
50+-- | Annotate a polymorphic monadic computation with a tag.
51+ethered :: proxy tag -> WrappedEther tag m a -> m a
52+ethered _proxy = unwrapEther
53+
2854instance MonadReader tag r m => Class.MonadReader r (WrappedEther tag m) where
2955 ask = WrapEther $ ask (Proxy :: Proxy tag)
3056 local f = WrapEther . local (Proxy :: Proxy tag) f . unwrapEther
@@ -58,6 +84,3 @@ instance MonadWriter tag w m => MonadWriter tag w (WrappedEther tag' m) where
5884 tell t = WrapEther . tell t
5985 listen t = WrapEther . listen t . unwrapEther
6086 pass t = WrapEther . pass t . unwrapEther
61−
62−ethered :: proxy tag -> WrappedEther tag m a -> m a
63−ethered _proxy = unwrapEther
src/Control/Monad/Ether/Except.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.Except".
3+
14module Control.Monad.Ether.Except
25 (
36 -- * MonadExcept class
src/Control/Monad/Ether/Except/Class.hs+5
@@ -10,6 +10,9 @@
1010{-# LANGUAGE ScopedTypeVariables #-}
1111{-# LANGUAGE RankNTypes #-}
1212{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13+
14+-- | See "Control.Monad.Except".
15+
1316module Control.Monad.Ether.Except.Class
1417 ( MonadExcept(..)
1518 ) where
@@ -36,6 +39,8 @@ import qualified Control.Monad.Trans.State.Strict as Trans.S.Strict
3639import qualified Control.Monad.Trans.Writer.Lazy as Trans.W.Lazy
3740import qualified Control.Monad.Trans.Writer.Strict as Trans.W.Strict
3841
42+
43+-- | See 'Control.Monad.Except.MonadError'.
3944class Monad m => MonadExcept tag e m | m tag -> e where
4045
4146 -- | Is used within a monadic computation to begin exception processing.
src/Control/Monad/Ether/Implicit/Except.hs+13
@@ -1,5 +1,8 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+
4+-- | See "Control.Monad.Ether.Except".
5+
36module Control.Monad.Ether.Implicit.Except
47 (
58 -- * MonadExcept class
@@ -19,26 +22,36 @@ module Control.Monad.Ether.Implicit.Except
1922import Data.Proxy
2023import qualified Control.Monad.Ether.Except as Explicit
2124
25+-- | See 'Control.Monad.Ether.Except.MonadExcept'.
2226type MonadExcept e = Explicit.MonadExcept e e
2327
28+-- | See 'Control.Monad.Ether.Except.throw'.
2429throw :: forall e m a . MonadExcept e m => e -> m a
2530throw = Explicit.throw (Proxy :: Proxy e)
2631
32+-- | See 'Control.Monad.Ether.Except.catch'.
2733catch :: forall e m a . MonadExcept e m => m a -> (e -> m a) -> m a
2834catch = Explicit.catch (Proxy :: Proxy e)
2935
36+-- | See 'Control.Monad.Ether.Except.Except'.
3037type Except e = Explicit.Except e e
3138
39+-- | See 'Control.Monad.Ether.Except.runExcept'.
3240runExcept :: Except e a -> Either e a
3341runExcept = Explicit.runExcept Proxy
3442
43+-- | See 'Control.Monad.Ether.Except.ExceptT'.
3544type ExceptT e = Explicit.ExceptT e e
3645
46+-- | See 'Control.Monad.Ether.Except.exceptT'.
3747exceptT :: m (Either e a) -> ExceptT e m a
3848exceptT = Explicit.exceptT Proxy
3949
50+-- | See 'Control.Monad.Ether.Except.runExceptT'.
4051runExceptT :: ExceptT e m a -> m (Either e a)
4152runExceptT = Explicit.runExceptT Proxy
4253
54+-- | See 'Control.Monad.Ether.Except.mapExceptT'.
55+-- Note that due to implicit tagging the exception type cannot be changed.
4356mapExceptT :: (m (Either e a) -> n (Either e b)) -> ExceptT e m a -> ExceptT e n b
4457mapExceptT = Explicit.mapExceptT Proxy
src/Control/Monad/Ether/Implicit/Except/TH.hs+10
@@ -1,12 +1,22 @@
11{-# LANGUAGE TemplateHaskell #-}
2+
3+-- | Template Haskell utilities for exception handling.
4+
25module Control.Monad.Ether.Implicit.Except.TH (try', try) where
36
47import qualified Language.Haskell.TH as TH
58import Control.Ether.TH (deepN)
69import Control.Monad.Ether.Implicit.Except
710
11+-- | Basic building block for 'try'. Runs an 'ExceptT' with a handler.
812try' :: Functor m => ExceptT e m a -> (e -> a) -> m a
913try' m h = either h id <$> runExceptT m
1014
15+-- | Handle @n@ exceptions with supplied handlers.
16+--
17+-- > $(try 3) monadicComputation
18+-- > (\Exception1 -> ...)
19+-- > (\Exception2 -> ...)
20+-- > (\Exception3 -> ...)
1121try :: Int -> TH.ExpQ
1222try n = deepN n [|try'|]
src/Control/Monad/Ether/Implicit/Reader.hs+15−1
@@ -1,5 +1,8 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+
4+-- | See "Control.Monad.Ether.Reader".
5+
36module Control.Monad.Ether.Implicit.Reader
47 (
58 -- * MonadReader class
@@ -21,32 +24,43 @@ module Control.Monad.Ether.Implicit.Reader
2124import Data.Proxy
2225import qualified Control.Monad.Ether.Reader as Explicit
2326
27+-- | See 'Control.Monad.Ether.Reader.ReaderT'.
2428type ReaderT r = Explicit.ReaderT r r
29+
30+-- | See 'Control.Monad.Ether.Reader.Reader'.
2531type Reader r = Explicit.Reader r r
2632
33+-- | See 'Control.Monad.Ether.Reader.readerT'.
2734readerT :: (r -> m a) -> ReaderT r m a
2835readerT = Explicit.readerT Proxy
2936
37+-- | See 'Control.Monad.Ether.Reader.runReaderT'.
3038runReaderT :: ReaderT r m a -> r -> m a
3139runReaderT = Explicit.runReaderT Proxy
3240
41+-- | See 'Control.Monad.Ether.Reader.runReader'.
3342runReader :: Reader r a -> r -> a
3443runReader = Explicit.runReader Proxy
3544
45+-- | See 'Control.Monad.Ether.Reader.mapReaderT'.
3646mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b
3747mapReaderT = Explicit.mapReaderT Proxy
3848
49+-- | See 'Control.Monad.Ether.Reader.MonadReader'.
3950type MonadReader r = Explicit.MonadReader r r
4051
52+-- | See 'Control.Monad.Ether.Reader.local'.
4153local :: forall m r a . MonadReader r m => (r -> r) -> m a -> m a
4254local = Explicit.local (Proxy :: Proxy r)
4355
56+-- | See 'Control.Monad.Ether.Reader.ask'.
4457ask :: forall m r . MonadReader r m => m r
4558ask = Explicit.ask (Proxy :: Proxy r)
4659
60+-- | See 'Control.Monad.Ether.Reader.reader'.
4761reader :: forall m r a . MonadReader r m => (r -> a) -> m a
4862reader = Explicit.reader (Proxy :: Proxy r)
4963
64+-- | See 'Control.Monad.Ether.Reader.asks'.
5065asks :: forall m r a . MonadReader r m => (r -> a) -> m a
5166asks = Explicit.asks (Proxy :: Proxy r)
52−
src/Control/Monad/Ether/Implicit/State.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.Ether.State".
3+
14module Control.Monad.Ether.Implicit.State
25 ( module Control.Monad.Ether.Implicit.State.Lazy
36 ) where
src/Control/Monad/Ether/Implicit/State/Lazy.hs+20
@@ -1,5 +1,8 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+
4+-- | See "Control.Monad.Ether.State.Lazy".
5+
36module Control.Monad.Ether.Implicit.State.Lazy
47 (
58 -- * MonadState class
@@ -26,46 +29,63 @@ module Control.Monad.Ether.Implicit.State.Lazy
2629import Data.Proxy
2730import qualified Control.Monad.Ether.State.Lazy as Explicit
2831
32+-- | See 'Control.Monad.Ether.State.Lazy.StateT'.
2933type StateT s = Explicit.StateT s s
34+
35+-- | See 'Control.Monad.Ether.State.Lazy.State'.
3036type State s = Explicit.State s s
3137
38+-- | See 'Control.Monad.Ether.State.Lazy.stateT'.
3239stateT :: (s -> m (a, s)) -> StateT s m a
3340stateT = Explicit.stateT Proxy
3441
42+-- | See 'Control.Monad.Ether.State.Lazy.runStateT'.
3543runStateT :: StateT s m a -> s -> m (a, s)
3644runStateT = Explicit.runStateT Proxy
3745
46+-- | See 'Control.Monad.Ether.State.Lazy.runState'.
3847runState :: State s a -> s -> (a, s)
3948runState = Explicit.runState Proxy
4049
50+-- | See 'Control.Monad.Ether.State.Lazy.evalStateT'.
4151evalStateT :: Monad m => StateT s m a -> s -> m a
4252evalStateT = Explicit.evalStateT Proxy
4353
54+-- | See 'Control.Monad.Ether.State.Lazy.evalState'.
4455evalState :: State s a -> s -> a
4556evalState = Explicit.evalState Proxy
4657
58+-- | See 'Control.Monad.Ether.State.Lazy.execStateT'.
4759execStateT :: Monad m => StateT s m a -> s -> m s
4860execStateT = Explicit.execStateT Proxy
4961
62+-- | See 'Control.Monad.Ether.State.Lazy.execState'.
5063execState :: State s a -> s -> s
5164execState = Explicit.execState Proxy
5265
66+-- | See 'Control.Monad.Ether.State.Lazy.mapStateT'.
5367mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
5468mapStateT = Explicit.mapStateT Proxy
5569
70+-- | See 'Control.Monad.Ether.State.Lazy.MonadState'.
5671type MonadState s = Explicit.MonadState s s
5772
73+-- | See 'Control.Monad.Ether.State.Lazy.get'.
5874get :: forall m s . MonadState s m => m s
5975get = Explicit.get (Proxy :: Proxy s)
6076
77+-- | See 'Control.Monad.Ether.State.Lazy.gets'.
6178gets :: forall m s a . MonadState s m => (s -> a) -> m a
6279gets = Explicit.gets (Proxy :: Proxy s)
6380
81+-- | See 'Control.Monad.Ether.State.Lazy.put'.
6482put :: forall m s . MonadState s m => s -> m ()
6583put = Explicit.put (Proxy :: Proxy s)
6684
85+-- | See 'Control.Monad.Ether.State.Lazy.state'.
6786state :: forall m s a . MonadState s m => (s -> (a, s)) -> m a
6887state = Explicit.state (Proxy :: Proxy s)
6988
89+-- | See 'Control.Monad.Ether.State.Lazy.modify'.
7090modify :: forall m s . MonadState s m => (s -> s) -> m ()
7191modify = Explicit.modify (Proxy :: Proxy s)
src/Control/Monad/Ether/Implicit/State/Strict.hs+20
@@ -1,5 +1,8 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+
4+-- | See "Control.Monad.Ether.State.Strict".
5+
36module Control.Monad.Ether.Implicit.State.Strict
47 (
58 -- * MonadState class
@@ -26,46 +29,63 @@ module Control.Monad.Ether.Implicit.State.Strict
2629import Data.Proxy
2730import qualified Control.Monad.Ether.State.Strict as Explicit
2831
32+-- | See 'Control.Monad.Ether.State.Strict.StateT'.
2933type StateT s = Explicit.StateT s s
34+
35+-- | See 'Control.Monad.Ether.State.Strict.State'.
3036type State s = Explicit.State s s
3137
38+-- | See 'Control.Monad.Ether.State.Strict.stateT'.
3239stateT :: (s -> m (a, s)) -> StateT s m a
3340stateT = Explicit.stateT Proxy
3441
42+-- | See 'Control.Monad.Ether.State.Strict.runStateT'.
3543runStateT :: StateT s m a -> s -> m (a, s)
3644runStateT = Explicit.runStateT Proxy
3745
46+-- | See 'Control.Monad.Ether.State.Strict.runState'.
3847runState :: State s a -> s -> (a, s)
3948runState = Explicit.runState Proxy
4049
50+-- | See 'Control.Monad.Ether.State.Strict.evalStateT'.
4151evalStateT :: Monad m => StateT s m a -> s -> m a
4252evalStateT = Explicit.evalStateT Proxy
4353
54+-- | See 'Control.Monad.Ether.State.Strict.evalState'.
4455evalState :: State s a -> s -> a
4556evalState = Explicit.evalState Proxy
4657
58+-- | See 'Control.Monad.Ether.State.Strict.execStateT'.
4759execStateT :: Monad m => StateT s m a -> s -> m s
4860execStateT = Explicit.execStateT Proxy
4961
62+-- | See 'Control.Monad.Ether.State.Strict.execState'.
5063execState :: State s a -> s -> s
5164execState = Explicit.execState Proxy
5265
66+-- | See 'Control.Monad.Ether.State.Strict.mapStateT'.
5367mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
5468mapStateT = Explicit.mapStateT Proxy
5569
70+-- | See 'Control.Monad.Ether.State.Strict.MonadState'.
5671type MonadState s = Explicit.MonadState s s
5772
73+-- | See 'Control.Monad.Ether.State.Strict.get'.
5874get :: forall m s . MonadState s m => m s
5975get = Explicit.get (Proxy :: Proxy s)
6076
77+-- | See 'Control.Monad.Ether.State.Strict.gets'.
6178gets :: forall m s a . MonadState s m => (s -> a) -> m a
6279gets = Explicit.gets (Proxy :: Proxy s)
6380
81+-- | See 'Control.Monad.Ether.State.Strict.put'.
6482put :: forall m s . MonadState s m => s -> m ()
6583put = Explicit.put (Proxy :: Proxy s)
6684
85+-- | See 'Control.Monad.Ether.State.Strict.state'.
6786state :: forall m s a . MonadState s m => (s -> (a, s)) -> m a
6887state = Explicit.state (Proxy :: Proxy s)
6988
89+-- | See 'Control.Monad.Ether.State.Strict.modify'.
7090modify :: forall m s . MonadState s m => (s -> s) -> m ()
7191modify = Explicit.modify (Proxy :: Proxy s)
src/Control/Monad/Ether/Implicit/Writer.hs+17
@@ -1,5 +1,8 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+
4+-- | See "Control.Monad.Ether.Writer".
5+
36module Control.Monad.Ether.Implicit.Writer
47 (
58 -- * MonadWriter class
@@ -24,41 +27,55 @@ module Control.Monad.Ether.Implicit.Writer
2427import Data.Proxy
2528import qualified Control.Monad.Ether.Writer as Explicit
2629
30+-- | See 'Control.Monad.Ether.Writer.MonadWriter'.
2731type MonadWriter w = Explicit.MonadWriter w w
2832
33+-- | See 'Control.Monad.Ether.Writer.writer'.
2934writer :: forall m w a . MonadWriter w m => (a, w) -> m a
3035writer = Explicit.writer (Proxy :: Proxy w)
3136
37+-- | See 'Control.Monad.Ether.Writer.tell'.
3238tell :: forall m w . MonadWriter w m => w -> m ()
3339tell = Explicit.tell (Proxy :: Proxy w)
3440
41+-- | See 'Control.Monad.Ether.Writer.listen'.
3542listen :: forall m w a . MonadWriter w m => m a -> m (a, w)
3643listen = Explicit.listen (Proxy :: Proxy w)
3744
45+-- | See 'Control.Monad.Ether.Writer.pass'.
3846pass :: forall m w a . MonadWriter w m => m (a, w -> w) -> m a
3947pass = Explicit.pass (Proxy :: Proxy w)
4048
49+-- | See 'Control.Monad.Ether.Writer.listens'.
4150listens :: forall m w a b . MonadWriter w m => (w -> b) -> m a -> m (a, b)
4251listens = Explicit.listens (Proxy :: Proxy w)
4352
53+-- | See 'Control.Monad.Ether.Writer.censor'.
4454censor :: forall m w a . MonadWriter w m => (w -> w) -> m a -> m a
4555censor = Explicit.censor (Proxy :: Proxy w)
4656
57+-- | See 'Control.Monad.Ether.Writer.Writer'.
4758type Writer w = Explicit.Writer w w
4859
60+-- | See 'Control.Monad.Ether.Writer.runWriter'.
4961runWriter :: Writer w a -> (a, w)
5062runWriter = Explicit.runWriter Proxy
5163
64+-- | See 'Control.Monad.Ether.Writer.execWriter'.
5265execWriter :: Writer w a -> w
5366execWriter = Explicit.execWriter Proxy
5467
68+-- | See 'Control.Monad.Ether.Writer.WriterT'.
5569type WriterT w = Explicit.WriterT w w
5670
71+-- | See 'Control.Monad.Ether.Writer.writerT'.
5772writerT :: m (a, w) -> WriterT w m a
5873writerT = Explicit.writerT Proxy
5974
75+-- | See 'Control.Monad.Ether.Writer.runWriterT'.
6076runWriterT :: WriterT w m a -> m (a, w)
6177runWriterT = Explicit.runWriterT Proxy
6278
79+-- | See 'Control.Monad.Ether.Writer.execWriterT'.
6380execWriterT :: Monad m => WriterT w m a -> m w
6481execWriterT = Explicit.execWriterT Proxy
src/Control/Monad/Ether/Reader.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.Reader".
3+
14module Control.Monad.Ether.Reader
25 (
36 -- * MonadReader class
src/Control/Monad/Ether/Reader/Class.hs+4
@@ -10,6 +10,9 @@
1010{-# LANGUAGE ScopedTypeVariables #-}
1111{-# LANGUAGE RankNTypes #-}
1212{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13+
14+-- | See "Control.Monad.Reader.Class".
15+
1316module Control.Monad.Ether.Reader.Class
1417 ( MonadReader(..)
1518 , asks
@@ -37,6 +40,7 @@ import qualified Control.Monad.Trans.State.Strict as Trans.Strict (StateT , m
3740import qualified Control.Monad.Trans.Writer.Lazy as Trans.Lazy (WriterT , mapWriterT)
3841import qualified Control.Monad.Trans.Writer.Strict as Trans.Strict (WriterT , mapWriterT)
3942
43+-- | See 'Control.Monad.Reader.MonadReader'.
4044class Monad m => MonadReader tag r m | m tag -> r where
4145
4246 {-# MINIMAL (ask | reader), local #-}
src/Control/Monad/Ether/State.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.State".
3+
14module Control.Monad.Ether.State
25 ( module Control.Monad.Ether.State.Lazy
36 ) where
src/Control/Monad/Ether/State/Class.hs+4
@@ -10,6 +10,9 @@
1010{-# LANGUAGE ScopedTypeVariables #-}
1111{-# LANGUAGE RankNTypes #-}
1212{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13+
14+-- | See "Control.Monad.State.Class".
15+
1316module Control.Monad.Ether.State.Class
1417 ( MonadState(..)
1518 , modify
@@ -36,6 +39,7 @@ import qualified Control.Monad.Trans.State.Strict as Trans.Strict (StateT)
3639import qualified Control.Monad.Trans.Writer.Lazy as Trans.Lazy (WriterT)
3740import qualified Control.Monad.Trans.Writer.Strict as Trans.Strict (WriterT)
3841
42+-- | See 'Control.Monad.State.MonadState'.
3943class Monad m => MonadState tag s m | m tag -> s where
4044
4145 {-# MINIMAL state | get, put #-}
src/Control/Monad/Ether/State/Lazy.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.State.Lazy".
3+
14module Control.Monad.Ether.State.Lazy
25 (
36 -- * MonadState class
src/Control/Monad/Ether/State/Strict.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.State.Strict".
3+
14module Control.Monad.Ether.State.Strict
25 (
36 -- * MonadState class
src/Control/Monad/Ether/Writer.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.Writer".
3+
14module Control.Monad.Ether.Writer
25 (
36 -- * MonadWriter class
src/Control/Monad/Ether/Writer/Class.hs+7
@@ -10,6 +10,9 @@
1010{-# LANGUAGE ScopedTypeVariables #-}
1111{-# LANGUAGE RankNTypes #-}
1212{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13+
14+-- | See "Control.Monad.Writer.Class".
15+
1316module Control.Monad.Ether.Writer.Class
1417 ( MonadWriter(..)
1518 , listens
@@ -36,6 +39,7 @@ import qualified Control.Monad.Trans.State.Lazy as Trans.S.Lazy
3639import qualified Control.Monad.Trans.State.Strict as Trans.S.Strict
3740import qualified Control.Monad.Trans.Writer.Lazy as Trans.W.Lazy
3841
42+-- | See 'Control.Monad.Writer.MonadWriter'.
3943class (Monoid w, Monad m) => MonadWriter tag w m | m tag -> w where
4044
4145 {-# MINIMAL (writer | tell), listen, pass #-}
@@ -58,11 +62,14 @@ class (Monoid w, Monad m) => MonadWriter tag w m | m tag -> w where
5862 -- and return the value, applying the function to the accumulator.
5963 pass :: proxy tag -> m (a, w -> w) -> m a
6064
65+-- | Execute an action and add the result of applying the given function to
66+-- its accumulator to the value of the computation.
6167listens :: MonadWriter tag w m => proxy tag -> (w -> b) -> m a -> m (a, b)
6268listens t f m = do
6369 ~(a, w) <- listen t m
6470 return (a, f w)
6571
72+-- | Execute an action and apply a function to its accumulator.
6673censor :: MonadWriter tag w m => proxy tag -> (w -> w) -> m a -> m a
6774censor t f m = pass t $ do
6875 a <- m
src/Control/Monad/Trans/Ether/Except.hs+5
@@ -7,6 +7,9 @@
77{-# LANGUAGE DataKinds #-}
88{-# LANGUAGE TypeOperators #-}
99{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+
11+-- | See "Control.Monad.Trans.Except".
12+
1013module Control.Monad.Trans.Ether.Except
1114 (
1215 -- * The Except monad
@@ -105,9 +108,11 @@ mapExceptT
105108 -> ExceptT tag e' n b
106109mapExceptT t f m = tagged t $ Trans.mapExceptT f (coerce m)
107110
111+-- | Is used within a monadic computation to begin exception processing.
108112throw :: Monad m => proxy tag -> e -> ExceptT tag e m a
109113throw t = tagged t . Trans.throwE
110114
115+-- | A handler function to handle previous exceptions and return to normal execution.
111116catch :: Monad m => proxy tag -> ExceptT tag e m a -> (e -> ExceptT tag e m a) -> ExceptT tag e m a
112117catch t m h = tagged t $ Trans.catchE (coerce m) (coerce . h)
113118
src/Control/Monad/Trans/Ether/Reader.hs+3
@@ -7,6 +7,9 @@
77{-# LANGUAGE DataKinds #-}
88{-# LANGUAGE TypeOperators #-}
99{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+
11+-- | See "Control.Monad.Trans.Reader".
12+
1013module Control.Monad.Trans.Ether.Reader
1114 (
1215 -- * The Reader monad
src/Control/Monad/Trans/Ether/State.hs+3
@@ -1,3 +1,6 @@
1+
2+-- | See "Control.Monad.Trans.State".
3+
14module Control.Monad.Trans.Ether.State
25 ( module Control.Monad.Trans.Ether.State.Lazy
36 ) where
src/Control/Monad/Trans/Ether/State/Lazy.hs+3
@@ -7,6 +7,9 @@
77{-# LANGUAGE DataKinds #-}
88{-# LANGUAGE TypeOperators #-}
99{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+
11+-- | See "Control.Monad.Trans.State.Lazy".
12+
1013module Control.Monad.Trans.Ether.State.Lazy
1114 (
1215 -- * The State monad
src/Control/Monad/Trans/Ether/State/Strict.hs+3
@@ -7,6 +7,9 @@
77{-# LANGUAGE DataKinds #-}
88{-# LANGUAGE TypeOperators #-}
99{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+
11+-- | See "Control.Monad.Trans.State.Strict".
12+
1013module Control.Monad.Trans.Ether.State.Strict
1114 (
1215 -- * The State monad
src/Control/Monad/Trans/Ether/Writer.hs+6
@@ -7,6 +7,9 @@
77{-# LANGUAGE DataKinds #-}
88{-# LANGUAGE TypeOperators #-}
99{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+
11+-- | See "Control.Monad.Trans.Writer".
12+
1013module Control.Monad.Trans.Ether.Writer
1114 (
1215 -- * The Writer monad
@@ -118,9 +121,12 @@ mapWriterT t f m = tagged t $ Trans.mapWriterT f (coerce m)
118121tell :: Monad m => proxy tag -> w -> WriterT tag w m ()
119122tell t w = writer t ((), w)
120123
124+-- | Executes an action and adds its accumulator to the value of the computation.
121125listen :: (Monoid w, Monad m) => proxy tag -> WriterT tag w m a -> WriterT tag w m (a, w)
122126listen t m = tagged t $ Trans.listen (coerce m)
123127
128+-- | Executes an action which returns a value and a function, and returns the
129+-- value, applying the function to the accumulator.
124130pass :: (Monoid w, Monad m) => proxy tag -> WriterT tag w m (a, w -> w) -> WriterT tag w m a
125131pass t m = tagged t $ Trans.pass (coerce m)
126132
test/Regression.hs+7−10
@@ -153,22 +153,19 @@ factorial' a = factorial (a :: Int)
153153
154154data DivideByZero = DivideByZero
155155 deriving (Show)
156−data NegativeLog = NegativeLog
156+data NegativeLog a = NegativeLog a
157157 deriving (Show)
158158
159159exceptCore
160160 :: ( Floating a, Ord a
161161 , I.MonadExcept DivideByZero m
162− , I.MonadExcept NegativeLog m
162+ , I.MonadExcept (NegativeLog a) m
163163 ) => a -> a -> m a
164164exceptCore a b = do
165− if b == 0
166− then I.throw DivideByZero
167− else
168− let d = a /b
169− in if d < 0
170− then I.throw NegativeLog
171− else return $ log (a / b)
165+ T.when (b == 0) (I.throw DivideByZero)
166+ let d = a /b
167+ T.when (d < 0) (I.throw (NegativeLog d))
168+ return (log d)
172169
173170try0 :: m a -> m a
174171try0 = $(try 0)
@@ -186,7 +183,7 @@ exceptCore' :: Double -> Double -> String
186183exceptCore' a b = runIdentity $ do
187184 $(try 2)
188185 (show <$> exceptCore a b)
189− (\NegativeLog -> "nl")
186+ (\(NegativeLog (x::Double)) -> "nl: " ++ show x)
190187 (\DivideByZero -> "dz")
191188
192189summatorCore