lemmascript 0.6.7 → 0.6.9

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.
@@ -340,13 +340,14 @@ function emitExpr(e, parentPrec) {
340
340
  const head = e.type ? `${e.type}.${leanCtorName(e.name)}` : `.${leanCtorName(e.name)}`;
341
341
  if (!e.args || e.args.length === 0)
342
342
  return head;
343
- const args = e.args.map(a => (a.kind === "binop" || a.kind === "unop" || a.kind === "implies" || a.kind === "app" || a.kind === "methodCall") ? `(${emitExpr(a)})` : emitExpr(a));
343
+ const args = e.args.map(a => (a.kind === "binop" || a.kind === "unop" || a.kind === "implies" || a.kind === "app" || a.kind === "methodCall" || a.kind === "if") ? `(${emitExpr(a)})` : emitExpr(a));
344
344
  return `${head} ${args.join(" ")}`;
345
345
  }
346
346
  case "arrayLiteral":
347
347
  if (e.elems.length === 0)
348
348
  return `#[]`;
349
349
  return `#[${e.elems.map(el => emitExpr(el)).join(", ")}]`;
350
+ case "arrayRange": return `((Array.range ${emitExpr(e.length)}).map Int.ofNat)`;
350
351
  case "tupleLiteral":
351
352
  return `(${e.elems.map(el => emitExpr(el)).join(", ")})`;
352
353
  case "tupleProj": {
@@ -390,7 +391,7 @@ function emitExpr(e, parentPrec) {
390
391
  return emitMethodCall(e.objTy.kind, e.method, e.monadic, receiver, args);
391
392
  }
392
393
  case "lambda": {
393
- const params = e.params.map(p => p.name).join(" ");
394
+ const params = e.params.map(p => e.typedParams ? `(${p.name} : ${tyToLean(p.type)})` : p.name).join(" ");
394
395
  // Single return statement → expression lambda
395
396
  if (e.body.length === 1 && e.body[0].kind === "return") {
396
397
  return `(fun ${params} => ${emitExpr(e.body[0].value)})`;
@@ -517,6 +518,7 @@ function emitExpr(e, parentPrec) {
517
518
  const wrap = e.expr.kind !== "var" && e.expr.kind !== "num" && e.expr.kind !== "bigint";
518
519
  return wrap ? `(${inner}).toNat` : `${inner}.toNat`;
519
520
  }
521
+ case "toInt": return `(Int.ofNat ${emitExpr(e.expr)})`;
520
522
  case "toReal":
521
523
  // A real value reached the Lean backend via coercion (e.g. number `/`).
522
524
  // Same unsupported-real story as the `real` type case in tyToLean.
@@ -546,7 +548,7 @@ function emitExpr(e, parentPrec) {
546
548
  }
547
549
  case "forall": return `∀ ${e.var} : ${tyToLean(e.type)}, ${emitExpr(e.body)}`;
548
550
  case "exists": return `∃ ${e.var} : ${tyToLean(e.type)}, ${emitExpr(e.body)}`;
549
- case "let": return `let ${e.name} := ${emitExpr(e.value)}\n${emitExpr(e.body)}`;
551
+ case "let": return `let ${e.name}${e.type ? ` : ${tyToLean(e.type)}` : ""} := ${emitExpr(e.value)}\n${emitExpr(e.body)}`;
550
552
  default: throw new Error(`Unsupported Lean expression: ${e.kind}`);
551
553
  }
552
554
  }
@@ -570,7 +572,6 @@ function isPropValued(e) {
570
572
  }
571
573
  // ── Statement emission ──────────────────────────────────────
572
574
  function emitStmts(stmts, indent) {
573
- const pad = " ".repeat(indent);
574
575
  return stmts.map(s => emitStmt(s, indent)).join("\n");
575
576
  }
576
577
  function emitStmt(s, indent) {
@@ -890,7 +891,7 @@ function emitPureExpr(e, indent) {
890
891
  return lines.join("\n");
891
892
  }
892
893
  case "let":
893
- return `${pad}let ${e.name} := ${emitExpr(e.value)}\n${emitPureExpr(e.body, indent)}`;
894
+ return `${pad}let ${e.name}${e.type ? ` : ${tyToLean(e.type)}` : ""} := ${emitExpr(e.value)}\n${emitPureExpr(e.body, indent)}`;
894
895
  default:
895
896
  return `${pad}${emitExpr(e)}`;
896
897
  }
package/tools/dist/lsc.js CHANGED
@@ -21,6 +21,7 @@ import { leanGen, leanCheck } from "./lean-commands.js";
21
21
  import { runInfo, runTypedInfo } from "./info-command.js";
22
22
  import { RunLog, resolveLogDir, runLogEnabled, safely } from "./run-log.js";
23
23
  import { runRuns } from "./runs-command.js";
24
+ import { runGuard } from "./guard-command.js";
24
25
  import { SourceProjects } from "./source-projects.js";
25
26
  import { loadConfigOptions, parseFileOptions, resolveDafnyArtifactDir, resolveOptions, } from "./config.js";
26
27
  /** Version of the lemmascript package — the root package.json sits two levels
@@ -78,6 +79,11 @@ function main() {
78
79
  }
79
80
  return;
80
81
  }
82
+ // Guard owns its arguments and is backend-neutral, like extraction.
83
+ if (args[0] === "guard") {
84
+ runGuard(args.slice(1));
85
+ return;
86
+ }
81
87
  // `lsc runs` reads the run log and takes its own flags (`--json`, `--config=`),
82
88
  // so it is dispatched before the file commands' flag parsing.
83
89
  if (args[0] === "runs") {
@@ -163,10 +169,17 @@ function main() {
163
169
  console.error(" lsc info --typed <file.ts> (machine-readable Typed IR contract to stdout)");
164
170
  console.error(" lsc <gen|gen-check|check> [--backend=…] [--slow] (no file: batch over LemmaScript-files.txt)");
165
171
  console.error(" lsc claimcheck [<file.ts>] [flags…] (forwards to lemmascript-claimcheck)");
172
+ console.error(" lsc guard <file.ts> (forwards to lemmascript-guard; Node.js >=22)");
166
173
  console.error(" lsc runs [--json] [--config=path] (what verification caught, from .lemmascript/runs.jsonl)");
167
174
  console.error(" lsc version");
168
175
  process.exit(1);
169
176
  }
177
+ // Validate before loading a file or entering batch mode: a backend mismatch
178
+ // must not turn an unknown command into a successful skipped run.
179
+ if (!["gen", "gen-check", "check", "regen", "extract", "info", "config"].includes(cmd)) {
180
+ console.error(`Unknown command: ${cmd}`);
181
+ process.exit(1);
182
+ }
170
183
  if (typedInfo && cmd !== "info") {
171
184
  console.error(`--typed is only valid with the info command (got: ${cmd})`);
172
185
  process.exit(1);
@@ -1,47 +1,4 @@
1
1
  import { patternCtor, patternBinders, usesName, usesNameInStmts } from "./ir.js";
2
- // ── Generic walkers (same shape as transform.ts) ─────────────
3
- function mapExpr(e, f) {
4
- const hit = f(e);
5
- if (hit)
6
- return hit;
7
- const r = (x) => mapExpr(x, f);
8
- switch (e.kind) {
9
- case "var":
10
- case "num":
11
- case "bigint":
12
- case "bool":
13
- case "str":
14
- case "constructor":
15
- case "emptyMap":
16
- case "emptySet":
17
- case "havoc":
18
- case "default":
19
- case "mapLiteral": return e;
20
- case "binop": return { ...e, left: r(e.left), right: r(e.right) };
21
- case "unop": return { ...e, expr: r(e.expr) };
22
- case "implies": return { ...e, premises: e.premises.map(r), conclusion: r(e.conclusion) };
23
- case "app": return { ...e, args: e.args.map(r) };
24
- case "field": return { ...e, obj: r(e.obj) };
25
- case "toNat": return { ...e, expr: r(e.expr) };
26
- case "toReal": return { ...e, expr: r(e.expr) };
27
- case "index": return { ...e, arr: r(e.arr), idx: r(e.idx) };
28
- case "tupleLiteral": return { ...e, elems: e.elems.map(r) };
29
- case "tupleProj": return { ...e, obj: r(e.obj) };
30
- case "record": return { ...e, spread: e.spread ? r(e.spread) : null,
31
- fields: e.fields.map(fi => ({ ...fi, value: r(fi.value) })) };
32
- case "arrayLiteral": return { ...e, elems: e.elems.map(r) };
33
- case "if": return { ...e, cond: r(e.cond), then: r(e.then), else: r(e.else) };
34
- case "match": {
35
- const scr = r(e.scrutinee);
36
- return { ...e, scrutinee: scr, arms: e.arms.map(a => ({ ...a, body: r(a.body) })) };
37
- }
38
- case "forall": return { ...e, body: r(e.body) };
39
- case "exists": return { ...e, body: r(e.body) };
40
- case "let": return { ...e, value: r(e.value), body: r(e.body) };
41
- case "methodCall": return { ...e, obj: r(e.obj), args: e.args.map(r) };
42
- case "lambda": return e;
43
- }
44
- }
45
2
  /** Detect map.get(k) — returns { obj, key, objTy } if so, else null. */
46
3
  function isMapGet(e) {
47
4
  if (e.kind !== "methodCall" || e.objTy.kind !== "map" || e.method !== "get" || e.args.length !== 1)
@@ -274,18 +231,19 @@ function rewriteChildrenExpr(e) {
274
231
  case "bigint":
275
232
  case "bool":
276
233
  case "str":
277
- case "constructor":
278
234
  case "emptyMap":
279
235
  case "emptySet":
280
236
  case "havoc":
281
- case "default":
282
- case "mapLiteral": return e;
237
+ case "default": return e;
238
+ case "constructor": return { ...e, args: e.args.map(r) };
239
+ case "mapLiteral": return { ...e, entries: e.entries.map(en => ({ ...en, key: r(en.key), value: r(en.value) })) };
283
240
  case "binop": return { ...e, left: r(e.left), right: r(e.right) };
284
241
  case "unop": return { ...e, expr: r(e.expr) };
285
242
  case "implies": return { ...e, premises: e.premises.map(r), conclusion: r(e.conclusion) };
286
243
  case "app": return { ...e, args: e.args.map(r) };
287
244
  case "field": return { ...e, obj: r(e.obj) };
288
245
  case "toNat": return { ...e, expr: r(e.expr) };
246
+ case "toInt": return { ...e, expr: r(e.expr) };
289
247
  case "toReal": return { ...e, expr: r(e.expr) };
290
248
  case "index": return { ...e, arr: r(e.arr), idx: r(e.idx) };
291
249
  case "tupleLiteral": return { ...e, elems: e.elems.map(r) };
@@ -293,6 +251,7 @@ function rewriteChildrenExpr(e) {
293
251
  case "record": return { ...e, spread: e.spread ? r(e.spread) : null,
294
252
  fields: e.fields.map(fi => ({ ...fi, value: r(fi.value) })) };
295
253
  case "arrayLiteral": return { ...e, elems: e.elems.map(r) };
254
+ case "arrayRange": return { ...e, length: r(e.length) };
296
255
  case "if": return { ...e, cond: r(e.cond), then: r(e.then), else: r(e.else) };
297
256
  case "match": {
298
257
  const scr = r(e.scrutinee);