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
@@ -12,10 +12,10 @@ import {
12
12
  writeFileSync,
13
13
  } from "node:fs";
14
14
  import { basename, join } from "node:path";
15
- import { version } from "../package.json";
16
- import { TlaProofError } from "./errors";
15
+ import { version } from "../../../package.json";
16
+ import { TlaProofError } from "../../errors";
17
17
  import { block, isBlock, readMarshaled, writeMarshaled, type OcamlValue } from "./ocaml-marshal";
18
- import type { ProofAttempt } from "./prove/prove";
18
+ import type { ProofAttempt } from "../prove";
19
19
 
20
20
  // One optional result per prover, in V13.str's order.
21
21
  export type FingerprintTable = Map<string, OcamlValue[]>;
@@ -82,13 +82,13 @@ const readBucket = (entry: OcamlValue | undefined): [OcamlValue, OcamlValue][] =
82
82
  export const loadFingerprints = (table: FingerprintTable, path: string, isSafe: boolean) => {
83
83
  const bytes = readFileSync(path);
84
84
  const magic = readMarshaled(bytes, 0);
85
- const fp = readMarshaled(bytes, magic.next).value;
86
- if (magic.value !== MAGIC_NUMBER || !isBlock(fp) || fp.tag !== 0)
85
+ const contents = readMarshaled(bytes, magic.next).value;
86
+ if (magic.value !== MAGIC_NUMBER || !isBlock(contents) || contents.tag !== 0)
87
87
  throw new TlaProofError("corrupted fingerprint file");
88
- const buckets = getField(getField(fp, 0), 1);
89
- const isTrusted = (sti: OcamlValue, field: number) => {
88
+ const buckets = getField(getField(contents, 0), 1);
89
+ const isTrusted = (result: OcamlValue, field: number) => {
90
90
  const index = CHECKED_VERSIONS.get(field);
91
- return !isSafe || index === undefined || getField(getField(sti, 0), index) === VERSION;
91
+ return !isSafe || index === undefined || getField(getField(result, 0), index) === VERSION;
92
92
  };
93
93
  table.clear();
94
94
  (buckets !== undefined && isBlock(buckets) ? buckets.fields : [])
@@ -98,13 +98,13 @@ export const loadFingerprints = (table: FingerprintTable, path: string, isSafe:
98
98
  throw new TlaProofError("corrupted fingerprint file");
99
99
  table.set(
100
100
  key,
101
- record.fields.map((sti, field) => (isTrusted(sti, field) ? sti : 0)),
101
+ record.fields.map((result, field) => (isTrusted(result, field) ? result : 0)),
102
102
  );
103
103
  });
104
104
  };
105
105
 
106
- const decodeResult = (prover: string, sti: OcamlValue | undefined): ProofAttempt | undefined => {
107
- const status = getField(sti, 0);
106
+ const decodeResult = (prover: string, result: OcamlValue | undefined): ProofAttempt | undefined => {
107
+ const status = getField(result, 0);
108
108
  const outcome = getField(status, 0);
109
109
  const argument = getField(getField(status, 1), 0);
110
110
  if (outcome === undefined || outcome === 1) return undefined;
@@ -1,7 +1,7 @@
1
1
  // OCaml's Marshal format, ported from runtime/intern.c and runtime/extern.c, for the values in
2
2
  // tlapm's fingerprints file. It writes no sharing, which OCaml reads the same.
3
3
 
4
- import { TlaProofError } from "./errors";
4
+ import { TlaProofError } from "../../errors";
5
5
 
6
6
  interface OcamlBlock {
7
7
  tag: number;
@@ -11,8 +11,3 @@ export interface DecideOptions {
11
11
  timeLimitMs: number;
12
12
  signal?: AbortSignal;
13
13
  }
14
-
15
- // V8's error when a recursion runs out of stack. The backends run as their own processes, which grow
16
- // their stacks or fail the obligation alone, so a backend ported in process answers unknown instead.
17
- export const isStackOverflow = (error: unknown) =>
18
- error instanceof RangeError && error.message === "Maximum call stack size exceeded";
@@ -1,14 +1,8 @@
1
1
  // Ported from Zenon's main.ml, as shipped in tlapm. Copyright 1997 INRIA, see LICENSE.
2
2
  //
3
- // Runs Zenon as tlapm does on a .znn file. A proof counts only once the independent checker
4
- // accepts it.
5
- //
6
- // tlapm passes -max-time 1d, so Zenon's own clock never stops it. The time limit is tlapm's
7
- // timeout instead: Schedule.run kills Zenon once the wall clock passes its launch time plus the
8
- // timeout, whether Zenon is still reading the file, searching or printing the proof.
9
- //
10
- // tlapm also passes -oisar, so Zenon prints the proof as Isar text. tlapm writes that output,
11
- // after a comment with Zenon's input, into the module's .thy file for Isabelle to check.
3
+ // Runs Zenon on a .znn file as tlapm does, with -oisar. A proof counts only once the independent
4
+ // checker accepts it. tlapm passes -max-time 1d and kills Zenon at its own timeout instead, whether
5
+ // Zenon is still reading, searching or printing the proof.
12
6
 
13
7
  import { TlaZenonError } from "../../errors";
14
8
  import type { DecideOptions } from "../verdict";
@@ -24,7 +18,7 @@ import type { Proof } from "./proof";
24
18
  import { search, type SearchFailure } from "./search";
25
19
  import { getBuiltInDefinitions, PREDEFINED_SYMBOLS } from "./tla/tla";
26
20
 
27
- export interface ZenonRun {
21
+ interface ZenonRun {
28
22
  proof: Proof | undefined;
29
23
  // Why Zenon gave up: in the search, or at the limits while it checked or printed the proof.
30
24
  // "zenon error" stands for the exceptions that stop Zenon mid-search.
@@ -103,7 +97,7 @@ export const runZenon = (text: string, options: DecideOptions): ZenonRun => {
103
97
  };
104
98
  };
105
99
 
106
- export type ZenonDecision = { verdict: "unsat"; proof: string } | { verdict: "unknown" };
100
+ type ZenonDecision = { verdict: "unsat"; proof: string } | { verdict: "unknown" };
107
101
 
108
102
  // Zenon's verdict on a .znn file: "unsat" with the Isar proof that tlapm writes, when Zenon refutes
109
103
  // the hypotheses with a proof that the checker accepts and prints it within the time limit.
@@ -2,17 +2,10 @@
2
2
  // each visible fact and a $goal, naming identifiers as Isabelle does.
3
3
 
4
4
  import { TlapmBugError, TlaProverError, TlaUnsupportedError } from "../../errors";
5
- import { createContext, getBoundName, popNames, pushName } from "../../obligations/ctx";
6
- import {
7
- shiftExpr,
8
- type Bound,
9
- type Builtin,
10
- type Expr,
11
- type Hyp,
12
- type Sequent,
13
- } from "../../obligations/expression";
5
+ import { createContext, getBoundName, popNames, pushName } from "../../obligations/printer";
6
+ import type { Bound, Builtin, Expr, Hyp, Sequent } from "../../obligations/expression";
14
7
  import { format, formatDelimited, renderDoc, text, type Doc } from "../../obligations/format";
15
- import { pickle } from "../../obligations/pickle";
8
+ import { getUnprintableError, pickle, printFact, splitFirstBound } from "../first-order";
16
9
 
17
10
  const SPACE = format("@ ");
18
11
 
@@ -76,17 +69,6 @@ const OPERATORS: Partial<Record<Builtin, [string, number]>> = {
76
69
  ToString: ["TLA.ToString", 1],
77
70
  };
78
71
 
79
- // The temporal operators Zenon has no counterpart for, by the name tlapm reports.
80
- const TEMPORAL_OPERATORS: Partial<Record<Builtin, [string, number]>> = {
81
- Leadsto: ["Leadsto", 2],
82
- ENABLED: ["ENABLED", 1],
83
- Cdot: ["\\cdot", 2],
84
- Actplus: ["-+->", 2],
85
- BoxTrue: ["[]", 1],
86
- BoxFalse: ["[]", 1],
87
- Diamond: ["<>", 1],
88
- };
89
-
90
72
  const getSuccessors = (integer: string) => {
91
73
  const count = Number.parseInt(integer, 10);
92
74
  return `${"(TLA.fapply TLA.Succ ".repeat(count)}0${")".repeat(count)}`;
@@ -104,9 +86,9 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
104
86
  const nonatomic = (name: string, args: Expr[]) => format("(%s %a)", name, delimited(args));
105
87
 
106
88
  // A binder's name binds in the body, and what comes before prints outside it.
107
- const printBinder = (name: string, before: Doc, body: Expr, after: string) => {
89
+ const printBinder = (name: string, before: Doc, body: Expr) => {
108
90
  const bound = adjoin(name);
109
- const doc = format("%a((%s) %a)%s", before, bound, printExpr(body), after);
91
+ const doc = format("%a((%s) %a))", before, bound, printExpr(body));
110
92
  popNames(context, 1);
111
93
  return doc;
112
94
  };
@@ -139,12 +121,7 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
139
121
  }
140
122
  }
141
123
  if (builtin === "Unprimable" && args.length === 1) return printExpr(args[0]);
142
- const isFrontend = builtin === "Prime" || builtin === "StrongPrime" || builtin === "UNCHANGED";
143
- if (isFrontend && args.length === 1) throw new TlapmBugError();
144
- const temporal = TEMPORAL_OPERATORS[builtin];
145
- if (temporal && temporal[1] === args.length)
146
- throw new TlaProverError(`Unsupported operator \`${temporal[0]}\`.\n`);
147
- throw new TlaProverError("Backend.Zenon.fmt_apply");
124
+ throw getUnprintableError(builtin, args.length, "Backend.Zenon.fmt_apply");
148
125
  };
149
126
 
150
127
  const printApply = (operator: Expr, args: Expr[]): Doc => {
@@ -171,42 +148,18 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
171
148
  }
172
149
  };
173
150
 
174
- // The first bound binds around a quantifier over the others, which move under it.
175
151
  const printQuant = (quantifier: "Forall" | "Exists", bounds: Bound[], body: Expr): Doc => {
176
152
  if (bounds.length === 0) return printExpr(body);
177
- const bound = bounds[0];
178
- const rest = bounds.slice(1);
179
- const undittoed =
180
- rest[0]?.domain.kind === "Ditto"
181
- ? [{ ...rest[0], domain: bound.domain }, ...rest.slice(1)]
182
- : rest;
183
- const inner: Expr =
184
- undittoed.length === 0
185
- ? body
186
- : {
187
- kind: "Quant",
188
- quantifier,
189
- bounds: undittoed.map((other) =>
190
- other.domain.kind === "Domain"
191
- ? {
192
- ...other,
193
- domain: { kind: "Domain", expression: shiftExpr(1, other.domain.expression) },
194
- }
195
- : other,
196
- ),
197
- body,
198
- location: body.location,
199
- };
153
+ const split = splitFirstBound(quantifier, bounds, body);
200
154
  const isForall = quantifier === "Forall";
201
- if (bound.domain.kind === "NoDomain")
202
- return printBinder(bound.name, text(isForall ? "(A. " : "(E. "), inner, ")");
203
- if (bound.domain.kind !== "Domain") throw new TlapmBugError();
204
- const domain = printExpr(bound.domain.expression);
155
+ if (split.first.domain.kind === "NoDomain")
156
+ return printBinder(split.first.name, text(isForall ? "(A. " : "(E. "), split.inner);
157
+ if (split.first.domain.kind !== "Domain") throw new TlapmBugError();
158
+ const domain = printExpr(split.first.domain.expression);
205
159
  return printBinder(
206
- bound.name,
160
+ split.first.name,
207
161
  format("(%s %a ", isForall ? "TLA.bAll" : "TLA.bEx", domain),
208
- inner,
209
- ")",
162
+ split.inner,
210
163
  );
211
164
  };
212
165
 
@@ -219,7 +172,6 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
219
172
  bound.name,
220
173
  format("(%s %a ", name, printExpr(bound.domain.expression)),
221
174
  body,
222
- ")",
223
175
  );
224
176
  };
225
177
 
@@ -293,15 +245,13 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
293
245
  expression.name,
294
246
  format("(TLA.bChoice %a ", printExpr(expression.domain)),
295
247
  expression.body,
296
- ")",
297
248
  )
298
- : printBinder(expression.name, text("(t. "), expression.body, ")");
249
+ : printBinder(expression.name, text("(t. "), expression.body);
299
250
  case "SetSt":
300
251
  return printBinder(
301
252
  expression.name,
302
253
  format("(TLA.subsetOf %a ", printExpr(expression.domain)),
303
254
  expression.body,
304
- ")",
305
255
  );
306
256
  case "SetOf":
307
257
  return printOneBound("TLA.setOfAll", expression.bounds, expression.element, "Setof");
@@ -315,10 +265,10 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
315
265
  text(")"),
316
266
  ];
317
267
  case "FcnApp": {
318
- const fcn = printExpr(expression.function);
268
+ const printedFunction = printExpr(expression.function);
319
269
  return expression.args.length === 1
320
- ? format("(TLA.fapply %a %a)", fcn, printExpr(expression.args[0]))
321
- : format("(TLA.fapply %a (TLA.tuple %a))", fcn, delimited(expression.args));
270
+ ? format("(TLA.fapply %a %a)", printedFunction, printExpr(expression.args[0]))
271
+ : format("(TLA.fapply %a (TLA.tuple %a))", printedFunction, delimited(expression.args));
322
272
  }
323
273
  case "Arrow":
324
274
  return format(
@@ -385,18 +335,6 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
385
335
  }
386
336
  };
387
337
 
388
- // tlapm prints a fact to a formatter that drops its output first, to omit an unsupported one.
389
- const getFactDoc = (expression: Expr) => {
390
- const depth = context.stack.length;
391
- try {
392
- return printExpr(expression);
393
- } catch (error) {
394
- if (!(error instanceof TlaUnsupportedError)) throw error;
395
- popNames(context, context.stack.length - depth);
396
- return error;
397
- }
398
- };
399
-
400
338
  const printHyp = (hyp: Hyp): Doc => {
401
339
  switch (hyp.kind) {
402
340
  case "Fresh": {
@@ -416,7 +354,10 @@ export const encodeZenon = (id: number, sequent: Sequent) => {
416
354
  }
417
355
  case "Fact": {
418
356
  const count = String(context.stack.length);
419
- const fact = hyp.visibility === "Visible" ? getFactDoc(hyp.expression) : undefined;
357
+ const fact =
358
+ hyp.visibility === "Visible"
359
+ ? printFact(context, () => printExpr(hyp.expression))
360
+ : undefined;
420
361
  bump();
421
362
  if (fact instanceof TlaUnsupportedError)
422
363
  return format("; omitted temporal assumption : %s @.", fact.message);
@@ -16,7 +16,7 @@ export interface Origin {
16
16
  variables: number[];
17
17
  }
18
18
 
19
- export interface RelationInfo {
19
+ interface RelationInfo {
20
20
  reflexive: Origin | undefined;
21
21
  symmetric: Origin | undefined;
22
22
  transitive: Origin | undefined;
@@ -20,7 +20,7 @@ import { insertNode, type Priority, type Queue } from "./node";
20
20
  import { makeRelationRule, makeRule } from "./proof";
21
21
  import { insertAll, makeNode, unequal, type SearchContext, type SearchRule } from "./rules";
22
22
 
23
- export const makeInstance = (
23
+ const makeInstance = (
24
24
  exprs: Expressions,
25
25
  queue: Queue,
26
26
  quantified: Expr,
@@ -115,7 +115,7 @@ const isBinary = (expression: Expr): expression is BinaryExpr =>
115
115
  expression.kind === "imply" ||
116
116
  expression.kind === "equiv";
117
117
 
118
- export const isBinder = (expression: Expr): expression is BinderExpr =>
118
+ const isBinder = (expression: Expr): expression is BinderExpr =>
119
119
  expression.kind === "all" ||
120
120
  expression.kind === "ex" ||
121
121
  expression.kind === "tau" ||
@@ -253,7 +253,7 @@ export const compareExprs = (left: Expr, right: Expr): number => {
253
253
  return 0;
254
254
  };
255
255
 
256
- export type Substitution = [Expr, Expr][];
256
+ type Substitution = [Expr, Expr][];
257
257
 
258
258
  const lookup = (map: Substitution, variable: Expr) => map.find((pair) => pair[0] === variable)?.[1];
259
259
 
@@ -45,7 +45,7 @@ const getLatestFirst = <Key, Value>(table: Map<Key, Value[]>, key: Key) =>
45
45
  [...(table.get(key) ?? [])].reverse();
46
46
 
47
47
  // Zenon's head of a term, or undefined for No_head. Taus and metavariables are heads by identity.
48
- export type Head = string | Expr;
48
+ type Head = string | Expr;
49
49
 
50
50
  export const getHead = (expression: Expr): Head | undefined => {
51
51
  switch (expression.kind) {
@@ -105,7 +105,7 @@ const increment = (indent: number) => (indent >= 15 ? indent : indent + 1);
105
105
  // What the search knew when it found a proof: the .znn file's phrases, the proofs that it reused
106
106
  // and the properties of relations that the hypotheses stated. The printer stops at the search's
107
107
  // limits.
108
- export interface IsarContext {
108
+ interface IsarContext {
109
109
  exprs: Expressions;
110
110
  phrases: Phrase[];
111
111
  sharedProofs: ReadonlySet<MlProof>;
@@ -53,7 +53,7 @@ export const connect = (exprs: Expressions, connective: Connective, left: Expr,
53
53
  ? exprs.eimply(left, right)
54
54
  : exprs.eequiv(left, right);
55
55
 
56
- export const getLemma = (lemmas: Map<string, Lemma>, name: string) => {
56
+ const getLemma = (lemmas: Map<string, Lemma>, name: string) => {
57
57
  const lemma = lemmas.get(name);
58
58
  if (lemma === undefined) throw new TlaZenonError(`Zenon: no lemma ${name}`);
59
59
  return lemma;
@@ -30,7 +30,7 @@ export interface MlProof {
30
30
  // A translated proof, with the hypotheses it uses that aren't conclusions of the node.
31
31
  export type Translated = [LlProof, Expr[]];
32
32
 
33
- export interface TranslateContext {
33
+ interface TranslateContext {
34
34
  exprs: Expressions;
35
35
  // Zenon's Index.get_number, which also names the formulas in the Isar output.
36
36
  getNumber: (expression: Expr) => number;
@@ -17,7 +17,7 @@ export interface Node {
17
17
  branches: Expr[][];
18
18
  }
19
19
 
20
- export interface Cons<Item> {
20
+ interface Cons<Item> {
21
21
  head: Item;
22
22
  tail: Cons<Item> | undefined;
23
23
  }
@@ -135,7 +135,7 @@ interface Frame {
135
135
  current: number;
136
136
  }
137
137
 
138
- export interface SearchStatistics {
138
+ interface SearchStatistics {
139
139
  inferences: number;
140
140
  proofNodes: number;
141
141
  // The proofs that closed another branch from the lemma cache. Zenon counts the uses in mlrefc and
@@ -40,25 +40,6 @@ export const SET_CONSTRUCTORS = [
40
40
 
41
41
  export const FUNCTION_CONSTRUCTORS = ["TLA.Fcn", "TLA.except", "TLA.oneArg", "TLA.extend"];
42
42
 
43
- const OTHER_SYMBOLS = [
44
- "TLA.in",
45
- "TLA.isAFcn",
46
- "TLA.cond",
47
- "TLA.CASE",
48
- "TLA.tuple",
49
- "TLA.record",
50
- "TLA.fapply",
51
- "TLA.box",
52
- ];
53
-
54
- // The symbols that hypotheses can't define, as Zenon's predef with the tla extension.
55
- export const PREDEFINED_SYMBOLS = [
56
- "=",
57
- ...SET_CONSTRUCTORS,
58
- ...FUNCTION_CONSTRUCTORS,
59
- ...OTHER_SYMBOLS,
60
- ];
61
-
62
43
  export const isVariable = (expression: Expr, name: string) =>
63
44
  expression.kind === "var" && expression.symbol === name;
64
45
 
@@ -68,7 +49,7 @@ export const isConstructor = (constructors: string[], expression: Expr) =>
68
49
 
69
50
  export const isNotEquiv = (expression: Expr) => isApp(expression, "$notequiv");
70
51
 
71
- export const isString = (expression: Expr) => isApp(expression, "$string");
52
+ const isString = (expression: Expr) => isApp(expression, "$string");
72
53
 
73
54
  export const equal = (exprs: Expressions, left: Expr, right: Expr) =>
74
55
  exprs.eapp("=", [left, right]);
@@ -481,7 +481,7 @@ const getEqualityPropositionNodes = (
481
481
  ];
482
482
  };
483
483
 
484
- export interface ExtensionNodes {
484
+ interface ExtensionNodes {
485
485
  nodes: Node[];
486
486
  isStopped: boolean;
487
487
  }
@@ -1,12 +1,15 @@
1
- import { createJavaOverrideError, defineStateOperator, getValue } from "./builtins";
2
- import { TlaModuleError } from "./errors";
1
+ import {
2
+ createJavaOverrideError,
3
+ createModuleArgumentError,
4
+ defineStateOperator,
5
+ getValue,
6
+ } from "./builtins";
3
7
  import type { Builtin, BuiltinArgument } from "./evaluate";
4
8
  import type { CheckerRuntime } from "./runtime";
5
- import { getEnumeratedFactors, getRandomSubset, type JavaRandom } from "./tlc-random";
9
+ import { getEnumeratedFactors, getRandomSubset, type JavaRandom } from "./tlc/random";
6
10
  import {
7
11
  createSet,
8
12
  createTuple,
9
- formatValue,
10
13
  isEmptySet,
11
14
  isEnumerable,
12
15
  isSetValue,
@@ -20,25 +23,13 @@ const RANDOM_SUBSET_SIGNATURE =
20
23
  // TLCGlobals.setBound: TLC 1.7.4's ValueVec holds at most this many elements of a set.
21
24
  const SET_BOUND_174 = 1000000;
22
25
 
23
- const createArgumentError = (position: string, name: string, expected: string, value: string) =>
24
- new TlaModuleError(
25
- `The ${position} argument of ${name} should be a ${expected}, but instead it is:\n${value}`,
26
- );
27
-
28
26
  const getCount = (value: Value, position: string, name: string) => {
29
27
  if (typeof value !== "number" || value < 0) {
30
- throw createArgumentError(position, name, "nonnegative integer", formatValue(value));
28
+ throw createModuleArgumentError(position, name, "nonnegative integer", value);
31
29
  }
32
30
  return value;
33
31
  };
34
32
 
35
- const getFiniteElements = (value: Value, position: string, name: string, expected: string) => {
36
- if (!isSetValue(value) || !isEnumerable(value)) {
37
- throw createArgumentError(position, name, expected, formatValue(value));
38
- }
39
- return toFiniteSet(value).elements;
40
- };
41
-
42
33
  // SubsetValue.CoinTossingSubsetEnumerator: each pick keeps each element with the probability.
43
34
  const tossSubsets = (elements: Value[], picks: number, probability: number, random: JavaRandom) =>
44
35
  createSet(
@@ -51,21 +42,25 @@ const getRandomSetOfSubsets = (args: BuiltinArgument[], runtime: CheckerRuntime)
51
42
  const name = "RandomSetOfSubsets";
52
43
  const picks = getCount(getValue(args, 0, name), "first", name);
53
44
  const size = getCount(getValue(args, 1, name), "second", name);
54
- const elements = getFiniteElements(getValue(args, 2, name), "third", name, "finite set");
45
+ const set = getValue(args, 2, name);
46
+ if (!isSetValue(set) || !isEnumerable(set)) {
47
+ throw createModuleArgumentError("third", name, "finite set", set);
48
+ }
49
+ const elements = toFiniteSet(set).elements;
55
50
  if (elements.length < 31 && picks > 2 ** elements.length) {
56
- throw createArgumentError(
51
+ throw createModuleArgumentError(
57
52
  "first",
58
53
  name,
59
54
  `nonnegative integer that is smaller than the subset's size of 2^${elements.length}`,
60
- String(picks),
55
+ picks,
61
56
  );
62
57
  }
63
58
  if (elements.length < size) {
64
- throw createArgumentError(
59
+ throw createModuleArgumentError(
65
60
  "second",
66
61
  name,
67
62
  "nonnegative integer in range 0..Cardinality(S)",
68
- String(size),
63
+ size,
69
64
  );
70
65
  }
71
66
  return tossSubsets(
@@ -80,14 +75,14 @@ export const createRandomizationOperators = (runtime: CheckerRuntime): Builtin[]
80
75
  defineStateOperator("RandomSubset", 2, (args) => {
81
76
  const count = getValue(args, 0, "RandomSubset");
82
77
  if (typeof count !== "number") {
83
- throw createArgumentError("first", "RandomSubset", "nonnegative integer", formatValue(count));
78
+ throw createModuleArgumentError("first", "RandomSubset", "nonnegative integer", count);
84
79
  }
85
80
  if (count < 0) {
86
81
  throw createJavaOverrideError(RANDOM_SUBSET_SIGNATURE, String(count));
87
82
  }
88
83
  const set = getValue(args, 1, "RandomSubset");
89
84
  if (!isSetValue(set) || (!isEmptySet(set) && !isEnumerable(set))) {
90
- throw createArgumentError("second", "RandomSubset", "a finite set", formatValue(set));
85
+ throw createModuleArgumentError("second", "RandomSubset", "a finite set", set);
91
86
  }
92
87
  const factors = getEnumeratedFactors(set, runtime.version);
93
88
  if (factors) {
package/src/resolve.ts CHANGED
@@ -36,7 +36,7 @@ export type SymbolKind =
36
36
 
37
37
  // range is where SANY reports the symbol, and two symbols at one range are one node to SANY. params
38
38
  // holds the arity of each parameter of a defined operator.
39
- export interface Binding {
39
+ interface Binding {
40
40
  kind: SymbolKind;
41
41
  arity: number;
42
42
  params?: number[];
@@ -74,14 +74,7 @@ interface ModuleBinding extends Binding {
74
74
  isPending?: boolean;
75
75
  }
76
76
 
77
- // errors and warnings collect what SANY reports while it goes on analyzing the unit, and bindings
78
- // holds the names the module binds. A label may appear where hasLabelScope holds, in a definition, a
79
- // named theorem, or a numbered step, except where isLabelForbidden holds, in the scope of a NEW in a
80
- // nested ASSUME, or where isInExcept holds, in the new value of an EXCEPT. labelParams holds the
81
- // names bound since the innermost definition, label, or step, which a label must take as parameters,
82
- // and labels the labels there. isGenerated holds in a definition tla-ts generates for a LET INSTANCE,
83
- // and in a subexpression that a name selects, which SANY analyzes where it is defined.
84
- // getInstance gives what an INSTANCE in a LET or a proof instantiates.
77
+ // What SANY reports while it goes on analyzing a unit of a module, and the scope it analyzes it in.
85
78
  interface Unit {
86
79
  imported: Symbols;
87
80
  getInstance: (instance: Instance) => InstancedModule;
@@ -94,11 +87,15 @@ interface Unit {
94
87
  bindings: Map<string, ModuleBinding>;
95
88
  errors: SanyError[];
96
89
  warnings: SanyError[];
90
+ // In a definition, a named theorem, or a numbered step.
97
91
  hasLabelScope: boolean;
92
+ // In the scope of a NEW in a nested ASSUME.
98
93
  isLabelForbidden: boolean;
99
94
  isInExcept: boolean;
95
+ // The names bound since the innermost definition, label, or step, which a label must take.
100
96
  labelParams: string[];
101
97
  labels: Set<string>;
98
+ // In a definition generated for a LET INSTANCE, or a subexpression analyzed where it is defined.
102
99
  isGenerated: boolean;
103
100
  }
104
101
 
@@ -168,10 +165,8 @@ const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefin
168
165
  const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
169
166
  left !== undefined && right !== undefined && formatRange(left) === formatRange(right);
170
167
 
171
- // SymbolTable.addSymbol binds a name nothing binds yet. It rejects a built-in operator, a parameter
172
- // or bound name already in use, and a symbol of another kind or arity than the one bound, and warns
173
- // of any other duplicate. tla-ts binds a symbol SANY never sees, such as one it generates. Tells
174
- // whether to bind the added symbol.
168
+ // SymbolTable.addSymbol: rejects redefining a built-in, a bound name, or a symbol of another kind
169
+ // or arity, and warns of any other duplicate. A symbol SANY never sees, such as a generated one, binds.
175
170
  const isBindable = (name: string, current: Binding | undefined, added: Binding, unit: Unit) => {
176
171
  if (current === undefined) return true;
177
172
  if (isSameRange(current.range, added.range)) return false;
@@ -1447,7 +1442,7 @@ const resolveTheorem = (theorem: Theorem, unit: Unit) => {
1447
1442
 
1448
1443
  // The declarations of the module an INSTANCE instantiates, the names the INSTANCE adds, and the
1449
1444
  // module that instantiates it.
1450
- export interface InstancedModule {
1445
+ interface InstancedModule {
1451
1446
  declarations: Declaration[];
1452
1447
  symbols: Symbols;
1453
1448
  instancer: string;
@@ -1461,18 +1456,16 @@ export interface ModuleReport {
1461
1456
  warnings: SanyError[];
1462
1457
  }
1463
1458
 
1464
- // bound holds the names in scope where the module starts: the built-in operators, the names of the
1465
- // module that encloses it, and those of the modules it extends. instanced holds the names each
1466
- // INSTANCE adds, getInstance what an INSTANCE in a LET or a proof instantiates, facts the theorems
1467
- // and assumptions the modules it extends or instantiates export, and warnings the warnings SANY
1468
- // reports for the spec.
1469
1459
  export interface ModuleUnit {
1470
1460
  imported: Symbols;
1471
1461
  getInstance: (instance: Instance) => InstancedModule;
1462
+ // The built-in operators and the names of the enclosing and extended modules.
1472
1463
  bound: Symbols;
1473
1464
  definitions: Map<string, Definition>;
1474
1465
  getKey: (definition: Definition) => string;
1466
+ // The names each INSTANCE adds.
1475
1467
  instanced: Map<Instance, Symbols>;
1468
+ // The theorems and assumptions the extended and instantiated modules export.
1476
1469
  facts: Set<string>;
1477
1470
  reports: ModuleReport[];
1478
1471
  warnings: SanyError[];
@@ -1631,7 +1624,7 @@ export const collectSemanticErrors = (
1631
1624
  warnings: SanyError[],
1632
1625
  ) => {
1633
1626
  const errors: SanyError[] = [];
1634
- const add = (list: SanyError[], item: SanyError) => {
1627
+ const addUnique = (list: SanyError[], item: SanyError) => {
1635
1628
  if (
1636
1629
  list.every(
1637
1630
  (existing) => existing.location !== item.location || existing.message !== item.message,
@@ -1659,8 +1652,8 @@ export const collectSemanticErrors = (
1659
1652
  }
1660
1653
  reported.push(...(error.sany?.errors ?? []));
1661
1654
  }
1662
- reported.forEach((item) => add(errors, item));
1663
- warned.forEach((item) => add(warnings, item));
1655
+ reported.forEach((item) => addUnique(errors, item));
1656
+ warned.forEach((item) => addUnique(warnings, item));
1664
1657
  });
1665
1658
  return errors;
1666
1659
  };