Appearance
Go constant expressions on the proved path
Status: accepted · Date: 2026-12-06
Follows Go package constants, which read only literal constants.
The decision
- The interpreter.
Eval.constExprevaluates a constant expression (Go specification, "Constant expressions", "Constant declarations", "Iota"): literals,trueandfalse,iota, constants declared before it, and-,!,+,-,*,/,%,<<,>>, comparisons,&&and||.Eval.packageConstswalks the declarations in order. Within aconstgroup theith spec hasiota = i, and a spec without type and values repeats the last one with values. - Exact arithmetic. Go evaluates constants exactly. A typed constant must be representable at each step, or
go buildrefuses the package, so for every package that compiles exactIntarithmetic gives Go's value./truncates and%follows the dividend, as Go does for integer constants.>>is a floor division by a power of two. The final value must still fitint64where the translator uses it. - The proof. The proved path already inlines a constant's value from
Program.consts, so the proof needs no change: the interpreter and the translator read the same value. - The output. The string translation writes
def X : T := vwith the computed value, since a spec that repeats the one before has no expression of its own. It writes every constant the interpreter reads, so an unused untyped one outsideint64, such as1 << 70, becomes adefof typeIntwith its exact value that nothing uses. - Variables. An unexported
varthat nothing changes may start at a constant expression. Without a type, an integer variable takes the type of its expression, which can be a typed constant's such asint64; the subset then accepts only a literal, which givesint. - Left out: constants from later ones (Go allows any order), floats, runes,
&,|,^,&^,lenand conversions in constants, shifts by more than 512, and variables whose value is no constant expression, which depend on the order of package initialization. - Types.
Eval.constExprgives each value its type name,nonewhen untyped. A constant without a type of its own takes the type of its expression:Mon = Sun + 1for aSunof typeDayis aDay, as a spec that repeats a typed one is. A comparison is an untyped boolean. Two typed operands must have the same type name, so constants oftype D intandtype W intdo not mix, and an alias and its target are refused the same way, conservatively. Every typed result, also an intermediate one, must fit its type:A * 4 / 8forA Day = 1 << 62leaves the subset, asgo buildrefuses it. The untyped operand of a mixed operation is converted to the typed one's type and must fit it too, soA < 1 << 70leaves the subset. A constant or variable with a type takes only an untyped value or one of that type, and it must fit. - Testing.
tests/go/Consts.leanchecks the translation against the interpreter and the refusals, and pins the types above.- The certified corpus package
tests/go/corpus/translate/constexprhasiota, a repeated typed spec,1 << 70 >> 68,/and%of a negative constant, and string and bool constants built from others.go run ./checkrequires Lean to accept its certificate, in CI. - The differential generator writes constants with
iota, the constant before and operators, whichgo runchecks. It leaves them out underTOLEAN_CERTIFY_ONLY=1, so the fixed certificate sample (seed 20261130) keeps the programs of the rows intests/certificates/expected-failures.tsv. Its certificates therefore do not cover these forms; theconstexprpackage does.
This page is docs/decisions/2026-12-06-go-constant-expressions.md at commit 5f1d031.
