tla-ts 0.0.1 → 0.0.3-dev.19c763f

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 (74) hide show
  1. package/README.md +98 -39
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +225 -30
  4. package/dist/index.d.ts +225 -30
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +21 -1
  11. package/dist/run-cli.js +21 -1
  12. package/dist/transfer.cjs +400 -39
  13. package/dist/transfer.js +400 -39
  14. package/package.json +6 -2
  15. package/src/ast.ts +213 -6
  16. package/src/bags.ts +161 -71
  17. package/src/builtins.ts +203 -23
  18. package/src/bundled-modules.ts +63 -32
  19. package/src/check.ts +1086 -244
  20. package/src/cli-options.ts +64 -0
  21. package/src/codegen.ts +58 -34
  22. package/src/config.ts +39 -11
  23. package/src/dependencies.ts +2 -2
  24. package/src/environment.ts +557 -153
  25. package/src/errors.ts +66 -1
  26. package/src/evaluate.ts +547 -255
  27. package/src/exit-codes.ts +53 -0
  28. package/src/fingerprints.ts +48 -13
  29. package/src/index.ts +9 -4
  30. package/src/instances.ts +235 -79
  31. package/src/java-overrides.ts +241 -0
  32. package/src/json.ts +153 -72
  33. package/src/levels.ts +850 -0
  34. package/src/lexer.ts +108 -29
  35. package/src/live-check.ts +710 -0
  36. package/src/liveness.ts +37 -599
  37. package/src/obligation-action.ts +384 -0
  38. package/src/obligation-anon.ts +575 -0
  39. package/src/obligation-deref.ts +637 -0
  40. package/src/obligation-expression.ts +1060 -0
  41. package/src/obligation-format.ts +406 -0
  42. package/src/obligation-generate.ts +1074 -0
  43. package/src/obligation-levels.ts +329 -0
  44. package/src/obligation-module.ts +1042 -0
  45. package/src/obligation-printer.ts +889 -0
  46. package/src/obligation-proof.ts +76 -0
  47. package/src/obligation-source.ts +1084 -0
  48. package/src/obligation-tuples.ts +184 -0
  49. package/src/obligations.ts +316 -0
  50. package/src/parallel-worker.ts +186 -201
  51. package/src/parallel.ts +179 -84
  52. package/src/parser.ts +970 -206
  53. package/src/pretty-print.ts +259 -0
  54. package/src/randomization.ts +24 -113
  55. package/src/resolve.ts +483 -51
  56. package/src/run-cli.ts +473 -62
  57. package/src/runtime.ts +65 -28
  58. package/src/spec-description.ts +521 -0
  59. package/src/standard-levels.ts +105 -0
  60. package/src/standard-modules.ts +223 -92
  61. package/src/state-graph.ts +157 -0
  62. package/src/subexpressions.ts +31 -45
  63. package/src/substitution-levels.ts +184 -0
  64. package/src/successors.ts +276 -53
  65. package/src/symmetry.ts +79 -23
  66. package/src/tableau.ts +43 -37
  67. package/src/tlc-ext.ts +148 -44
  68. package/src/tlc-module.ts +71 -66
  69. package/src/tlc-modules.ts +1022 -0
  70. package/src/tlc-order.ts +511 -0
  71. package/src/tlc-random.ts +411 -0
  72. package/src/tlc-summary.ts +109 -0
  73. package/src/transfer.ts +26 -5
  74. package/src/values.ts +1245 -176
package/src/tlc-module.ts CHANGED
@@ -1,43 +1,55 @@
1
- import { TlaEvaluationError } from "./errors";
1
+ import { TlaModuleError } from "./errors";
2
+ import { EXIT_STATUS } from "./exit-codes";
2
3
  import type { Builtin } from "./evaluate";
4
+ import { prettyPrint } from "./pretty-print";
3
5
  import {
4
6
  defineConstant,
5
7
  defineHigherOrderOperator,
6
8
  defineOperator,
7
9
  defineStateOperator,
8
10
  getElements,
11
+ getEnumerableSet,
12
+ getFunction,
9
13
  getOperator,
10
14
  getSequence,
11
15
  getValue,
12
16
  } from "./builtins";
13
17
  import { getNamedRegisters, NAMED_REGISTER_PREFIX, type CheckerRuntime } from "./runtime";
18
+ import { createActionRecord, setTlcValue } from "./tlc-ext";
19
+ import { getRandomElement } from "./tlc-random";
14
20
  import {
21
+ ANY_SET,
15
22
  createFunction,
16
23
  createRecord,
17
24
  createSet,
18
25
  createTuple,
26
+ createWrittenFunction,
19
27
  formatValue,
20
- toFiniteFunction,
28
+ getWrittenPairs,
29
+ toFiniteSet,
21
30
  type Value,
31
+ rankString,
22
32
  } from "./values";
23
33
 
24
- const createLocationRecord = () =>
25
- createRecord([
26
- ["beginLine", 0],
27
- ["beginColumn", 0],
28
- ["endLine", 0],
29
- ["endColumn", 0],
30
- ["module", "--unknown--"],
31
- ]);
34
+ const padTime = (value: number) => String(value).padStart(2, "0");
35
+
36
+ const formatCalendarVersion = (date: Date) =>
37
+ `${date.getUTCFullYear()}.${padTime(date.getUTCMonth() + 1)}.${padTime(date.getUTCDate())}.${padTime(date.getUTCHours())}${padTime(date.getUTCMinutes())}${padTime(date.getUTCSeconds())}`;
32
38
 
33
39
  const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
34
40
  createFunction([...registers].map(([key, value]): [Value, Value] => [key, createTuple([value])]));
35
41
 
42
+ // TLC creates its model checker after it processes the spec, so these are undefined before then.
43
+ const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
44
+
36
45
  const getRuntimeValue = (
37
46
  runtime: CheckerRuntime,
38
47
  key: string,
39
48
  state: (Value | undefined)[] | undefined,
40
- ): Value => {
49
+ ) => {
50
+ if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
51
+ throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
52
+ }
41
53
  const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
42
54
  switch (key) {
43
55
  case "level":
@@ -67,23 +79,24 @@ const getRuntimeValue = (
67
79
  ["mode", "bfs"],
68
80
  ["deadlock", runtime.checkDeadlock],
69
81
  ["worker", 1],
70
- ["seed", "0"],
82
+ ["seed", String(runtime.seed)],
71
83
  ["fingerprint", "0"],
72
- ["install", ""],
84
+ ["install", "unknown"],
73
85
  ]);
74
- case "revision":
86
+ case "revision": {
87
+ const now = new Date();
75
88
  return createRecord([
76
89
  ["count", 0],
77
- ["timestamp", 0],
78
- ["date", "1970-01-01T00:00:00.0Z"],
79
- ["tag", "tla-ts"],
80
- ["calver", "0"],
90
+ ["timestamp", Math.floor(now.getTime() / 1000)],
91
+ ["date", rankString(`${now.toISOString().slice(0, 19)}.${now.getUTCMilliseconds()}Z`)],
92
+ ["tag", "development"],
93
+ ["calver", rankString(formatCalendarVersion(new Date(runtime.startedAt)))],
81
94
  ]);
95
+ }
96
+ case "spec":
97
+ return runtime.getSpecification();
82
98
  case "action":
83
- return createRecord([
84
- ["name", "UnnamedAction"],
85
- ["location", createLocationRecord()],
86
- ]);
99
+ return createActionRecord(undefined);
87
100
  case "all":
88
101
  return getRegisterFunction(runtime.registers);
89
102
  case "all:named":
@@ -91,11 +104,11 @@ const getRuntimeValue = (
91
104
  }
92
105
  if (key.startsWith(NAMED_REGISTER_PREFIX)) {
93
106
  const value = getNamedRegisters(runtime).get(key);
94
- if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
107
+ if (value === undefined) throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
95
108
  return value;
96
109
  }
97
- if (key.startsWith("-D")) return runtime.properties.get(key.slice(2)) ?? key;
98
- throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
110
+ if (key.startsWith("-D")) return rankString(runtime.properties.get(key.slice(2)) ?? key);
111
+ throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
99
112
  };
100
113
 
101
114
  const getPermutations = (elements: Value[]): Value[][] =>
@@ -108,24 +121,34 @@ const getPermutations = (elements: Value[]): Value[][] =>
108
121
  ]),
109
122
  );
110
123
 
124
+ // TLC sorts the value of a TLCEval argument that it caches, with every value inside it.
125
+ const normalizeDeeply = (value: Value): void => {
126
+ if (typeof value !== "object") return;
127
+ if (value.kind === "Set" || value.kind === "MixedSet") {
128
+ toFiniteSet(value).elements.forEach(normalizeDeeply);
129
+ }
130
+ if (value.kind === "Function") value.range.forEach(normalizeDeeply);
131
+ };
132
+
111
133
  export const createTlcOperators = (
112
134
  print: (text: string) => void,
113
135
  runtime: CheckerRuntime,
114
136
  ): Builtin[] => [
115
137
  defineOperator("Print", 2, (args) => {
116
138
  print(
117
- `${formatValue(getValue(args, 0, "Print"), true)} ${formatValue(getValue(args, 1, "Print"), true)}`,
139
+ `${prettyPrint(formatValue(getValue(args, 0, "Print"), true, true))} ${prettyPrint(formatValue(getValue(args, 1, "Print"), true, true))}`,
118
140
  );
119
141
  return getValue(args, 1, "Print");
120
142
  }),
121
143
  defineOperator("PrintT", 1, (args) => {
122
- print(formatValue(getValue(args, 0, "PrintT"), true));
144
+ print(prettyPrint(formatValue(getValue(args, 0, "PrintT"), true, true)));
123
145
  return true;
124
146
  }),
125
147
  defineOperator("Assert", 2, (args) => {
126
148
  if (getValue(args, 0, "Assert") !== true) {
127
- throw new TlaEvaluationError(
128
- `The first argument of Assert evaluated to FALSE; the second argument was:\n${formatValue(getValue(args, 1, "Assert"))}`,
149
+ throw new TlaModuleError(
150
+ `The first argument of Assert evaluated to FALSE; the second argument was:\n${prettyPrint(formatValue(getValue(args, 1, "Assert")))}`,
151
+ EXIT_STATUS.assertViolation,
129
152
  );
130
153
  }
131
154
  return true;
@@ -135,15 +158,18 @@ export const createTlcOperators = (
135
158
  createFunction([[getValue(args, 0, ":>"), getValue(args, 1, ":>")]]),
136
159
  ),
137
160
  defineOperator("@@", 2, (args) => {
138
- const left = toFiniteFunction(getValue(args, 0, "@@"));
139
- const right = toFiniteFunction(getValue(args, 1, "@@"));
140
- return createFunction([
141
- ...left.domain.map((key, index): [Value, Value] => [key, left.range[index]]),
142
- ...right.domain.map((key, index): [Value, Value] => [key, right.range[index]]),
143
- ]);
161
+ const left = getFunction(args, 0, "@@");
162
+ const right = getFunction(args, 1, "@@");
163
+ return createWrittenFunction([...getWrittenPairs(left), ...getWrittenPairs(right)]);
164
+ }),
165
+ defineOperator("ToString", 1, (args) =>
166
+ rankString(formatValue(getValue(args, 0, "ToString"), true)),
167
+ ),
168
+ defineOperator("TLCEval", 1, (args) => {
169
+ const value = getValue(args, 0, "TLCEval");
170
+ normalizeDeeply(value);
171
+ return value;
144
172
  }),
145
- defineOperator("ToString", 1, (args) => formatValue(getValue(args, 0, "ToString"))),
146
- defineOperator("TLCEval", 1, (args) => getValue(args, 0, "TLCEval")),
147
173
  defineOperator("Permutations", 1, (args) => {
148
174
  const elements = getElements(args, 0, "Permutations");
149
175
  return createSet(
@@ -171,11 +197,11 @@ export const createTlcOperators = (
171
197
  const key = getValue(args, 0, "TLCGet");
172
198
  if (typeof key === "number" && key >= 0) {
173
199
  const value = runtime.registers.get(key);
174
- if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
200
+ if (value === undefined) throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
175
201
  return value;
176
202
  }
177
203
  if (typeof key !== "string") {
178
- throw new TlaEvaluationError(
204
+ throw new TlaModuleError(
179
205
  `The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
180
206
  );
181
207
  }
@@ -183,32 +209,11 @@ export const createTlcOperators = (
183
209
  }),
184
210
  defineStateOperator("TLCSet", 2, (args) => {
185
211
  const key = getValue(args, 0, "TLCSet");
186
- const value = getValue(args, 1, "TLCSet");
187
- if (typeof key === "number" && key >= 0) {
188
- runtime.registers.set(key, value);
189
- return true;
190
- }
191
- if (key === "exit") {
192
- if (value === true) runtime.isStopped = true;
193
- return true;
194
- }
195
- if (key === "pause") return true;
196
- if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
197
- runtime.namedRegisters.set(key, value);
198
- return true;
199
- }
200
- if (typeof key === "string" && key.startsWith("-D")) {
201
- runtime.properties.set(key.slice(2), formatValue(value));
202
- return true;
203
- }
204
- throw new TlaEvaluationError(
205
- `The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
206
- );
207
- }),
208
- defineConstant("Any", { kind: "InfiniteSet", name: "Any" }),
209
- defineOperator("RandomElement", 1, (args) => {
210
- const elements = getElements(args, 0, "RandomElement");
211
- if (elements.length === 0) throw new TlaEvaluationError("RandomElement of the empty set");
212
- return elements[Math.floor(runtime.random() * elements.length)];
212
+ setTlcValue(runtime, print, key, getValue(args, 1, "TLCSet"));
213
+ return true;
213
214
  }),
215
+ defineConstant("Any", ANY_SET),
216
+ defineOperator("RandomElement", 1, (args) =>
217
+ getRandomElement(getEnumerableSet(args, 0, "RandomElement"), runtime.getRandom()),
218
+ ),
214
219
  ];