Skip to content

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.lean has Outcome.panic, next to normal and ret. An expression evaluates to Core.Res Val Val, which is Option (Except Val Val): none when the subset has no meaning for it, .error with the panic's value, or .ok with its value. A panic is the string of Go's runtime error, such as runtime error: integer divide by zero.
  • The output uses the shared IR's Body.throw, as TypeScript's throw does. A function that can panic returns Except String T, pure of its value, and throw of the message. A function that cannot panic keeps its type T, 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.divisors lists the divisors of an expression, and Pure.guarded adds 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_sound now 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 its ret outcome as .error, so Core.Loop did 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 Except from 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.