Skip to content

Go slices on the proved path ​

Status: accepted · Date: 2026-11-29

A pure Go function may now take, return and build slices of int, int64, bool and string, read their length and index them. This record says which forms the proved path accepts, how the output writes them, and why the forms that change a slice stay on the string translation for now.

The decision ​

  • The interpreter in tolean/Go/Spec/Eval.lean adds a slice value: its list of elements. The Go specification defines slices in "Slice types", "Composite literals", "Index expressions" and "Length and capacity".
  • A slice type []T joins the subset when T is int, int64, bool or string, or a type of the package over one of them. It may be the type of a parameter, a result or a local. The output writes List T.
  • []T{a, b, c} evaluates its elements left to right, so a division by zero in an element panics before the slice exists. The output writes the list ([a, b, c] : List T). var xs []T gives nil, which has no elements, and the output writes [].
  • len(xs) is the number of elements, written Go.sliceLen xs.
  • xs[i] is the element at index i. Go panics when i is out of range, with the same message as for a string. The output tests the index before the statement, as it does for s[i] on a string, with Go.sliceFault xs i, and then reads Go.sliceAt xs i z, where z is the zero value of the element type. Lean's list access needs a value for an index out of range, and the test makes that value unreachable.

Why a list is the right value ​

A Go slice is a view of a backing array, and two slices can share one. A write through one slice, xs[0] = 1, or an append that fits in the capacity, changes what the other slice reads. A list cannot show that. On the proved path no code changes a slice: the subset has no index assignment, no append, no copy and no slice expression xs[a:b], and a function of the proved path calls only functions of the proved path. A slice that nothing changes reads the same as its list of elements, however many slices share its array. So the list is exact for every function that the translator accepts.

Why some forms are refused ​

  • append, xs[i] = e and make: each changes or makes a backing array. The append decision since accepts xs = append(xs, …) and make([]T, 0, c) on a local that the function owns.
  • Slice expressions xs[a:b]: the result shares the array of xs, and the capacity matters to a later append.
  • xs == nil: the value model does not tell nil from an empty slice. Both have length zero and no element, so every other operation of the subset agrees on them.
  • == between two slices: Go refuses it.
  • Keyed elements such as []int{2: 5}: they need the rules for indices in composite literals.
  • Slices of structs and slices of slices: their zero element needs a zero code for structs, as var p S does. Both came later (structs, slices).
  • for … range xs: the loop core takes a list of items, so this is the next loop form to add.

Proof ​

translate_sound covers every case. Proof/Expr.lean proves that a slice literal gives the list that the interpreter builds, with the checks of its elements in order, that len gives the same length, and that an index gives the interpreter's element or, through Go.sliceFault, its panic. The differential generator writes slice locals from literals and var xs []T, with len(xs) and xs[i] at indices that may be out of range, and the corpus package slices shows the output.

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