Skip to content

The Tolean IR ​

Status: accepted · Date: 2026-10-30, updated 2026-11-03

The Tolean intermediate representation, or Tolean IR, is the form a program takes after Tolean translates it and before it becomes Lean text. It is a small language of Lean code, written as Lean data: types, expressions, bodies and declarations. Three parts of Tolean use it. The translator builds it, the printer writes it as Lean text, and an evaluator gives it a meaning.

source ──parser──▶ syntax tree ──translator──▶ Tolean IR ──printer──▶ Lean text
                        │                          │
                  interpreter                  evaluator
                        └────── program_sound ─────┘

Because the IR has a meaning of its own, a theorem can relate it to the source. program_sound states that for every program that the translator accepts, the IR computes only what the interpreter computes for the source. A certificate then proves, for one file, that the printed Lean computes what the IR computes.

Today only TypeScript builds the IR. Its shared core is in tolean/IR.lean, and TypeScript's extension is in tolean/TypeScript/IR.lean. This record explains four choices about it.

1. The IR is our own type, not Lean.Expr ​

Lean already has a type for its terms, Lean.Expr, so the translator could build those instead of a type of its own. We chose not to, for four reasons.

Lean.Expr has no meaning that a theorem can use. program_sound needs a function, written in Lean, that says what each IR term computes. Lean cannot define such a function for its own terms in general. A term's meaning depends on the constants of an environment, and a logic cannot define the meaning of all of its own terms inside itself. With our own type, the evaluator in IR/Eval.lean is an ordinary total function, and the proofs unfold it.

Lean.Expr is the form after elaboration. It has de Bruijn indices, universe levels, implicit arguments and instances that elaboration has already filled in. To build it, the translator would have to do the elaborator's work itself, such as finding each type class instance. The Tolean IR stays at the level of the code that a person reads, and Lean's elaborator does that work when it compiles the printed text.

Lean.Expr can say anything. A proof about the translator has a case for each form that the IR has. The Tolean IR has only the forms that the translator writes, so the proofs stay as small as the translation.

The kernel runs our own type directly. A certificate makes the kernel run the translator and compare its IR with the one that the certificate holds. That needs an equality that the kernel can decide, which IR/Equality.lean proves for the IR. It needs nothing from Lean's environment.

2. Shared primitives, with constructs for each language ​

Most of the IR is not about TypeScript. Integers, booleans, strings, Option, List, structures, let, if, a match on Option, a fold over a list and function declarations are the same for Go. What belongs to one language is small. For TypeScript, it is JavaScript numbers: the number type N, its operators, its constants such as NaN, and null.

So the IR becomes a shared core with one extension point for each layer, and each language adds its own constructs there. The core also reuses Lean's own types for values, such as Int, String, List and Option, as the IR does now. The first step of the shared core plan moves the core to tolean/IR/ and makes JavaScript numbers the first extension. Go's translator then builds the same IR, with its own extension for int64.

3. A typing relation first, and a typed IR only if the proofs need it ​

The IR has types, but it does not enforce them. Ty lists the types, each function states the types of its parameters and its result, and list and structure literals state their own types. But an Expr is not indexed by its type, and the evaluator is untyped: Expr.eval gives an Option (Val N), which is a JavaScript value or nothing.

That leaves a gap. program_sound goes one way: when the IR gives a value, the interpreter gives the same value. An IR that never gave a value would meet it too. A certificate rules that out for its file, because its theorem states that the interpreter gives some value for every input. For a file without a certificate, only the tests rule it out.

So the next step is a typing relation, WellTyped, on the IR as it is, with two theorems. The first states that the translator gives well-typed IR. The second states that well-typed IR gives a value of its type. The subset has no recursion, and each loop is a fold over a list, so every call finishes. The depth of nested calls is at most the number of functions, so that much fuel is enough. With both theorems, program_sound says something for every program, not only for a certified file. The IR, its equality, the translator and the certificates stay as they are.

A typed IR, in which an expression's type is part of its type in Lean, as in Expr : Ctx → Ty → Type, would give the same theorems by construction. It costs more:

  • Equality in the kernel. To compare two nodes, the kernel must first compare their hidden types and then cast one to the other. The kernel can reduce such a cast when the types are known, but every node carries its context and its type. The IR terms in a certificate grow, and Lean takes longer to elaborate them.
  • The translator. It must give a pair of a type and an expression of that type, and check and cast types as it goes. Each certificate runs it in the kernel, so each run does more work. Proofs about it meet the casts too.
  • Structures. The meaning of a structure type must be a Lean type, but the printed code declares the structures. So a structure would mean a generic record, and an isomorphism with the printed structure would remain. A recursive interface would also break a meaning defined by recursion on Ty.
  • Names. A typed IR wants de Bruijn indices, but the printer needs the names of the source, so the context would carry the names too.

What it buys beyond the relation is simpler link theorems. The evaluator would give Lean values instead of Val, so the encoders and most of the work of link_tac would go. We will build it only if the link proofs become the bottleneck.

4. The parts of Lean that the IR uses ​

Each part below was tried on Lean 4.30.0. This section decides what the IR uses now and what it cannot use. A part marked planned has a plan that gives its steps, and a candidate has no plan yet.

Part of LeanUseWhy
Int, Bool, String, List, Option and structureUsed nowThey are the IR's values and the printed code's types. A Lean String holds Unicode characters, and a JavaScript string holds UTF-16 code units. They agree while the IR has only string literals and equality, and while the lexer refuses lone surrogates. They part once the subset allows .length or indexing.
EStateMUsed nowThe monads of the translator, the parser and the lexer. tolean/Core/EStateM.lean proves their lemmas once.
Int64 and BitVec 64Planned, for GoGo writes its own wrap64 on Int today. Int64 wraps in the same way, and it converts to BitVec 64, which has Lean's lemmas and bv_omega. The shared core plan gives Go the IR, with int64 as its extension.
FloatFor running onlyLean has no theorems about Float, so proofs keep the abstract JSNumber N. But instance : JSNumber Float in Interpret.lean lets the printed Lean run on real doubles, which gives a cheap test against Node for a file without a certificate.
deriving ToExprCandidateIt derives for every type of the IR, with one deriving instance line for each type. A certificate could then be a command, such as #certify "sensor/raise.ts", that runs the parser and the translator when Lean elaborates the file and inserts the IR as a term. The kernel would still check every step. Today tolean certify prints the IR as text through Repr.
deriving ToJsonCandidateIt derives for every type of the IR in the same way, so the IR can be written out for debugging, and for the playground to show.
partial_fixpointCandidate, for whileIt defines a loop that may not end in the Option monad, without fuel, and Lean proves its equation. It plays the part of the div result of Aeneas.
ExceptUsed nowA translated function that can throw returns Except JSError R, and the printed code passes an error on with >>=.
StateT, ExceptT and EStateMPlanned, for effectsStd.Do reasons about these monads, so effectful generated code uses them. The shared core plan says more.
Std.Do and mvcgenWatchedmvcgen proves a for … in loop with let mut from an invariant, with no axioms beyond the usual ones. But Lean 4.30 calls it experimental and advises against it in production. Once it is stable, the printer can write loops as for … in instead of List.foldl.
Std.Format and Lean.SyntaxPlanned, for the printerLean's layout engine, and Lean's own syntax trees and formatter, could write the printed text instead of our printer. The printing plan gives the steps and what each needs on Lean 4.30.
deriving DecidableEqNot possibleIt still fails for a nested inductive, such as Expr, which holds a List Expr. So IR/Equality.lean stays.
bv_decide and native_decideNot usedEach adds an axiom that trusts compiled code, so a proof that uses it fails tolean chain check.
partial defNot usedLean cannot unfold it, so neither the kernel nor a proof can use what it computes.
Std.Internal.ParsecOnly through Lean.JsonLean.Json uses it to read tolean.json and the chain manifests, and the README lists it among the parts of Lean that Tolean trusts. The TypeScript parser cannot build on it. Its repetition, manyCore, is a partial def. It reads a String by byte positions, which the kernel reduces slowly. And it is internal to Lean, with no promise that it stays the same.
Lean.IRNot usedIt is the IR of Lean's compiler, for making C code, and it changes between versions.

This page is docs/decisions/2026-10-30-tolean-ir.md at commit 5f1d031.