tla-ts 0.0.10-dev.b4a856304 → 0.0.10-dev.f16792fed

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 (72) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +34 -0
  3. package/dist/index.d.ts +34 -0
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +48 -48
  8. package/dist/prove.js +29 -29
  9. package/dist/run.cjs +40 -40
  10. package/dist/run.js +31 -31
  11. package/dist/transfer.cjs +36 -36
  12. package/dist/transfer.js +40 -40
  13. package/package.json +1 -1
  14. package/src/ast.ts +0 -5
  15. package/src/bags.ts +2 -5
  16. package/src/builtins.ts +1 -8
  17. package/src/check.ts +128 -193
  18. package/src/cli/options.ts +1 -1
  19. package/src/cli/run.ts +0 -2
  20. package/src/cli/runtime-invariants.ts +8 -7
  21. package/src/cli/shared.ts +0 -4
  22. package/src/cli/tlc-arguments.ts +24 -30
  23. package/src/cli/tlc-usage.ts +2 -3
  24. package/src/coverage.ts +3 -8
  25. package/src/evaluate.ts +26 -102
  26. package/src/java-hash.ts +7 -0
  27. package/src/java-overrides.ts +4 -9
  28. package/src/json-reader.ts +1 -2
  29. package/src/json.ts +4 -11
  30. package/src/levels.ts +7 -15
  31. package/src/lexer.ts +72 -83
  32. package/src/liveness-checker.ts +77 -106
  33. package/src/liveness.ts +10 -34
  34. package/src/parallel.ts +26 -33
  35. package/src/parser.ts +163 -226
  36. package/src/pcal/ast.ts +53 -65
  37. package/src/pcal/parse.ts +144 -191
  38. package/src/pcal/tokenize.ts +91 -115
  39. package/src/pcal/translate.ts +83 -98
  40. package/src/pcal/validate.ts +48 -50
  41. package/src/pretty-print.ts +6 -6
  42. package/src/randomization.ts +1 -4
  43. package/src/resolve.ts +12 -70
  44. package/src/runtime.ts +11 -18
  45. package/src/sany/builtins.ts +1 -3
  46. package/src/sany/generator.ts +74 -127
  47. package/src/sany/levels.ts +5 -9
  48. package/src/sany/lexer.ts +40 -44
  49. package/src/sany/linter.ts +0 -1
  50. package/src/sany/nodes.ts +4 -10
  51. package/src/search.ts +1 -1
  52. package/src/simulate.ts +50 -59
  53. package/src/spec-description.ts +23 -34
  54. package/src/standard-definitions.ts +0 -3
  55. package/src/standard-modules.ts +1 -12
  56. package/src/state-graph.ts +3 -15
  57. package/src/subexpressions.ts +38 -47
  58. package/src/successors.ts +14 -35
  59. package/src/symmetry.ts +4 -19
  60. package/src/tlc/fingerprint.ts +15 -21
  61. package/src/tlc/jar-data.ts +0 -1
  62. package/src/tlc/messages.ts +2 -21
  63. package/src/tlc/order.ts +22 -47
  64. package/src/tlc/random.ts +18 -30
  65. package/src/tlc/replay.ts +13 -33
  66. package/src/tlc/summary.ts +36 -45
  67. package/src/tlc/tlc-operators.ts +4 -14
  68. package/src/tlc/tlcext-operators.ts +11 -15
  69. package/src/tlc/trace-spec.ts +4 -9
  70. package/src/tlc/value-stream.ts +9 -13
  71. package/src/tlc/version.ts +0 -1
  72. package/src/values.ts +74 -186
@@ -46,12 +46,9 @@ const formatCalendarVersion = (date: Date) =>
46
46
  const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
47
47
  createFunction([...registers].map(([key, value]): [Value, Value] => [key, createTuple([value])]));
48
48
 
49
- // TLC doesn't normalize the records it builds, so they keep their written order.
50
49
  const createTlcRecord = (fields: [string, Value][]) =>
51
50
  setFunctionClass(createWrittenFunction(fields), "record");
52
51
 
53
- // TLC creates its model checker after it processes the spec, so these are undefined before then.
54
-
55
52
  const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
56
53
  const TLC_174_KEYS = new Set(["diameter", "distinct", "queue", "duration", "level"]);
57
54
  const CHECKER_KEYS_174 = new Set(["diameter", "distinct", "queue", "level"]);
@@ -153,7 +150,6 @@ const getPermutations = (elements: Value[]): Value[][] =>
153
150
  ]),
154
151
  );
155
152
 
156
- // TLC sorts the value of a TLCEval argument that it caches, with every value inside it.
157
153
  const normalizeDeeply = (value: Value): void => {
158
154
  if (typeof value !== "object") return;
159
155
  if (value.kind === "Set" || value.kind === "MixedSet") {
@@ -207,7 +203,6 @@ export const createTlcOperators = (
207
203
  defineOperator(":>", 2, (args) =>
208
204
  createFunction([[getValue(args, 0, ":>"), getValue(args, 1, ":>")]]),
209
205
  ),
210
- // CombineFcn converts both arguments before it checks either.
211
206
  defineOperator("@@", 2, (args) => {
212
207
  const leftValue = getValue(args, 0, "@@");
213
208
  const rightValue = getValue(args, 1, "@@");
@@ -224,7 +219,6 @@ export const createTlcOperators = (
224
219
  defineOperator("ToString", 1, (args) =>
225
220
  rankString(formatValue(getValue(args, 0, "ToString"), true)),
226
221
  ),
227
- // The checker passes no context where TLC caches the value, which it then sorts.
228
222
  {
229
223
  ...defineLazyOperator("TLCEval", 1, (args, context) => {
230
224
  const value = convertLikeTlcEval(getThunk(args, 0, "TLCEval")());
@@ -253,23 +247,20 @@ export const createTlcOperators = (
253
247
  if (typeof result === "boolean") return result;
254
248
  throw createModuleArgumentError("second", "SortSeq", "boolean function", result);
255
249
  };
256
- // TLC's insertion sort moves each element back past the ones it precedes.
257
250
  const sorted = elements.slice(0, 1);
258
251
  elements.slice(1).forEach((element) => {
259
- const position = { index: sorted.length };
260
- while (position.index > 0 && precedes(element, sorted[position.index - 1])) position.index--;
261
- sorted.splice(position.index, 0, element);
252
+ let position = sorted.length;
253
+ while (position > 0 && precedes(element, sorted[position - 1])) position--;
254
+ sorted.splice(position, 0, element);
262
255
  });
263
256
  return createTuple(sorted);
264
257
  }),
265
- // TLC reports a failure of TLCGet, its argument's too, as one of TLCGetSet.TLCGetEval.
266
258
  runtime.version === "1.7.4"
267
259
  ? overrideWithJava(
268
260
  {
269
261
  ...defineStateOperator("TLCGet", 1, (args, context) =>
270
262
  getTlcValue(runtime, getValue(args, 0, "TLCGet"), context),
271
263
  ),
272
- // Unlike TLCGetSet.TLCGetEval, TLC.TLCGet declares no minimum level.
273
264
  isLevelConstant: true,
274
265
  },
275
266
  "public static tlc2.value.impl.Value tlc2.module.TLC.TLCGet(tlc2.value.impl.Value)",
@@ -281,8 +272,7 @@ export const createTlcOperators = (
281
272
  "public static tlc2.value.impl.Value tlc2.module.TLCGetSet.TLCGetEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
282
273
  ),
283
274
  defineStateOperator("TLCSet", 2, (args) => {
284
- const key = getValue(args, 0, "TLCSet");
285
- setTlcValue(runtime, print, key, getValue(args, 1, "TLCSet"));
275
+ setTlcValue(runtime, print, getValue(args, 0, "TLCSet"), getValue(args, 1, "TLCSet"));
286
276
  return true;
287
277
  }),
288
278
  defineConstant("Any", ANY_SET),
@@ -54,7 +54,6 @@ export const createActionRecord = (action: string | undefined) =>
54
54
  ],
55
55
  ]);
56
56
 
57
- // TLC's CounterExample is its own class, so ToTrace refuses a record that only looks like one.
58
57
  const counterExamples = new WeakSet<Value & object>();
59
58
 
60
59
  const createCounterExampleRecord = (action: Value[], states: Value[]) => {
@@ -189,16 +188,18 @@ export const createTlcExtOperators = (
189
188
  }
190
189
  return false;
191
190
  }),
192
- defineStateOperator("TLCGetOrDefault", 2, (args) => {
193
- const key = getValue(args, 0, "TLCGetOrDefault");
194
- return getRegister(runtime, key) ?? getValue(args, 1, "TLCGetOrDefault");
195
- }),
196
- defineStateOperator("Trace", 0, (_, context) => {
191
+ defineStateOperator(
192
+ "TLCGetOrDefault",
193
+ 2,
194
+ (args) =>
195
+ getRegister(runtime, getValue(args, 0, "TLCGetOrDefault")) ??
196
+ getValue(args, 1, "TLCGetOrDefault"),
197
+ ),
198
+ defineStateOperator("Trace", 0, (_args, context) => {
197
199
  const variables = context?.environment.variables ?? [];
198
- // TLC evaluates an assumption in an empty state, which assigns none of the variables.
199
200
  const state = context?.state ?? variables.map(() => undefined);
200
201
  if (!context || !isCompleteState(state)) {
201
- const unassigned = variables.filter((_, index) => state[index] === undefined);
202
+ const unassigned = variables.filter((_name, index) => state[index] === undefined);
202
203
  throw new TlaEvaluationError(
203
204
  `In evaluating TLCExt!Trace, the state is not completely specified yet (variable${unassigned.length > 1 ? "s" : ""} ${unassigned.join(", ")} undefined).`,
204
205
  );
@@ -210,7 +211,6 @@ export const createTlcExtOperators = (
210
211
  name,
211
212
  step[index],
212
213
  ]);
213
- // A simulated behavior's states also record their actions.
214
214
  const action = runtime.getSimulationValue?.("action", step);
215
215
  return createRecord(action === undefined ? fields : [["_action", action], ...fields]);
216
216
  }),
@@ -244,7 +244,6 @@ export const createTlcExtOperators = (
244
244
  }
245
245
  return createModelValue(name);
246
246
  }),
247
- // Only simulation runs the deferred expression, for the successor it chooses.
248
247
  defineLazyOperator("TLCDefer", 1, (args, context) => {
249
248
  const expression = getOperator(args, 0, "TLCDefer").expression;
250
249
  if (context && expression) {
@@ -257,9 +256,7 @@ export const createTlcExtOperators = (
257
256
  defineOperator("TLCNoOp", 1, (args) => getValue(args, 0, "TLCNoOp")),
258
257
  {
259
258
  ...defineLazyOperator("PickSuccessor", 1, (args, context) => {
260
- // Outside an action, TLC's successor is an empty state, which assigns none of the variables.
261
- // TLCExt.tla promises TRUE for a successor not fully determined, where TLCExt#pickSuccessor
262
- // fingerprints it first and fails.
259
+ // Deviation from TLC: TLCExt#pickSuccessor fingerprints a partial successor and fails, where TLCExt.tla promises TRUE.
263
260
  const next = context?.next ?? (context?.environment.variables ?? []).map(() => undefined);
264
261
  if (!isCompleteState(next)) return true;
265
262
  if (context?.next && runtime.isSeen(next)) return true;
@@ -282,7 +279,7 @@ export const createTlcExtOperators = (
282
279
  `Extend behavior of length ${runtime.getLevel(context.state)} with a "${action?.name}" step [<Action ${action?.location.range && formatRange(action.location.range)}>]? (Yes/no/explored/states/diff):`,
283
280
  ),
284
281
  );
285
- // tla-ts gives PickSuccessor no standard input to read an answer from.
282
+ // Deviation from TLC: PickSuccessor has no standard input to read an answer from.
286
283
  throw new TlaEndOfInputError();
287
284
  }),
288
285
  isActionLevel: true,
@@ -323,7 +320,6 @@ export const createTlcExtOperators = (
323
320
  );
324
321
  }
325
322
  const definition = context ? getDefinition(name, context.environment) : undefined;
326
- // A definition that TLC overrides with Java has the Java method's value, not its body's.
327
323
  const builtin = definition ? undefined : context?.environment.builtins.get(name);
328
324
  if (builtin?.arity === 0) return builtin.evaluate([], context);
329
325
  if (!definition || !context) {
@@ -1,3 +1,4 @@
1
+ import { getJavaHashCode } from "../java-hash";
1
2
  import { formatValue, type Value } from "../values";
2
3
 
3
4
  interface TraceSpecInput {
@@ -23,19 +24,16 @@ const CLOSING_TAG = `${"=".repeat(77)}\n`;
23
24
  const LASSO_START = "_TTraceLassoStart";
24
25
  const LASSO_END = "_TTraceLassoEnd";
25
26
 
26
- const getJavaHash = (text: string) => {
27
- const hash = text
28
- .split("")
29
- .reduce((current, unit) => (Math.imul(current, 31) + unit.charCodeAt(0)) | 0, 0);
27
+ const getHashMapHash = (text: string) => {
28
+ const hash = getJavaHashCode(text);
30
29
  return hash ^ (hash >>> 16);
31
30
  };
32
31
 
33
- // TLC lists the modules and the variables in the order a Java HashSet or HashMap iterates them.
34
32
  const getHashOrder = (names: string[]) => {
35
33
  const unique = [...new Set(names)];
36
34
  const capacity = 2 ** Math.max(4, Math.ceil(Math.log2(unique.length / 0.75)));
37
35
  return unique
38
- .map((name, index) => ({ name, index, bucket: getJavaHash(name) & (capacity - 1) }))
36
+ .map((name, index) => ({ name, index, bucket: getHashMapHash(name) & (capacity - 1) }))
39
37
  .sort((left, right) => left.bucket - right.bucket || left.index - right.index)
40
38
  .map((entry) => entry.name);
41
39
  };
@@ -48,7 +46,6 @@ const formatJavaDate = (date: Date) => {
48
46
  return `${getPart({ weekday: "short" }, "weekday")} ${getPart({ month: "short" }, "month")} ${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())} ${getPart({ timeZoneName: "short" }, "timeZoneName")} ${date.getFullYear()}`;
49
47
  };
50
48
 
51
- // Java's String#split drops the empty strings at the end.
52
49
  const splitLines = (text: string) => {
53
50
  const lines = text.split("\n");
54
51
  while (lines.length > 1 && lines[lines.length - 1] === "") lines.pop();
@@ -199,8 +196,6 @@ ${lassoStep}${variables
199
196
 
200
197
  `;
201
198
  const statesWithoutLasso = trace.filter((state) => !state.isBackToState && !state.isStuttering);
202
- // TLC adds a view when a state appears twice before the lasso closes, so that TLCGet("level")
203
- // tells the copies apart.
204
199
  const isViewed =
205
200
  isLasso &&
206
201
  new Set(statesWithoutLasso.map((state) => JSON.stringify(state.values))).size <
@@ -24,14 +24,11 @@ const TAGS = {
24
24
  handle: 26,
25
25
  };
26
26
 
27
- // TLC fingerprints the value before it writes it, which normalizes every set and function.
28
- export const serializeValue = (value: Value) => {
27
+ const serializeValue = (value: Value) => {
29
28
  const bytes: number[] = [];
30
- const handles = {
31
- objects: new Map<object, number>(),
32
- names: new Map<string, number>(),
33
- count: 0,
34
- };
29
+ const objectHandles = new Map<object, number>();
30
+ const nameHandles = new Map<string, number>();
31
+ let handleCount = 0;
35
32
  const modelValues = getModelValues();
36
33
  const writeByte = (byte: number) => bytes.push(byte & 0xff);
37
34
  const writeShort = (short: number) => {
@@ -43,7 +40,6 @@ export const serializeValue = (value: Value) => {
43
40
  writeShort(int);
44
41
  };
45
42
  const writeNat = (nat: number) => (nat > 0x7fff ? writeInt(-nat) : writeShort(nat));
46
- // A reader skips the token and the variable location that TLC writes before the characters.
47
43
  const writeString = (text: string) => {
48
44
  writeByte(TAGS.string);
49
45
  writeInt(0);
@@ -67,7 +63,7 @@ export const serializeValue = (value: Value) => {
67
63
  return;
68
64
  }
69
65
  if (typeof inner === "string") {
70
- handles.count++;
66
+ handleCount++;
71
67
  writeString(inner);
72
68
  return;
73
69
  }
@@ -82,12 +78,12 @@ export const serializeValue = (value: Value) => {
82
78
  writeInt(inner.high);
83
79
  return;
84
80
  }
85
- const handle = handles.objects.get(inner);
81
+ const handle = objectHandles.get(inner);
86
82
  if (handle !== undefined) {
87
83
  writeHandle(handle);
88
84
  return;
89
85
  }
90
- handles.objects.set(inner, handles.count++);
86
+ objectHandles.set(inner, handleCount++);
91
87
  if (isSetValue(inner)) {
92
88
  const elements = toFiniteSet(inner).elements;
93
89
  writeByte(TAGS.set);
@@ -108,9 +104,9 @@ export const serializeValue = (value: Value) => {
108
104
  writeInt(functionValue.domain.length);
109
105
  functionValue.domain.forEach((name, index) => {
110
106
  const fieldName = String(name);
111
- const nameHandle = handles.names.get(fieldName);
107
+ const nameHandle = nameHandles.get(fieldName);
112
108
  if (nameHandle === undefined) {
113
- handles.names.set(fieldName, handles.count++);
109
+ nameHandles.set(fieldName, handleCount++);
114
110
  writeString(fieldName);
115
111
  } else {
116
112
  writeHandle(nameHandle);
@@ -1,6 +1,5 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
 
3
- // "master" stands for the master builds that tla-ts follows for any other tla2tools.jar.
4
3
  export type TlcVersion = "1.7.4" | "master";
5
4
 
6
5
  const MANIFEST = "META-INF/MANIFEST.MF";