Skip to content

Conversion checking normalizes before comparing, so identical large terms overflow the stack #1071

Closed as not planned

Description

@Giulio2002

What happens

The checker cannot confirm that a closed term equals an identical copy of itself when the term is large:

import Base

def p() -> {U32.to_nat(1073741824) == U32.to_nat(1073741824) : Nat}:
  {==}
$ bend p.bend --check-only
Error: the machine stack overflowed (a deep recursion, or a literal too large to expand)

The same happens when both sides apply the same function to the same argument:

import Base

def w(x: Nat) -> Nat:
  Nat.add(x, 0n)

def q() -> {w(U32.to_nat(1073741824)) == Nat.add(U32.to_nat(1073741824), 0n) : Nat}:
  {==}

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:

  1. If the two sides are syntactically identical (no unfolding), they are equal.
  2. If both sides apply the same definition, compare the arguments; if they match, the applications are equal without unfolding the definition.
  3. 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.

Activity

  1. 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
  2. ajoslin commented on Sep 27, 2026

    @ajoslin

    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:

    import Base
    
    def scaled() -> Nat:
      (100n * 10000n : Nat)
    
    law exact:
      {scaled() == 1000000n : Nat}
    
    def exact():
      {==}
    
    $ 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.

  3. VictorTaelin commented on Sep 27, 2026

    @VictorTaelin
    Contributor

    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.

  4. added
    wontfix
    on Sep 27, 2026
  5. adeerkhan commented on Nov 6, 2026

    @adeerkhan
    Contributor

    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:

    1. 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.

    2. 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).

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

    wontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions