Skip to content

test(proof): Array.get and Array.set on perfect trees, with the U32 facts they need (#2059) - #2213

Open
alstrup wants to merge 1 commit into
bendlang:mainfrom
alstrup:array-perfect-tree-laws
Open

alstrup wants to merge 1 commit into
bendlang:mainfrom
alstrup:array-perfect-tree-laws

Conversation

@alstrup

@alstrup alstrup commented Nov 20, 2026

Copy link
Copy Markdown

Closes the gap #2059 describes: the U32 facts behind Array's descent, and with them Array laws that hold on any perfect tree.

What it proves (tests/proof/array_perfect_tree.bend, Base only)

The U32 facts, phrased the way Array computes them. shln(1, d) is what Array.size returns for a tree of depth d.

law statement
u32_shln_one to_nat(shln(1, d)) == 2^d, for d < 32
u32_shr_shln_one shr(shln(1, e+1)) == shln(1, e) (the half size in get.go / swap.go)
u32_and_low_mask and(x, sub(shln(1, d), 1)) == x when to_nat(x) < 2^d (the mask in get.at / swap.at)
u32_is_lt_shln_one is_lt(x, shln(1, e)) == Nat.is_lt(to_nat(x), 2^e) (the branch bit)
u32_sub_shln_one to_nat(sub(x, shln(1, e))) == to_nat(x) - 2^e for to_nat(x) in [2^e, 2^(e+1)) (the right half's index)

The Array laws, on build(d, l), the perfect tree of depth d over the first 2^d entries of a list:

law statement
array_get_perfect Array.get(Nat, build(d, l), x) == (build(d, l), nth(l, to_nat(x)))
array_set_perfect Array.set(Nat, build(d, l), x, v) == build(d, upd(l, to_nat(x), v)), given 2^d ≤ len(l)

Both hold for every d < 32 and every index below 2^d.

Why the laws are stated on build(d, l)

A proof about a program that threads an array usually holds the array erased: Arrays are Type, so they can't be copied into several lemma calls. Lemmas whose proof matches on a live array can't be applied to such an array. build(d, l) describes the array by a live list, so these laws apply wherever the list is known. I used them to prove an in-place array algorithm correct (a Taylor shift, by simulation against the same template run on a list), with nothing assumed.

How the U32 facts are proved

Each goes through the bits of a Word, with no extra word laws beyond Base:

  • Borrow chain. sub(2^d, 1) is d ones (borrow, sub_one).
  • Set bit. Subtracting 2^e from a value with bit e set clears that bit (sub_set).
  • Comparison. Word.cmp agrees with Nat.cmp one bit at a time (cmp_to_nat).
  • Mask. Masking a word below 2^d with a word worth 2^d - 1 keeps it (wmask).
  • Shifts. shl/shr move 2^d structurally.

Checks

  • bun bend2/main.ts tests/proof/array_perfect_tree.bend prints {==} in about 0.9 s.
  • --verdict prints ALL PROOFS CHECK.

Notes

🤖 Generated with Claude Code

…acts they need (bendlang#2059)

Proves, over Base only, the four U32 facts bendlang#2059 names for Array's
descent, phrased as Array computes them (shln(1, d) is Array.size):

- u32_shln_one: to_nat(shln(1, d)) == 2^d, for d < 32
- u32_shr_shln_one: shr(shln(1, e+1)) == shln(1, e), for e+1 < 32
- u32_is_lt_shln_one: is_lt(x, shln(1, e)) == (to_nat(x) < 2^e)
- u32_sub_shln_one: to_nat(sub(x, shln(1, e))) == to_nat(x) - 2^e,
  for to_nat(x) in [2^e, 2^(e+1))
- u32_and_low_mask: and(x, sub(shln(1, d), 1)) == x, for to_nat(x) < 2^d

and with them the Array laws on build(d, l), the perfect tree of depth d
over a list:

- array_get_perfect: get(build(d, l), x) == (build(d, l), nth(l, to_nat(x)))
- array_set_perfect: set(build(d, l), x, v) == build(d, upd(l, to_nat(x), v))

Every lemma is proved in the file; it checks in under a second and
passes --verdict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant