tla-ts 0.0.4-dev.b254db0 → 0.0.4-dev.ca40dfe

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.
Files changed (207) hide show
  1. package/README.md +31 -10
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/deciders.cjs +27 -27
  5. package/dist/deciders.js +34 -34
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +12 -14
  8. package/dist/index.d.ts +12 -14
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +37 -27
  15. package/dist/prove.js +39 -29
  16. package/dist/run.cjs +45 -0
  17. package/dist/run.js +45 -0
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +76 -86
  21. package/dist/transfer.js +73 -83
  22. package/dist/worker.cjs +6 -6
  23. package/dist/worker.js +6 -6
  24. package/package.json +1 -1
  25. package/src/ast.ts +2 -2
  26. package/src/bags.ts +22 -23
  27. package/src/builtins.ts +16 -8
  28. package/src/check.ts +131 -1154
  29. package/src/{cli-options.ts → cli/options.ts} +54 -9
  30. package/src/cli/run.ts +63 -0
  31. package/src/cli/shared.ts +106 -0
  32. package/src/cli/tlapm.ts +374 -0
  33. package/src/cli/tlc.ts +941 -0
  34. package/src/cli/xml-exporter.ts +221 -0
  35. package/src/cli.ts +1 -1
  36. package/src/codegen.ts +17 -15
  37. package/src/config.ts +5 -5
  38. package/src/dependencies.ts +4 -6
  39. package/src/environment.ts +55 -60
  40. package/src/errors.ts +12 -36
  41. package/src/evaluate.ts +81 -101
  42. package/src/exit-codes.ts +1 -1
  43. package/src/fingerprints.ts +3 -3
  44. package/src/instances.ts +56 -46
  45. package/src/java-overrides.ts +79 -41
  46. package/src/json.ts +31 -36
  47. package/src/levels.ts +10 -13
  48. package/src/{live-check.ts → liveness-checker.ts} +169 -142
  49. package/src/liveness.ts +38 -34
  50. package/src/obligations/action.ts +9 -45
  51. package/src/obligations/{anon.ts → anonymize.ts} +13 -25
  52. package/src/obligations/equality.ts +260 -0
  53. package/src/obligations/expression.ts +42 -517
  54. package/src/obligations/generate.ts +88 -778
  55. package/src/obligations/index.ts +20 -22
  56. package/src/obligations/levels.ts +36 -41
  57. package/src/obligations/module.ts +88 -708
  58. package/src/obligations/normalize.ts +554 -0
  59. package/src/obligations/operators.ts +176 -0
  60. package/src/obligations/pragma.ts +21 -15
  61. package/src/obligations/printer.ts +81 -188
  62. package/src/obligations/proof.ts +0 -1
  63. package/src/obligations/regroup.ts +100 -0
  64. package/src/obligations/simplify.ts +668 -0
  65. package/src/obligations/source.ts +25 -131
  66. package/src/obligations/{deref.ts → subexpression.ts} +48 -56
  67. package/src/obligations/substitution.ts +261 -0
  68. package/src/obligations/temporal.ts +28 -26
  69. package/src/obligations/tuples.ts +37 -53
  70. package/src/parallel-worker.ts +37 -33
  71. package/src/parallel.ts +30 -24
  72. package/src/parser.ts +67 -74
  73. package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
  74. package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
  75. package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
  76. package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
  77. package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
  78. package/src/prove/decide-pool.ts +7 -12
  79. package/src/{obligations/pickle.ts → prove/first-order.ts} +79 -2
  80. package/src/prove/isabelle/decide.ts +3 -6
  81. package/src/prove/isabelle/encode.ts +35 -93
  82. package/src/prove/isabelle/isar/inner-syntax.ts +1 -1
  83. package/src/prove/isabelle/isar/method.ts +3 -3
  84. package/src/prove/isabelle/isar/type-check.ts +1 -1
  85. package/src/prove/isabelle/provers/blast/blast.ts +1 -1
  86. package/src/prove/isabelle/provers/blast/branch.ts +1 -1
  87. package/src/prove/isabelle/provers/blast/term.ts +3 -4
  88. package/src/prove/isabelle/provers/clasimp.ts +1 -1
  89. package/src/prove/isabelle/provers/classical.ts +3 -3
  90. package/src/prove/isabelle/provers/hypsubst.ts +1 -9
  91. package/src/prove/isabelle/pure/conv.ts +1 -1
  92. package/src/prove/isabelle/pure/drule.ts +2 -2
  93. package/src/prove/isabelle/pure/envir.ts +3 -35
  94. package/src/prove/isabelle/pure/logic.ts +4 -76
  95. package/src/prove/isabelle/pure/name.ts +1 -13
  96. package/src/prove/isabelle/pure/net.ts +2 -49
  97. package/src/prove/isabelle/pure/pattern.ts +3 -22
  98. package/src/prove/isabelle/pure/raw-simplifier.ts +3 -18
  99. package/src/prove/isabelle/pure/seq.ts +0 -23
  100. package/src/prove/isabelle/pure/simplifier.ts +2 -8
  101. package/src/prove/isabelle/pure/table.ts +1 -8
  102. package/src/prove/isabelle/pure/tactic.ts +5 -113
  103. package/src/prove/isabelle/pure/tactical.ts +1 -84
  104. package/src/prove/isabelle/pure/term-ord.ts +1 -55
  105. package/src/prove/isabelle/pure/term.ts +8 -128
  106. package/src/prove/isabelle/pure/thm.ts +4 -27
  107. package/src/prove/isabelle/pure/type.ts +0 -12
  108. package/src/prove/isabelle/pure/unify.ts +1 -1
  109. package/src/prove/isabelle/tla/classical-setup.ts +1 -1
  110. package/src/prove/ocaml.ts +1 -1
  111. package/src/prove/prep/action.ts +23 -36
  112. package/src/prove/prep/enabled-axioms.ts +2 -2
  113. package/src/prove/prep/fingerprint.ts +3 -3
  114. package/src/prove/prep/level-comparison.ts +9 -37
  115. package/src/prove/prep/md5.ts +2 -9
  116. package/src/prove/prep/normalize.ts +309 -0
  117. package/src/prove/prove-worker.ts +2 -5
  118. package/src/prove/prove.ts +98 -111
  119. package/src/prove/ptl/encode.ts +19 -31
  120. package/src/prove/smt/axioms.ts +45 -58
  121. package/src/prove/smt/encode.ts +124 -15
  122. package/src/prove/smt/instances.ts +2 -3
  123. package/src/prove/smt/rewrite.ts +24 -31
  124. package/src/prove/smt/solver/ast-util.ts +0 -56
  125. package/src/prove/smt/solver/case-split-queue.ts +1 -1
  126. package/src/prove/smt/solver/enode.ts +1 -1
  127. package/src/prove/smt/solver/lp/dep-intervals.ts +0 -14
  128. package/src/prove/smt/solver/model-macro-solver.ts +1 -1
  129. package/src/prove/smt/solver/nlsat/algebraic.ts +3 -3
  130. package/src/prove/smt/solver/nlsat/atoms.ts +1 -1
  131. package/src/prove/smt/solver/nlsat/explain.ts +1 -1
  132. package/src/prove/smt/solver/nlsat/interval-set.ts +1 -1
  133. package/src/prove/smt/solver/nlsat/levelwise.ts +4 -7
  134. package/src/prove/smt/solver/nlsat/polynomial.ts +4 -4
  135. package/src/prove/smt/solver/nlsat/upolynomial.ts +2 -2
  136. package/src/prove/smt/solver/pattern-inference.ts +1 -1
  137. package/src/prove/smt/solver/relevancy.ts +2 -2
  138. package/src/prove/smt/solver/rewriter.ts +1 -1
  139. package/src/prove/smt/solver/solver.ts +2 -2
  140. package/src/{tlapm-cache.ts → prove/tlapm/cache.ts} +11 -11
  141. package/src/{ocaml-marshal.ts → prove/tlapm/ocaml-marshal.ts} +1 -1
  142. package/src/prove/verdict.ts +0 -5
  143. package/src/prove/zenon/decide.ts +5 -11
  144. package/src/prove/zenon/encode.ts +21 -80
  145. package/src/prove/zenon/eqrel.ts +1 -1
  146. package/src/prove/zenon/equality-rules.ts +1 -1
  147. package/src/prove/zenon/expr.ts +2 -2
  148. package/src/prove/zenon/index-table.ts +1 -1
  149. package/src/prove/zenon/isar.ts +1 -1
  150. package/src/prove/zenon/llproof.ts +1 -1
  151. package/src/prove/zenon/mltoll.ts +1 -1
  152. package/src/prove/zenon/node.ts +1 -1
  153. package/src/prove/zenon/search.ts +1 -1
  154. package/src/prove/zenon/tla/terms.ts +1 -20
  155. package/src/prove/zenon/tla/tla.ts +1 -1
  156. package/src/randomization.ts +19 -24
  157. package/src/resolve.ts +15 -22
  158. package/src/runtime.ts +8 -8
  159. package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
  160. package/src/{sany-generator.ts → sany/generator.ts} +38 -55
  161. package/src/{sany-levels.ts → sany/levels.ts} +45 -48
  162. package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
  163. package/src/{sany-linter.ts → sany/linter.ts} +1 -1
  164. package/src/{sany-modules.ts → sany/modules.ts} +2 -2
  165. package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
  166. package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
  167. package/src/{sany-report.ts → sany/report.ts} +16 -17
  168. package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
  169. package/src/{sany-source.ts → sany/source.ts} +6 -7
  170. package/src/{sany-xml.ts → sany/xml.ts} +5 -6
  171. package/src/search.ts +969 -0
  172. package/src/spec-description.ts +26 -20
  173. package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
  174. package/src/standard-modules.ts +16 -20
  175. package/src/state-graph.ts +9 -8
  176. package/src/substitution-levels.ts +2 -2
  177. package/src/successors.ts +16 -19
  178. package/src/symmetry.ts +1 -1
  179. package/src/tableau.ts +6 -7
  180. package/src/tlc/fingerprint.ts +214 -0
  181. package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
  182. package/src/{tlc-order.ts → tlc/order.ts} +7 -7
  183. package/src/{tlc-random.ts → tlc/random.ts} +3 -213
  184. package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
  185. package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
  186. package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
  187. package/src/transfer.ts +32 -32
  188. package/src/values.ts +72 -67
  189. package/src/worker.ts +1 -4
  190. package/dist/run-cli.cjs +0 -45
  191. package/dist/run-cli.js +0 -45
  192. package/src/canonical.ts +0 -40
  193. package/src/obligations/ctx.ts +0 -64
  194. package/src/prove/prep/defs.ts +0 -33
  195. package/src/prove/prep/elab.ts +0 -156
  196. package/src/prove/prep/enabled.ts +0 -43
  197. package/src/prove/prep/flatten.ts +0 -70
  198. package/src/prove/prep/trivial.ts +0 -22
  199. package/src/prove/smt/axiomatize.ts +0 -69
  200. package/src/prove/smt/simplify.ts +0 -57
  201. package/src/run-cli.ts +0 -1806
  202. package/src/standard-ranges.ts +0 -116
  203. package/src/value-sources.ts +0 -29
  204. /package/src/prove/prep/{pragma.ts → methods.ts} +0 -0
  205. /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
  206. /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
  207. /package/src/{tlc-version.ts → tlc/version.ts} +0 -0
@@ -18,6 +18,7 @@ import type {
18
18
  import { TlaProofError, TlaUnsupportedError } from "../errors";
19
19
  import {
20
20
  at,
21
+ getFact,
21
22
  type Bound,
22
23
  type Builtin,
23
24
  type Defn,
@@ -29,17 +30,17 @@ import {
29
30
  type Sequent,
30
31
  } from "./expression";
31
32
  import type { ModuleUnit, Mule } from "./module";
32
- import { readNewMethod, readOldMethod } from "./pragma";
33
+ import { findPragma, readNewMethod, readOldMethod } from "./pragma";
33
34
  import {
34
35
  expandChoose,
35
36
  expandFcn,
36
37
  expandQuant,
37
38
  expandSetOf,
38
- expandSetSt,
39
39
  getUnbounded,
40
40
  type TuplyBound,
41
41
  } from "./tuples";
42
- import { lookupOperator, type Fixity, type Operator } from "./printer";
42
+ import { lookupOperator, type Fixity } from "./operators";
43
+ import { regroup } from "./regroup";
43
44
  import {
44
45
  getUnique,
45
46
  type Counter,
@@ -102,109 +103,17 @@ const mergeLoci = (left: Locus | undefined, right: Locus | undefined): Locus | u
102
103
  };
103
104
  };
104
105
 
105
- type Grouping =
106
- | { kind: "Attached"; expression: Expression }
107
- | { kind: "Reduced"; isFinal: boolean };
108
-
109
- const PREFIX_KEYWORDS: Partial<Record<Expression["kind"], string>> = {
110
- Always: "[]",
111
- Eventually: "<>",
112
- Enabled: "ENABLED",
113
- Unchanged: "UNCHANGED",
114
- };
115
-
116
- const isSamePosition = (left: SourceLocation | undefined, right: SourceLocation | undefined) =>
117
- left !== undefined &&
118
- right !== undefined &&
119
- left.line === right.line &&
120
- left.column === right.column;
121
-
122
- const getStart = (expression: Expression) => expression.location.range?.start;
123
-
124
- const getEnd = (expression: Expression) => expression.location.range?.end;
125
-
126
- const withEnd = (expression: Expression, end: SourceLocation | undefined): Expression => {
127
- const range = expression.location.range;
128
- return range && end
129
- ? { ...expression, location: { ...expression.location, range: { ...range, end } } }
130
- : expression;
131
- };
132
-
133
- // The operator an expression leaves open on tlapm's parse stack, with the operand after it.
134
- const getOpenOperator = (expression: Expression) => {
135
- if (expression.kind === "Apply") {
136
- const operator = lookupOperator(expression.operator);
137
- const isInfix = expression.args.length === 2 && operator.fixity.startsWith("Infix");
138
- const isPrefix = expression.args.length === 1 && operator.fixity === "Prefix";
139
- if (!isInfix && !isPrefix) return undefined;
140
- const last = expression.args.length - 1;
141
- return {
142
- operator,
143
- isInfix,
144
- operand: expression.args[last],
145
- replace: (operand: Expression): Expression => ({
146
- ...expression,
147
- args: [...expression.args.slice(0, last), operand],
148
- }),
149
- };
150
- }
151
- const keyword = PREFIX_KEYWORDS[expression.kind];
152
- if (keyword === undefined || !("expression" in expression)) return undefined;
153
- return {
154
- operator: lookupOperator(keyword),
155
- isInfix: false,
156
- operand: expression.expression,
157
- replace: (operand: Expression): Expression => ({ ...expression, expression: operand }),
158
- };
159
- };
160
-
161
- const isBelow = (left: Operator, right: Operator) => left.high < right.low;
162
-
163
- // Pco.resolve: tlapm's parser lets an operator take in every later infix operator that is not
164
- // entirely below it, so SUBSET S \ T is SUBSET (S \ T) to tlapm though SANY reads (SUBSET S) \ T.
165
- // This regroups an infix application the way tlapm does.
166
- const regroup = (expression: Extract<Expression, { kind: "Apply" }>): Expression => {
167
- const operator = lookupOperator(expression.operator);
168
- const [left, right] = expression.args;
169
- const end = getEnd(expression);
170
- const getInfix = (operand: Expression): Expression => {
171
- const range = expression.location.range;
172
- const start = getStart(operand);
173
- return {
174
- ...expression,
175
- args: [operand, right],
176
- location:
177
- range && start
178
- ? { ...expression.location, range: { ...range, start } }
179
- : expression.location,
180
- };
181
- };
182
- const attach = (node: Expression, isOpen: boolean): Grouping => {
183
- const open = isOpen ? getOpenOperator(node) : undefined;
184
- if (!open) return { kind: "Reduced", isFinal: false };
185
- const inner = attach(open.operand, isSamePosition(getEnd(open.operand), getEnd(node)));
186
- if (inner.kind === "Attached")
187
- return { kind: "Attached", expression: withEnd(open.replace(inner.expression), end) };
188
- const isReduced =
189
- !inner.isFinal &&
190
- (isBelow(operator, open.operator) ||
191
- (open.isInfix &&
192
- operator.fixity === "InfixLeft" &&
193
- open.operator.fixity === "InfixLeft" &&
194
- !isBelow(open.operator, operator)));
195
- if (isReduced) return { kind: "Reduced", isFinal: !isBelow(operator, open.operator) };
196
- return { kind: "Attached", expression: withEnd(open.replace(getInfix(open.operand)), end) };
197
- };
198
- const grouping = attach(left, isSamePosition(getStart(left), getStart(expression)));
199
- return grouping.kind === "Attached" ? grouping.expression : expression;
200
- };
201
-
202
106
  const TUPLY_FAILURE = "use instead the class`E_visit.iter_concrete`";
203
107
 
204
108
  const isDitto = (bounds: QuantifierExpression["bounds"], index: number) =>
205
109
  index > 0 && bounds[index - 1].set === bounds[index].set;
206
110
 
207
- export const createSource = (file: string, text: string, counter: Counter, isExpanded: boolean) => {
111
+ export const createSource = (
112
+ file: string,
113
+ text: string,
114
+ counter: Counter,
115
+ isExpandingTuples: boolean,
116
+ ) => {
208
117
  const lines = text.split("\n");
209
118
 
210
119
  const getRangeLocus = (range: SourceRange): Locus => ({
@@ -216,18 +125,10 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
216
125
  });
217
126
 
218
127
  const withPragma = (locus: Locus) => {
219
- const position = { line: locus.endLine - 1, column: locus.endColumn - 1 };
220
- const isBlank = (character: string | undefined) => character === " " || character === "\r";
221
- while (position.line < lines.length) {
222
- const line = lines[position.line];
223
- while (isBlank(line[position.column])) position.column++;
224
- if (position.column < line.length) break;
225
- position.line++;
226
- position.column = 0;
227
- }
228
- if (!(lines[position.line] ?? "").startsWith("(*{", position.column)) return locus;
229
- for (let index = position.line; index < lines.length; index++) {
230
- const close = lines[index].indexOf("}*)", index === position.line ? position.column : 0);
128
+ const open = findPragma(lines, { line: locus.endLine - 1, column: locus.endColumn - 1 });
129
+ if (!open) return locus;
130
+ for (let index = open.line; index < lines.length; index++) {
131
+ const close = lines[index].indexOf("}*)", index === open.line ? open.column : 0);
231
132
  if (close !== -1) return { ...locus, endLine: index + 1, endColumn: close + 4 };
232
133
  }
233
134
  return locus;
@@ -260,7 +161,7 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
260
161
  const getLocus = (location: SourceLocation): Locus =>
261
162
  location.range ? getRangeLocus(location.range) : getPointLocus(location, 1);
262
163
 
263
- const getOperator = (name: string, location: Locus | undefined): Expr => {
164
+ const convertOperator = (name: string, location: Locus | undefined): Expr => {
264
165
  const canonical = getCanonicalName(name);
265
166
  const builtin = BUILTINS[canonical];
266
167
  return at(builtin ? { kind: "Internal", builtin } : { kind: "Opaque", name: canonical }, {
@@ -269,7 +170,7 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
269
170
  };
270
171
 
271
172
  // A name with ! selects through instances, which anonymization resolves by joining the names.
272
- const getName = (
173
+ const convertName = (
273
174
  name: string,
274
175
  args: Expr[],
275
176
  location: Locus,
@@ -339,7 +240,7 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
339
240
  });
340
241
 
341
242
  const expandTuples = (expanded: Expr, location: Locus) => {
342
- if (!isExpanded)
243
+ if (!isExpandingTuples)
343
244
  throw new TlaProofError("Unexpected tuple declaration", { failure: TUPLY_FAILURE });
344
245
  return at(expanded, { location });
345
246
  };
@@ -393,7 +294,7 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
393
294
  return at({ kind: "String", value: written?.[1] ?? expression.value }, { location });
394
295
  }
395
296
  case "Identifier":
396
- return getName(expression.name, [], location);
297
+ return convertName(expression.name, [], location);
397
298
  case "Apply": {
398
299
  if (expression.args.length === 2) {
399
300
  const regrouped = regroup(expression);
@@ -403,14 +304,14 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
403
304
  const operator = lookupOperator(expression.operator);
404
305
  if (operator.fixity !== "Nonfix") {
405
306
  return at(
406
- { kind: "Apply", operator: getOperator(expression.operator, undefined), args },
307
+ { kind: "Apply", operator: convertOperator(expression.operator, undefined), args },
407
308
  { location: getOperatorLocus(args, operator.fixity) },
408
309
  );
409
310
  }
410
- return getName(expression.operator, args, location, expression.argumentCounts);
311
+ return convertName(expression.operator, args, location, expression.argumentCounts);
411
312
  }
412
313
  case "OperatorReference":
413
- return getOperator(expression.operator, location);
314
+ return convertOperator(expression.operator, location);
414
315
  case "Lambda":
415
316
  return at(
416
317
  {
@@ -516,8 +417,8 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
516
417
  if (expression.pattern.kind === "Tuple") {
517
418
  return expandTuples(
518
419
  expandChoose(
420
+ { kind: "Choose", domain: expression.set && recurse(expression.set) },
519
421
  expression.pattern.names,
520
- expression.set && recurse(expression.set),
521
422
  recurse(expression.body),
522
423
  ),
523
424
  location,
@@ -537,9 +438,9 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
537
438
  case "SetFilter":
538
439
  if (expression.bound.pattern.kind === "Tuple") {
539
440
  return expandTuples(
540
- expandSetSt(
441
+ expandChoose(
442
+ { kind: "SetSt", domain: recurse(expression.bound.set) },
541
443
  expression.bound.pattern.names,
542
- recurse(expression.bound.set),
543
444
  recurse(expression.predicate),
544
445
  ),
545
446
  location,
@@ -852,13 +753,7 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
852
753
  { location: getLocus(assumption.location) },
853
754
  )
854
755
  : convertFactLabel(assumption);
855
- return {
856
- kind: "Fact",
857
- expression,
858
- visibility: "Visible",
859
- time: "Now",
860
- location: expression.location,
861
- };
756
+ return getFact(expression, "Visible", "Now", expression.location);
862
757
  }),
863
758
  active: convertExpression(statement.conclusion, false),
864
759
  };
@@ -1134,7 +1029,6 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
1134
1029
  return {
1135
1030
  kind: "Mutate",
1136
1031
  mode: unit.use.kind,
1137
- isSuppressed: false,
1138
1032
  usable: convertUsable(unit.use),
1139
1033
  location: getUseLocus(unit.use),
1140
1034
  };
@@ -2,60 +2,62 @@
2
2
  // subexpression it names, abstracted over the bound names it uses.
3
3
 
4
4
  import { TlaProofError, TlapmBugError } from "../errors";
5
+ import { consAll, consSubstitution, shift, substituteExpr } from "./substitution";
5
6
  import {
6
7
  at,
7
- consAll,
8
- consSubstitution,
9
8
  getErrorNote,
9
+ getBoundFresh,
10
10
  getChildren,
11
11
  getFresh,
12
+ getParamFresh,
12
13
  mapChildren,
13
14
  mapSelectorArgs,
14
- shift,
15
- substituteExpr,
16
15
  type Bound,
17
16
  type Defn,
18
17
  type Expr,
19
18
  type Hyp,
20
- type ObligationWarning,
19
+ type Warn,
21
20
  type Param,
22
21
  type Selector,
23
22
  } from "./expression";
24
23
 
25
- interface Deref {
24
+ interface Resolution {
26
25
  params: Param[];
27
26
  definitions: Defn[];
28
27
  expression: Expr;
29
28
  }
30
29
 
31
- interface Label {
30
+ interface LabelMatch {
32
31
  context: Hyp[];
33
32
  expression: Expr;
34
33
  indices: { name: string; index: number }[];
35
34
  }
36
35
 
37
- const BAD = { kind: "Opaque", name: "$Bad" } satisfies Expr;
36
+ const UNRESOLVED = { kind: "Opaque", name: "$Bad" } satisfies Expr;
38
37
 
39
- const isBad = (expression: Expr) => expression.kind === "Opaque" && expression.name === BAD.name;
38
+ const isUnresolved = (expression: Expr) =>
39
+ expression.kind === "Opaque" && expression.name === UNRESOLVED.name;
40
40
 
41
- const clean = (expression: Expr): Expr => {
41
+ const stripParensAndAtomic = (expression: Expr): Expr => {
42
42
  const mapped =
43
- expression.kind === "Parens" ? clean(expression.expression) : mapChildren(expression, clean);
43
+ expression.kind === "Parens"
44
+ ? stripParensAndAtomic(expression.expression)
45
+ : mapChildren(expression, stripParensAndAtomic);
44
46
  if (!mapped.isAtomic) return mapped;
45
47
  const { isAtomic: _isAtomic, ...rest } = mapped;
46
48
  return rest;
47
49
  };
48
50
 
49
51
  // Parentheses go everywhere but in the definitions of a LET.
50
- const cleanParens = (expression: Expr): Expr => {
52
+ const stripParens = (expression: Expr): Expr => {
51
53
  if (expression.kind === "Let") {
52
54
  return at(
53
- { kind: "Let", definitions: expression.definitions, body: cleanParens(expression.body) },
55
+ { kind: "Let", definitions: expression.definitions, body: stripParens(expression.body) },
54
56
  expression.body,
55
57
  );
56
58
  }
57
- if (expression.kind === "Parens") return cleanParens(expression.expression);
58
- return mapChildren(expression, cleanParens);
59
+ if (expression.kind === "Parens") return stripParens(expression.expression);
60
+ return mapChildren(expression, stripParens);
59
61
  };
60
62
 
61
63
  const hasNegativeIndex = (expression: Expr): boolean =>
@@ -63,14 +65,14 @@ const hasNegativeIndex = (expression: Expr): boolean =>
63
65
 
64
66
  // Visit.iter's context for each binder, stopping at labels with other names and at declarations
65
67
  // in an ASSUME.
66
- const findLabel = (expression: Expr, label: string, context: Hyp[]): Label | undefined => {
68
+ const findLabel = (expression: Expr, label: string, context: Hyp[]): LabelMatch | undefined => {
67
69
  const search = (inner: Expr, innerContext = context) => findLabel(inner, label, innerContext);
68
70
  const searchBounds = (bounds: Bound[], body: Expr) =>
69
- bounds.reduce<Label | undefined>(
71
+ bounds.reduce<LabelMatch | undefined>(
70
72
  (found, bound) =>
71
73
  found ?? (bound.domain.kind === "Domain" ? search(bound.domain.expression) : undefined),
72
74
  undefined,
73
- ) ?? search(body, [...context, ...bounds.map((bound) => getFresh(bound.name, bound.level))]);
75
+ ) ?? search(body, [...context, ...bounds.map(getBoundFresh)]);
74
76
  switch (expression.kind) {
75
77
  case "Parens":
76
78
  if (expression.form.kind !== "Xlabel") return search(expression.expression);
@@ -78,10 +80,7 @@ const findLabel = (expression: Expr, label: string, context: Hyp[]): Label | und
78
80
  ? { context, expression: expression.expression, indices: expression.form.indices }
79
81
  : undefined;
80
82
  case "Lambda":
81
- return search(expression.body, [
82
- ...context,
83
- ...expression.params.map((param) => getFresh(param.name, "Unknown", param.shape)),
84
- ]);
83
+ return search(expression.body, [...context, ...expression.params.map(getParamFresh)]);
85
84
  case "Let": {
86
85
  const inner = [...context];
87
86
  for (const definition of expression.definitions) {
@@ -144,11 +143,7 @@ const getReversal = (count: number, paramCount: number) =>
144
143
  const getIndices = (count: number) =>
145
144
  Array.from({ length: count }, (_item, index): Expr => ({ kind: "Ix", index: count - index }));
146
145
 
147
- const instantiate = (
148
- onWarning: (warning: ObligationWarning) => void,
149
- expression: Expr,
150
- args: Expr[],
151
- ): Expr => {
146
+ const instantiate = (onWarning: Warn, expression: Expr, args: Expr[]): Expr => {
152
147
  switch (expression.kind) {
153
148
  case "Quant":
154
149
  case "Fcn":
@@ -214,11 +209,7 @@ const instantiate = (
214
209
  }
215
210
  };
216
211
 
217
- const abstract = (
218
- onWarning: (warning: ObligationWarning) => void,
219
- definitionCount: number,
220
- expression: Expr,
221
- ): [Param[], Expr] => {
212
+ const abstract = (onWarning: Warn, definitionCount: number, expression: Expr): [Param[], Expr] => {
222
213
  const params = ((): Param[] => {
223
214
  switch (expression.kind) {
224
215
  case "Quant":
@@ -244,7 +235,7 @@ const abstract = (
244
235
  };
245
236
 
246
237
  const getNumbered = (
247
- onWarning: (warning: ObligationWarning) => void,
238
+ onWarning: Warn,
248
239
  expression: Expr,
249
240
  position: number,
250
241
  origin: Expr,
@@ -347,7 +338,7 @@ const getNumbered = (
347
338
  at({ kind: "Tuple", elements: [arm.condition, arm.body] }, arm.body),
348
339
  ),
349
340
  ...(expression.other
350
- ? [at({ kind: "Tuple", elements: [BAD, expression.other] }, expression.other)]
341
+ ? [at({ kind: "Tuple", elements: [UNRESOLVED, expression.other] }, expression.other)]
351
342
  : []),
352
343
  ]);
353
344
  }
@@ -355,7 +346,7 @@ const getNumbered = (
355
346
  onWarning(getErrorNote(expression.location, emptyMessage));
356
347
  throw new TlaProofError(emptyMessage, { failure: "Expr.Deref.deref_num" });
357
348
  })();
358
- if (isBad(result)) {
349
+ if (isUnresolved(result)) {
359
350
  const badMessage = `Cannot resolve subexpression reference !${position}`;
360
351
  onWarning(getErrorNote(expression.location, badMessage));
361
352
  throw new TlaProofError(badMessage, { failure: "Expr.Deref.deref_num" });
@@ -375,16 +366,16 @@ const permute = (indices: number[], args: Expr[]) => {
375
366
  };
376
367
 
377
368
  const resolveLabel = (
378
- onWarning: (warning: ObligationWarning) => void,
369
+ onWarning: Warn,
379
370
  definitionCount: number,
380
371
  expression: Expr,
381
372
  label: string,
382
373
  labelArgs: Expr[],
383
- ): Deref => {
374
+ ): Resolution => {
384
375
  const found = findLabel(expression, label, []);
385
376
  if (!found) {
386
377
  onWarning(getErrorNote(expression.location, `Could not find label ${label}`));
387
- return { params: [], definitions: [], expression: BAD };
378
+ return { params: [], definitions: [], expression: UNRESOLVED };
388
379
  }
389
380
  const indices = found.indices;
390
381
  if (indices.length !== labelArgs.length && labelArgs.length > 0) {
@@ -438,12 +429,12 @@ const resolveLabel = (
438
429
  };
439
430
 
440
431
  const resolveLetOperator = (
441
- onWarning: (warning: ObligationWarning) => void,
432
+ onWarning: Warn,
442
433
  definitionCount: number,
443
434
  expression: Expr,
444
435
  label: string,
445
436
  args: Expr[],
446
- ): Deref => {
437
+ ): Resolution => {
447
438
  if (expression.kind !== "Let") {
448
439
  onWarning(getErrorNote(expression.location, `not a LET expression`));
449
440
  throw new TlaProofError("not a LET expression", { failure: "Expr.Deref.deref_letop" });
@@ -494,10 +485,10 @@ const resolveLetOperator = (
494
485
  );
495
486
  };
496
487
 
497
- const deref = (
498
- onWarning: (warning: ObligationWarning) => void,
488
+ const resolveSelectors = (
489
+ onWarning: Warn,
499
490
  isLabel: boolean,
500
- state: Deref,
491
+ state: Resolution,
501
492
  selectors: Selector[],
502
493
  ): Expr => {
503
494
  const [selector, ...rest] = selectors;
@@ -513,15 +504,15 @@ const deref = (
513
504
  state.params.length === 0
514
505
  ? withDefinitions
515
506
  : at({ kind: "Lambda", params: state.params, body: withDefinitions }, withDefinitions);
516
- return { ...clean(abstracted), isAtomic: true };
507
+ return { ...stripParensAndAtomic(abstracted), isAtomic: true };
517
508
  }
518
509
  if (state.expression.isAtomic) {
519
510
  onWarning(getErrorNote(state.expression.location, `Expression has no subexpressions`));
520
- return BAD;
511
+ return UNRESOLVED;
521
512
  }
522
- const expression = isLabel ? state.expression : cleanParens(state.expression);
513
+ const expression = isLabel ? state.expression : stripParens(state.expression);
523
514
  const count = state.definitions.length;
524
- const next = ((): Deref & { rest: Selector[]; isLabel: boolean } => {
515
+ const next = ((): Resolution & { rest: Selector[]; isLabel: boolean } => {
525
516
  const numbered = (position: number, message: string) => {
526
517
  const [definitions, inner] = getNumbered(
527
518
  onWarning,
@@ -571,7 +562,7 @@ const deref = (
571
562
  }
572
563
  }
573
564
  })();
574
- return deref(
565
+ return resolveSelectors(
575
566
  onWarning,
576
567
  next.isLabel,
577
568
  {
@@ -585,11 +576,7 @@ const deref = (
585
576
  );
586
577
  };
587
578
 
588
- const getInitialDeref = (
589
- onWarning: (warning: ObligationWarning) => void,
590
- operator: Expr,
591
- args: Expr[],
592
- ): Deref => {
579
+ const getInitialResolution = (onWarning: Warn, operator: Expr, args: Expr[]): Resolution => {
593
580
  if (args.length > 0) {
594
581
  return { params: [], definitions: [], expression: instantiate(onWarning, operator, args) };
595
582
  }
@@ -599,13 +586,18 @@ const getInitialDeref = (
599
586
  };
600
587
 
601
588
  export const resolveSubexpression = (
602
- onWarning: (warning: ObligationWarning) => void,
589
+ onWarning: Warn,
603
590
  operator: Expr,
604
591
  args: Expr[],
605
592
  selectors: Selector[],
606
593
  ) => {
607
- const result = deref(onWarning, true, getInitialDeref(onWarning, operator, args), selectors);
608
- if (isBad(result)) {
594
+ const result = resolveSelectors(
595
+ onWarning,
596
+ true,
597
+ getInitialResolution(onWarning, operator, args),
598
+ selectors,
599
+ );
600
+ if (isUnresolved(result)) {
609
601
  const message = "Could not resolve subexpression reference";
610
602
  onWarning(getErrorNote(operator.location, message));
611
603
  throw new TlaProofError(message, { failure: "Expr.Anon: 2" });