monad-coroutine-0.7.1: Coroutine monad transformer for suspending and resuming monadic computations

Safe HaskellSafe-Infered

Control.Monad.Coroutine.SuspensionFunctors

Contents

Description

This module defines some common suspension functors for use with the Control.Monad.Coroutine module.

Synopsis

Suspension functors

data Yield x y Source

The Yield functor instance is equivalent to (,) but more descriptive.

Constructors

Yield x y 

Instances

newtype Await x y Source

The Await functor instance is equivalent to (->) but more descriptive.

Constructors

Await (x -> y) 

Instances

data Request request response x Source

The Request functor instance combines a Yield of a request with an Await for a response.

Constructors

Request request (response -> x) 

Instances

data ParseRequest x z Source

Instances

data EitherFunctor l r x Source

Combines two alternative functors into one, applying one or the other. Used for nested coroutines.

Constructors

LeftF (l x) 
RightF (r x) 

Instances

eitherFunctor :: (l x -> y) -> (r x -> y) -> EitherFunctor l r x -> ySource

Like either for the EitherFunctor data type.

yield :: Monad m => x -> Coroutine (Yield x) m ()Source

Suspend the current coroutine yielding a value.

await :: Monad m => Coroutine (Await x) m xSource

Suspend the current coroutine until a value is provided.

request :: Monad m => x -> Coroutine (Request x y) m ySource

Suspend yielding a request and awaiting the response.

requestParse :: (Monad m, MonoidNull y) => Parser a [x] y -> Coroutine (ParseRequest x) m (y, Maybe (Parser a [x] y))Source

Suspend yielding a request and awaiting the response.

Utility functions

concatYields :: (Monad m, Foldable f) => Coroutine (Yield (f x)) m r -> Coroutine (Yield x) m rSource

Converts a coroutine yielding collections of values into one yielding single values.

concatAwaits :: (Monad m, Foldable f) => Coroutine (Await x) m r -> Coroutine (Await (f x)) m rSource

Converts a coroutine awaiting single values into one awaiting collections of values.

Resolvers for running pairs of coroutines

awaitYieldResolver :: SeesawResolver (Await x) (Yield x) s1 s2Source

A SeesawResolver for running two coroutines in parallel, one of which awaits values while the other yields them. The yielding coroutine must not terminate before the other one.

awaitMaybeYieldResolver :: SeesawResolver (Await (Maybe x)) (Yield x) s1 s2Source

A SeesawResolver for running two coroutines in parallel, one of which awaits values while the other yields them. If the yielding coroutine terminates before the awaiting one, the latter will receive Nothing.

awaitYieldChunkResolver :: SeesawResolver (Await [x]) (Yield [x]) s1 s2Source

A SeesawResolver for running two coroutines in parallel, one of which awaits non-empty lists of values while the other yields them. If the yielding coroutine dies, the awaiting coroutine receives empty lists.

requestsResolver :: SeesawResolver (Request x y) (Request y x) s1 s2Source

A SeesawResolver for running two requesting coroutines in parallel. One coroutine's request becomes the other's response, and vice versa.

tickerYieldResolver :: SeesawResolver (Request (Ticker x) [x]) (Yield [x]) (Request (Ticker x) [x]) (Yield [x])Source

A SeesawResolver for running two coroutines in parallel. One coroutine produces data in chunks, the other consumes data in chunks. The boundaries of the two kinds of chunks need not be the same, as the consumed chunks are determined by a Ticker provided by the consumer's input request.

tickerRequestResolver :: SeesawResolver (Request (Ticker x) [x]) (Request [x] [x]) (Request (Ticker x) [x]) (Request [x] [x])Source

Like tickerYieldResolver, the only difference being that the producing coroutine sends its chunks using request rather than yield. The feedback received from request is the unconsumed remainder of the chunk, which lets the coroutine know when its sibling terminates.

lazyTickerRequestResolver :: SeesawResolver (Request (Ticker x) ([x], Either x (Ticker x))) (Request [x] [x]) (Request (Ticker x) ([x], Either x (Ticker x))) (Request [x] [x])Source

Like tickerRequestResolver, except the consuming coroutine requests receive both the selected prefix of the input chunk and a peek at either the next unconsumed input item, if any, or the final Ticker value. Chunks sent by the producing coroutine never get combined for the consuming coroutine. This allows better synchronization between the two coroutines. It also leaks the information about the produced chunk boundaries into the consuming coroutine, so this resolver should be used with caution.

parserRequestResolver :: Monoid y => SeesawResolver (Request (Parser a [x] y) y) (Request [x] [x]) (Request (Parser a [x] y) y) (Request [x] [x])Source

Like parserYieldResolver, the only difference being that the producing coroutine sends its chunks using request rather than yield. The feedback received from request is the unconsumed remainder of the chunk, which lets the coroutine know when its sibling terminates.

lazyParserRequestResolver :: SeesawResolver (ParseRequest x) (Request [x] [x]) (ParseRequest x) (Request [x] [x])Source

Like parserRequestResolver, except the consuming coroutine requests receive both the selected prefix of the input chunk and a peek at either the next unconsumed input item, if any, or the final Parser value. Chunks sent by the producing coroutine never get combined for the consuming coroutine. This allows better synchronization between the two coroutines. It also leaks the information about the produced chunk boundaries into the consuming coroutine, so this resolver should be used with caution.

liftedTickerYieldResolver :: (Functor s1, Functor s2) => (forall a. Request (Ticker x) [x] a -> s1 a) -> (forall a. Yield [x] a -> s2 a) -> SeesawResolver (Request (Ticker x) [x]) (Yield [x]) s1 s2Source

A generic version of tickerYieldResolver, allowing coroutines with Request and Yield functors embedded in other functors.

liftedTickerRequestResolver :: (Functor s1, Functor s2) => (forall a. Request (Ticker x) [x] a -> s1 a) -> (forall a. Request [x] [x] a -> s2 a) -> SeesawResolver (Request (Ticker x) [x]) (Request [x] [x]) s1 s2Source

A generic version of tickerRequestResolver, allowing coroutines with Request functors embedded in other functors.

liftedLazyTickerRequestResolver :: (Functor s1, Functor s2) => (forall a. Request [x] [x] a -> s2 a) -> SeesawResolver (Request (Ticker x) ([x], Either x (Ticker x))) (Request [x] [x]) s1 s2Source

A generic version of lazyTickerRequestResolver, allowing coroutines with Request functors embedded in other functors.

liftedParserRequestResolver :: (Functor s1, Functor s2, Monoid y) => (forall b. Request (Parser a [x] y) y b -> s1 b) -> (forall b. Request [x] [x] b -> s2 b) -> SeesawResolver (Request (Parser a [x] y) y) (Request [x] [x]) s1 s2Source

A generic version of parserRequestResolver, allowing coroutines with Request functors embedded in other functors.

nestedLazyParserRequestResolver :: (Functor s1, Functor s2) => SeesawResolver (ParseRequest x) (Request [x] [x]) (EitherFunctor s1 (ParseRequest x)) (EitherFunctor s2 (Request [x] [x]))Source

A generic version of lazyParserRequestResolver, allowing coroutines with Request functors embedded in other functors.