Skip to content

--verdict takes a ~ constant's model from the template's instances, not from a search - #1580

Merged
nicolas-abril merged 7 commits into
bendlang:mainfrom
nicolas-abril:instance-models
Oct 16, 2026
Merged

nicolas-abril merged 7 commits into
bendlang:mainfrom
nicolas-abril:instance-models

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Supersedes #1444. Refs #1182.

Problem

--verdict checks a template root once, with each ~ parameter as an opaque constant k~p. The kernel has no axioms, so each constant needs a closed model of its type. Today the model is searched for: constructors, {==}, projections, in bounded rounds. That search can't prove an injectivity law (#1182's use~F), and it picks models one constant at a time, so later ones may not fit (#1263).

#1444 adds a lookup: any def in the book whose type is definitionally equal. That fixes the reported case, but it makes a root's verdict depend on unrelated defs, and it starts selecting premises by type, the first step of a prover. Each case it misses invites another rule. safe.ts should translate the program, not solve for terms.

Change

A constant's model is the argument that a bend2 instance of the root passes at that parameter (book.tmps, already read as e.inst). It is the user's own term, and bend2 already checked it at that type. All of a root's models come from one instance: inst_pick takes the first instance whose ~ arguments all fit, each checked in the model book at its parameter's type after the ones before it. Each model is checked again at its constant's type. The search builds a model only for a root with no instance that fits, e.g. a law nobody calls.

  • The constant stays opaque in the root, so the root is still checked for every argument, not just the instance's. The kernel checks every model.
  • One call supplies all of a root's constants, so they fit together without backtracking, and the verdict doesn't depend on the order of the calls. use2(~pred, ~inj2, …) in the new test: a built model f := n => Z{} leaves no model for P, and pred plus inj2 fit. In t, ia's g (n => pp(n, n)) uses n twice, so ia doesn't fit, and t takes f, g and P all from ib, in either order.
  • An instance argument is checked as the root's own code. An argument that calls a template (~(n => app(~pred, n))) used to throw a TypeError, which was read as "doesn't fit". It now checks. Only bend2's check errors count as "doesn't fit", and any other error is thrown.
  • def_emit reuses the model chosen in root_emit instead of searching again, and root_emit fits the instance's argument before it searches, so the search runs only for a constant with no fitting argument.
  • Roots share a ~ constant (Roots share their ~ constants, so a template goes to the kernel once instead of once per root that uses it #1358) only where they give it the same model (d913fa4), so a root's models are its own whatever the order of the book. On tests/ that loses a little sharing: the BendTT is 33.70 MB against 33.58 MB on main.
  • The search is unchanged, projection models included. 90a03b6 dropped them and c7c96fb restores them: a template that nothing calls still needs them. tk in the test is an example: its ~p: Pk record needs g := n => n.

Checks

  • New tests/proof/template_inst_model.bend: on main, --verdict gives no model for use~F and no model for use2~P. Here it gives ALL PROOFS CHECK. Running it prints ({==}, {==}, Z{}). It also covers both of @Lorenzobattistela's review cases (use4, and t/ia/ib), plus the uncalled tk. On 90a03b6, t fails with ia first (no model for t~P) and tk fails (no model for tk~p).
  • Every tests/**/*.bend (1,648 files) ran under --verdict with the local Lean 4.34.0 kernel, at d08a415 (5363880 gives the same verdicts):
    • Against 90a03b6: identical verdicts.
    • Against main: identical except the new test and tests/check/safe_parameterized_unit.bend, which goes from out of scope (no model for idt~A; no Base, so no Unit model) to ALL PROOFS CHECK, with A := In from main's call.
  • Gates (macOS, 5363880): repo 54/54, ping 57/57, test 1635/1635, perf 124/124. gates/safe.ts tests: agree 1060, unsafe 93, bend2 rejects 492, out of scope 3, 0 false rejects. safe.ts: 23,978 / 24,000 ttok.
  • tsc: same diagnostics in safe.ts as 90a03b6.

Limits

  • BendTT mismatch for opaque higher-order template with dependent equality result #1182's literal repro defines inj but never calls use, so use~F still has no model and stays out of scope. That is deliberate: nothing in the program says which term stands for F.
  • If an instance's argument is something the kernel can't express (foreign or @unsafe code), its root now goes out of scope where a built model used to reach the kernel. No test in tests/ does this; the hub corpus was not run.
  • If an instance's argument is out of scope, the template is reported out of scope with the argument's reason, for example "a word literal of a parameterized datatype". The verdict doesn't change.
  • Cost: --verdict on demos/app_slash_boss_3d/bend3d.bend takes 0.26 s against 0.28 s on main; nat_literal_no_arithmetic.bend takes 0.12 s against 0.06 s.
  • Not run: the hub corpus, Linux, CUDA.

🤖 Generated with Claude Code

…ot from a search

safe.ts checks a template root once, each ~ parameter an opaque constant
that needs a closed model. The model is now the argument a bend2 instance
of the root passes at that parameter, checked at the constant's type in
the model book, so it fits the constants before it. The search builds a
model only for a root with no instance that fits. The constant stays
opaque in the root, and the kernel checks every model.

A root whose ~ argument no constructor, {==} or projection model proves
(an injectivity proof, bendlang#1182's use~F) now reaches the kernel when a call
instantiates it. Its constants come from one call, so they fit together
without backtracking.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Nice, the instance-as-model idea is clean and the kernel still checks every model. One issue: since #1358, roots share a ~ constant per place and type, and with this PR that shared constant's model comes from whichever root made it first. So a root can lose a model it had on main because of an unrelated template:

def app(~f: N -> N, x: N) -> N: f(x)
def use4(~g: N -> N, ~R: @a: N -> {g(a) == Z{} : N}, a: N) -> {g(a) == Z{} : N}: R(a)
def main() -> N: app(~pred, S{Z{}})

main: ALL PROOFS CHECK; this PR: no model for use4~R (use4g reuses appf at pred). Swapping the order of app and use4 makes it check again. That's the "verdict depends on unrelated defs" problem from #1444.

One fix: put the instance model in the sharing key, so roots share a constant only when they share its model (+9/-6, safe.ts 23,863). It costs some of #1358's sharing when roots pass different arguments. Another option is to keep sharing and redo a root with its own constants when a reused one leaves a later constant without a model. Which do you prefer?

Gates on this PR merged with main: repo 54/54, test 1636, ping 58, perf 124/124, safe 0 false rejects.

nicolas-abril and others added 2 commits October 15, 2026 19:06
Since bendlang#1358 a root reuses the constant an earlier root made at the same
place and type. With instance models, that constant's model came from
the earlier root's instance, so an unrelated template could leave a
later constant without a model (use4~R in the test, from app~f := pred).
The model is now part of the sharing key, so a root's models are its
own whatever the order of the book.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril

nicolas-abril commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks, good catch. I went with the first option, the model in the sharing key (d913fa4, 23,794 ttok). A reused constant then always has the model the root would have given it alone, so a root's verdict doesn't depend on other templates or on the order of the book. The second option re-runs a root when a choice fails, which is the backtracking we're trying to keep out of safe.ts.

  • template_inst_model.bend now includes your app/use4 case: no model for use4~R on the previous head, ALL PROOFS CHECK now.
  • All 1,648 tests/**/*.bend under --verdict: the same verdicts as the previous head. Against main, only the new test and safe_parameterized_unit change.
  • Sharing cost on tests/: BendTT is 33.70 MB against 33.58 MB on main (+0.36%). Four files lose some sharing, e.g. fold_assoc passes Nat.add/add_assoc, so fold goes out once more at fold_assoc's constants.

Could you check the square-packing book from #1358? That's where losing sharing would actually show.

@nicolas-abril
nicolas-abril marked this pull request as draft October 15, 2026 18:13
A projection model took a λ variable before a constructor, for a ~ law
no constant satisfies. With models taken from instances, no test and no
bend-mathlib module that reaches the kernel needs one, on this branch or
on main: every verdict is unchanged without them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril marked this pull request as ready for review October 15, 2026 18:34
@Lorenzobattistela Lorenzobattistela self-assigned this Oct 16, 2026
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Reviewed the new head (90a03b6). The instance-as-model idea works on the PR's own test: on main template_inst_model.bend fails with "no model for useF" / "no model for use2P", and on the PR it passes in the interpreter, JS and C, and --verdict says ALL PROOFS CHECK. d913fa4 fixes the shared ~ constant case from my last comment. Gates pass with main merged in: repo 54/54, test 1636/1636, ping 59/59, perf 124/124, and safe has 0 false rejects on tests/ (safe_parameterized_unit goes from out of scope to agree). I didn't run safe on the hub.

Two problems:

1. The verdict depends on the order of calls. inst_model picks each ~ parameter's model from the first instance that fits on its own (B.Lone()), so one root can mix models from different instances:

import Base

type N is Data:
  Z{}
  S{p: N}

def pred(n: N) -> N:
  match n:
    case Z{}: Z{}
    case S{p}: p

def pp(a: N, b: N) -> N:
  pred(a)

def t(~f: N -> N, ~g: N -> N, ~P: @a: N -> {g(f(a)) == a : N}, a: N) -> {g(f(a)) == a : N}:
  P(a)

def ia() -> {Z{} == Z{} : N}:
  t(~(n => S{n}), ~(n => pp(n, n)), ~(a => {==}), Z{})

def ib() -> {Z{} == Z{} : N}:
  t(~(n => S{S{n}}), ~(n => pred(pred(n))), ~(a => {==}), Z{})

With ia first, t fails with "no model for t~g". Swap ia and ib and t certifies. Main fails both orders. Changing B.Lone() to B.None() (safe.ts:542) makes t certify in both orders, but each parameter can still take its model from a different instance. That contradicts a87c319's "fit together without backtracking". Taking all of a root's models from one instance would avoid the mix, but I haven't tested that.

2. 90a03b6 breaks uncalled templates. Dropping the projection models removed the fallback, so a ~ parameter that no instance fills has no model:

import Base

type N is Data:
  Z{}
  S{p: N}

def pred(n: N) -> N:
  match n:
    case Z{}: Z{}
    case S{p}: p

type Pk is Type:
  Mk{g: N -> N, e: {g(S{Z{}}) == S{Z{}} : N}}

def get(p: Pk, x: N) -> N:
  match p:
    case Mk{g, e}: g(x)

def tk(~p: Pk, x: N) -> N:
  get(p, x)

def main() -> N:
  Z{}

The PR gives "no model for tk~p" and SOME PROOFS FAIL. Main gives ALL PROOFS CHECK. A realistic version is an Iso record's identity law, which the PR now rejects even though it's true. It's a false reject, not unsoundness, but no test covers it.

Smaller things, not blocking:

  • Cost: the model search now runs before the e.consts lookup, and the model's term_key goes into the sharing key. Across the 97 template files, total --verdict time goes up 1.4%. demos/app_slash_boss_3d/bend3d.bend takes 25% longer (keys grow from 5.6k to 1.68M chars), and nat_literal_no_arithmetic.bend (id(~4097n)) goes from 70 to 141 ms. Finite ~ parameters multiply the work: 8 ~b: Bool before two ~ constants runs the search 512 times instead of 4. That's well under the 30 s cap at today's sizes, but caching the model per (k, j, type key) would bring back main's cost.
  • The bare catch {} in inst_model hides a TypeError. When an instance argument contains a template call (use2(~(n => app(~pred, n)), …)), term_check throws undefined is not an object (evaluating 'book.tlds[lhs.def].x') (bend.ts:3320), because c isn't in the book yet. The catch treats it as "doesn't fit". The verdict is the same as on main (out of scope), so it isn't a regression, but the error should be visible.
  • Misleading out-of-scope reason: if an instance argument is out of scope, the template is listed as out of scope too, with the argument's reason (for example "a word literal of a parameterized datatype"). The verdict doesn't change.
  • The PR body is stale. It doesn't mention d913fa4 or 90a03b6.

This reverts 90a03b6. A template nothing calls still needs a model of
each ~ parameter, and some laws hold only of a projection, e.g. a record
whose field g must satisfy {g(S{Z{}}) == S{Z{}}}.
inst_pick takes the first instance of the root whose ~ arguments all
fit, each at its parameter's type after the ones before it, so models
from different instances never mix and the verdict does not depend on
the order of the calls. The check runs as the root's own code, so an
argument that calls a template no longer throws, and only bend2's check
errors count as "does not fit".
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

🤖 Written by an AI agent for @nicolas-abril; not yet reviewed by a human.

Thanks. I pushed c7c96fb and d08a415.

1. Order of calls. All of a root's models now come from one instance, as you suggested. inst_pick takes the first instance whose ~ arguments all fit, each checked at its parameter's type after the ones before it. ia's g (n => pp(n, n)) uses n twice, so it doesn't fit at Lone and ia is skipped as a whole. t takes f, g and P from ib in both orders. I kept Lone rather than None, so the instance has to fit as written. One limit: the instance is picked along its own values of any finite ~ parameters. On the other branches of a finite split, if its argument doesn't fit at a constant, that constant falls back to the search.

2. Uncalled templates. c7c96fb reverts 90a03b6, so projection models are back and tk~p gets Mk{n => n, {==}}.

Both of your cases are now in tests/proof/template_inst_model.bend. I wrote t's P arguments as named defs (pa, pb) so ia and ib stay in scope. On 90a03b6 the file gives no model for t~P (with ia first) and no model for tk~p. At d08a415 it gives ALL PROOFS CHECK.

The bare catch. An instance argument is now checked as the root's own code (def: k) instead of as the not-yet-defined constant. That's where the TypeError came from. use2(~(n => app(~pred, n)), ~inj2, …) now certifies use2 instead of going out of scope. The catch only takes bend2's check errors as "doesn't fit" and rethrows anything else.

Not changed:

  • Cost. bend3d is 0.34 s against 0.28 s on main, and nat_literal_no_arithmetic is 0.13 s against 0.06 s, the same as before. I didn't add the per-(k, j, type) cache. safe.ts is at 23,959 / 24,000 ttok, so there isn't room for it under the cap.
  • The out-of-scope reason for an instance argument that can't go out is still the argument's own. It's now listed under Limits in the PR body.

Checks at d08a415:

  • --verdict on all 1,648 tests/**/*.bend: the same verdicts as 90a03b6. Against main, only the new test and safe_parameterized_unit differ.
  • Gates: repo 54/54, ping 57/57, test 1635/1635, perf 124/124.
  • gates/safe.ts tests: agree 1060, 0 false rejects.
  • tsc: same as before.
  • Not run: hub, Linux, CUDA.

I've updated the PR body.

@nicolas-abril
nicolas-abril merged commit b598221 into bendlang:main Oct 16, 2026
1 check failed
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 20, 2026
nohzafk added a commit to nohzafk/bendlib that referenced this pull request Oct 21, 2026
sorted_cons_cons_elim_le/tail called internal_all_cons_test with a
variable pair head, which --verdict reports as a TypeScript/BendTT
mismatch. internal_sorted_cons_test states the step with the head
written as (x, y), like internal_sorted_cons_true/false.

2.0.38 takes a ~ constant's model from a concrete call of its law
(bendlang/bend#1580), so algebra, order, sort and all now pass
--verdict too: the verdict job checks every module with it and the
kernel-only fallback for them goes. README, SKILL and PLAN F38 follow.
MuhDur added a commit to bendlib/bendlib that referenced this pull request Oct 21, 2026
…nd 2.0.38, every module passes --verdict

Bend 2.0.38 changed Base (List.all/any/find/contains short-circuit through a thunk, String.order
defined directly), so 0.7.3.0 no longer checks on it; PR #26 from @nohzafk re-proved the internal
bridges, with no statement changes. 2.0.38 also includes bendlang/bend#1580, so algebra, order, sort
and all pass `bend --verdict` like the other modules: a --verdict project can import any of them
(issue #25). release.ts now requires a clean --verdict on every module.

Verified from an empty BEND_LIB: importing 0.7.4.0's nat, sort and all prints ALL PROOFS CHECK under
--check-only and --verdict; the README and SKILL examples check against the hub package.

Closes bend-3ev

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NJjQ47v3mvnKmc8XojZitg
Giulio2002 added a commit to Giulio2002/bend-collections that referenced this pull request Oct 28, 2026
…-verdict (#59)

* fix(intrusive list): rename Root's Ready to RootReady, for Bend 2.0.36+

Bend 2.0.36 added `type Poll ... Ready{value} Wait{rest}` to Base
(bendlang/bend#1259), and Base's names are global, so `type Root is Data:
Ready{} Waiting{}` fails with "duplicate declaration: Ready" on 2.0.36 and
2.0.37. That stops the intrusive list's proof, its example and every root
that imports them (laws_containers.bend, proofs/PROOF.bend).

array_adapter.bend is regenerated from array_adapter.src with
tools/generators/mac.py (which reproduces the committed file byte for byte
before the change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* proofs: concrete calls of the generic laws --verdict needs a model for

From bendlang/bend#1580 (on bend main, after 2.0.37), --verdict takes a ~
constant's model from a call of its def at concrete ~ arguments, and no
longer searches for one. Nothing called prepend_valid, remove_head_valid,
optional_suffix, remove_nonhead_valid, prepend_preserves,
remove_head_preserves or remove_nonhead_preserves (at ~reflex) or set_post
(at ~hp) at any, so the kernel never saw them. One call each, at Bool (with
a local bool_eq) and at the empty map with m2 = set(m, key, x), gives them
models; the laws still hold for every argument. On 2.0.37 these calls
check and change nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(proofs): preserve verdict instances in edits source template

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Giulio Rebuffo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants