Corecursion

Lazy stream visualizer

Ask for a finite prefix of an infinite stream. Select any element to see the state that produced it and the state passed to the next step.

Request elements

Stream A


Stream B


Element trace

What is corecursion?

A corecursive definition produces a result from a current state and describes how to produce the rest. A lazy stream can be infinite because a consumer only asks for a finite number of elements. Try Take prefix with 3, then 8, then 3 again. The visualizer remembers the elements it already generated and reuses them.

In the first recipe, the pair (0, 1) emits 0 and moves to (1, 1). Each step emits the first number and advances to the pair made of the second number and the sum. In the linear recipe, iterate emits a seed and repeatedly applies the given rule. cycle wraps around a nonempty finite list.

The displayed definition is Haskell. The visualizer evaluates the built-in recipes using exact integers. See the GHC Prelude documentation for iterate and cycle.