Enumerator and iteratee: Difference between revisions
Line 49: | Line 49: | ||
== Functions == | == Functions == | ||
You can compose iteratees sequentially in time. This is done by <code-haskell>(>>)</code-haskell>. <code-haskell>it0 >> it1</code-haskell> means that when <code-haskell>it0</code-haskell> finishes, <code-haskell>it1</code-haskell> starts. Generally speaking, <code-haskell>Iteratee i</code-haskell> is a <code-haskell>Monad</code-haskell> | You can compose iteratees sequentially in time. This is done by <code-haskell>(>>)</code-haskell>. <code-haskell>it0 >> it1</code-haskell> means that when <code-haskell>it0</code-haskell> finishes, <code-haskell>it1</code-haskell> starts. Generally speaking, <code-haskell>Iteratee i</code-haskell> is a <code-haskell>Monad</code-haskell>, and it works exactly like a [[monadic parser]]. | ||
<pre-haskell> | <pre-haskell> | ||
{- s = state -} | {- s = state -} | ||
Line 64: | Line 64: | ||
</pre-haskell> | </pre-haskell> | ||
You can compose iteratees sequentially in space. <code-haskell>it0</code-haskell>'s output messages become <code-haskell>it1</code-haskell>'s input messages | You can also compose iteratees sequentially in space. <code-haskell>it0</code-haskell>'s output messages become <code-haskell>it1</code-haskell>'s input messages, so <code-haskell>it0</code-haskell> and <code-haskell>it1</code-haskell> work in parallel. Their composition is denoted <code-haskell>it1 . it0</code-haskell>. If <code-haskell>it0</code-haskell> finishes, it is resurrected to its original state. If <code-haskell>it1</code-haskell> finishes, <code-haskell>it1 . it0</code-haskell> finishes — The main feature here is that <code-haskell>it0</code-haskell> is restarted, as this is used for repetitive parsing. | ||
<pre-haskell> | <pre-haskell> | ||
arr0 f = Next $ \i -> Done (f i) | arr0 f = Next $ \i -> Done (f i) |
Revision as of 20:52, 4 March 2011
An enumerator is something that knows how to generate a list and an iteratee is something that does one step in processing another piece of the big list. E.g. to sum up all elements of a list, we do <pre-haskell> foldl (+) 0 xs </pre-haskell> Then <code-haskell>foldl</code-haskell> is the enumerator and <code-haskell>((+),0)</code-haskell> is the iteratee.
Clearly the function that sums the current element with the accumulator, <code-haskell>(+)</code-haskell>, doesn't know or care from which collection the elements are coming from. The initial seed, <code-haskell>0</code-haskell>, is again unaware of the collection. That achieves the separation of concerns: fold (aka, enumerator) has the intimate knowledge of the collection and how to get to the next element; iteratee knows what to do with the current element.
Definition
Do not rely on the <code-haskell>foldl</code-haskell> analogy too firmly, it is misleading. <code-haskell>((+),0)</code-haskell> is an F-algebra and <code-haskell>foldl (+) 0</code-haskell> is a catamorphism. But iteratee is different, it is an automaton. From this point of view, the enumerator sends elements of a list sequentially, from head to tail, as input messages to the iteratee. If the iteratee finishes, it outputs an accumulator. If the iteratee continues, it outputs nothing (i.e., <code-haskell>()</code-haskell>).
So, a set of states of iteratee is divided into subsets "Done" and "Next". Done-state means that automaton finished consuming a list, i.e., the automaton is dead. Next-state means that you can give an input message and obtain the same automaton in a new state. <pre-haskell> data Iteratee i o
= Done o | Next (i -> Iteratee i o)
</pre-haskell>
<code-haskell>i</code-haskell> is the type of the iteratee's input messages (or list elements) and <code-haskell>o</code-haskell> is a type of the output message (an accumulator). Precisely speaking, <code-haskell>Iteratee</code-haskell> stores not an automaton, but an automaton in some state, an automaton with distinguished state. As you see, if an <code-haskell>Iteratee</code-haskell> is in the <code-haskell>Next</code-haskell> state, then we have a function that takes an input message and returns a new <code-haskell>Iteratee</code-haskell>.
The distinct feature of iteratee is that it can say after which list element an iteratee finishes. An iteratee says this by sending "Done" to an enumerator. Then the enumerator can, for example, close a file or a socket (a stream) where a list of characters is read from. Lazy I/O, which uses lazy lists, closes a stream only when the stream is exhausted.
The drawback is that an enumerator can not tell an iteratee that an input is exhausted — an <code-haskell>Iteratee</code-haskell> consumes only infinite lists. You can remedy this by assuming <pre-haskell> i == Maybe i' </pre-haskell> where <code-haskell>i'</code-haskell> is a type of list elements. <code-haskell>Nothing</code-haskell> given to iteratee signals that the list is exhausted.
Here is a sample enumerator that takes input messages from a file: <pre-haskell> enumerator :: FilePath -> Iteratee (Maybe Char) o -> IO o enumerator file it = withFile file ReadMode
$ \h -> fix (\rc it -> case it of Done o -> return o Next f -> do eof <- hIsEOF h case eof of False -> do c <- hGetChar h rc (f (Just c)) True -> rc (f Nothing) ) it
</pre-haskell>
Functions
You can compose iteratees sequentially in time. This is done by <code-haskell>(>>)</code-haskell>. <code-haskell>it0 >> it1</code-haskell> means that when <code-haskell>it0</code-haskell> finishes, <code-haskell>it1</code-haskell> starts. Generally speaking, <code-haskell>Iteratee i</code-haskell> is a <code-haskell>Monad</code-haskell>, and it works exactly like a monadic parser. <pre-haskell> {- s = state -} instance Functor (Iteratee input) where
fmap f = fix $ \rc s -> case s of Done o -> Done (f o) Next g -> Next (rc . g)
instance Monad (Iteratee input) where
return = Done it0 >>= it1 = fix (\rc s -> case s of Done o -> it1 o Next g -> Next (rc . g) ) it0
</pre-haskell>
You can also compose iteratees sequentially in space. <code-haskell>it0</code-haskell>'s output messages become <code-haskell>it1</code-haskell>'s input messages, so <code-haskell>it0</code-haskell> and <code-haskell>it1</code-haskell> work in parallel. Their composition is denoted <code-haskell>it1 . it0</code-haskell>. If <code-haskell>it0</code-haskell> finishes, it is resurrected to its original state. If <code-haskell>it1</code-haskell> finishes, <code-haskell>it1 . it0</code-haskell> finishes — The main feature here is that <code-haskell>it0</code-haskell> is restarted, as this is used for repetitive parsing. <pre-haskell> arr0 f = Next $ \i -> Done (f i) instance Category Iteratee where
id = arr0 id it1 . it0 = fix (\rc1 it1 -> case it1 of Done c -> Done c Next f1 -> fix (\rc0 it0 -> case it0 of Done b -> rc1 (f1 b) Next f0 -> Next (rc0 . f0) ) it0 ) it1
</pre-haskell>
Generalization
You may note that <code-haskell>Iteratee</code-haskell> is final coalgebra. Other kinds of automata can be described with other F-coalgebras. In practice such automata can handle network protocols or interactive user input. See for example papers by Bart Jacobs for theoretical discussion.
See also
- Oleg Kiselyov: "Incremental multi-level input processing with left-fold enumerator - predictable, high-performance, safe, and elegant"
- Haskell-Cafe on understanding enumerator/iteratee
- Haskell-Cafe on Left fold enumerator - a real pearl overlooked?
- John Lato's cabalized package of Oleg's code
- Iteratee I/O