Skip to content

Go structs on the proved path ​

Status: accepted · Date: 2026-11-26

A pure Go function may now build a struct with a composite literal, read its fields and assign one field of a local struct. This record says which forms the proved path accepts, how the output writes them, and why the other forms stay on the string translation.

The decision ​

  • The interpreter in tolean/Go/Spec/Eval.lean gives Program the struct types that the package declares, with each field's name and type, and adds a struct value: its fields in declaration order. The Go specification defines these in "Struct types", "Composite literals" and "Selectors".
  • A struct type joins the subset when it has no embedded field, its field names are distinct, and every field has one of the types int, int64, bool or string. A function that uses any other struct stays on the string translation.
  • A literal joins the subset in three forms: S{}, which gives each field its zero value; S{a, b}, which gives every field in order; and S{X: a, Y: b}, which names every field once, in declaration order. Go evaluates the elements left to right, so the output does too, and a division by zero in an element panics before the struct exists.
  • x.f reads a field of any expression of a struct type. x.f = e sets one field, when x is a local. The interpreter evaluates e, then replaces the field in x, as "Assignment statements" says.
  • The output uses the shared IR's struct, field and structUpdate nodes, which the TypeScript path already prints: a structure with a default for each field, { X := a, Y := b } for a literal, p.X for a read, and { p with X := e } for an assignment. Go adds no IR node.

Why some forms are refused ​

  • A keyed literal with fields out of order: Go still evaluates the elements in the order written, so the output would have to evaluate them in one order and place them in another. Accepting it would make the proof follow two orders.
  • A field whose type is a struct, a named type or anything else outside the four basic types, and embedded fields: each needs its own rules for zero values and selectors.
  • A field assignment through anything but a local, such as f().x = 1, which Go refuses anyway, or a.b.c = 1, which needs nested updates.

var p S without a value ​

This was refused at first, because the translator had no zero code for a struct type. A later change writes the code of S{} for it, the struct of the zero value of each field (Pure.elemZero), which the output already uses for an index out of range. elemZero_sound in Proof/Stmt.lean proves that this code gives the struct that the interpreter's zeroOf gives. The differential generator declares some struct locals this way.

A keyed literal that leaves out fields ​

This was refused at first too. A later change accepts it when the keys name fields in the order of the fields: the output's literal writes every field, with the element's code for a named field and the zero code for the others (Pure.fillKeyed). The elements are then still evaluated in the order written. fillKeyed_sound and structLit_keyed in Proof/Expr.lean prove that the result is the struct that the interpreter builds, with the elements' divisors in order. The differential generator leaves out some fields of keyed literals.

== and != on structs ​

These were refused at first, because the output had no decidable equality on its structures. A later change accepts them. The interpreter's eqVal compares two structs field by field (eqFields), as the Go specification says, and the IR's == uses the same eqVal, so the proof of == covers structs with no new case. The printer adds DecidableEq to the deriving clause of a structure whose fields are all Int, Bool or String, so decide (p = q) is valid Lean. This also changed the golden output of every such structure in the corpus. Certificates prove a struct comparison with eqFields_nil and eqFields_cons. These are tried only on goals that omega leaves, because the lemmas in the main simp set made another certificate run past its heartbeat limit. The corpus package structs has an example, Same, and the differential generator compares struct locals with each other and with the zero struct.

Proof ​

translate_sound covers every case. Proof/Expr.lean proves that a selector reads the field the interpreter reads, and that a literal's code gives the struct that the interpreter builds, with the divisors of its elements in order. Proof/Stmt.lean proves the field assignment with the same steps as x = e. The differential generator writes a struct type for some programs, with literals of all three forms, field reads and field writes, and the corpus package structs shows the output.

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