Safe Haskell | None |
---|---|
Language | Haskell2010 |
Defines a capability for computations that consume a stream of values as part of their execution.
Programs comsuming streams of data are common. Examples: rolling up input events. Sources are similar to Python generators.
This can be thought of as a reader capability where there's no guarantee that one reads the same value each time.
The HasSource
capability enables separating the logic responsible for
emitting events from that responsible for collecting or handling them.
The name is because a source is needed to produce the locally consumed stream.
Synopsis
- class Monad m => HasSource (tag :: k) (a :: Type) (m :: Type -> Type) | tag m -> a where
- await :: forall tag a m. HasSource tag a m => m a
- awaits :: forall tag r m a. HasSource tag r m => (r -> a) -> m a
- type HasSource' (tag :: k) = HasSource tag (TypeOf k tag)
- type family TypeOf k (s :: k) :: Type
- newtype MonadReader (m :: Type -> Type) (a :: Type) = MonadReader (m a)
- newtype ReadStatePure (m :: Type -> Type) (a :: Type) = ReadStatePure (m a)
- newtype ReadState (m :: Type -> Type) (a :: Type) = ReadState (m a)
- newtype MonadState (m :: Type -> Type) (a :: Type) = MonadState (m a)
- newtype ReaderIORef m a = ReaderIORef (m a)
- newtype ReaderRef m (a :: Type) = ReaderRef (m a)
- module Capability.Accessors
Relational capability
class Monad m => HasSource (tag :: k) (a :: Type) (m :: Type -> Type) | tag m -> a where Source #
Sourcing capability.
An instance does not need to fulfill any additional laws besides the monad laws.
Instances
await :: forall tag a m. HasSource tag a m => m a Source #
await @tag a
takes a
from the source capability tag
.
awaits :: forall tag r m a. HasSource tag r m => (r -> a) -> m a Source #
awaits @tag
retrieves the image by f
of the environment
of the reader capability tag
.
awaits @tag f = f <$> await @tag
Functional capability
type HasSource' (tag :: k) = HasSource tag (TypeOf k tag) Source #
type family TypeOf k (s :: k) :: Type Source #
Type family associating a tag to the corresponding type. It is intended to simplify constraint declarations, by removing the need to redundantly specify the type associated to a tag.
It is poly-kinded, which allows users to define their own kind of tags.
Standard haskell types can also be used as tags by specifying the Type
kind
when defining the type family instance.
Defining TypeOf
instances for Symbol
s (typelevel string
literals) is discouraged. Since symbols all belong to the same global
namespace, such instances could conflict with others defined in external
libraries. More generally, as for typeclasses, TypeOf
instances should
always be defined in the same module as the tag type to prevent issues due to
orphan instances.
Example:
import Capability.Reader data Foo data Bar type instance TypeOf Type Foo = Int type instance TypeOf Type Bar = String -- Same as: foo :: HasReader Foo Int M => … foo :: HasReader' Foo m => … foo = …
Strategies
newtype MonadReader (m :: Type -> Type) (a :: Type) Source #
Derive HasSource
from m
's MonadReader
instance.
MonadReader (m a) |
Instances
newtype ReadStatePure (m :: Type -> Type) (a :: Type) Source #
Convert a pure state monad into a reader monad.
Pure meaning that the monad stack does not allow catching exceptions.
Otherwise, an exception occurring in the action passed to local
could cause
the context to remain modified outside of the call to local
. E.g.
local @tag (const r') (throw MyException) `catch` \MyException -> ask @tag
returns r'
instead of the previous value.
Note, that no MonadIO
instance is provided, as this would allow catching
exceptions.
See ReadState
.
ReadStatePure (m a) |
Instances
newtype ReadState (m :: Type -> Type) (a :: Type) Source #
Convert a state monad into a reader monad.
Use this if the monad stack allows catching exceptions.
See ReadStatePure
.
ReadState (m a) |
Instances
(HasState tag r m, MonadMask m) => HasSource (tag :: k) r (ReadState m) Source # | |
(HasState tag r m, MonadMask m) => HasReader (tag :: k) r (ReadState m) Source # | |
Monad m => Monad (ReadState m) Source # | |
Functor m => Functor (ReadState m) Source # | |
Applicative m => Applicative (ReadState m) Source # | |
Defined in Capability.Source.Internal.Strategies | |
MonadIO m => MonadIO (ReadState m) Source # | |
Defined in Capability.Source.Internal.Strategies | |
PrimMonad m => PrimMonad (ReadState m) Source # | |
type PrimState (ReadState m) Source # | |
Defined in Capability.Source.Internal.Strategies |
newtype MonadState (m :: Type -> Type) (a :: Type) Source #
Derive HasState
from m
's
MonadState
instance.
MonadState (m a) |
Instances
newtype ReaderIORef m a Source #
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.
ReaderIORef (m a) |
Instances
newtype ReaderRef m (a :: Type) Source #
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
.
ReaderRef (m a) |
Instances
(MutableRef ref, RefElement ref ~ s, HasSource tag ref m, PrimMonad m, PrimState m ~ MCState ref) => HasSink (tag :: k) s (ReaderRef m) Source # | |
(MutableRef ref, RefElement ref ~ s, HasSource tag ref m, PrimMonad m, PrimState m ~ MCState ref) => HasSource (tag :: k) s (ReaderRef m) Source # | |
(MutableRef ref, RefElement ref ~ s, HasReader tag ref m, PrimMonad m, PrimState m ~ MCState ref) => HasState (tag :: k) s (ReaderRef m) Source # | |
Monad m => Monad (ReaderRef m) Source # | |
Functor m => Functor (ReaderRef m) Source # | |
Applicative m => Applicative (ReaderRef m) Source # | |
Defined in Capability.State.Internal.Strategies.Common | |
MonadIO m => MonadIO (ReaderRef m) Source # | |
Defined in Capability.State.Internal.Strategies.Common | |
PrimMonad m => PrimMonad (ReaderRef m) Source # | |
type PrimState (ReaderRef m) Source # | |
Defined in Capability.State.Internal.Strategies.Common |
Modifiers
module Capability.Accessors