Repository navigation
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
Closed
Giulio2002 wants to merge 3 commits into
Giulio2002 wants to merge 3 commits into
Conversation
… 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
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]>
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]>
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. |
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.
bench/checker/*: within ±4% of main, the same range as the version that asked only at the top;.F(100000 * 2) == F(100000 + 100000)still unrollsNat.add,Nat.mul,Nat.cmpandU32.to_naton numerals return the literal their definitions compute.What changed (
bend2/bend.ts)Numerals (
term_wnf): when aReftoNat.add,Nat.mul,Nat.cmporU32.to_natis 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'sBUILTIN NATURALor Lean's kernelNat, 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.)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,absurdwalked 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) == TruewhereBool.and(A, B) == Trueis expected andok_Xis defined asBool.and(A, B). The identicalBs meet only one unfold down, andBcontains a check over 2^13 roots; asking only at the top normalizes both copies (the BeaconState root overflowed the stack).Sharing record (
term_compare): each top-level conversion counts steps; past 256 it records(lhs, rhs, mode, depth) → resultfor 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.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 variablex: nothing closed to state as a literal, each side a chain ofdshared nodes):Measured earlier on an Apple M-series machine against a previous revision of this branch (same three mechanisms):
100000n + 100000n,F(100000*2) == F(100000+100000)maxA 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-onlyoutput 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