Skip to content

Without import Base, safe.ts writes a Nat literal above 4096 as the book's own Nat.add(Nat.mul(q, 4096), r), so --verdict passes a book whose certified main is 1n while bend2 runs it to 4097n #1330

Closed

Description

@Vacrin

What you did

bend nat_tpl.bend (run, --check-only, --verdict, -o k.bendtt), then appended {main == 1n} and {main == 4097n} to k.bendtt and ran bendtt on each

What happened

$ bend nat_tpl.bend
4097n
$ bend nat_tpl.bend --check-only
ALL PROOFS CHECK
Use --verdict for mathematical validity.
$ bend nat_tpl.bend --verdict
ALL PROOFS CHECK
$ bend nat_tpl.bend -o k.bendtt          (exit 0, nothing out of scope; excerpt)
id.qv_ : Nat =
  (Nat.add (Nat.mul (.Succ, ((.Zero, ()), ())) <4096 as .Succ/.Zero>) (.Succ, ((.Zero, ()), ())))
main : Nat =
  id.qv_

Appended to k.bendtt, then bendtt k.bendtt:
chk_main_is_1 : {main == (.Succ, ((.Zero, ()), ())) : Nat} = {==}
  -> ALL PROOFS CHECK (exit 0)
chk_main_is_4097 : {main == <4097 as .Succ/.Zero> : Nat} = {==}
  -> SOME PROOFS FAIL
     In chk_main_is_4097:
     expected: main
     observed: (.Succ, ((.Succ, …        (exit 1)

bend2 runs main to 4097n; the certified book proves main == 1n.
With `import Base` in place of the local Nat, Nat.add and Nat.mul, the same id/main certifies main == 4097n.

The file

type Nat is Data:
  Zero{}
  Succ{pred: Nat}

def Nat.mul(a: Nat, b: Nat) -> Nat:
  a

def Nat.add(a: Nat, b: Nat) -> Nat:
  a

def id(~n: Nat) -> Nat:
  n

def main() -> Nat:
  id(~4097n)

bend --version

2.0.35 (source checkout of main at 565d7fd, bun bend2/main.ts)

uname -sm

Linux x86_64 (WSL2)

clang --version (the first line)

No response

term's Lit case in safe.ts splits a Nat literal above NAT_MAX (4096) through two bare book keys
(safe.ts#L843-L849;
safe.ts:869-875 on main, unchanged):

if (x.k === "Nat" && x.v > NAT_MAX) {
  // a long Nat is q * NAT_MAX + r, by base's Nat.mul and Nat.add
  const [q, r] = [Math.floor(x.v / NAT_MAX), x.v % NAT_MAX];
  const mul = B.App(B.App(B.Ref("Nat.mul"), B.Lit("Nat", q)), B.Lit("Nat", NAT_MAX));
  return term(e, s, B.App(B.App(B.Ref("Nat.add"), mul), B.Lit("Nat", r)), live);
}

The comment says "base's", but nothing checks it. With import Base anywhere, the keys are Base's, because
parse_fresh refuses a redeclaration. A file without import Base may declare its own Nat (CHANGELOG
2.0.18), and then its own Nat.add and Nat.mul take those keys. bend2 never calls them for a literal.
check-lit takes a literal by name only for Base's Nat
(bend.ts#L3559;
the #941 fix in 2.0.25), and otherwise it unfolds the literal structurally. So bend2 reads 4097n as 4097,
while the kernel reads Nat.add(Nat.mul(1, 4096), 1), which is 1 here.

The literal reaches the split raw by two routes. The first is a ~ argument: the instance is keyed on the
raw argument
(bend.ts#L3726),
and safe.ts rebuilds the arguments from that key. The second is a def's or law's type. The type route was
run on both versions:

  • def q1() -> {gt(5000n, 800n) == True{} : Bool}: {==} (with Nat.add(-a, b) = b) passes --verdict,
    certified as a claim about gt(904, 800).
  • {gt(5000n, 1000n) == True{}}, and def main() -> {big == 4097n : Nat} where big is 4097n, pass
    --check-only and MISMATCH on --verdict.

With no Nat.add in the book, id(~4097n) passes --check-only and MISMATCHes on --verdict, and -o
says main: an unknown name Nat.add.

A bend2-side oracle written with a literal does not catch it. def oracle() -> {main() == 4097n : Nat}: {==}
passes --verdict, because its type goes out through the same split.

Suggested fix (not tested). Split only when e.book.tlds["Nat.add"]?.b === true && e.book.tlds["Nat.mul"]?.b === true
(and Nat), as check-lit and the foreign-def check already do. Otherwise, emit the unary chain that
lit_step gives, or put the def out of scope with a reason. #1329 is the same missing guard at
model_at's Unit.

This is not a consistency problem: the kernel checked the text it was given, and a Base-free book has no
Base Empty.

Activity

  1. added
    bug
    on Oct 4, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions