tla-ts 0.0.3-dev.fe239f8 → 0.0.4-dev.5f6a4bf

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.3-dev.fe239f8",
3
+ "version": "0.0.4-dev.5f6a4bf",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/ast.ts CHANGED
@@ -34,10 +34,11 @@ export interface StringExpression {
34
34
  }
35
35
 
36
36
  // selectorRanges holds the range of each part of a name such as foo!lab, where SANY reports an
37
- // error in selecting that part.
37
+ // error in selecting that part. spelling holds the text of an operator written as a synonym.
38
38
  export interface IdentifierExpression {
39
39
  kind: "Identifier";
40
40
  name: string;
41
+ spelling?: string;
41
42
  selectorRanges?: SourceRange[];
42
43
  location: SourceLocation;
43
44
  resolution?: LocalResolution;
@@ -53,7 +54,8 @@ export interface ApplyExpression {
53
54
  // The number of args each part of a name like F(x)!l(y) takes, when a part before the last takes
54
55
  // some.
55
56
  argumentCounts?: number[];
56
- // The range of the parenthesized arguments of a named operator, where SANY reports their count.
57
+ // The range of the parenthesized arguments of a named operator, or of the whole name when its
58
+ // last part takes none, where SANY reports their count.
57
59
  argumentsRange?: SourceRange;
58
60
  selectorRanges?: SourceRange[];
59
61
  // The range of the arguments of each part of a name like F(x)!l(y), for a part that takes some.
package/src/config.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { TlaConfigError } from "./errors";
2
- import { matchOperator } from "./lexer";
2
+ import { matchOperator, matchUnicodeToken } from "./lexer";
3
3
  import { formatLexicalError } from "./sany-lexer";
4
4
  import type { TlcVersion } from "./tlc-version";
5
5
 
@@ -168,7 +168,7 @@ const tokenizeConfig = (source: string) => {
168
168
  skip(symbol);
169
169
  continue;
170
170
  }
171
- const operator = matchOperator(source, cursor.index);
171
+ const operator = matchUnicodeToken(source, cursor.index) ?? matchOperator(source, cursor.index);
172
172
  if (!operator) {
173
173
  addLexicalError(cursor.index, "");
174
174
  return tokens;
@@ -57,6 +57,8 @@ import {
57
57
  type Symbols,
58
58
  } from "./resolve";
59
59
  import { createRuntime } from "./runtime";
60
+ import { getSanyStandardModules } from "./sany-modules";
61
+ import { USER_STANDARD_MODULES } from "./tlc-modules";
60
62
  import { getSanyParseError } from "./sany-parse-error";
61
63
  import {
62
64
  CORE_OPERATORS,
@@ -381,6 +383,9 @@ export const createEnvironment = (
381
383
  };
382
384
 
383
385
  const getStandardSymbols = (name: string) => {
386
+ // SANY reads a module from the user's directories before its standard modules.
387
+ if (USER_STANDARD_MODULES.has(name) && options.loadModule?.(name) !== undefined)
388
+ return undefined;
384
389
  const symbols = addStandardModule(name);
385
390
  const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
386
391
  if (symbols) useModule(name, dependencies);
@@ -472,6 +477,17 @@ export const createEnvironment = (
472
477
  return cached;
473
478
  }
474
479
  const top = linkTopLevel(loadModule(name, importer));
480
+ // TLC replaces each definition of a module that has a standard module's name with the Java
481
+ // method of that name, so a user's Bags.tla keeps the definitions that Bags has no method for.
482
+ const standardModule = getSanyStandardModules(runtime.version).has(name)
483
+ ? getStandardModule(name, options.print, runtime, options.files, builtins)
484
+ : undefined;
485
+ standardModule?.operators.forEach((operator) => {
486
+ const symbol = top.linked.visible.get(operator.name);
487
+ if (symbol?.kind !== "Definition" || symbol.arity !== operator.arity) return;
488
+ builtins.set(symbol.key, operator);
489
+ definitions.delete(symbol.key);
490
+ });
475
491
  linkedModules.set(name, top.linked);
476
492
  dependencies.set(name, top.closure);
477
493
  useModule(name, top.closure);
@@ -912,9 +928,9 @@ export const createEnvironment = (
912
928
  reports,
913
929
  warnings: scope.warnings,
914
930
  });
931
+ // SANY keeps the first of two symbols of a name.
915
932
  const getSymbolsBefore = (location: SourceLocation): Symbols =>
916
- new Map([
917
- ...visible,
933
+ [
918
934
  ...declarations
919
935
  .filter((declaration) => isBefore(declaration.location, location))
920
936
  .map((declaration): [string, ModuleSymbol] => [
@@ -927,7 +943,10 @@ export const createEnvironment = (
927
943
  definition.name,
928
944
  getDefinitionSymbol(definition),
929
945
  ]),
930
- ]);
946
+ ].reduce(
947
+ (symbols, [name, symbol]) => (symbols.has(name) ? symbols : symbols.set(name, symbol)),
948
+ new Map(visible),
949
+ );
931
950
  // Generator.generateModule reports the errors of a nested module and goes on, and then
932
951
  // SymbolTable.addModule reports a module already defined.
933
952
  const linkNested = (nested: Module) => {
package/src/lexer.ts CHANGED
@@ -234,38 +234,84 @@ const SYMBOL_OPERATORS = [
234
234
  "?",
235
235
  ];
236
236
 
237
+ // The Unicode forms of the operators SANY's grammar, javacc/tla+.jj, reads, as their ASCII forms.
237
238
  const UNICODE_OPERATORS = new Map([
239
+ ["∃", "\\E"],
240
+ ["∀", "\\A"],
241
+ ["∷", "::"],
242
+ ["≜", "=="],
243
+ ["⟨", "<<"],
244
+ ["⟩_", ">>_"],
245
+ ["⟩", ">>"],
246
+ ["→", "->"],
247
+ ["←", "<-"],
248
+ ["↦", "|->"],
249
+ ["⁺", "^+"],
250
+ ["¬", "~"],
251
+ ["□", "[]"],
252
+ ["◇", "<>"],
238
253
  ["∧", "/\\"],
239
254
  ["∨", "\\/"],
240
- ["¬", "~"],
241
- ["⇒", "=>"],
242
- ["⟹", "=>"],
255
+ ["∈", "\\in"],
256
+ ["≈", "\\approx"],
257
+ ["≔", ":="],
258
+ ["≍", "\\asymp"],
259
+ ["◯", "\\bigcirc"],
260
+ ["⩴", "::="],
261
+ ["●", "\\bullet"],
262
+ ["∩", "\\cap"],
263
+ ["⋅", "\\cdot"],
264
+ ["∘", "\\o"],
265
+ ["≅", "\\cong"],
266
+ ["∪", "\\cup"],
267
+ ["÷", "\\div"],
268
+ ["≐", "\\doteq"],
269
+ ["‥", ".."],
270
+ ["…", "..."],
243
271
  ["≡", "<=>"],
272
+ ["‼", "!!"],
273
+ ["≥", ">="],
274
+ ["≫", "\\gg"],
244
275
  ["⇔", "<=>"],
245
- ["∈", "\\in"],
276
+ ["⇒", "=>"],
277
+ ["⫤", "=|"],
278
+ ["↝", "~>"],
279
+ ["≤", "<="],
280
+ ["≪", "\\ll"],
281
+ ["⊣", "-|"],
282
+ ["≠", "#"],
246
283
  ["∉", "\\notin"],
247
- ["⊆", "\\subseteq"],
284
+ ["⊙", "\\odot"],
285
+ ["⊖", "\\ominus"],
286
+ ["⊕", "\\oplus"],
287
+ ["⊘", "\\oslash"],
288
+ ["⊗", "\\otimes"],
289
+ ["⇸", "-+->"],
290
+ ["≺", "\\prec"],
291
+ ["⪯", "\\preceq"],
292
+ ["∝", "\\propto"],
293
+ ["⁇", "??"],
294
+ ["⊨", "|="],
295
+ ["⊢", "|-"],
296
+ ["∼", "\\sim"],
297
+ ["≃", "\\simeq"],
298
+ ["⊓", "\\sqcap"],
299
+ ["⊔", "\\sqcup"],
300
+ ["⊏", "\\sqsubset"],
301
+ ["⊑", "\\sqsubseteq"],
302
+ ["⊐", "\\sqsupset"],
303
+ ["⊒", "\\sqsupseteq"],
304
+ ["⋆", "\\star"],
248
305
  ["⊂", "\\subset"],
249
- ["⊇", "\\supseteq"],
306
+ ["⊆", "\\subseteq"],
307
+ ["≻", "\\succ"],
308
+ ["⪰", "\\succeq"],
250
309
  ["⊃", "\\supset"],
251
- ["∪", "\\cup"],
252
- ["∩", "\\cap"],
310
+ ["⊇", "\\supseteq"],
253
311
  ["×", "\\X"],
254
- ["∀", "\\A"],
255
- ["∃", "\\E"],
256
- ["≠", "#"],
257
- ["≤", "<="],
258
- ["≥", ">="],
259
- ["↦", "|->"],
260
- ["→", "->"],
261
- ["⟨", "<<"],
262
- ["⟩", ">>"],
263
- ["□", "[]"],
264
- ["◇", "<>"],
265
- ["≜", "=="],
266
- ["′", "'"],
267
- ["∘", "\\o"],
268
- ["⋅", "\\cdot"],
312
+ ["⊎", "\\uplus"],
313
+ ["‖", "||"],
314
+ ["≀", "\\wr"],
269
315
  ]);
270
316
 
271
317
  const BASED_NUMBER = /^\\([bB][01]+|[oO][0-7]+|[hH][0-9a-fA-F]+)/;
@@ -303,13 +349,27 @@ export const matchOperator = (source: string, index: number): OperatorMatch | un
303
349
  const operator = BACKSLASH_OPERATORS.get(word.slice(0, length.value));
304
350
  return { text: operator ?? "\\", length: operator === undefined ? 1 : length.value + 1 };
305
351
  }
306
- const unicodeOperator = UNICODE_OPERATORS.get(character);
307
- if (unicodeOperator) return { text: unicodeOperator, length: character.length };
352
+ const unicodeSymbol = [source.slice(index, index + 2), character].find((candidate) =>
353
+ UNICODE_OPERATORS.has(candidate),
354
+ );
355
+ if (unicodeSymbol !== undefined) {
356
+ return { text: UNICODE_OPERATORS.get(unicodeSymbol) ?? "", length: unicodeSymbol.length };
357
+ }
308
358
  const symbol = SYMBOL_OPERATORS.find((candidate) => source.startsWith(candidate, index));
309
359
  if (symbol) return { text: SYMBOL_ALIASES.get(symbol) ?? symbol, length: symbol.length };
310
360
  return character === "\\" ? { text: "\\", length: 1 } : undefined;
311
361
  };
312
362
 
363
+ // TLC's configuration parser reads its tokens with SANY's lexer, so it reads a Unicode operator, or
364
+ // ℕ, ℤ, or ℝ, as one token, which it compares as written with the words it expects.
365
+ const UNICODE_NAMES = new Set(["ℕ", "ℤ", "ℝ"]);
366
+ export const matchUnicodeToken = (source: string, index: number): OperatorMatch | undefined => {
367
+ const token = [source.slice(index, index + 2), source[index]].find(
368
+ (candidate) => UNICODE_OPERATORS.has(candidate) || UNICODE_NAMES.has(candidate),
369
+ );
370
+ return token === undefined ? undefined : { text: token, length: token.length };
371
+ };
372
+
313
373
  const isDigit = (character: string | undefined) =>
314
374
  character !== undefined && character >= "0" && character <= "9";
315
375
 
@@ -510,6 +570,11 @@ export const tokenize = (source: string): Token[] => {
510
570
  pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
511
571
  continue;
512
572
  }
573
+ if ("ℕℤℝ".includes(character)) {
574
+ pushToken("Identifier", names.get(character) ?? character, 1);
575
+ names.set(character, character);
576
+ continue;
577
+ }
513
578
  const stepLength = matchStep(source, cursor.index);
514
579
  if (stepLength > 0) {
515
580
  pushToken("Step", source.slice(cursor.index, cursor.index + stepLength), stepLength);
package/src/parser.ts CHANGED
@@ -356,9 +356,6 @@ const parse = (source: string, mode: ParseMode): Module => {
356
356
  const factNames = new Set<string>();
357
357
  // The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
358
358
  const proofScopes: Set<string>[] = [];
359
- // A fact or an operator argument may cite an operator of a parameterized instance without the
360
- // instance's arguments, as with Op <- I!Op in a substitution.
361
- const factStart = { index: -1 };
362
359
  // SANY reports a location in a nested module as one in the module of its file.
363
360
  const openModules: Module[] = [];
364
361
 
@@ -703,7 +700,7 @@ const parse = (source: string, mode: ParseMode): Module => {
703
700
  const location = getLocation(token);
704
701
  if (POSTFIX_OPERATORS.has(token.text)) {
705
702
  return withRange(
706
- { kind: "OperatorReference", operator: token.text, location },
703
+ { kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
707
704
  cursor.index - 1,
708
705
  );
709
706
  }
@@ -736,15 +733,9 @@ const parse = (source: string, mode: ParseMode): Module => {
736
733
  start,
737
734
  );
738
735
  }
739
- const argument = parseExpression();
740
- // SANY reads an argument that only names an operator of a parameterized instance, such as
741
- // I!Op, as that operator, which takes the arguments of the instance too.
742
- if (argument.kind === "Identifier") missingArguments.delete(argument);
743
- return argument;
736
+ return parseExpression();
744
737
  };
745
738
 
746
- const missingArguments = new Map<Expression, { location: string; message: string }>();
747
-
748
739
  const lastArguments: { range: SourceRange | undefined } = { range: undefined };
749
740
 
750
741
  const parseArguments = () =>
@@ -966,7 +957,6 @@ const parse = (source: string, mode: ParseMode): Module => {
966
957
 
967
958
  const parsePrimary = (preceding?: Precedence): Expression => {
968
959
  const start = cursor.index;
969
- const isFactStart = start === factStart.index;
970
960
  const token = advance();
971
961
  const location = getLocation(token);
972
962
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -1023,20 +1013,6 @@ const parse = (source: string, mode: ParseMode): Module => {
1023
1013
  ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
1024
1014
  : { kind: "Identifier", name, location };
1025
1015
  }
1026
- const instance = openModules
1027
- .flatMap((module) => module.instances)
1028
- .find((candidate) => candidate.name === name);
1029
- const isMissingArguments =
1030
- instance !== undefined &&
1031
- mode === "tlc" &&
1032
- !isFactStart &&
1033
- instance.params.length !== args.length;
1034
- if (isMissingArguments && args.length > 0) {
1035
- throw createError(
1036
- `The operator ${name} requires ${instance.params.length} arguments`,
1037
- token,
1038
- );
1039
- }
1040
1016
  const selectors: Selector[] = [{ kind: "Name", name, args }];
1041
1017
  const selectorRanges = [getRange(start, start)];
1042
1018
  while (isSelectorAhead()) {
@@ -1056,46 +1032,44 @@ const parse = (source: string, mode: ParseMode): Module => {
1056
1032
  const argumentCounts = selectors.map((selector) =>
1057
1033
  "args" in selector ? selector.args.length : 0,
1058
1034
  );
1059
- const argumentsRange = argumentRanges.findLast((range) => range !== undefined);
1060
- const reference: Expression =
1061
- names.length < selectors.length
1062
- ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1063
- : qualifiedArgs.length === 0
1064
- ? { kind: "Identifier", name: operator, selectorRanges, location }
1065
- : argumentCounts.slice(0, -1).some((count) => count > 0)
1066
- ? {
1067
- kind: "Apply",
1068
- operator,
1069
- args: qualifiedArgs,
1070
- argumentCounts,
1071
- argumentsRange,
1072
- selectorRanges,
1073
- argumentRanges,
1074
- location,
1075
- }
1076
- : {
1077
- kind: "Apply",
1078
- operator,
1079
- args: qualifiedArgs,
1080
- argumentsRange,
1081
- selectorRanges,
1082
- argumentRanges,
1083
- location,
1084
- };
1085
- if (isMissingArguments) {
1086
- missingArguments.set(reference, {
1087
- location: formatRange(getRange(start)),
1088
- message: `The operator ${name} requires ${instance.params.length} arguments.`,
1089
- });
1090
- }
1091
- return reference;
1035
+ const argumentsRange = argumentRanges.at(-1) ?? getRange(start);
1036
+ return names.length < selectors.length
1037
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1038
+ : qualifiedArgs.length === 0
1039
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
1040
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
1041
+ ? {
1042
+ kind: "Apply",
1043
+ operator,
1044
+ args: qualifiedArgs,
1045
+ argumentCounts,
1046
+ argumentsRange,
1047
+ selectorRanges,
1048
+ argumentRanges,
1049
+ location,
1050
+ }
1051
+ : {
1052
+ kind: "Apply",
1053
+ operator,
1054
+ args: qualifiedArgs,
1055
+ argumentsRange,
1056
+ selectorRanges,
1057
+ argumentRanges,
1058
+ location,
1059
+ };
1092
1060
  }
1093
1061
  // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1094
1062
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
1095
- return { kind: "Apply", operator: token.text, args: parseArguments(), location };
1063
+ return {
1064
+ kind: "Apply",
1065
+ operator: token.text,
1066
+ spelling: token.spelling,
1067
+ args: parseArguments(),
1068
+ location,
1069
+ };
1096
1070
  }
1097
1071
  if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
1098
- return { kind: "Identifier", name: token.text, location };
1072
+ return { kind: "Identifier", name: token.text, spelling: token.spelling, location };
1099
1073
  }
1100
1074
  switch (token.text) {
1101
1075
  case "(": {
@@ -1263,7 +1237,13 @@ const parse = (source: string, mode: ParseMode): Module => {
1263
1237
  advance();
1264
1238
  operators.last = POSTFIX_PRECEDENCE;
1265
1239
  state.left = withRange(
1266
- { kind: "Apply", operator: token.text, args: [state.left], location },
1240
+ {
1241
+ kind: "Apply",
1242
+ operator: token.text,
1243
+ spelling: token.spelling,
1244
+ args: [state.left],
1245
+ location,
1246
+ },
1267
1247
  start,
1268
1248
  );
1269
1249
  continue;
@@ -1607,7 +1587,6 @@ const parse = (source: string, mode: ParseMode): Module => {
1607
1587
  // start of an expression.
1608
1588
  const parseFact = (): Fact => {
1609
1589
  const start = cursor.index;
1610
- factStart.index = start;
1611
1590
  const token = peek();
1612
1591
  if (
1613
1592
  token.kind === "Operator" &&
@@ -1616,7 +1595,12 @@ const parse = (source: string, mode: ParseMode): Module => {
1616
1595
  ) {
1617
1596
  cursor.index++;
1618
1597
  return withRange(
1619
- { kind: "OperatorReference", operator: token.text, location: getLocation(token) },
1598
+ {
1599
+ kind: "OperatorReference",
1600
+ operator: token.text,
1601
+ spelling: token.spelling,
1602
+ location: getLocation(token),
1603
+ },
1620
1604
  start,
1621
1605
  );
1622
1606
  }
@@ -2006,7 +1990,6 @@ const parse = (source: string, mode: ParseMode): Module => {
2006
1990
  start: { line: location.line, column: location.column },
2007
1991
  end: { line: token.line, column: token.endColumn },
2008
1992
  };
2009
- if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
2010
1993
  openModules.pop();
2011
1994
  recursiveScopes.pop();
2012
1995
  return module;