liveBook · Manning

Fuel

As explained in Type-Driven Development with Idris

An Idris data type representing a supply of computation steps used to bound recursive execution.

Fuel makes an execution allowance explicit: `More` carries the remaining allowance, and `Dry` marks exhaustion. A runner consumes fuel as it recurses, letting the totality checker verify that execution is bounded. section 11.2.3

data Fuel = Dry | More Fuel                             #1
tank : Nat -> Fuel                                      #2
tank Z = Dry
tank (S k) = More (tank k)
run : Fuel -> InfIO -> IO ()
run (More fuel) (Do c f) = do res <- c
                              run fuel (f res)          #3
run Dry p = putStrLn "Out of fuel"                      #4

See the `Fuel` definition, the function that builds a finite supply, and a runner that executes until fuel runs out. section 11.2.3

With finite fuel, an interactive process stops at its limit; repeated output ends with “Out of fuel.” This keeps the runner total, but means execution is no longer indefinite. section 11.2.3

To allow indefinite execution, define `Fuel` with `More (Lazy Fuel)` and generate it using `forever`. The runner can remain total; `forever` is deliberately partial. `Lazy` delays evaluation so the totality checker accepts the recursive fuel structure. section 11.2.4

Fuel also bounds repeated invalid-input handling in a Hangman game and looping process execution. In both cases, the runner consumes fuel to ensure recursive execution has an explicit limit. section 14.3.5 section 15.2.2

How Type-Driven Development with Idris covers it

How deep it goes
Defines finite and lazy fuel, demonstrates exhaustion and total runners, and identifies deliberate nontermination in `forever`.
Examples
Uses finite repeated output, invalid Hangman guesses, and looping processes to demonstrate bounded execution.
What you can do after
Apply it by passing finite fuel to cap execution or lazy-backed fuel to permit indefinite running.
How it connects
Connects to totality checking, recursive processes, `Lazy`, and partial functions.

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.