ECMA-262 in Lean
1 / 10
← → step · Esc exit

Executing spec.html

Formalization's time has come

James M Snell · Dan Carney  ·  TC39 #117

Architecture

The pipeline

spec.html + proposal changes algorithms · grammar prose readings checked generate.py AST · types · grammar bijection check ecma-print = spec.html lean/Ecma Generated algorithms & tables Machine.step Model.Laws · Host.WF quoted sentences checked 0 tactics · Gen/ gitignored lean/Engine Engine.implements proved · runs test262 Debugger Wasm64 + Native lazy-loaded per proposal
Why now?

LLMs!

Robots at mechanical typewriters typing Lean 4 proofs alongside spec.html
Immediate value

Tools for champions, reviewers & editors — usable right now

Static & Structural

Mechanical merge

Merges a proposal into spec.html · checks grammar SDOs, slots & types

Caught across #117
  • Missing BoundNames row on import defer
  • *undefined* vs. ~empty~ / ~unused~ in Immutable ArrayBuffers
  • Missing [[DeferredNamespace]] in ParseModule & JSON modules
  • Stale clause IDs, untyped built-ins & dangling intrinsics
Interactive Wasm64

Spec debugger

Step forward & back through spec.html · inspect Slots & Realm

Examples
Adding a proposal · Step 1

Write the algorithms

spec.html §22.1.3.32 String.prototype.trim — proposal-trim-chars
<emu-clause id="sec-string.prototype.trim" type="built-in function">
  <h1>String.prototype.trim ( <ins>[ _characters_ ]</ins> )</h1>
  <emu-alg>
    1. Let _thisValue_ be the *this* value.
    1. Return ? TrimString(_thisValue_, ~start+end~<ins>, _characters_</ins>).
  </emu-alg>
</emu-clause>
Adding a proposal · Step 2

Generate the spec

lean/Ecma/Gen/S22_1_part2.lean Before (ECMA-262)
def «String.prototype.trim» : Alg := {
  id     := ⟨1454, 0⟩
  name   := "String.prototype.trim"
  header := { kind := .builtin, params := [], ret := none, recv := none, desc := [] }
  vars   := #["this value", "NewTarget", "thisValue"]
  body   := [
    (.mk [] (.let_ 2 (.thisValue false))),
    (.mk [] (.ret (some (.q (.call (.alg ⟨1455, 0⟩) [(.var 2), (.enum 88)])))))]
}
lean/Ecma/Gen/S22_1_part2.lean After generate.py · 0 proof lines needed
def «String.prototype.trim» : Alg := {
  id     := ⟨1454, 0⟩
  name   := "String.prototype.trim"
  header := { kind := .builtin, params := [{ x := 2, ty := none, optional := true, rest := false }], … }
  vars   := #["this value", "NewTarget", "characters", "thisValue"]
  body   := [
    (.mk [] (.let_ 3 (.thisValue false))),
    (.mk [] (.ret (some (.q (.call (.alg ⟨1455, 0⟩) [(.var 3), (.enum 88), (.var 2)])))))]
}
Adding a proposal · Step 3

Debug!

proposal_trim_chars.js ● Breakpoint: sec-string.prototype.trim · step 2
const path = "/__ecma-262__/".trim("/_");
// → "ecma-262"
Adding a proposal · Step 4

All 14 ECMA-262 proposals on the TC39 #117 agenda

StageProposal (TC39 #117)spec.htmlGenerated LeanModel / HostProofs
3import defer (with PR #97 cycle/TLA fix)+723 / −109+283 / −12200Debug →
3Import Text (for Stage 4)+39 / −4+26 / −15+40Debug →
3Thenable Curtailment (PRs #22 & #26)+157 / −23+80 / −25+2 / −1+1 / −1Debug →
2.7Deferred Re-exports (export defer)+2,154 / −186+1,330 / −28900Debug →
2.7JSON.parse Options (PR #27 nullPrototypeObjects)+96 / −7+71 / −3200Debug →
2.7ESM Phase Imports (PR #65)+1,083 / −89+430 / −258+11 / −4+1 / −1Debug →
2.7Immutable ArrayBuffers (for Stage 3)+366 / −206+223 / −15100Debug →
2Error code property (to Stage 2.7)+32 / −4+24 / −1300Debug →
2Math.fusedMultiplyAdd (to Stage 2.7)+63+23 / −100Debug →
2Composites+268 / −2+114 / −300Debug →
1Cancellation: AbortController & AbortSignal (PR #35)+790 / −14+281 / −33+4 / −1+2 / −1Debug →
0Iterator.prototype.transfer+83+40 / −1800Debug →
0Trim Specific Characters+21 / −15+12 / −1200Debug →
0Bitwise counting (Math.popcount32 / Math.ctz32)+23+23 / −100Debug →
Where this goes

What this can deliver

  • Invariants Module graph [[Status]] monotonicity · §6.1.7.3 essential object invariants · immutable ArrayBuffer bytes never mutate
  • Soundness (eventually) Every engine fast path or shortcut produces the exact same observable result as stepping spec.html line by line (Engine.implements)
  • Proofs about JS libraries (eventually) Tractable where intrinsics are frozen (SES / hardened realms), primordials are captured at module init, or values are immutable (Composite)

Questions?