reader package:capability

reader @tag act lifts a purely environment-dependent action act to a monadic action in an arbitrary monad m with capability HasReader. It happens to coincide with asks: reader = asks.
Defines a capability type class for a reader effect. A reader provides an environment, say an initialization context or a configuration. The environment held in the reader effect can be changed (with local) within the scope of a sub-computation. Contrary to the Capability.State, such a change is local, and does not persist when the local call ends.
For technical reasons, this method needs an extra proxy argument. You only need it if you are defining new instances of HasReader. Otherwise, you will want to use reader. See reader for more documentation.
Derive a state monad from a reader over an IORef. Example:
newtype MyState m a = MyState (ReaderT (IORef Int) m a)
deriving (Functor, Applicative, Monad)
deriving HasState "foo" Int via
ReaderIORef (MonadReader (ReaderT (IORef Int) m))
See ReaderRef for a more generic strategy.
Derive a state monad from a reader over a mutable reference. Mutable references are available in a PrimMonad. The corresponding PrimState has to match the MCState of the reference. This constraint makes a stand-alone deriving clause necessary. Example:
newtype MyState m a = MyState (ReaderT (IORef Int) m a)
deriving (Functor, Applicative, Monad)
deriving via ReaderRef (MonadReader (ReaderT (IORef Int) m))
instance (PrimMonad m, PrimState m ~ PrimState IO)
=> HasState "foo" Int (MyState m)
See ReaderIORef for a specialized version over IORef.
Reader capability An instance should fulfill the following laws. At this point these laws are not definitive, see https://github.com/haskell/mtl/issues/5.
k <*> ask @t = ask @t <**> k
ask @t *> m = m = m <* ask @t
local @t f (ask @t) = fmap f (ask @t)
local @t f . local @t g = local @t (g . f)
local @t f (pure x) = pure x
local @t f (m >>= \x -> k x) = local @t f m >>= \x -> local @t f (k x)
reader @t f = f <$> ask @t
Type synonym using the TypeOf type family to specify HasReader constraints without having to specify the type associated to a tag.
Derive HasSource from m's MonadReader instance.