tla-ts 0.0.3-dev.fe239f8 → 0.0.4-dev.0be9567

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/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/live-check.ts CHANGED
@@ -83,6 +83,7 @@ interface Check {
83
83
  firstNodes: Pool<Int32Array>;
84
84
  lastNodes: Pool<Int32Array>;
85
85
  nextSiblings: number[];
86
+ nodeIds: NodeTable;
86
87
  isDone: boolean[];
87
88
  errorNode: number | undefined;
88
89
  }
@@ -140,6 +141,26 @@ const writeToPool = <Data extends Int32Array | Uint8Array>(
140
141
 
141
142
  const NO_NODE = -1;
142
143
 
144
+ // A Map holds at most 2^24 entries, so each tableau node of a state finds its node in an
145
+ // open-addressing table of (state plus one, tableau node, node) triples.
146
+ interface NodeTable {
147
+ slots: Int32Array;
148
+ size: number;
149
+ shift: number;
150
+ }
151
+
152
+ const getNodeSlot = (table: NodeTable, state: number, tableauNode: number) => {
153
+ const mask = table.slots.length / 3 - 1;
154
+ const hash = Math.imul(Math.imul(state, 0x9e3779b1) ^ tableauNode, 0x85ebca6b) >>> table.shift;
155
+ for (let index = hash; ; index = (index + 1) & mask) {
156
+ const slot = index * 3;
157
+ const stored = table.slots[slot];
158
+ if (stored === 0 || (stored === state + 1 && table.slots[slot + 1] === tableauNode)) {
159
+ return slot;
160
+ }
161
+ }
162
+ };
163
+
143
164
  const createNodes = (length: number) => new Int32Array(length).fill(NO_NODE);
144
165
 
145
166
  const UNWRITTEN = -1;
@@ -227,6 +248,7 @@ export const createLivenessChecker = (
227
248
  firstNodes: createPool(createNodes),
228
249
  lastNodes: createPool(createNodes),
229
250
  nextSiblings: [],
251
+ nodeIds: { slots: new Int32Array(3 << 10), size: 0, shift: 22 },
230
252
  isDone: [],
231
253
  errorNode: undefined,
232
254
  }));
@@ -322,12 +344,29 @@ export const createLivenessChecker = (
322
344
  model.infinitelyOftenActions.length === 0,
323
345
  );
324
346
 
325
- const getNodeId = (check: Check, state: number, tableauNode: number) =>
326
- check.solution.tableau
327
- ? getStateNodes(check, state).find((node) => check.nodeTableaux[node] === tableauNode)
328
- : state;
347
+ const getNodeId = (check: Check, state: number, tableauNode: number) => {
348
+ if (!check.solution.tableau) return state;
349
+ const slots = check.nodeIds.slots;
350
+ const slot = getNodeSlot(check.nodeIds, state, tableauNode);
351
+ return slots[slot] === 0 ? undefined : slots[slot + 2];
352
+ };
329
353
  const recordNode = (check: Check, state: number, tableauNode: number) => {
330
354
  const node = pushNode(check, state, tableauNode);
355
+ const table = check.nodeIds;
356
+ if (++table.size * 2 > table.slots.length / 3) {
357
+ const old = table.slots;
358
+ table.slots = new Int32Array(old.length * 2);
359
+ table.shift--;
360
+ for (let slot = 0; slot < old.length; slot += 3) {
361
+ if (old[slot] !== 0) {
362
+ table.slots.set(
363
+ old.subarray(slot, slot + 3),
364
+ getNodeSlot(table, old[slot] - 1, old[slot + 1]),
365
+ );
366
+ }
367
+ }
368
+ }
369
+ table.slots.set([state + 1, tableauNode, node], getNodeSlot(table, state, tableauNode));
331
370
  const last = check.lastNodes.data[state] ?? NO_NODE;
332
371
  if (last === NO_NODE) writeToPool(check.firstNodes, state, [node]);
333
372
  else check.nextSiblings[last] = node;
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;