0017. Fuel counts calls and loop-head entries
- Status: Accepted
- Date: 2026-10-30
- Plan: M3.6, M9, M10.5
- Review: Compiler and backend
- Amendment: sandbox heap/output limits and pure entry are specified by ADR-0079; this record's fuel unit is unchanged.
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
- Charge every statement or IR node — finer, but couples the count to lowering details.
- Charge calls only — loops without calls would never stop.
- A distinct exit code — a second trap rule for callers; the kind text already separates them.
- Wall-clock timeouts — not deterministic.
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.