Skip to content

Nat.read("7") in a checked equality never finishes: every digit is compared against a unary 2^48 - 1 #1008

Closed

Description

@jasisz

What I did

import Base

def seven() -> {Nat.read("7") == Some{7n} : Maybe<&2, Nat>}:
  {==}
bun bend2/main.ts seven.bend --check-only

What happened

After two minutes the check had still not finished, using a full core. Evaluating Nat.read("7") with term_snf hangs the same way. U32.read("7") evaluates in about 0.2 s, and the compiled program prints 7 immediately.

Why it happens

Nat.read.go guards every digit with

Nat.read.fit(acc, Nat.divmod(Nat.sub(Nat.read.max(), U32.to_nat(d)), 10n))

where Nat.read.max() is Nat.mul(U32.to_nat(16777215), U32.to_nat(16777217)), that is, 2^48 − 1. The checker's Nat is unary, so computing that bound and dividing it by ten means building and walking about 2.8 × 10^14 successors, and this happens for every digit, including the first one of "7". The runtime lanes do this with native integers, which is why the program itself is instant.

As a result, no law that mentions Nat.read on a concrete string can be closed by evaluation. For example, {Nat.read(Nat.show(n)) == Some{n}} cannot be checked at n = 7n, and a law about a parser built on Nat.read cannot be tested on an example.

A related surprise: the same bound makes Nat.read return None{} for any input above 2^48 − 1 (Nat.read("281474976710656") is None{} on the runtime too). So Nat.read(Nat.show(n)) == Some{n} is false for large n, even though Nat is unbounded. Neither the bound nor the checker cost is documented next to Nat.read.

Expected behavior

The check should finish quickly on small inputs. One way would be for the overflow guard to compare the digit count, or to use a bound that is cheap to evaluate in unary, rather than dividing 2^48 − 1 on every step. If the 2^48 − 1 cap is intended, a comment next to Nat.read saying so would help readers of laws that use it.

Version

bend 2.0.26
commit 6a77e124
Bun 1.4.2
Darwin arm64

Activity

  1. yalexey-jcode commented on Sep 23, 2026

    @yalexey-jcode

    I resolved a similar situation involving the verification of a program operating on u32 values ​​by replacing them with a word of arbitrary bit-width. Compliance with the specification can be proven in the general case; mapping this to a concrete type is a separate task.

  2. added a commit that references this issue on Sep 25, 2026
    7819652
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions