Safe Haskell | None |
---|---|
Language | Haskell2010 |
Capability.Source
Description
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
- data family Reified (tag :: k) (c :: Capability) (m :: Type -> Type)
- class Monad m => HasSource (tag :: k) a (m :: Type -> Type) | tag m -> a where
- await :: forall {k} (tag :: k) a m. HasSource tag a m => m a
- awaits :: forall {k} (tag :: k) 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 :: Type -> Type) a = MonadReader (m a)
- newtype MonadState (m :: Type -> Type) a = MonadState (m a)
- newtype ReadStatePure (m :: Type -> Type) a = ReadStatePure (m a)
- newtype ReadState (m :: Type -> Type) a = ReadState (m a)
- newtype ReaderIORef (m :: k -> Type) (a :: k) = ReaderIORef (m a)
- newtype ReaderRef (m :: Type -> Type) a = ReaderRef (m a)
- module Capability.Accessors
Relational capability
data family Reified (tag :: k) (c :: Capability) (m :: Type -> Type) Source #
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 HasSink
capability has the method
. The corresponding dictionary type is
defined as follows.yield
:: a -> m ()
>>>
:{
data instance Reified tag (HasSink tag a) m = ReifiedSink { _yield :: forall a. a -> m () } :}
Superclass dictionaries are represented as nested records. For example, the
HasState
capability has the superclasses
HasSource
and HasSink
and the method
. The corresponding
dictionary type is defined as follows.state
:: (s -> (a, s)) -> m a
>>>
:{
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 } :}
Instances
data Reified (tag :: k) (HasCatch tag e) m Source # | |
Defined in Capability.Error data Reified (tag :: k) (HasCatch tag e) m = ReifiedCatch {
| |
data Reified (tag :: k) (HasThrow tag e) m Source # | |
Defined in Capability.Error | |
data Reified (tag :: k) (HasReader tag r) m Source # | |
Defined in Capability.Reader.Internal.Class data Reified (tag :: k) (HasReader tag r) m = ReifiedReader {
| |
data Reified (tag :: k) (HasSink tag a) m Source # | |
Defined in Capability.Sink.Internal.Class | |
data Reified (tag :: k) (HasSource tag a) m Source # | |
Defined in Capability.Source.Internal.Class | |
data Reified (tag :: k) (HasState tag s) m Source # | |
Defined in Capability.State.Internal.Class data Reified (tag :: k) (HasState tag s) m = ReifiedState {
| |
data Reified (tag :: k) (HasWriter tag w) m Source # | |
Defined in Capability.Writer |
class Monad m => HasSource (tag :: k) a (m :: Type -> Type) | tag m -> a where Source #
Sourcing capability.
An instance does not need to fulfill any additional laws besides the monad laws.
Methods
Instances
(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 Methods await_ :: Proxy# tag -> MonadReader m r Source # | |
(HasState tag r m, MonadMask m) => HasSource (tag :: k) r (ReadState m) Source # | |
HasState tag r m => HasSource (tag :: k) r (ReadStatePure m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods await_ :: Proxy# tag -> ReadStatePure m r Source # | |
MonadState s m => HasSource (tag :: k) s (MonadState m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods await_ :: Proxy# tag -> MonadState m s Source # | |
(MutableRef ref, RefElement ref ~ s, HasSource tag ref m, PrimMonad m, PrimState m ~ MCState ref) => HasSource (tag :: k) s (ReaderRef m) Source # | |
(HasSource tag (IORef s) m, MonadIO m) => HasSource (tag :: k) s (ReaderIORef m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods 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 # | |
(Monad m, Reifies s (Reified tag (HasSource tag a) m)) => HasSource (tag :: k) a (Reflected s (HasSource tag a) m) Source # | |
(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 # | |
(Monad m, Reifies s (Reified tag (HasReader tag r) m)) => HasSource (tag :: k) r (Reflected s (HasReader tag r) m) Source # | |
(Monad m, Reifies s' (Reified tag (HasState tag s) m)) => HasSource (tag :: k) s (Reflected s' (HasState tag s) m) Source # | |
HasSource oldtag r m => HasSource (newtag :: k2) r (Rename oldtag m) Source # | Rename the tag. |
(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 {k} (tag :: k) a m. HasSource tag a m => m a Source #
await @tag a
takes a
from the source capability tag
.
awaits :: forall {k} (tag :: k) r m a. HasSource tag r m => (r -> a) -> m a Source #
awaits @tag
retrieves the image by f
of the environment
of the source 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 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 Source #
Derive HasSource
from m
's MonadReader
instance.
Constructors
MonadReader (m a) |
Instances
newtype MonadState (m :: Type -> Type) a Source #
Derive HasState
from m
's
MonadState
instance.
Constructors
MonadState (m a) |
Instances
MonadState s m => HasSink (tag :: k) s (MonadState m) Source # | |||||
Defined in Capability.Sink.Internal.Strategies Methods yield_ :: Proxy# tag -> s -> MonadState m () Source # | |||||
MonadState s m => HasSource (tag :: k) s (MonadState m) Source # | |||||
Defined in Capability.Source.Internal.Strategies Methods await_ :: Proxy# tag -> MonadState m s Source # | |||||
MonadState s m => HasState (tag :: k) s (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies Methods state_ :: Proxy# tag -> (s -> (a, s)) -> MonadState m a Source # | |||||
MonadIO m => MonadIO (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common Methods liftIO :: IO a -> MonadState m a # | |||||
Applicative m => Applicative (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common Methods pure :: a -> MonadState m a # (<*>) :: MonadState m (a -> b) -> MonadState m a -> MonadState m b # liftA2 :: (a -> b -> c) -> MonadState m a -> MonadState m b -> MonadState m c # (*>) :: MonadState m a -> MonadState m b -> MonadState m b # (<*) :: MonadState m a -> MonadState m b -> MonadState m a # | |||||
Functor m => Functor (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common Methods fmap :: (a -> b) -> MonadState m a -> MonadState m b # (<$) :: a -> MonadState m b -> MonadState m a # | |||||
Monad m => Monad (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common Methods (>>=) :: MonadState m a -> (a -> MonadState m b) -> MonadState m b # (>>) :: MonadState m a -> MonadState m b -> MonadState m b # return :: a -> MonadState m a # | |||||
PrimMonad m => PrimMonad (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common Associated Types
Methods primitive :: (State# (PrimState (MonadState m)) -> (# State# (PrimState (MonadState m)), a #)) -> MonadState m a # | |||||
type PrimState (MonadState m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common |
newtype ReadStatePure (m :: Type -> Type) 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
.
Constructors
ReadStatePure (m a) |
Instances
HasState tag r m => HasReader (tag :: k) r (ReadStatePure m) Source # | |
Defined in Capability.Reader.Internal.Strategies Methods local_ :: Proxy# tag -> (r -> r) -> ReadStatePure m a -> ReadStatePure m a Source # reader_ :: Proxy# tag -> (r -> a) -> ReadStatePure m a Source # | |
HasState tag r m => HasSource (tag :: k) r (ReadStatePure m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods await_ :: Proxy# tag -> ReadStatePure m r Source # | |
Applicative m => Applicative (ReadStatePure m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods pure :: a -> ReadStatePure m a # (<*>) :: ReadStatePure m (a -> b) -> ReadStatePure m a -> ReadStatePure m b # liftA2 :: (a -> b -> c) -> ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m c # (*>) :: ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b # (<*) :: ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m a # | |
Functor m => Functor (ReadStatePure m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods fmap :: (a -> b) -> ReadStatePure m a -> ReadStatePure m b # (<$) :: a -> ReadStatePure m b -> ReadStatePure m a # | |
Monad m => Monad (ReadStatePure m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods (>>=) :: ReadStatePure m a -> (a -> ReadStatePure m b) -> ReadStatePure m b # (>>) :: ReadStatePure m a -> ReadStatePure m b -> ReadStatePure m b # return :: a -> ReadStatePure m a # |
newtype ReadState (m :: Type -> Type) a Source #
Convert a state monad into a reader monad.
Use this if the monad stack allows catching exceptions.
See ReadStatePure
.
Constructors
ReadState (m a) |
Instances
(HasState tag r m, MonadMask m) => HasReader (tag :: k) r (ReadState m) Source # | |||||
(HasState tag r m, MonadMask m) => HasSource (tag :: k) r (ReadState m) Source # | |||||
MonadIO m => MonadIO (ReadState m) Source # | |||||
Defined in Capability.Source.Internal.Strategies | |||||
Applicative m => Applicative (ReadState m) Source # | |||||
Defined in Capability.Source.Internal.Strategies | |||||
Functor m => Functor (ReadState m) Source # | |||||
Monad m => Monad (ReadState m) Source # | |||||
PrimMonad m => PrimMonad (ReadState m) Source # | |||||
Defined in Capability.Source.Internal.Strategies Associated Types
| |||||
type PrimState (ReadState m) Source # | |||||
Defined in Capability.Source.Internal.Strategies |
newtype ReaderIORef (m :: k -> Type) (a :: k) 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.
Constructors
ReaderIORef (m a) |
Instances
(HasSource tag (IORef s) m, MonadIO m) => HasSink (tag :: k) s (ReaderIORef m) Source # | |
Defined in Capability.Sink.Internal.Strategies Methods yield_ :: Proxy# tag -> s -> ReaderIORef m () Source # | |
(HasSource tag (IORef s) m, MonadIO m) => HasSource (tag :: k) s (ReaderIORef m) Source # | |
Defined in Capability.Source.Internal.Strategies Methods await_ :: Proxy# tag -> ReaderIORef m s Source # | |
(HasReader tag (IORef s) m, MonadIO m) => HasState (tag :: k) s (ReaderIORef m) Source # | |
Defined in Capability.State.Internal.Strategies Methods state_ :: Proxy# tag -> (s -> (a, s)) -> ReaderIORef m a Source # | |
Applicative m => Applicative (ReaderIORef m) Source # | |
Defined in Capability.State.Internal.Strategies.Common Methods pure :: a -> ReaderIORef m a # (<*>) :: ReaderIORef m (a -> b) -> ReaderIORef m a -> ReaderIORef m b # liftA2 :: (a -> b -> c) -> ReaderIORef m a -> ReaderIORef m b -> ReaderIORef m c # (*>) :: ReaderIORef m a -> ReaderIORef m b -> ReaderIORef m b # (<*) :: ReaderIORef m a -> ReaderIORef m b -> ReaderIORef m a # | |
Functor m => Functor (ReaderIORef m) Source # | |
Defined in Capability.State.Internal.Strategies.Common Methods fmap :: (a -> b) -> ReaderIORef m a -> ReaderIORef m b # (<$) :: a -> ReaderIORef m b -> ReaderIORef m a # | |
Monad m => Monad (ReaderIORef m) Source # | |
Defined in Capability.State.Internal.Strategies.Common Methods (>>=) :: ReaderIORef m a -> (a -> ReaderIORef m b) -> ReaderIORef m b # (>>) :: ReaderIORef m a -> ReaderIORef m b -> ReaderIORef m b # return :: a -> ReaderIORef m a # |
newtype ReaderRef (m :: Type -> Type) 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
.
Constructors
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 # | |||||
MonadIO m => MonadIO (ReaderRef m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common | |||||
Applicative m => Applicative (ReaderRef m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common | |||||
Functor m => Functor (ReaderRef m) Source # | |||||
Monad m => Monad (ReaderRef m) Source # | |||||
PrimMonad m => PrimMonad (ReaderRef m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common Associated Types
| |||||
type PrimState (ReaderRef m) Source # | |||||
Defined in Capability.State.Internal.Strategies.Common |
Modifiers
module Capability.Accessors