tla-ts 0.0.1 → 0.0.3-dev.037ac3b

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 (74) hide show
  1. package/README.md +98 -39
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +225 -30
  4. package/dist/index.d.ts +225 -30
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +21 -1
  11. package/dist/run-cli.js +21 -1
  12. package/dist/transfer.cjs +400 -39
  13. package/dist/transfer.js +400 -39
  14. package/package.json +6 -2
  15. package/src/ast.ts +213 -6
  16. package/src/bags.ts +161 -71
  17. package/src/builtins.ts +203 -23
  18. package/src/bundled-modules.ts +63 -32
  19. package/src/check.ts +1132 -258
  20. package/src/cli-options.ts +64 -0
  21. package/src/codegen.ts +58 -34
  22. package/src/config.ts +39 -11
  23. package/src/dependencies.ts +2 -2
  24. package/src/environment.ts +557 -153
  25. package/src/errors.ts +66 -1
  26. package/src/evaluate.ts +553 -258
  27. package/src/exit-codes.ts +53 -0
  28. package/src/fingerprints.ts +48 -13
  29. package/src/index.ts +9 -4
  30. package/src/instances.ts +235 -79
  31. package/src/java-overrides.ts +241 -0
  32. package/src/json.ts +153 -72
  33. package/src/levels.ts +850 -0
  34. package/src/lexer.ts +108 -29
  35. package/src/live-check.ts +692 -0
  36. package/src/liveness.ts +71 -596
  37. package/src/obligation-action.ts +384 -0
  38. package/src/obligation-anon.ts +575 -0
  39. package/src/obligation-deref.ts +637 -0
  40. package/src/obligation-expression.ts +1060 -0
  41. package/src/obligation-format.ts +406 -0
  42. package/src/obligation-generate.ts +1074 -0
  43. package/src/obligation-levels.ts +329 -0
  44. package/src/obligation-module.ts +1042 -0
  45. package/src/obligation-printer.ts +889 -0
  46. package/src/obligation-proof.ts +76 -0
  47. package/src/obligation-source.ts +1084 -0
  48. package/src/obligation-tuples.ts +184 -0
  49. package/src/obligations.ts +316 -0
  50. package/src/parallel-worker.ts +186 -201
  51. package/src/parallel.ts +179 -84
  52. package/src/parser.ts +970 -206
  53. package/src/pretty-print.ts +259 -0
  54. package/src/randomization.ts +24 -113
  55. package/src/resolve.ts +483 -51
  56. package/src/run-cli.ts +473 -62
  57. package/src/runtime.ts +65 -28
  58. package/src/spec-description.ts +521 -0
  59. package/src/standard-levels.ts +105 -0
  60. package/src/standard-modules.ts +218 -92
  61. package/src/state-graph.ts +157 -0
  62. package/src/subexpressions.ts +31 -45
  63. package/src/substitution-levels.ts +184 -0
  64. package/src/successors.ts +336 -57
  65. package/src/symmetry.ts +79 -23
  66. package/src/tableau.ts +93 -60
  67. package/src/tlc-ext.ts +148 -44
  68. package/src/tlc-module.ts +71 -66
  69. package/src/tlc-modules.ts +1022 -0
  70. package/src/tlc-order.ts +511 -0
  71. package/src/tlc-random.ts +414 -0
  72. package/src/tlc-summary.ts +124 -0
  73. package/src/transfer.ts +26 -5
  74. package/src/values.ts +1285 -179
package/src/lexer.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import { TlaSyntaxError } from "./errors";
2
2
 
3
- export type TokenKind =
3
+ type TokenKind =
4
4
  | "Identifier"
5
5
  | "Number"
6
+ | "Decimal"
6
7
  | "String"
7
8
  | "Operator"
8
9
  | "Keyword"
10
+ | "Step"
9
11
  | "Separator"
10
12
  | "ModuleEnd"
11
13
  | "End";
@@ -15,22 +17,31 @@ export interface Token {
15
17
  text: string;
16
18
  line: number;
17
19
  column: number;
20
+ endColumn: number;
18
21
  }
19
22
 
20
23
  const KEYWORDS = new Set([
24
+ "ACTION",
25
+ "ACTIONS",
21
26
  "ASSUME",
22
27
  "ASSUMPTION",
23
28
  "AXIOM",
29
+ "BY",
24
30
  "CASE",
25
31
  "CHOOSE",
26
32
  "CONSTANT",
27
33
  "CONSTANTS",
28
34
  "COROLLARY",
35
+ "DEF",
36
+ "DEFINE",
37
+ "DEFS",
29
38
  "DOMAIN",
30
39
  "ELSE",
31
40
  "ENABLED",
32
41
  "EXCEPT",
33
42
  "EXTENDS",
43
+ "HAVE",
44
+ "HIDE",
34
45
  "IF",
35
46
  "IN",
36
47
  "INSTANCE",
@@ -39,18 +50,32 @@ const KEYWORDS = new Set([
39
50
  "LET",
40
51
  "LOCAL",
41
52
  "MODULE",
53
+ "NEW",
54
+ "OBVIOUS",
55
+ "OMITTED",
56
+ "ONLY",
42
57
  "OTHER",
58
+ "PICK",
43
59
  "PROOF",
44
60
  "PROPOSITION",
61
+ "PROVE",
62
+ "QED",
45
63
  "RECURSIVE",
64
+ "STATE",
46
65
  "SUBSET",
66
+ "SUFFICES",
67
+ "TAKE",
68
+ "TEMPORAL",
69
+ "TEMPORALS",
47
70
  "THEN",
48
71
  "THEOREM",
49
72
  "UNCHANGED",
50
73
  "UNION",
74
+ "USE",
51
75
  "VARIABLE",
52
76
  "VARIABLES",
53
77
  "WITH",
78
+ "WITNESS",
54
79
  ]);
55
80
 
56
81
  const BACKSLASH_OPERATORS = new Map([
@@ -249,6 +274,12 @@ const NUMBER_BASES = new Map([
249
274
  ["h", 16],
250
275
  ]);
251
276
 
277
+ // A numeral token keeps its text as typed, such as \hFF, so errors can quote it as TLC does.
278
+ export const getNumeralValue = (text: string) =>
279
+ text.startsWith("\\")
280
+ ? parseInt(text.slice(2), NUMBER_BASES.get(text[1].toLowerCase()))
281
+ : Number(text);
282
+
252
283
  const MODULE_HEADER = /-{4,}[ \t]*MODULE\b/;
253
284
 
254
285
  const isIdentifierCharacter = (character: string) => /[A-Za-z0-9_]/.test(character);
@@ -262,8 +293,13 @@ export const matchOperator = (source: string, index: number): OperatorMatch | un
262
293
  const character = source[index];
263
294
  if (character === "\\" && /[A-Za-z]/.test(source[index + 1] ?? "")) {
264
295
  const word = /^[A-Za-z]+/.exec(source.slice(index + 1))?.[0] ?? "";
265
- const operator = BACKSLASH_OPERATORS.get(word);
266
- return operator === undefined ? undefined : { text: operator, length: word.length + 1 };
296
+ // Like SANY, read the longest operator that the word starts with, so S\used is S \ used.
297
+ const length = { value: word.length };
298
+ while (length.value > 0 && !BACKSLASH_OPERATORS.has(word.slice(0, length.value))) {
299
+ length.value--;
300
+ }
301
+ const operator = BACKSLASH_OPERATORS.get(word.slice(0, length.value));
302
+ return { text: operator ?? "\\", length: operator === undefined ? 1 : length.value + 1 };
267
303
  }
268
304
  const unicodeOperator = UNICODE_OPERATORS.get(character);
269
305
  if (unicodeOperator) return { text: unicodeOperator, length: character.length };
@@ -272,6 +308,33 @@ export const matchOperator = (source: string, index: number): OperatorMatch | un
272
308
  return character === "\\" ? { text: "\\", length: 1 } : undefined;
273
309
  };
274
310
 
311
+ const isDigit = (character: string | undefined) =>
312
+ character !== undefined && character >= "0" && character <= "9";
313
+
314
+ export const skipWhile = (
315
+ source: string,
316
+ index: number,
317
+ isMatch: (character: string) => boolean,
318
+ ) => {
319
+ const end = { value: index };
320
+ while (end.value < source.length && isMatch(source[end.value])) end.value++;
321
+ return end.value;
322
+ };
323
+
324
+ // The length of a step number such as <1>a., <+>, or <2>*., which SANY reads as one token wherever
325
+ // it appears.
326
+ const matchStep = (source: string, index: number) => {
327
+ if (source[index] !== "<") return 0;
328
+ const isImplicit = source[index + 1] === "+" || source[index + 1] === "*";
329
+ const levelEnd = isImplicit ? index + 2 : skipWhile(source, index + 1, isDigit);
330
+ if (levelEnd === index + 1 || source[levelEnd] !== ">") return 0;
331
+ const nameEnd = skipWhile(source, levelEnd + 1, isIdentifierCharacter);
332
+ const isDash = nameEnd === levelEnd + 1 && (source[nameEnd] === "*" || source[nameEnd] === "-");
333
+ return (
334
+ skipWhile(source, isDash ? nameEnd + 1 : nameEnd, (character) => character === ".") - index
335
+ );
336
+ };
337
+
275
338
  const TAB_SIZE = 8;
276
339
 
277
340
  const getWidthAfter = (width: number, character: string | undefined) =>
@@ -282,6 +345,8 @@ export const tokenize = (source: string): Token[] => {
282
345
  if (!headerMatch)
283
346
  throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
284
347
  const tokens: Token[] = [];
348
+ // Each name is one string object, so looking a name up in a scope compares pointers.
349
+ const names = new Map<string, string>();
285
350
  const prefixLines = source.slice(0, headerMatch.index).split("\n");
286
351
  const cursor = {
287
352
  index: headerMatch.index,
@@ -303,7 +368,8 @@ export const tokenize = (source: string): Token[] => {
303
368
  return getWidthAfter(scan.width, source[cursor.index]);
304
369
  };
305
370
  const pushToken = (kind: TokenKind, text: string, length: number) => {
306
- tokens.push({ kind, text, line: cursor.line, column: getColumn() });
371
+ const column = getColumn();
372
+ tokens.push({ kind, text, line: cursor.line, column, endColumn: column + length - 1 });
307
373
  cursor.index += length;
308
374
  };
309
375
  const advanceOver = (length: number) => {
@@ -316,22 +382,26 @@ export const tokenize = (source: string): Token[] => {
316
382
  }
317
383
  };
318
384
  const createError = (message: string) =>
319
- new TlaSyntaxError(`${message} at line ${cursor.line}, column ${getColumn()}`);
385
+ new TlaSyntaxError(`${message} at line ${cursor.line}, column ${getColumn()}`, undefined, {
386
+ reason: message,
387
+ line: cursor.line,
388
+ column: getColumn(),
389
+ isEnd: cursor.index >= source.length,
390
+ });
320
391
 
321
392
  while (cursor.index < source.length) {
322
393
  const character = source[cursor.index];
323
- const rest = source.slice(cursor.index, cursor.index + 8);
324
394
 
325
395
  if (character === "\n" || character === " " || character === "\t" || character === "\r") {
326
396
  advanceOver(1);
327
397
  continue;
328
398
  }
329
- if (rest.startsWith("\\*")) {
399
+ if (source.startsWith("\\*", cursor.index)) {
330
400
  const lineEnd = source.indexOf("\n", cursor.index);
331
401
  advanceOver((lineEnd === -1 ? source.length : lineEnd) - cursor.index);
332
402
  continue;
333
403
  }
334
- if (rest.startsWith("(*")) {
404
+ if (source.startsWith("(*", cursor.index)) {
335
405
  const depth = { value: 0 };
336
406
  do {
337
407
  if (cursor.index >= source.length) throw createError("Unterminated comment");
@@ -347,8 +417,8 @@ export const tokenize = (source: string): Token[] => {
347
417
  } while (depth.value > 0);
348
418
  continue;
349
419
  }
350
- if (rest.startsWith("----")) {
351
- const length = /^-+/.exec(source.slice(cursor.index))?.[0].length ?? 0;
420
+ if (source.startsWith("----", cursor.index)) {
421
+ const length = skipWhile(source, cursor.index, (next) => next === "-") - cursor.index;
352
422
  if (
353
423
  source
354
424
  .slice(cursor.index + length)
@@ -359,14 +429,11 @@ export const tokenize = (source: string): Token[] => {
359
429
  pushToken("Separator", "----", length);
360
430
  continue;
361
431
  }
362
- if (rest.startsWith("====")) {
363
- const length = /^=+/.exec(source.slice(cursor.index))?.[0].length ?? 0;
432
+ if (source.startsWith("====", cursor.index)) {
433
+ const length = skipWhile(source, cursor.index, (next) => next === "=") - cursor.index;
364
434
  pushToken("ModuleEnd", "====", length);
365
435
  cursor.moduleDepth--;
366
- if (cursor.moduleDepth > 0) continue;
367
- const nextHeader = MODULE_HEADER.exec(source.slice(cursor.index));
368
- if (!nextHeader) break;
369
- advanceOver(nextHeader.index);
436
+ if (cursor.moduleDepth <= 0) break;
370
437
  continue;
371
438
  }
372
439
  if (character === '"') {
@@ -380,8 +447,7 @@ export const tokenize = (source: string): Token[] => {
380
447
  }
381
448
  if (source[cursor.index] === "\\") {
382
449
  const escaped = source[cursor.index + 1];
383
- const replacement = { n: "\n", t: "\t", r: "\r", f: "\f" }[escaped] ?? escaped;
384
- characters.push(replacement);
450
+ characters.push({ n: "\n", t: "\t", r: "\r", f: "\f" }[escaped] ?? escaped);
385
451
  advanceOver(2);
386
452
  continue;
387
453
  }
@@ -389,26 +455,42 @@ export const tokenize = (source: string): Token[] => {
389
455
  advanceOver(1);
390
456
  }
391
457
  advanceOver(1);
392
- tokens.push({ kind: "String", text: characters.join(""), line, column });
458
+ tokens.push({
459
+ kind: "String",
460
+ text: characters.join(""),
461
+ line,
462
+ column,
463
+ endColumn: getColumn() - 1,
464
+ });
393
465
  continue;
394
466
  }
395
467
  const basedNumber = BASED_NUMBER.exec(source.slice(cursor.index, cursor.index + 64));
396
468
  if (basedNumber) {
397
- const base = NUMBER_BASES.get(basedNumber[1][0].toLowerCase());
398
- pushToken("Number", String(parseInt(basedNumber[1].slice(1), base)), basedNumber[0].length);
469
+ pushToken("Number", basedNumber[0], basedNumber[0].length);
399
470
  continue;
400
471
  }
401
472
  if (isIdentifierCharacter(character)) {
402
- const word = /^[A-Za-z0-9_]+/.exec(source.slice(cursor.index))?.[0] ?? "";
473
+ const word = source.slice(
474
+ cursor.index,
475
+ skipWhile(source, cursor.index, isIdentifierCharacter),
476
+ );
403
477
  if (/^\d+$/.test(word)) {
404
- pushToken("Number", word, word.length);
478
+ const number = /^\d+(\.\d+)?/.exec(source.slice(cursor.index))?.[0] ?? word;
479
+ pushToken(number.includes(".") ? "Decimal" : "Number", number, number.length);
405
480
  continue;
406
481
  }
407
482
  if (word.startsWith("WF_") || word.startsWith("SF_")) {
408
483
  pushToken("Keyword", word.slice(0, 3), 3);
409
484
  continue;
410
485
  }
411
- pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", word, word.length);
486
+ const name = names.get(word) ?? word;
487
+ names.set(name, name);
488
+ pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
489
+ continue;
490
+ }
491
+ const stepLength = matchStep(source, cursor.index);
492
+ if (stepLength > 0) {
493
+ pushToken("Step", source.slice(cursor.index, cursor.index + stepLength), stepLength);
412
494
  continue;
413
495
  }
414
496
  const operator = matchOperator(source, cursor.index);
@@ -416,13 +498,10 @@ export const tokenize = (source: string): Token[] => {
416
498
  pushToken("Operator", operator.text, operator.length);
417
499
  continue;
418
500
  }
419
- if (character === "\\") {
420
- const word = /^[A-Za-z]+/.exec(source.slice(cursor.index + 1))?.[0] ?? "";
421
- throw createError(`Unknown operator \\${word}`);
422
- }
423
501
  throw createError(`Unexpected character ${JSON.stringify(character)}`);
424
502
  }
425
503
 
426
- tokens.push({ kind: "End", text: "", line: cursor.line, column: getColumn() });
504
+ const endColumn = getColumn();
505
+ tokens.push({ kind: "End", text: "", line: cursor.line, column: endColumn, endColumn });
427
506
  return tokens;
428
507
  };