@@ -85,8 +85,8 @@ import Data.Kind
85import Data.Proxy
85import Data.Proxy
86import Data.Reflection
86import Data.Reflection
89import Ether.Internal
88import Ether.Internal
89+import Ether.TaggedTrans
91class Monad m => MonadState tag s m | m tag -> s where
91class Monad m => MonadState tag s m | m tag -> s where
@@ -114,55 +114,44 @@ instance {-# OVERLAPPABLE #-}
114 , MonadState tag s m
114 , MonadState tag s m
115 ) => MonadState tag s (t m)
115 ) => MonadState tag s (t m)
118 get = Lift.lift (get @tag)
117 get = Lift.lift (get @tag)
121 put = Lift.lift . put @tag
118 put = Lift.lift . put @tag
124 state = Lift.lift . state @tag
119 state = Lift.lift . state @tag
125− {-# INLINE state #-}
127instance {-# OVERLAPPABLE #-}
121instance {-# OVERLAPPABLE #-}
128 ( Monad (trans m)
122 ( Monad (trans m)
129− , MonadState tag s (Handler effs trans m)
123+ , MonadState tag s (TaggedTrans effs trans m)
130− ) => MonadState tag s (Handler (eff ': effs) trans (m :: Type -> Type))
124+ ) => MonadState tag s (TaggedTrans (eff ': effs) trans (m :: Type -> Type))
134 (coerce ::
128 (coerce ::
135− Handler effs trans m s ->
129+ TaggedTrans effs trans m s ->
136− Handler (eff ': effs) trans m s)
130+ TaggedTrans (eff ': effs) trans m s)
137 (get @tag)
131 (get @tag)
141 (coerce ::
134 (coerce ::
142− (s -> Handler effs trans m ()) ->
135+ (s -> TaggedTrans effs trans m ()) ->
143− (s -> Handler (eff ': effs) trans m ()))
136+ (s -> TaggedTrans (eff ': effs) trans m ()))
144 (put @tag)
137 (put @tag)
148 (coerce :: forall a .
140 (coerce :: forall a .
149− ((s -> (a, s)) -> Handler effs trans m a) ->
141+ ((s -> (a, s)) -> TaggedTrans effs trans m a) ->
150− ((s -> (a, s)) -> Handler (eff ': effs) trans m a))
142+ ((s -> (a, s)) -> TaggedTrans (eff ': effs) trans m a))
151 (state @tag)
143 (state @tag)
152− {-# INLINE state #-}
154−-- | Encode type-level information for 'StateT'.
157-- | Modifies the state inside a state monad.
145-- | Modifies the state inside a state monad.
158modify :: forall tag s m . MonadState tag s m => (s -> s) -> m ()
146modify :: forall tag s m . MonadState tag s m => (s -> s) -> m ()
159modify f = state @tag (\s -> ((), f s))
147modify f = state @tag (\s -> ((), f s))
160−{-# INLINABLE modify #-}
162-- | Gets specific component of the state, using a projection function supplied.
149-- | Gets specific component of the state, using a projection function supplied.
163gets :: forall tag s m a . MonadState tag s m => (s -> a) -> m a
150gets :: forall tag s m a . MonadState tag s m => (s -> a) -> m a
164gets f = fmap f (get @tag)
151gets f = fmap f (get @tag)
165−{-# INLINABLE gets #-}
152+
153+-- | Encode type-level information for 'StateT'.
167type instance HandleSuper STATE s trans = ()
156type instance HandleSuper STATE s trans = ()
168type instance HandleConstraint STATE s trans m =
157type instance HandleConstraint STATE s trans m =
@@ -170,64 +159,56 @@ type instance HandleConstraint STATE s trans m =
171instance Handle STATE s (T.Strict.StateT s) where
160instance Handle STATE s (T.Strict.StateT s) where
172 handling r = r
161 handling r = r
173− {-# INLINE handling #-}
175instance Handle STATE s (T.Lazy.StateT s) where
163instance Handle STATE s (T.Lazy.StateT s) where
176 handling r = r
164 handling r = r
177− {-# INLINE handling #-}
180 ( Handle STATE s trans
167 ( Handle STATE s trans
181 , Monad m, Monad (trans m)
168 , Monad m, Monad (trans m)
182− ) => MonadState tag s (Handler (TAGGED STATE tag) trans m)
169+ ) => MonadState tag s (TaggedTrans (TAGGED STATE tag) trans m)
186 handling @STATE @s @trans @m $
173 handling @STATE @s @trans @m $
187 coerce (T.get @s @(trans m))
174 coerce (T.get @s @(trans m))
191 handling @STATE @s @trans @m $
177 handling @STATE @s @trans @m $
192 coerce (T.put @s @(trans m))
178 coerce (T.put @s @(trans m))
196 handling @STATE @s @trans @m $
181 handling @STATE @s @trans @m $
197 coerce (T.state @s @(trans m) @a) ::
182 coerce (T.state @s @(trans m) @a) ::
198− forall eff a . (s -> (a, s)) -> Handler eff trans m a
183+ forall eff a . (s -> (a, s)) -> TaggedTrans eff trans m a
199− {-# INLINE state #-}
202 ( HasLens tag payload s
186 ( HasLens tag payload s
203 , Handle STATE payload trans
187 , Handle STATE payload trans
204 , Monad m, Monad (trans m)
188 , Monad m, Monad (trans m)
205− ) => MonadState tag s (Handler (TAGGED STATE tag ': effs) trans m)
189+ ) => MonadState tag s (TaggedTrans (TAGGED STATE tag ': effs) trans m)
209 handling @STATE @payload @trans @m $
193 handling @STATE @payload @trans @m $
210 (coerce :: forall eff a .
194 (coerce :: forall eff a .
211 trans m a ->
195 trans m a ->
212− Handler eff trans m a)
196+ TaggedTrans eff trans m a)
213 (T.gets (view (lensOf @tag @payload @s)))
197 (T.gets (view (lensOf @tag @payload @s)))
217 handling @STATE @payload @trans @m $
200 handling @STATE @payload @trans @m $
218 (coerce :: forall eff a .
201 (coerce :: forall eff a .
219 trans m a ->
202 trans m a ->
220− Handler eff trans m a)
203+ TaggedTrans eff trans m a)
221 (T.modify (over (lensOf @tag @payload @s) (const s)))
204 (T.modify (over (lensOf @tag @payload @s) (const s)))
224 state f =
206 state f =
225 handling @STATE @payload @trans @m $
207 handling @STATE @payload @trans @m $
226 (coerce :: forall eff a .
208 (coerce :: forall eff a .
227 trans m a ->
209 trans m a ->
228− Handler eff trans m a)
210+ TaggedTrans eff trans m a)
229 (T.state (lensOf @tag @payload @s f))
211 (T.state (lensOf @tag @payload @s f))
230− {-# INLINE state #-}
232-- | The parametrizable state monad.
213-- | The parametrizable state monad.
@@ -241,48 +222,41 @@ type State tag r = StateT tag r Identity
242-- The 'return' function leaves the state unchanged, while '>>=' uses
223-- The 'return' function leaves the state unchanged, while '>>=' uses
243-- the final state of the first computation as the initial state of the second.
224-- the final state of the first computation as the initial state of the second.
244−type StateT tag s = Handler (TAGGED STATE tag) (T.Strict.StateT s)
225+type StateT tag s = TaggedTrans (TAGGED STATE tag) (T.Strict.StateT s)
246-- | Constructor for computations in the state monad transformer.
227-- | Constructor for computations in the state monad transformer.
247stateT :: forall tag s m a . (s -> m (a, s)) -> StateT tag s m a
228stateT :: forall tag s m a . (s -> m (a, s)) -> StateT tag s m a
248stateT = coerce (T.Strict.StateT @s @m @a)
229stateT = coerce (T.Strict.StateT @s @m @a)
249−{-# INLINE stateT #-}
251-- | Runs a 'StateT' with the given initial state
231-- | Runs a 'StateT' with the given initial state
252-- and returns both the final value and the final state.
232-- and returns both the final value and the final state.
253runStateT :: forall tag s m a . StateT tag s m a -> s -> m (a, s)
233runStateT :: forall tag s m a . StateT tag s m a -> s -> m (a, s)
254runStateT = coerce (T.Strict.runStateT @s @m @a)
234runStateT = coerce (T.Strict.runStateT @s @m @a)
255−{-# INLINE runStateT #-}
257-- | Runs a 'StateT' with the given initial state
236-- | Runs a 'StateT' with the given initial state
258-- and returns the final value, discarding the final state.
237-- and returns the final value, discarding the final state.
259evalStateT :: forall tag s m a . Monad m => StateT tag s m a -> s -> m a
238evalStateT :: forall tag s m a . Monad m => StateT tag s m a -> s -> m a
260evalStateT = coerce (T.Strict.evalStateT @m @s @a)
239evalStateT = coerce (T.Strict.evalStateT @m @s @a)
261−{-# INLINE evalStateT #-}
263-- | Runs a 'StateT' with the given initial state
241-- | Runs a 'StateT' with the given initial state
264-- and returns the final state, discarding the final value.
242-- and returns the final state, discarding the final value.
265execStateT :: forall tag s m a . Monad m => StateT tag s m a -> s -> m s
243execStateT :: forall tag s m a . Monad m => StateT tag s m a -> s -> m s
266execStateT = coerce (T.Strict.execStateT @m @s @a)
244execStateT = coerce (T.Strict.execStateT @m @s @a)
267−{-# INLINE execStateT #-}
269-- | Runs a 'State' with the given initial state
246-- | Runs a 'State' with the given initial state
270-- and returns both the final value and the final state.
247-- and returns both the final value and the final state.
271runState :: forall tag s a . State tag s a -> s -> (a, s)
248runState :: forall tag s a . State tag s a -> s -> (a, s)
272runState = coerce (T.Strict.runState @s @a)
249runState = coerce (T.Strict.runState @s @a)
273−{-# INLINE runState #-}
275-- | Runs a 'State' with the given initial state
251-- | Runs a 'State' with the given initial state
276-- and returns the final value, discarding the final state.
252-- and returns the final value, discarding the final state.
277evalState :: forall tag s a . State tag s a -> s -> a
253evalState :: forall tag s a . State tag s a -> s -> a
278evalState = coerce (T.Strict.evalState @s @a)
254evalState = coerce (T.Strict.evalState @s @a)
279−{-# INLINE evalState #-}
281-- | Runs a 'State' with the given initial state
256-- | Runs a 'State' with the given initial state
282-- and returns the final state, discarding the final value.
257-- and returns the final state, discarding the final value.
283execState :: forall tag s a . State tag s a -> s -> s
258execState :: forall tag s a . State tag s a -> s -> s
284execState = coerce (T.Strict.execState @s @a)
259execState = coerce (T.Strict.execState @s @a)
285−{-# INLINE execState #-}
287-- | The parametrizable state monad.
261-- | The parametrizable state monad.
@@ -296,159 +270,127 @@ type LazyState tag r = LazyStateT tag r Identity
297-- The 'return' function leaves the state unchanged, while '>>=' uses
271-- The 'return' function leaves the state unchanged, while '>>=' uses
298-- the final state of the first computation as the initial state of the second.
272-- the final state of the first computation as the initial state of the second.
299−type LazyStateT tag s = Handler (TAGGED STATE tag) (T.Lazy.StateT s)
273+type LazyStateT tag s = TaggedTrans (TAGGED STATE tag) (T.Lazy.StateT s)
301-- | Constructor for computations in the state monad transformer.
275-- | Constructor for computations in the state monad transformer.
302lazyStateT :: forall tag s m a . (s -> m (a, s)) -> LazyStateT tag s m a
276lazyStateT :: forall tag s m a . (s -> m (a, s)) -> LazyStateT tag s m a
303lazyStateT = coerce (T.Lazy.StateT @s @m @a)
277lazyStateT = coerce (T.Lazy.StateT @s @m @a)
304−{-# INLINE lazyStateT #-}
306-- | Runs a 'StateT' with the given initial state
279-- | Runs a 'StateT' with the given initial state
307-- and returns both the final value and the final state.
280-- and returns both the final value and the final state.
308runLazyStateT :: forall tag s m a . LazyStateT tag s m a -> s -> m (a, s)
281runLazyStateT :: forall tag s m a . LazyStateT tag s m a -> s -> m (a, s)
309runLazyStateT = coerce (T.Lazy.runStateT @s @m @a)
282runLazyStateT = coerce (T.Lazy.runStateT @s @m @a)
310−{-# INLINE runLazyStateT #-}
312-- | Runs a 'StateT' with the given initial state
284-- | Runs a 'StateT' with the given initial state
313-- and returns the final value, discarding the final state.
285-- and returns the final value, discarding the final state.
314evalLazyStateT :: forall tag s m a . Monad m => LazyStateT tag s m a -> s -> m a
286evalLazyStateT :: forall tag s m a . Monad m => LazyStateT tag s m a -> s -> m a
315evalLazyStateT = coerce (T.Lazy.evalStateT @m @s @a)
287evalLazyStateT = coerce (T.Lazy.evalStateT @m @s @a)
316−{-# INLINE evalLazyStateT #-}
318-- | Runs a 'StateT' with the given initial state
289-- | Runs a 'StateT' with the given initial state
319-- and returns the final state, discarding the final value.
290-- and returns the final state, discarding the final value.
320execLazyStateT :: forall tag s m a . Monad m => LazyStateT tag s m a -> s -> m s
291execLazyStateT :: forall tag s m a . Monad m => LazyStateT tag s m a -> s -> m s
321execLazyStateT = coerce (T.Lazy.execStateT @m @s @a)
292execLazyStateT = coerce (T.Lazy.execStateT @m @s @a)
322−{-# INLINE execLazyStateT #-}
324-- | Runs a 'State' with the given initial state
294-- | Runs a 'State' with the given initial state
325-- and returns both the final value and the final state.
295-- and returns both the final value and the final state.
326runLazyState :: forall tag s a . LazyState tag s a -> s -> (a, s)
296runLazyState :: forall tag s a . LazyState tag s a -> s -> (a, s)
327runLazyState = coerce (T.Lazy.runState @s @a)
297runLazyState = coerce (T.Lazy.runState @s @a)
328−{-# INLINE runLazyState #-}
330-- | Runs a 'State' with the given initial state
299-- | Runs a 'State' with the given initial state
331-- and returns the final value, discarding the final state.
300-- and returns the final value, discarding the final state.
332evalLazyState :: forall tag s a . LazyState tag s a -> s -> a
301evalLazyState :: forall tag s a . LazyState tag s a -> s -> a
333evalLazyState = coerce (T.Lazy.evalState @s @a)
302evalLazyState = coerce (T.Lazy.evalState @s @a)
334−{-# INLINE evalLazyState #-}
336-- | Runs a 'State' with the given initial state
304-- | Runs a 'State' with the given initial state
337-- and returns the final state, discarding the final value.
305-- and returns the final state, discarding the final value.
338execLazyState :: forall tag s a . LazyState tag s a -> s -> s
306execLazyState :: forall tag s a . LazyState tag s a -> s -> s
339execLazyState = coerce (T.Lazy.execState @s @a)
307execLazyState = coerce (T.Lazy.execState @s @a)
340−{-# INLINE execLazyState #-}
342type family STATES (ts :: HList xs) :: [Type] where
309type family STATES (ts :: HList xs) :: [Type] where
343 STATES 'HNil = '[]
310 STATES 'HNil = '[]
344 STATES ('HCons t ts) = TAGGED STATE t ': STATES ts
311 STATES ('HCons t ts) = TAGGED STATE t ': STATES ts
346−type StatesT s = Handler (STATES (Tags s)) (T.Strict.StateT s)
313+type StatesT s = TaggedTrans (STATES (Tags s)) (T.Strict.StateT s)
348type States s = StatesT s Identity
315type States s = StatesT s Identity
350runStatesT :: forall p m a . StatesT p m a -> p -> m (a, p)
317runStatesT :: forall p m a . StatesT p m a -> p -> m (a, p)
351runStatesT = coerce (T.Strict.runStateT @p @m @a)
318runStatesT = coerce (T.Strict.runStateT @p @m @a)
352−{-# INLINE runStatesT #-}
354runStates :: forall p a . States p a -> p -> (a, p)
320runStates :: forall p a . States p a -> p -> (a, p)
355runStates = coerce (T.Strict.runState @p @a)
321runStates = coerce (T.Strict.runState @p @a)
356−{-# INLINE runStates #-}
358type StateT' s = StateT s s
323type StateT' s = StateT s s
360stateT' :: (s -> m (a, s)) -> StateT' s m a
325stateT' :: (s -> m (a, s)) -> StateT' s m a
361stateT' = stateT
326stateT' = stateT
362−{-# INLINE stateT' #-}
364runStateT' :: StateT' s m a -> s -> m (a, s)
328runStateT' :: StateT' s m a -> s -> m (a, s)
365runStateT' = runStateT
329runStateT' = runStateT
366−{-# INLINE runStateT' #-}
368runState' :: State' s a -> s -> (a, s)
331runState' :: State' s a -> s -> (a, s)
369runState' = runState
332runState' = runState
370−{-# INLINE runState' #-}
372evalStateT' :: Monad m => StateT' s m a -> s -> m a
334evalStateT' :: Monad m => StateT' s m a -> s -> m a
373evalStateT' = evalStateT
335evalStateT' = evalStateT
374−{-# INLINE evalStateT' #-}
376type State' s = State s s
337type State' s = State s s
378evalState' :: State' s a -> s -> a
339evalState' :: State' s a -> s -> a
379evalState' = evalState
340evalState' = evalState
380−{-# INLINE evalState' #-}
382execStateT' :: Monad m => StateT' s m a -> s -> m s
342execStateT' :: Monad m => StateT' s m a -> s -> m s
383execStateT' = execStateT
343execStateT' = execStateT
384−{-# INLINE execStateT' #-}
386execState' :: State' s a -> s -> s
345execState' :: State' s a -> s -> s
387execState' = execState
346execState' = execState
388−{-# INLINE execState' #-}
390type LazyStateT' s = LazyStateT s s
348type LazyStateT' s = LazyStateT s s
392lazyStateT' :: (s -> m (a, s)) -> LazyStateT' s m a
350lazyStateT' :: (s -> m (a, s)) -> LazyStateT' s m a
393lazyStateT' = lazyStateT
351lazyStateT' = lazyStateT
394−{-# INLINE lazyStateT' #-}
396runLazyStateT' :: LazyStateT' s m a -> s -> m (a, s)
353runLazyStateT' :: LazyStateT' s m a -> s -> m (a, s)
397runLazyStateT' = runLazyStateT
354runLazyStateT' = runLazyStateT
398−{-# INLINE runLazyStateT' #-}
400runLazyState' :: LazyState' s a -> s -> (a, s)
356runLazyState' :: LazyState' s a -> s -> (a, s)
401runLazyState' = runLazyState
357runLazyState' = runLazyState
402−{-# INLINE runLazyState' #-}
404evalLazyStateT' :: Monad m => LazyStateT' s m a -> s -> m a
359evalLazyStateT' :: Monad m => LazyStateT' s m a -> s -> m a
405evalLazyStateT' = evalLazyStateT
360evalLazyStateT' = evalLazyStateT
406−{-# INLINE evalLazyStateT' #-}
408type LazyState' s = LazyState s s
362type LazyState' s = LazyState s s
410evalLazyState' :: LazyState' s a -> s -> a
364evalLazyState' :: LazyState' s a -> s -> a
411evalLazyState' = evalLazyState
365evalLazyState' = evalLazyState
412−{-# INLINE evalLazyState' #-}
414execLazyStateT' :: Monad m => LazyStateT' s m a -> s -> m s
367execLazyStateT' :: Monad m => LazyStateT' s m a -> s -> m s
415execLazyStateT' = execLazyStateT
368execLazyStateT' = execLazyStateT
416−{-# INLINE execLazyStateT' #-}
418execLazyState' :: LazyState' s a -> s -> s
370execLazyState' :: LazyState' s a -> s -> s
419execLazyState' = execLazyState
371execLazyState' = execLazyState
420−{-# INLINE execLazyState' #-}
422type MonadState' s = MonadState s s
373type MonadState' s = MonadState s s
424get' :: forall s m . MonadState' s m => m s
375get' :: forall s m . MonadState' s m => m s
425get' = get @s
376get' = get @s
428gets' :: forall s m a . MonadState' s m => (s -> a) -> m a
378gets' :: forall s m a . MonadState' s m => (s -> a) -> m a
429gets' = gets @s
379gets' = gets @s
432put' :: forall s m . MonadState' s m => s -> m ()
381put' :: forall s m . MonadState' s m => s -> m ()
433put' = put @s
382put' = put @s
436state' :: forall s m a . MonadState' s m => (s -> (a, s)) -> m a
384state' :: forall s m a . MonadState' s m => (s -> (a, s)) -> m a
437state' = state @s
385state' = state @s
438−{-# INLINE state' #-}
440modify' :: forall s m . MonadState' s m => (s -> s) -> m ()
387modify' :: forall s m . MonadState' s m => (s -> s) -> m ()
441modify' = modify @s
388modify' = modify @s
442−{-# INLINE modify' #-}
444-- | Encode type-level information for 'zoom'.
390-- | Encode type-level information for 'zoom'.
445data ZOOM t z
391data ZOOM t z
447−type ZoomT t (z :: Type) = Handler (ZOOM t z) IdentityT
393+type ZoomT t (z :: Type) = TaggedTrans (ZOOM t z) IdentityT
449−newtype ReifiedLens s t a b = Lens (Lens s t a b)
451−type ReifiedLens' s a = ReifiedLens s s a a
453-- | Zoom into a part of a state using a lens.
395-- | Zoom into a part of a state using a lens.
@@ -457,19 +399,17 @@ zoom
457 -> (forall z . Reifies z (ReifiedLens' sOuter sInner) => ZoomT tag z m a)
399 -> (forall z . Reifies z (ReifiedLens' sOuter sInner) => ZoomT tag z m a)
459zoom l m = reify (Lens l) (\(_ :: Proxy z) -> coerce (m @z))
401zoom l m = reify (Lens l) (\(_ :: Proxy z) -> coerce (m @z))
463 ( MonadState tag sOuter m
404 ( MonadState tag sOuter m
464 , Reifies z (ReifiedLens' sOuter sInner)
405 , Reifies z (ReifiedLens' sOuter sInner)
465 , trans ~ IdentityT
406 , trans ~ IdentityT
466− ) => MonadState tag sInner (Handler (ZOOM tag z) trans m)
407+ ) => MonadState tag sInner (TaggedTrans (ZOOM tag z) trans m)
469 (coerce :: forall eff r a .
410 (coerce :: forall eff r a .
470− (r -> m a) ->
411+ (r -> m a) ->
471− (r -> Handler eff trans m a))
412+ (r -> TaggedTrans eff trans m a))
472 (state @tag . l)
413 (state @tag . l)
474 Lens l = reflect (Proxy :: Proxy z)
415 Lens l = reflect (Proxy :: Proxy z)
475− {-# INLINE state #-}