Data.Enumerator.Internal

enumerator-0.4.20: Reliable, high-performance processing with left-fold enumerators

Copyright | 2010-2011 John Millikin
License | MIT
Maintainer | jmillikin@gmail.com
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Data.Enumerator.Internal

Contents

- Primitives
- Operators
- Miscellaneous

Description

Core enumerator types, and some useful primitives.

Be careful when using the functions defined in this module, as they will allow you to create iteratees which violate the monad laws.

Synopsis

- data Stream a
  - = Chunks [a]
  - | EOF
- newtype Iteratee a m b = Iteratee {
  - runIteratee :: m (Step a m b)
  }
- data Step a m b
  - = Continue (Stream a -> Iteratee a m b)
  - | Yield b (Stream a)
  - | Error SomeException
- type Enumerator a m b = Step a m b -> Iteratee a m b
- type Enumeratee ao ai m b = Step ai m b -> Iteratee ao m (Step ai m b)
- returnI :: Monad m => Step a m b -> Iteratee a m b
- continue :: Monad m => (Stream a -> Iteratee a m b) -> Iteratee a m b
- yield :: Monad m => b -> Stream a -> Iteratee a m b
- (>>==) :: Monad m => Iteratee a m b -> (Step a m b -> Iteratee a' m b') -> Iteratee a' m b'
- (==<<) :: Monad m => (Step a m b -> Iteratee a' m b') -> Iteratee a m b -> Iteratee a' m b'
- ($$) :: Monad m => (Step a m b -> Iteratee a' m b') -> Iteratee a m b -> Iteratee a' m b'
- (>==>) :: Monad m => Enumerator a m b -> (Step a m b -> Iteratee a' m b') -> Step a m b -> Iteratee a' m b'
- (<==<) :: Monad m => (Step a m b -> Iteratee a' m b') -> Enumerator a m b -> Step a m b -> Iteratee a' m b'
- enumEOF :: Monad m => Enumerator a m b
- checkContinue0 :: Monad m => (Enumerator a m b -> (Stream a -> Iteratee a m b) -> Iteratee a m b) -> Enumerator a m b
- checkContinue1 :: Monad m => ((s1 -> Enumerator a m b) -> s1 -> (Stream a -> Iteratee a m b) -> Iteratee a m b) -> s1 -> Enumerator a m b
- checkDoneEx :: Monad m => Stream a' -> ((Stream a -> Iteratee a m b) -> Iteratee a' m (Step a m b)) -> Enumeratee a' a m b
- checkDone :: Monad m => ((Stream a -> Iteratee a m b) -> Iteratee a' m (Step a m b)) -> Enumeratee a' a m b

# Documentation

data Stream a

A `Stream` is a sequence of chunks generated by an `Enumerator`.

`(`Chunks` [])` is used to indicate that a stream is still active, but currently has no available data. Iteratees should ignore empty chunks.

Constructors

Chunks [a] |
EOF |

Instances

Monad Stream |
Functor Stream |
Applicative Stream | Since: 0.4.5
Eq a => Eq (Stream a) |
Show a => Show (Stream a) |
Monoid (Stream a) |

newtype Iteratee a m b

The primary data type for this library; an iteratee consumes chunks of input from a stream until it either yields a value or encounters an error.

Compatibility note: `Iteratee` will become abstract in `enumerator_0.5`. If you depend on internal implementation details, please import `Data.Enumerator.Internal`.

Constructors

Iteratee |
Fields runIteratee :: m (Step a m b)

Instances

MonadTrans (Iteratee a) |
Monad m => Monad (Iteratee a m) |
Monad m => Functor (Iteratee a m) |
Monad m => Applicative (Iteratee a m) |
MonadIO m => MonadIO (Iteratee a m) |

data Step a m b

Constructors

Continue (Stream a -> Iteratee a m b) | The `Iteratee` is capable of accepting more input. Note that more input is not necessarily required; the `Iteratee` might be able to generate a value immediately if it receives `EOF`.
Yield b (Stream a) | The `Iteratee` cannot receive any more input, and has generated a result. Included in this value is left-over input, which can be passed to composed `Iteratee`s.
Error SomeException | The `Iteratee` encountered an error which prevents it from proceeding further.

type Enumerator a m b = Step a m b -> Iteratee a m b

Enumerators are sources of data, to be consumed by iteratees. Enumerators typically read from an external source (parser, handle, random generator, etc), then feed chunks into an tteratee until:

- The input source runs out of data.
- The iteratee yields a result value.
- The iteratee throws an exception.

type Enumeratee ao ai m b = Step ai m b -> Iteratee ao m (Step ai m b)

An enumeratee acts as a stream adapter; place one between an enumerator and an iteratee, and it changes the type or contents of the input stream.

Most users will want to combine enumerators, enumeratees, and iteratees using the stream combinators `joinI` and `joinE`, or their operator aliases `(=$)` and `($=)`. These combinators are used to manage how left-over input is passed between elements of the data processing pipeline.

# Primitives

returnI :: Monad m => Step a m b -> Iteratee a m b

    returnI step = Iteratee (return step)

continue :: Monad m => (Stream a -> Iteratee a m b) -> Iteratee a m b

    continue k = returnI (Continue k)

yield :: Monad m => b -> Stream a -> Iteratee a m b

    yield x extra = returnI (Yield x extra)

WARNING: due to the current encoding of iteratees in this library, careless use of the `yield` primitive may violate the monad laws. To prevent this, always make sure that an iteratee never yields extra data unless it has received at least one input element.

More strictly, iteratees may not yield data that they did not receive as input. Don't use `yield` to “inject” elements into the stream.

# Operators

(>>==) :: Monad m => Iteratee a m b -> (Step a m b -> Iteratee a' m b') -> Iteratee a' m b' infixl 1

The most primitive stream operator. `iter >>== enum` returns a new iteratee which will read from `enum` before continuing.

(==<<) :: Monad m => (Step a m b -> Iteratee a' m b') -> Iteratee a m b -> Iteratee a' m b' infixr 1

    (==<<) = flip (>>==)

($$) :: Monad m => (Step a m b -> Iteratee a' m b') -> Iteratee a m b -> Iteratee a' m b' infixr 0

    ($$) = (==<<)

This is somewhat easier to read when constructing an iteratee from many processing stages. You can treat it like `(`$`)`, and read the data flow from left to right.

Since: 0.1.1

(>==>) :: Monad m => Enumerator a m b -> (Step a m b -> Iteratee a' m b') -> Step a m b -> Iteratee a' m b' infixr 1

    (>==>) enum1 enum2 step = enum1 step >>== enum2

The moral equivalent of `(`>=>`)` for iteratees.

Since: 0.1.1

(<==<) :: Monad m => (Step a m b -> Iteratee a' m b') -> Enumerator a m b -> Step a m b -> Iteratee a' m b' infixr 1

    (<==<) = flip (>==>)

Since: 0.1.1

# Miscellaneous

enumEOF :: Monad m => Enumerator a m b

Sends `EOF` to its iteratee. Most clients should use `run` or `run_` instead.

checkContinue0 :: Monad m => (Enumerator a m b -> (Stream a -> Iteratee a m b) -> Iteratee a m b) -> Enumerator a m b

A common pattern in `Enumerator` implementations is to check whether the inner `Iteratee` has finished, and if so, to return its output. `checkContinue0` passes its parameter a continuation if the `Iteratee` can still consume input; if not, it returns the iteratee's step.

The type signature here is a bit crazy, but it's actually very easy to use. Take this code:

    repeat :: Monad m => a -> Enumerator a m b
    repeat x = loop where
    	loop (Continue k) = k (Chunks [x]) >>== loop
    	loop step = returnI step

And rewrite it without the boilerplate:

    repeat :: Monad m => a -> Enumerator a m b
    repeat x = checkContinue0 $ \loop k -> k (Chunks [x] >>== loop

Since: 0.4.9

checkContinue1 :: Monad m => ((s1 -> Enumerator a m b) -> s1 -> (Stream a -> Iteratee a m b) -> Iteratee a m b) -> s1 -> Enumerator a m b

Like `checkContinue0`, but allows each loop step to use a state value:

    iterate :: Monad m => (a -> a) -> a -> Enumerator a m b
    iterate f = checkContinue1 $ \loop a k -> k (Chunks [a]) >>== loop (f a)

Since: 0.4.9

checkDoneEx :: Monad m => Stream a' -> ((Stream a -> Iteratee a m b) -> Iteratee a' m (Step a m b)) -> Enumeratee a' a m b

A common pattern in `Enumeratee` implementations is to check whether the inner `Iteratee` has finished, and if so, to return its output. `checkDone` passes its parameter a continuation if the `Iteratee` can still consume input, or yields otherwise.

Since: 0.4.3

checkDone :: Monad m => ((Stream a -> Iteratee a m b) -> Iteratee a' m (Step a m b)) -> Enumeratee a' a m b

    checkDone = checkDoneEx (Chunks [])

Use this for enumeratees which do not have an input buffer.