Repository navigation
Conversation
…lt do Base keeps them structural (bendlang#860): the checker unfolds them in step with Nat.cmp, so Nat.max_ge_l and Nat.max_ge_r prove by cases. base.bend is untouched here, and every law and proof over them stays as it was. The JS and C lanes ran that same recursion, one step per unit of the smaller argument, on a Nat that is a machine word there. OPERATIONS gains nat_min and nat_max beside nat_add, nat_sub, nat_cmp and nat_is_lt: ($0 < $1 ? $0 : $1) and ($0 > $1 ? $0 : $1) on both lanes. A pure main run by the checker's evaluator still unfolds them, as it does Nat.add. 10,000 calls of Nat.min(t, 10000n), t past 10000n, in a tail loop (median of the runs): C 141 ms -> under 1 ms, JS 10.9 s -> 9 ms, `bend file.bend` (IO main) 10.9 s -> 3 ms; Nat.max(10000n, t) 117 ms -> under 1 ms in C. One Nat.max of two IO.now() readings near 2.6e8 ms: 980 ms -> under 1 ms in a binary. tests/base/nat_min_max prints its 22 lines on check, interp, JS and C. On ac0ddb7 the three running lanes print the 12 small cases, then stop with "memory fault (machine stack overflow?)" at the first pair near 2^40. The whole tests/ through a local replica of gates/test.ts (its shard script, parser and judge, bash in place of ssh): 1440 / 1442 here, 1439 / 1441 on ac0ddb7, the same two failing on both: gfx_clicks and gfx_window [c], which expect a host without a display. comp.ts is 62258 ttok (cap 64000). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thank you, this is a clean change, and we verified it. It keeps the structural Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself. |
What
Two rows in
OPERATIONS(bend2/comp.ts):nat_minandnat_max,($0 < $1 ? $0 : $1)and($0 > $1 ? $0 : $1)on the C and JS lanes, besidenat_add,nat_sub,nat_cmpandnat_is_lt. A test,tests/base/nat_min_max.bend.base.bendis untouched.Nat.min/Nat.maxstay structural, as #860 made them, so the checker still unfolds them in step withNat.cmp.Nat.max_ge_l,Nat.max_ge_rand any user proof that relies on that unfolding are unchanged. This is the split Base already uses forNat.addorNat.cmp: the structural definition is what the checker sees, and the machine operation is what the compiled lanes run.Why
On the JS and C lanes a
Natis a machine word, butNat.min/Nat.maxran the structural recursion there: one step per unit of the smaller argument. With a clock reading as an argument, that recursion is the whole cost.demos/io_rollback_netcodetakesNat.maxof millisecond timestamps (Nat.max(ctime, born)in the relay,Nat.max(ctime, Nat.sub(stime, tol))in the client).I measured 10,000 calls of
Nat.min(t, 10000n)in a tail-recursive loop, withtpast10000n, timed withIO.now()around the loop. The table gives the median of 3-5 runs, on Linux x86-64:-o x)-o x.js, run with bun)bend file.bend(IO main)Nat.max(10000n, t)behaves the same: 117 ms → under 1 ms in C, 10.9 s → 9 ms in JS. A singleNat.maxof twoIO.now()readings near 2.6·10⁸ ms took 980 ms in a binary before this change and under 1 ms after.A pure
mainrun by the checker's evaluator (bend file.bendon a non-IOmain) still unfolds the definitions, as it doesNat.add.Test
tests/base/nat_min_max.bendcovers 0 with 0, 0 with n on either side, equal values, a < b, a > b, and the same cases on pairs near 2^40 (below the 2^48-1 immediate cap). It also callsNat.maxinside the lambdaList.foldltakes as its template argument (~Nat.maxitself is refused there: its+quantities don't matchB -> A -> B), andNat.minthrough a closure. It prints its 22#|lines on all four lanes:--check-only,bend file.bend,-o x.jsunder bun, and-o x.On ac0ddb7 the three running lanes print the 12 small cases. At the first pair near 2^40 they stop with
memory fault (machine stack overflow?), so the old definition cannot pass the test by printing a wrong line.Regression
I ran all of
tests/through a local replica ofgates/test.ts: the same test reading, shard script, parser and judge, with bash instead of ssh.The same two tests fail on both:
gfx_clicksandgfx_windowon the C lane. They expect a host without a display. No bench inbench/callsNat.minorNat.max.demos/io_rollback_netcode(server.bend,walkers_test.bend) builds to C and JS.comp.tsis 62258 ttok, under its 64000 cap.Not tested
u64comparison with the same shape asnat_is_ltandnat_sub, but I had no such host to build on.