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