HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulecapability-0.5.0.1Haskell2010

Capability.State.Internal.Class

  • 1 class
  • 12 values
classclass (Monad m, HasSource tag s m, HasSink tag s m) => HasState (tag :: k) s (m :: Type -> Type) | tag m -> s where
#

State capability

An instance should fulfill the following laws. At this point these laws are not definitive, see https://github.com/haskell/mtl/issues/5.

Property
get @t >>= \s1 -> get @t >>= \s2 -> pure (s1, s2) = get @t >>= \s -> pure (s, s)
Property
get @t >>= \_ -> put @t s = put @t s
Property
put @t s1 >> put @t s2 = put @t s2
Property
put @t s >> get @t = put @t s >> pure s
Property
state @t f = get @t >>= \s -> let (a, s') = f s in put @t s' >> pure a

Methods

  • state_ :: Proxy# tag -> (s -> (a, s)) -> m a

    For technical reasons, this method needs an extra proxy argument. You only need it if you are defining new instances of 'HasState. Otherwise, you will want to use state. See state for more documentation.

Instances13HasSource, HasSink, HasState, Reified, …
  • MonadState s m => HasState tag s (MonadState m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan
  • (HasState tag s m, MonadTrans t, Monad (t m)) => HasState tag s (Lift (t m))Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan

    Lift one layer in a monad transformer stack.

  • (MutableRef ref, RefElement ref ~ s, HasReader tag ref m, PrimMonad m, PrimState m ~ MCState ref) => HasState tag s (ReaderRef m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan
  • (HasReader tag (IORef s) m, MonadIO m) => HasState tag s (ReaderIORef m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan
  • (Coercible from to, HasState tag from m, forall x y. Coercible x y => Coercible (m x) (m y)) => HasState tag to (Coerce to m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan

    Convert the state using safe coercion.

  • (Monad m, Reifies s' (Reified tag (HasState tag s) m)) => HasSource tag s (Reflected s' (HasState tag s) m)Defined in capability-0.5.0.1 · Capability.State.Internal.Class
  • HasState oldtag s m => HasState newtag s (Rename oldtag m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan

    Rename the tag.

  • (forall x. Coercible (m x) (t2 (t1 m) x), Monad m, HasState tag s (t2 (t1 m))) => HasState tag s ((:.:) t2 t1 m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan

    Compose two accessors.

  • (Monad m, Reifies s' (Reified tag (HasState tag s) m)) => HasState tag s (Reflected s' (HasState tag s) m)Defined in capability-0.5.0.1 · Capability.State.Internal.Class
  • (Monad m, Reifies s' (Reified tag (HasState tag s) m)) => HasSink tag s (Reflected s' (HasState tag s) m)Defined in capability-0.5.0.1 · Capability.State.Internal.Class
  • (tag ~ field, HasField' field record v, HasState oldtag record m) => HasState tag v (Field field oldtag m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan

    Zoom in on the record field field of type v in the state record.

  • (tag ~ pos, HasPosition' pos struct v, HasState oldtag struct m) => HasState tag v (Pos pos oldtag m)Defined in capability-0.5.0.1 · Capability.State.Internal.Strategies · orphan

    Zoom in on the field at position pos of type v in the state struct.

  • data Reified tag (HasState tag s)Defined in capability-0.5.0.1 · Capability.State.Internal.Class
valueget :: HasState tag s m => m s
#

get @tag retrieve the current state of the state capability tag.

valueput :: HasState tag s m => s -> m ()
#

put @tag s replace the current state of the state capability tag with s.

valuestate :: HasState tag s m => (s -> (a, s)) -> m a
#

state @tag f lifts a pure state computation f to a monadic action in an arbitrary monad m with capability HasState.

Given the current state s of the state capability tag and (a, s') = f s, update the state to s' and return a.

valuemodify :: HasState tag s m => (s -> s) -> m ()
#

modify @tag f given the current state s of the state capability tag and s' = f s, updates the state of the capability tag to s'.

valuemodify' :: HasState tag s m => (s -> s) -> m ()
#

Same as modify but strict in the new state.

valuegets :: HasState tag s m => (s -> a) -> m a
#

gets @tag f retrieves the image, by f of the current state of the state capability tag.

Property
gets @tag f = f <$> get @tag
valuezoom
  1. :: (forall x. Coercible (t m x) (m x), HasState innertag inner (t m), All cs m)
  2. => forall (m' :: Type -> Type). All (HasState innertag inner ': cs) m' => m' a
  3. -> m a
#

Execute the given state action on a sub-component of the current state as defined by the given transformer t. The set of retained capabilities must be passed as @cs. If no capabilities are required, Capabilities.Constraints.None can be used.

Examples:

foo :: HasState "foo" Int m => m ()
zoom @"foo" @(Field "foo" "foobar") @None foo
  :: (HasField' "foobar" record Int, HasState "foobar" record m) => m ()

zoom @"foo" @(Field "foo" "foobar") @('[MonadIO]) bar
  :: ( HasField' "foobar" record Int, HasState "foobar" record m
     , MonadIO m) => m ()

foo :: HasState "foo" Int m => m ()
bar :: (MonadIO m, HasState "foo" Int m) => m ()

Note: the HasField' constraint comes from the generic-lens package.

This function is experimental and subject to change. See https://github.com/tweag/capability/issues/46.

data familydata family Reified (tag :: k) (c :: Capability) (m :: Type -> Type)
#
Reified tag capability m

Defines the dictionary type for the methods of capability under tag in the monad m. Refer to interpret_ for an example use-case.

For example, the Capability.Sink.HasSink capability has the method Capability.Sink.yield :: a -> m (). The corresponding dictionary type is defined as follows.

Example1 expression
:{  data instance Reified tag (HasSink tag a) m =    ReifiedSink { _yield :: forall a. a -> m () }  :}

Superclass dictionaries are represented as nested records. For example, the Capability.State.HasState capability has the superclasses Capability.Source.HasSource and Capability.Sink.HasSink and the method Capability.State.state :: (s -> (a, s)) -> m a. The corresponding dictionary type is defined as follows.

Example1 expression
:{  data instance Reified tag (HasState tag s) m =    ReifiedState      { _stateSource :: Reified tag (HasSource tag s) m,        _stateSink :: Reified tag (HasSink tag s) m,        _state :: forall a. (s -> (a, s)) -> m a      }  :}
Instances7Reified, …
value_state :: Reified tag (HasState tag s) m -> forall a. (s -> (a, s)) -> m a
#