ECMA-262 in Lean

ECMA-262 formalized in SpecTec, bijective with spec.html, compiled and proved in Lean

How it works

The specification lives in SpecTec (spec/spectec/) in two parts: structured SpecTec for ECMA-262's algorithms, grammars, early errors and tables (bijective with spec.html), and prose SpecTec modules for its abstract sorts, grammar and §29 memory relations, and the 246 model, parser, memory-model and host laws that quote the English text. Everything else — the Lean specification semantics, the proved reference engine, and the WebAssembly debugger — is downstream of SpecTec.

The whole

spec/spectec/: source of truth (62 files) spec.html tc39/ecma262 at b1eaed82f + spec/html/ meta spectec-html 0-diff splice DECISIONS.md 57 resolved errata & choices no runtime switches Structured SpecTec 36 files (55,936 lines) def @sec-… · grammar · sdo early_error · field · enum · table spectec roundtrip + ecma-print Prose SpecTec (prose/) 26 modules (4,304 lines) syntax · relation · laws · def Model (148) · Parser (7) · §29 (19) · Host (72) every law quotes spec.html spectec emit Downstream spec compiled to Lean 4 Lower + Machine.step over any Model & Host Ecma.Implements Gen/ gitignored The engine: lean/Engine concrete model & host, proved parser, verified shortcuts Engine.implements · run_sound · parseLaws · coverLaws 0 native_decide pins · 3 standard axioms · 71 runtime checks implements The debugger: ecma-dbg Wasm + native, step by step test262 TC39's suite, run on ecma-run
spec.html and structured SpecTec are checked against each other by spectec-html with zero byte diffs; prose SpecTec states the abstract sorts, relations and quoted laws. lean/SpecTec (spectec) compiles SpecTec into Lean, where the concrete engine is proved to implement the specification and drives both the debugger and test262.

The specification in SpecTec

SpecTec (spec/spectec/, 62 files) is the human-readable formal representation of ECMA-262 in this repository, expressing the specification directly in typed declarations:

Bidirectional bijections: spec.html ↔ SpecTec ↔ Lean

The specification's source, spec.html, is pinned at a revision of tc39/ecma262 (with any active proposals from spec/proposals.txt merged by scripts/spec_text.py (Applying a proposal shows one applied)). Three automated round-trips keep spec.html, SpecTec, and the downstream Lean representation locked together:

  1. spec.html ↔ SpecTec (spectec-html check, lean/SpecTec/Html): Using four small presentation tables in spec/html/ (English call templates, grammar attributes, clause headers, and step patches), spectec-html renders every SpecTec grammar block, algorithm, equation and early-error rule back into ecmarkup HTML and splices them into spec.html with zero byte diffs.
  2. SpecTec ↔ SpecTec (spectec roundtrip): Every .spectec file — structured and prose alike — is parsed into SpecTec's AST and pretty-printed back byte for byte.
  3. SpecTec ↔ Lean (spectec emit & ecma-print, lean/Tools/SpecPrint): lean/SpecTec compiles spec/spectec/ into gitignored Lean modules, and ecma-print prints all 36 structured .spectec files back from those compiled definitions and verifies them byte for byte.

Symbolic names and sorted declarations

In SpecTec, everything is written by name (@sec-iscallable, [[Call]], ~start+end~, |IdentifierName|). When lean/SpecTec emits Lean code for the machine and proofs, it collects all declared and referenced names across each namespace in spec/spectec/, sorts them lexicographically (0..N-1), and generates symbolic constants in Ecma.Known. Because downstream Lean code references symbolic names rather than raw numbers, editing a clause or applying a proposal stays local to the changed SpecTec definitions.

Prose concepts in SpecTec: syntax, relation and laws

Algorithms and grammars are only half of ECMA-262: §5.1, §6, §9, §21 and §29 state in English prose what values are, how grammars derive parse trees, how shared memory behaves, and what a host environment may do. Those prose sections are formalized in 26 SpecTec modules under spec/spectec/prose/ (4,304 lines) using four declarations:

Specification decisions and errata

Where ECMA-262 as written is contradictory, incomplete, or disagrees with every engine and test262, SpecTec resolves each ambiguity directly in spec/spectec/ with no command-line errata switches. Every such choice is documented in spec/DECISIONS.md, including 57 active specification errata and proposed normative changes (54 defects of the text and 3 proposed normative changes): 33 bake in the reading test262 requires, 13 bake in a proposed fix, and 11 keep the literal wording so that reaching a broken Assert: stops at the exact offending step. Spec issues & errata quotes each entry's sentences and alternative readings, tracks upstream issues and PRs, links to its long-form write-up, and verifies a demonstration program where one applies.

Downstream: the small-step machine and proved engine

Everything under lean/ is pure Lean 4, consuming the compiled SpecTec specification:

One clause, end to end

§7.2.3 IsCallable is three steps in spec.html:

§7.2.3 IsCallable ( arg )
  1. If arg is not an Object, return false.
  2. If arg has a [[Call]] internal method, return true.
  3. Return false.

In SpecTec. Clause §7.2.3 lives in spec/spectec/clauses/S07.spectec as a five-line def:

--- spec/spectec/clauses/S07.spectecdef @sec-iscallable "IsCallable" (kind ao params [arg : #"ECMAScript language value"] -> Boolean) vars [arg] {  if arg is not ty {Object} -> return false  if arg sat $HasMethod([[Call]]) -> return true  return false}

Every part of the clause is explicit and readable by name: @sec-iscallable identifies the clause (mapped to sorted ClauseId 1364 when emitting Lean), kind ao marks an abstract operation taking arg of sort #"ECMAScript language value" and returning Boolean, step 1 tests arg is not ty {Object}, and step 2 checks arg sat $HasMethod([[Call]]) before step 3 returns false. Running spectec-html renders those three steps back into spec.html's exact ecmarkup.

A step of the machine. Downstream, Lower compiles the body with its step numbers preserved. When EvaluateCall reaches IsCallable(func), the small-step machine pushes a frame for @sec-iscallable with arg bound to the callee; Ecma.Machine.step checks whether the callee is an Object in the store, inspects its [[Call]] internal method at step 2, and returns true or advances to step 3.

In the debugger. The example tests/run/iscallable.js calls both a function and an ordinary object: the debugger pauses at IsCallable step 2 for the function and step 3 for the object, displaying IsCallable above EvaluateCall in the spec stack alongside the value of arg.

Sizes

PartFilesLinesRole
spec/spectec/ (structured)3655,936SpecTec declarations, grammars, early errors, tables & clause algorithms (bijective with spec.html)
spec/spectec/prose/264,304SpecTec prose modules: Model, Host, derivations, §29 memory & 246 quoted laws
spec/html/*.txt419,907ecmarkup presentation templates & metadata for spectec-html
lean/Ecma (hand-written)709,570downstream small-step machine, lowering & Implements contract (Gen/ is gitignored)
lean/Engine (outside proofs)6312,291concrete VM model, parser, host & shortcuts
lean/Engine/Proofs8557,206conformance & parser proofs (Engine.implements, Engine.run_sound, Engine.Claims.parseLaws, Engine.Claims.coverLaws)
lean/Debugger104,165step debugger (ecma-dbg, native & Wasm)
lean/SpecTec2115,245standalone SpecTec compiler & spec.html bijection (spectec, spectec-html)
lean/Tools2012,457standalone tools (ecma-print, ecma-typecheck, test262-runner, wasm-emit)

Lines are counted in this commit's tracked sources. Gitignored outputs (lean/Ecma/Gen: 108 modules, 42,287 lines; lean/Ecma/Grammar/Gen: 16 modules, 6,391 lines) are generated from spec/spectec/ by scripts/generate.py. Not counted: Unicode Character Database tables.