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
package/src/liveness.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import { isLocallyResolved, type Bound, type Definition, type Expression } from "./ast";
2
- import { getUnexpectedException, TlaEvaluationError, TlaStackOverflowError } from "./errors";
2
+ import {
3
+ createStackOverflowError,
4
+ TlaEvaluationError,
5
+ TlaStackOverflowError,
6
+ UNEXPECTED_EVAL_EXCEPTION,
7
+ } from "./errors";
3
8
  import {
4
9
  describeNode,
5
10
  describeRange,
@@ -21,10 +26,10 @@ import {
21
26
  type Thunk,
22
27
  } from "./evaluate";
23
28
  import { mapChildren } from "./instances";
24
- import { ACTION_LEVEL, TEMPORAL_LEVEL } from "./levels";
29
+ import { ACTION_LEVEL, CONSTANT_LEVEL, TEMPORAL_LEVEL, VARIABLE_LEVEL } from "./levels";
25
30
  import { isEnabled } from "./successors";
26
31
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
27
- import { isEvalException } from "./tlc-messages";
32
+ import { isEvalException } from "./tlc/messages";
28
33
  import {
29
34
  createBoolean,
30
35
  createJunction,
@@ -59,9 +64,6 @@ export const createTautologyError = () =>
59
64
  EXIT_STATUS.livenessEvaluationFailure,
60
65
  );
61
66
 
62
- // TLC expands a temporal formula that refers to itself until Java's stack overflows.
63
- const createStackOverflowError = () => setExitCode(new TlaStackOverflowError(), EXIT_STATUS.error);
64
-
65
67
  const TEMPORAL_OPERATORS = new Set(["~>", "-+->"]);
66
68
 
67
69
  const definitionLevels = new WeakMap<Definition, number>();
@@ -90,26 +92,31 @@ export const getLevel = (
90
92
  const getBindingLevel = (name: string) => {
91
93
  const binding = lookupScope(scope, name);
92
94
  if (!binding) return undefined;
93
- if (binding.kind === "Value") return 0;
95
+ if (binding.kind === "Value") return CONSTANT_LEVEL;
94
96
  if (binding.kind === "Thunk") return getLevel(binding.expression, binding.scope, environment);
95
97
  const operator = binding.operator;
96
- return operator.kind === "Closure" ? getLevel(operator.body, operator.scope, environment) : 0;
98
+ return operator.kind === "Closure"
99
+ ? getLevel(operator.body, operator.scope, environment)
100
+ : CONSTANT_LEVEL;
97
101
  };
98
102
  const getGlobalLevel = (name: string) => {
99
- if (environment.variableIndex.has(name)) return 1;
100
- if (environment.constants.has(name)) return 0;
103
+ if (environment.variableIndex.has(name)) return VARIABLE_LEVEL;
104
+ if (environment.constants.has(name)) return CONSTANT_LEVEL;
101
105
  const definition = getDefinition(name, environment);
102
106
  if (definition) return getDefinitionLevel(definition, environment);
103
107
  const builtin = environment.builtins.get(environment.substitutions.get(name) ?? name);
104
- return builtin ? (builtin.isStateLevel && !builtin.isLevelConstant ? 1 : 0) : undefined;
108
+ if (!builtin) return undefined;
109
+ return builtin.isStateLevel && !builtin.isLevelConstant ? VARIABLE_LEVEL : CONSTANT_LEVEL;
105
110
  };
106
111
  const getNameLevel = (name: string, node: Expression) => {
107
- if (shadowed.has(name)) return 0;
108
- if (isLocallyResolved(node)) return getBindingLevel(name) ?? getGlobalLevel(name) ?? 0;
109
- return getGlobalLevel(name) ?? getBindingLevel(name) ?? 0;
112
+ if (shadowed.has(name)) return CONSTANT_LEVEL;
113
+ if (isLocallyResolved(node)) {
114
+ return getBindingLevel(name) ?? getGlobalLevel(name) ?? CONSTANT_LEVEL;
115
+ }
116
+ return getGlobalLevel(name) ?? getBindingLevel(name) ?? CONSTANT_LEVEL;
110
117
  };
111
118
  const getChildrenLevel = (node: Expression) => {
112
- const level = { value: 0 };
119
+ const level = { value: CONSTANT_LEVEL };
113
120
  mapChildren(node, (child, bound) => {
114
121
  level.value = Math.max(
115
122
  level.value,
@@ -131,7 +138,7 @@ export const getLevel = (
131
138
  case "AngleAction":
132
139
  return ACTION_LEVEL;
133
140
  case "Enabled":
134
- return 1;
141
+ return VARIABLE_LEVEL;
135
142
  case "Always":
136
143
  case "Eventually":
137
144
  case "WeakFairness":
@@ -173,9 +180,7 @@ const getUnrolledElements = (bounds: Bound[], context: Context) => {
173
180
  if (!(error instanceof TlaEvaluationError)) throw error;
174
181
  if (!isEvalException(error)) return undefined;
175
182
  throw setExitCode(
176
- new TlaEvaluationError(
177
- `${getUnexpectedException("tlc2.tool.EvalException")}${error.message}`,
178
- ),
183
+ new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`),
179
184
  EXIT_STATUS.error,
180
185
  );
181
186
  }
@@ -229,7 +234,7 @@ export const expandTemporal = (
229
234
  !binding &&
230
235
  definition?.kind === "Operator" &&
231
236
  definition.params.length === 0 &&
232
- getDefinitionLevel(definition, environment) > 0
237
+ getDefinitionLevel(definition, environment) > CONSTANT_LEVEL
233
238
  ) {
234
239
  if (expanding.has(definition)) {
235
240
  throw createStackOverflowError();
@@ -273,7 +278,7 @@ export const expandTemporal = (
273
278
  const isUnrolled =
274
279
  isProperty &&
275
280
  expression.kind === "Forall" &&
276
- expression.bounds.every((bound) => getLevel(bound.set, scope, environment) === 0);
281
+ expression.bounds.every((bound) => getLevel(bound.set, scope, environment) === CONSTANT_LEVEL);
277
282
  const sets = isUnrolled ? getUnrolledElements(expression.bounds, context) : undefined;
278
283
  if (isUnrolled && sets) {
279
284
  const expanded: ExpandedExpression[] = [];
@@ -299,13 +304,6 @@ const bindLetDefinitions = (
299
304
  return bound;
300
305
  };
301
306
 
302
- const createAngleAction = (action: Expression, subscript: Expression): Expression => ({
303
- kind: "AngleAction",
304
- action,
305
- subscript,
306
- location: action.location,
307
- });
308
-
309
307
  export const toLiveExpression = (
310
308
  root: Expression,
311
309
  rootScope: Scope | undefined,
@@ -318,8 +316,9 @@ export const toLiveExpression = (
318
316
  level: number,
319
317
  ): LiveExpression => {
320
318
  // TLC after 1.7.4 bounds the level of a formula whose parts are constant by the formula's own.
321
- const atomLevel = level === 0 && !is174 ? getLevel(expression, scope, environment) : level;
322
- if (atomLevel === 0) {
319
+ const atomLevel =
320
+ level === CONSTANT_LEVEL && !is174 ? getLevel(expression, scope, environment) : level;
321
+ if (atomLevel === CONSTANT_LEVEL) {
323
322
  const value = evaluate(expression, getConstantContext(environment, scope));
324
323
  if (typeof value !== "boolean") {
325
324
  throw new TlaEvaluationError(
@@ -328,7 +327,7 @@ export const toLiveExpression = (
328
327
  }
329
328
  return createBoolean(value);
330
329
  }
331
- if (atomLevel === 1) return { kind: "State", expression, scope };
330
+ if (atomLevel === VARIABLE_LEVEL) return { kind: "State", expression, scope };
332
331
  return { kind: "Action", expression, unchanged: undefined, scope };
333
332
  };
334
333
 
@@ -419,7 +418,7 @@ export const toLiveExpression = (
419
418
  ) => {
420
419
  const level = getLevel(expression, scope, environment);
421
420
  const isConstant = expression.bounds.every(
422
- (bound) => getLevel(bound.set, scope, environment) === 0,
421
+ (bound) => getLevel(bound.set, scope, environment) === CONSTANT_LEVEL,
423
422
  );
424
423
  if (!isConstant) {
425
424
  if (level <= ACTION_LEVEL) return toAtom(expression, scope, level);
@@ -519,7 +518,7 @@ export const toLiveExpression = (
519
518
  return convertQuantifier(expression, scope);
520
519
  case "If": {
521
520
  // TLC 1.7.4 translates both branches whatever the guard.
522
- if (!is174 && getLevel(expression.condition, scope, environment) === 0) {
521
+ if (!is174 && getLevel(expression.condition, scope, environment) === CONSTANT_LEVEL) {
523
522
  const condition = evaluate(expression.condition, getConstantContext(environment, scope));
524
523
  if (typeof condition === "boolean") {
525
524
  return convert(condition ? expression.consequent : expression.alternative, scope);
@@ -545,7 +544,12 @@ export const toLiveExpression = (
545
544
  return createUnary(expression.kind, convert(expression.expression, scope));
546
545
  case "WeakFairness":
547
546
  case "StrongFairness": {
548
- const step = createAngleAction(expression.action, expression.subscript);
547
+ const step: Expression = {
548
+ kind: "AngleAction",
549
+ action: expression.action,
550
+ subscript: expression.subscript,
551
+ location: expression.action.location,
552
+ };
549
553
  const enabled: LiveEnabled = { kind: "Enabled", step: expression.enabled ?? step, scope };
550
554
  const disabled = createUnary("Not", enabled);
551
555
  const taken: LiveExpression = {
@@ -3,31 +3,28 @@
3
3
  // and ExpandCdot frontends then replace each LAMBDA with an existential quantifier.
4
4
 
5
5
  import { TlaProofError, TlapmBugError, TlaProverError } from "../errors";
6
- import type { Anonymizer } from "./anon";
6
+ import type { Anonymizer } from "./anonymize";
7
+ import { bumpBy, normalizeApply, shift, substituteExpr, type Substitution } from "./substitution";
7
8
  import {
8
9
  adjoin,
9
10
  adjoinAll,
10
11
  applyBuiltin,
11
12
  at,
12
- bumpBy,
13
13
  getAtPrefix,
14
14
  getErrorNote,
15
- getFresh,
16
- getLocusText,
15
+ getLocationText,
16
+ getParamFresh,
17
17
  getScopeAt,
18
+ isBuiltinApply,
18
19
  mapScoped,
19
- normalizeApply,
20
- shift,
21
20
  stripProps,
22
- substituteExpr,
23
21
  type Expr,
24
- type Locus,
25
- type ObligationWarning,
22
+ type Warn,
26
23
  type Param,
27
24
  type Scope,
28
- type Substitution,
29
25
  } from "./expression";
30
26
  import { getLevelInfo, LEVEL_NUMBERS } from "./levels";
27
+ import { rewriteUnchanged } from "./temporal";
31
28
 
32
29
  // Which of ENABLED and \cdot a pass handles, and whether it may expand the definitions that
33
30
  // soundness needs expanded, as the AutoUSE directive allows.
@@ -42,8 +39,6 @@ interface FreshNames {
42
39
  depth: number;
43
40
  }
44
41
 
45
- type Warn = (warning: ObligationWarning) => void;
46
-
47
42
  const ENABLED_WRAPPER = "EnabledWrapper";
48
43
  const CDOT_WRAPPER = "CdotWrapper";
49
44
 
@@ -56,9 +51,6 @@ const NESTED_CDOT =
56
51
  // Unlike auto_expansion_of_defs, tlapm puts a space before "For example" here.
57
52
  const NESTED_CDOT_SPACED = `${NESTED_CDOT} For example, writing \`BY ExpandCdot\`.`;
58
53
 
59
- const getLocationText = (locus: Locus | undefined) =>
60
- locus ? getLocusText(locus) : "<unknown location>";
61
-
62
54
  // Visit.map_visible_hyp with no state.
63
55
  const mapVisibleChildren = (expression: Expr, map: (child: Expr) => Expr) =>
64
56
  mapScoped(
@@ -90,14 +82,6 @@ const isLambdified = (expression: Expr) =>
90
82
  expression.args[0].kind === "Lambda" &&
91
83
  (isActionOperator(expression.operator) || isWrapper(expression.operator));
92
84
 
93
- const isBuiltinApply = (
94
- expression: Expr,
95
- builtin: "ENABLED" | "Cdot",
96
- ): expression is Expr & { kind: "Apply" } =>
97
- expression.kind === "Apply" &&
98
- expression.operator.kind === "Internal" &&
99
- expression.operator.builtin === builtin;
100
-
101
85
  const getInitialLevel = (level: number | undefined) =>
102
86
  level === undefined ? 2 : Math.min(2, level);
103
87
 
@@ -225,19 +209,6 @@ const expandDefinitions = (
225
209
  );
226
210
  };
227
211
 
228
- const getEquality = (expression: Expr): Expr =>
229
- applyBuiltin("Eq", [applyBuiltin("Prime", [expression], expression), expression], expression);
230
-
231
- // E_tla_norm.rewrite_unch: UNCHANGED <<x, <<y>>>> as x' = x /\ y' = y.
232
- export const rewriteUnchanged = (expression: Expr): Expr => {
233
- const flatten = (inner: Expr): Expr[] =>
234
- inner.kind === "Tuple" ? inner.elements.flatMap(flatten) : [inner];
235
- const items = flatten(expression);
236
- if (items.length === 0) return at({ kind: "Internal", builtin: "TRUE" }, expression);
237
- if (items.length === 1) return getEquality(items[0]);
238
- return at({ kind: "List", bullet: "And", items: items.map(getEquality) }, expression);
239
- };
240
-
241
212
  // expand_propositional_action_operators: UNCHANGED v, [A]_v and <<A>>_v in terms of primes.
242
213
  const expandPropositional = (expression: Expr): Expr => {
243
214
  if (expression.kind === "Apply" && expression.args.length === 1) {
@@ -500,10 +471,7 @@ const replaceWithQuantifiers = (
500
471
  body: replaceWithQuantifiers(
501
472
  options,
502
473
  onWarning,
503
- adjoinAll(
504
- scope,
505
- params.map((param) => getFresh(param.name, "Unknown", param.shape)),
506
- ),
474
+ adjoinAll(scope, params.map(getParamFresh)),
507
475
  body,
508
476
  true,
509
477
  ),
@@ -563,11 +531,7 @@ const commuteExistsDisjunction = (expression: Expr): Expr => {
563
531
  items: body.items.map((item) => ({ ...expression, body: item })),
564
532
  });
565
533
  }
566
- if (
567
- body.kind === "Apply" &&
568
- body.operator.kind === "Internal" &&
569
- body.operator.builtin === "Disj"
570
- ) {
534
+ if (isBuiltinApply(body, "Disj")) {
571
535
  if (body.args.length !== 2) throw new TlapmBugError();
572
536
  return commuteExistsDisjunction({
573
537
  ...body,
@@ -2,41 +2,39 @@
2
2
  // indices into the context, builtin definitions are inlined, and anonymous steps get names.
3
3
 
4
4
  import { TlapmBugError, TlaProofError } from "../errors";
5
+ import { consSubstitution, normalizeApply, shift, shiftExpr, substituteExpr } from "./substitution";
6
+ import { isEqualExpr } from "./equality";
5
7
  import {
6
8
  adjoin,
7
9
  adjoinAll,
8
10
  applyBuiltin,
9
11
  at,
10
- consSubstitution,
11
12
  exprifySequent,
12
13
  findNamed,
14
+ getBoundFresh,
13
15
  getErrorNote,
14
16
  getFresh,
17
+ getLocalDefn,
15
18
  getLocusPrefix,
16
19
  getScopeAt,
17
- isEqualExpr,
18
20
  mapAccumulate,
19
21
  mapBoundDomains,
20
22
  mapScoped,
21
23
  mapSelectorArgs,
22
- normalizeApply,
23
- shift,
24
- shiftExpr,
25
- substituteExpr,
26
24
  type Bound,
27
25
  type Defn,
28
26
  type ExceptPoint,
29
27
  type Expr,
30
28
  type ExprCore,
31
29
  type Hyp,
32
- type ObligationWarning,
30
+ type Warn,
33
31
  type ParenForm,
34
32
  type Scope,
35
33
  type Selector,
36
34
  type Sequent,
37
35
  type WhereDefined,
38
36
  } from "./expression";
39
- import { resolveSubexpression } from "./deref";
37
+ import { resolveSubexpression } from "./subexpression";
40
38
  import { getTime } from "./temporal";
41
39
  import {
42
40
  getStepName,
@@ -67,6 +65,10 @@ const ANON_SELECTOR_FAILURES = {
67
65
 
68
66
  const BAD_EXPRESSION: Expr = { kind: "Opaque", name: "$badexp" };
69
67
 
68
+ // Util.set_locus: the node keeps its properties and takes the location of from.
69
+ const withLocus = (expression: Expr, from: Expr) =>
70
+ from.location ? { ...expression, location: from.location } : expression;
71
+
70
72
  interface AnonContext {
71
73
  warnedSteps: string[];
72
74
  scope: Scope | undefined;
@@ -78,7 +80,7 @@ export const createAnonymizer = (
78
80
  scope: Scope | undefined,
79
81
  definition: Extract<Defn, { kind: "Instance" }>,
80
82
  ) => Defn[],
81
- onWarning: (warning: ObligationWarning) => void,
83
+ onWarning: Warn,
82
84
  ) => {
83
85
  // Elab.desugar's current_at: the value @ stands for inside an EXCEPT, and the context size there.
84
86
  let currentAt: { expression: Expr; size: number } | undefined;
@@ -278,10 +280,7 @@ export const createAnonymizer = (
278
280
  withScope(context, adjoin(context.scope, hyp));
279
281
 
280
282
  const adjoinBounds = (context: AnonContext, items: Bound[]) =>
281
- items.reduce(
282
- (current, bound) => adjoinContext(current, getFresh(bound.name, bound.level)),
283
- context,
284
- );
283
+ withScope(context, adjoinAll(context.scope, items.map(getBoundFresh)));
285
284
 
286
285
  const bounds = (context: AnonContext, items: Bound[]): [AnonContext, Bound[]] => {
287
286
  const mapped = mapBoundDomains(items, (domain) => expr(context, domain));
@@ -331,14 +330,7 @@ export const createAnonymizer = (
331
330
  continue;
332
331
  }
333
332
  const result = defn(current, definition);
334
- current = adjoinContext(current, {
335
- kind: "Defn",
336
- definition: result,
337
- whereDefined: "User",
338
- visibility: "Visible",
339
- isLocal: true,
340
- isConstnessUnset: true,
341
- });
333
+ current = adjoinContext(current, { ...getLocalDefn(result), isConstnessUnset: true });
342
334
  mapped.push(result);
343
335
  }
344
336
  return [current, mapped];
@@ -578,8 +570,4 @@ export const createAnonymizer = (
578
570
  };
579
571
  };
580
572
 
581
- // Util.set_locus: the node keeps its properties and takes the location of from.
582
- const withLocus = (expression: Expr, from: Expr) =>
583
- from.location ? { ...expression, location: from.location } : expression;
584
-
585
573
  export type Anonymizer = ReturnType<typeof createAnonymizer>;
@@ -0,0 +1,260 @@
1
+ // tlapm's E_eq: alpha-equivalence of expressions.
2
+
3
+ import type { Bound, Defn, Expr, Field, Hyp, Selector, Sequent } from "./expression";
4
+
5
+ const isEqualList = <Item>(
6
+ left: Item[],
7
+ right: Item[],
8
+ isEqual: (left: Item, right: Item) => boolean,
9
+ ) => left.length === right.length && left.every((item, index) => isEqual(item, right[index]));
10
+
11
+ const isEqualOptional = (left: Expr | undefined, right: Expr | undefined) =>
12
+ left && right ? isEqualExpr(left, right) : !left && !right;
13
+
14
+ const isEqualFields = (left: Field[], right: Field[]) => {
15
+ const leftMap = new Map(left.map((field) => [field.name, field.value]));
16
+ const rightMap = new Map(right.map((field) => [field.name, field.value]));
17
+ if (leftMap.size !== rightMap.size) return false;
18
+ for (const [name, value] of leftMap) {
19
+ const other = rightMap.get(name);
20
+ if (!other || !isEqualExpr(value, other)) return false;
21
+ }
22
+ return true;
23
+ };
24
+
25
+ const isEqualBound = (left: Bound, right: Bound) =>
26
+ left.level === right.level &&
27
+ left.domain.kind === right.domain.kind &&
28
+ (left.domain.kind !== "Domain" ||
29
+ (right.domain.kind === "Domain" &&
30
+ isEqualExpr(left.domain.expression, right.domain.expression)));
31
+
32
+ const isEqualSelector = (left: Selector, right: Selector) => {
33
+ if (left.kind !== right.kind) return false;
34
+ switch (left.kind) {
35
+ case "Number":
36
+ return right.kind === "Number" && left.value === right.value;
37
+ case "Instance":
38
+ return right.kind === "Instance" && isEqualList(left.args, right.args, isEqualExpr);
39
+ case "Label":
40
+ return (
41
+ right.kind === "Label" &&
42
+ left.label === right.label &&
43
+ isEqualList(left.args, right.args, isEqualExpr)
44
+ );
45
+ default:
46
+ return true;
47
+ }
48
+ };
49
+
50
+ const isEqualDefn = (left: Defn, right: Defn): boolean => {
51
+ if ((left.kind === "Operator" || left.kind === "Bpragma") && right.kind === left.kind)
52
+ return isEqualExpr(left.body, right.body);
53
+ if (left.kind === "Instance" && right.kind === "Instance") {
54
+ const leftSubstitutions = left.instance.substitutions.map((entry): Field => ({
55
+ name: entry.name,
56
+ value: entry.expression,
57
+ }));
58
+ const rightSubstitutions = right.instance.substitutions.map((entry): Field => ({
59
+ name: entry.name,
60
+ value: entry.expression,
61
+ }));
62
+ return (
63
+ isEqualList(
64
+ left.instance.args,
65
+ right.instance.args,
66
+ (leftArg, rightArg) => leftArg === rightArg,
67
+ ) &&
68
+ left.instance.module === right.instance.module &&
69
+ isEqualFields(leftSubstitutions, rightSubstitutions)
70
+ );
71
+ }
72
+ return false;
73
+ };
74
+
75
+ export const isEqualHyp = (left: Hyp, right: Hyp): boolean => {
76
+ if (left.kind === "Fresh" && right.kind === "Fresh") {
77
+ return (
78
+ left.level === right.level &&
79
+ left.shape === right.shape &&
80
+ isEqualOptional(left.domain, right.domain)
81
+ );
82
+ }
83
+ if (left.kind === "Flex" && right.kind === "Flex") return true;
84
+ if (left.kind === "Defn" && right.kind === "Defn") {
85
+ return isEqualDefn(left.definition, right.definition) && left.isLocal === right.isLocal;
86
+ }
87
+ if (left.kind === "Fact" && right.kind === "Fact") {
88
+ return isEqualExpr(left.expression, right.expression);
89
+ }
90
+ return false;
91
+ };
92
+
93
+ const isEqualSequent = (left: Sequent, right: Sequent) =>
94
+ isEqualList(left.context, right.context, isEqualHyp) && isEqualExpr(left.active, right.active);
95
+
96
+ // Alpha-equivalence, including E_eq's quirk of comparing only the first CASE arm.
97
+ export const isEqualExpr = (left: Expr, right: Expr): boolean => {
98
+ if (left.kind === "Parens") return isEqualExpr(left.expression, right);
99
+ if (right.kind === "Parens") return isEqualExpr(left, right.expression);
100
+ switch (left.kind) {
101
+ case "Ix":
102
+ return right.kind === "Ix" && left.index === right.index;
103
+ case "Opaque":
104
+ return right.kind === "Opaque" && left.name === right.name;
105
+ case "Internal":
106
+ return right.kind === "Internal" && left.builtin === right.builtin;
107
+ case "Lambda":
108
+ return (
109
+ right.kind === "Lambda" &&
110
+ left.params.length === right.params.length &&
111
+ isEqualExpr(left.body, right.body)
112
+ );
113
+ case "Apply":
114
+ return (
115
+ right.kind === "Apply" &&
116
+ isEqualExpr(left.operator, right.operator) &&
117
+ isEqualList(left.args, right.args, isEqualExpr)
118
+ );
119
+ case "Sequent":
120
+ return right.kind === "Sequent" && isEqualSequent(left.sequent, right.sequent);
121
+ case "Bang":
122
+ return (
123
+ right.kind === "Bang" &&
124
+ isEqualExpr(left.expression, right.expression) &&
125
+ isEqualList(left.selectors, right.selectors, isEqualSelector)
126
+ );
127
+ case "With":
128
+ return right.kind === "With" && isEqualExpr(left.expression, right.expression);
129
+ case "Let":
130
+ return (
131
+ right.kind === "Let" &&
132
+ isEqualList(left.definitions, right.definitions, isEqualDefn) &&
133
+ isEqualExpr(left.body, right.body)
134
+ );
135
+ case "If":
136
+ return (
137
+ right.kind === "If" &&
138
+ isEqualExpr(left.condition, right.condition) &&
139
+ isEqualExpr(left.consequent, right.consequent) &&
140
+ isEqualExpr(left.alternative, right.alternative)
141
+ );
142
+ case "List":
143
+ return (
144
+ right.kind === "List" &&
145
+ left.bullet === right.bullet &&
146
+ isEqualList(left.items, right.items, isEqualExpr)
147
+ );
148
+ case "Quant":
149
+ return (
150
+ right.kind === "Quant" &&
151
+ left.quantifier === right.quantifier &&
152
+ isEqualList(left.bounds, right.bounds, isEqualBound) &&
153
+ isEqualExpr(left.body, right.body)
154
+ );
155
+ case "Tquant":
156
+ return (
157
+ right.kind === "Tquant" &&
158
+ left.quantifier === right.quantifier &&
159
+ isEqualList(left.names, right.names, (leftName, rightName) => leftName === rightName) &&
160
+ isEqualExpr(left.body, right.body)
161
+ );
162
+ case "Choose":
163
+ return (
164
+ right.kind === "Choose" &&
165
+ isEqualOptional(left.domain, right.domain) &&
166
+ isEqualExpr(left.body, right.body)
167
+ );
168
+ case "SetSt":
169
+ return (
170
+ right.kind === "SetSt" &&
171
+ isEqualExpr(left.domain, right.domain) &&
172
+ isEqualExpr(left.body, right.body)
173
+ );
174
+ case "SetOf":
175
+ return (
176
+ right.kind === "SetOf" &&
177
+ isEqualExpr(left.element, right.element) &&
178
+ isEqualList(left.bounds, right.bounds, isEqualBound)
179
+ );
180
+ case "SetEnum":
181
+ case "Tuple":
182
+ return right.kind === left.kind && isEqualList(left.elements, right.elements, isEqualExpr);
183
+ case "Arrow":
184
+ return (
185
+ right.kind === "Arrow" &&
186
+ isEqualExpr(left.domain, right.domain) &&
187
+ isEqualExpr(left.range, right.range)
188
+ );
189
+ case "Fcn":
190
+ return (
191
+ right.kind === "Fcn" &&
192
+ isEqualList(left.bounds, right.bounds, isEqualBound) &&
193
+ isEqualExpr(left.body, right.body)
194
+ );
195
+ case "FcnApp":
196
+ return (
197
+ right.kind === "FcnApp" &&
198
+ isEqualList([left.function, ...left.args], [right.function, ...right.args], isEqualExpr)
199
+ );
200
+ case "Product":
201
+ return right.kind === "Product" && isEqualList(left.factors, right.factors, isEqualExpr);
202
+ case "Rect":
203
+ case "Record":
204
+ return right.kind === left.kind && isEqualFields(left.fields, right.fields);
205
+ case "Except":
206
+ return (
207
+ right.kind === "Except" &&
208
+ isEqualExpr(left.function, right.function) &&
209
+ isEqualList(
210
+ left.specs,
211
+ right.specs,
212
+ (leftSpec, rightSpec) =>
213
+ isEqualList(leftSpec.trail, rightSpec.trail, (leftPoint, rightPoint) =>
214
+ leftPoint.kind === "Dot"
215
+ ? rightPoint.kind === "Dot" && leftPoint.field === rightPoint.field
216
+ : rightPoint.kind === "Apply" &&
217
+ isEqualExpr(leftPoint.expression, rightPoint.expression),
218
+ ) && isEqualExpr(leftSpec.value, rightSpec.value),
219
+ )
220
+ );
221
+ case "Dot":
222
+ return (
223
+ right.kind === "Dot" &&
224
+ isEqualExpr(left.expression, right.expression) &&
225
+ left.field === right.field
226
+ );
227
+ case "Sub":
228
+ case "Tsub":
229
+ return (
230
+ right.kind === left.kind &&
231
+ left.modal === right.modal &&
232
+ isEqualExpr(left.action, right.action) &&
233
+ isEqualExpr(left.subscript, right.subscript)
234
+ );
235
+ case "Fair":
236
+ return (
237
+ right.kind === "Fair" &&
238
+ left.fairness === right.fairness &&
239
+ isEqualExpr(left.subscript, right.subscript) &&
240
+ isEqualExpr(left.action, right.action)
241
+ );
242
+ case "Case": {
243
+ if (right.kind !== "Case") return false;
244
+ const isArmsEqual =
245
+ left.arms.length === 0 || right.arms.length === 0
246
+ ? left.arms.length === right.arms.length
247
+ : isEqualExpr(left.arms[0].condition, right.arms[0].condition) &&
248
+ isEqualExpr(left.arms[0].body, right.arms[0].body);
249
+ return isArmsEqual && isEqualOptional(left.other, right.other);
250
+ }
251
+ case "String":
252
+ return right.kind === "String" && left.value === right.value;
253
+ case "Num":
254
+ return (
255
+ right.kind === "Num" && left.integer === right.integer && left.fraction === right.fraction
256
+ );
257
+ case "At":
258
+ return right.kind === "At" && left.isExcept === right.isExcept;
259
+ }
260
+ };