Skip to content

safe.ts: simplify; three small fixes - #1930

Merged
nicolas-abril merged 10 commits into
bendlang:mainfrom
nicolas-abril:refactor/safe-simplify
Oct 29, 2026
Merged

nicolas-abril merged 10 commits into
bendlang:mainfrom
nicolas-abril:refactor/safe-simplify

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Split out of #1896, to merge before it: the parts of that PR that only simplify safe.ts, with nothing about η, a further cleanup pass, and three small fixes the reviews of it found (see Behaviour changes). Meant for a squash merge, so #1896 rebases cleanly after it.

What

New helpers, beside the existing alls, lams and mats:

  • Of<K> for Extract<HTerm, { $: K }>.

  • vr(l), ref(k) and lab(k) for the kernel's Var, Ref and Lab.

  • apps(f, xs) for a head applied to its arguments, each at its quantity. It replaces eight hand-written reduces:

    • root_emit's annotation
    • adt_emit's at
    • tree's convoy and dry match
    • args
    • group_call
    • group_emit
    • arm
  • ann(e, s, o, T, why) for a kernel term annotated at T unless the kernel infers it. spine, let_term and rwt_term each spelled this out, with their own handling of a missing T. With no T, why is the out-of-scope reason.

  • tup(xs) for the tuple (x, y, .., ()). ctr_term builds constructors with it, and group_call builds selectors.

  • wnf(b, T) for B.term_wnf(b, T) (27 uses).

  • fill(b, T, xs) for B.tele_fill(b, T, xs, B.ctx_nil()) (7 uses).

    • Both take the book, so the Model section uses them at e.mb and the rest at e.book.
  • tkey(t) for B.term_key(B.term_lower(t)) (3 uses).

  • node(x): a type guard for whether x is a λ, a match or a refutation, which tree and arg_term each tested by hand.

  • pass(e, s, n, ps, live, qs) for a call's non-specialized arguments, each at its quantity. args and group_call share it.

  • pair_lams(e, q, l, b) for a default arm's λs over its tag and fields.

  • copies(e, q, b, ls): q, or 2 when q is 1 and b uses one of the levels ls twice. This rule is now written once, for lams, pair_lams and let_term.

  • scope_var(s, T, q) binds the next bend2 variable to a new kernel binder. It replaces scope_bind's kb flag and scope_kq. scope_hide(s, q) takes the binder's quantity directly.

  • group_emit builds its selector types with alls and apps, instead of nesting the Σ and ∀ by hand.

  • convoy_bind always ends in a function f(s), which every caller already built, instead of taking a tree and its chain too.

  • A tree's λ goes through lams, which handles the copy, instead of reassigning q.

  • o_show and o_uses walk with an inner closure. o_uses no longer threads its accumulator through its own signature.

  • A binder's refutation and tag go into its scope as it is built, in tree's λ and swi's default.

Reuse and consistency:

  • B.term_strip(t) replaces open(t)[0] (9 uses). safe.ts's open stays for the uses that need the annotation too.
  • A missing entry is x ?? oos(..), as spine already did: item_emit, swi, term's variables and ctr_term.
  • mark is a lookup table, as bend.ts's quant_show is.
  • Locals no longer shadow module functions:
    • safe_pass's oos is now gone;
    • root_emit's pick is now chosen;
    • tree's and rebuild's arm are now h and branch;
    • group_call's args is now pass;
    • arg_term's tree is now bare;
    • let_term's put is now inl.

Other changes:

  • safe_pass keeps the out-of-scope lines as it prints them (string[]), so safe_book's two callers stop formatting [name, why] pairs.

  • group_of is inlined (e.groups.get).

  • args checks once whether k is a name, instead of three times.

  • model finds its fallback def with find.

  • kernel_check trims the kernel's output once.

  • def_emit builds its no-model reason apart from the term, and only when no model is found.

  • tree's λ normalises its domain only when it might rebuild the λ, and only once.

  • closed_val reads a variable's level once, instead of casting it four times.

  • safe_book runs one pass per loop round, with no repeated call in the loop header.

  • def_emit's comment points to the header for how a def with no body goes out, instead of repeating it.

  • Duplicate cases merged: o_show's ∀ and Σ; ctr_term starts its fields with the label; arm calls scope_bind once.

  • One-liners inlined (o_refs, o_uses), .at(-1) for last elements, redundant parentheses and !== undefined && !== "" checks dropped.

  • Comments:

    • Scope and Safe comment each field in place, as bend.ts's TermOf does, instead of one run-on list. Scope gains d, which the list left out.
    • the Model section describes what the search does, in the file's voice, instead of giving instructions;
    • those that repeat the header are shortened;
    • tele_open's garbled sentence is rewritten;
    • qsig_eq loses a clause that restated the rest;
    • Safe's list now names the undo log and is rewrapped.
  • Types: five generic constructors (B.Var, B.App) are typed HTerm, so safe.ts type-checks under bend2/pack's tsconfig.json. On main it has 5 errors there, and the rest of bend2/ is unchanged at 7.

Behaviour changes

No file in tests/ reaches any of these; the output on all of them is unchanged (see Checks).

  • kernel_check tests SOME PROOFS FAIL against the trimmed output, as it already did ALL PROOFS CHECK. Output with leading whitespace before the failure line used to be an infrastructure error; it is now a verdict.
  • A rewrite whose evidence the kernel cannot infer, and which bend2 left with no type, is out of scope (a rewrite's evidence with no known type). On main it went out bare, the kernel rejected it, and --verdict reported a false reject.
  • swi's default arm at q=0 no longer adds its tag to the refutations, as tree's λ already did not. Its tag and fields stay at q=0 instead of becoming q=2 when the body uses one twice.

Checks

  • -o on all 1794 files of tests/, main (4f99f4c) against this branch: the same BendTT and the same messages on every file.
  • bunx tsc -p bend2/pack: no errors in safe.ts.
  • bun gates/_run.ts: repo 54 / 54, test 1781 / 1781, perf 122 / 124 (pass), ping 80 / 81 (pass). safe.ts goes from 23996 to 23289 ttok.
  • bun gates/safe.ts tests: the same table as main: agree 1143, unsafe 141, bend2 rejects 507, out of scope 3, false reject 0, timeout 0, infrastructure 0.

🤖 Generated with Claude Code

An Of<K> alias for Extract<HTerm, { $: K }>, and helpers beside the
ones there: vr(l) for the kernel variable, apps(f, xs) for a head at its
arguments (eight hand-written reduces), tup(xs) for a constructor's or a
selector's tuple, ann(e, s, o, T) for a term annotated unless the
kernel infers it (spine, let_term, rwt_term), wnf(e, T), fill(e, T, xs)
for B.tele_fill at no context, and tkey(t) for a closed term's key.
group_emit's selector types go through alls and apps. B.term_strip(t)
stands for open(t)[0], and a missing entry is `?? oos(..)`. quant and
mark are tables, as bend.ts's quant_show is. Locals no longer shadow
module functions (oos, pick, arm, args, tree, put).

safe_pass keeps the out-of-scope lines as it prints them, group_of is
inlined, args checks whether k is a name once, a few duplicate cases are
merged (o_show's ∀ and Σ, ctr_term's label, arm's scope_bind), and
one-liners are inlined. Scope and Safe comment each field in place of
one run-on list, the Model comment describes instead of instructing,
redundant parentheses go, and comments that repeat the header or read
garbled are rewritten. Five generic constructors are typed HTerm, so
safe.ts type-checks under bend2/pack's tsconfig.

-o emits the same BendTT, with the same messages, on all 1794 files of
tests/. safe.ts goes from 23996 to 23451 ttok.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril and others added 7 commits October 29, 2026 09:57
… kernel variable, for scope_kq and scope_bind's flag; the stack stands for going
…lab and node for the kernel's Ref, Lab and a case tree's node; arm always substitutes
…he trimmed output for both verdicts; copies is the one copy rule

- swi's Efq default emits its λ pair at q again, as on main, not through pair_lams
- kernel_check tests SOME PROOFS FAIL on the trimmed output, as it does ALL PROOFS CHECK
- tree's λ takes its domain's weak head once, and only when it would rebuild
- def_emit builds its no-model reason only when no model is found
- item_ref checks a live cycle in the going set again, not a scan of the stack
- wnf and fill take the book, so the Model section uses them at e.mb
- ann's why is required; null makes the bare fallback explicit (rwt_term)
- copies(e, q, b, ls) holds the copy rule of lams, pair_lams and let_term

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril self-assigned this Oct 29, 2026
nicolas-abril and others added 2 commits October 29, 2026 13:52
…node narrows; no shadowed locals

- pair_lams copies a default's tag and fields whenever b uses either twice, at any q, as swi did before copies
- quant is the ternary again, so a quantity it does not know reads as Many
- node is a type guard
- arg_term's lam is bare: a λ, a match, a refutation or a constructor
- scope_hide copies kq only when it sets a quantity
- o_uses' inner kept, apps' reduce f and model's find k no longer shadow

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…=0 default's tag refutes nothing and stays erased

- rwt_term gives ann a reason, so evidence the kernel cannot infer and bend2 left
  unannotated is out of scope, not emitted bare for the kernel to reject;
  ann's why is a plain string again
- swi's default refutes with its tag only at q > 0, as tree's λ does
- pair_lams copies through copies at its own q, so a q=0 tag and fields stay erased

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril changed the title safe.ts: simplify, same output safe.ts: simplify; three small fixes Oct 29, 2026
@nicolas-abril
nicolas-abril merged commit fd6bc77 into bendlang:main Oct 29, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the refactor/safe-simplify branch October 29, 2026 14:14
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.

1 participant