Repository navigation
Conversation
…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>
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 whatArray.sizereturns for a tree of depth d.u32_shln_oneto_nat(shln(1, d)) == 2^d, for d < 32u32_shr_shln_oneshr(shln(1, e+1)) == shln(1, e)(the half size inget.go/swap.go)u32_and_low_maskand(x, sub(shln(1, d), 1)) == xwhento_nat(x) < 2^d(the mask inget.at/swap.at)u32_is_lt_shln_oneis_lt(x, shln(1, e)) == Nat.is_lt(to_nat(x), 2^e)(the branch bit)u32_sub_shln_oneto_nat(sub(x, shln(1, e))) == to_nat(x) - 2^eforto_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:array_get_perfectArray.get(Nat, build(d, l), x) == (build(d, l), nth(l, to_nat(x)))array_set_perfectArray.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:
sub(2^d, 1)is d ones (borrow,sub_one).sub_set).Word.cmpagrees withNat.cmpone bit at a time (cmp_to_nat).wmask).shl/shrmove 2^d structurally.Checks
bun bend2/main.ts tests/proof/array_perfect_tree.bendprints{==}in about 0.9 s.--verdictprintsALL PROOFS CHECK.Notes
2^dword andshllemmas) but not its text. I'm happy to rebase onto test(proof): the U32 bit lemmas an Array law on perfect trees needs (#2059) #2142's lemmas if you'd rather keep one copy.buildand the laws are written for Nat elements. A generic version needs a default value fornthon short lists.🤖 Generated with Claude Code