Skip to content

Go: certificates for the printer link ​

Status: accepted · Date: 2026-11-30

The Go translator is proved up to the printer: when the evaluator of the IR gives a value, the interpreter gives the same value. The printed Lean is text, so no theorem can state for every program that it computes what the evaluator gives. TypeScript closes that gap for one file at a time with a certificate, and step 5 of the shared core plan asks the same for Go.

The decision ​

A Go package gets a certificate the way a TypeScript file does: with "certify": true in its settings in tolean.json, or with the --certify flag of translate-go. The certificate goes next to the translation, and the translation's manifest records it. A first version had its own command, tolean certify-go, which needed the module and the namespaces of the translation again. It was dropped before it merged, because one command surface for every language is simpler to learn and to script, and the project file already knows the output and the namespace.

The certificate holds the declarations that the parser read, as a Lean term, and the IR that Pure.translateFuncs gives for them. The kernel runs the translator and checks the IR with decide +kernel. For each covered function, a link theorem states that the printed definition computes what IR/Eval.lean gives, and the go_link tactic proves it by unfolding the evaluator and splitting each if. The theorem about the interpreter then follows from translate_sound.

The manifest marks each theorem as proved, and the translator as proved when every function of the output has one. Chain.withCertificate takes parserProved := false for Go, so the manifest keeps the parser tested and makes it a link of each theorem, instead of claiming a proof of the parse.

The certificates cover functions whose parameters and results are int, int64, bool, string, a struct of the package, or a slice of one of these. A function that can panic is covered too: its result is .error of the message. The certificate lists each other function with the reason, and the translator refuses to certify a package where no function is covered. The functions are picked automatically, so a user names none.

A theorem states its parameters and result as the interpreter's values, so each printed type needs an encoder to Eval.Val. A struct's encoder is a definition in the certificate: enc.Point p is .strct of each field of p, in the order that the type declares them, as the interpreter builds a struct. A slice maps the encoder of its elements, so []Point is .slice (ps.map enc.Point). A function with several results prints a tuple A × B × C, and the interpreter returns pair a (pair b c), so the encoder of t is Val.pair (encA t.1) (Val.pair (encB t.2.1) (encC t.2.2)). A slice of slices nests the encoder, so [][]int is .slice (xss.map fun xs => .slice (xs.map .int)). For xss[i][j], the output evaluates xss[i] before it panics on j, also when i is out of range, so go_link has a lemma that such an element is still a slice (getD_map_slice), and go_and splits the equations of a matched list. The encoders live in the certificate, not in the library, because the printed structures belong to the translation. The go_link tactic gets one lemma for slices: sliceAt_eq writes xs[i] with the same test of the index as Go.sliceFault, so one split decides the index on both sides.

Every link proof runs within Lean's default limit of 200000 heartbeats. simp needs the equations of the match in ExprExt.apply, and Lean builds them the first time it uses them. That took more than the limit on its own, and each certificate built them again in every proof. So Certify/Tactic.lean builds them once, and every certificate imports them. For xss[i][j], dite_slice moves the test of i inside Val.slice. Then the match of the next operation sees a slice and reduces, and the proof does not carry the whole match into each branch.

Three levels, as in g[i][j][k], still exceed the heartbeat limit, even at 1000000, because the link proof unfolds and splits every index again. The certificate leaves out such a function until link lemmas for one index each let the proof compose them (#406). The same holds when one index takes its position or its slice from another, through a local, a call, a branch, a loop or a write, as in j := g[i]; k := g[j]; g[k] on a []int.

Rules that followed values through the function missed one such form after another, so the rule (deepIndex) is an allowlist that reads only the syntax. A function that indexes has a theorem only if each slice that it indexes is a parameter, a local list literal of constants and parameters that hold no slice, or an index of one of them, none of them bound again, and each position is built from int parameters that keep their value, constants, counters of loops over constants or up to such a bound, and len of such a slice. In addition, no parameter, result, literal or loop of the function may have a type that takes three indexes, counting a string as one, such as [][][]int or [][]string. This leaves out indexes at a loop element of another slice, as in xs[i] for each i of is, and indexes of a local slice written through, as in xs[i] = v, whose certificates compiled before.

A callee may index at an argument, so a call passes on the chain of indexes that its arguments carry. The rule for arguments needs no dataflow either: it holds only for a callee that may index, and each argument of one is a constant, a parameter or counter that keeps its value, len of an allowed slice, or arithmetic on them. A callee is free of indexes (indexFree) when the package defines it before the caller, and neither it nor a function that it calls indexes or slices. An imported or recursive callee and a method count as callees that index, so methods.Distance, which calls p.Add(…) on a local, loses its theorem too. Restoring these functions is #425.

Loops ​

A loop gets a lemma of its own, as in TypeScript's certificates. The evaluator runs a loop with runFold, which binds each element and then copies the changed locals back with bindState. The printed definition runs List.foldl over Lean values. So the lemma for a loop states that, in every environment where the locals have values of their types, the evaluator gives the environment with the locals set to the result of the printed fold.

The lemma reads the starting state and the other locals that the body reads back from the environment, with decoders such as decInt and decSlice decInt. exec_fold in Certify/Loop.lean proves the lemma from two facts about one step: the body ends normally, and the locals it leaves are the encoded result of the printed step. go_link proves that step for an arbitrary environment, so it needs lemmas about Env.set on an unknown list, such as lookup_set_ne.

The link theorem then uses the lemma as a rewrite rule. This works only if simp reaches the loop with a concrete environment, so it must not unfold the loop first. go_loop_link therefore unfolds every equation of Body.exec except the loop's. A certificate with loops also raises maxRecDepth, because a loop over a long literal list, such as 0 to 255, makes the IR deep.

A loop that can end early or panic uses the same plan with a different printed form. A break makes the output print forIn, whose step gives ForInStep.done at a break and ForInStep.yield otherwise. A body that can panic runs in Except String, and the output prints List.foldlM, or forIn when the body can also break. exec_forIn, exec_foldM and exec_forInM prove the lemmas for these forms. The evaluator stops a loop at a panic and gives the panic as the result of the whole loop. So the lemma states the rest of the function with afterFold, which goes on with the final state or panics with the message.

The condition on one step is a definition, ForInRan, and not a match in the lemma. simp rewrites it once the step is a constructor, after go_link has split each if of the step. When go_link split the match itself, it left equations such as .yield s = .yield t that the proof did not close.

A loop whose body calls a function needs that function's body. So its lemma is about the functions of ir, not every list of functions, and it holds when the fuel covers the deepest chain of calls. The lemma takes fs with the hypothesis fs = functions ir, so that simp can still rewrite with it after it has unfolded ir in the link theorem. The proof unfolds the callee and its own callees, as a link theorem does. A callee with a loop runs through that loop's lemma, as in a link theorem. The locals of the outer loop are first made variables with generalize: decAt has to unfold for the callee's locals, and a hypothesis ρ.lookup n = some (enc (decAt dec ρ n)) would then rewrite its own right-hand side without end.

A loop can also read a local that the function bound before it, such as a slice literal. The loop does not record the types of these locals, so the certificate reads them off the lets in front of the loop: a literal, a list, a local, or an integer operation.

A return inside a loop is not on the proved path of the Go translator. So no certificate needs a lemma for it yet.

What stays open ​

  • The parser. The Go parser is a partial def, so the kernel cannot run it. A certificate states its theorems about the declarations that the parser gave, and the test against go/parser remains the evidence that they are what the source says. TypeScript's certificates check the parser too, because its parser is total. Making the Go parser total would close this link, and it is a separate change.
  • Nested loops. The proved path refuses a loop that holds another loop, so no certificate meets one. A return in a loop will need a lemma once the proved path accepts it.

A loop over a struct local or struct elements reads the struct back from the environment with a decoder that the certificate defines, such as dec.Point, next to the encoder. The simp lemma dec.Point (enc.Point v) = v lets the proofs use it as they use decInt. The link theorem unfolds the decoder, because the loop starts from a struct value that the function built.

The encoders and decoders of structs live in the namespaces enc and dec of the certificate, and it names the library's decoders and loop helpers in full, such as tolean.Go.Certify.decStr. A struct named Str would otherwise define a decStr that hides the library's.

Go.byteLen is irreducible. While simp rewrote a loop over a list literal inside a branch, a unification encoded a literal string and exhausted the recursion depth (#416).

A function that uses package state needs no shape of its own either. The interpreter and the printed Lean both pass the package's variables as parameters before the others, and return their new values after the results, so the theorem has the form of one for a function. The kernel has to run the translator on such a package for translated, and it could not while Stmt.declares, Stmt.callsOK, Stmt.thread and Stmt.withState used well-founded recursion. They now recurse structurally, in mutual blocks with helpers for lists of statements and clauses, as Stmt.idents does, and compute the same values.

A method needs no shape of its own. The proved path gives the method M on T the name T.M with the receiver as its first parameter, in the interpreter, in the IR and in the printed Lean. So the theorem for T.M has the same form as one for a function, and its proof is the same.

This page is docs/decisions/2026-11-30-go-certificates.md at commit 5f1d031.