On this page

0017. Fuel counts calls and loop-head entries

Context

Generators and hosts need a deterministic way to stop a program that does not halt (ADR-0004, ADR-0005). Wall-clock limits are not reproducible. The reference interpreter (ADR-0003) is the place to build the mechanism; native code stays free of it.

Decision

The interpreter takes an optional fuel limit, harness interp --fuel N. One unit is charged on every function entry (including main) and on every loop-head entry: the first iteration, each continue, and the final failing test. A while with N iterations costs N+1; hello costs 1.

Before each charge, if no fuel remains, the program stops. --fuel N therefore allows exactly N charges, and --fuel 0 stops at main. Work between charges is bounded by program size, so every program halts.

Exhaustion is a third trap kind, fuel exhausted. It prints trap: fuel exhausted at <file>:<line>:<col> on stderr after flushing stdout and exits 101, extending ADR-0012's list of trap kinds. The location is the function name for an entry and the while or for keyword for a loop (M5.4 amendment, ADR-0040). A for head includes its final failing test; generated indexing and increment add no charges. Callers tell kinds apart by the kind text, and in-process callers by Outcome.

Fuel is interpreter-only policy. Native code has no counter and normal programs cannot observe it. It is not a diagnostic and has no code in the registry.

Alternatives considered

Consequences

Charges are pinned to source-level constructs. Future IR passes that duplicate, remove or unroll loops or calls must not change the count seen by the interpreter, or must be disabled for it. A test pins an exact count to catch drift. M9 hooks and M10.5 policies build on this unit.