liveBook · Manning

Lazy

As explained in Type-Driven Development with Idris

Lazy is an Idris type constructor marking a computation whose result is evaluated only when required.

In Idris, `Lazy ty` marks a computation that produces a `ty` only when its result is needed. The type checker inserts `Delay` and `Force` applications implicitly, so `Lazy` can serve as an annotation for deferred evaluation. section 10.2.4 section 11.2.4

At runtime, `Lazy` and `Inf` behave the same way, but the totality checker treats them differently. `Lazy` lets a recursive value be generated on demand without making a function that consumes it non-total. section 11.2.4

Generating fuel on demand

Define `Fuel` with `More (Lazy Fuel)`, then define `forever = More forever`. A total `run` can consume available fuel; `forever` itself is deliberately non-total because it never terminates. section 11.2.3 section 11.2.4

data Fuel = Dry | More (Lazy Fuel)                   #1
forever : Fuel
forever = More forever                               #2

Shows the recursive Fuel definition and how `forever` supplies fuel indefinitely. section 11.2.4

How Type-Driven Development with Idris covers it

How deep it goes
Defines Lazy, Delay, and Force, and distinguishes Lazy from Inf by their treatment by the totality checker.
Examples
Uses recursively generated Fuel for indefinite processes and delayed recursive halves in total merge sort.
What you can do after
Apply it by marking computations for on-demand evaluation, allowing recursive Fuel to support indefinite processes while run remains total.
How it connects
Connects to Inf, Delay, Force, totality checking, and interactive IO processes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.