lemmascript 0.6.14 → 0.6.16

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.
@@ -9,7 +9,7 @@
9
9
  * and record-field assignments) always propagate back to the owning lexical
10
10
  * storage variable instead of mutating a discarded copy or match binder.
11
11
  */
12
- import { extendFieldPlace, extendUnwrapPlace, extendVariantPlace, makeVarPlace, } from "./places.js";
12
+ import { extendFieldPlace, extendUnwrapPlace, makeVarPlace, } from "./places.js";
13
13
  export function makeUnwrapFrame(binder, rootVar, fields) {
14
14
  let place = makeVarPlace(rootVar);
15
15
  for (const f of fields)
@@ -17,10 +17,6 @@ export function makeUnwrapFrame(binder, rootVar, fields) {
17
17
  place = extendUnwrapPlace(place);
18
18
  return { kind: "unwrap", binder, rootVar, fields, place };
19
19
  }
20
- export function makeVariantFrame(unionVar, typeName, ctorName, fields) {
21
- const place = extendVariantPlace(makeVarPlace(unionVar), ctorName);
22
- return { kind: "variant", unionVar, typeName, ctorName, fields, place };
23
- }
24
20
  /** True for in-place mutating collection methods modeled by functional write-back. */
25
21
  export function isMutatingCollectionMethod(recvTy, method) {
26
22
  if ((recvTy.kind === "map" || recvTy.kind === "set") &&
@@ -32,22 +28,30 @@ export function isMutatingCollectionMethod(recvTy, method) {
32
28
  }
33
29
  return false;
34
30
  }
35
- /** Build a nested record-update expression rooted at `rootVar` for `fields`
36
- * (length >= 1), setting the innermost field to `newVal`. */
37
- export function buildNestedFieldExpr(rootVar, fields, newVal) {
31
+ /** Build a nested record-update expression rooted at `base` for `fields`
32
+ * (length >= 1), setting the innermost field to `newVal`. Optional
33
+ * `unionsByDepth` pins the union constructor/type at each nesting depth `i`
34
+ * whose owner is a discriminated union (for collision-renamed destructors in
35
+ * Dafny and match-updates in Lean). */
36
+ export function buildNestedFieldExpr(base, fields, newVal, unionsByDepth) {
38
37
  const baseFor = (len) => {
39
- let cur = { kind: "var", name: rootVar };
38
+ let cur = base;
40
39
  for (let i = 0; i < len; i++) {
41
- cur = { kind: "field", obj: cur, field: fields[i], datatypeField: true };
40
+ const u = unionsByDepth?.[i];
41
+ const unionTag = u ? { fromUnion: u.ctorOf, ctor: u.ctor } : {};
42
+ cur = { kind: "field", obj: cur, field: fields[i], ...unionTag, datatypeField: true };
42
43
  }
43
44
  return cur;
44
45
  };
45
46
  let curVal = newVal;
46
47
  for (let i = fields.length - 1; i >= 0; i--) {
48
+ const u = unionsByDepth?.[i];
49
+ const unionTag = u ? { ctor: u.ctor, ctorOf: u.ctorOf } : {};
47
50
  curVal = {
48
51
  kind: "record",
49
52
  spread: baseFor(i),
50
53
  fields: [{ name: fields[i], value: curVal }],
54
+ ...unionTag,
51
55
  };
52
56
  }
53
57
  return curVal;
@@ -58,7 +62,7 @@ function optionSome(value) {
58
62
  /**
59
63
  * Given a variable `mutatedTarget` that was just assigned in the lowered IR,
60
64
  * walk `frames` from innermost to outermost and emit the cascading write-back
61
- * assignments that synchronize every enclosing narrowing binder and the owning
65
+ * assignments that synchronize every enclosing `someMatch` binder and the owning
62
66
  * lexical storage variable.
63
67
  */
64
68
  export function emitWriteBacksForTarget(mutatedTarget, frames) {
@@ -66,7 +70,7 @@ export function emitWriteBacksForTarget(mutatedTarget, frames) {
66
70
  let current = mutatedTarget;
67
71
  for (let i = frames.length - 1; i >= 0; i--) {
68
72
  const frame = frames[i];
69
- if (frame.kind === "unwrap" && frame.binder === current) {
73
+ if (frame.binder === current) {
70
74
  const wrapped = optionSome({ kind: "var", name: frame.binder });
71
75
  if (frame.fields.length === 0) {
72
76
  out.push({ kind: "assign", target: frame.rootVar, value: wrapped });
@@ -75,21 +79,10 @@ export function emitWriteBacksForTarget(mutatedTarget, frames) {
75
79
  out.push({
76
80
  kind: "assign",
77
81
  target: frame.rootVar,
78
- value: buildNestedFieldExpr(frame.rootVar, frame.fields, wrapped),
82
+ value: buildNestedFieldExpr({ kind: "var", name: frame.rootVar }, frame.fields, wrapped),
79
83
  });
80
84
  }
81
85
  current = frame.rootVar;
82
- continue;
83
- }
84
- if (frame.kind === "variant" && frame.fields.some(f => f.binder === current)) {
85
- const reconstructed = {
86
- kind: "app",
87
- fn: frame.ctorName,
88
- ctorOf: frame.typeName,
89
- args: frame.fields.map(f => ({ kind: "var", name: f.binder })),
90
- };
91
- out.push({ kind: "assign", target: frame.unionVar, value: reconstructed });
92
- current = frame.unionVar;
93
86
  }
94
87
  }
95
88
  return out;
@@ -98,8 +91,7 @@ export function emitWriteBacksForTarget(mutatedTarget, frames) {
98
91
  export function findActiveUnwrapFrame(rootVar, fields, frames) {
99
92
  for (let i = frames.length - 1; i >= 0; i--) {
100
93
  const frame = frames[i];
101
- if (frame.kind === "unwrap" &&
102
- frame.rootVar === rootVar &&
94
+ if (frame.rootVar === rootVar &&
103
95
  frame.fields.length === fields.length &&
104
96
  frame.fields.every((f, idx) => f === fields[idx])) {
105
97
  return frame;
@@ -107,54 +99,23 @@ export function findActiveUnwrapFrame(rootVar, fields, frames) {
107
99
  }
108
100
  return null;
109
101
  }
110
- /** Resolve a source dotted path like `["b", "count"]`, `["u", "v"]`, or
111
- * `["u", "box", "count"]` through any enclosing `unwrap` and `variant` frames.
112
- * Returns:
113
- * - `currentRoot`: the deepest active binder/variable reached,
114
- * - `remainingFields`: any field steps remaining below `currentRoot`,
115
- * - `leafUnwrapFrame`: if the entire path ended at an active `unwrap` frame
116
- * (so non-optional writes update `leafUnwrapFrame.binder` and cascade,
117
- * while `undefined`/optional writes update `leafUnwrapFrame.rootVar`),
118
- * - `leafVariantBinder`: if the entire path ended directly at a union
119
- * variant field binder (e.g. `u.v` -> `i_u_v`). */
102
+ /** Resolve a source dotted path like `["b", "count"]` through any enclosing
103
+ * `someMatch` unwrap frames. */
120
104
  export function resolveWriteTargetThroughFrames(rootVar, fields, frames) {
121
105
  let currentRoot = rootVar;
122
106
  let idx = 0;
123
107
  let lastUnwrap = null;
124
- let lastVariantBinder = null;
125
- // Iteratively step through active `unwrap` (0-field or prefix-field) and
126
- // `variant` (1-field) frames until no more frames match.
127
108
  while (true) {
128
- // 1. Bare unwrap on `currentRoot` (e.g. `b` -> `_b_val`, or `u` -> `_u_val`).
109
+ // 1. Bare unwrap on `currentRoot` (e.g. `b` -> `_b_val`).
129
110
  const bareUnwrap = findActiveUnwrapFrame(currentRoot, [], frames);
130
111
  if (bareUnwrap) {
131
112
  currentRoot = bareUnwrap.binder;
132
113
  lastUnwrap = bareUnwrap;
133
- lastVariantBinder = null;
134
114
  continue;
135
115
  }
136
116
  if (idx >= fields.length)
137
117
  break;
138
- // 2. Variant field projection on `currentRoot` (e.g. `u.v` -> `i_u_v`, `u.box` -> `i_u_box`).
139
- let matchedVariant = null;
140
- for (let i = frames.length - 1; i >= 0; i--) {
141
- const frame = frames[i];
142
- if (frame.kind === "variant" && frame.unionVar === currentRoot) {
143
- const hit = frame.fields.find(f => f.fieldName === fields[idx]);
144
- if (hit) {
145
- matchedVariant = hit.binder;
146
- break;
147
- }
148
- }
149
- }
150
- if (matchedVariant) {
151
- currentRoot = matchedVariant;
152
- idx += 1;
153
- lastUnwrap = null;
154
- lastVariantBinder = matchedVariant;
155
- continue;
156
- }
157
- // 3. Field-prefix unwrap on `currentRoot` (e.g. `_b_val.items` -> `_b_val_items_val`).
118
+ // 2. Field-prefix unwrap on `currentRoot` (e.g. `_b_val.items` -> `_b_val_items_val`).
158
119
  let matchedLen = 0;
159
120
  let matchedUnwrap = null;
160
121
  for (let len = fields.length - idx; len >= 1; len--) {
@@ -170,7 +131,6 @@ export function resolveWriteTargetThroughFrames(rootVar, fields, frames) {
170
131
  currentRoot = matchedUnwrap.binder;
171
132
  idx += matchedLen;
172
133
  lastUnwrap = matchedUnwrap;
173
- lastVariantBinder = null;
174
134
  continue;
175
135
  }
176
136
  break;
@@ -179,7 +139,6 @@ export function resolveWriteTargetThroughFrames(rootVar, fields, frames) {
179
139
  currentRoot,
180
140
  remainingFields: fields.slice(idx),
181
141
  leafUnwrapFrame: idx === fields.length ? lastUnwrap : null,
182
- leafVariantBinder: idx === fields.length ? lastVariantBinder : null,
183
142
  };
184
143
  }
185
144
  /** Collect every variable name assigned anywhere in lowered IR `Stmt[]`