Lean backend: a closure over a variable that changes is translated as a copy, so a false ensures proves
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- backend-api-design, compilers
Research direction
Start at the Lean backend code that translates closures and captured bindings; the issue does not name its file, so trace the --backend=lean path from the compiler entry point. Reproduce the reassigned-variable and mutated-array cases, then compare generated Lean and proof results with JavaScript. Done means generation either rejects unsafe captures with a useful error or preserves their JavaScript behavior, and zero-parameter arrows are handled or rejected clearly.
Written by the indexing model from the issue text.
Description
Summary
With --backend=lean, a closure that captures a variable which changes after the closure is made is translated as a copy of the variable's value at that point. The generated Lean builds, and velvet_vcgen proves a postcondition that JavaScript falsifies, while the true postcondition fails to prove. lsc gen, lsc check and lake build all report success.
Versions
lemmascript (npm), lsc version |
0.6.6 |
| LemmaScript | tag v0.6.6 (736fb7ce5ef89555b566bdbcca0c8fe057fbb205) |
| Velvet | branch lemma2 (22ed7829f4a90e786eed9b193d29fe46e61fe949) |
| Mathlib | 5ed2965256430c3649e86755f9576b54eca72435 (LemmaScript's manifest) |
| Lean / Lake | 4.34.0 / 5.0.0-src+293d5d0, Linux x86-64 |
Reproduction 1: a let reassigned after a closure reads it
//@ ensures \result === 2
export function late(): number {
let n = 1;
const f = (x: number): number => x + n;
n = 5;
return f(1);
}
In JavaScript, late() returns 6: f reads n when it's called, and n is 5 by then.
lsc gen --backend=lean late.ts exits 0 and writes:
method late returns (res : Int)
ensures ensures_1: (res = 2 : Prop)
do
let mut n : Int := 1
let f := (fun x => x + n)
n := 5
return f 1
f closes over the value n had at let f, which is 1. With
prove_correct late by
velvet_vcgen [late] with finish
lake build succeeds, so the false ensures \result === 2 is proved. With the true //@ ensures \result === 6, the same proof fails with finish failed.
Reproduction 2: an array mutated after a closure reads it
//@ ensures \result === 1
export function stale(): number {
const a: number[] = [1];
const len = (k: number): number => a.length + k;
a.push(2);
return len(0);
}
JavaScript returns 2. The generated Lean is:
method stale returns (res : Int)
ensures ensures_1: (res = 1 : Prop)
do
let mut a : Array Int := #[1]
let len := (fun k => a.size + k)
a := Array.push a 2
return len 0
With prove_correct stale by velvet_vcgen [stale] with finish, lake build succeeds and \result === 1 is proved.
Reproduction 3: the change happens through a call or an alias
function push1(a: number[]): void { a.push(1); }
//@ ensures \result === 0
export function viaCall(): number {
const arr: number[] = [];
const f = (k: number): number => arr.length + k;
push1(arr);
return f(0);
}
JavaScript returns 1. The generated Lean passes arr to push1 by value, and f has copied it anyway:
method viaCall returns (res : Int)
ensures ensures_1: (res = 0 : Prop)
do
let mut arr : Array Int := #[]
let f := (fun k => arr.size + k)
let _t0 ← push1 arr
_ := _t0
return f 0
With prove_correct push1 by velvet_vcgen [push1] with finish and prove_correct viaCall by velvet_vcgen [viaCall, push1] with finish, the build succeeds and === 0 is proved. === 1 fails with finish failed.
Through an alias the result is the same: const b = arr; b.push(1); in place of the call generates let mut b : Array Int := arr and b := Array.push b 1, and === 0 is proved while JavaScript returns 1.
Related: shapes that generate but don't build
lsc gen --backend=lean also exits 0 on these. The generated Lean is rejected later, with no TypeScript-level message:
| TypeScript | Lean error |
|---|---|
let c = 0; const bump = (x: number): number => { c = c + x; return c; }; return xs.map(bump).length; |
Variable c cannot be mutated. Only variables declared using let mut can be mutated |
export function counter(): () => number { let n = 0; return () => { n = n + 1; return n; }; } |
`unexpected token '=>'; expected '[', '{', ' |
any zero-parameter arrow, for example const len = (): number => a.length; |
the same: it's emitted as fun => ... with no binder |
A pure closure over a const primitive (const k = 3; xs.map(x => x + k)) generates Lean that builds and means the same as the TypeScript.
Expected
Either:
lsc gen --backend=leanrefuses a closure that captures a binding which is reassigned, or an array or object that is mutated (directly, through an alias, or by a call), with an error naming the closure and the variable; or- it translates the capture so that the Lean has JavaScript's meaning: in reproduction 1,
ensures \result === 6proves andensures \result === 2doesn't.
The first seems the smaller change, and the safer one while the translation is unproved.
Separately, a zero-parameter arrow should be emitted as fun () => ... (or refused), rather than producing Lean that doesn't parse.
How it was run
lsc gen --backend=lean late.ts # exit 0
lake build # package requiring LemmaScript by path, roots «late.def» and «late.proof»
node -e 'function late(){ let n = 1; const f = (x) => x + n; n = 5; return f(1); } console.log(late())' # 6
- Dominant language
- TypeScript
- Stars
- 113
- Forks
- 13
- Avg merge
- 15h 35m
- Merged PRs (30d)
- 56
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from midspiral/LemmaScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
midspiral/LemmaScript#355 · 1 comment ·
Maintainers usually reply within 1 day
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
midspiral/LemmaScript#362 ·
Maintainers usually reply within 1 day
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
midspiral/LemmaScript#361 ·
Maintainers usually reply within 1 day
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 18/100
midspiral/LemmaScript#360 ·
Maintainers usually reply within 1 day
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
midspiral/LemmaScript#356 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in midspiral/LemmaScript
Similar issues
-
bug done
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neavo/LinguaGacha#1124 ·
Maintainers usually reply within 1 day
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPossibly taken @fullsend-ai-coder claimed this today. Opene2e-failure ready-to-code
Difficulty 2/5 Under an hour Newbie friendliness 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
brylie/mission-island#225 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google-gemini/gemini-cli#30412 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day