Narrowing substitution skips lambda bodies and loop `decreases`/`done_with` clauses, leaving the unnarrowed variable in the output
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
- 領域
- compilers
調査の方向性
The gaps are in the walkers in tools/src/transform.ts: mapTExpr (its lambda case returns the expression unchanged), mapTStmt (decreases and doneWith are not mapped for while and forof), and the lowered-IR mapExpr and mapStmt. Start with reproduction 1 or 2 and check the emitted Dafny. Done when the narrowed name is replaced inside lambda bodies, decreases and doneWith, and lambda parameters that shadow it are left alone. Open PR #358 already targets this, so coordinate with it first.
索引モデルが issue の本文から書いたものです。
説明
Problem
When lsc narrows an optional, it replaces the variable with the match binder in the narrowed block, but the replacement skips lambda bodies and loop decreases and done_with clauses. Those places keep the Option-typed variable. Dafny rejects the generated code for lambdas and decreases, and on the Lean backend done_with refers to the unsubstituted variable. Narrowing by Array.isArray in a pure function skips lambdas the same way (reproduction 4). No false proof results; valid TypeScript is rejected.
Reproduced on upstream main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.
Reproduction 1: a lambda reads the narrowed variable
export function closureReadsNarrowed(o: number | undefined, xs: number[]): number[] {
if (o !== undefined) {
return xs.map((x) => x + o);
}
return [];
}
emits
match o {
case Some(i_o_val) =>
seq(|xs|, i_map requires 0 <= i_map < |xs| => var x := xs[i_map]; (x + o))
and Dafny reports Error: type of + must be of a numeric type, ... (instead got Option<int>).
Reproduction 2: a loop decreases reads the narrowed variable
export function whileDecreasesNarrowed(o: number | undefined): number {
if (o !== undefined && o >= 0) {
let i = 0;
while (i < o) {
//@ invariant 0 <= i && i <= o
//@ decreases o - i
i = i + 1;
}
return i;
}
return 0;
}
emits
while (i < i_o_val)
invariant (0 <= i)
invariant (i <= i_o_val)
decreases (o - i)
The guard and invariants use the binder, but decreases keeps o. Dafny reports Error: type of - must be of a numeric type, ... (instead got Option<int>).
Reproduction 3: a loop done_with reads the narrowed variable (Lean backend)
The Dafny backend does not emit done_with, so this affects only --backend=lean.
export function whileDoneWithNarrowed(o: number | undefined): number {
//@ requires o === undefined || o >= 0
if (o !== undefined) {
let i = 0;
while (i < o) {
//@ invariant 0 <= i && i <= o
//@ done_with i === o
i = i + 1;
}
return i;
}
return 0;
}
lsc gen --backend=lean emits
if h_o : (o).isSome = true then
let _o_val := (o).get h_o
...
while i < _o_val
...
invariant invariant_2: (i ≤ _o_val : Prop)
done_with (i = o : Prop)
I did not build the Lean output.
Reproduction 4: a lambda in a function narrowed with Array.isArray
Pure functions lower the narrowed arm first and substitute afterwards with the lowered-IR walker mapExpr, which also skips lambdas:
export function closureReadsSynthUnionPure(xs: number[], u: number | number[]): number[] {
if (Array.isArray(u)) return xs.map((x) => x + u.length);
return [];
}
emits seq(|xs|, i_map requires 0 <= i_map < |xs| => var x := xs[i_map]; (x + |u|)), and Dafny reports Error: size operator expects a collection argument (instead got ArrayOf_number_Or_number).
Expected
The binder substitution should cover lambda bodies and the decreases and doneWith fields of while and forof. In a lambda it should skip any parameter that has the narrowed name.
forof's doneWith is currently dropped when transform lowers the loop to forin (around line 1766 of tools/src/transform.ts), so it will not reach the output even once the walker maps it.
Likely cause
In tools/src/transform.ts, mapTExpr has case "lambda": return e;, and mapTStmt maps invariants and body for while and forof but not decreases or doneWith. replaceVarInTStmts, replaceFieldsInTStmts and replacePathInTStmts all use these walkers.
The lowered-IR walkers in the same file have the same gaps: mapExpr has case "lambda": return e; (around line 60), and mapStmt maps cond, invariants and body for while but not decreasing or doneWith (around line 77). replaceFieldAccess, replaceVarInExpr and replaceExprInExpr, which pure functions use for discriminated-union and Array.isArray narrowing, go through mapExpr.
Related
- #351 (shadowing): once
mapTExprenters lambda bodies, it needs to respect parameters that shadow the narrowed name. - PR #346 changes
replacePathInTStmtsbut does not fix these cases: both reproductions fail the same way on that branch.
- 主要言語
- TypeScript
- スター
- 113
- フォーク
- 13
- 平均マージ
- 15時間 35分
- マージ済み PR(30日)
- 56
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
midspiral/LemmaScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
midspiral/LemmaScript#355 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
midspiral/LemmaScript#362 ·
メンテナーはふだん 1 日以内に返信
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 15/100
midspiral/LemmaScript#361 ·
メンテナーはふだん 1 日以内に返信
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 18/100
midspiral/LemmaScript#360 ·
メンテナーはふだん 1 日以内に返信
-
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 verify対応中かも @namin が今日担当しました。 オープン
難易度 3/5 半日 初心者へのやさしさ 30/100
midspiral/LemmaScript#356 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
midspiral/LemmaScript の issue をすべて見る
似ている issue
-
bug done
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
neavo/LinguaGacha#1124 ·
メンテナーはふだん 1 日以内に返信
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errors対応中かも @fullsend-ai-coder が今日担当しました。 オープンe2e-failure ready-to-code
難易度 2/5 1時間未満 初心者へのやさしさ 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
brylie/mission-island#225 ·
メンテナーはふだん 1 日以内に返信
-
status/need-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
google-gemini/gemini-cli#30412 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信