Skip to content

Preserve match-refined Data views in copyable lets - #1369

Merged
Lorenzobattistela merged 4 commits into
bendlang:mainfrom
oxura:fix/refined-copy-lets
Oct 7, 2026
Merged

Lorenzobattistela merged 4 commits into
bendlang:mainfrom
oxura:fix/refined-copy-lets

Conversation

@oxura

@oxura oxura commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1349.

A + binder whose type is a match-refined view (for example proof: AtEnd(rest, b) refined to an equality in a match arm) was left out of the kernel text when used twice. tree() in bend2/safe.ts chose how to handle a binder used twice from the declared kind of its type's head (AtEnd ... -> Type), not from the type it reduces to. So it sent the binder to rebuild, which can't rebuild an equality, and --verdict said SOME PROOFS FAIL even though bend2 accepted the program.

Fix: in tree(), reduce the binder's type first. If it reduces to an equality or to Data, the binder copies it (q=2) instead of rebuilding it. This uses the existing q=2 path, and the kernel already checks that a q=2 binder's type is Data, so no new trust is added. There is no special case in let_term. duplicate now goes out as λ+x1 => (x1, x1).

This also covers a refined copy passed to a self-call. After #1368 such a let is rebuilt at each use, so a fix at the let site loses the view there.

Test: tests/proof/refined_proof_copy.bend covers the reported equality pair, the helper call, the consumed-once boundary, a Bool refinement, and the self-call case (walk). It fails on main and checks with this change.

Verification (main f1a69ec, BendTT kernel b9f3e025):

No changes to the kernel, bend2/bend.ts, gates/caps, GUIDE, or CHANGELOG. No GPU cluster run (safe.ts doesn't affect runtime).

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Joint verification with #1368, #1370 and #1371 now passes on upstream 237c7226 / Bend 2.0.36, using a freshly built real Lean 4.34.0 kernel with the new Book map and UTF-8 parser.

#1368 and this PR touch the same let block. The resolved version opens [y, V] before #1368's early deferred-use binding, then retains this PR's typed refined view after normal term elaboration. Both safe regressions give ALL PROOFS CHECK; the let-depth runtime prints 9n.

All 93 existing proof fixtures have identical result classifications before/after the combined fixes: 58 successful, 35 unsuccessful, no new regressions or timeouts. The unchanged repository gate passes 50/50. The deep Array round trip prints ok in source IO CLI, emitted JS on Bun/Node and native CPU output; all 16 Process.run checks stay True. No GPU or mini-cluster run claimed.

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR itself after #1368 merged: merged origin/main (f1a69ec4) without rewriting the branch and applied the already-verified combined let resolution. Both let_put_depth and refined_proof_copy pass the newly built current Lean 4.34 kernel; the actual let-depth run prints 9n, and the unchanged repo gate passes 50/50. This removes the merge conflict without a force-push.

… a let

The let_term special case only covered `+copy = proof`. The root cause is
tree() deciding a doubly-used λ variable's kind from its declared type
instead of the reduced one: when a match refines it to Eql or Data, it is
copyable (q=2), so copy it there. This also covers the self-call copy,
which the let_term special case still rejected (test added).

@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! Your fix was sound and fixed #1349. I pushed one commit on top (7b69f1f) that moves the fix to its root cause, and I updated the description to match.

The cause was in tree(): it chose rebuild from the declared kind of the type's head (AtEnd -> Type), not from the type it reduces to. Now a binder whose type reduces to an equality or Data goes to the existing q=2 copy path, which the kernel already checks. This also fixes a case the let_term version missed: a refined copy passed to a self-call. After #1368 that let is rebuilt at each use and loses the view. I added that case (walk) to your test. It is also smaller (safe.ts +58 tokens vs +153).

Gates on main f1a69ec: repo 50/50, ping 47/47, test 1583/1583, safe gate +1 agree (the new test) and otherwise the same as main, perf the same as main, and the corpus .bendtt are byte-identical except the new test. let_put_depth and the #1326 repros still pass. Approving.

@Lorenzobattistela
Lorenzobattistela merged commit 58de721 into bendlang:main Oct 7, 2026
1 check passed
@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for moving the fix to tree() and adding the self-call case. That is the correct layer: using the reduced binder type also preserves the copy path when recursive let values are rebuilt at each use. I will carry the walk regression forward; the old let_term workaround will not be reintroduced.

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for moving the correction to the root cause in tree() and extending the regression to the refined self-call case. Keeping your follow-up commit intact.

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.

-o/--verdict leave out a def that copies a hypothesis of computed type after a match refined it ("a λ that uses a variable twice") (2.0.35)

2 participants