Appearance
Go panics on the proved path
Status: accepted · Date: 2026-11-26
Go's integer / and % panic when the divisor is zero. Lean's Int.tdiv and Int.tmod give a value instead. To prove the translation of / and %, the Go interpreter, the IR evaluator and the theorem need an outcome for a panic. This record says how they model it.
The decision
- The interpreter in
tolean/Go/Spec/Eval.leanhasOutcome.panic, next tonormalandret. An expression evaluates toCore.Res Val Val, which isOption (Except Val Val):nonewhen the subset has no meaning for it,.errorwith the panic's value, or.okwith its value. A panic is the string of Go's runtime error, such asruntime error: integer divide by zero. - The output uses the shared IR's
Body.throw, as TypeScript'sthrowdoes. A function that can panic returnsExcept String T,pureof its value, andthrowof the message. A function that cannot panic keeps its typeT, so no output changes for code that does not divide. - The output does not panic by itself, because Lean divides by zero without one. A statement that divides first tests its divisors and throws when one of them is zero.
Pure.divisorslists the divisors of an expression, andPure.guardedadds the test. A divisor that is a nonzero literal needs no test. - When the test finds a zero, the output evaluates the expression before it throws. The interpreter panics only when it reaches the division, and a call in the expression may have no value. Evaluating the expression in the output makes the two sides fail together. Lean drops the unused
let. - The translator refuses a division that Go might not evaluate: one in the right operand of
&&or||. Then a zero divisor of a statement always means a panic in Go, whatever the order of evaluation. translate_soundnow states that when the output returns a value or throws, the interpreter returns the same value or panics with the same message. The shared loop core carries a panic in itsretoutcome as.error, soCore.Loopdid not change.
What is left out
- A call of a function that can panic. The record on panicking calls adds it.
recover, and panics other than division by zero.- The state piece of the shared core (step 3 of the shared-core plan) and external calls (step 7).
Alternatives
- A Go-only outcome in the IR. The shared IR already has
throw, and TypeScript's evaluator already reads it as an exception. A second form for the same idea would need its own printer and lemmas. - Returning
Exceptfrom every function. It would change the output of every pure function, including those that cannot panic, and a proof about them would have to unwrap a value that is never an error. - A guard around each division, inside the expression. Lean has no early exit from an expression, so each operator above a division would need a bind. A test before the statement keeps expressions pure.
This page is docs/decisions/2026-11-26-go-panics.md at commit 5f1d031.
