tla-ts 0.0.2 → 0.0.3-dev.252cf06

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 (57) hide show
  1. package/README.md +73 -11
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +52 -1
  4. package/dist/index.d.ts +52 -1
  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 -2
  11. package/dist/run-cli.js +21 -2
  12. package/dist/transfer.cjs +19 -15
  13. package/dist/transfer.js +19 -15
  14. package/package.json +3 -1
  15. package/src/ast.ts +134 -4
  16. package/src/check.ts +120 -40
  17. package/src/cli-options.ts +58 -0
  18. package/src/codegen.ts +1 -3
  19. package/src/dependencies.ts +2 -2
  20. package/src/environment.ts +10 -10
  21. package/src/errors.ts +42 -1
  22. package/src/evaluate.ts +57 -114
  23. package/src/exit-codes.ts +20 -30
  24. package/src/fingerprints.ts +46 -11
  25. package/src/instances.ts +12 -11
  26. package/src/levels.ts +8 -8
  27. package/src/lexer.ts +11 -2
  28. package/src/live-check.ts +710 -0
  29. package/src/liveness.ts +5 -523
  30. package/src/obligation-action.ts +384 -0
  31. package/src/obligation-anon.ts +575 -0
  32. package/src/obligation-deref.ts +637 -0
  33. package/src/obligation-expression.ts +1060 -0
  34. package/src/obligation-format.ts +406 -0
  35. package/src/obligation-generate.ts +1074 -0
  36. package/src/obligation-levels.ts +329 -0
  37. package/src/obligation-module.ts +1042 -0
  38. package/src/obligation-printer.ts +889 -0
  39. package/src/obligation-proof.ts +76 -0
  40. package/src/obligation-source.ts +1084 -0
  41. package/src/obligation-tuples.ts +184 -0
  42. package/src/obligations.ts +316 -0
  43. package/src/parallel-worker.ts +166 -210
  44. package/src/parallel.ts +73 -35
  45. package/src/parser.ts +297 -53
  46. package/src/resolve.ts +16 -15
  47. package/src/run-cli.ts +304 -29
  48. package/src/runtime.ts +1 -1
  49. package/src/spec-description.ts +7 -2
  50. package/src/standard-levels.ts +3 -3
  51. package/src/subexpressions.ts +10 -10
  52. package/src/successors.ts +17 -9
  53. package/src/symmetry.ts +14 -2
  54. package/src/tlc-ext.ts +2 -10
  55. package/src/tlc-order.ts +17 -3
  56. package/src/transfer.ts +22 -1
  57. package/src/values.ts +59 -23
package/src/parser.ts CHANGED
@@ -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
+ };
434
+
435
+ const createUnexpected = (message: string, token: Token = peekRaw()) =>
436
+ createError(message, token, token);
329
437
 
330
- const isFieldName = (token: Token) => token.kind === "Identifier" || token.kind === "Keyword";
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(
@@ -524,6 +651,7 @@ export const parseModule = (source: string): Module => {
524
651
  return {
525
652
  kind: bullet.text === "/\\" ? "And" : "Or",
526
653
  operands,
654
+ isBulleted: true,
527
655
  location: getLocation(bullet),
528
656
  };
529
657
  };
@@ -561,7 +689,7 @@ export const parseModule = (source: string): Module => {
561
689
  while (isAt(".") || isAt("[")) {
562
690
  if (accept(".")) {
563
691
  if (!isFieldName(peek()))
564
- throw createError(`Expected a field name but found ${describe(peek())}`);
692
+ throw createUnexpected(`Expected a field name but found ${describe(peek())}`);
565
693
  path.push({ kind: "Field", name: advance().text });
566
694
  continue;
567
695
  }
@@ -637,7 +765,14 @@ export const parseModule = (source: string): Module => {
637
765
  }
638
766
  const names = parseSeparated(expectIdentifier);
639
767
  expect(":");
640
- return { kind: "UnboundedQuantifier", names, body: parseExpression(), isTemporal, location };
768
+ return {
769
+ kind: "UnboundedQuantifier",
770
+ quantifier: start.text === "\\A" || start.text === "\\AA" ? "Forall" : "Exists",
771
+ names,
772
+ body: parseExpression(),
773
+ isTemporal,
774
+ location,
775
+ };
641
776
  };
642
777
 
643
778
  const parseCase = (start: Token): Expression => {
@@ -691,7 +826,13 @@ export const parseModule = (source: string): Module => {
691
826
  const isFactStart = cursor.index === factStart.index;
692
827
  const token = advance();
693
828
  const location = getLocation(token);
829
+ if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
830
+ const params = parseSeparated(expectIdentifier);
831
+ expect(":");
832
+ return { kind: "Lambda", params, body: parseExpression(), location };
833
+ }
694
834
  if (token.kind === "Number") {
835
+ if (isTlapm && /^\\[BOH]/.test(token.text)) throw createError("incomplete expression", token);
695
836
  return { kind: "Number", value: getNumeralValue(token.text), text: token.text, location };
696
837
  }
697
838
  if (token.kind === "Decimal") return { kind: "Decimal", text: token.text, location };
@@ -721,7 +862,7 @@ export const parseModule = (source: string): Module => {
721
862
  const instance = openModules
722
863
  .flatMap((module) => module.instances)
723
864
  .find((candidate) => candidate.name === name);
724
- if (instance && !isFactStart && instance.params.length !== args.length) {
865
+ if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
725
866
  throw createError(
726
867
  `The operator ${name} requires ${instance.params.length} arguments`,
727
868
  token,
@@ -740,9 +881,13 @@ export const parseModule = (source: string): Module => {
740
881
  const qualifiedArgs = selectors.flatMap((selector) =>
741
882
  "args" in selector ? selector.args : [],
742
883
  );
743
- return qualifiedArgs.length > 0
744
- ? { kind: "Apply", operator, args: qualifiedArgs, location }
745
- : { kind: "Identifier", name: operator, location };
884
+ if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
885
+ const argumentCounts = selectors.map((selector) =>
886
+ "args" in selector ? selector.args.length : 0,
887
+ );
888
+ return argumentCounts.slice(0, -1).some((count) => count > 0)
889
+ ? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
890
+ : { kind: "Apply", operator, args: qualifiedArgs, location };
746
891
  }
747
892
  if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
748
893
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
@@ -779,6 +924,8 @@ export const parseModule = (source: string): Module => {
779
924
  const instances: Instance[] = [];
780
925
  recursiveScopes.push(new Set());
781
926
  while (!accept("IN")) {
927
+ if (isTlapm && isAt("RECURSIVE"))
928
+ throw createError("Unexpected keyword RECURSIVE", getDefinitionEnd(peekRaw()));
782
929
  if (accept("RECURSIVE")) {
783
930
  declareRecursive();
784
931
  continue;
@@ -816,7 +963,7 @@ export const parseModule = (source: string): Module => {
816
963
  };
817
964
  }
818
965
  }
819
- throw createError(`Unexpected ${describe(token)}`, token);
966
+ throw createUnexpected(`Unexpected ${describe(token)}`, token);
820
967
  };
821
968
 
822
969
  const parsePrefix = (): Expression => {
@@ -832,7 +979,12 @@ export const parseModule = (source: string): Module => {
832
979
  return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
833
980
  case "-":
834
981
  advance();
835
- return { kind: "Apply", operator: "-.", args: [parseExpression(13)], location };
982
+ return {
983
+ kind: "Apply",
984
+ operator: "-.",
985
+ args: [parseExpression(NEGATION_PRECEDENCE + 1)],
986
+ location,
987
+ };
836
988
  case "[]":
837
989
  advance();
838
990
  return { kind: "Always", expression: parseExpression(16), location };
@@ -909,6 +1061,10 @@ export const parseModule = (source: string): Module => {
909
1061
  }
910
1062
  }
911
1063
  advance();
1064
+ // SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
1065
+ if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1066
+ throw createError(`Precedence conflict between ${token.text} and -`, peek());
1067
+ }
912
1068
  const right = parseExpression(infix.high + 1);
913
1069
  const left = state.left;
914
1070
  if (token.text === "/\\" || token.text === "\\/") {
@@ -1004,9 +1160,13 @@ export const parseModule = (source: string): Module => {
1004
1160
  location,
1005
1161
  };
1006
1162
  }
1163
+ const definitionStart = cursor.index;
1007
1164
  const params = parseParams();
1008
1165
  expect("==");
1009
- if (isAt("INSTANCE")) return parseInstance(name, params, isLocal, location);
1166
+ if (isAt("INSTANCE")) {
1167
+ const instance = parseInstance(name, params, isLocal, location);
1168
+ return { ...instance, definitionRange: getRange(definitionStart) };
1169
+ }
1010
1170
  return parseOperator(name, params);
1011
1171
  };
1012
1172
 
@@ -1016,6 +1176,7 @@ export const parseModule = (source: string): Module => {
1016
1176
  isLocal: boolean,
1017
1177
  location: SourceLocation,
1018
1178
  ): Instance => {
1179
+ const start = cursor.index;
1019
1180
  expect("INSTANCE");
1020
1181
  const module = expectIdentifier();
1021
1182
  const substitutions = accept("WITH")
@@ -1025,7 +1186,16 @@ export const parseModule = (source: string): Module => {
1025
1186
  return { name: target, expression: parseArgument() };
1026
1187
  })
1027
1188
  : [];
1028
- return { kind: "Instance", name, params, module, substitutions, isLocal, location };
1189
+ return {
1190
+ kind: "Instance",
1191
+ name,
1192
+ params,
1193
+ module,
1194
+ substitutions,
1195
+ isLocal,
1196
+ location,
1197
+ instanceRange: getRange(start),
1198
+ };
1029
1199
  };
1030
1200
 
1031
1201
  const parseDeclaration = (): Declaration => {
@@ -1049,10 +1219,13 @@ export const parseModule = (source: string): Module => {
1049
1219
 
1050
1220
  const parseDeclarations = () => parseSeparated(parseDeclaration);
1051
1221
 
1052
- const declareRecursive = () =>
1053
- parseDeclarations().forEach((declaration) =>
1222
+ const declareRecursive = () => {
1223
+ const declarations = parseDeclarations();
1224
+ declarations.forEach((declaration) =>
1054
1225
  recursiveScopes[recursiveScopes.length - 1].add(declaration.name),
1055
1226
  );
1227
+ return declarations;
1228
+ };
1056
1229
 
1057
1230
  const getClosingOffset = (offset: number) => {
1058
1231
  const depth = { value: 0, offset };
@@ -1200,7 +1373,30 @@ export const parseModule = (source: string): Module => {
1200
1373
  module.variables.some((variable) => variable.name === name),
1201
1374
  );
1202
1375
 
1376
+ // tlapm's definable: names and operators joined by !.
1377
+ const parseDefinable = (): Fact => {
1378
+ const start = cursor.index;
1379
+ const token = peek();
1380
+ const parts = parseSeparated(() => {
1381
+ const part = peek();
1382
+ const isName = part.kind === "Identifier" && !isTlapmKeyword(part);
1383
+ if (!isName && part.kind !== "Operator") {
1384
+ throw createUnexpected(`Expected a definition name but found ${describe(part)}`, part);
1385
+ }
1386
+ cursor.index++;
1387
+ return part.text;
1388
+ }, "!");
1389
+ const location = getLocation(token);
1390
+ return withRange(
1391
+ parts.length === 1 && token.kind === "Operator"
1392
+ ? { kind: "OperatorReference", operator: token.text, location }
1393
+ : { kind: "Identifier", name: parts.join("!"), location },
1394
+ start,
1395
+ );
1396
+ };
1397
+
1203
1398
  const parseDefs = () => {
1399
+ if (isTlapm) return parseSeparated(parseDefinable);
1204
1400
  const defs = parseSeparated(parseFact);
1205
1401
  const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1206
1402
  if (invalid) {
@@ -1218,13 +1414,16 @@ export const parseModule = (source: string): Module => {
1218
1414
  const start = cursor.index;
1219
1415
  const keyword = advance();
1220
1416
  const isOnly = keyword.text === "USE" && accept("ONLY");
1417
+ const keywordRange = getRange(start);
1221
1418
  const use: UseOrHide = {
1222
1419
  kind: keyword.text === "USE" ? "Use" : "Hide",
1223
1420
  isOnly,
1224
1421
  ...parseFacts(),
1422
+ keyword: keywordRange,
1225
1423
  location: getLocation(keyword),
1226
1424
  };
1227
- const invalid = use.kind === "Hide" ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1425
+ const invalid =
1426
+ use.kind === "Hide" && !isTlapm ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1228
1427
  if (invalid) {
1229
1428
  throw createError(
1230
1429
  "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
@@ -1240,7 +1439,7 @@ export const parseModule = (source: string): Module => {
1240
1439
  const isNew = accept("NEW");
1241
1440
  const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
1242
1441
  if (!isNew && level === undefined) {
1243
- throw createError(`Expected a declaration but found ${describe(peek())}`);
1442
+ throw createUnexpected(`Expected a declaration but found ${describe(peek())}`);
1244
1443
  }
1245
1444
  if (level !== undefined) advance();
1246
1445
  if (level === "Variable") {
@@ -1347,7 +1546,7 @@ export const parseModule = (source: string): Module => {
1347
1546
  const parseStepStart = (isQed: boolean) => {
1348
1547
  const step = peek();
1349
1548
  if (step.kind !== "Step") {
1350
- throw createError(`Expected a step number but found ${describe(step)}`);
1549
+ throw createUnexpected(`Expected a step number but found ${describe(step)}`, step);
1351
1550
  }
1352
1551
  if (!isCorrectLevel(step)) {
1353
1552
  throw createError(`${isQed ? "QED step" : "step"}'s number has bad level`, step);
@@ -1356,7 +1555,7 @@ export const parseModule = (source: string): Module => {
1356
1555
  throw createError("<*> and <+> cannot be used for a named step", step);
1357
1556
  }
1358
1557
  cursor.index++;
1359
- return { name: getStepName(step), location: getLocation(step) };
1558
+ return { name: getStepName(step), token: step.text, location: getLocation(step) };
1360
1559
  };
1361
1560
 
1362
1561
  // The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
@@ -1367,6 +1566,7 @@ export const parseModule = (source: string): Module => {
1367
1566
  const scope = new Set<string>();
1368
1567
  proofScopes.push(scope);
1369
1568
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
+ const head = getRange(start);
1370
1570
  const hasProof = isProofStart(peek());
1371
1571
  if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1372
1572
  throw createError("Proof of a step that does not take a proof");
@@ -1374,7 +1574,7 @@ export const parseModule = (source: string): Module => {
1374
1574
  const proof = hasProof ? parseProof() : undefined;
1375
1575
  proofScopes.pop();
1376
1576
  if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1377
- return withRange({ ...step, body, proof }, start);
1577
+ return withRange({ ...step, body, head, proof }, start);
1378
1578
  };
1379
1579
 
1380
1580
  const parseProofBody = (): Proof => {
@@ -1382,7 +1582,8 @@ export const parseModule = (source: string): Module => {
1382
1582
  if (isAt("BY") || (isAt("PROOF") && isAt("BY", 1))) {
1383
1583
  accept("PROOF");
1384
1584
  advance();
1385
- return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), location };
1585
+ const keyword = getRange(cursor.index - 1);
1586
+ return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
1386
1587
  }
1387
1588
  const terminal = peek(isAt("PROOF") ? 1 : 0);
1388
1589
  if (
@@ -1391,7 +1592,8 @@ export const parseModule = (source: string): Module => {
1391
1592
  ) {
1392
1593
  accept("PROOF");
1393
1594
  advance();
1394
- return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", location };
1595
+ const keyword = getRange(cursor.index - 1);
1596
+ return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
1395
1597
  }
1396
1598
  accept("PROOF");
1397
1599
  const steps = [parseStep()];
@@ -1421,9 +1623,12 @@ export const parseModule = (source: string): Module => {
1421
1623
  return { statement, proof: isProofStart(peek()) ? parseProof() : undefined };
1422
1624
  });
1423
1625
  const statement = parsed.statement;
1424
- module.theorems.push(
1425
- withRange({ name, statement, proof: parsed.proof, location: getLocation(keyword) }, start),
1626
+ const theorem = withRange(
1627
+ { name, statement, proof: parsed.proof, location: getLocation(keyword) },
1628
+ start,
1426
1629
  );
1630
+ module.theorems.push(theorem);
1631
+ module.units.push({ kind: "Theorem", theorem });
1427
1632
  if (name === undefined || statement.kind === "AssumeProve") return;
1428
1633
  module.definitions.push(
1429
1634
  withRange(
@@ -1467,7 +1672,8 @@ export const parseModule = (source: string): Module => {
1467
1672
  const module: Module = {
1468
1673
  name,
1469
1674
  modules: [],
1470
- extends: accept("EXTENDS") ? parseSeparated(expectIdentifier) : [],
1675
+ extends: [],
1676
+ extendsRanges: [],
1471
1677
  constants: [],
1472
1678
  variables: [],
1473
1679
  definitions: [],
@@ -1476,20 +1682,38 @@ export const parseModule = (source: string): Module => {
1476
1682
  location,
1477
1683
  theorems: [],
1478
1684
  uses: [],
1685
+ units: [],
1479
1686
  };
1480
1687
  openModules.push(module);
1481
1688
  recursiveScopes.push(new Set());
1689
+ if (accept("EXTENDS")) {
1690
+ parseSeparated(() => {
1691
+ const start = cursor.index;
1692
+ module.extends.push(expectIdentifier());
1693
+ module.extendsRanges.push(getRange(start));
1694
+ });
1695
+ }
1482
1696
  while (true) {
1483
1697
  const tokenIndex = cursor.index;
1698
+ unitStart.index = tokenIndex;
1484
1699
  const token = peek();
1485
1700
  if (token.kind === "ModuleEnd" || token.kind === "End") {
1701
+ if (token.kind === "ModuleEnd") {
1702
+ location.range = {
1703
+ module: name,
1704
+ start: { line: location.line, column: location.column },
1705
+ end: { line: token.line, column: token.endColumn },
1706
+ };
1707
+ }
1486
1708
  openModules.pop();
1487
1709
  recursiveScopes.pop();
1488
1710
  return module;
1489
1711
  }
1490
1712
  if (token.kind === "Separator") {
1491
1713
  if (isAt("MODULE", 1)) {
1492
- module.modules.push(parseModuleBody());
1714
+ const submodule = parseModuleBody();
1715
+ module.modules.push(submodule);
1716
+ module.units.push({ kind: "Module", module: submodule });
1493
1717
  expect("====");
1494
1718
  continue;
1495
1719
  }
@@ -1498,26 +1722,31 @@ export const parseModule = (source: string): Module => {
1498
1722
  }
1499
1723
  switch (token.text) {
1500
1724
  case "CONSTANT":
1501
- case "CONSTANTS":
1725
+ case "CONSTANTS": {
1502
1726
  advance();
1503
- module.constants.push(...parseDeclarations());
1727
+ const declarations = parseDeclarations();
1728
+ module.constants.push(...declarations);
1729
+ module.units.push({ kind: "Constants", declarations });
1504
1730
  continue;
1731
+ }
1505
1732
  case "VARIABLE":
1506
- case "VARIABLES":
1733
+ case "VARIABLES": {
1507
1734
  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
- );
1735
+ const declarations = parseSeparated(() => {
1736
+ const start = cursor.index;
1737
+ const location = getLocation(peek());
1738
+ return withRange({ name: expectIdentifier(), arity: 0, location }, start);
1739
+ });
1740
+ module.variables.push(...declarations);
1741
+ module.units.push({ kind: "Variables", declarations });
1515
1742
  continue;
1743
+ }
1516
1744
  case "ASSUME":
1517
1745
  case "ASSUMPTION":
1518
1746
  case "AXIOM": {
1519
1747
  const assumption = parseAssumption();
1520
1748
  module.assumptions.push(assumption);
1749
+ module.units.push({ kind: "Assumption", assumption });
1521
1750
  if (assumption.name !== undefined) {
1522
1751
  module.definitions.push({
1523
1752
  kind: "Operator",
@@ -1532,29 +1761,32 @@ export const parseModule = (source: string): Module => {
1532
1761
  continue;
1533
1762
  }
1534
1763
  case "USE":
1535
- case "HIDE":
1536
- module.uses.push(parseUseOrHide());
1764
+ case "HIDE": {
1765
+ const use = parseUseOrHide();
1766
+ module.uses.push(use);
1767
+ module.units.push({ kind: "Use", use });
1537
1768
  continue;
1769
+ }
1538
1770
  case "RECURSIVE":
1539
1771
  advance();
1540
- declareRecursive();
1772
+ module.units.push({ kind: "Recursive", declarations: declareRecursive() });
1541
1773
  continue;
1542
1774
  case "INSTANCE":
1543
- module.instances.push(
1775
+ addDefinitionUnit(
1776
+ module,
1544
1777
  withRange(parseInstance(undefined, [], false, getLocation(token)), tokenIndex),
1545
1778
  );
1546
1779
  continue;
1547
1780
  case "LOCAL": {
1548
1781
  advance();
1549
1782
  if (isAt("INSTANCE")) {
1550
- module.instances.push(
1783
+ addDefinitionUnit(
1784
+ module,
1551
1785
  withRange(parseInstance(undefined, [], true, getLocation(token)), tokenIndex),
1552
1786
  );
1553
1787
  continue;
1554
1788
  }
1555
- const unit = parseDefinition(true);
1556
- if (unit.kind === "Instance") module.instances.push(unit);
1557
- else module.definitions.push(unit);
1789
+ addDefinitionUnit(module, parseDefinition(true));
1558
1790
  continue;
1559
1791
  }
1560
1792
  }
@@ -1562,11 +1794,23 @@ export const parseModule = (source: string): Module => {
1562
1794
  parseTheorem(module);
1563
1795
  continue;
1564
1796
  }
1565
- const unit = parseDefinition(false);
1566
- if (unit.kind === "Instance") module.instances.push(unit);
1567
- else module.definitions.push(unit);
1797
+ addDefinitionUnit(module, parseDefinition(false));
1798
+ }
1799
+ };
1800
+
1801
+ const addDefinitionUnit = (module: Module, unit: Definition | Instance) => {
1802
+ if (unit.kind === "Instance") {
1803
+ module.instances.push(unit);
1804
+ module.units.push({ kind: "Instance", instance: unit });
1805
+ return;
1568
1806
  }
1807
+ module.definitions.push(unit);
1808
+ module.units.push({ kind: "Definition", definition: unit });
1569
1809
  };
1570
1810
 
1571
1811
  return parseModuleBody();
1572
1812
  };
1813
+
1814
+ export const parseModule = (source: string) => parse(source, false);
1815
+
1816
+ export const parseTlapmModule = (source: string) => parse(source, true);