on master help sign in
Proxy-hash for MonadState
commit3b7d34397cbb0232a10b27c7765701fb74a7d7dd authorIndex Int <vlad.z.4096@gmail.com> dateSat 30 Apr 2016, 14:00 (10 years ago) parent7a9ddd0
6 files changed+63−56
src/Control/Monad/Ether/Implicit/State/Lazy.hs+14−13 src/Control/Monad/Ether/Implicit/State/Strict.hs+14−13 src/Control/Monad/Ether/State/Class.hs+7−5 src/Control/Monad/Trans/Ether/Dispatch.hs+4−5 src/Control/Monad/Trans/Ether/State/Lazy.hs+12−10 src/Control/Monad/Trans/Ether/State/Strict.hs+12−10
src/Control/Monad/Ether/Implicit/State/Lazy.hs+14−13
@@ -1,5 +1,6 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE MagicHash #-}
34
45-- | See "Control.Monad.Ether.State.Lazy".
56
@@ -25,7 +26,7 @@ module Control.Monad.Ether.Implicit.State.Lazy
2526 , execStateT
2627 ) where
2728
28−import Data.Proxy
29+import GHC.Prim (Proxy#, proxy#)
2930import qualified Control.Monad.Ether.State.Lazy as Explicit
3031
3132-- | See 'Control.Monad.Ether.State.Lazy.StateT'.
@@ -36,51 +37,51 @@ type State s = Explicit.State s s
3637
3738-- | See 'Control.Monad.Ether.State.Lazy.stateT'.
3839stateT :: (s -> m (a, s)) -> StateT s m a
39−stateT = Explicit.stateT Proxy
40+stateT = Explicit.stateT proxy#
4041
4142-- | See 'Control.Monad.Ether.State.Lazy.runStateT'.
4243runStateT :: StateT s m a -> s -> m (a, s)
43−runStateT = Explicit.runStateT Proxy
44+runStateT = Explicit.runStateT proxy#
4445
4546-- | See 'Control.Monad.Ether.State.Lazy.runState'.
4647runState :: State s a -> s -> (a, s)
47−runState = Explicit.runState Proxy
48+runState = Explicit.runState proxy#
4849
4950-- | See 'Control.Monad.Ether.State.Lazy.evalStateT'.
5051evalStateT :: Monad m => StateT s m a -> s -> m a
51−evalStateT = Explicit.evalStateT Proxy
52+evalStateT = Explicit.evalStateT proxy#
5253
5354-- | See 'Control.Monad.Ether.State.Lazy.evalState'.
5455evalState :: State s a -> s -> a
55−evalState = Explicit.evalState Proxy
56+evalState = Explicit.evalState proxy#
5657
5758-- | See 'Control.Monad.Ether.State.Lazy.execStateT'.
5859execStateT :: Monad m => StateT s m a -> s -> m s
59−execStateT = Explicit.execStateT Proxy
60+execStateT = Explicit.execStateT proxy#
6061
6162-- | See 'Control.Monad.Ether.State.Lazy.execState'.
6263execState :: State s a -> s -> s
63−execState = Explicit.execState Proxy
64+execState = Explicit.execState proxy#
6465
6566-- | See 'Control.Monad.Ether.State.Lazy.MonadState'.
6667type MonadState s = Explicit.MonadState s s
6768
6869-- | See 'Control.Monad.Ether.State.Lazy.get'.
6970get :: forall s m . MonadState s m => m s
70−get = Explicit.get (Proxy :: Proxy s)
71+get = Explicit.get (proxy# :: Proxy# s)
7172
7273-- | See 'Control.Monad.Ether.State.Lazy.gets'.
7374gets :: forall s m a . MonadState s m => (s -> a) -> m a
74−gets = Explicit.gets (Proxy :: Proxy s)
75+gets = Explicit.gets (proxy# :: Proxy# s)
7576
7677-- | See 'Control.Monad.Ether.State.Lazy.put'.
7778put :: forall s m . MonadState s m => s -> m ()
78−put = Explicit.put (Proxy :: Proxy s)
79+put = Explicit.put (proxy# :: Proxy# s)
7980
8081-- | See 'Control.Monad.Ether.State.Lazy.state'.
8182state :: forall s m a . MonadState s m => (s -> (a, s)) -> m a
82−state = Explicit.state (Proxy :: Proxy s)
83+state = Explicit.state (proxy# :: Proxy# s)
8384
8485-- | See 'Control.Monad.Ether.State.Lazy.modify'.
8586modify :: forall s m . MonadState s m => (s -> s) -> m ()
86−modify = Explicit.modify (Proxy :: Proxy s)
87+modify = Explicit.modify (proxy# :: Proxy# s)
src/Control/Monad/Ether/Implicit/State/Strict.hs+14−13
@@ -1,5 +1,6 @@
11{-# LANGUAGE ConstraintKinds #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE MagicHash #-}
34
45-- | See "Control.Monad.Ether.State.Strict".
56
@@ -25,7 +26,7 @@ module Control.Monad.Ether.Implicit.State.Strict
2526 , execStateT
2627 ) where
2728
28−import Data.Proxy
29+import GHC.Prim (Proxy#, proxy#)
2930import qualified Control.Monad.Ether.State.Strict as Explicit
3031
3132-- | See 'Control.Monad.Ether.State.Strict.StateT'.
@@ -36,51 +37,51 @@ type State s = Explicit.State s s
3637
3738-- | See 'Control.Monad.Ether.State.Strict.stateT'.
3839stateT :: (s -> m (a, s)) -> StateT s m a
39−stateT = Explicit.stateT Proxy
40+stateT = Explicit.stateT proxy#
4041
4142-- | See 'Control.Monad.Ether.State.Strict.runStateT'.
4243runStateT :: StateT s m a -> s -> m (a, s)
43−runStateT = Explicit.runStateT Proxy
44+runStateT = Explicit.runStateT proxy#
4445
4546-- | See 'Control.Monad.Ether.State.Strict.runState'.
4647runState :: State s a -> s -> (a, s)
47−runState = Explicit.runState Proxy
48+runState = Explicit.runState proxy#
4849
4950-- | See 'Control.Monad.Ether.State.Strict.evalStateT'.
5051evalStateT :: Monad m => StateT s m a -> s -> m a
51−evalStateT = Explicit.evalStateT Proxy
52+evalStateT = Explicit.evalStateT proxy#
5253
5354-- | See 'Control.Monad.Ether.State.Strict.evalState'.
5455evalState :: State s a -> s -> a
55−evalState = Explicit.evalState Proxy
56+evalState = Explicit.evalState proxy#
5657
5758-- | See 'Control.Monad.Ether.State.Strict.execStateT'.
5859execStateT :: Monad m => StateT s m a -> s -> m s
59−execStateT = Explicit.execStateT Proxy
60+execStateT = Explicit.execStateT proxy#
6061
6162-- | See 'Control.Monad.Ether.State.Strict.execState'.
6263execState :: State s a -> s -> s
63−execState = Explicit.execState Proxy
64+execState = Explicit.execState proxy#
6465
6566-- | See 'Control.Monad.Ether.State.Strict.MonadState'.
6667type MonadState s = Explicit.MonadState s s
6768
6869-- | See 'Control.Monad.Ether.State.Strict.get'.
6970get :: forall s m . MonadState s m => m s
70−get = Explicit.get (Proxy :: Proxy s)
71+get = Explicit.get (proxy# :: Proxy# s)
7172
7273-- | See 'Control.Monad.Ether.State.Strict.gets'.
7374gets :: forall s m a . MonadState s m => (s -> a) -> m a
74−gets = Explicit.gets (Proxy :: Proxy s)
75+gets = Explicit.gets (proxy# :: Proxy# s)
7576
7677-- | See 'Control.Monad.Ether.State.Strict.put'.
7778put :: forall s m . MonadState s m => s -> m ()
78−put = Explicit.put (Proxy :: Proxy s)
79+put = Explicit.put (proxy# :: Proxy# s)
7980
8081-- | See 'Control.Monad.Ether.State.Strict.state'.
8182state :: forall s m a . MonadState s m => (s -> (a, s)) -> m a
82−state = Explicit.state (Proxy :: Proxy s)
83+state = Explicit.state (proxy# :: Proxy# s)
8384
8485-- | See 'Control.Monad.Ether.State.Strict.modify'.
8586modify :: forall s m . MonadState s m => (s -> s) -> m ()
86−modify = Explicit.modify (Proxy :: Proxy s)
87+modify = Explicit.modify (proxy# :: Proxy# s)
src/Control/Monad/Ether/State/Class.hs+7−5
@@ -11,6 +11,7 @@
1111{-# LANGUAGE FunctionalDependencies #-}
1212{-# LANGUAGE TypeFamilies #-}
1313{-# LANGUAGE PolyKinds #-}
14+{-# LANGUAGE MagicHash #-}
1415
1516-- | See "Control.Monad.State.Class".
1617
@@ -20,6 +21,7 @@ module Control.Monad.Ether.State.Class
2021 , gets
2122 ) where
2223
24+import GHC.Prim (Proxy#)
2325import qualified Control.Monad.Trans.Ether.State.Lazy as S.L
2426import qualified Control.Monad.Trans.Ether.State.Strict as S.S
2527import qualified Control.Monad.Trans as Lift
@@ -31,15 +33,15 @@ class Monad m => MonadState tag s m | m tag -> s where
3133 {-# MINIMAL state | get, put #-}
3234
3335 -- | Return the state from the internals of the monad.
34− get :: proxy tag -> m s
36+ get :: Proxy# tag -> m s
3537 get t = state t (\s -> (s, s))
3638
3739 -- | Replace the state inside the monad.
38− put :: proxy tag -> s -> m ()
40+ put :: Proxy# tag -> s -> m ()
3941 put t s = state t (\_ -> ((), s))
4042
4143 -- | Embed a simple state action into the monad.
42− state :: proxy tag -> (s -> (a, s)) -> m a
44+ state :: Proxy# tag -> (s -> (a, s)) -> m a
4345 state t f = do
4446 s <- get t
4547 let ~(a, s') = f s
@@ -47,11 +49,11 @@ class Monad m => MonadState tag s m | m tag -> s where
4749 return a
4850
4951-- | Modifies the state inside a state monad.
50−modify :: MonadState tag s m => proxy tag -> (s -> s) -> m ()
52+modify :: MonadState tag s m => Proxy# tag -> (s -> s) -> m ()
5153modify t f = state t (\s -> ((), f s))
5254
5355-- | Gets specific component of the state, using a projection function supplied.
54−gets :: MonadState tag s m => proxy tag -> (s -> a) -> m a
56+gets :: MonadState tag s m => Proxy# tag -> (s -> a) -> m a
5557gets t f = Util.fmap f (get t)
5658
5759instance (Monad m, s ~ s') => MonadState tag s (S.L.StateT tag s' m) where
src/Control/Monad/Trans/Ether/Dispatch.hs+4−5
@@ -67,7 +67,6 @@ import Control.Monad.Morph (MFunctor, MMonad)
6767import Control.Monad.Catch (MonadThrow, MonadCatch, MonadMask)
6868import GHC.Generics (Generic)
6969import Data.Coerce (coerce)
70−import Data.Proxy
7170import GHC.Prim (Proxy#, proxy#)
7271
7372import qualified Control.Monad.Base as MB
@@ -170,8 +169,8 @@ instance MonadReader tag r m
170169
171170instance MonadState tag s m
172171 => Class.MonadState s (DispatchTagAttachT tag m) where
173− get = let t = Proxy :: Proxy tag in Lift.lift (get t)
174− put = let t = Proxy :: Proxy tag in Lift.lift . put t
172+ get = let t = proxy# :: Proxy# tag in Lift.lift (get t)
173+ put = let t = proxy# :: Proxy# tag in Lift.lift . put t
175174
176175instance MonadExcept tag e m
177176 => Class.MonadError e (DispatchTagAttachT tag m) where
@@ -195,8 +194,8 @@ instance MonadReader tNew r m
195194
196195instance MonadState tNew s m
197196 => MonadState tOld s (DispatchTagReplaceT tOld tNew m) where
198− get _ = let t = Proxy :: Proxy tNew in Lift.lift (get t)
199− put _ = let t = Proxy :: Proxy tNew in Lift.lift . put t
197+ get _ = let t = proxy# :: Proxy# tNew in Lift.lift (get t)
198+ put _ = let t = proxy# :: Proxy# tNew in Lift.lift . put t
200199
201200instance MonadExcept tNew e m
202201 => MonadExcept tOld e (DispatchTagReplaceT tOld tNew m) where
src/Control/Monad/Trans/Ether/State/Lazy.hs+12−10
@@ -6,6 +6,7 @@
66{-# LANGUAGE DataKinds #-}
77{-# LANGUAGE PolyKinds #-}
88{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9+{-# LANGUAGE MagicHash #-}
910
1011-- | See "Control.Monad.Trans.State.Lazy".
1112
@@ -28,6 +29,7 @@ module Control.Monad.Trans.Ether.State.Lazy
2829 , put
2930 ) where
3031
32+import GHC.Prim (Proxy#)
3133import Data.Functor.Identity (Identity(..))
3234import qualified Control.Monad.Trans.State.Lazy as Trans
3335import Control.Monad.Trans.Ether.Tagged
@@ -47,48 +49,48 @@ type State tag r = StateT tag r Identity
4749type StateT tag s = TaggedTrans tag (Trans.StateT s)
4850
4951-- | Constructor for computations in the state monad transformer.
50−stateT :: proxy tag -> (s -> m (a, s)) -> StateT tag s m a
52+stateT :: Proxy# tag -> (s -> m (a, s)) -> StateT tag s m a
5153stateT _ = pack . Trans.StateT
5254
5355-- | Constructor for computations in the state monad
5456-- (the inverse of 'runState').
55−state :: Monad m => proxy tag -> (s -> (a, s)) -> StateT tag s m a
57+state :: Monad m => Proxy# tag -> (s -> (a, s)) -> StateT tag s m a
5658state _ = pack . Trans.state
5759
5860-- | Runs a 'StateT' with the given initial state
5961-- and returns both the final value and the final state.
60−runStateT :: proxy tag -> StateT tag s m a -> s -> m (a, s)
62+runStateT :: Proxy# tag -> StateT tag s m a -> s -> m (a, s)
6163runStateT _ = Trans.runStateT . unpack
6264
6365-- | Runs a 'StateT' with the given initial state
6466-- and returns the final value, discarding the final state.
65−evalStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m a
67+evalStateT :: Monad m => Proxy# tag -> StateT tag s m a -> s -> m a
6668evalStateT _ = Trans.evalStateT . unpack
6769
6870-- | Runs a 'StateT' with the given initial state
6971-- and returns the final state, discarding the final value.
70−execStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m s
72+execStateT :: Monad m => Proxy# tag -> StateT tag s m a -> s -> m s
7173execStateT _ = Trans.execStateT . unpack
7274
7375-- | Runs a 'State' with the given initial state
7476-- and returns both the final value and the final state.
75−runState :: proxy tag -> State tag s a -> s -> (a, s)
77+runState :: Proxy# tag -> State tag s a -> s -> (a, s)
7678runState _ = Trans.runState . unpack
7779
7880-- | Runs a 'State' with the given initial state
7981-- and returns the final value, discarding the final state.
80−evalState :: proxy tag -> State tag s a -> s -> a
82+evalState :: Proxy# tag -> State tag s a -> s -> a
8183evalState _ = Trans.evalState . unpack
8284
8385-- | Runs a 'State' with the given initial state
8486-- and returns the final state, discarding the final value.
85−execState :: proxy tag -> State tag s a -> s -> s
87+execState :: Proxy# tag -> State tag s a -> s -> s
8688execState _ = Trans.execState . unpack
8789
8890-- | Fetch the current value of the state within the monad.
89−get :: Monad m => proxy tag -> StateT tag s m s
91+get :: Monad m => Proxy# tag -> StateT tag s m s
9092get _ = pack Trans.get
9193
9294-- | Set the value of the state within the monad.
93−put :: Monad m => proxy tag -> s -> StateT tag s m ()
95+put :: Monad m => Proxy# tag -> s -> StateT tag s m ()
9496put _ = pack . Trans.put
src/Control/Monad/Trans/Ether/State/Strict.hs+12−10
@@ -6,6 +6,7 @@
66{-# LANGUAGE DataKinds #-}
77{-# LANGUAGE PolyKinds #-}
88{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9+{-# LANGUAGE MagicHash #-}
910
1011-- | See "Control.Monad.Trans.State.Strict".
1112
@@ -28,6 +29,7 @@ module Control.Monad.Trans.Ether.State.Strict
2829 , put
2930 ) where
3031
32+import GHC.Prim (Proxy#)
3133import Data.Functor.Identity (Identity(..))
3234import qualified Control.Monad.Trans.State.Strict as Trans
3335import Control.Monad.Trans.Ether.Tagged
@@ -47,48 +49,48 @@ type State tag r = StateT tag r Identity
4749type StateT tag s = TaggedTrans tag (Trans.StateT s)
4850
4951-- | Constructor for computations in the state monad transformer.
50−stateT :: proxy tag -> (s -> m (a, s)) -> StateT tag s m a
52+stateT :: Proxy# tag -> (s -> m (a, s)) -> StateT tag s m a
5153stateT _ = pack . Trans.StateT
5254
5355-- | Constructor for computations in the state monad
5456-- (the inverse of 'runState').
55−state :: Monad m => proxy tag -> (s -> (a, s)) -> StateT tag s m a
57+state :: Monad m => Proxy# tag -> (s -> (a, s)) -> StateT tag s m a
5658state _ = pack . Trans.state
5759
5860-- | Runs a 'StateT' with the given initial state
5961-- and returns both the final value and the final state.
60−runStateT :: proxy tag -> StateT tag s m a -> s -> m (a, s)
62+runStateT :: Proxy# tag -> StateT tag s m a -> s -> m (a, s)
6163runStateT _ = Trans.runStateT . unpack
6264
6365-- | Runs a 'StateT' with the given initial state
6466-- and returns the final value, discarding the final state.
65−evalStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m a
67+evalStateT :: Monad m => Proxy# tag -> StateT tag s m a -> s -> m a
6668evalStateT _ = Trans.evalStateT . unpack
6769
6870-- | Runs a 'StateT' with the given initial state
6971-- and returns the final state, discarding the final value.
70−execStateT :: Monad m => proxy tag -> StateT tag s m a -> s -> m s
72+execStateT :: Monad m => Proxy# tag -> StateT tag s m a -> s -> m s
7173execStateT _ = Trans.execStateT . unpack
7274
7375-- | Runs a 'State' with the given initial state
7476-- and returns both the final value and the final state.
75−runState :: proxy tag -> State tag s a -> s -> (a, s)
77+runState :: Proxy# tag -> State tag s a -> s -> (a, s)
7678runState _ = Trans.runState . unpack
7779
7880-- | Runs a 'State' with the given initial state
7981-- and returns the final value, discarding the final state.
80−evalState :: proxy tag -> State tag s a -> s -> a
82+evalState :: Proxy# tag -> State tag s a -> s -> a
8183evalState _ = Trans.evalState . unpack
8284
8385-- | Runs a 'State' with the given initial state
8486-- and returns the final state, discarding the final value.
85−execState :: proxy tag -> State tag s a -> s -> s
87+execState :: Proxy# tag -> State tag s a -> s -> s
8688execState _ = Trans.execState . unpack
8789
8890-- | Fetch the current value of the state within the monad.
89−get :: Monad m => proxy tag -> StateT tag s m s
91+get :: Monad m => Proxy# tag -> StateT tag s m s
9092get _ = pack Trans.get
9193
9294-- | Set the value of the state within the monad.
93−put :: Monad m => proxy tag -> s -> StateT tag s m ()
95+put :: Monad m => Proxy# tag -> s -> StateT tag s m ()
9496put _ = pack . Trans.put