tla-ts 0.0.2 → 0.0.3-dev.07eab97

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 (68) hide show
  1. package/README.md +78 -12
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +98 -15
  4. package/dist/index.d.ts +98 -15
  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 +23 -2
  11. package/dist/run-cli.js +23 -2
  12. package/dist/transfer.cjs +36 -16
  13. package/dist/transfer.js +36 -16
  14. package/package.json +3 -1
  15. package/src/ast.ts +139 -4
  16. package/src/builtins.ts +9 -3
  17. package/src/check.ts +555 -107
  18. package/src/cli-options.ts +64 -0
  19. package/src/codegen.ts +15 -22
  20. package/src/config.ts +224 -170
  21. package/src/dependencies.ts +2 -2
  22. package/src/environment.ts +156 -48
  23. package/src/errors.ts +123 -3
  24. package/src/evaluate.ts +258 -160
  25. package/src/exit-codes.ts +39 -42
  26. package/src/fingerprints.ts +48 -12
  27. package/src/instances.ts +16 -15
  28. package/src/levels.ts +181 -45
  29. package/src/lexer.ts +11 -2
  30. package/src/live-check.ts +772 -0
  31. package/src/liveness.ts +120 -543
  32. package/src/obligation-action.ts +384 -0
  33. package/src/obligation-anon.ts +575 -0
  34. package/src/obligation-deref.ts +637 -0
  35. package/src/obligation-expression.ts +1060 -0
  36. package/src/obligation-format.ts +406 -0
  37. package/src/obligation-generate.ts +1074 -0
  38. package/src/obligation-levels.ts +329 -0
  39. package/src/obligation-module.ts +1042 -0
  40. package/src/obligation-printer.ts +889 -0
  41. package/src/obligation-proof.ts +76 -0
  42. package/src/obligation-source.ts +1084 -0
  43. package/src/obligation-tuples.ts +184 -0
  44. package/src/obligations.ts +316 -0
  45. package/src/parallel-worker.ts +168 -212
  46. package/src/parallel.ts +78 -37
  47. package/src/parser.ts +350 -63
  48. package/src/resolve.ts +124 -44
  49. package/src/run-cli.ts +405 -45
  50. package/src/runtime.ts +3 -1
  51. package/src/spec-description.ts +19 -8
  52. package/src/standard-levels.ts +3 -3
  53. package/src/standard-modules.ts +54 -45
  54. package/src/state-graph.ts +157 -0
  55. package/src/subexpressions.ts +10 -10
  56. package/src/substitution-levels.ts +38 -17
  57. package/src/successors.ts +183 -39
  58. package/src/symmetry.ts +30 -8
  59. package/src/tableau.ts +51 -24
  60. package/src/tlc-ext.ts +29 -12
  61. package/src/tlc-module.ts +14 -33
  62. package/src/tlc-modules.ts +4 -0
  63. package/src/tlc-order.ts +25 -7
  64. package/src/tlc-random.ts +9 -6
  65. package/src/tlc-summary.ts +20 -5
  66. package/src/transfer.ts +22 -1
  67. package/src/value-sources.ts +29 -0
  68. package/src/values.ts +300 -83
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 {
@@ -135,6 +135,8 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
135
135
  ["^^", createInfix(14, 14, false)],
136
136
  ]);
137
137
 
138
+ const NEGATION_PRECEDENCE = 12;
139
+
138
140
  const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
139
141
 
140
142
  const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
@@ -153,6 +155,8 @@ const EXPRESSION_PREFIXES = new Set([
153
155
  "<>",
154
156
  ]);
155
157
 
158
+ const CIRCLED_OPERATORS = new Set(["+", "-", "/", "\\X"]);
159
+
156
160
  const OPERATOR_ARGUMENTS = new Set([
157
161
  "SUBSET",
158
162
  "UNION",
@@ -213,6 +217,41 @@ const PROOF_STARTS = new Set(["BY", "PROOF", "OBVIOUS", "OMITTED"]);
213
217
 
214
218
  const PROVABLE_STEPS = new Set(["Qed", "Pick", "Case", "Assert"]);
215
219
 
220
+ const TLAPM_PUNCTUATION = new Set([
221
+ ",",
222
+ ".",
223
+ "_",
224
+ "(",
225
+ ")",
226
+ "[",
227
+ "]",
228
+ "{",
229
+ "}",
230
+ "<<",
231
+ ">>",
232
+ "]_",
233
+ ">>_",
234
+ "==",
235
+ "!",
236
+ "@",
237
+ ":",
238
+ "::",
239
+ ";",
240
+ "->",
241
+ "<-",
242
+ "|->",
243
+ "\\A",
244
+ "\\AA",
245
+ "\\E",
246
+ "\\EE",
247
+ "\\forall",
248
+ "\\exists",
249
+ "WF_",
250
+ "SF_",
251
+ "(*{",
252
+ "}*)",
253
+ ]);
254
+
216
255
  const UNNAMED_STEP_LABELS = new Set(["", "*", "-"]);
217
256
 
218
257
  const FACT_KEYWORDS = new Set([
@@ -264,7 +303,9 @@ export const extractModule = (source: string, name: string) => {
264
303
  return lines.slice(start, end === -1 ? undefined : end + 1).join("\n");
265
304
  };
266
305
 
267
- export const parseModule = (source: string): Module => {
306
+ // isTlapm parses as tlapm does for proof obligations, which accepts and rejects some modules that
307
+ // SANY does not.
308
+ const parse = (source: string, isTlapm: boolean): Module => {
268
309
  const tokens = tokenize(source);
269
310
  const cursor = { index: 0, offsideColumns: [] as number[] };
270
311
  // A RECURSIVE declaration covers the definitions of its own LET or module.
@@ -280,7 +321,7 @@ export const parseModule = (source: string): Module => {
280
321
 
281
322
  const getLocation = (token: Token): SourceLocation => ({
282
323
  line: token.line,
283
- column: token.column,
324
+ column: isTlapm && token.kind === "String" ? token.endColumn : token.column,
284
325
  range: undefined,
285
326
  });
286
327
 
@@ -324,10 +365,78 @@ export const parseModule = (source: string): Module => {
324
365
  const describe = (token: Token) =>
325
366
  token.kind === "End" ? "end of input" : JSON.stringify(token.text);
326
367
 
327
- const createError = (message: string, token: { line: number; column: number } = peekRaw()) =>
328
- new TlaSyntaxError(`${message} at line ${token.line}, column ${token.column}`);
368
+ const unitStart = { index: 0 };
369
+
370
+ const getDefinitionEnd = (fallback: { line: number; column: number }) => {
371
+ const equals = tokens
372
+ .slice(unitStart.index, cursor.index)
373
+ .find((token) => token.text === "==" && token.kind !== "String");
374
+ return equals ? { line: equals.line, column: equals.endColumn + 1 } : fallback;
375
+ };
376
+
377
+ // tlapm reads TRUE, FALSE, BOOLEAN, STRING and the other keywords only as keywords.
378
+ const isTlapmKeyword = (token: Token) =>
379
+ isTlapm && (token.kind === "Keyword" || BUILTIN_CONSTANTS.has(token.text));
380
+
381
+ const getPreviousEnd = (position: { line: number; column: number }) => {
382
+ const previous = tokens.findLast(
383
+ (token) =>
384
+ token.kind !== "End" &&
385
+ (token.line < position.line ||
386
+ (token.line === position.line && token.column < position.column)),
387
+ );
388
+ return previous ? { line: previous.line, column: previous.endColumn + 1 } : position;
389
+ };
390
+
391
+ const getTlapmName = (token: Token) => {
392
+ if (/^-{4,}$/.test(token.text)) return "----";
393
+ if (/^={4,}$/.test(token.text)) return "====";
394
+ if (TLAPM_PUNCTUATION.has(token.text) || token.kind === "Number" || token.kind === "Decimal")
395
+ return token.text;
396
+ if (token.kind === "String") return JSON.stringify(token.text);
397
+ if (isTlapmKeyword(token)) return `keyword ${token.text}`;
398
+ return token.kind === "Identifier" ? `identifier ${token.text}` : `operator ${token.text}`;
399
+ };
400
+
401
+ const createError = (
402
+ message: string,
403
+ token: { line: number; column: number } = peekRaw(),
404
+ unexpected?: Token,
405
+ ) => {
406
+ const end = tokens[tokens.length - 1];
407
+ const isEnd = token.line === end.line && token.column === end.column;
408
+ return new TlaSyntaxError(
409
+ `${message} at line ${token.line}, column ${token.column}`,
410
+ undefined,
411
+ getFailure(message, token, isEnd, unexpected),
412
+ );
413
+ };
414
+
415
+ const getFailure = (
416
+ message: string,
417
+ token: { line: number; column: number },
418
+ isEnd: boolean,
419
+ unexpected: Token | undefined,
420
+ ) => {
421
+ if (isTlapm && isEnd) {
422
+ const end = tokens[tokens.length - 1];
423
+ return { reason: "Unexpected EOF", ...getDefinitionEnd(getPreviousEnd(end)), isEnd };
424
+ }
425
+ if (isTlapm && unexpected) {
426
+ return {
427
+ reason: `Unexpected ${getTlapmName(unexpected)}`,
428
+ ...getPreviousEnd(unexpected),
429
+ isEnd,
430
+ };
431
+ }
432
+ return { reason: message, line: token.line, column: token.column, isEnd };
433
+ };
329
434
 
330
- const isFieldName = (token: Token) => token.kind === "Identifier" || token.kind === "Keyword";
435
+ const createUnexpected = (message: string, token: Token = peekRaw()) =>
436
+ createError(message, token, token);
437
+
438
+ const isFieldName = (token: Token) =>
439
+ (token.kind === "Identifier" || token.kind === "Keyword") && !isTlapmKeyword(token);
331
440
 
332
441
  const isAt = (text: string, offset = 0) => {
333
442
  const token = peek(offset);
@@ -349,15 +458,16 @@ export const parseModule = (source: string): Module => {
349
458
 
350
459
  const expect = (text: string) => {
351
460
  if (!isAt(text))
352
- throw createError(`Expected ${JSON.stringify(text)} but found ${describe(peek())}`);
461
+ throw createUnexpected(`Expected ${JSON.stringify(text)} but found ${describe(peek())}`);
353
462
  return advance();
354
463
  };
355
464
 
356
465
  const expectIdentifier = () => {
357
466
  const token = peek();
358
467
  if (token.kind !== "Identifier") {
359
- throw createError(`Expected an identifier but found ${describe(token)}`);
468
+ throw createUnexpected(`Expected an identifier but found ${describe(token)}`);
360
469
  }
470
+ if (isTlapmKeyword(token)) throw createUnexpected(`Unexpected keyword ${token.text}`);
361
471
  cursor.index++;
362
472
  return token.text;
363
473
  };
@@ -422,7 +532,7 @@ export const parseModule = (source: string): Module => {
422
532
  const token = peek();
423
533
  if (token.kind === "Identifier") {
424
534
  const start = cursor.index;
425
- const names = [expectIdentifier()];
535
+ const names = [advance().text];
426
536
  while (isAt("!") && peek(1).kind === "Identifier") {
427
537
  advance();
428
538
  names.push(expectIdentifier());
@@ -433,7 +543,7 @@ export const parseModule = (source: string): Module => {
433
543
  );
434
544
  }
435
545
  if (isAt("<<") || isAt("(")) return parsePrimary();
436
- throw createError(`Expected a subscript but found ${describe(token)}`);
546
+ throw createUnexpected(`Expected a subscript but found ${describe(token)}`);
437
547
  };
438
548
 
439
549
  const isDefinitionAhead = (operator: Token) => {
@@ -470,6 +580,23 @@ export const parseModule = (source: string): Module => {
470
580
  token.kind === "Operator" &&
471
581
  (INFIX_OPERATORS.has(token.text) || PREFIX_OPERATOR_REFERENCES.has(token.text)) &&
472
582
  isStandalone(token);
583
+ // tlapm reads a prefix operator as the start of an expression, and (+), (-), (/) and (\X) as
584
+ // the operators \oplus, \ominus, \oslash and \otimes.
585
+ const isTlapmIncomplete =
586
+ isTlapm &&
587
+ token.kind !== "String" &&
588
+ (PREFIX_OPERATOR_REFERENCES.has(token.text) ||
589
+ (OPERATOR_ARGUMENTS.has(token.text) &&
590
+ token.text !== "'" &&
591
+ !POSTFIX_OPERATORS.has(token.text)) ||
592
+ (CIRCLED_OPERATORS.has(token.text) &&
593
+ peek(-1).text === "(" &&
594
+ peek(-1).line === token.line &&
595
+ peek(-1).endColumn + 1 === token.column &&
596
+ peek(1).text === ")" &&
597
+ peek(1).line === token.line &&
598
+ peek(1).column === token.endColumn + 1));
599
+ if (isTlapmIncomplete && isStandalone(token)) throw createError("incomplete expression", token);
473
600
  if (isOperatorAlone) {
474
601
  cursor.index++;
475
602
  return withRange(
@@ -501,14 +628,24 @@ export const parseModule = (source: string): Module => {
501
628
  const body = parseExpression();
502
629
  return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
503
630
  }
504
- return parseExpression();
631
+ const argument = parseExpression();
632
+ // SANY reads an argument that only names an operator of a parameterized instance, such as
633
+ // I!Op, as that operator, which takes the arguments of the instance too.
634
+ if (argument.kind === "Identifier") missingArguments.delete(argument);
635
+ return argument;
505
636
  };
506
637
 
638
+ const missingArguments = new Map<Expression, { location: string; message: string }>();
639
+
640
+ const lastArguments: { range: SourceRange | undefined } = { range: undefined };
641
+
507
642
  const parseArguments = () =>
508
643
  withoutOffside(() => {
644
+ const start = cursor.index;
509
645
  expect("(");
510
646
  const args = parseSeparated(parseArgument);
511
647
  expect(")");
648
+ lastArguments.range = getRange(start);
512
649
  return args;
513
650
  });
514
651
 
@@ -524,6 +661,7 @@ export const parseModule = (source: string): Module => {
524
661
  return {
525
662
  kind: bullet.text === "/\\" ? "And" : "Or",
526
663
  operands,
664
+ isBulleted: true,
527
665
  location: getLocation(bullet),
528
666
  };
529
667
  };
@@ -534,9 +672,12 @@ export const parseModule = (source: string): Module => {
534
672
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
535
673
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
536
674
  const fields = parseSeparated(() => {
675
+ const fieldStart = cursor.index;
676
+ const fieldLocation = getLocation(peek());
537
677
  const name = expectIdentifier();
678
+ fieldLocation.range = getRange(fieldStart);
538
679
  expect(kind === "Record" ? "|->" : ":");
539
- return { name, value: parseExpression() };
680
+ return { name, value: parseExpression(), location: fieldLocation };
540
681
  });
541
682
  expect("]");
542
683
  return { kind, fields, location };
@@ -561,7 +702,7 @@ export const parseModule = (source: string): Module => {
561
702
  while (isAt(".") || isAt("[")) {
562
703
  if (accept(".")) {
563
704
  if (!isFieldName(peek()))
564
- throw createError(`Expected a field name but found ${describe(peek())}`);
705
+ throw createUnexpected(`Expected a field name but found ${describe(peek())}`);
565
706
  path.push({ kind: "Field", name: advance().text });
566
707
  continue;
567
708
  }
@@ -637,7 +778,14 @@ export const parseModule = (source: string): Module => {
637
778
  }
638
779
  const names = parseSeparated(expectIdentifier);
639
780
  expect(":");
640
- return { kind: "UnboundedQuantifier", names, body: parseExpression(), isTemporal, location };
781
+ return {
782
+ kind: "UnboundedQuantifier",
783
+ quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
784
+ names,
785
+ body: parseExpression(),
786
+ isTemporal,
787
+ location,
788
+ };
641
789
  };
642
790
 
643
791
  const parseCase = (start: Token): Expression => {
@@ -688,10 +836,17 @@ export const parseModule = (source: string): Module => {
688
836
  };
689
837
 
690
838
  const parsePrimary = (): Expression => {
691
- const isFactStart = cursor.index === factStart.index;
839
+ const start = cursor.index;
840
+ const isFactStart = start === factStart.index;
692
841
  const token = advance();
693
842
  const location = getLocation(token);
843
+ if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
844
+ const params = parseSeparated(expectIdentifier);
845
+ expect(":");
846
+ return { kind: "Lambda", params, body: parseExpression(), location };
847
+ }
694
848
  if (token.kind === "Number") {
849
+ if (isTlapm && /^\\[BOH]/.test(token.text)) throw createError("incomplete expression", token);
695
850
  return { kind: "Number", value: getNumeralValue(token.text), text: token.text, location };
696
851
  }
697
852
  if (token.kind === "Decimal") return { kind: "Decimal", text: token.text, location };
@@ -715,13 +870,18 @@ export const parseModule = (source: string): Module => {
715
870
  const args = hasArguments ? parseArguments() : [];
716
871
  if (!isSelectorAhead()) {
717
872
  return hasArguments
718
- ? { kind: "Apply", operator: name, args, location }
873
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
719
874
  : { kind: "Identifier", name, location };
720
875
  }
721
876
  const instance = openModules
722
877
  .flatMap((module) => module.instances)
723
878
  .find((candidate) => candidate.name === name);
724
- if (instance && !isFactStart && instance.params.length !== args.length) {
879
+ const isMissingArguments =
880
+ instance !== undefined &&
881
+ !isFactStart &&
882
+ !isTlapm &&
883
+ instance.params.length !== args.length;
884
+ if (isMissingArguments && args.length > 0) {
725
885
  throw createError(
726
886
  `The operator ${name} requires ${instance.params.length} arguments`,
727
887
  token,
@@ -735,14 +895,36 @@ export const parseModule = (source: string): Module => {
735
895
  const names = selectors.flatMap((selector) =>
736
896
  selector.kind === "Name" ? [selector.name] : [],
737
897
  );
738
- if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
739
898
  const operator = names.join("!");
740
899
  const qualifiedArgs = selectors.flatMap((selector) =>
741
900
  "args" in selector ? selector.args : [],
742
901
  );
743
- return qualifiedArgs.length > 0
744
- ? { kind: "Apply", operator, args: qualifiedArgs, location }
745
- : { kind: "Identifier", name: operator, location };
902
+ const argumentCounts = selectors.map((selector) =>
903
+ "args" in selector ? selector.args.length : 0,
904
+ );
905
+ const argumentsRange = lastArguments.range;
906
+ const reference: Expression =
907
+ names.length < selectors.length
908
+ ? { kind: "Subexpression", selectors, location }
909
+ : qualifiedArgs.length === 0
910
+ ? { kind: "Identifier", name: operator, location }
911
+ : argumentCounts.slice(0, -1).some((count) => count > 0)
912
+ ? {
913
+ kind: "Apply",
914
+ operator,
915
+ args: qualifiedArgs,
916
+ argumentCounts,
917
+ argumentsRange,
918
+ location,
919
+ }
920
+ : { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
921
+ if (isMissingArguments) {
922
+ missingArguments.set(reference, {
923
+ location: formatRange(getRange(start)),
924
+ message: `The operator ${name} requires ${instance.params.length} arguments.`,
925
+ });
926
+ }
927
+ return reference;
746
928
  }
747
929
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
748
930
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -779,6 +961,8 @@ export const parseModule = (source: string): Module => {
779
961
  const instances: Instance[] = [];
780
962
  recursiveScopes.push(new Set());
781
963
  while (!accept("IN")) {
964
+ if (isTlapm && isAt("RECURSIVE"))
965
+ throw createError("Unexpected keyword RECURSIVE", getDefinitionEnd(peekRaw()));
782
966
  if (accept("RECURSIVE")) {
783
967
  declareRecursive();
784
968
  continue;
@@ -816,7 +1000,7 @@ export const parseModule = (source: string): Module => {
816
1000
  };
817
1001
  }
818
1002
  }
819
- throw createError(`Unexpected ${describe(token)}`, token);
1003
+ throw createUnexpected(`Unexpected ${describe(token)}`, token);
820
1004
  };
821
1005
 
822
1006
  const parsePrefix = (): Expression => {
@@ -832,7 +1016,12 @@ export const parseModule = (source: string): Module => {
832
1016
  return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
833
1017
  case "-":
834
1018
  advance();
835
- return { kind: "Apply", operator: "-.", args: [parseExpression(13)], location };
1019
+ return {
1020
+ kind: "Apply",
1021
+ operator: "-.",
1022
+ args: [parseExpression(NEGATION_PRECEDENCE + 1)],
1023
+ location,
1024
+ };
836
1025
  case "[]":
837
1026
  advance();
838
1027
  return { kind: "Always", expression: parseExpression(16), location };
@@ -909,6 +1098,10 @@ export const parseModule = (source: string): Module => {
909
1098
  }
910
1099
  }
911
1100
  advance();
1101
+ // SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
1102
+ if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1103
+ throw createError(`Precedence conflict between ${token.text} and -`, peek());
1104
+ }
912
1105
  const right = parseExpression(infix.high + 1);
913
1106
  const left = state.left;
914
1107
  if (token.text === "/\\" || token.text === "\\/") {
@@ -1004,9 +1197,13 @@ export const parseModule = (source: string): Module => {
1004
1197
  location,
1005
1198
  };
1006
1199
  }
1200
+ const definitionStart = cursor.index;
1007
1201
  const params = parseParams();
1008
1202
  expect("==");
1009
- if (isAt("INSTANCE")) return parseInstance(name, params, isLocal, location);
1203
+ if (isAt("INSTANCE")) {
1204
+ const instance = parseInstance(name, params, isLocal, location);
1205
+ return { ...instance, definitionRange: getRange(definitionStart) };
1206
+ }
1010
1207
  return parseOperator(name, params);
1011
1208
  };
1012
1209
 
@@ -1016,6 +1213,7 @@ export const parseModule = (source: string): Module => {
1016
1213
  isLocal: boolean,
1017
1214
  location: SourceLocation,
1018
1215
  ): Instance => {
1216
+ const start = cursor.index;
1019
1217
  expect("INSTANCE");
1020
1218
  const module = expectIdentifier();
1021
1219
  const substitutions = accept("WITH")
@@ -1025,7 +1223,16 @@ export const parseModule = (source: string): Module => {
1025
1223
  return { name: target, expression: parseArgument() };
1026
1224
  })
1027
1225
  : [];
1028
- return { kind: "Instance", name, params, module, substitutions, isLocal, location };
1226
+ return {
1227
+ kind: "Instance",
1228
+ name,
1229
+ params,
1230
+ module,
1231
+ substitutions,
1232
+ isLocal,
1233
+ location,
1234
+ instanceRange: getRange(start),
1235
+ };
1029
1236
  };
1030
1237
 
1031
1238
  const parseDeclaration = (): Declaration => {
@@ -1049,10 +1256,13 @@ export const parseModule = (source: string): Module => {
1049
1256
 
1050
1257
  const parseDeclarations = () => parseSeparated(parseDeclaration);
1051
1258
 
1052
- const declareRecursive = () =>
1053
- parseDeclarations().forEach((declaration) =>
1259
+ const declareRecursive = () => {
1260
+ const declarations = parseDeclarations();
1261
+ declarations.forEach((declaration) =>
1054
1262
  recursiveScopes[recursiveScopes.length - 1].add(declaration.name),
1055
1263
  );
1264
+ return declarations;
1265
+ };
1056
1266
 
1057
1267
  const getClosingOffset = (offset: number) => {
1058
1268
  const depth = { value: 0, offset };
@@ -1200,7 +1410,30 @@ export const parseModule = (source: string): Module => {
1200
1410
  module.variables.some((variable) => variable.name === name),
1201
1411
  );
1202
1412
 
1413
+ // tlapm's definable: names and operators joined by !.
1414
+ const parseDefinable = (): Fact => {
1415
+ const start = cursor.index;
1416
+ const token = peek();
1417
+ const parts = parseSeparated(() => {
1418
+ const part = peek();
1419
+ const isName = part.kind === "Identifier" && !isTlapmKeyword(part);
1420
+ if (!isName && part.kind !== "Operator") {
1421
+ throw createUnexpected(`Expected a definition name but found ${describe(part)}`, part);
1422
+ }
1423
+ cursor.index++;
1424
+ return part.text;
1425
+ }, "!");
1426
+ const location = getLocation(token);
1427
+ return withRange(
1428
+ parts.length === 1 && token.kind === "Operator"
1429
+ ? { kind: "OperatorReference", operator: token.text, location }
1430
+ : { kind: "Identifier", name: parts.join("!"), location },
1431
+ start,
1432
+ );
1433
+ };
1434
+
1203
1435
  const parseDefs = () => {
1436
+ if (isTlapm) return parseSeparated(parseDefinable);
1204
1437
  const defs = parseSeparated(parseFact);
1205
1438
  const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1206
1439
  if (invalid) {
@@ -1218,17 +1451,23 @@ export const parseModule = (source: string): Module => {
1218
1451
  const start = cursor.index;
1219
1452
  const keyword = advance();
1220
1453
  const isOnly = keyword.text === "USE" && accept("ONLY");
1454
+ const keywordRange = getRange(start);
1221
1455
  const use: UseOrHide = {
1222
1456
  kind: keyword.text === "USE" ? "Use" : "Hide",
1223
1457
  isOnly,
1224
1458
  ...parseFacts(),
1459
+ keyword: keywordRange,
1225
1460
  location: getLocation(keyword),
1226
1461
  };
1227
- const invalid = use.kind === "Hide" ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1228
- if (invalid) {
1229
- throw createError(
1230
- "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1231
- invalid.location,
1462
+ const invalid =
1463
+ use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1464
+ if (invalid.length > 0) {
1465
+ throw createSanyError(
1466
+ invalid.map((fact) => ({
1467
+ location: formatRange(fact.location.range ?? getRange(start)),
1468
+ message:
1469
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1470
+ })),
1232
1471
  );
1233
1472
  }
1234
1473
  return withRange(use, start);
@@ -1240,7 +1479,7 @@ export const parseModule = (source: string): Module => {
1240
1479
  const isNew = accept("NEW");
1241
1480
  const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
1242
1481
  if (!isNew && level === undefined) {
1243
- throw createError(`Expected a declaration but found ${describe(peek())}`);
1482
+ throw createUnexpected(`Expected a declaration but found ${describe(peek())}`);
1244
1483
  }
1245
1484
  if (level !== undefined) advance();
1246
1485
  if (level === "Variable") {
@@ -1347,7 +1586,7 @@ export const parseModule = (source: string): Module => {
1347
1586
  const parseStepStart = (isQed: boolean) => {
1348
1587
  const step = peek();
1349
1588
  if (step.kind !== "Step") {
1350
- throw createError(`Expected a step number but found ${describe(step)}`);
1589
+ throw createUnexpected(`Expected a step number but found ${describe(step)}`, step);
1351
1590
  }
1352
1591
  if (!isCorrectLevel(step)) {
1353
1592
  throw createError(`${isQed ? "QED step" : "step"}'s number has bad level`, step);
@@ -1356,7 +1595,7 @@ export const parseModule = (source: string): Module => {
1356
1595
  throw createError("<*> and <+> cannot be used for a named step", step);
1357
1596
  }
1358
1597
  cursor.index++;
1359
- return { name: getStepName(step), location: getLocation(step) };
1598
+ return { name: getStepName(step), token: step.text, location: getLocation(step) };
1360
1599
  };
1361
1600
 
1362
1601
  // The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
@@ -1364,9 +1603,12 @@ export const parseModule = (source: string): Module => {
1364
1603
  const parseStep = (): ProofStep => {
1365
1604
  const start = cursor.index;
1366
1605
  const step = parseStepStart(isAt("QED", 1));
1606
+ const statementStart = cursor.index;
1367
1607
  const scope = new Set<string>();
1368
1608
  proofScopes.push(scope);
1369
1609
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1610
+ const head = getRange(start);
1611
+ const statement = getRange(statementStart);
1370
1612
  const hasProof = isProofStart(peek());
1371
1613
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1372
1614
  throw createError("Proof of a step that does not take a proof");
@@ -1374,7 +1616,7 @@ export const parseModule = (source: string): Module => {
1374
1616
  const proof = hasProof ? parseProof() : undefined;
1375
1617
  proofScopes.pop();
1376
1618
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1377
- return withRange({ ...step, body, proof }, start);
1619
+ return withRange({ ...step, body, head, statement, proof }, start);
1378
1620
  };
1379
1621
 
1380
1622
  const parseProofBody = (): Proof => {
@@ -1382,7 +1624,8 @@ export const parseModule = (source: string): Module => {
1382
1624
  if (isAt("BY") || (isAt("PROOF") && isAt("BY", 1))) {
1383
1625
  accept("PROOF");
1384
1626
  advance();
1385
- return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), location };
1627
+ const keyword = getRange(cursor.index - 1);
1628
+ return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
1386
1629
  }
1387
1630
  const terminal = peek(isAt("PROOF") ? 1 : 0);
1388
1631
  if (
@@ -1391,7 +1634,8 @@ export const parseModule = (source: string): Module => {
1391
1634
  ) {
1392
1635
  accept("PROOF");
1393
1636
  advance();
1394
- return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", location };
1637
+ const keyword = getRange(cursor.index - 1);
1638
+ return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
1395
1639
  }
1396
1640
  accept("PROOF");
1397
1641
  const steps = [parseStep()];
@@ -1421,9 +1665,12 @@ export const parseModule = (source: string): Module => {
1421
1665
  return { statement, proof: isProofStart(peek()) ? parseProof() : undefined };
1422
1666
  });
1423
1667
  const statement = parsed.statement;
1424
- module.theorems.push(
1425
- withRange({ name, statement, proof: parsed.proof, location: getLocation(keyword) }, start),
1668
+ const theorem = withRange(
1669
+ { name, statement, proof: parsed.proof, location: getLocation(keyword) },
1670
+ start,
1426
1671
  );
1672
+ module.theorems.push(theorem);
1673
+ module.units.push({ kind: "Theorem", theorem });
1427
1674
  if (name === undefined || statement.kind === "AssumeProve") return;
1428
1675
  module.definitions.push(
1429
1676
  withRange(
@@ -1467,7 +1714,8 @@ export const parseModule = (source: string): Module => {
1467
1714
  const module: Module = {
1468
1715
  name,
1469
1716
  modules: [],
1470
- extends: accept("EXTENDS") ? parseSeparated(expectIdentifier) : [],
1717
+ extends: [],
1718
+ extendsRanges: [],
1471
1719
  constants: [],
1472
1720
  variables: [],
1473
1721
  definitions: [],
@@ -1476,20 +1724,39 @@ export const parseModule = (source: string): Module => {
1476
1724
  location,
1477
1725
  theorems: [],
1478
1726
  uses: [],
1727
+ units: [],
1479
1728
  };
1480
1729
  openModules.push(module);
1481
1730
  recursiveScopes.push(new Set());
1731
+ if (accept("EXTENDS")) {
1732
+ parseSeparated(() => {
1733
+ const start = cursor.index;
1734
+ module.extends.push(expectIdentifier());
1735
+ module.extendsRanges.push(getRange(start));
1736
+ });
1737
+ }
1482
1738
  while (true) {
1483
1739
  const tokenIndex = cursor.index;
1740
+ unitStart.index = tokenIndex;
1484
1741
  const token = peek();
1485
1742
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1743
+ if (token.kind === "ModuleEnd") {
1744
+ location.range = {
1745
+ module: name,
1746
+ start: { line: location.line, column: location.column },
1747
+ end: { line: token.line, column: token.endColumn },
1748
+ };
1749
+ }
1750
+ if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
1486
1751
  openModules.pop();
1487
1752
  recursiveScopes.pop();
1488
1753
  return module;
1489
1754
  }
1490
1755
  if (token.kind === "Separator") {
1491
1756
  if (isAt("MODULE", 1)) {
1492
- module.modules.push(parseModuleBody());
1757
+ const submodule = parseModuleBody();
1758
+ module.modules.push(submodule);
1759
+ module.units.push({ kind: "Module", module: submodule });
1493
1760
  expect("====");
1494
1761
  continue;
1495
1762
  }
@@ -1498,26 +1765,31 @@ export const parseModule = (source: string): Module => {
1498
1765
  }
1499
1766
  switch (token.text) {
1500
1767
  case "CONSTANT":
1501
- case "CONSTANTS":
1768
+ case "CONSTANTS": {
1502
1769
  advance();
1503
- module.constants.push(...parseDeclarations());
1770
+ const declarations = parseDeclarations();
1771
+ module.constants.push(...declarations);
1772
+ module.units.push({ kind: "Constants", declarations });
1504
1773
  continue;
1774
+ }
1505
1775
  case "VARIABLE":
1506
- case "VARIABLES":
1776
+ case "VARIABLES": {
1507
1777
  advance();
1508
- module.variables.push(
1509
- ...parseSeparated(() => {
1510
- const start = cursor.index;
1511
- const location = getLocation(peek());
1512
- return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1513
- }),
1514
- );
1778
+ const declarations = parseSeparated(() => {
1779
+ const start = cursor.index;
1780
+ const location = getLocation(peek());
1781
+ return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1782
+ });
1783
+ module.variables.push(...declarations);
1784
+ module.units.push({ kind: "Variables", declarations });
1515
1785
  continue;
1786
+ }
1516
1787
  case "ASSUME":
1517
1788
  case "ASSUMPTION":
1518
1789
  case "AXIOM": {
1519
1790
  const assumption = parseAssumption();
1520
1791
  module.assumptions.push(assumption);
1792
+ module.units.push({ kind: "Assumption", assumption });
1521
1793
  if (assumption.name !== undefined) {
1522
1794
  module.definitions.push({
1523
1795
  kind: "Operator",
@@ -1532,29 +1804,32 @@ export const parseModule = (source: string): Module => {
1532
1804
  continue;
1533
1805
  }
1534
1806
  case "USE":
1535
- case "HIDE":
1536
- module.uses.push(parseUseOrHide());
1807
+ case "HIDE": {
1808
+ const use = parseUseOrHide();
1809
+ module.uses.push(use);
1810
+ module.units.push({ kind: "Use", use });
1537
1811
  continue;
1812
+ }
1538
1813
  case "RECURSIVE":
1539
1814
  advance();
1540
- declareRecursive();
1815
+ module.units.push({ kind: "Recursive", declarations: declareRecursive() });
1541
1816
  continue;
1542
1817
  case "INSTANCE":
1543
- module.instances.push(
1818
+ addDefinitionUnit(
1819
+ module,
1544
1820
  withRange(parseInstance(undefined, [], false, getLocation(token)), tokenIndex),
1545
1821
  );
1546
1822
  continue;
1547
1823
  case "LOCAL": {
1548
1824
  advance();
1549
1825
  if (isAt("INSTANCE")) {
1550
- module.instances.push(
1826
+ addDefinitionUnit(
1827
+ module,
1551
1828
  withRange(parseInstance(undefined, [], true, getLocation(token)), tokenIndex),
1552
1829
  );
1553
1830
  continue;
1554
1831
  }
1555
- const unit = parseDefinition(true);
1556
- if (unit.kind === "Instance") module.instances.push(unit);
1557
- else module.definitions.push(unit);
1832
+ addDefinitionUnit(module, parseDefinition(true));
1558
1833
  continue;
1559
1834
  }
1560
1835
  }
@@ -1562,11 +1837,23 @@ export const parseModule = (source: string): Module => {
1562
1837
  parseTheorem(module);
1563
1838
  continue;
1564
1839
  }
1565
- const unit = parseDefinition(false);
1566
- if (unit.kind === "Instance") module.instances.push(unit);
1567
- else module.definitions.push(unit);
1840
+ addDefinitionUnit(module, parseDefinition(false));
1841
+ }
1842
+ };
1843
+
1844
+ const addDefinitionUnit = (module: Module, unit: Definition | Instance) => {
1845
+ if (unit.kind === "Instance") {
1846
+ module.instances.push(unit);
1847
+ module.units.push({ kind: "Instance", instance: unit });
1848
+ return;
1568
1849
  }
1850
+ module.definitions.push(unit);
1851
+ module.units.push({ kind: "Definition", definition: unit });
1569
1852
  };
1570
1853
 
1571
1854
  return parseModuleBody();
1572
1855
  };
1856
+
1857
+ export const parseModule = (source: string) => parse(source, false);
1858
+
1859
+ export const parseTlapmModule = (source: string) => parse(source, true);