You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conversion checking normalizes before comparing, so identical large terms overflow the stack #1071
Both files are valid and both fail within a second (bend 2.0.25).
Why
term_compare skips work only when the two sides are the same object. Otherwise it puts both sides in weak head normal form and compares the results. Here that means expanding 2^30 into Succ nodes on each side and comparing them one by one, even though the two sides are the same term before any reduction.
Suggestion
Before normalizing, try the cheap checks that conversion checkers usually try first:
If the two sides are syntactically identical (no unfolding), they are equal.
If both sides apply the same definition, compare the arguments; if they match, the applications are equal without unfolding the definition.
If the heads differ and one of them is a plain wrapper definition (its body is another call), unfold it one step and retry.
Each check only ever answers "equal", and each such answer is a valid conversion step. When a check does not apply, the checker falls back to what it does today. So the set of accepted programs stays the same; only the resource failures above go away. I tried this on a large proof development and it also made checking 2-4x faster overall.
changed the title [-]Checker: faster, smaller conversion checking and a module cache, with the same accepted programs (2-4x time, 1.6-3.4x memory on a 300-module proof)[/-][+]Conversion checking normalizes before comparing, so identical large terms overflow the stack[/+]on Sep 25, 2026
A related case on 2.0.29 (macOS arm64, default stack) where the two sides are not syntactically identical, so the fast paths suggested above would not apply:
$ bun .toolchain/bend2/main.ts normalize.bend
Error: the machine stack overflowed (a deep recursion, or a literal too large to expand)
The same scaled() executed through IO.print(Nat.show(scaled())) prints 1000000.
We hit this while proving closed instances of exact rational position sizing, such as 100 / (10 × 1/10 × 1/10000), where the values reach about a million. Conversion appears to expand both sides into successors. A comparison that evaluates Nat arithmetic on literals natively (binary, as the runtime already does) before falling back to structural comparison would cover this. For now we check such closed instances as executed assertions and keep only universal laws in the checker.
We will not fix this. The checker recurses on the machine stack, so some large terms overflow it; this is a known limit, not a bug. WONTFIX.txt now covers it: "The checker is not optimal: some terms are exponential or overflow (#993)". Please do not open an issue for each stack overflow.
Note: an AI wrote this reply after I made the decision. If anything here is wrong, reply and I will review it myself.
Measured on Bend 2.0.38, Linux x86_64 (the ceiling moves with the host stack, so these are one machine's numbers, not a language limit). Three files differ only in two literals, with up(rows, w, c) -> TA.Mat<Nat.mul(Nat.mul(2n, rows), Nat.mul(2n, w)), c>:
assertion
--check-only
m : TA.Mat<8192n, 16n> <- up(64n, 32n, 16n)
checks
m : TA.Mat<16384n, 32n> <- up(128n, 32n, 32n)
checks
m : TA.Mat<32768n, 32n> <- up(256n, 32n, 32n)
machine stack overflowed
8,192 is comfortable, 16,384 fits on an empty stack, 32,768 does not fit at all.
Two observations these reports do not record, in case they are useful for prioritising:
The identical expression written inline passes where the same expression behind a def alias fails.m : TA.Mat<Nat.mul(Nat.mul(2n, 256n), Nat.mul(2n, 32n)), 32n> <- up(256n, 32n, 32n) checks; with def UP() -> Type: TA.Mat<Nat.mul(Nat.mul(2n, 256n), Nat.mul(2n, 32n)), 32n> and m : UP() <- up(256n, 32n, 32n) it overflows. So unfolding a definition alias is part of the cost, independently of the literal.
The budget is shared across module imports, so splitting a file does not raise the ceiling. A 226-line model file whose types carry 16,384 checks on its own; a 62-line module that imports it and adds three small defs overflows. This is worth knowing because "split the module" is the usual workaround for this family (it worked for a different overflow of ours).
The downstream consequence, if it helps set priority: BendTorch keeps tensor shapes relevant in the types (that relevance is what makes a transposed gradient fail to compile), so a training batch is B*H*W rows. A 64x64 example needs 8,192 rows for 2 images and cannot go to 4 without meeting this ceiling, so the limit is not confined to proof terms.
Workaround for the record: keep the row-level Nats at 8,192 or below and grow channels (which are small: 576, 1152) or drop the resolution (32x32 gives a batch of 8 for the same 8,192 rows).
Activity
[-]Checker: faster, smaller conversion checking and a module cache, with the same accepted programs (2-4x time, 1.6-3.4x memory on a 300-module proof)[/-][+]Conversion checking normalizes before comparing, so identical large terms overflow the stack[/+]Multiplication laws: mul_nat, mul_exact, mul_comm
ajoslin commentedon Sep 27, 2026
A related case on 2.0.29 (macOS arm64, default stack) where the two sides are not syntactically identical, so the fast paths suggested above would not apply:
The same
scaled()executed throughIO.print(Nat.show(scaled()))prints1000000.We hit this while proving closed instances of exact rational position sizing, such as
100 / (10 × 1/10 × 1/10000), where the values reach about a million. Conversion appears to expand both sides into successors. A comparison that evaluates Nat arithmetic on literals natively (binary, as the runtime already does) before falling back to structural comparison would cover this. For now we check such closed instances as executed assertions and keep only universal laws in the checker.VictorTaelin commentedon Sep 27, 2026
We will not fix this. The checker recurses on the machine stack, so some large terms overflow it; this is a known limit, not a bug. WONTFIX.txt now covers it: "The checker is not optimal: some terms are exponential or overflow (#993)". Please do not open an issue for each stack overflow.
Note: an AI wrote this reply after I made the decision. If anything here is wrong, reply and I will review it myself.
Bend 2.0.33: two copies of one term are equal before either unfolds (#…
Bend 2.0.34: a shared graph is compared once (#1071, PR #1151 by Giul…
adeerkhan commentedon Nov 6, 2026
Measured on Bend 2.0.38, Linux x86_64 (the ceiling moves with the host stack, so these are one machine's numbers, not a language limit). Three files differ only in two literals, with
up(rows, w, c) -> TA.Mat<Nat.mul(Nat.mul(2n, rows), Nat.mul(2n, w)), c>:--check-onlym : TA.Mat<8192n, 16n> <- up(64n, 32n, 16n)m : TA.Mat<16384n, 32n> <- up(128n, 32n, 32n)m : TA.Mat<32768n, 32n> <- up(256n, 32n, 32n)8,192 is comfortable, 16,384 fits on an empty stack, 32,768 does not fit at all.
Two observations these reports do not record, in case they are useful for prioritising:
The identical expression written inline passes where the same expression behind a
defalias fails.m : TA.Mat<Nat.mul(Nat.mul(2n, 256n), Nat.mul(2n, 32n)), 32n> <- up(256n, 32n, 32n)checks; withdef UP() -> Type: TA.Mat<Nat.mul(Nat.mul(2n, 256n), Nat.mul(2n, 32n)), 32n>andm : UP() <- up(256n, 32n, 32n)it overflows. So unfolding a definition alias is part of the cost, independently of the literal.The budget is shared across module imports, so splitting a file does not raise the ceiling. A 226-line model file whose types carry 16,384 checks on its own; a 62-line module that imports it and adds three small defs overflows. This is worth knowing because "split the module" is the usual workaround for this family (it worked for a different overflow of ours).
The downstream consequence, if it helps set priority: BendTorch keeps tensor shapes relevant in the types (that relevance is what makes a transposed gradient fail to compile), so a training batch is
B*H*Wrows. A 64x64 example needs 8,192 rows for 2 images and cannot go to 4 without meeting this ceiling, so the limit is not confined to proof terms.Workaround for the record: keep the row-level Nats at 8,192 or below and grow channels (which are small:
576,1152) or drop the resolution (32x32 gives a batch of 8 for the same 8,192 rows).