Skip to content

Conversion without blowups on numerals, same calls and shared graphs, at no cost to ordinary checks (#1071, #1075) - #1151

Closed
Giulio2002 wants to merge 3 commits into
bendlang:mainfrom
Giulio2002:conversion-no-blowups
Closed

Giulio2002 wants to merge 3 commits into
bendlang:mainfrom
Giulio2002:conversion-no-blowups

Conversation

@Giulio2002

@Giulio2002 Giulio2002 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor
Objection This change
"Identical first" is quadratic when nested Every step still asks it (generated proofs need it: see below), but no walk is repeated: a walk remembers the calls it found different and its answer at share cells, so a nested step that meets them answers at once. Linear in the terms, not quadratic.
…or slows every comparison by a constant A walk starts only if a side is a call; it first checks the two heads and arities without allocating, and stops at the first difference. Measured cost on bench/checker/*: within ±4% of main, the same range as the version that asked only at the top;.
Capping (4096 visits) trades one problem for another No cap. The only threshold is when the sharing record switches on (after 256 steps of one conversion).
F(100000 * 2) == F(100000 + 100000) still unrolls Base's Nat.add, Nat.mul, Nat.cmp and U32.to_nat on numerals return the literal their definitions compute.
Blowups come from shared graphs walked as trees Past 256 steps, a conversion records the pairs it settles where a side is a share cell (the only node reachable twice), so a DAG is compared once.

What changed (bend2/bend.ts)

  1. Numerals (term_wnf): when a Ref to Nat.add, Nat.mul, Nat.cmp or U32.to_nat is about to unfold and every argument is a numeral as written (share cells and annotations seen through, nothing evaluated), it returns the result as a literal; results above 2^32 − 1, and any other argument, unfold as today. Base's definitions stay the specification — this is the same kind of extension as Agda's BUILTIN NATURAL or Lean's kernel Nat, limited to Base's own functions. Evidence: a set of agreement laws covering these ops checks on main by plain unfolding and on this branch through the shortcut; false laws (e.g. Nat.mul(6n, 6n) == 35n) are still rejected. (Kept out of the repo to keep the diff small; happy to add them.)

  2. Same term (term_compare): at every step, before normalizing, if a side is a call and both sides are written the same way (variables by level; calls, constructors, datatypes, equalities, refl, absurd walked like Conversion checks syntactic identity before normalizing (#1071) #1075's check), they're equal. The walk checks heads and arities first, compares arguments first to last, and remembers the calls it found different and its answer at share cells, so no walk is repeated.

    Why every step and not just the top: generated proofs (bend-ssz) pass evidence for ok_X(s, dv) == True where Bool.and(A, B) == True is expected and ok_X is defined as Bool.and(A, B). The identical Bs meet only one unfold down, and B contains a check over 2^13 roots; asking only at the top normalizes both copies (the BeaconState root overflowed the stack).

  3. Sharing record (term_compare): each top-level conversion counts steps; past 256 it records (lhs, rhs, mode, depth) → result for pairs where a side is a share cell and answers repeats from the record. The record is dropped when the top-level conversion returns.

In short, this is #1075 but without quadratic complexity.

Numbers

Apple M-series laptop (idle), CPU time (user + sys) of bend <file> --check-only, 7 interleaved rounds of main (b2111cf) and this branch, medians; ±4% is noise here.

bench/checker main this
compute_1600 2.05 s 2.13 s (+2%)
defs_12800 1.83 s 1.79 s (−2%)
generics_3200 1.84 s 1.88 s (+2%)
proofs_3200 4.03 s 4.10 s (+2%)
trees_400 2.56 s 2.66 s (+4%)
hashes_4 8.80 s 0.40 s
shared_4 0.38 s 0.35 s

bend-ssz (generated SSZ proofs, checked today on Bend 2.0.28 + #1075), proofs/obj/big_root_BeaconState.bend (12,882 definitions), with this change ported onto the same 2.0.28 base, both run side by side on a 48-core Linux box: #1075 384 s CPU, this 342 s; every definition checks. Asking the identity question only at the top overflows the stack on it.

A symbolic-leaf Merkle root, loop form vs recursive form (root_loop(d, x) == root_rec(d, x) for a variable x: nothing closed to state as a literal, each side a chain of d shared nodes):

depth main this
16 0.8 s 0.5 s
20 7.2 s 0.5 s
24 70 s 0.4 s
32 > 300 s (killed) 0.5 s

Measured earlier on an Apple M-series machine against a previous revision of this branch (same three mechanisms):

case main #1075 this
hashes_100 150 s 0.41 s 0.41 s
#1071 a / b, jeromelaurens' closed bound stack overflow ~0.17 s ~0.17 s
100000n + 100000n, F(100000*2) == F(100000+100000) stack overflow stack overflow 0.17 s
aldeni fold, 16 / 20 steps, old copying max 4.4 / 54 s 0.92 / 8.2 s 0.23 / 0.19 s
false equality inside the copying fold rejected, 11.8 s rejected, 11.9 s rejected, 3.4 s

A real proof book (bend-collections, 36 roots, being ported to 2.0.32): main checks 29, this 34; five roots that time out at 900 s on main check in 18–59 s; the 29 roots that check on both use 53% less CPU; none that checks on main fails here.

Soundness: all 1,510 files under tests/ give the same --check-only output on this commit as on the previous revisions, which matched main on every file (identical error output on the failing ones).

What it doesn't fix

  • Numerals past 2^32 − 1 unfold as before.
  • Two different closed expressions with the same value that aren't these Nat functions still evaluate fully — your literal pattern remains the answer there.
  • Terms duplicated as separate objects (not shared) are still walked twice.

Giulio2002 and others added 2 commits September 28, 2026 03:40
… at no cost to ordinary checks (bendlang#1071, bendlang#1075)

- Base's Nat.add, Nat.mul, Nat.cmp and U32.to_nat on numerals (as
  written, never evaluated) give the literal their definitions compute
  instead of unfolding through Succ; any other argument unfolds as before.
- A conversion first asks whether both sides are the same call written the
  same way: one walk, stopping at the first difference, once per top-level
  comparison.
- Past 256 steps, a conversion records the pairs it settles where a side is
  a share cell, so a shared graph is compared once instead of as a tree.

Tests and benches are bendlang#1075's: tests/check/conversion_same_before_whnf.bend,
bench/checker/hashes_4 and shared_4.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ecting arguments

- The step count and the sharing record live in a context that
  term_compare creates and passes down, instead of module-level variables;
  the record is only allocated once a conversion passes 256 steps.
- term_wnf collected the arguments of every unfolding Ref before asking
  whether it was one of the four Nat ops, which cost trees_400 about 30%;
  it now asks first.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Giulio2002
Giulio2002 marked this pull request as ready for review September 28, 2026 02:40
Proofs generated by bend-ssz pass evidence for ok_X(s, dv) where the
expected type is its body, Bool.and(A, B). The two sides meet as the same
calls only one unfold down, and B contains a check over 2^13 roots, so a
conversion that asks the identity question only at the top normalizes
both copies of it (the BeaconState root overflowed the stack).

- Every step of a conversion asks whether both sides are the same term,
  written the same way, as bendlang#1075 did; variables match by level, and
  equalities, datatypes, refl and absurd are walked like bendlang#1075's check.
- A walk first checks that both calls have the same head and arity
  (no allocation), then compares the arguments first to last, and
  records the calls it found different, so a failed walk is never
  repeated as the conversion descends: no quadratic cost, no cap.
- The sharing record, the Nat shortcut and the per-comparison context
  are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Giulio2002

Copy link
Copy Markdown
Contributor Author

this is something existing in lean so since unlike the previous one, it does not introduce significant overhead and quadratic complexity, I think it is a good addition

VictorTaelin added a commit that referenced this pull request Sep 29, 2026
…1071, PR #1151 by Giulio2002)

Checker (bend2/bend.ts): term_compare first runs the existing comparison
(now compare_go) with RIGID, one empty book, so no def unfolds and every
call stays stuck; the App case then matches two copies of one call by head
and arguments. Only if that pass fails does the comparison run as before.
The recursion goes through compare_go, so the pass runs once per
conversion: no new walk, no cap, no memo.

Giulio2002 found the case (#1071, #1075, #1151 and his Merkle gist): a law
proven by induction, agree(d, x), used at a fixed size, agree(32n, x),
walked a shared chain of 32 nodes as a tree of 2^32 when both copies of
root_loop(32n, x) unfolded. It now checks in 0.09 s (was > 20 s).

Tests: conversion_copy_before_unfold (the Merkle law at 32n) and
conversion_def_named_like_object_key (a def named toString). Gates: tests
1509/1509, repo 49/49, --verdict parity on tests 969 agree, 0 false
rejects, 0 timeouts. bench/checker on the minis: trees -0.4%, compute
-18.5%, defs -2.9%, proofs -4.9%, generics -1.6%. bend.ts +104 ttok.

CHANGELOG.md and VERSION name 2.0.33; the notes also cover the fixes since 2.0.32.

Co-Authored-By: Giulio2002 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
VictorTaelin added a commit that referenced this pull request Sep 29, 2026
…io2002)

Checker (bend2/bend.ts, compare_go): when both sides are filled share
cells and EQ proves their values equal, the rhs cell takes the lhs cell's
value, so a later comparison of the same pair meets one object (a === b)
and stops. A value used twice on each side, as in mix(h, h), is compared
once instead of walked as a tree.

Giulio2002's Merkle gist, as written: root_loop(32n, x) == root_rec(32n,
x) by {==} for a symbolic leaf checks in 0.07 s (2^32 steps before; depth
20 was 1.19 s). A false variant is still rejected at once; its error still
prints the 2^d-sized term, as before.

Tests: conversion_shared_cells_once (the gist at depth 32). Gates: tests
1510/1510, repo 49/49; --verdict parity 969 agree and 1 false reject,
this test: the BendTT kernel does not share and runs out of fuel.
bench/checker vs 2.0.33 on the minis: compute_1600 +8.6% (the branch fires
in 446k of 959k compares), the rest within noise; hashes_4 0.08 s.
bend.ts +105 ttok.

CHANGELOG.md and VERSION name 2.0.34.

Co-Authored-By: Giulio2002 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@VictorTaelin

Copy link
Copy Markdown
Contributor

Bend 2.0.34 (7d8a3eb) takes this in a simpler form, credited to this PR. The details and a request to test again are in my reply on your gist: https://gist.github.com/Giulio2002/b85a64d89f1b383ccf1f9e0ecc3d4182?permalink_comment_id=6395528#gistcomment-6395528

Note: an AI wrote this reply after I made the decision. If anything here is wrong, reply and I will review it myself.

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.

2 participants