Repository navigation
safe.ts: a ~ argument's run-time Data variables are its instance's captures (#1483) - #1959
Lorenzobattistela wants to merge 1 commit into
Conversation
…ptures (#1483) A template argument that names a run-time variable used to send its def out of scope. Each run-time variable of a Data type that the argument's normal form names is now a capture: the instance takes it first, at q=2, and the call passes the caller's variable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Verified against current main ( I could not run Blocker: repo-gate fails. Merged with current main, Please:
Possible simplifications: replace the Happy to help with a token-diet patch if useful. |
nicolas-abril
left a comment
There was a problem hiding this comment.
Review of bend2/safe.ts. I ran small probe programs through --verdict on both this branch and the base (9d514f3). The PR's own test and the other probes (dependent captures, a template with a finite ~Bool parameter, match-field captures, a linear variable that is captured and also used live in both match arms) all reach ALL PROOFS CHECK. One correctness bug is confirmed.
Correctness
1. A captured variable that the kernel erased is passed live at q=2, and the kernel rejects the def (safe.ts:1218)
A capture is always passed at q=2, even when the caller's kernel binder for it is erased (q=0). If the capture is used in live code, for example inside a template def's body, it becomes a live q=2 use of an erased variable.
def V(~n: Nat, g: Box<n> -> Nat) -> Data: {g(B{0n}) == 0n : Nat}
def W(~m: Nat -> Nat, -h: Nat) -> Data: V(~Nat.add(h, m(0n)), b => 0n)
def W_ok(j: Nat, -k: Nat) -> W(~(z => j), k): {==}
On main, --verdict reports this as out of scope ("a template argument that depends on a run-time value"). On this branch it reports In W.qv: affine live code, calls that descend plus the TS/kernel mismatch message, so bend2 accepts a book the kernel rejects.
Suggested fix: in closed_val, raise Open_Error when the captured Bind's kernel level has kq 0 and the call is live, or don't capture such variables.
2. A leftover \0capk reference can make a value look closed when it isn't (safe.ts:523)
An enclosing capture (a Ref \0capk found in s.cap) is re-captured only if kind(e, s, C) === 2. Otherwise cap returns undefined and leaves the raw Ref \0capk in the value. The closedness check (mentions on Vars) doesn't see it, so the value counts as closed while still using the caller's capture numbering.
If kind() can't resolve the capture's type inside an instance (for example fill/wnf throws and the catch returns null), the callee's key and cols contain \0capk. The callee then reads it as its own k-th capture, or as an unknown name when it has fewer captures, and two callers whose k-th captures differ would share one instance. Fix: always re-capture a Ref found in s.cap (it was Data when it was admitted), or treat a leftover \0cap Ref as not closed.
Design and fragility
3. Captures stored as a property on the cols array are lost on copy (safe.ts:1210)
Object.assign(array, { caps }) / Cols = Array & { caps? }: every .slice, .map or spread silently drops caps, but item_key and def_emit depend on them for the instance's identity and its binders. arm (cols.slice(tld.x)), group_call (ps.slice(0, n).map(...)) and def_emit (cols.slice(0, tld.x)) already copy cols. If a future path copies cols before item_ref/item_key/item_emit (the todo queue, Reroute, group re-emission), the instance would be keyed without its capture types and emitted without capture binders, while callers still prepend [2, cap] arguments. That's a kernel arity mismatch or a key collision. Passing the captures as an explicit [Cols, caps] pair or as a separate item_ref parameter would be more robust.
4. Group calls and root_emit's G fall back to Open_Error instead of using the mechanism (safe.ts:1207)
Any capturing ~ argument into a mutually recursive base def (e.g. the String.cmp/String.cmp.fin family) is still out of scope. Roots with a finite ~ parameter whose type captures lose their λ-match check, and the arms name the instances bare. group_emit could take the captures first at q=2 exactly as def_emit does, and G could capture its own Data ~ parameters. That would remove both special cases.
5. scope_move doesn't update s.cap (safe.ts:732)
scope_move updates s.c, empty and tags when a convoy moves a kernel variable, but not the s.cap Binds, so a moved capture would keep its stale o. Nothing triggers this today: captures are q=2 and the convoy (cv0) only collects q=1 levels. But if those rules change (q=2 binders riding a dependent match, or tentative quantities for captures), term on Ref \0capj would return the old vr(l) and produce an ill-scoped instance. Mapping s.cap through mv in scope_move is cheap insurance.
6. Later parameter types depend on whether an earlier argument captured (safe.ts:1199)
U = F.B(cs.length > 0 ? x : v ?? x) switches every later parameter type to the raw x as soon as any earlier ~ argument captured, even for arguments that captured nothing. Later F.A (used by arg_term, eta_finite, all_of) is then built from a different term shape depending on an unrelated earlier argument, so e.fin/e.keys caching and η decisions depend on argument order. Since v is only the callee-side key, always using F.B(x) for the caller-side telescope seems simpler.
Performance and cleanup
7. Extra normalization pass on every closed_val call (safe.ts:535)
closed_val now always runs a second subst pass (term_lower, traversal, term_higher) after term_snf, including every kind() call where nothing can be captured, and on each call it re-lowers and re-scans every capture type already collected. kind() is on hot paths in tree (each λ with a reused variable, data(l) per variable per match), and a call with several ~ arguments becomes quadratic in the capture checks. Suggestion: return nf(x) immediately when cs === undefined || !s.cap, and run mentions on each capture type once, when it's pushed.
8. kind() is recomputed for each occurrence of an uncaptured variable (safe.ts:523)
A non-Data variable that appears n times in a ~ argument pays n kind computations (wnf + fill + closed_val), and again on each Reroute retry. A local Map<Bind, boolean> inside closed_val would cache it.
9. Shared mutable hit counter (safe.ts:518)
hit is shared across the mutually recursive cap and cl closures just to decide whether to re-normalize, and nested cl(b.T) calls inside the outer subst bump the outer counter as a side effect. Having cap return [term, changed] (or comparing cs.length plus a local flag) would be clearer. The o.$ === "Var" && i >= 0 && i < s.d predicate is also duplicated from nf and could be factored out.
Fixes #1483.
A template argument that names a run-time variable, as in
List.all(~&2, ~Nat, ~(y => Nat.is_le(lo, y)), xs)inside a type, sent the def out of scope ("a template argument that depends on a run-time value"), so every lemma over such a type was unverifiable.closed_valnow lifts those variables: each run-time variable of a Data type (or an enclosing capture) that the argument's normal form names becomes\0cap<j>, numbered by first place. The instance takes the captures first, at q=2 (def_emit), its key includes their types (item_key), and the call passes the caller's variables (args). Function captures stay out of scope, since the kernel's q=2 binders need a Data type. root_emit's G keeps the old Open_Error fallback (cap: undefined), and a group call with captures is Open_Error.~b, a capture the normal form drops, captures passed on between nested instances. Out of scope on main, ALL PROOFS CHECK under--verdicthere.-ois byte-identical to main on every existing test.internal_leastback in itsList.allform: 16 defs out of scope on main, ALL PROOFS CHECK here.Gates on 9d514f3: test PASS 1784 / 1784; safe agree 1145 (main 1144), false reject 0, timeout 0; repo FAIL only on
bend2/safe.ts: 24637 > 24000 ttok(main 23,993).Supersedes #1489.
🤖 Generated with Claude Code