lemmascript 0.6.5 → 0.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/tools/dist/condition-facts.js +54 -31
- package/tools/dist/dafny-emit.js +40 -4
- package/tools/dist/lean-emit.js +4 -1
- package/tools/dist/lsc.js +2 -2
- package/tools/dist/narrow.js +36 -34
- package/tools/dist/run-log.js +1 -1
- package/tools/dist/transform.js +6 -1
package/package.json
CHANGED
|
@@ -19,15 +19,27 @@
|
|
|
19
19
|
* State is explicit (§6.1): detection that mints binders or consults type
|
|
20
20
|
* declarations takes a `CondCtx`; nothing module-level.
|
|
21
21
|
*/
|
|
22
|
-
import { freshName } from "./names.js";
|
|
22
|
+
import { freshName, freshNameWhere, isUserName } from "./names.js";
|
|
23
23
|
import { unionDeclOfTy, discriminantOf } from "./typedecls.js";
|
|
24
|
+
/** Allocate within this context, retaining a path's name on repeated
|
|
25
|
+
* detection. Semantic-only callers need no registry. */
|
|
26
|
+
function freshBinder(ctx, hint, key) {
|
|
27
|
+
const names = ctx.binders ??= { names: new Map(), used: new Set() };
|
|
28
|
+
const existing = key === undefined ? undefined : names.names.get(key);
|
|
29
|
+
if (existing !== undefined)
|
|
30
|
+
return existing;
|
|
31
|
+
const binder = freshNameWhere(hint, name => isUserName(name) || names.used.has(name));
|
|
32
|
+
names.used.add(binder);
|
|
33
|
+
if (key !== undefined)
|
|
34
|
+
names.names.set(key, binder);
|
|
35
|
+
return binder;
|
|
36
|
+
}
|
|
24
37
|
export function freshOcBinder(ctx) {
|
|
25
|
-
return
|
|
38
|
+
return freshBinder(ctx, `_oc${ctx.oc.n++}_val`);
|
|
26
39
|
}
|
|
27
|
-
/** Pure access paths: var
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
export function binderHintFor(e) {
|
|
40
|
+
/** Pure access paths: the raw var root followed by its field names;
|
|
41
|
+
* null for non-path shapes. */
|
|
42
|
+
function pathParts(e) {
|
|
31
43
|
const fields = [];
|
|
32
44
|
let cur = e;
|
|
33
45
|
while (cur.kind === "field") {
|
|
@@ -36,9 +48,24 @@ export function binderHintFor(e) {
|
|
|
36
48
|
}
|
|
37
49
|
if (cur.kind !== "var")
|
|
38
50
|
return null;
|
|
51
|
+
return [cur.name, ...fields];
|
|
52
|
+
}
|
|
53
|
+
export function binderHintFor(e) {
|
|
54
|
+
const parts = pathParts(e);
|
|
55
|
+
if (!parts)
|
|
56
|
+
return null;
|
|
39
57
|
// \result is stored as the IR var name "\\result"; sanitize for a valid identifier.
|
|
40
|
-
const root =
|
|
41
|
-
return
|
|
58
|
+
const root = parts[0] === "\\result" ? "result" : parts[0];
|
|
59
|
+
return `_${[root, ...parts.slice(1)].join("_")}_val`;
|
|
60
|
+
}
|
|
61
|
+
/** The hint is deliberately readable, not a path identity: `a_b` and `a.b`
|
|
62
|
+
* share it. Key the allocation by the complete path instead. */
|
|
63
|
+
export function freshPathBinder(e, ctx) {
|
|
64
|
+
const hint = binderHintFor(e);
|
|
65
|
+
return hint === null ? null : freshBinder(ctx, hint, JSON.stringify(["path", ...pathParts(e)]));
|
|
66
|
+
}
|
|
67
|
+
export function bindPresentFact(f, ctx) {
|
|
68
|
+
return { ...f, binder: freshPathBinder(f.scrutinee, ctx) };
|
|
42
69
|
}
|
|
43
70
|
/** Detect optional checks: `e !== undefined`, `e === undefined`, `!e`, or a
|
|
44
71
|
* bare optional `e` — for a pure-access-path optional-typed `e`. `!e` and
|
|
@@ -95,29 +122,24 @@ export const bound = (f) => ({ kind: "var", name: f.binder, ty: f.innerTy });
|
|
|
95
122
|
* the tree with that conjunct removed:
|
|
96
123
|
* `(x !== undefined && b) && c` → { check, restCond: b && c }.
|
|
97
124
|
*
|
|
98
|
-
* Search order
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* already-typed path, so hoisting one above the others is observationally
|
|
103
|
-
* neutral. The one thing it does change: the residual now sits inside the
|
|
104
|
-
* match arm, so a conjunct that would have run before a failing check no
|
|
105
|
-
* longer runs at all.
|
|
106
|
-
* Harmless while conditions stay pure — revisit if that ever stops holding. */
|
|
125
|
+
* Search matching conjuncts in source order. A later check may read a field
|
|
126
|
+
* whose root an earlier check unwraps, so the left subtree must be searched
|
|
127
|
+
* before the right operand. Keep the remaining conjuncts in their original
|
|
128
|
+
* relative order. */
|
|
107
129
|
export function extractConjunct(cond, parse) {
|
|
108
130
|
if (cond.kind !== "binop" || cond.op !== "&&")
|
|
109
131
|
return null;
|
|
110
132
|
const left = parse(cond.left);
|
|
111
133
|
if (left)
|
|
112
134
|
return { check: left, restCond: cond.right };
|
|
113
|
-
const right = parse(cond.right);
|
|
114
|
-
if (right)
|
|
115
|
-
return { check: right, restCond: cond.left };
|
|
116
135
|
if (cond.left.kind === "binop" && cond.left.op === "&&") {
|
|
117
136
|
const inner = extractConjunct(cond.left, parse);
|
|
118
137
|
if (inner)
|
|
119
138
|
return { check: inner.check, restCond: { ...cond, left: inner.restCond } };
|
|
120
139
|
}
|
|
140
|
+
const right = parse(cond.right);
|
|
141
|
+
if (right)
|
|
142
|
+
return { check: right, restCond: cond.left };
|
|
121
143
|
if (cond.right.kind === "binop" && cond.right.op === "&&") {
|
|
122
144
|
const inner = extractConjunct(cond.right, parse);
|
|
123
145
|
if (inner)
|
|
@@ -125,9 +147,8 @@ export function extractConjunct(cond, parse) {
|
|
|
125
147
|
}
|
|
126
148
|
return null;
|
|
127
149
|
}
|
|
128
|
-
/** The positive presence fact
|
|
129
|
-
*
|
|
130
|
-
* outermost match, wherever in the chain it was written. */
|
|
150
|
+
/** The first positive presence fact in source order, plus the residual.
|
|
151
|
+
* It becomes the outermost match, keeping later path checks in its arm. */
|
|
131
152
|
export function leadingPresent(cond) {
|
|
132
153
|
return extractConjunct(cond, e => {
|
|
133
154
|
const f = presentFact(e);
|
|
@@ -146,10 +167,9 @@ export function noneDetector(leaf, ctx) {
|
|
|
146
167
|
if (leaf.kind === "binop" && leaf.op === "!==") {
|
|
147
168
|
const oc = leaf.left.kind === "optChain" ? leaf.left : leaf.right.kind === "optChain" ? leaf.right : null;
|
|
148
169
|
if (oc && oc.kind === "optChain" && oc.obj.ty.kind === "optional") {
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
170
|
+
const binder = freshPathBinder(oc.obj, ctx);
|
|
171
|
+
if (binder === null)
|
|
151
172
|
return null;
|
|
152
|
-
const binder = freshName(hint);
|
|
153
173
|
const unwrapped = applyChain({ kind: "var", name: binder, ty: oc.obj.ty.inner }, oc.chain);
|
|
154
174
|
restoreDiscriminantFlag(unwrapped, ctx.decls);
|
|
155
175
|
const lit = leaf.left === oc ? leaf.right : leaf.left;
|
|
@@ -157,8 +177,9 @@ export function noneDetector(leaf, ctx) {
|
|
|
157
177
|
}
|
|
158
178
|
}
|
|
159
179
|
// `!x` / `x === undefined`.
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
180
|
+
const detected = presentFact(leaf);
|
|
181
|
+
if (detected && detected.negated) {
|
|
182
|
+
const f = bindPresentFact(detected, ctx);
|
|
162
183
|
const residual = canBeFalsy(f)
|
|
163
184
|
? { kind: "unop", op: "!", expr: { kind: "var", name: f.binder, ty: f.innerTy }, ty: { kind: "bool" } }
|
|
164
185
|
: null;
|
|
@@ -222,7 +243,7 @@ export function binderHintForMapAccess(m, k, ctx) {
|
|
|
222
243
|
// mHint is `_m_val`, kHint is `_k_val` — stitch into `_m_k_val`.
|
|
223
244
|
const mStem = mHint.replace(/_val$/, "");
|
|
224
245
|
const kStem = kHint.replace(/^_/, "").replace(/_val$/, "");
|
|
225
|
-
return
|
|
246
|
+
return freshBinder(ctx, `${mStem}_${kStem}_val`, JSON.stringify(["map", pathParts(m), pathParts(k)]));
|
|
226
247
|
}
|
|
227
248
|
return freshOcBinder(ctx);
|
|
228
249
|
}
|
|
@@ -342,7 +363,8 @@ export function negVariantFact(cond, ctx) {
|
|
|
342
363
|
/** Reify a presence fact in statement position: the falsy gate (for
|
|
343
364
|
* truthiness checks over falsy-capable inners) tests the bound value inside
|
|
344
365
|
* the Some arm, routing falsy inners to `none`. */
|
|
345
|
-
export function presentMatchStmts(f, some, none) {
|
|
366
|
+
export function presentMatchStmts(f, some, none, ctx) {
|
|
367
|
+
f = bindPresentFact(f, ctx);
|
|
346
368
|
return {
|
|
347
369
|
kind: "someMatch",
|
|
348
370
|
scrutinee: f.scrutinee, binderTy: f.innerTy,
|
|
@@ -352,7 +374,8 @@ export function presentMatchStmts(f, some, none) {
|
|
|
352
374
|
};
|
|
353
375
|
}
|
|
354
376
|
/** Reify a presence fact in expression position; same falsy gate. */
|
|
355
|
-
export function presentMatchExpr(f, some, none, ty) {
|
|
377
|
+
export function presentMatchExpr(f, some, none, ty, ctx) {
|
|
378
|
+
f = bindPresentFact(f, ctx);
|
|
356
379
|
return {
|
|
357
380
|
kind: "someMatch",
|
|
358
381
|
scrutinee: f.scrutinee, binderTy: f.innerTy,
|
package/tools/dist/dafny-emit.js
CHANGED
|
@@ -454,8 +454,10 @@ function emitExpr(e) {
|
|
|
454
454
|
if (ty === "string") {
|
|
455
455
|
if (e.method === "indexOf") {
|
|
456
456
|
needPreamble("StringIndexOf");
|
|
457
|
-
if (args.length === 2)
|
|
458
|
-
|
|
457
|
+
if (args.length === 2) {
|
|
458
|
+
needPreamble("StringSearchPosition");
|
|
459
|
+
return `StringIndexOfFrom(${obj}, ${args[0]}, StringSearchPosition(${obj}, ${args[1]}))`;
|
|
460
|
+
}
|
|
459
461
|
return `StringIndexOf(${obj}, ${args[0]})`;
|
|
460
462
|
}
|
|
461
463
|
if (e.method === "split") {
|
|
@@ -484,8 +486,14 @@ function emitExpr(e) {
|
|
|
484
486
|
return `${obj}[${args[0]}..]`;
|
|
485
487
|
return `${obj}[${args[0]}..${args[1]}]`;
|
|
486
488
|
}
|
|
487
|
-
if (e.method === "endsWith")
|
|
489
|
+
if (e.method === "endsWith") {
|
|
490
|
+
if (args.length === 2) {
|
|
491
|
+
needPreamble("StringSearchPosition");
|
|
492
|
+
needPreamble("StringEndsWithAt");
|
|
493
|
+
return `StringEndsWithAt(${obj}, ${args[0]}, ${args[1]})`;
|
|
494
|
+
}
|
|
488
495
|
return `(|${obj}| >= |${args[0]}| && ${obj}[|${obj}|-|${args[0]}|..] == ${args[0]})`;
|
|
496
|
+
}
|
|
489
497
|
if (e.method === "trim") {
|
|
490
498
|
needPreamble("StringTrim");
|
|
491
499
|
return `StringTrim(${obj})`;
|
|
@@ -508,10 +516,20 @@ function emitExpr(e) {
|
|
|
508
516
|
}
|
|
509
517
|
if (e.method === "includes") {
|
|
510
518
|
needPreamble("StringIndexOf");
|
|
519
|
+
if (args.length === 2) {
|
|
520
|
+
needPreamble("StringSearchPosition");
|
|
521
|
+
return `(StringIndexOfFrom(${obj}, ${args[0]}, StringSearchPosition(${obj}, ${args[1]})) >= 0)`;
|
|
522
|
+
}
|
|
511
523
|
return `(StringIndexOf(${obj}, ${args[0]}) >= 0)`;
|
|
512
524
|
}
|
|
513
|
-
if (e.method === "startsWith")
|
|
525
|
+
if (e.method === "startsWith") {
|
|
526
|
+
if (args.length === 2) {
|
|
527
|
+
needPreamble("StringSearchPosition");
|
|
528
|
+
needPreamble("StringStartsWithAt");
|
|
529
|
+
return `StringStartsWithAt(${obj}, ${args[0]}, ${args[1]})`;
|
|
530
|
+
}
|
|
514
531
|
return `(|${obj}| >= |${args[0]}| && ${obj}[..|${args[0]}|] == ${args[0]})`;
|
|
532
|
+
}
|
|
515
533
|
if (e.method === "charCodeAt")
|
|
516
534
|
return `(${obj}[${args[0]}] as int)`;
|
|
517
535
|
if (e.method === "repeat") {
|
|
@@ -1271,6 +1289,21 @@ function SafeSlice<T>(s: seq<T>, lo: int, hi: int): seq<T>
|
|
|
1271
1289
|
var hi' := NormalizeSliceIndex(|s|, hi);
|
|
1272
1290
|
if hi' < lo' then [] else s[lo'..hi']
|
|
1273
1291
|
}`;
|
|
1292
|
+
const STRING_SEARCH_POSITION = `function StringSearchPosition(s: string, position: int): nat
|
|
1293
|
+
ensures StringSearchPosition(s, position) <= |s|
|
|
1294
|
+
{
|
|
1295
|
+
if position < 0 then 0 else if position > |s| then |s| else position
|
|
1296
|
+
}`;
|
|
1297
|
+
const STRING_STARTS_WITH_AT = `function StringStartsWithAt(s: string, sub: string, position: int): bool
|
|
1298
|
+
{
|
|
1299
|
+
var start := StringSearchPosition(s, position);
|
|
1300
|
+
|sub| <= |s| - start && s[start..start + |sub|] == sub
|
|
1301
|
+
}`;
|
|
1302
|
+
const STRING_ENDS_WITH_AT = `function StringEndsWithAt(s: string, sub: string, position: int): bool
|
|
1303
|
+
{
|
|
1304
|
+
var end := StringSearchPosition(s, position);
|
|
1305
|
+
|sub| <= end && s[end - |sub|..end] == sub
|
|
1306
|
+
}`;
|
|
1274
1307
|
const STRING_INDEX_OF = `function StringIndexOf(s: string, sub: string): int
|
|
1275
1308
|
ensures StringIndexOf(s, sub) == -1
|
|
1276
1309
|
|| (0 <= StringIndexOf(s, sub) <= |s| - |sub| && s[StringIndexOf(s, sub)..StringIndexOf(s, sub) + |sub|] == sub)
|
|
@@ -1524,6 +1557,9 @@ const PREAMBLE_CODE = [
|
|
|
1524
1557
|
["SeqJoin", SEQ_JOIN],
|
|
1525
1558
|
["SafeSlice", SAFE_SLICE],
|
|
1526
1559
|
["StringIndexOf", STRING_INDEX_OF],
|
|
1560
|
+
["StringSearchPosition", STRING_SEARCH_POSITION],
|
|
1561
|
+
["StringStartsWithAt", STRING_STARTS_WITH_AT],
|
|
1562
|
+
["StringEndsWithAt", STRING_ENDS_WITH_AT],
|
|
1527
1563
|
["StringSplit", STRING_SPLIT],
|
|
1528
1564
|
["SeqSort", SEQ_SORT],
|
|
1529
1565
|
["SeqSortBy", SEQ_SORT_BY],
|
package/tools/dist/lean-emit.js
CHANGED
|
@@ -278,8 +278,11 @@ function emitMethodCall(tyKind, method, monadic, obj, args) {
|
|
|
278
278
|
// String methods
|
|
279
279
|
if (tyKind === "string") {
|
|
280
280
|
_needsJSString = true;
|
|
281
|
-
if (method === "indexOf")
|
|
281
|
+
if (method === "indexOf") {
|
|
282
|
+
if (args.length !== 1)
|
|
283
|
+
throw new Error("Lean string.indexOf does not support a position argument");
|
|
282
284
|
return `JSString.indexOf ${obj} ${args[0]}`;
|
|
285
|
+
}
|
|
283
286
|
if (method === "slice")
|
|
284
287
|
return `JSString.slice ${obj} ${args[0]} ${args[1]}`;
|
|
285
288
|
}
|
package/tools/dist/lsc.js
CHANGED
|
@@ -20,7 +20,7 @@ import { emitDafnyFile, emittedNameMap } from "./dafny-emit.js";
|
|
|
20
20
|
import { dafnyGen, dafnyCheckDiff, dafnyVerify, dafnyRegen } from "./dafny-commands.js";
|
|
21
21
|
import { leanGen, leanCheck } from "./lean-commands.js";
|
|
22
22
|
import { runInfo, runTypedInfo } from "./info-command.js";
|
|
23
|
-
import { RunLog, resolveLogDir, runLogEnabled } from "./run-log.js";
|
|
23
|
+
import { RunLog, resolveLogDir, runLogEnabled, safely } from "./run-log.js";
|
|
24
24
|
import { runRuns } from "./runs-command.js";
|
|
25
25
|
import { findUp, loadConfigOptions, parseFileOptions, resolveDafnyArtifactDir, resolveOptions, } from "./config.js";
|
|
26
26
|
/** Version of the lemmascript package — the root package.json sits two levels
|
|
@@ -376,7 +376,7 @@ function runFile(cmd, filePath, backend, timeLimit, extraFlags, noVerify = false
|
|
|
376
376
|
// so an aborted run leaves no snapshot; each helper records the stage it detects.
|
|
377
377
|
const logCmd = cmd === "check" || cmd === "regen" ? cmd : null;
|
|
378
378
|
const log = logCmd && runLogOn
|
|
379
|
-
? new RunLog({ logDir: resolveLogDir(absPath, configFile), cmd: logCmd, sourcePath: absPath, extraFlags, lscVersion: lscVersion
|
|
379
|
+
? new RunLog({ logDir: resolveLogDir(absPath, configFile), cmd: logCmd, sourcePath: absPath, extraFlags, lscVersion: safely(lscVersion) })
|
|
380
380
|
: undefined;
|
|
381
381
|
if (cmd === "gen") {
|
|
382
382
|
dafnyGen(genPath, dfyPath, text);
|
package/tools/dist/narrow.js
CHANGED
|
@@ -34,13 +34,12 @@
|
|
|
34
34
|
* binder counter) threads through the walk; no module-level state.
|
|
35
35
|
*/
|
|
36
36
|
import { isTerminatorKind } from "./typedir.js";
|
|
37
|
-
import { freshName } from "./names.js";
|
|
38
37
|
import { builtinSpec } from "./builtins.js";
|
|
39
|
-
import { presentFact, leadingPresent, leadingIsArray, flattenOr, noneDetector,
|
|
38
|
+
import { presentFact, leadingPresent, leadingIsArray, flattenOr, noneDetector, bindPresentFact, freshPathBinder, binderHintForMapAccess, freshOcBinder, applyChain, restoreDiscriminantFlag, arrayBoundsCond, exprEqual, isArrayFact, typeofStringFact, variantFact, negVariantFact, presentMatchStmts, presentMatchExpr, } from "./condition-facts.js";
|
|
40
39
|
// ── Walkers ──────────────────────────────────────────────────
|
|
41
40
|
function walkExpr(e, ctx) {
|
|
42
41
|
const r = recurseExpr(e, ctx);
|
|
43
|
-
return ruleNullish(r, ctx) ?? ruleNullishIndex(r) ?? ruleOptChainIndex(r) ?? ruleOptChain(r, ctx) ?? ruleImplOptional(r, ctx) ?? ruleImplArrayIsArray(r, ctx) ?? ruleConditionalArrayIsArray(r, ctx) ?? ruleConditionalAndArrayIsArray(r, ctx) ?? ruleConditionalAndOptional(r, ctx) ?? ruleConditionalOptionalSimple(r) ?? ruleConditionalInMap(r, ctx) ?? ruleConditionalOptionalTruthy(r) ?? r;
|
|
42
|
+
return ruleNullish(r, ctx) ?? ruleNullishIndex(r) ?? ruleOptChainIndex(r) ?? ruleOptChain(r, ctx) ?? ruleImplOptional(r, ctx) ?? ruleImplArrayIsArray(r, ctx) ?? ruleConditionalArrayIsArray(r, ctx) ?? ruleConditionalAndArrayIsArray(r, ctx) ?? ruleConditionalAndOptional(r, ctx) ?? ruleConditionalOptionalSimple(r, ctx) ?? ruleConditionalInMap(r, ctx) ?? ruleConditionalOptionalTruthy(r, ctx) ?? r;
|
|
44
43
|
}
|
|
45
44
|
function recurseExpr(e, ctx) {
|
|
46
45
|
const re = (x) => walkExpr(x, ctx);
|
|
@@ -85,7 +84,7 @@ function walkStmt(s, ctx) {
|
|
|
85
84
|
// array rule fires; independent narrows commute, so the order is harmless.)
|
|
86
85
|
// && rules fire before the simple rule because they produce nested ifs whose
|
|
87
86
|
// inner shape doesn't match the simple rule directly.
|
|
88
|
-
return ruleIfAndOptional(r, ctx) ?? ruleIfAndArrayIsArray(r, ctx) ?? ruleIfOptionalSimple(r) ?? ruleExprStmtAndOptional(r, ctx) ?? ruleOptionalIndexBinding(r) ?? r;
|
|
87
|
+
return ruleIfAndOptional(r, ctx) ?? ruleIfAndArrayIsArray(r, ctx) ?? ruleIfOptionalSimple(r, ctx) ?? ruleExprStmtAndOptional(r, ctx) ?? ruleOptionalIndexBinding(r) ?? r;
|
|
89
88
|
}
|
|
90
89
|
function walkStmts(stmts, ctx) {
|
|
91
90
|
const result = [];
|
|
@@ -99,7 +98,7 @@ function walkStmts(stmts, ctx) {
|
|
|
99
98
|
i += tagged.consumed - 1;
|
|
100
99
|
continue;
|
|
101
100
|
}
|
|
102
|
-
const consumed = ruleEarlyReturnOrChain(s, rest, ctx) ?? ruleEarlyReturnConsume(s, rest) ?? ruleEarlyReturnOptChainCompare(s, rest, ctx);
|
|
101
|
+
const consumed = ruleEarlyReturnOrChain(s, rest, ctx) ?? ruleEarlyReturnConsume(s, rest, ctx) ?? ruleEarlyReturnOptChainCompare(s, rest, ctx);
|
|
103
102
|
if (consumed) {
|
|
104
103
|
result.push(walkStmt(consumed, ctx));
|
|
105
104
|
return result;
|
|
@@ -107,7 +106,7 @@ function walkStmts(stmts, ctx) {
|
|
|
107
106
|
// walkStmt first — narrow's expression rules may rewrite the let init from
|
|
108
107
|
// `conditional` to `someMatch`, in which case the let-cond desugar shouldn't fire.
|
|
109
108
|
const walked = walkStmt(s, ctx);
|
|
110
|
-
const expanded = ruleLetCondAndOptional(walked);
|
|
109
|
+
const expanded = ruleLetCondAndOptional(walked, ctx);
|
|
111
110
|
if (expanded) {
|
|
112
111
|
for (const x of expanded)
|
|
113
112
|
result.push(walkStmt(x, ctx));
|
|
@@ -159,7 +158,7 @@ function isTerminating(stmts) {
|
|
|
159
158
|
/** Driver: `if (e !== undefined) then else` — presence fact in if position.
|
|
160
159
|
* → `someMatch e { Some(_e_val) => then, None => else }`. Requires a
|
|
161
160
|
* non-empty Some branch. */
|
|
162
|
-
function ruleIfOptionalSimple(s) {
|
|
161
|
+
function ruleIfOptionalSimple(s, ctx) {
|
|
163
162
|
if (s.kind !== "if")
|
|
164
163
|
return null;
|
|
165
164
|
const check = presentFact(s.cond);
|
|
@@ -169,12 +168,12 @@ function ruleIfOptionalSimple(s) {
|
|
|
169
168
|
const noneBody = check.negated ? s.then : s.else;
|
|
170
169
|
if (someBody.length === 0)
|
|
171
170
|
return null;
|
|
172
|
-
return presentMatchStmts(check, someBody, noneBody);
|
|
171
|
+
return presentMatchStmts(check, someBody, noneBody, ctx);
|
|
173
172
|
}
|
|
174
173
|
/** Driver: `if (e === undefined) terminate; rest` — presence fact in
|
|
175
174
|
* early-return position, consuming the rest of the block into the
|
|
176
175
|
* narrowed scope. Fires when the Some branch is empty. */
|
|
177
|
-
function ruleEarlyReturnConsume(s, rest) {
|
|
176
|
+
function ruleEarlyReturnConsume(s, rest, ctx) {
|
|
178
177
|
if (s.kind !== "if")
|
|
179
178
|
return null;
|
|
180
179
|
if (rest.length === 0)
|
|
@@ -188,7 +187,7 @@ function ruleEarlyReturnConsume(s, rest) {
|
|
|
188
187
|
return null;
|
|
189
188
|
if (!isTerminating(noneBranch))
|
|
190
189
|
return null;
|
|
191
|
-
return presentMatchStmts(check, rest, noneBranch);
|
|
190
|
+
return presentMatchStmts(check, rest, noneBranch, ctx);
|
|
192
191
|
}
|
|
193
192
|
/** Driver: `if (D1 || … || Dn) terminate; rest` — De-Morgan position. Each `Di`
|
|
194
193
|
* that detects some optional `x` is None (`!x`, `x === undefined`,
|
|
@@ -217,7 +216,7 @@ function ruleEarlyReturnOrChain(s, rest, ctx) {
|
|
|
217
216
|
residualLeaves.push(leaf);
|
|
218
217
|
continue;
|
|
219
218
|
}
|
|
220
|
-
const key =
|
|
219
|
+
const key = d.binder; // memoized by full path, not by the ambiguous hint
|
|
221
220
|
if (seen.has(key))
|
|
222
221
|
return null; // two detectors on one optional: rare; leave to other rules
|
|
223
222
|
seen.add(key);
|
|
@@ -259,10 +258,9 @@ function ruleEarlyReturnOptChainCompare(s, rest, ctx) {
|
|
|
259
258
|
return null;
|
|
260
259
|
const lit = c.left === oc ? c.right : c.left;
|
|
261
260
|
const innerTy = oc.obj.ty.inner;
|
|
262
|
-
const
|
|
263
|
-
if (
|
|
261
|
+
const binder = freshPathBinder(oc.obj, ctx);
|
|
262
|
+
if (binder === null)
|
|
264
263
|
return null;
|
|
265
|
-
const binder = freshName(hint);
|
|
266
264
|
const binderVar = { kind: "var", name: binder, ty: innerTy };
|
|
267
265
|
const unwrapped = applyChain(binderVar, oc.chain);
|
|
268
266
|
restoreDiscriminantFlag(unwrapped, ctx.decls);
|
|
@@ -276,7 +274,7 @@ function ruleEarlyReturnOptChainCompare(s, rest, ctx) {
|
|
|
276
274
|
}
|
|
277
275
|
/** Driver (expression): `e !== undefined ? a : b` — presence fact in
|
|
278
276
|
* ternary position. */
|
|
279
|
-
function ruleConditionalOptionalSimple(e) {
|
|
277
|
+
function ruleConditionalOptionalSimple(e, ctx) {
|
|
280
278
|
if (e.kind !== "conditional")
|
|
281
279
|
return null;
|
|
282
280
|
const check = presentFact(e.cond);
|
|
@@ -284,7 +282,7 @@ function ruleConditionalOptionalSimple(e) {
|
|
|
284
282
|
return null;
|
|
285
283
|
const someBody = check.negated ? e.else : e.then;
|
|
286
284
|
const noneBody = check.negated ? e.then : e.else;
|
|
287
|
-
return presentMatchExpr(check, someBody, noneBody, e.ty);
|
|
285
|
+
return presentMatchExpr(check, someBody, noneBody, e.ty, ctx);
|
|
288
286
|
}
|
|
289
287
|
/** Driver (expression): `(path !== undefined [&& rest]) ==> B` — presence
|
|
290
288
|
* fact in implication position (spec premises). The premise's optional
|
|
@@ -312,6 +310,7 @@ function ruleImplOptional(e, ctx) {
|
|
|
312
310
|
const innerBody = restCond
|
|
313
311
|
? { kind: "binop", op: "==>", left: restCond, right: e.right, ty: { kind: "bool" } }
|
|
314
312
|
: e.right;
|
|
313
|
+
check = bindPresentFact(check, ctx);
|
|
315
314
|
return {
|
|
316
315
|
kind: "someMatch",
|
|
317
316
|
scrutinee: check.scrutinee, binderTy: check.innerTy,
|
|
@@ -457,15 +456,14 @@ function ruleConditionalInMap(e, ctx) {
|
|
|
457
456
|
/** Driver (expression): `opt ? a : b` (truthiness — cond itself is optional).
|
|
458
457
|
* Only fires for simple var or simple `obj.field` cond. Historical shape:
|
|
459
458
|
* no falsy gate on the bound value (unlike the other truthiness positions). */
|
|
460
|
-
function ruleConditionalOptionalTruthy(e) {
|
|
459
|
+
function ruleConditionalOptionalTruthy(e, ctx) {
|
|
461
460
|
if (e.kind !== "conditional")
|
|
462
461
|
return null;
|
|
463
462
|
if (e.cond.ty.kind !== "optional")
|
|
464
463
|
return null;
|
|
465
|
-
const
|
|
466
|
-
if (
|
|
464
|
+
const binder = freshPathBinder(e.cond, ctx);
|
|
465
|
+
if (binder === null)
|
|
467
466
|
return null;
|
|
468
|
-
const binder = freshName(hint);
|
|
469
467
|
return {
|
|
470
468
|
kind: "someMatch",
|
|
471
469
|
scrutinee: e.cond, binderTy: e.cond.ty.inner,
|
|
@@ -488,7 +486,7 @@ function ruleIfAndOptional(s, ctx) {
|
|
|
488
486
|
return null;
|
|
489
487
|
const { check, restCond } = extracted;
|
|
490
488
|
const innerIf = { kind: "if", cond: restCond, then: s.then, else: [] };
|
|
491
|
-
return presentMatchStmts(check, [walkStmt(innerIf, ctx)], []);
|
|
489
|
+
return presentMatchStmts(check, [walkStmt(innerIf, ctx)], [], ctx);
|
|
492
490
|
}
|
|
493
491
|
/** Driver: a bare expression statement `x !== undefined && rest` (the
|
|
494
492
|
* `if`-less guard idiom, TS-equivalent to `if (x !== undefined) rest;`).
|
|
@@ -507,14 +505,14 @@ function ruleExprStmtAndOptional(s, ctx) {
|
|
|
507
505
|
return null;
|
|
508
506
|
const { check, restCond } = extracted;
|
|
509
507
|
const innerStmt = { kind: "expr", expr: restCond };
|
|
510
|
-
return presentMatchStmts(check, [walkStmt(innerStmt, ctx)], []);
|
|
508
|
+
return presentMatchStmts(check, [walkStmt(innerStmt, ctx)], [], ctx);
|
|
511
509
|
}
|
|
512
510
|
/** Driver (statement): `let x = (e_opt && rest) ? a : b` — presence fact in
|
|
513
511
|
* conditional-initializer position, where rest may contain method calls.
|
|
514
512
|
* → `var x: T := b; someMatch e_opt { Some(_v) => { if rest { x := a } } }`.
|
|
515
513
|
* Statement-level form is needed because Dafny doesn't allow method calls
|
|
516
514
|
* inside match expression arms. */
|
|
517
|
-
function ruleLetCondAndOptional(s) {
|
|
515
|
+
function ruleLetCondAndOptional(s, ctx) {
|
|
518
516
|
if (s.kind !== "let" || s.mutable)
|
|
519
517
|
return null;
|
|
520
518
|
if (s.init.kind !== "conditional")
|
|
@@ -527,7 +525,7 @@ function ruleLetCondAndOptional(s) {
|
|
|
527
525
|
then: [{ kind: "assign", target: s.name, value: s.init.then }], else: [] };
|
|
528
526
|
return [
|
|
529
527
|
{ kind: "let", name: s.name, ty: s.ty, mutable: true, init: s.init.else },
|
|
530
|
-
presentMatchStmts(check, [assignIf], []),
|
|
528
|
+
presentMatchStmts(check, [assignIf], [], ctx),
|
|
531
529
|
];
|
|
532
530
|
}
|
|
533
531
|
/** Does this expression contain a method call that would be lifted to a
|
|
@@ -593,7 +591,7 @@ function ruleConditionalAndOptional(e, ctx) {
|
|
|
593
591
|
kind: "conditional",
|
|
594
592
|
cond: restCond, then: e.then, else: e.else, ty: e.ty,
|
|
595
593
|
};
|
|
596
|
-
return presentMatchExpr(check, walkExpr(innerCond, ctx), e.else, e.ty);
|
|
594
|
+
return presentMatchExpr(check, walkExpr(innerCond, ctx), e.else, e.ty, ctx);
|
|
597
595
|
}
|
|
598
596
|
// ── Variant drivers (discriminants and synth array-unions) ──
|
|
599
597
|
/** Driver (expression): `Array.isArray(x) ==> B` or `!Array.isArray(x) ==> B` —
|
|
@@ -637,15 +635,14 @@ function ruleConditionalArrayIsArray(e, ctx) {
|
|
|
637
635
|
const matched = pos ?? tof ?? (neg ? { scrutinee: neg.scrutinee, typeName: neg.typeName, variant: "NonArrayBranch" } : null);
|
|
638
636
|
if (!matched)
|
|
639
637
|
return null;
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
const elseBody = positive ? e.else : e.then;
|
|
638
|
+
// The selected variant already accounts for negation. Its arm always runs
|
|
639
|
+
// the source then-branch; swapping branches here would negate the test twice.
|
|
643
640
|
return {
|
|
644
641
|
kind: "tagMatch",
|
|
645
642
|
scrutinee: matched.scrutinee,
|
|
646
643
|
typeName: matched.typeName,
|
|
647
|
-
cases: [{ variant: matched.variant, body: walkExpr(
|
|
648
|
-
fallthrough: walkExpr(
|
|
644
|
+
cases: [{ variant: matched.variant, body: walkExpr(e.then, ctx) }],
|
|
645
|
+
fallthrough: walkExpr(e.else, ctx),
|
|
649
646
|
ty: e.ty,
|
|
650
647
|
};
|
|
651
648
|
}
|
|
@@ -766,12 +763,17 @@ function ruleDiscriminantNegEarlyReturn(stmts, ctx) {
|
|
|
766
763
|
}
|
|
767
764
|
// ── Function / module entry ──────────────────────────────────
|
|
768
765
|
function narrowFunction(fn, ctx) {
|
|
766
|
+
// Contracts and the body bind their payloads in separate generated scopes.
|
|
767
|
+
// Do not reserve a body's names because of a contract's \result (or another
|
|
768
|
+
// function's paths). Keep the shared counter's traversal order unchanged.
|
|
769
|
+
const specCtx = { decls: ctx.decls, oc: ctx.oc };
|
|
770
|
+
const bodyCtx = { decls: ctx.decls, oc: ctx.oc };
|
|
769
771
|
return {
|
|
770
772
|
...fn,
|
|
771
|
-
requires: fn.requires.map(e => walkExpr(e,
|
|
772
|
-
ensures: fn.ensures.map(e => walkExpr(e,
|
|
773
|
-
decreases: fn.decreases ? walkExpr(fn.decreases,
|
|
774
|
-
body: walkStmts(fn.body,
|
|
773
|
+
requires: fn.requires.map(e => walkExpr(e, specCtx)),
|
|
774
|
+
ensures: fn.ensures.map(e => walkExpr(e, specCtx)),
|
|
775
|
+
decreases: fn.decreases ? walkExpr(fn.decreases, specCtx) : null,
|
|
776
|
+
body: walkStmts(fn.body, bodyCtx),
|
|
775
777
|
};
|
|
776
778
|
}
|
|
777
779
|
export function narrowModule(mod) {
|
package/tools/dist/run-log.js
CHANGED
package/tools/dist/transform.js
CHANGED
|
@@ -297,6 +297,8 @@ const OP_MAP = {
|
|
|
297
297
|
"&&": "∧", "||": "∨", "+": "+", "-": "-", "*": "*", "/": "/", "%": "%",
|
|
298
298
|
"==": "=", "!=": "≠", "<==>": "↔",
|
|
299
299
|
};
|
|
300
|
+
/** Relational operators with their operands swapped (`a < b` ≡ `b > a`). */
|
|
301
|
+
const MIRROR_OP = { "<": ">", "<=": ">=", ">": "<", ">=": "<=" };
|
|
300
302
|
/** Bool-valued operators (for code-level conditions needing Decidable). */
|
|
301
303
|
const BOOL_OP_MAP = {
|
|
302
304
|
...OP_MAP, "===": "==", "!==": "!=",
|
|
@@ -557,7 +559,10 @@ function lowerExpr(e, binds) {
|
|
|
557
559
|
const valExpr = valSide.kind === "str" && innerTy.kind === "user"
|
|
558
560
|
? { kind: "constructor", name: valSide.value, type: innerTy.name, args: [] }
|
|
559
561
|
: lowerExpr(valSide, binds);
|
|
560
|
-
|
|
562
|
+
// The bound value goes on the left, so mirror the operator when the
|
|
563
|
+
// optional was on the right: `x < opt` → `Some(v) => v > x`.
|
|
564
|
+
const op = e.left.ty.kind === "optional" ? e.op : MIRROR_OP[e.op] ?? e.op;
|
|
565
|
+
const cmpOp = BOOL_OP_MAP[op] ?? op;
|
|
561
566
|
const noneVal = e.op === "!==" ? true : false;
|
|
562
567
|
const bound = matchBinder("value");
|
|
563
568
|
return {
|