Skip to content

Base: should Array state its own laws (set keeps the size, get after set, set leaves other cells alone)? #1934

Open

Description

@nuxyel

What Bend should do

# Base would state these next to Array.set, so packages don't each carry a copy.
# sz(T, a) = Pair.snd(Array<T>, U32, Array.size(T, a))
# J(i, n) = U32.and(i, U32.sub(n, 1)) and Z(i, n) = U32.is_lt(J(i, n), U32.shr(n)): the masked index and
# the first branch, as Array.swap.at and Array.get.at compute them
law size_set:
  for -T: Data
  for a: Array<T>
  for +i: U32
  for +v: T
  {sz(T, Array.set(T, a, i, v)) == sz(T, a) : U32}

law get_after_set:
  for -T: Data
  for a: Array<T>
  for +i: U32
  for +v: T
  {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, a, i, v), i)) == v : T}

law get_other_set:
  for -T: Data
  for a: Array<T>
  for +i: U32
  for +k: U32
  for +v: T
  for h: Apart(T, a, sz(T, a), J(i, sz(T, a)), Z(i, sz(T, a)), J(k, sz(T, a)), Z(k, sz(T, a)))
  {Pair.snd(Array<T>, T, Array.get(T, Array.set(T, a, i, v), k)) == Pair.snd(Array<T>, T, Array.get(T, a, k)) : T}

Why

Base defines Array.get, Array.set and Array.size but states no law about them, so every package that reasons about an array's contents has to prove them from the tree. I needed these three for bend-ml's tensor-array and proved them outside Base, on Bend 2.0.39, with --verdict, no @unsafe and no axioms, for any T: Data: docs/upstream/array_set_laws.bend (462 lines; bend array_set_laws.bend --verdict prints ALL PROOFS CHECK, and running it prints (1.5, 2.5, 9.0)). The first two hold for every i, because the two descents mask it the same way, and for every tree, balanced or not (#808).

The third needs a hypothesis, and "i and k differ after the mask" is not enough: Array.size follows the leftmost path, so in ANode{ANode{ALeaf{0.0}, ALeaf{1.0}}, ALeaf{2.0}} (size 4) a write at 2 changes the read at 3 (the 9.0 above). Apart is defined by recursion on the tree with the branch tests Array.swap.go and Array.get.go use: Empty when both descents reach the same leaf, Unit at the node where they part. On a concrete tree it computes, so Unit{} proves it. On a perfect tree it holds exactly when the masked indices differ, but I have not proved that in Bend; it needs lemmas about U32 bits.

It is longer than it looks because an array is affine inside a proof too: the lemma that Array.size returns the array it measured, the induction step and the lemma that opens the pair Array.swap.go returns each need the same subtree. The file proves them all in one pass over the tree, and works around #1077 with a trailing Unit.

Three questions:

  1. Would you like these in Base, so packages don't each carry a copy? I can send a PR.
  2. Is a tree predicate like Apart an acceptable hypothesis for the third, or would you rather state it for perfect trees with "the masked indices differ", next to a predicate for perfect trees? The second reads better but needs the U32 bit lemmas, which I'd have to write first.
  3. If not in Base, is a package the intended home for laws about Base's types?

Activity

  1. nuxyel commented on Nov 1, 2026

    @nuxyel
    ContributorAuthor

    A short follow-up, in case it helps decide. The three laws now ship in a package: bend-ml's tensor-array 0.1.19.0 carries size_set, get_after_set and get_other_set, checked with --verdict on Bend 2.0.39, no @unsafe and no axioms. So nothing is blocked on my side; the question is only whether Base wants to own them, so that every package that reasons about an array's contents doesn't carry its own copy of the proofs.

    One related thing came up since: #1980 (filed by adeerkhan) is about Array<T> accepting an unbalanced tree that the compiled runtime refuses with a fail-stop. If Base states laws about Array, they would hold for every tree the type allows, including the ones the compiled program can't build, so the two issues may want one answer.

    If laws like these don't belong in Base, a word saying so is enough, and I'll keep them in the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions