Safe Haskell | None |
---|---|
Language | Haskell2010 |
Defines a capability for computations that produce a stream of values as part of their execution.
Programs producing streams of data are common. Examples: emitting events on input, or emitting events whenever certain conditions are observed. streams are similar to Python generators.
The HasSink
capability enables separating the logic responsible for
emitting events from that responsible for collecting or handling them.
The name is because a sink is needed to consume the locally produced stream.
This can be thought of as a writer capability of a list of values HasWriter
tag [v]
with \x -> tell @tag [x]
as a primitive operation. However, that
implementation would be inefficient.
For example using the Stream
instance, a producer defined
using this capability can be consumed efficiently in a streaming fashion.
Synopsis
- class Monad m => HasSink (tag :: k) (a :: *) (m :: * -> *) | tag m -> a where
- yield :: forall tag a m. HasSink tag a m => a -> m ()
- type HasSink' (tag :: k) = HasSink tag (TypeOf k tag)
- type family TypeOf k (s :: k) :: *
- newtype SinkStack m (a :: *) = SinkStack (m a)
- newtype SinkDList m (a :: *) = SinkDList (m a)
- newtype SinkLog m (a :: *) = SinkLog (m a)
- module Capability.Accessors
Relational capability
class Monad m => HasSink (tag :: k) (a :: *) (m :: * -> *) | tag m -> a where Source #
Sinking 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) => HasSink (tag :: k) s (ReaderRef m) Source # | |
MonadState s m => HasSink (tag :: k) s (MonadState m) Source # | |
Defined in Capability.Sink.Internal.Strategies yield_ :: Proxy# tag -> s -> MonadState m () Source # | |
(HasSink tag a m, MonadTrans t, Monad (t m)) => HasSink (tag :: k) a (Lift (t m)) Source # | Lift one layer in a monad transformer stack. Note, that if the deriving (HasSink tag w) via SinkLog (Lift (SomeTrans (MonadState SomeStateMonad))) over deriving (HasSink tag w) via Lift (SomeTrans (SinkLog (MonadState SomeStateMonad))) |
HasState tag [a] m => HasSink (tag :: k) a (SinkStack m) Source # | |
HasSink tag (DList a) m => HasSink (tag :: k) a (SinkDList m) Source # | This instance may seem a bit odd at first. All it does is wrap each
deriving (HasSink tag w) via SinkDList (SinkLog (MonadState SomeStateMonad)) |
(Monoid w, HasState tag w m) => HasSink (tag :: k) w (SinkLog m) Source # | |
(HasSource tag (IORef s) m, MonadIO m) => HasSink (tag :: k) s (ReaderIORef m) Source # | |
Defined in Capability.Sink.Internal.Strategies yield_ :: Proxy# tag -> s -> ReaderIORef m () Source # | |
(Coercible from to, HasSink tag from m) => HasSink (tag :: k) to (Coerce to m) Source # | Convert the state using safe coercion. |
Monad m => HasSink (tag :: k) a (Stream (Of a) m) Source # | |
HasSink oldtag s m => HasSink (newtag :: k1) s (Rename oldtag m) Source # | Rename the tag. |
(forall x. Coercible (m x) (t2 (t1 m) x), Monad m, HasSink tag a (t2 (t1 m))) => HasSink (tag :: k) a ((t2 :.: t1) m) Source # | Compose two accessors. |
(tag ~ pos, HasPosition' pos struct v, HasState oldtag struct m) => HasSink (tag :: Nat) v (Pos pos oldtag m) Source # | Zoom in on the field at position |
(tag ~ field, HasField' field record v, HasState oldtag record m) => HasSink (tag :: Symbol) v (Field field oldtag m) Source # | Zoom in on the record field |
yield :: forall tag a m. HasSink tag a m => a -> m () Source #
yield @tag a
emits a
in the sink capability tag
.
Functional capability
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 SinkStack m (a :: *) Source #
Accumulate sunk values in a reverse order list.
SinkStack (m a) |
Instances
HasState tag [a] m => HasSink (tag :: k) a (SinkStack m) Source # | |
Monad m => Monad (SinkStack m) Source # | |
Functor m => Functor (SinkStack m) Source # | |
Applicative m => Applicative (SinkStack m) Source # | |
Defined in Capability.Sink.Internal.Strategies | |
MonadIO m => MonadIO (SinkStack m) Source # | |
Defined in Capability.Sink.Internal.Strategies | |
PrimMonad m => PrimMonad (SinkStack m) Source # | |
type PrimState (SinkStack m) Source # | |
Defined in Capability.Sink.Internal.Strategies |
newtype SinkDList m (a :: *) Source #
Accumulate sunk values in forward order in a difference list.
SinkDList (m a) |
Instances
HasSink tag (DList a) m => HasSink (tag :: k) a (SinkDList m) Source # | This instance may seem a bit odd at first. All it does is wrap each
deriving (HasSink tag w) via SinkDList (SinkLog (MonadState SomeStateMonad)) |
Monad m => Monad (SinkDList m) Source # | |
Functor m => Functor (SinkDList m) Source # | |
Applicative m => Applicative (SinkDList m) Source # | |
Defined in Capability.Sink.Internal.Strategies | |
MonadIO m => MonadIO (SinkDList m) Source # | |
Defined in Capability.Sink.Internal.Strategies | |
PrimMonad m => PrimMonad (SinkDList m) Source # | |
type PrimState (SinkDList m) Source # | |
Defined in Capability.Sink.Internal.Strategies |
newtype SinkLog m (a :: *) Source #
Accumulate sunk values with their own monoid.
SinkLog (m a) |
Instances
(Monoid w, HasState tag w m) => HasSink (tag :: k) w (SinkLog m) Source # | |
(Monoid w, HasState tag w m) => HasWriter (tag :: k) w (WriterLog m) Source # | |
Monad m => Monad (SinkLog m) Source # | |
Functor m => Functor (SinkLog m) Source # | |
Applicative m => Applicative (SinkLog m) Source # | |
Defined in Capability.Sink.Internal.Strategies | |
MonadIO m => MonadIO (SinkLog m) Source # | |
Defined in Capability.Sink.Internal.Strategies | |
PrimMonad m => PrimMonad (SinkLog m) Source # | |
type PrimState (SinkLog m) Source # | |
Defined in Capability.Sink.Internal.Strategies |
Modifiers
module Capability.Accessors