Skip to content

fix(verdict): lift Data captures in type-only templates - #1489

Closed
oxura wants to merge 2 commits into
bendlang:mainfrom
oxura:fix/1483-type-template-captures-20261011
Closed

oxura wants to merge 2 commits into
bendlang:mainfrom
oxura:fix/1483-type-template-captures-20261011

Conversation

@oxura

@oxura oxura commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Addresses #1483; still draft.

A predicate used in a type can capture a runtime value without turning it into a global constant. Each instance now owns its typed captures in cols, and its cache key names them by position. This fixes the collision between an outer capture and a local at the same frontend level.

The root ~ probe keeps its closed-instance fallback. Live runtime capture lifting still raises Open_Error; closing an argument also checks for leftover free variables.

The new dependent-proof regression failed in BendTT before and now checks. The full local safe corpus has 1,721 files: 1,111 agree, 492 frontend rejections, 115 unsafe, 3 out of scope, and no false rejects, timeouts or infrastructure errors.

Not ready to merge: safe.ts is 25,029 / 24,000 tokens. No cap changes. The mutually recursive capture-group case is not qualified yet.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 22, 2026
@Lorenzobattistela
Lorenzobattistela force-pushed the fix/1483-type-template-captures-20261011 branch from 56977b6 to a44617d Compare October 22, 2026 20:30

@Lorenzobattistela Lorenzobattistela left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. #1483 still reproduces on main, and the approach of lifting captures into kernel parameters is the right direction. I rebased the branch onto main (2e9722d): safe.ts conflicted with #1225, #1580, #1666 and #1704, so please pull before you push again. I ran the gates on the rebase and probed --verdict. These must change before merge:

  1. The kernel can check a different statement from the one bend2 checked. A capture's name is keyed by (level, type) across the whole pass, so inside a lifted instance a local variable at the same level and type gets the same name as an outer capture:

    def T(f: Nat -> Bool, h: Nat, xs: List<&2, Nat>) -> Data:
    {List.all(
    &2, ~Nat, ~(y => Bool.and(f(y), Nat.is_le(y, h))), xs) == True{} : Bool}

    def thm(lo: Nat, h: Nat) -> T(~(y => Nat.is_le(lo, y)), h, []):
    {==}

    lo and h both become \0capture0, and the emitted predicate is is_le(x0, y) && is_le(y, x0), so lo is gone. Please name captures by their position in the instance's cols instead. That also removes the duplicate instances: least and shifted in your test emit byte-identical List.all instances.

  2. This adds a new false reject. Main passes this with --verdict, and the PR fails it:

    law refl_all:
    for b: Bool
    for xs: List<&2, Bool>
    {List.all(
    &2, ~Bool, (y => Bool.and(y, b)), xs) == List.all(&2, ~Bool, ~(y => Bool.and(y, b)), xs) : Bool}

    def refl_all(b, xs):
    {==}

    spec_val no longer throws Open_Error, so root_emit's fallback (#1225: when G calls a template at one of k's ~ parameters, the arms name the instances bare) never fires. G lifts the law's own ~b as a capture, and the kernel cannot equate the lifted recursive instance with the closed one. Please keep the Open_Error path there, or don't lift a root's own ~ parameters.

  3. safe.ts is now 25,001 ttok, and its cap is 24,000. Main is at exactly 24,000, so the PR has to fit inside the current budget.

  4. tests/check/template_type_capture.bend fails the test gate: bend2 also prints Use --verdict for mathematical validity., so the file needs that second #| line. Note that this test prints the same thing on main, so the test gate cannot catch a regression here; only gates/safe.ts tells main and the PR apart.

Should change:

  1. spec_val no longer re-checks that the closed argument is closed. A variable with a value (s.c[i].v) is replaced by that value without closing the variables free inside it, so an open Var can reach an instance's cols. Before, this case went out of scope cleanly. Please re-run the mentions check after close.

  2. args lifts captures in live positions too. The WONTFIX text ("does not admit runtime captures in live template applications") then rests only on bend2's checker. Please keep the oos(..., Open_Error) when live is true.

  3. The wrap comment in tests/check/spec_finite_match.bend is stale: its emission changes on this PR. The same goes for the Open_Error comment and the Scope/Safe field comments in safe.ts. The code lifts any typed variable, not only Data, so "Data captures" in the header and in WONTFIX overstates it.

Minor: capkeys and captypes are not in Trail/undo. Once a pass has one capture, every item reference re-walks its cols in spec_caps. Nothing tests the group (mutual recursion) capture path.

Gates on the rebase: ping 73/73; perf 122/124, the same as main; safe has 0 false rejects on tests/ (finding 2 is not in the suite).

@oxura

oxura commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — pulled a44617d. I’m fixing the capture-name collision and the root ~ fallback first. I’ll keep the 24k cap and the live-capture restriction.

@oxura
oxura marked this pull request as draft October 22, 2026 22:36
@oxura

oxura commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

The collision and root ~ fallback are fixed. Captures now belong to each instance's cols; memo keys normalize their names by slot, with no pass-global capture tables to survive undo. The dependent-proof regression fails before the fix and checks in BendTT after it. Full local safe corpus: 1,721 files, zero false rejects/timeouts/infrastructure errors.

Still 25,029/24,000 tokens, so I put this back in draft. I tried retaining open ~ arguments as generic kernel parameters instead, but that runs into the kernel's affine function/quantity rules. I'm keeping the checked explicit-capture version rather than weakening the kernel. The mutual-group case still needs qualification too.

@oxura

oxura commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Checked the mutual case through a real temporary Prelude, not a constructed Book. Both the captured predicate and a fully closed control stop at no live parameter ... leads; I’m not counting that as capture-group coverage. Supporting that case needs template-prefix handling in group detection.

The ordinary capture regression and the 1,721-file corpus pass, but this stays draft at 25,029/24,000. The generic-parameter alternative failed the actual kernel, so I dropped it rather than weaken the check. Would you rather hold this until there’s room, or is there a simplification you’d prefer here? I’ll leave the cap alone.

@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Closing in favor of #1959, which fixes #1483 with the same idea (lift the template argument's run-time captures into the instance's parameters) but numbers captures by position inside closed_val, so it needs no pass-global maps and stays much smaller. It also covers the two issues from the earlier review here (capture name collisions and the ~b root fallback). Thanks for the groundwork on this one.

@oxura

oxura commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for carrying the idea forward in #1959. Positional capture numbering is a better fit than the global maps here.

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