This module defines various Control.Concurrent.SCC.Coroutine
types that operate on
Sink
and Source
values. The simplest of the bunch
are Consumer
and Producer
types, which respectively operate on a single source or sink. A Transducer
has access
both to a Source
to read from and a Sink
to write
into. Finally, a Splitter
reads from a single source and writes all input into two sinks of the same type,
signalling interesting input boundaries by writing into the third sink.
- newtype Performer m r = Performer {
- perform :: m r
- type OpenConsumer m a d x r = AncestorFunctor a d => Source m a x -> Coroutine d m r
- newtype Consumer m x r = Consumer {
- consume :: forall a d. OpenConsumer m a d x r
- type OpenProducer m a d x r = AncestorFunctor a d => Sink m a x -> Coroutine d m r
- newtype Producer m x r = Producer {
- produce :: forall a d. OpenProducer m a d x r
- type OpenTransducer m a1 a2 d x y r = (AncestorFunctor a1 d, AncestorFunctor a2 d) => Source m a1 x -> Sink m a2 y -> Coroutine d m r
- newtype Transducer m x y = Transducer {
- transduce :: forall a1 a2 d. OpenTransducer m a1 a2 d x y ()
- type OpenSplitter m a1 a2 a3 a4 d x b r = (AncestorFunctor a1 d, AncestorFunctor a2 d, AncestorFunctor a3 d, AncestorFunctor a4 d) => Source m a1 x -> Sink m a2 x -> Sink m a3 x -> Sink m a4 b -> Coroutine d m r
- newtype Splitter m x b = Splitter {
- split :: forall a1 a2 a3 a4 d. OpenSplitter m a1 a2 a3 a4 d x b ()
- data Boundary y
- data Markup y x
- type Parser m x b = Transducer m x (Markup b x)
- class Branching c m x r | c -> m x where
- combineBranches :: (forall d. Bool -> (forall a d'. AncestorFunctor d d' => OpenConsumer m a d' x r) -> (forall a d'. AncestorFunctor d d' => OpenConsumer m a d' x r) -> forall a. OpenConsumer m a d x r) -> Bool -> c -> c -> c
- isolateConsumer :: forall m x r. Monad m => (forall d. Functor d => Source m d x -> Coroutine d m r) -> Consumer m x r
- isolateProducer :: forall m x r. Monad m => (forall d. Functor d => Sink m d x -> Coroutine d m r) -> Producer m x r
- isolateTransducer :: forall m x y. Monad m => (forall d. Functor d => Source m d x -> Sink m d y -> Coroutine d m ()) -> Transducer m x y
- isolateSplitter :: forall m x b. Monad m => (forall d. Functor d => Source m d x -> Sink m d x -> Sink m d x -> Sink m d b -> Coroutine d m ()) -> Splitter m x b
- oneToOneTransducer :: Monad m => (x -> y) -> Transducer m x y
- statelessTransducer :: Monad m => (x -> [y]) -> Transducer m x y
- statefulTransducer :: Monad m => (state -> x -> (state, [y])) -> state -> Transducer m x y
- statelessSplitter :: Monad m => (x -> Bool) -> Splitter m x b
- statefulSplitter :: Monad m => (state -> x -> (state, Bool)) -> state -> Splitter m x ()
- splitToConsumers :: (Functor d, Monad m, d1 ~ SinkFunctor d x, AncestorFunctor a (SinkFunctor (SinkFunctor d1 x) b)) => Splitter m x b -> Source m a x -> (Source m (SourceFunctor d x) x -> Coroutine (SourceFunctor d x) m r1) -> (Source m (SourceFunctor d1 x) x -> Coroutine (SourceFunctor d1 x) m r2) -> (Source m (SourceFunctor (SinkFunctor d1 x) b) b -> Coroutine (SourceFunctor (SinkFunctor d1 x) b) m r3) -> Coroutine d m ((), r1, r2, r3)
- splitInputToConsumers :: forall m a d d1 x b. (MonadParallel m, d1 ~ SinkFunctor d x, AncestorFunctor a d) => Bool -> Splitter m x b -> Source m a x -> (Source m (SourceFunctor d1 x) x -> Coroutine (SourceFunctor d1 x) m ()) -> (Source m (SourceFunctor d x) x -> Coroutine (SourceFunctor d x) m ()) -> Coroutine d m ()
- pipePS :: forall m a a1 a2 x r1 r2. (MonadParallel m, Functor a, a1 ~ SinkFunctor a x, a2 ~ SourceFunctor a x) => Bool -> (Sink m a1 x -> Coroutine a1 m r1) -> (Source m a2 x -> Coroutine a2 m r2) -> Coroutine a m (r1, r2)
- (>|>) :: MonadParallel m => Transducer m x y -> Transducer m y z -> Transducer m x z
- (<|<) :: MonadParallel m => Transducer m y z -> Transducer m x y -> Transducer m x z
Types
A coroutine that has no inputs nor outputs - and therefore may not suspend at all, which means it's not really a coroutine.
MonadParallel m => PipeableComponentPair m x (Producer m x ()) (Consumer m x ()) (Performer m ()) | |
MonadParallel m => JoinableComponentPair TransducerType (PerformerType r) TransducerType m [x] [y] (Transducer m x y) (Performer m r) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (PerformerType r) TransducerType TransducerType m [x] [y] (Performer m r) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (PerformerType r1) (PerformerType r2) (PerformerType r2) m () () (Performer m r1) (Performer m r2) (Performer m r2) | |
MonadParallel m => JoinableComponentPair (PerformerType r1) (ProducerType r2) (ProducerType r2) m () [x] (Performer m r1) (Producer m x r2) (Producer m x r2) | |
MonadParallel m => JoinableComponentPair (ProducerType r1) (PerformerType r2) (ProducerType r2) m () [x] (Producer m x r1) (Performer m r2) (Producer m x r2) | |
MonadParallel m => JoinableComponentPair (PerformerType r1) (ConsumerType r2) (ConsumerType r2) m [x] () (Performer m r1) (Consumer m x r2) (Consumer m x r2) | |
MonadParallel m => JoinableComponentPair (ConsumerType r1) (PerformerType r2) (ConsumerType r2) m [x] () (Consumer m x r1) (Performer m r2) (Consumer m x r2) | |
(AnyListOrUnit x, AnyListOrUnit y) => CompatibleSignature (Performer m r) (PerformerType r) m x y | |
(AnyListOrUnit x, AnyListOrUnit y) => CompatibleSignature (Performer m r) (PerformerType r) m x y |
type OpenConsumer m a d x r = AncestorFunctor a d => Source m a x -> Coroutine d m rSource
A coroutine that consumes values from a Source
.
Consumer | |
|
MonadParallel m => PipeableComponentPair m x (Producer m x ()) (Consumer m x ()) (Performer m ()) | |
MonadParallel m => PipeableComponentPair m y (Transducer m x y) (Consumer m y r) (Consumer m x r) | |
MonadParallel m => JoinableComponentPair TransducerType (ConsumerType ()) TransducerType m [x] [y] (Transducer m x y) (Consumer m x ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ConsumerType ()) TransducerType TransducerType m [x] [y] (Consumer m x ()) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ProducerType ()) (ConsumerType ()) TransducerType m [x] [y] (Producer m y ()) (Consumer m x ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ConsumerType ()) (ProducerType ()) TransducerType m [x] [y] (Consumer m x ()) (Producer m y ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (PerformerType r1) (ConsumerType r2) (ConsumerType r2) m [x] () (Performer m r1) (Consumer m x r2) (Consumer m x r2) | |
MonadParallel m => JoinableComponentPair (ConsumerType r1) (PerformerType r2) (ConsumerType r2) m [x] () (Consumer m x r1) (Performer m r2) (Consumer m x r2) | |
MonadParallel m => JoinableComponentPair (ConsumerType ()) (ConsumerType ()) (ConsumerType ()) m [x] () (Consumer m x ()) (Consumer m x ()) (Consumer m x ()) | |
Monad m => Branching (Consumer m x r) m x r | |
AnyListOrUnit y => CompatibleSignature (Consumer m x r) (ConsumerType r) m [x] y | |
AnyListOrUnit y => CompatibleSignature (Consumer m x r) (ConsumerType r) m [x] y |
type OpenProducer m a d x r = AncestorFunctor a d => Sink m a x -> Coroutine d m rSource
A coroutine that produces values and puts them into a Sink
.
Producer | |
|
MonadParallel m => PipeableComponentPair m x (Producer m x ()) (Consumer m x ()) (Performer m ()) | |
MonadParallel m => PipeableComponentPair m x (Producer m x r) (Transducer m x y) (Producer m y r) | |
MonadParallel m => JoinableComponentPair TransducerType (ProducerType ()) TransducerType m [x] [y] (Transducer m x y) (Producer m y ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ProducerType ()) TransducerType TransducerType m [x] [y] (Producer m y ()) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ProducerType ()) (ConsumerType ()) TransducerType m [x] [y] (Producer m y ()) (Consumer m x ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ConsumerType ()) (ProducerType ()) TransducerType m [x] [y] (Consumer m x ()) (Producer m y ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (PerformerType r1) (ProducerType r2) (ProducerType r2) m () [x] (Performer m r1) (Producer m x r2) (Producer m x r2) | |
MonadParallel m => JoinableComponentPair (ProducerType r1) (PerformerType r2) (ProducerType r2) m () [x] (Producer m x r1) (Performer m r2) (Producer m x r2) | |
Monad m => JoinableComponentPair (ProducerType r1) (ProducerType r2) (ProducerType r2) m () [x] (Producer m x r1) (Producer m x r2) (Producer m x r2) | |
AnyListOrUnit y => CompatibleSignature (Producer m x r) (ProducerType r) m y [x] | |
AnyListOrUnit y => CompatibleSignature (Producer m x r) (ProducerType r) m y [x] |
type OpenTransducer m a1 a2 d x y r = (AncestorFunctor a1 d, AncestorFunctor a2 d) => Source m a1 x -> Sink m a2 y -> Coroutine d m rSource
newtype Transducer m x y Source
The Transducer
type represents coroutines that transform a data stream. Execution of transduce
must continue
consuming the given Source
and feeding the Sink
as
long as there is any data in the source.
Transducer | |
|
MonadParallel m => JoinableComponentPair TransducerType TransducerType TransducerType m [x] [y] (Transducer m x y) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => PipeableComponentPair m y (Transducer m x y) (Transducer m y z) (Transducer m x z) | |
MonadParallel m => PipeableComponentPair m x (Producer m x r) (Transducer m x y) (Producer m y r) | |
MonadParallel m => PipeableComponentPair m y (Transducer m x y) (Consumer m y r) (Consumer m x r) | |
MonadParallel m => JoinableComponentPair TransducerType (PerformerType r) TransducerType m [x] [y] (Transducer m x y) (Performer m r) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair TransducerType (ProducerType ()) TransducerType m [x] [y] (Transducer m x y) (Producer m y ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair TransducerType (ConsumerType ()) TransducerType m [x] [y] (Transducer m x y) (Consumer m x ()) (Transducer m x y) | |
Monad m => Category (Transducer m) | |
MonadParallel m => JoinableComponentPair (PerformerType r) TransducerType TransducerType m [x] [y] (Performer m r) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ProducerType ()) TransducerType TransducerType m [x] [y] (Producer m y ()) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ConsumerType ()) TransducerType TransducerType m [x] [y] (Consumer m x ()) (Transducer m x y) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ProducerType ()) (ConsumerType ()) TransducerType m [x] [y] (Producer m y ()) (Consumer m x ()) (Transducer m x y) | |
MonadParallel m => JoinableComponentPair (ConsumerType ()) (ProducerType ()) TransducerType m [x] [y] (Consumer m x ()) (Producer m y ()) (Transducer m x y) | |
Monad m => Branching (Transducer m x y) m x () | |
CompatibleSignature (Transducer m x y) TransducerType m [x] [y] | |
CompatibleSignature (Transducer m x y) TransducerType m [x] [y] |
type OpenSplitter m a1 a2 a3 a4 d x b r = (AncestorFunctor a1 d, AncestorFunctor a2 d, AncestorFunctor a3 d, AncestorFunctor a4 d) => Source m a1 x -> Sink m a2 x -> Sink m a3 x -> Sink m a4 b -> Coroutine d m rSource
The Splitter
type represents coroutines that distribute the input stream acording to some criteria. A splitter
should distribute only the original input data, and feed it into the sinks in the same order it has been read from
the source. Furthermore, the input source should be entirely consumed and fed into the first two sinks. The third
sink can be used to supply extra information at arbitrary points in the input.
A splitter can be used in two ways: as a predicate to determine which portions of its input stream satisfy a certain property, or as a chunker to divide the input stream into chunks. In the former case, the predicate is considered true for exactly those parts of the input that are written to its true sink. In the latter case, a chunk is a contiguous section of the input stream that is written exclusively to one sink, either true or false. Anything written to the third sink also terminates the chunk.
Splitter | |
|
MonadParallel m => Branching (Splitter m x b) m x () |
type Parser m x b = Transducer m x (Markup b x)Source
Type classes
class Branching c m x r | c -> m x whereSource
Branching
is a type class representing all types that can act as consumers, namely Consumer
,
Transducer
, and Splitter
.
combineBranches :: (forall d. Bool -> (forall a d'. AncestorFunctor d d' => OpenConsumer m a d' x r) -> (forall a d'. AncestorFunctor d d' => OpenConsumer m a d' x r) -> forall a. OpenConsumer m a d x r) -> Bool -> c -> c -> cSource
combineBranches
is used to combine two values of Branch
class into one, using the given Consumer
binary
combinator.
MonadParallel m => Branching (Splitter m x b) m x () | |
Monad m => Branching (Transducer m x y) m x () | |
Monad m => Branching (Consumer m x r) m x r |
Constructors
isolateConsumer :: forall m x r. Monad m => (forall d. Functor d => Source m d x -> Coroutine d m r) -> Consumer m x rSource
Creates a proper Consumer
from a function that is, but can't be proven to be, an OpenConsumer
.
isolateProducer :: forall m x r. Monad m => (forall d. Functor d => Sink m d x -> Coroutine d m r) -> Producer m x rSource
Creates a proper Producer
from a function that is, but can't be proven to be, an OpenProducer
.
isolateTransducer :: forall m x y. Monad m => (forall d. Functor d => Source m d x -> Sink m d y -> Coroutine d m ()) -> Transducer m x ySource
Creates a proper Transducer
from a function that is, but can't be proven to be, an OpenTransducer
.
isolateSplitter :: forall m x b. Monad m => (forall d. Functor d => Source m d x -> Sink m d x -> Sink m d x -> Sink m d b -> Coroutine d m ()) -> Splitter m x bSource
Creates a proper Splitter
from a function that is, but can't be proven to be, an OpenSplitter
.
oneToOneTransducer :: Monad m => (x -> y) -> Transducer m x ySource
Function oneToOneTransducer
takes a function that maps one input value to one output value each, and lifts it
into a Transducer
.
statelessTransducer :: Monad m => (x -> [y]) -> Transducer m x ySource
Function statelessTransducer
takes a function that maps one input value into a list of output values, and
lifts it into a Transducer
.
statefulTransducer :: Monad m => (state -> x -> (state, [y])) -> state -> Transducer m x ySource
Function statefulTransducer
constructs a Transducer
from a state-transition function and the initial
state. The transition function may produce arbitrary output at any transition step.
statelessSplitter :: Monad m => (x -> Bool) -> Splitter m x bSource
Function statelessSplitter
takes a function that assigns a Boolean value to each input item and lifts it into
a Splitter
.
statefulSplitter :: Monad m => (state -> x -> (state, Bool)) -> state -> Splitter m x ()Source
Function statefulSplitter
takes a state-converting function that also assigns a Boolean value to each input
item and lifts it into a Splitter
.
Utility functions
splitToConsumers :: (Functor d, Monad m, d1 ~ SinkFunctor d x, AncestorFunctor a (SinkFunctor (SinkFunctor d1 x) b)) => Splitter m x b -> Source m a x -> (Source m (SourceFunctor d x) x -> Coroutine (SourceFunctor d x) m r1) -> (Source m (SourceFunctor d1 x) x -> Coroutine (SourceFunctor d1 x) m r2) -> (Source m (SourceFunctor (SinkFunctor d1 x) b) b -> Coroutine (SourceFunctor (SinkFunctor d1 x) b) m r3) -> Coroutine d m ((), r1, r2, r3)Source
Given a Splitter
, a Source
, and three consumer functions, splitToConsumers
runs the splitter on the source
and feeds the splitter's outputs to its true, false, and edge sinks, respectively, to the three consumers.
splitInputToConsumers :: forall m a d d1 x b. (MonadParallel m, d1 ~ SinkFunctor d x, AncestorFunctor a d) => Bool -> Splitter m x b -> Source m a x -> (Source m (SourceFunctor d1 x) x -> Coroutine (SourceFunctor d1 x) m ()) -> (Source m (SourceFunctor d x) x -> Coroutine (SourceFunctor d x) m ()) -> Coroutine d m ()Source
Given a Splitter
, a Source
, and two consumer functions, splitInputToConsumers
runs the splitter on the source
and feeds the splitter's true and false outputs, respectively, to the two consumers.
pipePS :: forall m a a1 a2 x r1 r2. (MonadParallel m, Functor a, a1 ~ SinkFunctor a x, a2 ~ SourceFunctor a x) => Bool -> (Sink m a1 x -> Coroutine a1 m r1) -> (Source m a2 x -> Coroutine a2 m r2) -> Coroutine a m (r1, r2)Source
(>|>) :: MonadParallel m => Transducer m x y -> Transducer m y z -> Transducer m x zSource
Same as Control.Category.>>>
except it runs the two transducers in parallel.
(<|<) :: MonadParallel m => Transducer m y z -> Transducer m x y -> Transducer m x zSource
Same as Control.Category.<<<
except it runs the two transducers in parallel.