Safe Haskell | None |
---|

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

- data Yield x y = Yield x y
- newtype Await x y = Await (x -> y)
- data Request request response x = Request request (response -> x)
- data ReadRequest x z
- data ReadingResult x py y
- = ResultPart py (Reader x py y)
- | FinalResult y

- type Reader x py y = x -> Reading x py y
- data Reading x py y
- data EitherFunctor l r x
- eitherFunctor :: (l x -> y) -> (r x -> y) -> EitherFunctor l r x -> y
- yield :: Monad m => x -> Coroutine (Yield x) m ()
- await :: Monad m => Coroutine (Await x) m x
- request :: Monad m => x -> Coroutine (Request x y) m y
- requestRead :: (Monad m, Monoid x) => Reader x py y -> Coroutine (ReadRequest x) m (ReadingResult x py y)
- concatYields :: (Monad m, Foldable f) => Coroutine (Yield (f x)) m r -> Coroutine (Yield x) m r
- concatAwaits :: (Monad m, Foldable f) => Coroutine (Await x) m r -> Coroutine (Await (f x)) m r
- weaveAwaitYield :: Monad m => x -> WeaveStepper (Await x) (Yield x) Identity m r1 r2 (r1, r2)
- weaveAwaitMaybeYield :: Monad m => WeaveStepper (Await (Maybe x)) (Yield x) Identity m r1 r2 (r1, r2)
- weaveRequests :: Monad m => x -> y -> WeaveStepper (Request x y) (Request y x) (Yield (x, y)) m r1 r2 (r1, r2)
- weaveReadWriteRequests :: (Monad m, Monoid x) => WeaveStepper (ReadRequest x) (Request x x) Identity m r1 r2 (r1, r2)
- weaveNestedReadWriteRequests :: (Monad m, Functor s, Monoid x) => NestWeaveStepper s (ReadRequest x) (Request x x) m r1 r2 (r1, r2)

# Suspension functors

The `Yield`

functor instance is equivalent to (,) but more descriptive. A coroutine with this suspension functor
provides a value with every suspension.

Yield x y |

The `Await`

functor instance is equivalent to (->) but more descriptive. A coroutine with this suspension functor
demands a value whenever it suspends, before it can resume its execution.

Await (x -> y) |

data ReadRequest x z Source

Combines a `Yield`

of a `Reader`

with an `Await`

for a `ReadingResult`

.

Functor (ReadRequest x) |

data ReadingResult x py y Source

ResultPart py (Reader x py y) | A part of the result with the reader of more input |

FinalResult y | Final result chunk |

data EitherFunctor l r x Source

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

(Functor l, Functor r) => Functor (EitherFunctor l r) | |

(Functor (EitherFunctor p s), Functor p, Functor s) => ChildFunctor (EitherFunctor p s) |

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.

requestRead :: (Monad m, Monoid x) => Reader x py y -> Coroutine (ReadRequest x) m (ReadingResult x py y)Source

Suspend yielding a `ReadRequest`

and awaiting the `ReadingResult`

.

# 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.

# WeaveSteppers for weaving pairs of coroutines

weaveAwaitYield :: Monad m => x -> WeaveStepper (Await x) (Yield x) Identity m r1 r2 (r1, r2)Source

weaveAwaitMaybeYield :: Monad m => WeaveStepper (Await (Maybe x)) (Yield x) Identity m r1 r2 (r1, r2)Source

weaveRequests :: Monad m => x -> y -> WeaveStepper (Request x y) (Request y x) (Yield (x, y)) m r1 r2 (r1, r2)Source

weaveReadWriteRequests :: (Monad m, Monoid x) => WeaveStepper (ReadRequest x) (Request x x) Identity m r1 r2 (r1, r2)Source

The consumer coroutine requests input through `ReadRequest`

and gets `ReadingResult`

in response. The producer
coroutine receives the unconsumed portion of its last requested chunk as response.

weaveNestedReadWriteRequests :: (Monad m, Functor s, Monoid x) => NestWeaveStepper s (ReadRequest x) (Request x x) m r1 r2 (r1, r2)Source

Like `weaveReadWriteRequests`

but for nested coroutines.