on master help sign in
Proxy-hash for MonadWriter
commit7a9ddd003878bddc88854c1c1f1c8b3eade7f7e0 authorIndex Int <vlad.z.4096@gmail.com> dateSat 30 Apr 2016, 13:38 (10 years ago) parentcdd0909
4 files changed+40−35
src/Control/Monad/Ether/Implicit/Writer.hs+13−12 src/Control/Monad/Ether/Writer/Class.hs+8−6 src/Control/Monad/Trans/Ether/Dispatch.hs+8−8 src/Control/Monad/Trans/Ether/Writer.hs+11−9
src/Control/Monad/Ether/Implicit/Writer.hs+13−12
@@ -1,5 +1,6 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE MagicHash #-}
34
45-- | See "Control.Monad.Ether.Writer".
56
@@ -24,7 +25,7 @@ module Control.Monad.Ether.Implicit.Writer
2425 , execWriterT
2526 ) where
2627
27−import Data.Proxy
28+import GHC.Prim (Proxy#, proxy#)
2829import qualified Control.Monad.Ether.Writer as Explicit
2930
3031-- | See 'Control.Monad.Ether.Writer.MonadWriter'.
@@ -32,50 +33,50 @@ type MonadWriter w = Explicit.MonadWriter w w
3233
3334-- | See 'Control.Monad.Ether.Writer.writer'.
3435writer :: forall w m a . MonadWriter w m => (a, w) -> m a
35−writer = Explicit.writer (Proxy :: Proxy w)
36+writer = Explicit.writer (proxy# :: Proxy# w)
3637
3738-- | See 'Control.Monad.Ether.Writer.tell'.
3839tell :: forall w m . MonadWriter w m => w -> m ()
39−tell = Explicit.tell (Proxy :: Proxy w)
40+tell = Explicit.tell (proxy# :: Proxy# w)
4041
4142-- | See 'Control.Monad.Ether.Writer.listen'.
4243listen :: forall w m a . MonadWriter w m => m a -> m (a, w)
43−listen = Explicit.listen (Proxy :: Proxy w)
44+listen = Explicit.listen (proxy# :: Proxy# w)
4445
4546-- | See 'Control.Monad.Ether.Writer.pass'.
4647pass :: forall w m a . MonadWriter w m => m (a, w -> w) -> m a
47−pass = Explicit.pass (Proxy :: Proxy w)
48+pass = Explicit.pass (proxy# :: Proxy# w)
4849
4950-- | See 'Control.Monad.Ether.Writer.listens'.
5051listens :: forall w m a b . MonadWriter w m => (w -> b) -> m a -> m (a, b)
51−listens = Explicit.listens (Proxy :: Proxy w)
52+listens = Explicit.listens (proxy# :: Proxy# w)
5253
5354-- | See 'Control.Monad.Ether.Writer.censor'.
5455censor :: forall w m a . MonadWriter w m => (w -> w) -> m a -> m a
55−censor = Explicit.censor (Proxy :: Proxy w)
56+censor = Explicit.censor (proxy# :: Proxy# w)
5657
5758-- | See 'Control.Monad.Ether.Writer.Writer'.
5859type Writer w = Explicit.Writer w w
5960
6061-- | See 'Control.Monad.Ether.Writer.runWriter'.
6162runWriter :: Writer w a -> (a, w)
62−runWriter = Explicit.runWriter Proxy
63+runWriter = Explicit.runWriter proxy#
6364
6465-- | See 'Control.Monad.Ether.Writer.execWriter'.
6566execWriter :: Writer w a -> w
66−execWriter = Explicit.execWriter Proxy
67+execWriter = Explicit.execWriter proxy#
6768
6869-- | See 'Control.Monad.Ether.Writer.WriterT'.
6970type WriterT w = Explicit.WriterT w w
7071
7172-- | See 'Control.Monad.Ether.Writer.writerT'.
7273writerT :: m (a, w) -> WriterT w m a
73−writerT = Explicit.writerT Proxy
74+writerT = Explicit.writerT proxy#
7475
7576-- | See 'Control.Monad.Ether.Writer.runWriterT'.
7677runWriterT :: WriterT w m a -> m (a, w)
77−runWriterT = Explicit.runWriterT Proxy
78+runWriterT = Explicit.runWriterT proxy#
7879
7980-- | See 'Control.Monad.Ether.Writer.execWriterT'.
8081execWriterT :: Monad m => WriterT w m a -> m w
81−execWriterT = Explicit.execWriterT Proxy
82+execWriterT = Explicit.execWriterT proxy#
src/Control/Monad/Ether/Writer/Class.hs+8−6
@@ -11,6 +11,7 @@
1111{-# LANGUAGE FunctionalDependencies #-}
1212{-# LANGUAGE TypeFamilies #-}
1313{-# LANGUAGE PolyKinds #-}
14+{-# LANGUAGE MagicHash #-}
1415
1516-- | See "Control.Monad.Writer.Class".
1617
@@ -24,6 +25,7 @@ module Control.Monad.Ether.Writer.Class
2425import Data.Monoid
2526#endif
2627
28+import GHC.Prim (Proxy#)
2729import qualified Control.Monad.Trans.Ether.Writer as W
2830import qualified Control.Monad.Trans.Lift.Listen as Lift
2931import qualified Control.Monad.Trans.Lift.Pass as Lift
@@ -34,32 +36,32 @@ class (Monoid w, Monad m) => MonadWriter tag w m | m tag -> w where
3436 {-# MINIMAL (writer | tell), listen, pass #-}
3537
3638 -- | Embed a simple writer action.
37− writer :: proxy tag -> (a, w) -> m a
39+ writer :: Proxy# tag -> (a, w) -> m a
3840 writer t ~(a, w) = do
3941 tell t w
4042 return a
4143
4244 -- | Append a value to the accumulator within the monad.
43− tell :: proxy tag -> w -> m ()
45+ tell :: Proxy# tag -> w -> m ()
4446 tell t w = writer t ((),w)
4547
4648 -- | Execute an action and add its accumulator
4749 -- to the value of the computation.
48− listen :: proxy tag -> m a -> m (a, w)
50+ listen :: Proxy# tag -> m a -> m (a, w)
4951
5052 -- | Execute an action which returns a value and a function,
5153 -- and return the value, applying the function to the accumulator.
52− pass :: proxy tag -> m (a, w -> w) -> m a
54+ pass :: Proxy# tag -> m (a, w -> w) -> m a
5355
5456-- | Execute an action and add the result of applying the given function to
5557-- its accumulator to the value of the computation.
56−listens :: MonadWriter tag w m => proxy tag -> (w -> b) -> m a -> m (a, b)
58+listens :: MonadWriter tag w m => Proxy# tag -> (w -> b) -> m a -> m (a, b)
5759listens t f m = do
5860 ~(a, w) <- listen t m
5961 return (a, f w)
6062
6163-- | Execute an action and apply a function to its accumulator.
62−censor :: MonadWriter tag w m => proxy tag -> (w -> w) -> m a -> m a
64+censor :: MonadWriter tag w m => Proxy# tag -> (w -> w) -> m a -> m a
6365censor t f m = pass t $ do
6466 a <- m
6567 return (a, f)
src/Control/Monad/Trans/Ether/Dispatch.hs+8−8
@@ -180,10 +180,10 @@ instance MonadExcept tag e m
180180
181181instance MonadWriter tag w m
182182 => Class.MonadWriter w (DispatchTagAttachT tag m) where
183− writer = let t = Proxy :: Proxy tag in Lift.lift . writer t
184− tell = let t = Proxy :: Proxy tag in Lift.lift . tell t
185− listen = let t = Proxy :: Proxy tag in Lift.liftListen (listen t)
186− pass = let t = Proxy :: Proxy tag in Lift.liftPass (pass t)
183+ writer = let t = proxy# :: Proxy# tag in Lift.lift . writer t
184+ tell = let t = proxy# :: Proxy# tag in Lift.lift . tell t
185+ listen = let t = proxy# :: Proxy# tag in Lift.liftListen (listen t)
186+ pass = let t = proxy# :: Proxy# tag in Lift.liftPass (pass t)
187187
188188
189189-- TagReplace instances
@@ -205,7 +205,7 @@ instance MonadExcept tNew e m
205205
206206instance MonadWriter tNew w m
207207 => MonadWriter tOld w (DispatchTagReplaceT tOld tNew m) where
208− writer _ = let t = Proxy :: Proxy tNew in Lift.lift . writer t
209− tell _ = let t = Proxy :: Proxy tNew in Lift.lift . tell t
210− listen _ = let t = Proxy :: Proxy tNew in Lift.liftListen (listen t)
211− pass _ = let t = Proxy :: Proxy tNew in Lift.liftPass (pass t)
208+ writer _ = let t = proxy# :: Proxy# tNew in Lift.lift . writer t
209+ tell _ = let t = proxy# :: Proxy# tNew in Lift.lift . tell t
210+ listen _ = let t = proxy# :: Proxy# tNew in Lift.liftListen (listen t)
211+ pass _ = let t = proxy# :: Proxy# tNew in Lift.liftPass (pass t)
src/Control/Monad/Trans/Ether/Writer.hs+11−9
@@ -7,6 +7,7 @@
77{-# LANGUAGE DataKinds #-}
88{-# LANGUAGE PolyKinds #-}
99{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10+{-# LANGUAGE MagicHash #-}
1011
1112-- | See "Control.Monad.Trans.Writer".
1213
@@ -32,6 +33,7 @@ module Control.Monad.Trans.Ether.Writer
3233import Data.Monoid
3334#endif
3435
36+import GHC.Prim (Proxy#)
3537import Data.Functor.Identity (Identity(..))
3638import qualified Control.Monad.Trans.Writer.Lazy as Trans
3739import Control.Monad.Trans.Ether.Tagged
@@ -51,36 +53,36 @@ type Writer tag w = WriterT tag w Identity
5153type WriterT tag w = TaggedTrans tag (Trans.WriterT w)
5254
5355-- | Constructor for computations in the writer monad transformer.
54−writerT :: proxy tag -> m (a, w) -> WriterT tag w m a
56+writerT :: Proxy# tag -> m (a, w) -> WriterT tag w m a
5557writerT _ = pack . Trans.WriterT
5658
5759-- | Constructor for computations in the writer monad
5860-- (the inverse of 'runWriter').
59−writer :: Monad m => proxy tag -> (a, w) -> WriterT tag w m a
61+writer :: Monad m => Proxy# tag -> (a, w) -> WriterT tag w m a
6062writer _ = pack . Trans.writer
6163
6264-- | Runs a 'WriterT' and returns both the normal value
6365-- and the final accumulator.
64−runWriterT :: proxy tag -> WriterT tag w m a -> m (a, w)
66+runWriterT :: Proxy# tag -> WriterT tag w m a -> m (a, w)
6567runWriterT _ = Trans.runWriterT . unpack
6668
6769-- | Runs a 'Writer' and returns both the normal value
6870-- and the final accumulator.
69−runWriter :: proxy tag -> Writer tag w a -> (a, w)
71+runWriter :: Proxy# tag -> Writer tag w a -> (a, w)
7072runWriter _ = Trans.runWriter . unpack
7173
7274-- | Runs a 'WriterT' and returns the final accumulator,
7375-- discarding the normal value.
74−execWriterT :: Monad m => proxy tag -> WriterT tag w m a -> m w
76+execWriterT :: Monad m => Proxy# tag -> WriterT tag w m a -> m w
7577execWriterT _ = Trans.execWriterT . unpack
7678
7779-- | Runs a 'Writer' and returns the final accumulator,
7880-- discarding the normal value.
79−execWriter :: proxy tag -> Writer tag w a -> w
81+execWriter :: Proxy# tag -> Writer tag w a -> w
8082execWriter _ = Trans.execWriter . unpack
8183
8284-- | Appends a value to the accumulator within the monad.
83−tell :: Monad m => proxy tag -> w -> WriterT tag w m ()
85+tell :: Monad m => Proxy# tag -> w -> WriterT tag w m ()
8486tell t w = writer t ((), w)
8587
8688-- | Executes an action and adds its accumulator to the value of the computation.
@@ -90,7 +92,7 @@ listen
9092#else
9193 :: Monad m
9294#endif
93− => proxy tag -> WriterT tag w m a -> WriterT tag w m (a, w)
95+ => Proxy# tag -> WriterT tag w m a -> WriterT tag w m (a, w)
9496listen _ = pack . Trans.listen . unpack
9597
9698-- | Executes an action which returns a value and a function, and returns the
@@ -101,5 +103,5 @@ pass
101103#else
102104 :: Monad m
103105#endif
104− => proxy tag -> WriterT tag w m (a, w -> w) -> WriterT tag w m a
106+ => Proxy# tag -> WriterT tag w m (a, w -> w) -> WriterT tag w m a
105107pass _ = pack . Trans.pass . unpack