ECMA-262 in Lean

Downstream conformance proofs over the compiled SpecTec specification · lean/Engine/Claims.lean, lean/Ecma/Implements.lean

What is proved

SpecTec (spec/spectec/) defines ECMA-262's algorithms, grammars, model laws and host laws, and compiles downstream to Lean 4. There, Ecma.Implements states what it means for an execution engine to conform to the specification, and lean/Engine is proved to meet that definition: every run of the engine is a run of the specification's machine, or a stop at an implementation limit of its own. This page states the claim, then lists what is proved, trusted, owed, and outside it.

The definition: Ecma.Implements

An implementation (Implementation) provides concrete values (a Model), a Host, host algorithms beside the specification's, and its own transition between machine configurations. Its transition may execute many specification steps at once or compute a fast-path shortcut directly. It implements the specification when three conditions hold:

-- lean/Ecma/Implements.lean, in namespace Ecmastructure Implements (I : Implementation M) : Prop where  /-- its values are the specification's: the model's laws (§6, sec-ecmascript-data-types-and-values) -/  model : Model.Laws M ∧ Model.UnicodeLaws M ∧ Model.ParentLaws M  /-- its host meets every requirement the specification makes of a host (§4.2, sec-hosts-and-implementations) -/  host : Host.WF I.host  /-- each transition it takes from a configuration the specification's machine reaches is some transitions of  that machine, and where it ends, that machine's run ends so; or it gives up at a limit of its own (§2,  sec-conformance; §5.2, sec-algorithm-conventions) -/  step : ∀ chk c, Reached (I.spec chk) c →    match I.step chk c with    | .inl c' => Steps (I.spec chk) c c'    | .inr f => Runs (I.spec chk) c f ∨ f = .stop .implLimit
model

Its values obey all 148 SpecTec model laws: Model.Laws (116 core laws, joined with Model.NumLaws's 12 for Numbers and Model.ByteLaws's 9 for Data Blocks), the Unicode laws (Model.UnicodeLaws, 7), and the Parse Node parent laws (Model.ParentLaws, 4).

host

Its host obeys every SpecTec law the specification requires of a host (Host.WF, 72 laws across 10 topics).

step

Whether Asserts are checked or unchecked (chk): from any configuration the specification's machine reaches (Reached), a transition the implementation takes is zero or more transitions of the specification's machine; where the implementation finishes, the specification's machine finishes from there with the same result; or the implementation stops at a resource limit of its own (.stop .implLimit). It never answers otherwise than the specification.

c c′ I.step chk c = .inl c′ one transition of the implementation Steps (I.spec chk) c c′: the specification's machine, step by step reached by the specification
The step condition. Above, what the implementation does in one transition; below, the specification's machine with the same compiled SpecTec program and host, which reaches the same configuration in zero or more of its own transitions. Where the implementation finishes with a result, the specification's run from the same configuration finishes with that result.

The theorems, verbatim

The engine as an implementation, Engine.implementation o cells mc accel, bundles the concrete model Impl, its host under the options o over the cells cells, the host's own algorithms, and its transition with the store ceiling mc, with or without shortcuts (accel). Both theorems are in lean/Engine/Proofs/Model.lean and have no hypothesis beyond what they state.

The engine implements the specification

-- lean/Engine/Proofs/Model.lean, in namespace Enginetheorem implements (o : Engine.Host.HostOpts) (cells : Engine.Host.Cells) (mc : Option Nat) (accel : Bool) :    Implements (implementation o cells mc accel)

For every option set of the host, every allocation of its cells, every store ceiling, with or without the shortcuts, the engine is an implementation of the specification in the sense above.

Every run of the engine is the specification's

-- lean/Engine/Proofs/Model.lean, in namespace Enginetheorem run_sound (o : Engine.Host.HostOpts) (cells : Engine.Host.Cells) (mc : Option Nat) (accel : Bool)    (chk : Bool) (units : Thunk Engine.Machine.Units) (ob : Engine.Machine.Obs)    (s : State Impl) (k : Call Impl) (f : Final Impl) (hs : s.frames = [])    (h : Machine.runT (setupAt (hostBy o cells) mc accel chk) units ob (start s k) = some f) :    Runs (Ctx.spec (Spec.program hostAlgs) (hostBy o cells) chk) (start s k) f ∨ f = .stop .implLimit

If the engine's run loop (runT), started on a call in a state with no suspended execution context, returns a result f, then the specification's machine, running the compiled SpecTec program with the engine's host, has a run from the same configuration that ends with f; or f is a stop at an implementation limit. A companion, Engine.run_sound_reached, proves the same from any configuration the specification's machine reaches, covering jobs and runs after preludes.

The parser and cover laws, sound and complete

-- lean/Engine/Claims.lean, in namespace Engine.Claimstheorem parseLaws (cx : Grammar.Ctx) (strict : Bool) :    Model.ParseLaws Impl (Engine.Impl.grammarCtx cx Engine.Model.codePrims strict)
-- lean/Engine/Claims.lean, in namespace Engine.Claimstheorem coverLaws (cx : Grammar.Ctx) (strict : Bool) :    Model.CoverLaws Impl (Engine.Impl.grammarCtx cx Engine.Model.codePrims strict)

In lean/Engine/Claims.lean (proved in lean/Engine/Proofs/Parse/Laws.lean and Cover.lean): for every parser context and strictness flag, Model.parse is both sound and complete against GrammarCtx.ParseIs / GrammarCtx.Parses (Model.ParseLaws), and Model.cover is sound against GrammarCtx.CoverIs (Model.CoverLaws), alongside Engine.Claims.nodeLaws and Engine.Claims.parentLaws.

Proved, trusted, owed, outside

Proved What the theorems establish

  • The engine's values obey the SpecTec model laws and Unicode laws (Engine.Impl.laws, Engine.Impl.unicodeLaws).
  • The engine's host obeys all 72 SpecTec host laws under every option set (Engine.SpecHost.wfBy).
  • The code table the engine runs equals the compiled SpecTec program plus the host's own algorithms where the specification defines none (Engine.P'_eq).
  • Each transition of the engine, shortcuts included, matches the specification's at every reachable configuration, with 0 native_decide pins; the 71 checks on the generated program and parser tables are verified at run time (Engine.Machine.pinsOk; if any failed, Engine.setupAt falls back to Engine.Machine.plain).
  • The parser's soundness and completeness laws (Engine.Claims.parseLaws), the cover soundness law (Engine.Claims.coverLaws), and the laws of a Parse Node's parent (Engine.Claims.parentLaws) and of its children and descendants (Engine.Claims.nodeLaws), over Engine.Model.codePrims.
  • The set-ups covered: ecma-run by default and with --no-accel, under every --checked, --max-heap, every host option (--host-seed, --web-browser, --annex-b, --can-block, --resize-in-place), and with tracing, --sites and --cover.

Trusted Relied on, not proved

  • Lean's 3 standard axioms (propext, Classical.choice, Quot.sound), lean/SpecTec's emitter, and Lean's kernel and compiler.
  • The implemented_by overrides: the run loop's observers (traceOut, noteOut, noteStep, the identity to the logic), and the model's binary64 operations addHw, mulHw, divHw, and Parent's treeIndex, treeHeight, parentFast.
  • The driver between runs: realm initialization, preludes and the main program as separate runs, snapshots and --serve's caches. None of them mutates suspended execution contexts, so each run starts from a configuration Reached covers. That is inspected in the code, not proved.

Owed Stated, not proved

Outside Not covered by the claim

  • The step debugger's loop (Debugger.Machine.runW, a partial definition): ecma-dbg steps the same machine, but its interactive loop is not proved.
  • Optional mark-and-sweep store collection (ecma-run --gc via Engine.GC.runGC, a partial loop, and ecma-dbg's "gc" request): reclaiming unreachable cells in Engine.Model.ChunkedHeap while keeping live addresses monotonic is outside Engine.implements; default ecma-run runs without --gc.
  • Test results: the theorem says nothing about which test262 tests pass.

What the theorem does not say

  1. That SpecTec is the English. The correspondence between spec/spectec/ and spec.html is enforced by mechanical checks rather than a mathematical theorem: lean/SpecTec/Html (spectec-html check) splices all SpecTec grammars, algorithms, equations and early errors back into spec.html with zero byte diffs, spectec roundtrip and ecma-print (lean/Tools/SpecPrint) verify the SpecTec ↔ Lean round-trips byte for byte, and scripts/law_quotes.py verifies every law's quotation against spec.html. The resolved decisions in spec/DECISIONS.md and the SpecTec readings of prose laws are reviewed by humans.
  2. That every program gets an answer. The theorem governs runs that return from the engine's loop. A run may also stop at an implementation limit (the store ceiling or an exact power exceeding 230 bits), which the claim explicitly permits. Non-terminating runs produce no final value to compare.
  3. That every source text is decided within fixed limits. The parser's soundness and completeness laws (Engine.Claims.parseLaws, Engine.Claims.coverLaws) are proved where Model.parse decides, while fuel or nesting-depth limits leave the parse undecided (.stop .implLimit).
  4. That every host law matches its sentence word for word. Two host laws read their sentence more strictly than written (candidates for proposed normative changes) and two read theirs more weakly: see the host's laws.

The engine as an implementation

From lean/Engine/Claims.lean: the host's algorithms (hostAlgs: built-in functions, agent API hooks, HostLoadImportedModule, and entry-point drivers), none at an ID the specification defines; the host under options o; and the transition Engine.Machine.stepE: a stop at the store ceiling, else a verified shortcut where one applies (disabled when accel is false or if Engine.Machine.pinsOkThunk were false), else the specification's own step.

-- lean/Engine/Claims.lean, in namespace Enginedef implementation (o : Engine.Host.HostOpts) (cells : Engine.Host.Cells) (mc : Option Nat) (accel : Bool := true) :    Ecma.Implementation Impl where  extras := hostAlgs  host := hostBy o cells  step chk := Engine.Machine.stepE (setupAt (hostBy o cells) mc accel chk)