on master help sign in
Ambiguous default for MonadWriter
commit47b2ec6b08d3b25a55bc02a5555e67f646cfc969 authorIndex Int <vlad.z.4096@gmail.com> dateSat 30 Apr 2016, 18:58 (10 years ago) parent15f3a29
7 files changed+74−120
ether.cabal−1 src/Control/Monad/Ether/Ambiguous.hs+1−1 src/Control/Monad/Ether/Ambiguous/Writer.hs−74 src/Control/Monad/Ether/Implicit/Writer.hs+1−1 src/Control/Monad/Ether/Writer.hs+43−2 src/Control/Monad/Ether/Writer/Class.hs+8−21 src/Control/Monad/Trans/Ether/Writer.hs+21−20
ether.cabal−1
@@ -55,7 +55,6 @@ library
5555 Control.Monad.Ether.Implicit.State.Strict
5656 Control.Monad.Ether.Implicit.Except
5757 Control.Monad.Ether.Ambiguous
58− Control.Monad.Ether.Ambiguous.Writer
5958 Control.Monad.Ether.Ambiguous.State
6059 Control.Monad.Ether.Ambiguous.State.Lazy
6160 Control.Monad.Ether.Ambiguous.State.Strict
src/Control/Monad/Ether/Ambiguous.hs+1−1
@@ -7,7 +7,7 @@ module Control.Monad.Ether.Ambiguous (module X) where
77import Control.Monad as X
88import Control.Monad.Fix as X
99import Control.Monad.Ether.Reader as X
10−import Control.Monad.Ether.Ambiguous.Writer as X
10+import Control.Monad.Ether.Writer as X
1111import Control.Monad.Ether.Ambiguous.State as X
1212import Control.Monad.Ether.Ambiguous.Except as X
1313import Control.Monad.Trans.Ether.Dispatch as X
src/Control/Monad/Ether/Ambiguous/Writer.hsdeleted−74
@@ -1,74 +0,0 @@
1−{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE MagicHash #-}
4−
5−-- | See "Control.Monad.Ether.Writer".
6−
7−module Control.Monad.Ether.Ambiguous.Writer
8− (
9− -- * MonadWriter class
10− MonadWriter
11− , writer
12− , tell
13− , listen
14− , pass
15− , listens
16− , censor
17− -- * The Writer monad
18− , Writer
19− , runWriter
20− , execWriter
21− -- * The WriterT monad transformer
22− , WriterT
23− , writerT
24− , runWriterT
25− , execWriterT
26− ) where
27−
28−import GHC.Prim (Proxy#, proxy#)
29−import Control.Monad.Ether.Writer (MonadWriter, WriterT, Writer)
30−import qualified Control.Monad.Ether.Writer as Explicit
31−
32−-- | See 'Control.Monad.Ether.Writer.writer'.
33−writer :: forall tag w m a . MonadWriter tag w m => (a, w) -> m a
34−writer = Explicit.writer (proxy# :: Proxy# tag)
35−
36−-- | See 'Control.Monad.Ether.Writer.tell'.
37−tell :: forall tag w m . MonadWriter tag w m => w -> m ()
38−tell = Explicit.tell (proxy# :: Proxy# tag)
39−
40−-- | See 'Control.Monad.Ether.Writer.listen'.
41−listen :: forall tag w m a . MonadWriter tag w m => m a -> m (a, w)
42−listen = Explicit.listen (proxy# :: Proxy# tag)
43−
44−-- | See 'Control.Monad.Ether.Writer.pass'.
45−pass :: forall tag w m a . MonadWriter tag w m => m (a, w -> w) -> m a
46−pass = Explicit.pass (proxy# :: Proxy# tag)
47−
48−-- | See 'Control.Monad.Ether.Writer.listens'.
49−listens :: forall tag w m a b . MonadWriter tag w m => (w -> b) -> m a -> m (a, b)
50−listens = Explicit.listens (proxy# :: Proxy# tag)
51−
52−-- | See 'Control.Monad.Ether.Writer.censor'.
53−censor :: forall tag w m a . MonadWriter tag w m => (w -> w) -> m a -> m a
54−censor = Explicit.censor (proxy# :: Proxy# tag)
55−
56−-- | See 'Control.Monad.Ether.Writer.runWriter'.
57−runWriter :: forall tag w a . Writer tag w a -> (a, w)
58−runWriter = Explicit.runWriter proxy#
59−
60−-- | See 'Control.Monad.Ether.Writer.execWriter'.
61−execWriter :: forall tag w a . Writer tag w a -> w
62−execWriter = Explicit.execWriter proxy#
63−
64−-- | See 'Control.Monad.Ether.Writer.writerT'.
65−writerT :: forall tag w m a . m (a, w) -> WriterT tag w m a
66−writerT = Explicit.writerT proxy#
67−
68−-- | See 'Control.Monad.Ether.Writer.runWriterT'.
69−runWriterT :: forall tag w m a . WriterT tag w m a -> m (a, w)
70−runWriterT = Explicit.runWriterT proxy#
71−
72−-- | See 'Control.Monad.Ether.Writer.execWriterT'.
73−execWriterT :: forall tag w m a . Monad m => WriterT tag w m a -> m w
74−execWriterT = Explicit.execWriterT proxy#
src/Control/Monad/Ether/Implicit/Writer.hs+1−1
@@ -25,7 +25,7 @@ module Control.Monad.Ether.Implicit.Writer
2525 , execWriterT
2626 ) where
2727
28−import qualified Control.Monad.Ether.Ambiguous.Writer as A
28+import qualified Control.Monad.Ether.Writer as A
2929
3030-- | See 'Control.Monad.Ether.Writer.MonadWriter'.
3131type MonadWriter w = A.MonadWriter w w
src/Control/Monad/Ether/Writer.hs+43−2
@@ -1,10 +1,18 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
2+{-# LANGUAGE TypeApplications #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE MagicHash #-}
15
26-- | See "Control.Monad.Writer".
37
48module Control.Monad.Ether.Writer
59 (
610 -- * MonadWriter class
7− MonadWriter(..)
11+ MonadWriter
12+ , writer
13+ , tell
14+ , listen
15+ , pass
816 , listens
917 , censor
1018 -- * The Writer monad
@@ -18,5 +26,38 @@ module Control.Monad.Ether.Writer
1826 , execWriterT
1927 ) where
2028
21−import Control.Monad.Ether.Writer.Class
29+import GHC.Prim (Proxy#, proxy#)
30+import Control.Monad.Ether.Writer.Class (MonadWriter)
31+import qualified Control.Monad.Ether.Writer.Class as C
2232import Control.Monad.Trans.Ether.Writer hiding (writer, tell, listen, pass)
33+
34+-- | Embed a simple writer action.
35+writer :: forall tag w m a . MonadWriter tag w m => (a, w) -> m a
36+writer = C.writer (proxy# :: Proxy# tag)
37+
38+-- | Append a value to the accumulator within the monad.
39+tell :: forall tag w m . MonadWriter tag w m => w -> m ()
40+tell = C.tell (proxy# :: Proxy# tag)
41+
42+-- | Execute an action and add its accumulator
43+-- to the value of the computation.
44+listen :: forall tag w m a . MonadWriter tag w m => m a -> m (a, w)
45+listen = C.listen (proxy# :: Proxy# tag)
46+
47+-- | Execute an action which returns a value and a function,
48+-- and return the value, applying the function to the accumulator.
49+pass :: forall tag w m a . MonadWriter tag w m => m (a, w -> w) -> m a
50+pass = C.pass (proxy# :: Proxy# tag)
51+
52+-- | Execute an action and add the result of applying the given function to
53+-- its accumulator to the value of the computation.
54+listens :: forall tag w m a b . MonadWriter tag w m => (w -> b) -> m a -> m (a, b)
55+listens f m = do
56+ ~(a, w) <- listen @tag m
57+ return (a, f w)
58+
59+-- | Execute an action and apply a function to its accumulator.
60+censor :: forall tag w m a . MonadWriter tag w m => (w -> w) -> m a -> m a
61+censor f m = pass @tag $ do
62+ a <- m
63+ return (a, f)
src/Control/Monad/Ether/Writer/Class.hs+8−21
@@ -4,15 +4,15 @@
44{-# LANGUAGE FunctionalDependencies #-}
55{-# LANGUAGE TypeFamilies #-}
66{-# LANGUAGE PolyKinds #-}
7+{-# LANGUAGE TypeApplications #-}
8+{-# LANGUAGE ScopedTypeVariables #-}
79{-# LANGUAGE MagicHash #-}
810
911-- | See "Control.Monad.Writer.Class".
1012
1113module Control.Monad.Ether.Writer.Class
12− ( MonadWriter(..)
13− , listens
14− , censor
15− ) where
14+ ( MonadWriter(..)
15+ ) where
1616
1717import GHC.Prim (Proxy#)
1818import qualified Control.Monad.Trans.Ether.Writer as W
@@ -42,24 +42,11 @@ class (Monoid w, Monad m) => MonadWriter tag w m | m tag -> w where
4242 -- and return the value, applying the function to the accumulator.
4343 pass :: Proxy# tag -> m (a, w -> w) -> m a
4444
45−-- | Execute an action and add the result of applying the given function to
46−-- its accumulator to the value of the computation.
47−listens :: MonadWriter tag w m => Proxy# tag -> (w -> b) -> m a -> m (a, b)
48−listens t f m = do
49− ~(a, w) <- listen t m
50− return (a, f w)
51−
52−-- | Execute an action and apply a function to its accumulator.
53−censor :: MonadWriter tag w m => Proxy# tag -> (w -> w) -> m a -> m a
54−censor t f m = pass t $ do
55− a <- m
56− return (a, f)
57−
5845instance (Monoid w, Monad m, w ~ w') => MonadWriter tag w (W.WriterT tag w' m) where
59− writer = W.writer
60− tell = W.tell
61− listen = W.listen
62− pass = W.pass
46+ writer _ = W.writer @tag
47+ tell _ = W.tell @tag
48+ listen _ = W.listen @tag
49+ pass _ = W.pass @tag
6350
6451instance {-# OVERLAPPABLE #-}
6552 ( Lift.LiftListen t
src/Control/Monad/Trans/Ether/Writer.hs+21−20
@@ -6,7 +6,9 @@
66{-# LANGUAGE DataKinds #-}
77{-# LANGUAGE PolyKinds #-}
88{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9−{-# LANGUAGE MagicHash #-}
9+{-# LANGUAGE AllowAmbiguousTypes #-}
10+{-# LANGUAGE TypeApplications #-}
11+{-# LANGUAGE ScopedTypeVariables #-}
1012
1113-- | See "Control.Monad.Trans.Writer".
1214
@@ -28,7 +30,6 @@ module Control.Monad.Trans.Ether.Writer
2830 , pass
2931 ) where
3032
31−import GHC.Prim (Proxy#)
3233import Data.Functor.Identity (Identity(..))
3334import qualified Control.Monad.Trans.Writer.Lazy as Trans
3435import Control.Monad.Trans.Ether.Tagged
@@ -48,43 +49,43 @@ type Writer tag w = WriterT tag w Identity
4849type WriterT tag w = TaggedTrans tag (Trans.WriterT w)
4950
5051-- | Constructor for computations in the writer monad transformer.
51−writerT :: Proxy# tag -> m (a, w) -> WriterT tag w m a
52−writerT _ = pack . Trans.WriterT
52+writerT :: forall tag w m a . m (a, w) -> WriterT tag w m a
53+writerT = pack . Trans.WriterT
5354
5455-- | Constructor for computations in the writer monad
5556-- (the inverse of 'runWriter').
56−writer :: Monad m => Proxy# tag -> (a, w) -> WriterT tag w m a
57−writer _ = pack . Trans.writer
57+writer :: forall tag w m a . Monad m => (a, w) -> WriterT tag w m a
58+writer = pack . Trans.writer
5859
5960-- | Runs a 'WriterT' and returns both the normal value
6061-- and the final accumulator.
61−runWriterT :: Proxy# tag -> WriterT tag w m a -> m (a, w)
62−runWriterT _ = Trans.runWriterT . unpack
62+runWriterT :: forall tag w m a . WriterT tag w m a -> m (a, w)
63+runWriterT = Trans.runWriterT . unpack
6364
6465-- | Runs a 'Writer' and returns both the normal value
6566-- and the final accumulator.
66−runWriter :: Proxy# tag -> Writer tag w a -> (a, w)
67−runWriter _ = Trans.runWriter . unpack
67+runWriter :: forall tag w a . Writer tag w a -> (a, w)
68+runWriter = Trans.runWriter . unpack
6869
6970-- | Runs a 'WriterT' and returns the final accumulator,
7071-- discarding the normal value.
71−execWriterT :: Monad m => Proxy# tag -> WriterT tag w m a -> m w
72−execWriterT _ = Trans.execWriterT . unpack
72+execWriterT :: forall tag w m a . Monad m => WriterT tag w m a -> m w
73+execWriterT = Trans.execWriterT . unpack
7374
7475-- | Runs a 'Writer' and returns the final accumulator,
7576-- discarding the normal value.
76−execWriter :: Proxy# tag -> Writer tag w a -> w
77−execWriter _ = Trans.execWriter . unpack
77+execWriter :: forall tag w a . Writer tag w a -> w
78+execWriter = Trans.execWriter . unpack
7879
7980-- | Appends a value to the accumulator within the monad.
80−tell :: Monad m => Proxy# tag -> w -> WriterT tag w m ()
81−tell t w = writer t ((), w)
81+tell :: forall tag w m . Monad m => w -> WriterT tag w m ()
82+tell w = writer @tag ((), w)
8283
8384-- | Executes an action and adds its accumulator to the value of the computation.
84−listen :: Monad m => Proxy# tag -> WriterT tag w m a -> WriterT tag w m (a, w)
85−listen _ = pack . Trans.listen . unpack
85+listen :: forall tag w m a . Monad m => WriterT tag w m a -> WriterT tag w m (a, w)
86+listen = pack . Trans.listen . unpack
8687
8788-- | Executes an action which returns a value and a function, and returns the
8889-- value, applying the function to the accumulator.
89−pass :: Monad m => Proxy# tag -> WriterT tag w m (a, w -> w) -> WriterT tag w m a
90−pass _ = pack . Trans.pass . unpack
90+pass :: forall tag w m a . Monad m => WriterT tag w m (a, w -> w) -> WriterT tag w m a
91+pass = pack . Trans.pass . unpack