Skip to content

Go: slice expressions xs[a:] ​

Status: accepted · Date: 2026-12-03

Step 4 of the Go plan lists slice expressions xs[a:b] as the next slice item. This record says which of them the proved path accepts, and why the others wait.

The decision ​

  • xs[a:] and xs[:] on a slice are on the proved path. The output is Go.sliceFrom xs a, which is List.drop, after the test Go.sliceFromFault xs a: Go panics with slice bounds out of range [a:n], or [a:] for a negative a, unless 0 ≤ a ≤ len(xs). Go makes the test after it evaluates xs and a, and the translation places it among the other checks in that order.
  • The interpreter gains the same rule (Eval.sliceFromVal), and translate_sound covers it (sliceFrom_outcome).
  • The result shares the array of xs. The ownership check (Owned) already treats xs[a:] as a read that lets the array escape, so a local that the function changes with append or xs[i] = e cannot be sliced, and a slice made by xs[a:] cannot be changed. The one exception is xs[a:][i], which reads an element and keeps nothing: it counts as a read of an element, like xs[i].

Refused ​

  • A high bound, as in xs[a:b] and xs[:b]. Go accepts b up to the capacity of xs, not its length, so xs[:b] can show elements past len(xs). The list model has no capacity, so it cannot give these elements or the right panic. It needs a slice value that records the capacity and the array, which is larger than one step.
  • Three-index expressions xs[a:b:c], for the same reason.
  • Slice expressions on strings, such as s[1:]: they need byte offsets checked against UTF-8, a separate step.
  • append to a slice made by xs[a:], or to xs[a:] itself, and xs[i] = e on either: the two share an array, so the list model would not be exact.
  • for … range xs[a:] directly: range takes a local or a parameter, so the corpus writes ys := xs[a:] first.
  • A call that may panic as the argument of len, as before.

Checks ​

tests/go/Slices.lean runs both sides on xs[a:], xs[:], a low bound at the length, both panics, a divisor inside the bound, a function that returns xs[a:], and xs[a:][i] on an owned local, and checks each refusal. The corpus package slices gains Rest. The differential generator writes xs[a:][i] and xs[:][i] with literal and computed bounds.

This page is docs/decisions/2026-12-03-go-slice-from.md at commit 5f1d031.