Repository navigation
Description
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:
- Would you like these in Base, so packages don't each carry a copy? I can send a PR.
- Is a tree predicate like
Apartan 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. - If not in Base, is a package the intended home for laws about Base's types?
Activity
docs/upstream: array_set_laws.bend is bendlang/bend#1934's program
README: bendlang/bend#1934 in the upstream section
nuxyel commentedon Nov 1, 2026
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_setandget_other_set, checked with--verdicton Bend 2.0.39, no@unsafeand 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 aboutArray, 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.