Skip to content
Part IA Michaelmas Term

Pipelines and Lazy Evaluation

Two kinds of programs

SequentialReactive
Accepts problem, processes, terminatesInteracts continuously with environment
E.g. numerical simulationE.g. aircraft control software
Most OCaml functions in this courseRequires 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.

Pipeline flow

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)