In a second `for…of` loop with the same loop variable, specs that use `_x_idx2` get an unknown type
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- compilers
Research direction
Start with the forof case in tools/src/resolve.ts, where the loop index is bound for specs, and compare it with the transform code that numbers repeated loops per function as _x_idx, _x_idx2, and so on. The two counters must agree so that a //@ invariant naming _x_idx2 resolves to type nat. Done when the reproduction emits Dafny that verifies and a regression test checks the second loop's counter name and type.
Written by the indexing model from the issue text.
Description
Problem
SPEC.md §4.1 ("For-of loops") says that when several for…of loops use the same variable name, the generated index is disambiguated as _id_idx, _id_idx2, and so on. Resolve binds the index in the loop's environment so that specs such as //@ invariant can refer to it (tools/src/resolve.ts, the forof case). But resolve and transform name the counter separately: resolve gives every such loop's counter the name _x_idx, while transform numbers repeated loops per function and emits _x_idx2 for the second. A spec in the second loop that writes _x_idx2 therefore names a variable resolve never bound, and it gets an unknown type.
The unknown type matters wherever lowering depends on the type. In real arithmetic the counter is not converted with as real, and Dafny rejects the output. No false proof results, but a documented spec does not verify.
Reproduced on upstream main at add6ccd (0.6.10).
Reproduction
export function halfTwice(xs: number[]): number {
let n = 0;
for (const x of xs) {
n = n + 1;
}
let m = 0;
for (const x of xs) {
//@ invariant m * 1.5 === _x_idx2 * 1.5
m = m + 1;
}
return n + m;
}
emits, for the second loop:
var i_x_idx2 := 0;
while i_x_idx2 < |xs|
invariant (i_x_idx2 <= |xs|)
invariant (((m as real) * 1.5) == (i_x_idx2 * 1.5))
dafny verify reports arguments must have comparable types (got real and int) and type of right argument to * (real) must agree with the result type (int).
The same invariant in a function with one loop, //@ invariant m * 1.5 === _x_idx * 1.5, emits ((i_x_idx as real) * 1.5) and verifies.
Expected
Resolve binds the same counter name that transform emits for each loop, so _x_idx2 in the second loop has type nat and lowers like _x_idx in the first. Generating the name once in resolve, recording it on the forof statement, and having transform reuse it removes the disagreement. robertpenner/LemmaScript#37 does this; with it, the reproduction emits ((i_x_idx2 as real) * 1.5) and verifies.
Acceptance criteria
- The reproduction generates Dafny that verifies.
- A regression test checks that resolve gives the second loop's counter the name
_x_idx2with typenat, and that an invariant using it verifies.
- 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