tla-ts 0.0.3-dev.a85c0bd → 0.0.3-dev.a91da46

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 (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +167 -27
  5. package/dist/index.d.ts +167 -27
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -20
  12. package/dist/run-cli.js +1351 -20
  13. package/dist/transfer.cjs +83 -28
  14. package/dist/transfer.js +83 -28
  15. package/package.json +5 -1
  16. package/src/ast.ts +52 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1332 -262
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +431 -120
  26. package/src/errors.ts +195 -7
  27. package/src/evaluate.ts +718 -146
  28. package/src/exit-codes.ts +6 -24
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +32 -4
  34. package/src/live-check.ts +204 -148
  35. package/src/liveness.ts +190 -117
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +6 -1
  39. package/src/parallel.ts +57 -6
  40. package/src/parser.ts +436 -128
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1364 -405
  48. package/src/run-cli.ts +1167 -155
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +26 -7
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +158 -120
  69. package/src/symmetry.ts +168 -50
  70. package/src/tableau.ts +3 -1
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +49 -21
  74. package/src/tlc-modules.ts +0 -4
  75. package/src/tlc-order.ts +55 -9
  76. package/src/tlc-random.ts +28 -3
  77. package/src/tlc-replay.ts +655 -0
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +683 -256
@@ -7,13 +7,12 @@ import {
7
7
  } from "./ast";
8
8
  import type { ModelConfig } from "./config";
9
9
  import type { InstanceAction } from "./environment";
10
- import { TlaError } from "./errors";
10
+ import { formatRange, TlaError } from "./errors";
11
11
  import {
12
12
  bindDefinitions,
13
13
  bindOperatorArguments,
14
14
  evaluate,
15
15
  forEachBinding,
16
- formatRange,
17
16
  getDefinition,
18
17
  isConstantLevel,
19
18
  lookupScope,
@@ -25,6 +24,7 @@ import {
25
24
  import { TEMPORAL_LEVEL } from "./levels";
26
25
  import { getLevel } from "./liveness";
27
26
  import { createRecord, createSet, createTuple, formatValue, type Value } from "./values";
27
+ import type { TlcVersion } from "./tlc-version";
28
28
 
29
29
  export interface SpecAction {
30
30
  name: string;
@@ -63,7 +63,8 @@ const toLocationValue = (location: SourceLocation) => {
63
63
  ]);
64
64
  };
65
65
 
66
- const toActionValue = (action: SpecAction) => {
66
+ // TLC's RecordValue(Action): the action's name and location, with the values of its parameters.
67
+ export const toActionValue = (action: SpecAction) => {
67
68
  const fields: [string, Value][] = [
68
69
  ["name", action.name],
69
70
  ["location", toLocationValue(action.location)],
@@ -250,8 +251,12 @@ const getActions = (
250
251
  return getInstanceActions(instanceAction, expression, scope, called, environment);
251
252
  if (!called) return [createSplitAction()];
252
253
  const args = getArguments(expression);
254
+ // TLC names an action after the operator it applies, as SANY names it in the spec.
255
+ const calledName = isLocallyResolved(expression)
256
+ ? called.name
257
+ : (environment.instanceNames.get(getOperatorName(expression) ?? "") ?? called.name);
253
258
  const callAction = (): SpecAction[] => [
254
- { name: called.name, location: expression.location, parameters: [], expression, scope },
259
+ { name: calledName, location: expression.location, parameters: [], expression, scope },
255
260
  ];
256
261
  if (isConstantDefinition(called, environment)) return callAction();
257
262
  if (args.some((arg) => getLevel(arg, scope, environment) !== 0)) return callAction();
@@ -267,7 +272,13 @@ const getActions = (
267
272
  }),
268
273
  called.scope,
269
274
  );
270
- return getActions(called.body, boundScope, called, environment, instanceActions);
275
+ return getActions(
276
+ called.body,
277
+ boundScope,
278
+ { ...called, name: calledName },
279
+ environment,
280
+ instanceActions,
281
+ );
271
282
  }, callAction);
272
283
  };
273
284
 
@@ -462,6 +473,13 @@ const describe = (
462
473
  return description;
463
474
  };
464
475
 
476
+ // TLC's initial predicates, which it conjoins to compute the initial states.
477
+ export const getInitActions = (
478
+ config: ModelConfig,
479
+ environment: Environment,
480
+ instanceActions: Map<string, InstanceAction>,
481
+ ) => describe(config, environment, instanceActions).inits;
482
+
465
483
  // TLC's split of the next-state relation into the actions it names in a behavior.
466
484
  export const getNextActions = (
467
485
  config: ModelConfig,
@@ -474,8 +492,9 @@ const formatParameters = (action: SpecAction) =>
474
492
  ? ""
475
493
  : `(${action.parameters.map((parameter) => formatValue(parameter[1])).join(",")})`;
476
494
 
477
- export const getActionLabel = (action: SpecAction) =>
478
- `${action.name === UNNAMED_ACTION ? "Action" : action.name}${formatParameters(action)} ${formatRange(action.location.range ?? BUILTIN_RANGE)}`;
495
+ // TLC's Action.getLocation, which TLC 1.7.4 names the action in without its parameters.
496
+ export const getActionLabel = (action: SpecAction, version?: TlcVersion) =>
497
+ `${action.name === UNNAMED_ACTION ? "Action" : action.name}${version === "1.7.4" ? "" : formatParameters(action)} ${formatRange(action.location.range ?? BUILTIN_RANGE)}`;
479
498
 
480
499
  // TLC's Action.getInvocationSignature, which names the action and the values of its parameters.
481
500
  export const getActionSignature = (action: SpecAction) =>
@@ -11,7 +11,7 @@ import {
11
11
  getNumber,
12
12
  getOperator,
13
13
  getSet,
14
- getIntegersSignature,
14
+ getJavaSignature,
15
15
  getJavaInteger,
16
16
  getValue,
17
17
  isOutOfRange,
@@ -19,7 +19,7 @@ import {
19
19
  } from "./builtins";
20
20
  import type { Builtin, BuiltinArgument } from "./evaluate";
21
21
  import type { CheckerRuntime } from "./runtime";
22
- import { BAGS_OPERATORS } from "./bags";
22
+ import { createBagsOperators } from "./bags";
23
23
  import { createJsonOperators, type FileSystem } from "./json";
24
24
  import { createRandomizationOperators } from "./randomization";
25
25
  import { createPossibleOperators, createTlcExtOperators } from "./tlc-ext";
@@ -40,7 +40,9 @@ import {
40
40
  isEnumerable,
41
41
  isFiniteSet,
42
42
  isFunctionValue,
43
- isReducible,
43
+ computeLikeTlcOnFailure,
44
+ isReducibleSet,
45
+ markStreamedSet,
44
46
  isSetValue,
45
47
  toFiniteSet,
46
48
  unionSorted,
@@ -48,13 +50,14 @@ import {
48
50
  createEnumeratedSet,
49
51
  EMPTY_SET,
50
52
  isEnumeratedSet,
51
- type ReducibleSet,
53
+ type IntervalValue,
52
54
  type MixedSetValue,
53
55
  type SetOperationValue,
54
56
  type SetValue,
55
57
  type Value,
56
58
  rankString,
57
59
  } from "./values";
60
+ import type { TlcVersion } from "./tlc-version";
58
61
 
59
62
  interface StandardModule {
60
63
  extends: string[];
@@ -76,6 +79,8 @@ const unionInTree = (sets: SetValue[], start: number, end: number): SetValue =>
76
79
 
77
80
  const SET_OPERATORS: SetOperationValue["operator"][] = ["\\cup", "\\cap", "\\"];
78
81
 
82
+ type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
83
+
79
84
  const isMixedSet = (value: Value) => typeof value === "object" && value.kind === "MixedSet";
80
85
 
81
86
  const getReducibleElements = (set: ReducibleSet) =>
@@ -96,7 +101,9 @@ const combineReducible = (
96
101
  );
97
102
  }
98
103
  if (elements.length === 0) return other;
99
- if (!isReducible(other)) return { kind: "SetOperation", operator, left: set, right: other };
104
+ if (!isReducibleSet(other)) {
105
+ return markStreamedSet({ kind: "SetOperation", operator, left: set, right: other });
106
+ }
100
107
  const added = toFiniteSet(other).elements.filter((element) => !isElementOf(element, set));
101
108
  return createEnumeratedSet([...elements, ...added]);
102
109
  };
@@ -123,7 +130,8 @@ export const CORE_OPERATORS: Builtin[] = [
123
130
  ),
124
131
  defineOperator("\\subseteq", 2, (args) => {
125
132
  const left = getSet(args, 0, "\\subseteq");
126
- const right = getSet(args, 1, "\\subseteq");
133
+ // TLC tests each element of the left set for membership in the right value.
134
+ const right = getValue(args, 1, "\\subseteq");
127
135
  const isPowerSets =
128
136
  typeof left === "object" &&
129
137
  left.kind === "Subset" &&
@@ -133,11 +141,18 @@ export const CORE_OPERATORS: Builtin[] = [
133
141
  if (isPowerSets) return isSubsetOf(toFiniteSet(left.base).elements, right.base);
134
142
  return isSubsetOf(toFiniteSet(left).elements, right);
135
143
  }),
144
+ // TLC defers the set operations of values that aren't sets to the enumeration or the membership
145
+ // tests of the result.
136
146
  ...SET_OPERATORS.map((operator) =>
137
147
  defineOperator(operator, 2, (args) => {
138
- const left = getSet(args, 0, operator);
139
- const right = getSet(args, 1, operator);
140
- if (isReducible(left) && isReducible(right) && !isMixedSet(left) && !isMixedSet(right)) {
148
+ const left = getValue(args, 0, operator);
149
+ const right = getValue(args, 1, operator);
150
+ if (
151
+ isReducibleSet(left) &&
152
+ isReducibleSet(right) &&
153
+ !isMixedSet(left) &&
154
+ !isMixedSet(right)
155
+ ) {
141
156
  if (operator === "\\cup" && isEmptySet(left)) return right;
142
157
  const merge =
143
158
  operator === "\\cup"
@@ -145,19 +160,24 @@ export const CORE_OPERATORS: Builtin[] = [
145
160
  : operator === "\\cap"
146
161
  ? intersectSorted
147
162
  : differenceSorted;
148
- return merge(toFiniteSet(left), toFiniteSet(right));
163
+ // The merge compares the elements, which TLC tests for membership one by one instead.
164
+ return computeLikeTlcOnFailure(
165
+ () => merge(toFiniteSet(left), toFiniteSet(right)),
166
+ () => combineReducible(operator, left, right),
167
+ );
149
168
  }
150
- if (isReducible(left)) return combineReducible(operator, left, right);
151
- if (operator !== "\\" && isReducible(right)) return combineReducible(operator, right, left);
152
- return { kind: "SetOperation", operator, left, right };
169
+ if (isReducibleSet(left)) return combineReducible(operator, left, right);
170
+ if (operator !== "\\" && isReducibleSet(right))
171
+ return combineReducible(operator, right, left);
172
+ return markStreamedSet({ kind: "SetOperation", operator, left, right });
153
173
  }),
154
174
  ),
155
- defineOperator("SUBSET", 1, (args) => ({ kind: "Subset", base: getSet(args, 0, "SUBSET") })),
175
+ defineOperator("SUBSET", 1, (args) => ({ kind: "Subset", base: getValue(args, 0, "SUBSET") })),
156
176
  defineOperator("UNION", 1, (args) => {
157
177
  const outer = getSet(args, 0, "UNION");
158
178
  const sets = isEnumeratedSet(outer) ? outer.elements.filter(isEnumeratedSet) : [];
159
179
  if (!isEnumeratedSet(outer) || sets.length !== outer.elements.length) {
160
- return { kind: "Union", base: outer };
180
+ return markStreamedSet({ kind: "Union", base: outer });
161
181
  }
162
182
  if (isMixedSet(outer) || sets.some(isMixedSet)) {
163
183
  const union: MixedSetValue = { kind: "MixedSet", elements: [] };
@@ -179,51 +199,79 @@ export const CORE_OPERATORS: Builtin[] = [
179
199
  }),
180
200
  ];
181
201
 
182
- const DOT_DOT_SIGNATURE = getIntegersSignature("DotDot", "IntervalValue", 2);
202
+ const NEG_SIGNATURE = getJavaSignature("Integers", "Neg", "IntValue", 1);
183
203
 
184
- const NEG_SIGNATURE = getIntegersSignature("Neg", "IntValue", 1);
204
+ // TLC overrides the arithmetic of Naturals with the Java methods of tlc2.module.Naturals, or with
205
+ // those of tlc2.module.Integers when the spec loads Integers, and names the method in an error.
206
+ const createArithmeticOperators = (module: string) => {
207
+ const dotDotSignature = getJavaSignature(module, "DotDot", "IntervalValue", 2);
208
+ return [
209
+ defineArithmetic(module, "+", "Plus", (left, right) => left + right),
210
+ defineArithmetic(module, "-", "Minus", (left, right) => left - right),
211
+ defineArithmetic(module, "*", "Times", (left, right) => left * right),
212
+ defineArithmetic(module, "^", "Expt", (base, exponent) => {
213
+ if (exponent < 0) {
214
+ throw new TlaModuleError(
215
+ `The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
216
+ );
217
+ }
218
+ if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
219
+ return base ** exponent;
220
+ }),
221
+ defineArithmetic(module, "\\div", "Divide", (left, right) => {
222
+ if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
223
+ // tlc2.module.Naturals#Divide floors only a quotient Java truncates below 0, and lets
224
+ // -2147483648 \div -1 overflow.
225
+ if (module === "Naturals") {
226
+ const truncated = Math.trunc(left / right) | 0;
227
+ return truncated < 0 && Math.imul(truncated, right) !== left ? truncated - 1 : truncated;
228
+ }
229
+ const quotient = Math.floor(left / right);
230
+ if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
231
+ return quotient;
232
+ }),
233
+ defineArithmetic(module, "%", "Mod", (left, right) => {
234
+ if (right <= 0) {
235
+ throw new TlaModuleError(
236
+ `The second argument of % should be a positive number, but instead it is:\n${right}`,
237
+ );
238
+ }
239
+ return ((left % right) + right) % right;
240
+ }),
241
+ defineOperator("..", 2, (args) => ({
242
+ kind: "Interval",
243
+ low: getJavaInteger(getValue(args, 0, ".."), dotDotSignature),
244
+ high: getJavaInteger(getValue(args, 1, ".."), dotDotSignature),
245
+ })),
246
+ ];
247
+ };
248
+
249
+ const NATURALS_ARITHMETIC = createArithmeticOperators("Naturals");
250
+
251
+ const INTEGERS_ARITHMETIC = createArithmeticOperators("Integers");
252
+
253
+ // The operators of Naturals that TLC overrides with the methods of tlc2.module.Integers when it
254
+ // loads Integers for its trace exploration spec, by the operator each replaces.
255
+ export const getTraceSpecArithmetic = (builtin: Builtin) => {
256
+ const index = NATURALS_ARITHMETIC.indexOf(builtin);
257
+ return index === -1 ? undefined : INTEGERS_ARITHMETIC[index];
258
+ };
185
259
 
186
260
  const NATURALS_OPERATORS = [
187
261
  defineConstant("Nat", { kind: "InfiniteSet", name: "Nat" }),
188
- defineArithmetic("+", "Plus", (left, right) => left + right),
189
- defineArithmetic("-", "Minus", (left, right) => left - right),
190
- defineArithmetic("*", "Times", (left, right) => left * right),
191
- defineArithmetic("^", "Expt", (base, exponent) => {
192
- if (exponent < 0) {
193
- throw new TlaModuleError(
194
- `The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
195
- );
196
- }
197
- if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
198
- return base ** exponent;
199
- }),
200
- defineArithmetic("\\div", "Divide", (left, right) => {
201
- if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
202
- const quotient = Math.floor(left / right);
203
- if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
204
- return quotient;
205
- }),
206
- defineArithmetic("%", "Mod", (left, right) => {
207
- if (right <= 0) {
208
- throw new TlaModuleError(
209
- `The second argument of % should be a positive number, but instead it is:\n${right}`,
210
- );
211
- }
212
- return ((left % right) + right) % right;
213
- }),
262
+ ...NATURALS_ARITHMETIC,
214
263
  defineComparison("<", (left, right) => left < right),
215
264
  defineComparison(">", (left, right) => left > right),
216
265
  defineComparison("<=", (left, right) => left <= right),
217
- defineComparison(">=", (left, right) => left >= right),
218
- defineOperator("..", 2, (args) => ({
219
- kind: "Interval",
220
- low: getJavaInteger(getValue(args, 0, ".."), DOT_DOT_SIGNATURE),
221
- high: getJavaInteger(getValue(args, 1, ".."), DOT_DOT_SIGNATURE),
222
- })),
266
+ // tlc2.module.Naturals#GEQ names itself > in its errors. Only a spec that extends Integers gets
267
+ // the GEQ of tlc2.module.Integers, not one that loads Integers for its trace exploration spec.
268
+ defineComparison(">=", (left, right) => left >= right, ">"),
223
269
  ];
224
270
 
225
271
  const INTEGERS_OPERATORS = [
226
272
  defineConstant("Int", { kind: "InfiniteSet", name: "Int" }),
273
+ ...INTEGERS_ARITHMETIC,
274
+ defineComparison(">=", (left, right) => left >= right),
227
275
  defineOperator("-.", 1, (args) => {
228
276
  const value = getJavaInteger(getValue(args, 0, "-"), NEG_SIGNATURE);
229
277
  if (isOutOfRange(-value)) throw createOverflowError(`-${value}`);
@@ -263,103 +311,111 @@ const withJavaMethods = (module: string, operators: Builtin[]) => {
263
311
  });
264
312
  };
265
313
 
266
- const SEQUENCES_OPERATORS = withJavaMethods("Sequences", [
267
- defineOperator("Seq", 1, (args) => ({ kind: "SequenceSet", base: getValue(args, 0, "Seq") })),
268
- defineOperator(
269
- "Len",
270
- 1,
271
- (args) => getSequenceOrString(args, 0, getSequenceMessage("", "Len")).length,
272
- ),
273
- defineOperator("\\o", 2, (args) => {
274
- const left = getSequenceOrString(args, 0, getEvaluatingMessage("s \\o t", "sequence"));
275
- if (typeof left !== "string") {
276
- const right = getSequenceArgument(args, 1, getEvaluatingMessage("t \\o s", "sequence"));
277
- return createTuple([...left, ...right]);
278
- }
279
- const right = getValue(args, 1, "\\o");
280
- if (typeof right !== "string") {
281
- throw new TlaModuleError(
282
- `${getEvaluatingMessage("t \\o s", "string")}\n${formatValue(right)}`,
283
- );
284
- }
285
- return rankString(left + right);
286
- }),
287
- defineOperator("Append", 2, (args) =>
288
- createTuple([
289
- ...getSequenceArgument(args, 0, getEvaluatingMessage("Append(s, v)", "sequence")),
290
- getValue(args, 1, "Append"),
291
- ]),
292
- ),
293
- defineOperator("Head", 1, (args) => {
294
- const elements = getSequenceArgument(args, 0, getSequenceMessage("", "Head"));
295
- if (elements.length === 0)
296
- throw new TlaModuleError("Attempted to apply Head to the empty sequence.");
297
- return elements[0];
298
- }),
299
- defineOperator("Tail", 1, (args) => {
300
- const sequence = getSequenceOrString(args, 0, getSequenceMessage("", "Tail"));
301
- if (sequence.length === 0)
302
- throw new TlaModuleError("Attempted to apply Tail to the empty sequence.");
303
- return sliceSequence(sequence, 1);
304
- }),
305
- defineOperator("SubSeq", 3, (args) => {
306
- const sequence = getSequenceOrString(args, 0, getSequenceMessage("first ", "SubSeq"));
307
- const start = getNumber(args, 1, "SubSeq");
308
- const end = getNumber(args, 2, "SubSeq");
309
- if (start > end) return sliceSequence(sequence, 0, 0);
310
- const outOfDomain =
311
- start < 1
312
- ? { position: "second", value: start }
313
- : end > sequence.length
314
- ? { position: "third", value: end }
315
- : undefined;
316
- if (outOfDomain) {
317
- throw new TlaModuleError(
318
- `The ${outOfDomain.position} argument of SubSeq must be in the domain of its first argument:\n${formatValue(getValue(args, 0, "SubSeq"))}\n, but instead it is\n${outOfDomain.value}`,
314
+ const createSequencesOperators = (version: TlcVersion) =>
315
+ withJavaMethods("Sequences", [
316
+ defineOperator("Seq", 1, (args) => ({ kind: "SequenceSet", base: getValue(args, 0, "Seq") })),
317
+ defineOperator(
318
+ "Len",
319
+ 1,
320
+ (args) => getSequenceOrString(args, 0, getSequenceMessage("", "Len")).length,
321
+ ),
322
+ defineOperator("\\o", 2, (args) => {
323
+ const left = getSequenceOrString(args, 0, getEvaluatingMessage("s \\o t", "sequence"));
324
+ if (typeof left !== "string") {
325
+ const right = getSequenceArgument(args, 1, getEvaluatingMessage("t \\o s", "sequence"));
326
+ return createTuple([...left, ...right]);
327
+ }
328
+ const right = getValue(args, 1, "\\o");
329
+ if (typeof right !== "string") {
330
+ throw new TlaModuleError(
331
+ `${getEvaluatingMessage("t \\o s", "string")}\n${formatValue(right)}`,
332
+ );
333
+ }
334
+ return rankString(left + right);
335
+ }),
336
+ defineOperator("Append", 2, (args) =>
337
+ createTuple([
338
+ ...getSequenceArgument(
339
+ args,
340
+ 0,
341
+ getEvaluatingMessage(version === "1.7.4" ? "Append(v, s)" : "Append(s, v)", "sequence"),
342
+ ),
343
+ getValue(args, 1, "Append"),
344
+ ]),
345
+ ),
346
+ defineOperator("Head", 1, (args) => {
347
+ const elements = getSequenceArgument(args, 0, getSequenceMessage("", "Head"));
348
+ if (elements.length === 0)
349
+ throw new TlaModuleError("Attempted to apply Head to the empty sequence.");
350
+ return elements[0];
351
+ }),
352
+ defineOperator("Tail", 1, (args) => {
353
+ const sequence = getSequenceOrString(args, 0, getSequenceMessage("", "Tail"));
354
+ if (sequence.length === 0)
355
+ throw new TlaModuleError("Attempted to apply Tail to the empty sequence.");
356
+ return sliceSequence(sequence, 1);
357
+ }),
358
+ defineOperator("SubSeq", 3, (args) => {
359
+ const sequence = getSequenceOrString(args, 0, getSequenceMessage("first ", "SubSeq"));
360
+ const start = getNumber(args, 1, "SubSeq");
361
+ const end = getNumber(args, 2, "SubSeq");
362
+ if (start > end) return sliceSequence(sequence, 0, 0);
363
+ const outOfDomain =
364
+ start < 1 || start > sequence.length
365
+ ? { position: "second", value: start }
366
+ : end > sequence.length
367
+ ? { position: "third", value: end }
368
+ : undefined;
369
+ if (outOfDomain) {
370
+ throw new TlaModuleError(
371
+ `The ${outOfDomain.position} argument of SubSeq must be in the domain of its first argument:\n${formatValue(getValue(args, 0, "SubSeq"))}\n, but instead it is\n${outOfDomain.value}`,
372
+ );
373
+ }
374
+ return sliceSequence(sequence, start - 1, end);
375
+ }),
376
+ defineHigherOrderOperator("SelectSeq", 2, (args) => {
377
+ const test = getOperator(args, 1, "SelectSeq");
378
+ return createTuple(
379
+ getSequenceArgument(args, 0, getSequenceMessage("first ", "SelectSeq")).filter(
380
+ (element) => {
381
+ const result = test.call([element]);
382
+ if (typeof result !== "boolean") {
383
+ throw new TlaModuleError(
384
+ `The second argument of SelectSeq should be a operator, but instead it is:\n${formatValue(result)}`,
385
+ );
386
+ }
387
+ return result;
388
+ },
389
+ ),
319
390
  );
320
- }
321
- return sliceSequence(sequence, start - 1, end);
322
- }),
323
- defineHigherOrderOperator("SelectSeq", 2, (args) => {
324
- const test = getOperator(args, 1, "SelectSeq");
325
- return createTuple(
326
- getSequenceArgument(args, 0, getSequenceMessage("first ", "SelectSeq")).filter((element) => {
327
- const result = test.call([element]);
328
- if (typeof result !== "boolean") {
329
- throw new TlaModuleError(
330
- `The second argument of SelectSeq should be a operator, but instead it is:\n${formatValue(result)}`,
331
- );
332
- }
333
- return result;
334
- }),
335
- );
336
- }),
337
- ]);
391
+ }),
392
+ ]);
338
393
 
339
- const FINITE_SETS_OPERATORS = withJavaMethods("FiniteSets", [
340
- defineOperator("IsFiniteSet", 1, (args) => {
341
- const value = getValue(args, 0, "IsFiniteSet");
342
- if (isSetValue(value)) return isFiniteSet(value);
343
- if (typeof value === "object" && value.kind === "Function") return true;
344
- throw new TlaEvaluationError(
345
- typeof value === "object" && value.kind === "LazyFunction"
346
- ? `Attempted to check if the function:\n${formatValue(value)}\nis a finite set.`
347
- : `Attempted to check if the ${describeKind(value)} ${formatValue(value)} is a finite set.`,
348
- );
349
- }),
350
- defineOperator("Cardinality", 1, (args) => {
351
- const set = getValue(args, 0, "Cardinality");
352
- if (
353
- !isSetValue(set) ||
354
- (typeof set === "object" && (set.kind === "InfiniteSet" || set.kind === "SequenceSet"))
355
- ) {
356
- throw new TlaModuleError(
357
- `Attempted to compute cardinality of the value\n${formatValue(set)}`,
394
+ const createFiniteSetsOperators = (version: TlcVersion) =>
395
+ withJavaMethods("FiniteSets", [
396
+ defineOperator("IsFiniteSet", 1, (args) => {
397
+ const value = getValue(args, 0, "IsFiniteSet");
398
+ if (isSetValue(value)) return isFiniteSet(value);
399
+ if (typeof value === "object" && value.kind === "Function") return true;
400
+ throw new TlaEvaluationError(
401
+ typeof value === "object" && value.kind === "LazyFunction"
402
+ ? `Attempted to check if the function:\n${formatValue(value)}\nis a finite set.`
403
+ : `Attempted to check if the ${describeKind(value)} ${formatValue(value)} is a finite set.`,
358
404
  );
359
- }
360
- return getCardinality(set);
361
- }),
362
- ]);
405
+ }),
406
+ defineOperator("Cardinality", 1, (args) => {
407
+ const set = getValue(args, 0, "Cardinality");
408
+ if (
409
+ !isSetValue(set) ||
410
+ (typeof set === "object" && (set.kind === "InfiniteSet" || set.kind === "SequenceSet"))
411
+ ) {
412
+ throw new TlaModuleError(
413
+ `Attempted to compute cardinality of the value\n${formatValue(set)}`,
414
+ );
415
+ }
416
+ return getCardinality(set, version);
417
+ }),
418
+ ]);
363
419
 
364
420
  // The modules that each standard module loads through EXTENDS and LOCAL INSTANCE in TLC.
365
421
  export const STANDARD_DEPENDENCIES = new Map([
@@ -379,6 +435,7 @@ export const getStandardModule = (
379
435
  print: (text: string) => void,
380
436
  runtime: CheckerRuntime,
381
437
  files: FileSystem,
438
+ builtins: ReadonlyMap<string, Builtin>,
382
439
  ): StandardModule | undefined => {
383
440
  switch (name) {
384
441
  case "Naturals":
@@ -386,9 +443,9 @@ export const getStandardModule = (
386
443
  case "Integers":
387
444
  return { extends: ["Naturals"], operators: INTEGERS_OPERATORS };
388
445
  case "Sequences":
389
- return { extends: [], operators: SEQUENCES_OPERATORS };
446
+ return { extends: [], operators: createSequencesOperators(runtime.version) };
390
447
  case "FiniteSets":
391
- return { extends: [], operators: FINITE_SETS_OPERATORS };
448
+ return { extends: [], operators: createFiniteSetsOperators(runtime.version) };
392
449
  case "TLC":
393
450
  return { extends: [], operators: withJavaMethods("TLC", createTlcOperators(print, runtime)) };
394
451
  case "Randomization":
@@ -398,7 +455,7 @@ export const getStandardModule = (
398
455
  case "Json":
399
456
  return { extends: [], operators: createJsonOperators(files) };
400
457
  case "Bags":
401
- return { extends: [], operators: BAGS_OPERATORS };
458
+ return { extends: [], operators: createBagsOperators(builtins) };
402
459
  case "TLCExt":
403
460
  return { extends: [], operators: createTlcExtOperators(print, runtime) };
404
461
  default: