tla-ts 0.0.3-dev.5e999f2 → 0.0.3-dev.62cf3d1

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.
package/src/parser.ts CHANGED
@@ -22,7 +22,7 @@ import type {
22
22
  StepBody,
23
23
  UseOrHide,
24
24
  } from "./ast";
25
- import { TlaSyntaxError } from "./errors";
25
+ import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
26
26
  import { getNumeralValue, tokenize, type Token } from "./lexer";
27
27
 
28
28
  interface InfixOperator {
@@ -631,11 +631,15 @@ const parse = (source: string, isTlapm: boolean): Module => {
631
631
  return parseExpression();
632
632
  };
633
633
 
634
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
635
+
634
636
  const parseArguments = () =>
635
637
  withoutOffside(() => {
638
+ const start = cursor.index;
636
639
  expect("(");
637
640
  const args = parseSeparated(parseArgument);
638
641
  expect(")");
642
+ lastArguments.range = getRange(start);
639
643
  return args;
640
644
  });
641
645
 
@@ -662,9 +666,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
662
666
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
663
667
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
664
668
  const fields = parseSeparated(() => {
669
+ const fieldStart = cursor.index;
670
+ const fieldLocation = getLocation(peek());
665
671
  const name = expectIdentifier();
672
+ fieldLocation.range = getRange(fieldStart);
666
673
  expect(kind === "Record" ? "|->" : ":");
667
- return { name, value: parseExpression() };
674
+ return { name, value: parseExpression(), location: fieldLocation };
668
675
  });
669
676
  expect("]");
670
677
  return { kind, fields, location };
@@ -856,7 +863,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
856
863
  const args = hasArguments ? parseArguments() : [];
857
864
  if (!isSelectorAhead()) {
858
865
  return hasArguments
859
- ? { kind: "Apply", operator: name, args, location }
866
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
860
867
  : { kind: "Identifier", name, location };
861
868
  }
862
869
  const instance = openModules
@@ -885,9 +892,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
885
892
  const argumentCounts = selectors.map((selector) =>
886
893
  "args" in selector ? selector.args.length : 0,
887
894
  );
895
+ const argumentsRange = lastArguments.range;
888
896
  return argumentCounts.slice(0, -1).some((count) => count > 0)
889
- ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
890
- : { kind: "Apply", operator, args: qualifiedArgs, location };
897
+ ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, argumentsRange, location }
898
+ : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
891
899
  }
892
900
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
893
901
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -1423,11 +1431,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
1423
1431
  location: getLocation(keyword),
1424
1432
  };
1425
1433
  const invalid =
1426
- use.kind === "Hide" && !isTlapm ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1427
- if (invalid) {
1428
- throw createError(
1429
- "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1430
- invalid.location,
1434
+ use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1435
+ if (invalid.length > 0) {
1436
+ throw createSanyError(
1437
+ invalid.map((fact) => ({
1438
+ location: formatRange(fact.location.range ?? getRange(start)),
1439
+ message:
1440
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1441
+ })),
1431
1442
  );
1432
1443
  }
1433
1444
  return withRange(use, start);
package/src/resolve.ts CHANGED
@@ -13,9 +13,16 @@ import {
13
13
  type Proof,
14
14
  type ProofStep,
15
15
  type SourceLocation,
16
+ type SourceRange,
16
17
  type Theorem,
17
18
  } from "./ast";
18
- import { TlaSyntaxError } from "./errors";
19
+ import {
20
+ createSanyError,
21
+ createSemanticError,
22
+ formatRange,
23
+ TlaSyntaxError,
24
+ type SanyError,
25
+ } from "./errors";
19
26
 
20
27
  export interface ModuleSymbol {
21
28
  key: string;
@@ -103,6 +110,21 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
103
110
  : unit.imported.get(name);
104
111
  };
105
112
 
113
+ // SANY reports a name at the range of its text.
114
+ const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
115
+ module: location.range?.module ?? "",
116
+ start: location,
117
+ end: { line: location.line, column: location.column + name.length - 1 },
118
+ });
119
+
120
+ // SANY reports an operator symbol applied to operands as one it cannot resolve.
121
+ const getFixity = (expression: Expression) => {
122
+ if (expression.kind !== "Apply") return undefined;
123
+ if (expression.args.length === 2) return "infix";
124
+ if (expression.args.length !== 1) return undefined;
125
+ return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
126
+ };
127
+
106
128
  // SANY names an undefined operator symbol such as + differently from an undefined identifier.
107
129
  const isOperatorSymbol = (name: string) =>
108
130
  name[0].toLowerCase() === name[0].toUpperCase() &&
@@ -135,7 +157,7 @@ const getSymbolArity = (
135
157
  }
136
158
  // A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
137
159
  // arguments.
138
- const prefix = name.slice(0, name.indexOf("!"));
160
+ const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
139
161
  const isSelected =
140
162
  prefix !== "" &&
141
163
  (lookupLocal(scope, prefix) !== undefined ||
@@ -144,11 +166,23 @@ const getSymbolArity = (
144
166
  if (isSelected) {
145
167
  return expression.kind === "Apply" ? expression.args.length : 0;
146
168
  }
147
- const position = `at line ${location.line}, column ${location.column}`;
148
- throw new TlaSyntaxError(
149
- isOperatorSymbol(name)
150
- ? `Could not find declaration or definition of symbol '${name}' ${position}`
151
- : `Unknown operator: \`${name}' ${position}`,
169
+ const range = getNameRange(location, name === "-." ? "-" : name);
170
+ if (!isOperatorSymbol(name)) throw createSemanticError(`Unknown operator: \`${name}'.`, range);
171
+ const missing = {
172
+ location: formatRange(range),
173
+ message: `Could not find declaration or definition of symbol '${name}'.`,
174
+ };
175
+ const fixity = getFixity(expression);
176
+ throw createSanyError(
177
+ fixity && location.range
178
+ ? [
179
+ missing,
180
+ {
181
+ location: formatRange(location.range),
182
+ message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
183
+ },
184
+ ]
185
+ : [missing],
152
186
  );
153
187
  };
154
188
 
@@ -235,13 +269,13 @@ const resolveApply = (
235
269
  scope: LocalName | undefined,
236
270
  unit: Unit,
237
271
  ) => {
238
- const callee = lookupLocal(scope, expression.operator)
239
- ? undefined
240
- : getVisibleDefinition(expression.operator, unit);
241
- const arity = getSymbolArity(expression, expression.operator, scope, unit);
272
+ const operator = expression.operator;
273
+ const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
274
+ const arity = getSymbolArity(expression, operator, scope, unit);
242
275
  if (arity !== expression.args.length) {
243
- throw new TlaSyntaxError(
244
- `The operator ${expression.operator} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
276
+ throw createSemanticError(
277
+ `The operator ${operator} requires ${arity} arguments.`,
278
+ expression.argumentsRange ?? getNameRange(expression.location, operator),
245
279
  );
246
280
  }
247
281
  expression.args.forEach((arg, index) => {
@@ -275,10 +309,12 @@ const resolveExpression = (
275
309
  case "ExceptAt":
276
310
  return;
277
311
  case "Identifier": {
278
- const arity = getSymbolArity(expression, expression.name, scope, unit);
312
+ const name = expression.name;
313
+ const arity = getSymbolArity(expression, name, scope, unit);
279
314
  if (arity > 0) {
280
- throw new TlaSyntaxError(
281
- `The operator ${expression.name} requires ${arity} arguments at line ${expression.location.line}, column ${expression.location.column}`,
315
+ throw createSemanticError(
316
+ `The operator ${name} requires ${arity} arguments.`,
317
+ getNameRange(expression.location, name),
282
318
  );
283
319
  }
284
320
  return;
@@ -347,14 +383,21 @@ const resolveExpression = (
347
383
  resolveAll([expression.domain, expression.range]);
348
384
  return;
349
385
  case "Record":
350
- case "RecordSet":
351
- if (new Set(expression.fields.map((field) => field.name)).size < expression.fields.length) {
352
- throw new TlaSyntaxError(
353
- `Non-unique fields in constructor at line ${expression.location.line}, column ${expression.location.column}`,
386
+ case "RecordSet": {
387
+ const repeated = expression.fields.filter((field, index) =>
388
+ expression.fields.slice(0, index).some((earlier) => earlier.name === field.name),
389
+ );
390
+ if (repeated.length > 0) {
391
+ throw createSanyError(
392
+ repeated.map((field) => ({
393
+ location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
394
+ message: "Non-unique fields in constructor.",
395
+ })),
354
396
  );
355
397
  }
356
398
  resolveAll(expression.fields.map((field) => field.value));
357
399
  return;
400
+ }
358
401
  case "Except":
359
402
  resolveAll([
360
403
  expression.function,
@@ -769,6 +812,29 @@ export const resolveSubstitutions = (
769
812
  );
770
813
  };
771
814
 
815
+ // SANY reports the errors of every unit it checks, in the order of the units in the module.
816
+ export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
817
+ const errors: SanyError[] = [];
818
+ units
819
+ .toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
820
+ .forEach((unit) => {
821
+ try {
822
+ unit.check();
823
+ } catch (error) {
824
+ if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
825
+ error.sany.errors
826
+ .filter((item) =>
827
+ errors.every(
828
+ (existing) =>
829
+ existing.location !== item.location || existing.message !== item.message,
830
+ ),
831
+ )
832
+ .forEach((item) => errors.push(item));
833
+ }
834
+ });
835
+ if (errors.length > 0) throw createSanyError(errors);
836
+ };
837
+
772
838
  export const resolveModule = (module: Module, unit: ModuleUnit) => {
773
839
  const symbols = createSymbols(module, unit.imported);
774
840
  const facts = new Set(
@@ -782,13 +848,24 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
782
848
  self,
783
849
  facts,
784
850
  });
785
- module.definitions.forEach((definition) =>
786
- resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
787
- );
788
- module.assumptions.forEach((assumption) =>
789
- resolveExpression(assumption.expression, undefined, createUnit(assumption.location, undefined)),
790
- );
791
- module.theorems.forEach((theorem) =>
792
- resolveTheorem(theorem, createUnit(theorem.location, undefined)),
793
- );
851
+ collectSemanticErrors([
852
+ ...module.definitions.map((definition) => ({
853
+ location: definition.location,
854
+ check: () =>
855
+ resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
856
+ })),
857
+ ...module.assumptions.map((assumption) => ({
858
+ location: assumption.location,
859
+ check: () =>
860
+ resolveExpression(
861
+ assumption.expression,
862
+ undefined,
863
+ createUnit(assumption.location, undefined),
864
+ ),
865
+ })),
866
+ ...module.theorems.map((theorem) => ({
867
+ location: theorem.location,
868
+ check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
869
+ })),
870
+ ]);
794
871
  };
package/src/run-cli.ts CHANGED
@@ -10,16 +10,18 @@ import {
10
10
  import { spawnSync } from "node:child_process";
11
11
  import { basename, delimiter, dirname, join, resolve } from "node:path";
12
12
  import { availableParallelism } from "node:os";
13
- import type { CheckResult, CheckStatus, TraceStep } from "./check";
13
+ import type { CheckResult, CheckStatus, Replay, TraceStep } from "./check";
14
14
  import { defineCommand, type ParsedArgs } from "./cli-options";
15
15
  import { parseConfig } from "./config";
16
16
  import {
17
+ createSanyError,
17
18
  getErrorExitCode,
18
19
  TlaConfigError,
19
20
  TlaError,
20
21
  TlaEvaluationError,
21
22
  TlaProofError,
22
23
  TlaSyntaxError,
24
+ type SanyReport,
23
25
  } from "./errors";
24
26
  import { EXIT_STATUS } from "./exit-codes";
25
27
  import type { LineRange } from "./obligation-generate";
@@ -135,14 +137,42 @@ const getTemporalHeadline = (names: string[]) =>
135
137
  ? `Error: Temporal property ${names[0]} was violated.\n`
136
138
  : `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
137
139
 
140
+ const getFrames = (callStack: string[]) =>
141
+ callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
142
+
143
+ const printCallStack = (frames: string) =>
144
+ console.log(
145
+ `Error: The error occurred when TLC was evaluating the nested\nexpressions at the following positions:\n${frames ? `${frames}\n` : " The error call stack is empty.\n"}`,
146
+ );
147
+
148
+ const printReplay = (replay: Replay) => {
149
+ replay.output.forEach((line) => console.log(line));
150
+ const frames = getFrames(replay.callStack ?? []);
151
+ const fingerprint = replay.fingerprint;
152
+ if (replay.isBeforeError) {
153
+ if (frames) printCallStack(frames);
154
+ } else if (replay.callStack && fingerprint) {
155
+ const sources = fingerprint.sources.map((source) => `${source}\n`).join("");
156
+ console.log(
157
+ `Error: TLC was unable to fingerprint.\n\nFingerprint Stack Trace:\n${frames ? `${frames}\n` : sources}\nReason:\n${fingerprint.reason}`,
158
+ );
159
+ } else if (replay.callStack) {
160
+ printCallStack(frames);
161
+ }
162
+ };
163
+
138
164
  const printResult = (result: CheckResult) => {
139
165
  if (result.failure) {
166
+ const replay = result.failure.replay;
167
+ if (replay?.isBeforeError) printReplay(replay);
140
168
  console.log(`Error: ${result.failure.message}`);
141
169
  if (result.failure.initialState) {
142
170
  console.log("While working on the initial state:");
143
171
  printState(result.failure.initialState);
144
172
  }
145
173
  printTrace(result.failure.trace);
174
+ if (replay && !replay.isBeforeError) printReplay(replay);
175
+ if (result.failure.isWithoutStatistics) return;
146
176
  } else if (result.violation) {
147
177
  const violation = result.violation;
148
178
  const name = violation.name;
@@ -219,8 +249,14 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
219
249
  const fileName = basename(specPath, ".tla");
220
250
  const moduleName = parseModule(source).name;
221
251
  if (moduleName !== fileName) {
222
- throw new TlaSyntaxError(
223
- `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
252
+ throw createSanyError(
253
+ [
254
+ {
255
+ location: "Unknown location",
256
+ message: `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
257
+ },
258
+ ],
259
+ true,
224
260
  );
225
261
  }
226
262
  const configPath =
@@ -286,21 +322,32 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
286
322
  return result.failure?.exitCode ?? EXIT_CODES[result.status];
287
323
  };
288
324
 
289
- // TLC prints the errors it raises itself. tla-ts names the others, such as a module SANY rejects.
290
- const printError = (error: TlaError) => {
325
+ const formatSanyErrors = (report: SanyReport) =>
326
+ `*** Errors: ${report.errors.length}\n\n${report.errors.map((error) => `${error.location}\n\n${error.message}\n\n`).join("\n")}`;
327
+
328
+ // TLC prints the errors it raises itself, and SANY's report of a spec it rejects. tla-ts names the
329
+ // others, such as a module it cannot parse.
330
+ const printError = (error: TlaError, path?: string) => {
291
331
  if (error instanceof TlaConfigError || error instanceof TlaEvaluationError) {
292
332
  return console.log(`Error: ${error.message}`);
293
333
  }
294
- console.error(`${error.name}: ${error.message}`);
334
+ const report = error instanceof TlaSyntaxError ? error.sany : undefined;
335
+ if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
336
+ console.log(
337
+ report.isFatal
338
+ ? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
339
+ : `Semantic errors:\n\n${formatSanyErrors(report)}`,
340
+ );
341
+ console.log("Error: Parsing or semantic analysis failed.");
295
342
  };
296
343
 
297
344
  // Prints a TlaError and returns its exit code.
298
- const reportErrors = async (run: () => number | Promise<number>) => {
345
+ const reportErrors = async (run: () => number | Promise<number>, path?: string) => {
299
346
  try {
300
347
  return await run();
301
348
  } catch (error) {
302
349
  if (!(error instanceof TlaError)) throw error;
303
- printError(error);
350
+ printError(error, path);
304
351
  return getErrorExitCode(error);
305
352
  }
306
353
  };
@@ -311,7 +358,7 @@ const runEach = async (paths: string[], runPath: (path: string) => number | Prom
311
358
  const exitCodes: number[] = [];
312
359
  for (const [index, path] of paths.entries()) {
313
360
  if (index > 0) console.log("");
314
- exitCodes.push(await reportErrors(() => runPath(path)));
361
+ exitCodes.push(await reportErrors(() => runPath(path), path));
315
362
  }
316
363
  return exitCodes.find((exitCode) => exitCode !== EXIT_STATUS.success) ?? EXIT_STATUS.success;
317
364
  };
@@ -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,
@@ -11,7 +11,7 @@ import {
11
11
  getNumber,
12
12
  getOperator,
13
13
  getSet,
14
- getIntegersSignature,
14
+ getArithmeticSignature,
15
15
  getJavaInteger,
16
16
  getValue,
17
17
  isOutOfRange,
@@ -123,7 +123,7 @@ export const CORE_OPERATORS: Builtin[] = [
123
123
  ),
124
124
  defineOperator("\\subseteq", 2, (args) => {
125
125
  const left = getSet(args, 0, "\\subseteq");
126
- const right = getSet(args, 1, "\\subseteq");
126
+ const right = getValue(args, 1, "\\subseteq");
127
127
  const isPowerSets =
128
128
  typeof left === "object" &&
129
129
  left.kind === "Subset" &&
@@ -135,8 +135,8 @@ export const CORE_OPERATORS: Builtin[] = [
135
135
  }),
136
136
  ...SET_OPERATORS.map((operator) =>
137
137
  defineOperator(operator, 2, (args) => {
138
- const left = getSet(args, 0, operator);
139
- const right = getSet(args, 1, operator);
138
+ const left = getValue(args, 0, operator);
139
+ const right = getValue(args, 1, operator);
140
140
  if (isReducible(left) && isReducible(right) && !isMixedSet(left) && !isMixedSet(right)) {
141
141
  if (operator === "\\cup" && isEmptySet(left)) return right;
142
142
  const merge =
@@ -145,7 +145,13 @@ export const CORE_OPERATORS: Builtin[] = [
145
145
  : operator === "\\cap"
146
146
  ? intersectSorted
147
147
  : differenceSorted;
148
- return merge(toFiniteSet(left), toFiniteSet(right));
148
+ // Merging compares elements across the sets. Where that fails, TLC's element-by-element
149
+ // membership test fails or succeeds on its own.
150
+ try {
151
+ return merge(toFiniteSet(left), toFiniteSet(right));
152
+ } catch (error) {
153
+ if (!(error instanceof TlaEvaluationError)) throw error;
154
+ }
149
155
  }
150
156
  if (isReducible(left)) return combineReducible(operator, left, right);
151
157
  if (operator !== "\\" && isReducible(right)) return combineReducible(operator, right, left);
@@ -179,51 +185,59 @@ export const CORE_OPERATORS: Builtin[] = [
179
185
  }),
180
186
  ];
181
187
 
182
- const DOT_DOT_SIGNATURE = getIntegersSignature("DotDot", "IntervalValue", 2);
188
+ const NEG_SIGNATURE = getArithmeticSignature("Integers", "Neg", "IntValue", 1);
183
189
 
184
- const NEG_SIGNATURE = getIntegersSignature("Neg", "IntValue", 1);
190
+ // TLC overrides the operators of Naturals with the Java class of Integers when a spec loads
191
+ // Integers, which names it in their errors.
192
+ const getArithmeticOperators = (javaClass: "Naturals" | "Integers") => {
193
+ const dotDotSignature = getArithmeticSignature(javaClass, "DotDot", "IntervalValue", 2);
194
+ return [
195
+ defineArithmetic(javaClass, "+", "Plus", (left, right) => left + right),
196
+ defineArithmetic(javaClass, "-", "Minus", (left, right) => left - right),
197
+ defineArithmetic(javaClass, "*", "Times", (left, right) => left * right),
198
+ defineArithmetic(javaClass, "^", "Expt", (base, exponent) => {
199
+ if (exponent < 0) {
200
+ throw new TlaModuleError(
201
+ `The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
202
+ );
203
+ }
204
+ if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
205
+ return base ** exponent;
206
+ }),
207
+ defineArithmetic(javaClass, "\\div", "Divide", (left, right) => {
208
+ if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
209
+ const quotient = Math.floor(left / right);
210
+ if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
211
+ return quotient;
212
+ }),
213
+ defineArithmetic(javaClass, "%", "Mod", (left, right) => {
214
+ if (right <= 0) {
215
+ throw new TlaModuleError(
216
+ `The second argument of % should be a positive number, but instead it is:\n${right}`,
217
+ );
218
+ }
219
+ return ((left % right) + right) % right;
220
+ }),
221
+ defineOperator("..", 2, (args) => ({
222
+ kind: "Interval",
223
+ low: getJavaInteger(getValue(args, 0, ".."), dotDotSignature),
224
+ high: getJavaInteger(getValue(args, 1, ".."), dotDotSignature),
225
+ })),
226
+ ];
227
+ };
185
228
 
186
229
  const NATURALS_OPERATORS = [
187
230
  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
- }),
231
+ ...getArithmeticOperators("Naturals"),
214
232
  defineComparison("<", (left, right) => left < right),
215
233
  defineComparison(">", (left, right) => left > right),
216
234
  defineComparison("<=", (left, right) => left <= right),
217
235
  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
- })),
223
236
  ];
224
237
 
225
238
  const INTEGERS_OPERATORS = [
226
239
  defineConstant("Int", { kind: "InfiniteSet", name: "Int" }),
240
+ ...getArithmeticOperators("Integers"),
227
241
  defineOperator("-.", 1, (args) => {
228
242
  const value = getJavaInteger(getValue(args, 0, "-"), NEG_SIGNATURE);
229
243
  if (isOutOfRange(-value)) throw createOverflowError(`-${value}`);