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
@@ -0,0 +1,668 @@
1
+ // A port of tlapm's Proof.Simplify, which rewrites each proof into the steps Proof.Gen handles:
2
+ // BY becomes USE steps and an OBVIOUS QED, and HAVE, TAKE, WITNESS, CASE and PICK become
3
+ // assertions.
4
+
5
+ import { TlaProofError } from "../errors";
6
+ import { isEqualExpr } from "./equality";
7
+ import {
8
+ applyBuiltin,
9
+ at,
10
+ getFact,
11
+ isBuiltinApply,
12
+ mapAccumulate,
13
+ mapBoundDomains,
14
+ mapChildren,
15
+ stripProps,
16
+ type Bound,
17
+ type Builtin,
18
+ type Expr,
19
+ type Hyp,
20
+ type Sequent,
21
+ type Time,
22
+ } from "./expression";
23
+ import { format, renderDoc, type Doc } from "./format";
24
+ import { AT_NOWHERE, getTrue, proveAssertion, useAssertion } from "./generate";
25
+ import { printExpression } from "./printer";
26
+ import { getUnique, type Counter, type Proof, type Step, type StepNumber } from "./proof";
27
+ import {
28
+ consSubstitution,
29
+ normalizeApply,
30
+ shift,
31
+ shiftExpr,
32
+ substituteBounds,
33
+ substituteExpr,
34
+ } from "./substitution";
35
+
36
+ // tlapm builds nodes with x @@ step, which copies all of the step's properties.
37
+ const getProps = (from: Step) => ({
38
+ location: from.location,
39
+ step: from.step,
40
+ method: from.method,
41
+ });
42
+
43
+ const getLevel = (step: StepNumber | undefined) => step?.level ?? 0;
44
+
45
+ const isBinary = (
46
+ expression: Expr,
47
+ builtin: Builtin,
48
+ ): expression is Extract<Expr, { kind: "Apply" }> =>
49
+ isBuiltinApply(expression, builtin) && expression.args.length === 2;
50
+
51
+ const getMessage = (pattern: string, ...args: (string | Doc)[]) =>
52
+ renderDoc(format(pattern, ...args));
53
+
54
+ // Visit.hyps_of_bounds_unditto: dittos repeat the previous domain, and each domain shifts past the
55
+ // names bound before it.
56
+ const getBoundHyps = (bounds: Bound[]): Hyp[] =>
57
+ expandDittoes(bounds).map((bound, index): Hyp => {
58
+ const domain =
59
+ bound.domain.kind === "Domain" ? shiftExpr(index, bound.domain.expression) : undefined;
60
+ return {
61
+ kind: "Fresh",
62
+ name: bound.name,
63
+ shape: 0,
64
+ level: domain ? "Constant" : bound.level,
65
+ domain,
66
+ domainVisibility: "Visible",
67
+ };
68
+ });
69
+
70
+ const expandDittoes = (bounds: Bound[]): Bound[] => {
71
+ let domain: Bound["domain"] = { kind: "NoDomain" };
72
+ return bounds.map((bound) => {
73
+ if (bound.domain.kind === "Ditto") return { ...bound, domain };
74
+ domain = bound.domain;
75
+ return bound;
76
+ });
77
+ };
78
+
79
+ // The bounds after a witnessed one, where a ditto takes the witnessed domain.
80
+ const getRemainingBounds = (first: Bound, rest: Bound[]): Bound[] => {
81
+ const shifted = substituteBounds(shift(1), rest)[1];
82
+ return shifted[0].domain.kind === "Ditto" && first.domain.kind === "Domain"
83
+ ? [
84
+ {
85
+ ...shifted[0],
86
+ domain: { kind: "Domain", expression: shiftExpr(1, first.domain.expression) },
87
+ },
88
+ ...shifted.slice(1),
89
+ ]
90
+ : shifted;
91
+ };
92
+
93
+ // A WITNESS proves each membership and subset it relies on, then the goal from them.
94
+ const getWitnessProof = (step: Step, sideFacts: Expr[]): Proof => {
95
+ if (sideFacts.length === 0) return { kind: "Obvious", ...getProps(step) };
96
+ const steps = sideFacts.map((item, index): Step => {
97
+ const active = shiftExpr(2 * index + 2, item);
98
+ return {
99
+ kind: "Assert",
100
+ sequent: { context: [], active },
101
+ proof: {
102
+ kind: "By",
103
+ usable: { facts: [shiftExpr(3, active)], defs: [] },
104
+ isOnly: false,
105
+ location: item.location,
106
+ method: step.method,
107
+ },
108
+ location: item.location,
109
+ };
110
+ });
111
+ const facts = [
112
+ at({ kind: "Ix", index: 2 * sideFacts.length + 2 }, step),
113
+ ...sideFacts.map((_item, index) =>
114
+ at({ kind: "Ix", index: 2 * (sideFacts.length - index) }, step),
115
+ ),
116
+ ];
117
+ // tlapm removes the method from the QED.
118
+ const qedProof: Proof = {
119
+ kind: "By",
120
+ usable: { facts, defs: [] },
121
+ isOnly: true,
122
+ ...getProps(step),
123
+ method: undefined,
124
+ };
125
+ return {
126
+ kind: "Steps",
127
+ steps,
128
+ qed: { proof: qedProof, location: qedProof.location },
129
+ ...getProps(step),
130
+ };
131
+ };
132
+
133
+ const replaceAt = (expression: Expr, replacement: Expr): Expr =>
134
+ expression.kind === "At" && !expression.isExcept
135
+ ? at(stripProps(replacement), expression)
136
+ : mapChildren(expression, (inner) => replaceAt(inner, replacement));
137
+
138
+ export const createSimplifier = (counter: Counter) => {
139
+ const getAnonymous = (level: number): StepNumber => ({
140
+ kind: "Named",
141
+ level,
142
+ label: `_a${getUnique(counter)}`,
143
+ isAnonymous: true,
144
+ });
145
+
146
+ const renumberNumber = (level: number, step: StepNumber | undefined): StepNumber =>
147
+ step?.kind === "Named" ? { ...step, level } : getAnonymous(level);
148
+
149
+ const renumber = (level: number, proof: Proof): Proof => {
150
+ const renumbered: Proof = { ...proof, step: renumberNumber(level, proof.step) };
151
+ if (renumbered.kind !== "Steps") return renumbered;
152
+ const steps = renumbered.steps.map((step) => renumberStep(level, step));
153
+ return {
154
+ ...renumbered,
155
+ steps,
156
+ qed: { ...renumbered.qed, proof: renumber(level + 1, renumbered.qed.proof) },
157
+ };
158
+ };
159
+
160
+ const renumberStep = (level: number, step: Step): Step => {
161
+ const renumbered: Step = { ...step, step: renumberNumber(level, step.step) };
162
+ return "proof" in renumbered
163
+ ? { ...renumbered, proof: renumber(level + 1, renumbered.proof) }
164
+ : renumbered;
165
+ };
166
+
167
+ const exposeConnective = (context: Hyp[], goal: Expr): Expr => {
168
+ const head = goal.kind === "Apply" ? goal.operator : goal;
169
+ if (head.kind === "Ix") {
170
+ const hyp = context[context.length - head.index];
171
+ if (!hyp) throw new TlaProofError("expose_connective");
172
+ if (hyp.kind !== "Defn" || hyp.definition.kind !== "Operator") return goal;
173
+ const body = shiftExpr(head.index, hyp.definition.body);
174
+ if (goal.kind !== "Apply") return exposeConnective(context, body);
175
+ const operator: Expr = { ...body, location: goal.location };
176
+ return exposeConnective(context, at(normalizeApply(operator, goal.args), operator));
177
+ }
178
+ if (isBuiltinApply(goal, "Prime")) {
179
+ const inner = exposeConnective(context, goal.args[0]);
180
+ const prime = (expression: Expr) => applyBuiltin("Prime", [expression], expression);
181
+ if (inner.kind === "Quant") {
182
+ return at(
183
+ {
184
+ kind: "Quant",
185
+ quantifier: inner.quantifier,
186
+ bounds: mapBoundDomains(inner.bounds, prime),
187
+ body: prime(inner.body),
188
+ },
189
+ goal,
190
+ );
191
+ }
192
+ if (isBinary(inner, "Implies") || isBinary(inner, "Mem")) {
193
+ return at(
194
+ {
195
+ kind: "Apply",
196
+ operator: inner.operator,
197
+ args: [prime(inner.args[0]), prime(inner.args[1])],
198
+ },
199
+ goal,
200
+ );
201
+ }
202
+ return prime(inner);
203
+ }
204
+ if (goal.kind === "Parens") return exposeConnective(context, goal.expression);
205
+ return goal;
206
+ };
207
+
208
+ const getRightOperand = (step: Step): Expr | undefined => {
209
+ if (step.kind !== "Assert") return undefined;
210
+ const active = step.sequent.active;
211
+ return active.kind === "Apply" && active.args.length === 2 ? active.args[1] : undefined;
212
+ };
213
+
214
+ // at_expand: @ in a step stands for the right side of the previous step's assertion.
215
+ const expandAt = (proof: Proof): Proof => {
216
+ if (proof.kind !== "Steps") return proof;
217
+ const steps: Step[] = [];
218
+ proof.steps.forEach((step) => {
219
+ const previous = steps.at(-1);
220
+ const right = previous && getRightOperand(previous);
221
+ if (step.kind !== "Assert" || !right) {
222
+ steps.push(step);
223
+ return;
224
+ }
225
+ const active = replaceAt(step.sequent.active, shiftExpr(2, right));
226
+ steps.push({ ...step, sequent: { ...step.sequent, active } });
227
+ });
228
+ return { ...proof, steps };
229
+ };
230
+
231
+ const simplify = (context: Hyp[], goal: Expr, original: Proof, time: Time): Proof => {
232
+ const proof = expandAt(original);
233
+ switch (proof.kind) {
234
+ case "Obvious":
235
+ case "Omitted":
236
+ case "Error":
237
+ return proof;
238
+ case "By": {
239
+ const usable = proof.usable;
240
+ const useDefs: Step[] =
241
+ usable.defs.length === 0
242
+ ? []
243
+ : [
244
+ {
245
+ kind: "Use",
246
+ usable: { facts: [], defs: usable.defs },
247
+ isOnly: false,
248
+ location: proof.location,
249
+ },
250
+ ];
251
+ const useFacts = usable.facts.map((fact, index): Step => ({
252
+ kind: "Use",
253
+ usable: { facts: [shiftExpr(index, fact)], defs: [] },
254
+ isOnly: false,
255
+ location: fact.location,
256
+ useLocation: proof.location,
257
+ }));
258
+ const forget: Step[] = proof.isOnly
259
+ ? [{ kind: "Forget", count: usable.facts.length, location: proof.location }]
260
+ : [];
261
+ const level = getLevel(proof.step);
262
+ const steps = [...useDefs, ...useFacts, ...forget].map((step): Step => ({
263
+ ...step,
264
+ step: getAnonymous(level),
265
+ }));
266
+ const qedProof: Proof = {
267
+ kind: "Obvious",
268
+ location: proof.location,
269
+ step: getAnonymous(level),
270
+ method: proof.method,
271
+ };
272
+ return {
273
+ kind: "Steps",
274
+ steps,
275
+ qed: { proof: qedProof, location: qedProof.location, step: qedProof.step },
276
+ location: proof.location,
277
+ step: proof.step,
278
+ };
279
+ }
280
+ case "Steps": {
281
+ const [inner, steps] = mapAccumulate(
282
+ { context, goal, time },
283
+ proof.steps,
284
+ (state, step) => {
285
+ const [nextContext, nextGoal, simplified, nextTime] = simplifyStep(
286
+ state.context,
287
+ state.goal,
288
+ step,
289
+ state.time,
290
+ );
291
+ return [{ context: nextContext, goal: nextGoal, time: nextTime }, simplified];
292
+ },
293
+ );
294
+ const qedProof = simplify(inner.context, inner.goal, proof.qed.proof, inner.time);
295
+ return { ...proof, steps: steps.flat(), qed: { ...proof.qed, proof: qedProof } };
296
+ }
297
+ }
298
+ };
299
+
300
+ const simplifyStep = (
301
+ context: Hyp[],
302
+ goal: Expr,
303
+ step: Step,
304
+ time: Time,
305
+ ): [Hyp[], Expr, Step[], Time] => {
306
+ const level = getLevel(step.step);
307
+ switch (step.kind) {
308
+ case "Forget":
309
+ return [context, goal, [step], time];
310
+ case "Assert": {
311
+ const inner = proveAssertion(context, step.sequent, step, time);
312
+ const simplified: Step = {
313
+ ...step,
314
+ proof: simplify(inner.context, inner.active, step.proof, inner.time),
315
+ };
316
+ const next = useAssertion(false, context, goal, step.sequent, simplified, time);
317
+ return [next.context, next.active, [simplified], time];
318
+ }
319
+ case "Suffices": {
320
+ const inner = useAssertion(true, context, goal, step.sequent, step, time);
321
+ const simplified: Step = {
322
+ ...step,
323
+ proof: simplify(inner.context, inner.active, step.proof, time),
324
+ };
325
+ const next = proveAssertion(context, step.sequent, simplified, time);
326
+ return [next.context, next.active, [simplified], next.time];
327
+ }
328
+ case "Use": {
329
+ const byProof: Proof = {
330
+ kind: "By",
331
+ usable: step.usable,
332
+ isOnly: step.isOnly,
333
+ location: step.location,
334
+ step: step.step,
335
+ method: step.method,
336
+ };
337
+ const simplified = simplify(context, goal, byProof, time);
338
+ if (simplified.kind !== "Steps")
339
+ throw new TlaProofError("simplifying BY does not produce a non-leaf proof!");
340
+ const assumed = step.usable.facts.map(() =>
341
+ getFact(AT_NOWHERE, "Visible", time, step.location),
342
+ );
343
+ return [
344
+ [...context, ...assumed],
345
+ shiftExpr(step.usable.facts.length, goal),
346
+ simplified.steps,
347
+ time,
348
+ ];
349
+ }
350
+ case "Hide": {
351
+ const hidden = step.usable.defs.reduce((current, def) => {
352
+ if (def.kind !== "Dx") throw new TlaProofError("set_vis");
353
+ const index = current.length - def.index;
354
+ const hyp = current[index];
355
+ if (hyp?.kind !== "Defn") {
356
+ const name = hyp ? (hyp.kind === "Fact" ? "??? FACT ???" : hyp.name) : "";
357
+ throw new TlaProofError(
358
+ `USE DEF ${name} invalid because ${name} is not a defined operator`,
359
+ );
360
+ }
361
+ return current.map((entry, position) =>
362
+ position === index
363
+ ? { ...hyp, visibility: "Hidden" as const, location: step.location }
364
+ : entry,
365
+ );
366
+ }, context);
367
+ return [hidden, goal, [step], time];
368
+ }
369
+ case "Define":
370
+ return [
371
+ [
372
+ ...context,
373
+ ...step.definitions.map((definition): Hyp => ({
374
+ kind: "Defn",
375
+ definition,
376
+ whereDefined: { kind: "Proof", time },
377
+ visibility: "Visible",
378
+ isLocal: true,
379
+ location: step.location,
380
+ })),
381
+ ],
382
+ shiftExpr(step.definitions.length, goal),
383
+ [step],
384
+ time,
385
+ ];
386
+ case "Have": {
387
+ const connective = exposeConnective(context, goal);
388
+ const isImplication = isBinary(connective, "Implies");
389
+ const sequent: Sequent = {
390
+ context: [getFact(step.expression, "Visible", time, step.location)],
391
+ active: isImplication ? shiftExpr(1, connective.args[1]) : connective,
392
+ };
393
+ const proof: Proof = isImplication
394
+ ? {
395
+ kind: "By",
396
+ usable: { facts: [at({ kind: "Ix", index: 2 }, step)], defs: [] },
397
+ isOnly: true,
398
+ ...getProps(step),
399
+ }
400
+ : {
401
+ kind: "Error",
402
+ message: getMessage(
403
+ "@.@[<v2>@[%s@ %s@]@,%a@]@.",
404
+ "Error: this HAVE step is applied to the following goal,",
405
+ "which is not an implication:",
406
+ printExpression(context, goal),
407
+ ),
408
+ ...getProps(step),
409
+ };
410
+ const suffices = renumberStep(level, {
411
+ kind: "Suffices",
412
+ sequent,
413
+ proof,
414
+ location: step.location,
415
+ step: step.step,
416
+ });
417
+ return simplifyStep(context, goal, suffices, time);
418
+ }
419
+ case "Take":
420
+ return simplifyStep(
421
+ context,
422
+ goal,
423
+ renumberStep(level, getTakeSuffices(context, goal, step)),
424
+ time,
425
+ );
426
+ case "Witness":
427
+ return simplifyStep(
428
+ context,
429
+ goal,
430
+ renumberStep(level, getWitnessSuffices(context, goal, step)),
431
+ time,
432
+ );
433
+ case "Pcase": {
434
+ const assertion: Step = {
435
+ kind: "Assert",
436
+ sequent: {
437
+ context: [getFact(step.expression, "Visible", time, step.location)],
438
+ active: shiftExpr(1, goal),
439
+ },
440
+ proof: step.proof,
441
+ ...getProps(step),
442
+ };
443
+ return simplifyStep(context, goal, renumberStep(level, assertion), time);
444
+ }
445
+ case "Pick": {
446
+ const existential: Expr = at(
447
+ { kind: "Quant", quantifier: "Exists", bounds: step.bounds, body: step.expression },
448
+ step,
449
+ );
450
+ const salted = getSalted(step.step, "ex");
451
+ const [assertContext, assertGoal, asserted, assertTime] = simplifyStep(
452
+ context,
453
+ goal,
454
+ {
455
+ kind: "Assert",
456
+ sequent: { context: [], active: existential },
457
+ proof: step.proof,
458
+ ...getProps(step),
459
+ step: salted,
460
+ },
461
+ time,
462
+ );
463
+ const shifted = shiftExpr(2, existential);
464
+ if (shifted.kind !== "Quant") throw new TlaProofError("Proof.Simplify.simplify_step/PICK");
465
+ const hyps = [
466
+ ...getBoundHyps(shifted.bounds),
467
+ getFact(shifted.body, "Visible", assertTime, shifted.body.location),
468
+ ];
469
+ const [nextContext, nextGoal, sufficed, nextTime] = simplifyStep(
470
+ assertContext,
471
+ assertGoal,
472
+ {
473
+ kind: "Suffices",
474
+ sequent: { context: hyps, active: shiftExpr(hyps.length, assertGoal) },
475
+ proof: { kind: "Omitted", omission: "Elsewhere", ...getProps(step) },
476
+ ...getProps(step),
477
+ },
478
+ assertTime,
479
+ );
480
+ return [nextContext, nextGoal, [...asserted, ...sufficed], nextTime];
481
+ }
482
+ }
483
+ };
484
+
485
+ const getSalted = (step: StepNumber | undefined, salt: string): StepNumber => ({
486
+ kind: "Named",
487
+ level: getLevel(step),
488
+ label: `${step?.kind === "Named" ? step.label : "_a"}${salt}${getUnique(counter)}`,
489
+ isAnonymous: false,
490
+ });
491
+
492
+ const getTakeSuffices = (context: Hyp[], goal: Expr, step: Step & { kind: "Take" }): Step => {
493
+ const bounds = expandDittoes(step.bounds);
494
+ const stripBounds = (
495
+ amount: number,
496
+ sideFacts: Expr[],
497
+ current: Expr,
498
+ rest: Bound[],
499
+ ): { sideFacts: Expr[]; goal: Expr } | string => {
500
+ const bound = rest[0];
501
+ if (!bound) return { sideFacts, goal: current };
502
+ const exposed = exposeConnective(context, current);
503
+ if (
504
+ exposed.kind !== "Quant" ||
505
+ exposed.quantifier !== "Forall" ||
506
+ exposed.bounds.length === 0
507
+ ) {
508
+ return getMessage(
509
+ "@.@[<v2>@[<b0>%s@ %s@]@,%a@]@.",
510
+ "Error: this TAKE step is applied to the following goal,",
511
+ "which is not a universal formula:",
512
+ printExpression(context, current),
513
+ );
514
+ }
515
+ const goalBound = exposed.bounds[0];
516
+ const goalBounds = expandDittoes(exposed.bounds).slice(1);
517
+ const domain = bound.domain;
518
+ const goalDomain = goalBound.domain;
519
+ if (domain.kind === "NoDomain" && goalDomain.kind === "Domain") {
520
+ return getMessage(
521
+ "@.@[<b0>%s (%s)@ %s (%s \\in %a)@]@.",
522
+ "Error: the TAKE argument",
523
+ bound.name,
524
+ "does not match the pattern",
525
+ goalBound.name,
526
+ printExpression(context, goalDomain.expression),
527
+ );
528
+ }
529
+ if (domain.kind === "Domain" && goalDomain.kind === "NoDomain") {
530
+ return getMessage(
531
+ "@.@[<b0>%s (%s \\in %a)@ %s (%s)@]@.",
532
+ "Error: the TAKE argument",
533
+ bound.name,
534
+ printExpression(context, domain.expression),
535
+ "does not match the pattern",
536
+ goalBound.name,
537
+ );
538
+ }
539
+ const nextSideFacts =
540
+ domain.kind === "Domain" &&
541
+ goalDomain.kind === "Domain" &&
542
+ !isEqualExpr(domain.expression, goalDomain.expression)
543
+ ? [
544
+ {
545
+ kind: "Apply",
546
+ operator: { kind: "Internal", builtin: "Subseteq" },
547
+ args: [goalDomain.expression, domain.expression],
548
+ } satisfies Expr,
549
+ ...sideFacts,
550
+ ]
551
+ : sideFacts;
552
+ if (goalBounds.length === 0)
553
+ return stripBounds(1, nextSideFacts, exposed.body, rest.slice(1));
554
+ const shiftedBounds =
555
+ rest.length === 1
556
+ ? goalBounds.map((item) => substituteBounds(shift(amount), [item])[1][0])
557
+ : goalBounds;
558
+ return stripBounds(
559
+ amount + 1,
560
+ nextSideFacts,
561
+ at(
562
+ { kind: "Quant", quantifier: "Forall", bounds: shiftedBounds, body: exposed.body },
563
+ exposed,
564
+ ),
565
+ rest.slice(1),
566
+ );
567
+ };
568
+ const stripped = stripBounds(1, [], goal, bounds);
569
+ if (typeof stripped === "string") {
570
+ return {
571
+ kind: "Suffices",
572
+ sequent: {
573
+ context: [getFact(getTrue(goal), "Visible", "Now", step.location)],
574
+ active: goal,
575
+ },
576
+ proof: { kind: "Error", message: stripped, ...getProps(step) },
577
+ ...getProps(step),
578
+ };
579
+ }
580
+ const unique = stripped.sideFacts.filter(
581
+ (item, index) => stripped.sideFacts.findIndex((other) => isEqualExpr(item, other)) === index,
582
+ );
583
+ const facts = [at({ kind: "Ix", index: 2 }, step), ...unique.map((item) => shiftExpr(2, item))];
584
+ return {
585
+ kind: "Suffices",
586
+ sequent: { context: getBoundHyps(bounds), active: stripped.goal },
587
+ proof: { kind: "By", usable: { facts, defs: [] }, isOnly: true, ...getProps(step) },
588
+ ...getProps(step),
589
+ };
590
+ };
591
+
592
+ const getWitnessSuffices = (
593
+ context: Hyp[],
594
+ goal: Expr,
595
+ step: Step & { kind: "Witness" },
596
+ ): Step => {
597
+ const sideFacts: Expr[] = [];
598
+ let error = "";
599
+ let current = goal;
600
+ for (const witness of step.expressions) {
601
+ const exposed = exposeConnective(context, current);
602
+ if (
603
+ exposed.kind !== "Quant" ||
604
+ exposed.quantifier !== "Exists" ||
605
+ exposed.bounds.length === 0
606
+ ) {
607
+ error ||= getMessage(
608
+ "@.@[<v2>@[<b0>%s@ %s@]@,%a@]@.",
609
+ "Error: this WITNESS step is applied to the following goal,",
610
+ "which is not an existential formula:",
611
+ printExpression(context, exposed),
612
+ );
613
+ current = exposed;
614
+ break;
615
+ }
616
+ const [first, ...rest] = exposed.bounds;
617
+ const next =
618
+ rest.length === 0
619
+ ? exposed.body
620
+ : at(
621
+ {
622
+ kind: "Quant",
623
+ quantifier: "Exists",
624
+ bounds: getRemainingBounds(first, rest),
625
+ body: exposed.body,
626
+ },
627
+ exposed,
628
+ );
629
+ let value = witness;
630
+ if (first.domain.kind === "Domain") {
631
+ const domain = first.domain.expression;
632
+ const membership = exposeConnective(context, witness);
633
+ if (isBinary(membership, "Mem")) {
634
+ const [term, termDomain] = membership.args;
635
+ const subset = isEqualExpr(termDomain, domain)
636
+ ? []
637
+ : [applyBuiltin("Subseteq", [termDomain, domain], termDomain)];
638
+ sideFacts.push(...subset, membership);
639
+ value = term;
640
+ } else {
641
+ error ||= getMessage(
642
+ "@.@[<b0>%s (%a)@ %s (%s \\in %a)@]@.",
643
+ "Error: the WITNESS argument",
644
+ printExpression(context, membership),
645
+ "does not match the pattern",
646
+ first.name,
647
+ printExpression(context, domain),
648
+ );
649
+ value = membership;
650
+ }
651
+ }
652
+ const unprimable = applyBuiltin("Unprimable", [value], value);
653
+ current = substituteExpr(consSubstitution(unprimable, shift(0)), next);
654
+ }
655
+ const proof: Proof =
656
+ error === ""
657
+ ? getWitnessProof(step, sideFacts)
658
+ : { kind: "Error", message: error, ...getProps(step) };
659
+ return {
660
+ kind: "Suffices",
661
+ sequent: { context: [], active: current },
662
+ proof,
663
+ ...getProps(step),
664
+ };
665
+ };
666
+
667
+ return simplify;
668
+ };