lemmascript 0.6.5 → 0.6.7

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.
@@ -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, "===": "==", "!==": "!=",
@@ -513,6 +515,22 @@ function lowerExpr(e, binds) {
513
515
  const { premises, conclusion } = flattenImpl(e);
514
516
  return { kind: "implies", premises: premises.map(transformExpr), conclusion: transformExpr(conclusion) };
515
517
  }
518
+ // Boolean connectives evaluate the RHS only if the left operand selects
519
+ // it. Keep lifted calls in that branch instead of hoisting them eagerly.
520
+ if ((e.op === "&&" || e.op === "||") && e.left.ty.kind === "bool" && e.right.ty.kind === "bool") {
521
+ const left = lowerExpr(e.left, binds);
522
+ const rightBinds = binds ? [] : null;
523
+ const right = lowerExpr(e.right, rightBinds);
524
+ if (binds && rightBinds?.length) {
525
+ const name = freshName(`_branch${_branchCounter++}`);
526
+ const value = { kind: "var", name };
527
+ binds.push({ kind: "let", name, type: { kind: "bool" }, mutable: true, value: left });
528
+ binds.push({ kind: "if", cond: e.op === "&&" ? value : { kind: "unop", op: "¬", expr: value },
529
+ then: [...rightBinds, { kind: "assign", target: name, value: right }], else: [] });
530
+ return value;
531
+ }
532
+ return { kind: "binop", op: OP_MAP[e.op], left, right };
533
+ }
516
534
  // Discriminant check: x.discriminant === "foo" → x = .foo (before generic string literal comparison)
517
535
  if ((e.op === "===" || e.op === "!==") && e.left.kind === "field" && e.left.isDiscriminant && e.right.kind === "str") {
518
536
  const objTy = e.left.obj.ty.kind === "user" ? e.left.obj.ty.name : undefined;
@@ -557,7 +575,10 @@ function lowerExpr(e, binds) {
557
575
  const valExpr = valSide.kind === "str" && innerTy.kind === "user"
558
576
  ? { kind: "constructor", name: valSide.value, type: innerTy.name, args: [] }
559
577
  : lowerExpr(valSide, binds);
560
- const cmpOp = BOOL_OP_MAP[e.op] ?? e.op;
578
+ // The bound value goes on the left, so mirror the operator when the
579
+ // optional was on the right: `x < opt` → `Some(v) => v > x`.
580
+ const op = e.left.ty.kind === "optional" ? e.op : MIRROR_OP[e.op] ?? e.op;
581
+ const cmpOp = BOOL_OP_MAP[op] ?? op;
561
582
  const noneVal = e.op === "!==" ? true : false;
562
583
  const bound = matchBinder("value");
563
584
  return {
@@ -1055,12 +1076,25 @@ function lowerExpr(e, binds) {
1055
1076
  // negation forms (`!s` → `s == ""`). Optional conds are already
1056
1077
  // rewritten to someMatch by narrow.ts.
1057
1078
  const cond = lowerExpr(asCondition(e.cond), binds);
1058
- let thenExpr = lowerExpr(e.then, binds);
1059
- let elseExpr = lowerExpr(e.else, binds);
1079
+ const thenBinds = binds ? [] : null;
1080
+ const elseBinds = binds ? [] : null;
1081
+ let thenExpr = lowerExpr(e.then, thenBinds);
1082
+ let elseExpr = lowerExpr(e.else, elseBinds);
1060
1083
  if (e.ty.kind === "optional") {
1061
1084
  thenExpr = wrapOptionalBranch(thenExpr, e.then);
1062
1085
  elseExpr = wrapOptionalBranch(elseExpr, e.else);
1063
1086
  }
1087
+ if (binds && (thenBinds?.length || elseBinds?.length)) {
1088
+ const name = freshName(`_branch${_branchCounter++}`);
1089
+ // Both arms assign before the result is read. This initializer is not
1090
+ // observable and supplies no assumption about the selected result.
1091
+ binds.push({ kind: "let", name, type: e.ty, mutable: true,
1092
+ value: { kind: _opts.backend === "dafny" ? "havoc" : "default", type: e.ty } });
1093
+ binds.push({ kind: "if", cond,
1094
+ then: [...(thenBinds ?? []), { kind: "assign", target: name, value: thenExpr }],
1095
+ else: [...(elseBinds ?? []), { kind: "assign", target: name, value: elseExpr }] });
1096
+ return { kind: "var", name };
1097
+ }
1064
1098
  return { kind: "if", cond, then: thenExpr, else: elseExpr };
1065
1099
  }
1066
1100
  case "optChain":
@@ -1635,6 +1669,8 @@ function transformStmts(stmts, typeDecls) {
1635
1669
  return result;
1636
1670
  }
1637
1671
  let _liftCounter = 0;
1672
+ // Branch results must not renumber the existing lifted-call temporaries.
1673
+ let _branchCounter = 0;
1638
1674
  function liftMethodCalls(e) {
1639
1675
  const binds = [];
1640
1676
  return { binds, expr: lowerExpr(e, binds) };
@@ -2453,6 +2489,7 @@ export function transformModuleDafny(mod) {
2453
2489
  export function transformModule(mod, specImport, moduleBaseOverride) {
2454
2490
  _forofCounters.clear();
2455
2491
  _liftCounter = 0;
2492
+ _branchCounter = 0;
2456
2493
  _typeDecls = mod.typeDecls;
2457
2494
  _neededKindHelpers = new Map();
2458
2495
  _pureDefNames = new Set(mod.functions.filter(f => f.isPure).map(f => f.name));
@@ -2644,6 +2681,8 @@ export function transformModule(mod, specImport, moduleBaseOverride) {
2644
2681
  }
2645
2682
  };
2646
2683
  const knownTypeNames = new Set(typeDecls.map(d => d.name));
2684
+ for (const cls of mod.classes ?? [])
2685
+ knownTypeNames.add(cls.name);
2647
2686
  const allTypeParams = new Set();
2648
2687
  // Exclude type params from the *source* decls — the transformed IR drops
2649
2688
  // them for aliases (`type Step<S, A> = …`), and a generic alias's params
@@ -70,6 +70,18 @@ function tyFromTypeNode(tn) {
70
70
  ? { kind: "optional", inner: { kind: "string", values } }
71
71
  : { kind: "string", values };
72
72
  }
73
+ // Numeric literal unions have the same integer model as number, including
74
+ // in inline annotations and optional types with several numeric arms.
75
+ if (nonNullish.length >= 2 && nonNullish.every(a => {
76
+ if ("syntheticBool" in a)
77
+ return false;
78
+ const ty = tyFromTypeNode(a.node);
79
+ return ty.kind === "int" && !ty.big;
80
+ })) {
81
+ const inner = { kind: "int" };
82
+ return normalized.some(a => !("syntheticBool" in a) && isNullish(a.node))
83
+ ? { kind: "optional", inner } : inner;
84
+ }
73
85
  if (nonNullish.length === 1 && normalized.length >= 2) {
74
86
  const sole = nonNullish[0];
75
87
  const inner = "syntheticBool" in sole ? { kind: "bool" } : tyFromTypeNode(sole.node);
@@ -109,9 +121,13 @@ function tyFromTypeNode(tn) {
109
121
  return { kind: "fn", params, result };
110
122
  }
111
123
  if (Node.isLiteralTypeNode(tn)) {
112
- const lk = tn.getLiteral().getKind();
124
+ const literal = tn.getLiteral();
125
+ const lk = literal.getKind();
113
126
  if (lk === SyntaxKind.TrueKeyword || lk === SyntaxKind.FalseKeyword)
114
127
  return { kind: "bool" };
128
+ if (Node.isNumericLiteral(literal) ||
129
+ (Node.isPrefixUnaryExpression(literal) && Node.isNumericLiteral(literal.getOperand())))
130
+ return { kind: "int" };
115
131
  }
116
132
  switch (tn.getKind()) {
117
133
  case SyntaxKind.NumberKeyword: