Skip to content

Base: the U32 bit facts an Array law on perfect trees needs #2059

Closed

Description

@adeerkhan

Base: the U32 bit facts an Array law on perfect trees needs

tensor-array proves get_after_set and get_other_set outside Base (#1934), and the last one takes
ASet.Apart as its hypothesis: the write's descent and the read's descent part at some node. On a perfect tree of
2^d leaves, Apart holds exactly when the two masked indices differ, and supplying that needs four facts about
U32: masked index, shr(2^d), the branch bit, and the subtraction bound. Base states only U32.add_comm.

The lemmas that do not need the missing pieces are proved outside Base, verified by the Lean kernel
(lean 4.34.0), in u32_bits.bend (local draft):

  • shr_shl
  • ctop_eq
  • top_shl
  • shl_p2
    plus the helper defs they rest on (ctop, top, init, zeros, p2, shl_put_zeros, shl_put, top_put,
    top_cons, init_put_eq). The canonical 2^d is p2(d, n): d zeros, a one, then zeros, with
    shl(p2(d,n)) == p2(d+1,n).

Reproduce:

PATH=$HOME/.elan/bin:$PATH bend u32_bits.bend --verdict

What is still missing, and why it belongs here

  1. Nat.add(d, Nat.sub(32, d)) == 32 for d <= 32. top(p2(d,32)) == False for d <= 30 closes as
    Q(K,d) = top(p2(d, d+K)) == False by induction on d (the step is p2(d+1,n) == F :: p2(d,n-1) and the top of a
    cons is the top of its tail), but instantiating at the 32-bit width needs K = 32-d. Base states no Nat.sub law.
  2. Word.and, Word.sub, Word.cmp. The mask fact (and(j, 2^d - 1) == j for j < 2^d) needs
    sub(2^d, 1) == ones(d) with borrow, a mask law, and cmp as unsigned comparison; the subtraction bound needs
    subtraction without borrow. Base proves Word.adc and Word.add_comm only.

The six lemmas above are offered as they stand: they are the part of the library that does not need the missing
pieces, and they make the remaining work a matter of Nat and word arithmetic rather than tree induction.

The program

# DRAFT, local only (never committed): the four U32 bit facts bend-ml#26 needs, as a library.
#
#   PATH=$HOME/.elan/bin:$PATH bend .local/u32_bits.bend --verdict
#
# ASet.Apart on a perfect tree of 2^d leaves needs, from ASet/get_other_set:
#   1. J(j, 2^d) == j                     when j < 2^d        (the mask keeps the low d bits)
#   2. shr(2^(d+1)) == 2^d
#   3. Z(j, 2^d) == bit d-1 of j
#   4. j with bit d-1 set: sub(j, 2^(d-1)) < 2^(d-1)
#
# Base has only U32.add_comm, so each needs Word (little-endian bit list) lemmas first.
#
# Proved: A and B.
#   A. shr(shl(w)) == ctop(w)      shifting up and back down clears the top bit
#   B. ctop(w) == w when top(w) == False
# Still to prove:
#   C. top(2^d) == False for d <= 30. It is the SAME statement as fact 2: A gives
#      ctop(w) == shr(shl(w)), so "ctop(pow2 d) == pow2 d" is "shr(pow2(d+1)) == pow2 d".
#      Neither is a shortcut for the other; both need the canonical pattern of shln(1, d),
#      because shl moves the single set bit. Done toward it: init/init_shl (dropping the top
#      bit commutes with shifting up) and shl_put. Note init_p and shl.put are the same
#      recursion; they differ only at length 0.
#      Also done: top_shl (top(shl(w)) == top(init(w))), via top_put/init_put_eq/top_cons.
#      Also done: the canonical word p2(d, n) = d zeros, one, then zeros (matching d first,
#      then n, avoids the match-order trap that blocks the natural two-way recursion), and
#      shl_p2: shl(p2(d,n)) == p2(d+1,n), which is what makes 2^d structural.
#      Also done: top_zeros and top_p2 — top(p2(d, d+2+j)) == False, i.e. the fact for every
#      word whose length exceeds the one's position by at least 2, by induction on d.
#      What remains of C is only the instantiation at the 32-bit width: top(32, p2(d,32)) needs
#      d + (2+j) == 32, i.e. the size bound d <= 30 that belongs to the array law (a U32 holds
#      2^d, so d <= 31; the top bit is the d = 31 edge). No standalone bit lemma can supply it.
#   D. the mask fact, E. Z is bit d-1, F. the subtraction bound, after C.
#
# The measured oracle these unlock is tensor-array/tests/apart.bend: 21,590 pairs through
# depth 7, 0 failures (2026-11-05).
import Base

import Base

def ctop_p(+p: Nat, +b: Bool, +t: Word(p)) -> Word(1n+p):
  match p:
    case 0n:
      match t:
        case WNil{}:
          WCon{False{}, WNil{}}
    case 1n+q:
      match t:
        case WCon{b0, tq}:
          WCon{b, ctop_p(q, b0, tq)}

def ctop(n: Nat, w: Word(n)) -> Word(n):
  match n:
    case 0n:
      WNil{}
    case 1n+p:
      match w:
        case WCon{b, t}:
          ctop_p(p, b, t)

def top_p(+p: Nat, +b: Bool, +t: Word(p)) -> Bool:
  match p:
    case 0n:
      b
    case 1n+q:
      match t:
        case WCon{b0, tq}:
          top_p(q, b0, tq)

def top(n: Nat, w: Word(n)) -> Bool:
  match n:
    case 0n:
      False{}
    case 1n+p:
      match w:
        case WCon{b, t}:
          top_p(p, b, t)

# LEMMA A: shifting a word up and back down clears its top bit
def pad_put(+p: Nat, +b: Bool, +t: Word(p)) -> {Word.shr.pad(p, Word.shl.put(p, b, t)) == ctop_p(p, b, t) : Word(1n+p)}:
  match p:
    case 0n:
      match t:
        case WNil{}:
          {==}
    case 1n+q:
      match t:
        case WCon{b0, tq}:
          %pad_put(q, b0, tq) : {WCon{b, Word.shr.pad(q, Word.shl.put(q, b0, tq))} == WCon{b, _} : Word(1n+p)}
          {==}

law shr_shl:
  for +n: Nat
  for w: Word(n)
  {Word.shr(n, Word.shl(n, w)) == ctop(n, w) : Word(n)}

def shr_shl(n, w):
  match n:
    case 0n:
      {==}
    case 1n+p:
      match w:
        case WCon{b, t}:
          %pad_put(p, b, t) : {Word.shr.pad(p, Word.shl.put(p, b, t)) == _ : Word(1n+p)}
          {==}

# LEMMA B: a word whose top bit is False is its own top-cleared copy
def ctop_p_eq(+p: Nat, +b: Bool, +t: Word(p), h: {False{} == top_p(p, b, t) : Bool}) -> {ctop_p(p, b, t) == WCon{b, t} : Word(1n+p)}:
  match p:
    case 0n:
      match t:
        case WNil{}:
          Equal.sym(Word(1n), WCon{b, WNil{}}, WCon{False{}, WNil{}},
            Equal.cong(Bool, Word(1n), x => WCon{x, WNil{}}, b, False{}, Equal.sym(Bool, False{}, b, h)))
    case 1n+q:
      match t:
        case WCon{b0, tq}:
          Equal.cong(Word(1n+q), Word(1n+(1n+q)), x => WCon{b, x},
            ctop_p(q, b0, tq), WCon{b0, tq}, ctop_p_eq(q, b0, tq, h))

law ctop_eq:
  for +n: Nat
  for w: Word(n)
  for h: {False{} == top(n, w) : Bool}
  {ctop(n, w) == w : Word(n)}

def ctop_eq(n, w, h):
  match n:
    case 0n:
      match w:
        case WNil{}:
          {==}
    case 1n+p:
      match w:
        case WCon{b, t}:
          ctop_p_eq(p, b, t, h)

# drop the most significant bit
def init_p(+p: Nat, +b: Bool, +t: Word(p)) -> Word(p):
  match p:
    case 0n:
      WNil{}
    case 1n+q:
      match t:
        case WCon{b0, tq}:
          WCon{b, init_p(q, b0, tq)}

def init(+p: Nat, w: Word(1n+p)) -> Word(p):
  match w:
    case WCon{b, t}:
      init_p(p, b, t)

# the p-shape: init_p(p, b, shl.put(p, b0, t)) == shl.put(p, b, init_p(p, b0, t))
def init_put(+p: Nat, +b: Bool, +b0: Bool, +t: Word(p)) -> {init_p(p, b, Word.shl.put(p, b0, t)) == Word.shl.put(p, b, init_p(p, b0, t)) : Word(p)}:
  match p:
    case 0n:
      match t:
        case WNil{}:
          {==}
    case 1n+q:
      match t:
        case WCon{b1, tq}:
          %init_put(q, b0, b1, tq) : {WCon{b, init_p(q, b0, Word.shl.put(q, b1, tq))} == WCon{b, _} : Word(1n+q)}
          {==}

# shl(p, w) is shl.put(p, False, w): both are False at the head and the bits shifted in
def shl_put(+p: Nat, w: Word(p)) -> {Word.shl(p, w) == Word.shl.put(p, False{}, w) : Word(p)}:
  match p:
    case 0n:
      match w:
        case WNil{}:
          {==}
    case 1n+q:
      match w:
        case WCon{b, t}:
          {==}

# LEMMA C1: dropping the top bit commutes with shifting up (in shl.put form, which is
# what init_put gives; shl_put converts when shl itself is needed)
def init_shl(+p: Nat, w: Word(1n+p)) -> {init(p, Word.shl(1n+p, w)) == Word.shl.put(p, False{}, init(p, w)) : Word(p)}:
  match w:
    case WCon{b, t}:
      init_put(p, False{}, b, t)

# init_p and shl.put are the same recursion; only length 0 differs in the operands
def init_put_eq(+p: Nat, +b: Bool, +t: Word(p)) -> {init_p(p, b, t) == Word.shl.put(p, b, t) : Word(p)}:
  match p:
    case 0n:
      match t:
        case WNil{}:
          {==}
    case 1n+q:
      match t:
        case WCon{b0, tq}:
          %init_put_eq(q, b0, tq) : {WCon{b, init_p(q, b0, tq)} == WCon{b, _} : Word(1n+q)}
          {==}

# top of a word is top_p with an ignored head
def top_cons(+p: Nat, w: Word(p)) -> {top(p, w) == top_p(p, False{}, w) : Bool}:
  match p:
    case 0n:
      match w:
        case WNil{}:
          {==}
    case 1n+q:
      match w:
        case WCon{b, t}:
          {==}

# the top bit of shl.put and of init_p agree: they are the same recursion
def top_put(+p: Nat, +c: Bool, +b: Bool, +t: Word(p)) -> {top_p(p, c, Word.shl.put(p, b, t)) == top_p(p, c, init_p(p, b, t)) : Bool}:
  match p:
    case 0n:
      match t:
        case WNil{}:
          {==}
    case 1n+q:
      match t:
        case WCon{b1, tq}:
          %top_put(q, b, b1, tq) : {top_p(q, b, Word.shl.put(q, b1, tq)) == _ : Bool}
          {==}

law top_shl:
  for +p: Nat
  for w: Word(1n+p)
  {top(1n+p, Word.shl(1n+p, w)) == top(p, init(p, w)) : Bool}

def top_shl(p, w):
  match p:
    case 0n:
      match w:
        case WCon{b, t}:
          match t:
            case WNil{}:
              {==}
    case 1n+q:
      match w:
        case WCon{b, t}:
          match t:
            case WCon{b0, tq}:
              %top_put(q, b, b0, tq) : {top_p(q, b, Word.shl.put(q, b0, tq)) == _ : Bool}
              {==}


def zeros(+n: Nat) -> Word(n):
  match n:
    case 0n:
      WNil{}
    case 1n+p:
      WCon{False{}, zeros(p)}

# 2^d as a word of length n: d zeros, a one, then zeros
def p2(+d: Nat, +n: Nat) -> Word(n):
  match d:
    case 0n:
      match n:
        case 0n:
          WNil{}
        case 1n+q:
          WCon{True{}, zeros(q)}
    case 1n+p:
      match n:
        case 0n:
          WNil{}
        case 1n+q:
          WCon{False{}, p2(p, q)}

# shl.put(p, b, zeros(p)) is b followed by zeros
def shl_put_zeros(+p: Nat, +b: Bool) -> {Word.shl.put(1n+p, b, zeros(1n+p)) == WCon{b, zeros(p)} : Word(1n+p)}:
  match p:
    case 0n:
      {==}
    case 1n+q:
      %shl_put_zeros(q, False{}) : {WCon{b, Word.shl.put(1n+q, False{}, zeros(1n+q))} == WCon{b, _} : Word(1n+(1n+q))}
      {==}

law shl_p2:
  for +p: Nat
  for +n: Nat
  {Word.shl(n, p2(p, n)) == p2(1n+p, n) : Word(n)}

def shl_p2(p, n):
  match p:
    case 0n:
      match n:
        case 0n:
          {==}
        case 1n+q:
          match q:
            case 0n:
              {==}
            case 1n+r:
              Equal.cong(Word(1n+r), Word(1n+(1n+r)), x => WCon{False{}, x},
                Word.shl.put(1n+r, True{}, zeros(1n+r)), WCon{True{}, zeros(r)},
                shl_put_zeros(r, True{}))
    case 1n+r:
      match n:
        case 0n:
          {==}
        case 1n+q:
          Equal.cong(Word(q), Word(1n+q), x => WCon{False{}, x},
            Word.shl.put(q, False{}, p2(r, q)), p2(1n+r, q),
            Equal.trans(Word(q), Word.shl.put(q, False{}, p2(r, q)), Word.shl(q, p2(r, q)), p2(1n+r, q),
              Equal.sym(Word(q), Word.shl(q, p2(r, q)), Word.shl.put(q, False{}, p2(r, q)), shl_put(q, p2(r, q))),
              shl_p2(r, q)))

# the top of a nonempty all-zero word is False
def top_zeros(+p: Nat) -> {top(1n+p, zeros(1n+p)) == False{} : Bool}:
  match p:
    case 0n:
      {==}
    case 1n+q:
      top_zeros(q)

# C, in the form that needs no width: d zeros, a one, then 2+k zeros, so the top is False
law top_p2:
  for +j: Nat
  for +d: Nat
  {top(Nat.add(d, Nat.add(2n, j)), p2(d, Nat.add(d, Nat.add(2n, j)))) == False{} : Bool}

def top_p2(j, d):
  match d:
    case 0n:
      %top_zeros(1n+j) : {top(Nat.add(0n, Nat.add(2n, j)), p2(0n, Nat.add(0n, Nat.add(2n, j)))) == _ : Bool}
      {==}
    case 1n+r:
      Equal.trans(Bool,
        top_p(Nat.add(r, Nat.add(2n, j)), False{}, p2(r, Nat.add(r, Nat.add(2n, j)))),
        top(Nat.add(r, Nat.add(2n, j)), p2(r, Nat.add(r, Nat.add(2n, j)))),
        False{},
        Equal.sym(Bool, top(Nat.add(r, Nat.add(2n, j)), p2(r, Nat.add(r, Nat.add(2n, j)))),
          top_p(Nat.add(r, Nat.add(2n, j)), False{}, p2(r, Nat.add(r, Nat.add(2n, j)))),
          top_cons(Nat.add(r, Nat.add(2n, j)), p2(r, Nat.add(r, Nat.add(2n, j))))),
        top_p2(j, r))

Activity

  1. adeerkhan commented on Nov 5, 2026

    @adeerkhan
    ContributorAuthor

    Status after another pass, so the proportions are clear: the and half of the mask layer is now proved too (and_ones, and_zeros, and_idem, over ones/zeros). What remains for the mask fact is the borrow chain sub(2^d, 1) = ones(d) through Word.adc and Word.cmp as an unsigned bound; the subtraction bound needs borrow-free subtraction on top of those. That is more work than the six lemmas above, and none of it is a correctness problem — it is the arithmetic the four facts were standing on.

  2. YidaWeng commented on Nov 9, 2026

    @YidaWeng
    Contributor

    Status check rather than a claim: I took the draft at the top of this issue exactly as it stands and ran it against current main (f0ddfe7b, Lean 4.34.0):

    • bun bend2/main.ts u32_bits.bend --verdict exits 0 with ALL PROOFS CHECK (kernel PASS), in about 30 s.
    • The file is 333 lines. Adding a three-line main and its #| line makes it run and print under tests/proof.
    • The six lemmas and their helpers are unchanged and still verify after the 86 commits since 2.0.40.

    The remaining facts are, as your update says, substantially more than the six lemmas: Nat.sub at the 32-bit width, then the borrow chain through Word.adc, Word.sub and Word.cmp. Base now has the Word.and/sub/cmp definitions but no laws for them, and nothing in Base uses these lemmas yet.

    So the placement question: land the proved part now as tests/proof/u32_bits.bend, which --verdict checks, and move it into Base when the four facts are complete and something uses them? Or put it in Base as it stands, accepting unused laws until the rest lands?

    I can turn the draft into that test as a PR if the first is the right home.

  3. YidaWeng commented on Nov 13, 2026

    @YidaWeng
    Contributor

    I went ahead with the tests/proof home: #2142 lands the proved lemmas as tests/proof/u32_bits.bend, with a main instance, and re-verifies them on upstream/main 9162bfe9 (ALL PROOFS CHECK under the Lean 4.34.0 kernel; bun gates/repo.ts passes 56 / 56).

    The proofs are adeerkhan's draft; the PR only adds the test wrapper and the re-run. I kept them out of Base for the reason above -- no Base consumer yet, and the helper defs are local -- but I'm happy to move them into Base if you'd prefer that now.

    The remaining work is as you described: Nat.add(d, Nat.sub(32, d)) == 32 for d <= 32, then the borrow chain through Word.adc/Word.sub/Word.cmp, the mask fact, the branch bit, and the subtraction bound. I'd be glad to help on that next if it's useful.

  4. alstrup commented on Nov 20, 2026

    @alstrup
    Contributor

    The remaining facts are proved in #2213. It's a self-contained tests/proof/array_perfect_tree.bend, Base only, that passes --verdict on current main:

    • The mask: and(x, sub(shln(1, d), 1)) == x for to_nat(x) < 2^d, through the borrow chain sub(2^d, 1) = d ones.
    • The halving shift: shr(shln(1, e+1)) == shln(1, e).
    • The branch bit: is_lt(x, shln(1, e)) == Nat.is_lt(to_nat(x), 2^e), via Word.cmp agreeing with Nat.cmp.
    • The right-half index: to_nat(sub(x, shln(1, e))) == to_nat(x) - 2^e for to_nat(x) in [2^e, 2^(e+1)).

    On top of these it proves Array.get and Array.set for every perfect tree of depth d < 32. The tree is described as build(d, l), a tree over a list. That keeps the laws usable in proofs where the array itself is erased, which is the usual case once an array is threaded through a program.

    It doesn't reuse #2142's text; the 2^d word lemmas there and here could be merged into one copy, whichever the maintainers prefer.

  5. Lorenzobattistela commented on Nov 23, 2026

    @Lorenzobattistela
    Collaborator

    Closing as done: #2213 is merged, and tests/proof/array_perfect_tree.bend proves the four facts this issue asked for (the mask, the halving shift, the branch bit and the right-half index), plus Array.get and Array.set on perfect trees of Nat, all passing --verdict. They live in tests/proof, not in Base. Whether Base should own Array laws stays open in #1934.

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