Part IA Michaelmas Term
Pipelines and Lazy Evaluation
Two kinds of programs
| Sequential | Reactive |
|---|---|
| Accepts problem, processes, terminates | Interacts continuously with environment |
| E.g. numerical simulation | E.g. aircraft control software |
| Most OCaml functions in this course | Requires concurrency (beyond Part IA scope) |
Producer → Filter → Consumer pipeline
We can model a simple pipeline without full concurrency. The model is:
Producer → Filter → ... → Filter → Consumer
- The Producer outputs items as a stream.
- Filters transform the stream (perhaps consuming several items to produce one output).
- The Consumer takes elements as needed.
The Consumer drives the pipeline: nothing is computed except in response to demand for the next datum. Execution of filter stages is interleaved as needed. The programmer sets up data dependencies but has no clear idea of what happens when - creating an illusion of concurrency.
Unix analogy
Unix pipes (|) link processes together similarly. Data flows from one process to the next, with the consumer pulling data through the chain. In OCaml, lazy lists provide the same effect within a single program.
Why this matters
- Avoids computing values that are never consumed
- Handles potentially infinite data sources naturally
- Separates generation, transformation, and consumption concerns cleanly
- Particularly useful when a problem has many solutions but only a few are needed (e.g. making change, search problems)