Appearance
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.leanadds 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
[]Tjoins the subset whenTisint,int64,boolorstring, 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 writesList 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 []Tgivesnil, which has no elements, and the output writes[].len(xs)is the number of elements, writtenGo.sliceLen xs.xs[i]is the element at indexi. Go panics wheniis out of range, with the same message as for a string. The output tests the index before the statement, as it does fors[i]on a string, withGo.sliceFault xs i, and then readsGo.sliceAt xs i z, wherezis 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] = eandmake: each changes or makes a backing array. The append decision since acceptsxs = append(xs, …)andmake([]T, 0, c)on a local that the function owns.- Slice expressions
xs[a:b]: the result shares the array ofxs, and the capacity matters to a laterappend. xs == nil: the value model does not tellnilfrom 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 Sdoes. 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.
