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 :: *) (m :: * -> *) | 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) :: *
- newtype MonadReader (m :: * -> *) (a :: *) = MonadReader (m a)
- newtype ReadStatePure (m :: * -> *) (a :: *) = ReadStatePure (m a)
- newtype ReadState (m :: * -> *) (a :: *) = ReadState (m a)
- newtype MonadState (m :: * -> *) (a :: *) = MonadState (m a)
- newtype ReaderIORef m a = ReaderIORef (m a)
- newtype ReaderRef m (a :: *) = ReaderRef (m a)
- module Capability.Accessors
Relational capability
class Monad m => HasSource (tag :: k) (a :: *) (m :: * -> *) | tag m -> a where Source #
Sourcing capability.
An instance does not need to fulfill any additional laws besides the monad laws.
Instances
(MutableRef ref, RefElement ref ~ s, HasSource tag ref m, PrimMonad m, PrimState m ~ MCState ref) => HasSource (tag :: k) s (ReaderRef m) Source # | |
MonadState s m => HasSource (tag :: k) s (MonadState m) Source # | |
Defined in Capability.Source.Internal.Strategies await_ :: Proxy# tag -> MonadState m s Source # | |
(HasSource tag r m, MonadTrans t, Monad (t m)) => HasSource (tag :: k) r (Lift (t m)) Source # | |
MonadReader r m => HasSource (tag :: k) r (MonadReader m) Source # | |
Defined in Capability.Source.Internal.Strategies await_ :: Proxy# tag -> MonadReader m r Source # | |
HasState tag r m => HasSource (tag :: k) r (ReadStatePure m) Source # | |
Defined in Capability.Source.Internal.Strategies await_ :: Proxy# tag -> ReadStatePure m r Source # | |
(HasState tag r m, MonadMask m) => HasSource (tag :: k) r (ReadState m) Source # | |
(HasSource tag (IORef s) m, MonadIO m) => HasSource (tag :: k) s (ReaderIORef m) Source # | |
Defined in Capability.Source.Internal.Strategies await_ :: Proxy# tag -> ReaderIORef m s Source # | |
(Coercible from to, HasSource tag from m, forall x y. Coercible x y => Coercible (m x) (m y)) => HasSource (tag :: k) to (Coerce to m) Source # | |
HasSource oldtag r m => HasSource (newtag :: k1) r (Rename oldtag m) Source # | Rename the tag. |
(forall x. Coercible (m x) (t2 (t1 m) x), Monad m, HasSource tag r (t2 (t1 m))) => HasSource (tag :: k) r ((t2 :.: t1) m) Source # | |
(tag ~ pos, HasPosition' pos struct v, HasSource oldtag struct m) => HasSource (tag :: Nat) v (Pos pos oldtag m) Source # | |
(tag ~ field, HasField' field record v, HasSource oldtag record m) => HasSource (tag :: Symbol) v (Field field oldtag m) Source # | |
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) :: * 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 *
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 * Foo = Int type instance TypeOf * Bar = String -- Same as: foo :: HasReader Foo Int M => … foo :: HasReader' Foo m => … foo = …
Strategies
newtype MonadReader (m :: * -> *) (a :: *) Source #
Derive HasSource
from m
's MonadReader
instance.
MonadReader (m a) |
Instances
newtype ReadStatePure (m :: * -> *) (a :: *) 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 :: * -> *) (a :: *) 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 :: * -> *) (a :: *) 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 :: *) 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