tla-ts 0.0.1 → 0.0.3-dev.2032e26

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 (71) hide show
  1. package/README.md +95 -40
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +216 -30
  4. package/dist/index.d.ts +216 -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 +20 -1
  11. package/dist/run-cli.js +20 -1
  12. package/dist/transfer.cjs +392 -39
  13. package/dist/transfer.js +392 -39
  14. package/package.json +6 -2
  15. package/src/ast.ts +122 -5
  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 +950 -238
  20. package/src/cli-options.ts +58 -0
  21. package/src/codegen.ts +52 -18
  22. package/src/config.ts +39 -11
  23. package/src/environment.ts +485 -140
  24. package/src/errors.ts +64 -1
  25. package/src/evaluate.ts +492 -138
  26. package/src/exit-codes.ts +75 -0
  27. package/src/fingerprints.ts +48 -13
  28. package/src/index.ts +9 -4
  29. package/src/instances.ts +221 -66
  30. package/src/java-overrides.ts +241 -0
  31. package/src/json.ts +153 -72
  32. package/src/levels.ts +850 -0
  33. package/src/lexer.ts +108 -29
  34. package/src/liveness.ts +47 -91
  35. package/src/obligation-action.ts +384 -0
  36. package/src/obligation-anon.ts +575 -0
  37. package/src/obligation-deref.ts +637 -0
  38. package/src/obligation-expression.ts +1060 -0
  39. package/src/obligation-format.ts +406 -0
  40. package/src/obligation-generate.ts +1074 -0
  41. package/src/obligation-levels.ts +329 -0
  42. package/src/obligation-module.ts +1042 -0
  43. package/src/obligation-printer.ts +889 -0
  44. package/src/obligation-proof.ts +76 -0
  45. package/src/obligation-source.ts +1084 -0
  46. package/src/obligation-tuples.ts +184 -0
  47. package/src/obligations.ts +316 -0
  48. package/src/parallel-worker.ts +186 -201
  49. package/src/parallel.ts +176 -84
  50. package/src/parser.ts +970 -206
  51. package/src/pretty-print.ts +259 -0
  52. package/src/randomization.ts +24 -113
  53. package/src/resolve.ts +472 -41
  54. package/src/run-cli.ts +427 -61
  55. package/src/runtime.ts +63 -28
  56. package/src/spec-description.ts +509 -0
  57. package/src/standard-levels.ts +105 -0
  58. package/src/standard-modules.ts +223 -92
  59. package/src/subexpressions.ts +21 -35
  60. package/src/substitution-levels.ts +184 -0
  61. package/src/successors.ts +266 -48
  62. package/src/symmetry.ts +79 -23
  63. package/src/tableau.ts +43 -37
  64. package/src/tlc-ext.ts +120 -33
  65. package/src/tlc-module.ts +71 -47
  66. package/src/tlc-modules.ts +1022 -0
  67. package/src/tlc-order.ts +511 -0
  68. package/src/tlc-random.ts +411 -0
  69. package/src/tlc-summary.ts +109 -0
  70. package/src/transfer.ts +26 -5
  71. package/src/values.ts +1234 -176
package/src/parser.ts CHANGED
@@ -1,24 +1,29 @@
1
1
  import type {
2
+ AssumeProve,
2
3
  Assumption,
3
4
  Bound,
4
5
  BoundPattern,
5
6
  CaseArm,
6
7
  Declaration,
8
+ DefineStep,
7
9
  Definition,
8
10
  ExceptPathStep,
9
- ExceptUpdate,
10
11
  Expression,
12
+ Fact,
11
13
  Module,
12
14
  Instance,
13
- OuterScope,
14
- InstanceSubstitution,
15
+ NewSymbol,
15
16
  OperatorParam,
16
- RecordField,
17
+ Proof,
18
+ ProofStep,
17
19
  Selector,
18
20
  SourceLocation,
21
+ SourceRange,
22
+ StepBody,
23
+ UseOrHide,
19
24
  } from "./ast";
20
25
  import { TlaSyntaxError } from "./errors";
21
- import { tokenize, type Token } from "./lexer";
26
+ import { getNumeralValue, tokenize, type Token } from "./lexer";
22
27
 
23
28
  interface InfixOperator {
24
29
  low: number;
@@ -26,7 +31,7 @@ interface InfixOperator {
26
31
  isAssociative: boolean;
27
32
  }
28
33
 
29
- const createInfix = (low: number, high: number, isAssociative: boolean): InfixOperator => ({
34
+ const createInfix = (low: number, high: number, isAssociative: boolean) => ({
30
35
  low,
31
36
  high,
32
37
  isAssociative,
@@ -130,10 +135,28 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
130
135
  ["^^", createInfix(14, 14, false)],
131
136
  ]);
132
137
 
138
+ const NEGATION_PRECEDENCE = 12;
139
+
133
140
  const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
134
141
 
135
142
  const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
136
143
 
144
+ const EXPRESSION_PREFIXES = new Set([
145
+ "-",
146
+ "~",
147
+ "/\\",
148
+ "\\/",
149
+ "SUBSET",
150
+ "UNION",
151
+ "DOMAIN",
152
+ "ENABLED",
153
+ "UNCHANGED",
154
+ "[]",
155
+ "<>",
156
+ ]);
157
+
158
+ const CIRCLED_OPERATORS = new Set(["+", "-", "/", "\\X"]);
159
+
137
160
  const OPERATOR_ARGUMENTS = new Set([
138
161
  "SUBSET",
139
162
  "UNION",
@@ -146,6 +169,9 @@ const OPERATOR_ARGUMENTS = new Set([
146
169
  ...POSTFIX_OPERATORS,
147
170
  ]);
148
171
 
172
+ const isOperatorName = (text: string) =>
173
+ INFIX_OPERATORS.has(text) || POSTFIX_OPERATORS.has(text) || text === "-.";
174
+
149
175
  const getOperatorApplication = (
150
176
  operator: string,
151
177
  argument: Expression,
@@ -187,27 +213,148 @@ const UNIT_KEYWORDS = new Set([
187
213
 
188
214
  const THEOREM_KEYWORDS = new Set(["THEOREM", "LEMMA", "COROLLARY", "PROPOSITION"]);
189
215
 
190
- const PROOF_WORDS = new Set(["BY", "OBVIOUS", "OMITTED", "QED", "USE", "HIDE"]);
216
+ const PROOF_STARTS = new Set(["BY", "PROOF", "OBVIOUS", "OMITTED"]);
217
+
218
+ const PROVABLE_STEPS = new Set(["Qed", "Pick", "Case", "Assert"]);
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
+
255
+ const UNNAMED_STEP_LABELS = new Set(["", "*", "-"]);
256
+
257
+ const FACT_KEYWORDS = new Set([
258
+ "CASE",
259
+ "CHOOSE",
260
+ "DOMAIN",
261
+ "ENABLED",
262
+ "IF",
263
+ "LAMBDA",
264
+ "LET",
265
+ "SF_",
266
+ "SUBSET",
267
+ "UNCHANGED",
268
+ "UNION",
269
+ "WF_",
270
+ ]);
271
+
272
+ const NEW_SYMBOL_LEVELS = new Map<string, NewSymbol["level"]>([
273
+ ["CONSTANT", "Constant"],
274
+ ["CONSTANTS", "Constant"],
275
+ ["VARIABLE", "Variable"],
276
+ ["VARIABLES", "Variable"],
277
+ ["STATE", "State"],
278
+ ["ACTION", "Action"],
279
+ ["ACTIONS", "Action"],
280
+ ["TEMPORAL", "Temporal"],
281
+ ["TEMPORALS", "Temporal"],
282
+ ]);
283
+
284
+ const NEW_SYMBOL_STARTS = new Set(["NEW", ...NEW_SYMBOL_LEVELS.keys()]);
285
+
286
+ const OPENING_BRACKETS = new Set(["(", "[", "{", "<<"]);
287
+
288
+ const CLOSING_BRACKETS = new Set([")", "]", "}", ">>", "]_", ">>_"]);
289
+
290
+ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
191
291
 
192
292
  const NO_OFFSIDE = -1;
193
293
 
194
- export const parseModule = (source: string): Module => {
294
+ const getModuleHeader = (name: string) => new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
295
+
296
+ // The text of a module that starts on its own line in a file, as TLC's monolith extractor reads
297
+ // modules that follow the root module.
298
+ export const extractModule = (source: string, name: string) => {
299
+ const lines = source.split(/\r?\n/);
300
+ const start = lines.findIndex((line) => getModuleHeader(name).test(line));
301
+ if (start === -1) return undefined;
302
+ const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
303
+ return lines.slice(start, end === -1 ? undefined : end + 1).join("\n");
304
+ };
305
+
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 => {
195
309
  const tokens = tokenize(source);
196
- const cursor = { index: 0, limit: tokens.length, offsideColumns: [] as number[] };
197
- const recursiveNames = new Set<string>();
310
+ const cursor = { index: 0, offsideColumns: [] as number[] };
311
+ // A RECURSIVE declaration covers the definitions of its own LET or module.
312
+ const recursiveScopes: Set<string>[] = [];
313
+ const factNames = new Set<string>();
314
+ // The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
315
+ const proofScopes: Set<string>[] = [];
316
+ // A fact may cite an operator of a parameterized instance without the instance's arguments.
317
+ const factStart = { index: -1 };
318
+ const openModules: Module[] = [];
319
+
320
+ const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
198
321
 
199
322
  const getLocation = (token: Token): SourceLocation => ({
200
323
  line: token.line,
201
- column: token.column,
324
+ column: isTlapm && token.kind === "String" ? token.endColumn : token.column,
325
+ range: undefined,
202
326
  });
203
327
 
328
+ const tokenStarts: SourceLocation[] = [];
329
+ const tokenEnds: SourceLocation[] = [];
330
+ const tokenRanges: SourceRange[] = [];
331
+
332
+ const getRange = (startIndex: number) => {
333
+ const endIndex = cursor.index - 1;
334
+ const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
335
+ if (cached) return cached;
336
+ const last = tokens[endIndex];
337
+ tokenStarts[startIndex] ??= getLocation(tokens[startIndex]);
338
+ tokenEnds[endIndex] ??= { line: last.line, column: last.endColumn };
339
+ const range = {
340
+ module: openModules[openModules.length - 1].name,
341
+ start: tokenStarts[startIndex],
342
+ end: tokenEnds[endIndex],
343
+ };
344
+ if (startIndex === endIndex) tokenRanges[startIndex] = range;
345
+ return range;
346
+ };
347
+
348
+ // Each node gets its own location object from getLocation, so the range can be set in place.
349
+ const withRange = <Node extends { location: SourceLocation }>(node: Node, startIndex: number) => {
350
+ node.location.range = getRange(startIndex);
351
+ return node;
352
+ };
353
+
204
354
  const peekRaw = (offset = 0) => tokens[Math.min(cursor.index + offset, tokens.length - 1)];
205
355
 
206
356
  const peek = (offset = 0) => {
207
- const token =
208
- cursor.index + offset >= cursor.limit
209
- ? { ...peekRaw(offset), kind: "End" as const, text: "" }
210
- : peekRaw(offset);
357
+ const token = peekRaw(offset);
211
358
  const offsideColumn = cursor.offsideColumns[cursor.offsideColumns.length - 1];
212
359
  if (offsideColumn === undefined || offsideColumn === NO_OFFSIDE || token.kind === "End") {
213
360
  return token;
@@ -216,12 +363,80 @@ export const parseModule = (source: string): Module => {
216
363
  };
217
364
 
218
365
  const describe = (token: Token) =>
219
- token.kind === "End" ? "end of input" : `${JSON.stringify(token.text)}`;
366
+ token.kind === "End" ? "end of input" : JSON.stringify(token.text);
367
+
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
+ };
220
390
 
221
- const createError = (message: string, token = peekRaw()) =>
222
- new TlaSyntaxError(`${message} at line ${token.line}, column ${token.column}`);
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
+ };
223
400
 
224
- const isFieldName = (token: Token) => token.kind === "Identifier" || token.kind === "Keyword";
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);
437
+
438
+ const isFieldName = (token: Token) =>
439
+ (token.kind === "Identifier" || token.kind === "Keyword") && !isTlapmKeyword(token);
225
440
 
226
441
  const isAt = (text: string, offset = 0) => {
227
442
  const token = peek(offset);
@@ -243,15 +458,16 @@ export const parseModule = (source: string): Module => {
243
458
 
244
459
  const expect = (text: string) => {
245
460
  if (!isAt(text))
246
- throw createError(`Expected ${JSON.stringify(text)} but found ${describe(peek())}`);
461
+ throw createUnexpected(`Expected ${JSON.stringify(text)} but found ${describe(peek())}`);
247
462
  return advance();
248
463
  };
249
464
 
250
465
  const expectIdentifier = () => {
251
466
  const token = peek();
252
467
  if (token.kind !== "Identifier") {
253
- throw createError(`Expected an identifier but found ${describe(token)}`);
468
+ throw createUnexpected(`Expected an identifier but found ${describe(token)}`);
254
469
  }
470
+ if (isTlapmKeyword(token)) throw createUnexpected(`Unexpected keyword ${token.text}`);
255
471
  cursor.index++;
256
472
  return token.text;
257
473
  };
@@ -304,7 +520,7 @@ export const parseModule = (source: string): Module => {
304
520
  return { kind: "Tuple", names };
305
521
  };
306
522
 
307
- const parseBounds = (): Bound[] =>
523
+ const parseBounds = () =>
308
524
  parseSeparated(() => {
309
525
  const patterns = parseSeparated(parsePattern);
310
526
  expect("\\in");
@@ -315,26 +531,81 @@ export const parseModule = (source: string): Module => {
315
531
  const parseSubscript = (): Expression => {
316
532
  const token = peek();
317
533
  if (token.kind === "Identifier") {
318
- cursor.index++;
319
- return { kind: "Identifier", name: token.text, location: getLocation(token) };
534
+ const start = cursor.index;
535
+ const names = [advance().text];
536
+ while (isAt("!") && peek(1).kind === "Identifier") {
537
+ advance();
538
+ names.push(expectIdentifier());
539
+ }
540
+ return withRange(
541
+ { kind: "Identifier", name: names.join("!"), location: getLocation(token) },
542
+ start,
543
+ );
320
544
  }
321
545
  if (isAt("<<") || isAt("(")) return parsePrimary();
322
- throw createError(`Expected a subscript but found ${describe(token)}`);
546
+ throw createUnexpected(`Expected a subscript but found ${describe(token)}`);
547
+ };
548
+
549
+ const isDefinitionAhead = (operator: Token) => {
550
+ const token = peek(1);
551
+ const next = peek(2);
552
+ if (token.kind === "Identifier" && next.text === "==") return true;
553
+ if (token.line === operator.line) return false;
554
+ if (token.text === "-.") return true;
555
+ return (
556
+ token.kind === "Identifier" &&
557
+ (next.text === "(" ||
558
+ next.text === "[" ||
559
+ (next.kind === "Operator" && isAt("==", POSTFIX_OPERATORS.has(next.text) ? 3 : 4)))
560
+ );
561
+ };
562
+
563
+ const isStandalone = (operator: Token) => {
564
+ if (!EXPRESSION_PREFIXES.has(operator.text)) return true;
565
+ const token = peek(1);
566
+ return (
567
+ token.kind === "End" ||
568
+ token.kind === "ModuleEnd" ||
569
+ token.kind === "Separator" ||
570
+ isAt(",", 1) ||
571
+ isAt(")", 1) ||
572
+ (token.kind === "Keyword" && UNIT_KEYWORDS.has(token.text)) ||
573
+ isDefinitionAhead(operator)
574
+ );
323
575
  };
324
576
 
325
577
  const parseArgument = (): Expression => {
326
578
  const token = peek();
327
- const next = peek(1);
328
579
  const isOperatorAlone =
329
580
  token.kind === "Operator" &&
330
581
  (INFIX_OPERATORS.has(token.text) || PREFIX_OPERATOR_REFERENCES.has(token.text)) &&
331
- (next.text === "," || next.text === ")");
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);
332
600
  if (isOperatorAlone) {
333
601
  cursor.index++;
334
- return { kind: "OperatorReference", operator: token.text, location: getLocation(token) };
602
+ return withRange(
603
+ { kind: "OperatorReference", operator: token.text, location: getLocation(token) },
604
+ cursor.index - 1,
605
+ );
335
606
  }
336
607
  const isPrefixAlone =
337
- OPERATOR_ARGUMENTS.has(token.text) && (next.text === "," || next.text === ")");
608
+ token.kind !== "String" && OPERATOR_ARGUMENTS.has(token.text) && isStandalone(token);
338
609
  if (isPrefixAlone) {
339
610
  cursor.index++;
340
611
  const location = getLocation(token);
@@ -350,10 +621,12 @@ export const parseModule = (source: string): Module => {
350
621
  };
351
622
  }
352
623
  if (isAt("LAMBDA")) {
624
+ const start = cursor.index;
353
625
  advance();
354
626
  const params = parseSeparated(expectIdentifier);
355
627
  expect(":");
356
- return { kind: "Lambda", params, body: parseExpression(), location: getLocation(token) };
628
+ const body = parseExpression();
629
+ return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
357
630
  }
358
631
  return parseExpression();
359
632
  };
@@ -378,6 +651,7 @@ export const parseModule = (source: string): Module => {
378
651
  return {
379
652
  kind: bullet.text === "/\\" ? "And" : "Or",
380
653
  operands,
654
+ isBulleted: true,
381
655
  location: getLocation(bullet),
382
656
  };
383
657
  };
@@ -387,7 +661,7 @@ export const parseModule = (source: string): Module => {
387
661
  const location = getLocation(start);
388
662
  if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
389
663
  const kind = isAt("|->", 1) ? "Record" : "RecordSet";
390
- const fields = parseSeparated((): RecordField => {
664
+ const fields = parseSeparated(() => {
391
665
  const name = expectIdentifier();
392
666
  expect(kind === "Record" ? "|->" : ":");
393
667
  return { name, value: parseExpression() };
@@ -409,13 +683,13 @@ export const parseModule = (source: string): Module => {
409
683
  return { kind: "FunctionSet", domain: expression, range, location };
410
684
  }
411
685
  if (accept("EXCEPT")) {
412
- const updates = parseSeparated((): ExceptUpdate => {
686
+ const updates = parseSeparated(() => {
413
687
  expect("!");
414
688
  const path: ExceptPathStep[] = [];
415
689
  while (isAt(".") || isAt("[")) {
416
690
  if (accept(".")) {
417
691
  if (!isFieldName(peek()))
418
- throw createError(`Expected a field name but found ${describe(peek())}`);
692
+ throw createUnexpected(`Expected a field name but found ${describe(peek())}`);
419
693
  path.push({ kind: "Field", name: advance().text });
420
694
  continue;
421
695
  }
@@ -468,10 +742,9 @@ export const parseModule = (source: string): Module => {
468
742
 
469
743
  const parseTuple = (start: Token): Expression => {
470
744
  const location = getLocation(start);
471
- const elements = withoutOffside(() => {
472
- if (isAt(">>") || isAt(">>_")) return [];
473
- return parseSeparated(() => parseExpression());
474
- });
745
+ const elements = withoutOffside(() =>
746
+ isAt(">>") || isAt(">>_") ? [] : parseSeparated(() => parseExpression()),
747
+ );
475
748
  if (accept(">>_")) {
476
749
  if (elements.length !== 1)
477
750
  throw createError("Expected a single action inside <<A>>_v", start);
@@ -492,23 +765,28 @@ export const parseModule = (source: string): Module => {
492
765
  }
493
766
  const names = parseSeparated(expectIdentifier);
494
767
  expect(":");
495
- 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
+ };
496
776
  };
497
777
 
498
778
  const parseCase = (start: Token): Expression => {
499
779
  const arms: CaseArm[] = [];
500
- const other = { value: undefined as Expression | undefined };
501
780
  do {
502
781
  if (accept("OTHER")) {
503
782
  expect("->");
504
- other.value = parseExpression();
505
- break;
783
+ return { kind: "Case", arms, other: parseExpression(), location: getLocation(start) };
506
784
  }
507
785
  const condition = parseExpression();
508
786
  expect("->");
509
787
  arms.push({ condition, body: parseExpression() });
510
788
  } while (accept("[]"));
511
- return { kind: "Case", arms, other: other.value, location: getLocation(start) };
789
+ return { kind: "Case", arms, other: undefined, location: getLocation(start) };
512
790
  };
513
791
 
514
792
  const isSelectorAhead = () => {
@@ -527,7 +805,7 @@ export const parseModule = (source: string): Module => {
527
805
 
528
806
  const parseSelector = (): Selector => {
529
807
  const token = advance();
530
- if (token.kind === "Number") return { kind: "Operand", operand: Number(token.text) };
808
+ if (token.kind === "Number") return { kind: "Operand", operand: getNumeralValue(token.text) };
531
809
  switch (token.text) {
532
810
  case "<<":
533
811
  case ">>":
@@ -545,25 +823,52 @@ export const parseModule = (source: string): Module => {
545
823
  };
546
824
 
547
825
  const parsePrimary = (): Expression => {
826
+ const isFactStart = cursor.index === factStart.index;
548
827
  const token = advance();
549
828
  const location = getLocation(token);
550
- if (token.kind === "Number") return { kind: "Number", value: Number(token.text), location };
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
+ }
834
+ if (token.kind === "Number") {
835
+ if (isTlapm && /^\\[BOH]/.test(token.text)) throw createError("incomplete expression", token);
836
+ return { kind: "Number", value: getNumeralValue(token.text), text: token.text, location };
837
+ }
838
+ if (token.kind === "Decimal") return { kind: "Decimal", text: token.text, location };
551
839
  if (token.kind === "String") return { kind: "String", value: token.text, location };
552
- if (token.kind === "Identifier") {
840
+ const isStep = token.kind === "Step" && isStepReference(token);
841
+ if (isStep && getProofLevel() < 0 && proofLevels.length <= 1) {
842
+ throw createError("Step number used outside proof", token);
843
+ }
844
+ if (isStep && token.text[1] === "+") {
845
+ throw createError("<+> step number used in an expression", token);
846
+ }
847
+ if (token.kind === "Identifier" || isStep) {
848
+ const name = isStep ? (getStepName(token) ?? token.text) : token.text;
553
849
  if (isLabelAhead()) {
554
850
  const params = accept("(") ? parseSeparated(expectIdentifier) : [];
555
851
  if (params.length > 0) expect(")");
556
852
  expect("::");
557
- return { kind: "Label", name: token.text, params, body: parseExpression(), location };
853
+ return { kind: "Label", name, params, body: parseExpression(), location };
558
854
  }
559
855
  const hasArguments = isAt("(");
560
856
  const args = hasArguments ? parseArguments() : [];
561
857
  if (!isSelectorAhead()) {
562
858
  return hasArguments
563
- ? { kind: "Apply", operator: token.text, args, location }
564
- : { kind: "Identifier", name: token.text, location };
859
+ ? { kind: "Apply", operator: name, args, location }
860
+ : { kind: "Identifier", name, location };
861
+ }
862
+ const instance = openModules
863
+ .flatMap((module) => module.instances)
864
+ .find((candidate) => candidate.name === name);
865
+ if (instance && !isFactStart && !isTlapm && instance.params.length !== args.length) {
866
+ throw createError(
867
+ `The operator ${name} requires ${instance.params.length} arguments`,
868
+ token,
869
+ );
565
870
  }
566
- const selectors: Selector[] = [{ kind: "Name", name: token.text, args }];
871
+ const selectors: Selector[] = [{ kind: "Name", name, args }];
567
872
  while (isSelectorAhead()) {
568
873
  advance();
569
874
  selectors.push(parseSelector());
@@ -576,13 +881,15 @@ export const parseModule = (source: string): Module => {
576
881
  const qualifiedArgs = selectors.flatMap((selector) =>
577
882
  "args" in selector ? selector.args : [],
578
883
  );
579
- return qualifiedArgs.length > 0
580
- ? { kind: "Apply", operator, args: qualifiedArgs, location }
581
- : { 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 };
582
891
  }
583
- const isOperatorName =
584
- INFIX_OPERATORS.has(token.text) || POSTFIX_OPERATORS.has(token.text) || token.text === "-.";
585
- if (token.kind === "Operator" && isOperatorName && isAt("("))
892
+ if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
586
893
  return { kind: "Apply", operator: token.text, args: parseArguments(), location };
587
894
  switch (token.text) {
588
895
  case "(": {
@@ -615,15 +922,19 @@ export const parseModule = (source: string): Module => {
615
922
  case "LET": {
616
923
  const definitions: Definition[] = [];
617
924
  const instances: Instance[] = [];
925
+ recursiveScopes.push(new Set());
618
926
  while (!accept("IN")) {
927
+ if (isTlapm && isAt("RECURSIVE"))
928
+ throw createError("Unexpected keyword RECURSIVE", getDefinitionEnd(peekRaw()));
619
929
  if (accept("RECURSIVE")) {
620
- parseDeclarations().forEach((declaration) => recursiveNames.add(declaration.name));
930
+ declareRecursive();
621
931
  continue;
622
932
  }
623
933
  const unit = parseDefinition(false);
624
934
  if (unit.kind === "Instance") instances.push(unit);
625
935
  else definitions.push(unit);
626
936
  }
937
+ recursiveScopes.pop();
627
938
  const body = parseExpression();
628
939
  return instances.length === 0
629
940
  ? { kind: "Let", definitions, body, location }
@@ -652,7 +963,7 @@ export const parseModule = (source: string): Module => {
652
963
  };
653
964
  }
654
965
  }
655
- throw createError(`Unexpected ${describe(token)}`, token);
966
+ throw createUnexpected(`Unexpected ${describe(token)}`, token);
656
967
  };
657
968
 
658
969
  const parsePrefix = (): Expression => {
@@ -668,7 +979,12 @@ export const parseModule = (source: string): Module => {
668
979
  return { kind: "Apply", operator: "~", args: [parseExpression(5)], location };
669
980
  case "-":
670
981
  advance();
671
- return { kind: "Apply", operator: "-.", args: [parseExpression(13)], location };
982
+ return {
983
+ kind: "Apply",
984
+ operator: "-.",
985
+ args: [parseExpression(NEGATION_PRECEDENCE + 1)],
986
+ location,
987
+ };
672
988
  case "[]":
673
989
  advance();
674
990
  return { kind: "Always", expression: parseExpression(16), location };
@@ -692,8 +1008,10 @@ export const parseModule = (source: string): Module => {
692
1008
  };
693
1009
 
694
1010
  const parseExpression = (minimumPrecedence = 0): Expression => {
1011
+ const start = cursor.index;
1012
+ const prefix = parsePrefix();
695
1013
  const state = {
696
- left: parsePrefix(),
1014
+ left: prefix.location.range ? prefix : withRange(prefix, start),
697
1015
  previous: undefined as (InfixOperator & { text: string }) | undefined,
698
1016
  };
699
1017
  while (true) {
@@ -702,24 +1020,31 @@ export const parseModule = (source: string): Module => {
702
1020
  const location = getLocation(token);
703
1021
  if (token.text === "'" && minimumPrecedence <= 15) {
704
1022
  advance();
705
- state.left = { kind: "Prime", expression: state.left, location };
1023
+ state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
706
1024
  continue;
707
1025
  }
708
1026
  if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
709
1027
  advance();
710
- state.left = { kind: "Apply", operator: token.text, args: [state.left], location };
1028
+ state.left = withRange(
1029
+ { kind: "Apply", operator: token.text, args: [state.left], location },
1030
+ start,
1031
+ );
711
1032
  continue;
712
1033
  }
713
1034
  if (token.text === "[") {
714
1035
  advance();
715
1036
  const args = withoutOffside(() => parseSeparated(() => parseExpression()));
716
1037
  withoutOffside(() => expect("]"));
717
- state.left = { kind: "FunctionApplication", function: state.left, args, location };
1038
+ state.left = withRange(
1039
+ { kind: "FunctionApplication", function: state.left, args, location },
1040
+ start,
1041
+ );
718
1042
  continue;
719
1043
  }
720
1044
  if (token.text === "." && isFieldName(peek(1))) {
721
1045
  advance();
722
- state.left = { kind: "FieldAccess", record: state.left, field: advance().text, location };
1046
+ const field = advance().text;
1047
+ state.left = withRange({ kind: "FieldAccess", record: state.left, field, location }, start);
723
1048
  continue;
724
1049
  }
725
1050
  const infix = INFIX_OPERATORS.get(token.text);
@@ -736,6 +1061,10 @@ export const parseModule = (source: string): Module => {
736
1061
  }
737
1062
  }
738
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
+ }
739
1068
  const right = parseExpression(infix.high + 1);
740
1069
  const left = state.left;
741
1070
  if (token.text === "/\\" || token.text === "\\/") {
@@ -752,44 +1081,59 @@ export const parseModule = (source: string): Module => {
752
1081
  } else {
753
1082
  state.left = { kind: "Apply", operator: token.text, args: [left, right], location };
754
1083
  }
1084
+ state.left = withRange(state.left, start);
755
1085
  state.previous = { ...infix, text: token.text };
756
1086
  }
757
1087
  return state.left;
758
1088
  };
759
1089
 
760
- const parseParams = (): OperatorParam[] => {
1090
+ const parseParams = () => {
761
1091
  if (!accept("(")) return [];
762
1092
  const params = parseDeclarations();
763
1093
  expect(")");
764
1094
  return params;
765
1095
  };
766
1096
 
1097
+ const parseBody = () => {
1098
+ const start = getLocation(peek());
1099
+ const body = parseExpression();
1100
+ const last = tokens[cursor.index - 1];
1101
+ const end = { line: last.line, column: last.endColumn };
1102
+ return { body, bodyRange: { module: openModules[openModules.length - 1].name, start, end } };
1103
+ };
1104
+
767
1105
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
1106
+ const start = cursor.index;
1107
+ return withRange(parseDefinitionUnit(isLocal), start);
1108
+ };
1109
+
1110
+ const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
768
1111
  const token = peek();
769
1112
  const location = getLocation(token);
1113
+ const parseOperator = (name: string, params: OperatorParam[]): Definition => ({
1114
+ kind: "Operator",
1115
+ name,
1116
+ params,
1117
+ ...parseBody(),
1118
+ isLocal,
1119
+ isRecursive: isRecursiveName(name),
1120
+ location,
1121
+ });
770
1122
  if (
771
1123
  token.kind === "Identifier" &&
772
1124
  peek(1).kind === "Operator" &&
773
- INFIX_OPERATORS.has(peek(1).text)
1125
+ INFIX_OPERATORS.has(peek(1).text) &&
1126
+ peek(2).kind === "Identifier" &&
1127
+ isAt("==", 3)
774
1128
  ) {
775
- if (peek(2).kind === "Identifier" && isAt("==", 3)) {
776
- const left = expectIdentifier();
777
- const operator = advance().text;
778
- const right = expectIdentifier();
779
- expect("==");
780
- return {
781
- kind: "Operator",
782
- name: operator,
783
- params: [
784
- { name: left, arity: 0 },
785
- { name: right, arity: 0 },
786
- ],
787
- body: parseExpression(),
788
- isLocal,
789
- isRecursive: recursiveNames.has(operator),
790
- location,
791
- };
792
- }
1129
+ const left = expectIdentifier();
1130
+ const operator = advance().text;
1131
+ const right = expectIdentifier();
1132
+ expect("==");
1133
+ return parseOperator(operator, [
1134
+ { name: left, arity: 0 },
1135
+ { name: right, arity: 0 },
1136
+ ]);
793
1137
  }
794
1138
  const isPrefixDefinition = token.kind === "Operator" && token.text === "-.";
795
1139
  const isPostfixDefinition =
@@ -799,15 +1143,7 @@ export const parseModule = (source: string): Module => {
799
1143
  cursor.index += isPrefixDefinition ? 1 : 2;
800
1144
  const param = isPrefixDefinition ? expectIdentifier() : token.text;
801
1145
  expect("==");
802
- return {
803
- kind: "Operator",
804
- name: operator,
805
- params: [{ name: param, arity: 0 }],
806
- body: parseExpression(),
807
- isLocal,
808
- isRecursive: recursiveNames.has(operator),
809
- location,
810
- };
1146
+ return parseOperator(operator, [{ name: param, arity: 0 }]);
811
1147
  }
812
1148
  const name = expectIdentifier();
813
1149
  if (accept("[")) {
@@ -820,22 +1156,18 @@ export const parseModule = (source: string): Module => {
820
1156
  bounds,
821
1157
  body: parseExpression(),
822
1158
  isLocal,
823
- isRecursive: recursiveNames.has(name),
1159
+ isRecursive: isRecursiveName(name),
824
1160
  location,
825
1161
  };
826
1162
  }
1163
+ const definitionStart = cursor.index;
827
1164
  const params = parseParams();
828
1165
  expect("==");
829
- if (isAt("INSTANCE")) return parseInstance(name, params, isLocal, location);
830
- return {
831
- kind: "Operator",
832
- name,
833
- params,
834
- body: parseExpression(),
835
- isLocal,
836
- isRecursive: recursiveNames.has(name),
837
- location,
838
- };
1166
+ if (isAt("INSTANCE")) {
1167
+ const instance = parseInstance(name, params, isLocal, location);
1168
+ return { ...instance, definitionRange: getRange(definitionStart) };
1169
+ }
1170
+ return parseOperator(name, params);
839
1171
  };
840
1172
 
841
1173
  const parseInstance = (
@@ -844,145 +1176,577 @@ export const parseModule = (source: string): Module => {
844
1176
  isLocal: boolean,
845
1177
  location: SourceLocation,
846
1178
  ): Instance => {
1179
+ const start = cursor.index;
847
1180
  expect("INSTANCE");
848
1181
  const module = expectIdentifier();
849
1182
  const substitutions = accept("WITH")
850
- ? parseSeparated((): InstanceSubstitution => {
1183
+ ? parseSeparated(() => {
851
1184
  const target = advance().text;
852
1185
  expect("<-");
853
1186
  return { name: target, expression: parseArgument() };
854
1187
  })
855
1188
  : [];
856
- 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
+ };
857
1199
  };
858
1200
 
859
- const parseDeclarations = (): Declaration[] =>
860
- parseSeparated(() => {
861
- if (isAt("_") && peek(1).kind === "Operator") {
862
- advance();
863
- const operator = advance().text;
864
- return accept("_") ? { name: operator, arity: 2 } : { name: operator, arity: 1 };
865
- }
866
- if (peek().kind === "Operator" && isAt("_", 1)) {
867
- const operator = advance().text;
868
- advance();
869
- return { name: operator === "-" ? "-." : operator, arity: 1 };
1201
+ const parseDeclaration = (): Declaration => {
1202
+ const location = getLocation(peek());
1203
+ if (isAt("_") && peek(1).kind === "Operator") {
1204
+ advance();
1205
+ const name = advance().text;
1206
+ return { name, arity: accept("_") ? 2 : 1, location };
1207
+ }
1208
+ if (peek().kind === "Operator" && isAt("_", 1)) {
1209
+ const operator = advance().text;
1210
+ advance();
1211
+ return { name: operator === "-" ? "-." : operator, arity: 1, location };
1212
+ }
1213
+ const name = expectIdentifier();
1214
+ if (!accept("(")) return { name, arity: 0, location };
1215
+ const placeholders = parseSeparated(() => expect("_"));
1216
+ expect(")");
1217
+ return { name, arity: placeholders.length, location };
1218
+ };
1219
+
1220
+ const parseDeclarations = () => parseSeparated(parseDeclaration);
1221
+
1222
+ const declareRecursive = () => {
1223
+ const declarations = parseDeclarations();
1224
+ declarations.forEach((declaration) =>
1225
+ recursiveScopes[recursiveScopes.length - 1].add(declaration.name),
1226
+ );
1227
+ return declarations;
1228
+ };
1229
+
1230
+ const getClosingOffset = (offset: number) => {
1231
+ const depth = { value: 0, offset };
1232
+ do {
1233
+ const token = peek(depth.offset);
1234
+ if (token.kind === "End") return depth.offset;
1235
+ if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth.value++;
1236
+ if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth.value--;
1237
+ depth.offset++;
1238
+ } while (depth.value > 0);
1239
+ return depth.offset;
1240
+ };
1241
+
1242
+ // SANY inserts a break before each definition, which ends the fact list or step before it.
1243
+ const isDefinitionStart = () => {
1244
+ const token = peek();
1245
+ const next = peek(1);
1246
+ if (token.kind !== "Identifier") {
1247
+ return isAt("-.") && next.kind === "Identifier" && isAt("==", 2);
1248
+ }
1249
+ if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1250
+ if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
1251
+ if (next.kind === "Operator" && INFIX_OPERATORS.has(next.text)) {
1252
+ return peek(2).kind === "Identifier" && isAt("==", 3);
1253
+ }
1254
+ return isAt("==", 1);
1255
+ };
1256
+
1257
+ // SANY's proof levels, one for each open proof, which stay -1 until a step sets them.
1258
+ const proofLevels: number[] = [];
1259
+
1260
+ const getProofLevel = () => proofLevels[proofLevels.length - 1] ?? -1;
1261
+
1262
+ const getStepLevel = (step: Token) => {
1263
+ if (step.text[1] === "*") return -1;
1264
+ if (step.text[1] === "+") return -2;
1265
+ return Number(step.text.slice(1, step.text.indexOf(">")));
1266
+ };
1267
+
1268
+ const getStepLabel = (step: Token) => {
1269
+ const label = step.text.slice(step.text.indexOf(">") + 1);
1270
+ const end = { value: label.length };
1271
+ while (label[end.value - 1] === ".") end.value--;
1272
+ return label.slice(0, end.value);
1273
+ };
1274
+
1275
+ const isStepReference = (step: Token) =>
1276
+ !step.text.endsWith(".") && !UNNAMED_STEP_LABELS.has(getStepLabel(step));
1277
+
1278
+ const getStepName = (step: Token) => {
1279
+ const label = getStepLabel(step);
1280
+ if (UNNAMED_STEP_LABELS.has(label)) return undefined;
1281
+ const level = getStepLevel(step);
1282
+ if (level >= 0) return `<${level}>${label}`;
1283
+ const current = getProofLevel();
1284
+ const outer = proofLevels[proofLevels.length - 2];
1285
+ return `<${current < 0 && outer !== undefined ? outer : current}>${label}`;
1286
+ };
1287
+
1288
+ const isProofStart = (token: Token) => {
1289
+ if (token.kind === "Keyword") return PROOF_STARTS.has(token.text);
1290
+ if (token.kind !== "Step") return false;
1291
+ if (token.text[1] === "*") return proofLevels.length === 0;
1292
+ if (token.text[1] === "+" || proofLevels.length === 0) return true;
1293
+ const level = getProofLevel();
1294
+ return level >= 0 && getStepLevel(token) > level;
1295
+ };
1296
+
1297
+ const isCorrectLevel = (step: Token) => {
1298
+ const level = getStepLevel(step);
1299
+ const top = proofLevels.length - 1;
1300
+ const outer = proofLevels[top - 1] ?? -1;
1301
+ const isFirst = proofLevels[top] < 0;
1302
+ if (isFirst) proofLevels[top] = level < 0 ? outer + 1 : level;
1303
+ if (level === -1) return true;
1304
+ if (level === -2) return isFirst;
1305
+ return isFirst ? level > outer : proofLevels[top] === level;
1306
+ };
1307
+
1308
+ const isDeclared = (name: string) =>
1309
+ !factNames.has(name) &&
1310
+ openModules.some(
1311
+ (module) =>
1312
+ module.constants.some((constant) => constant.name === name) ||
1313
+ module.variables.some((variable) => variable.name === name) ||
1314
+ module.definitions.some((definition) => definition.name === name),
1315
+ );
1316
+
1317
+ const isHideFact = (fact: Fact) => {
1318
+ if (fact.kind === "ModuleFact" || fact.kind === "Subexpression") return true;
1319
+ if (fact.kind === "Apply") return fact.operator.includes("!");
1320
+ if (fact.kind !== "Identifier") return false;
1321
+ if (fact.name.startsWith("<") || fact.name.includes("!")) return true;
1322
+ return !BUILTIN_CONSTANTS.has(fact.name) && !isDeclared(fact.name);
1323
+ };
1324
+
1325
+ const isFactAhead = () => {
1326
+ const token = peek();
1327
+ if (token.kind === "Step") return isStepReference(token);
1328
+ if (token.kind === "Keyword") return FACT_KEYWORDS.has(token.text);
1329
+ if (token.kind === "End" || token.kind === "ModuleEnd" || token.kind === "Separator") {
1330
+ return false;
1331
+ }
1332
+ return !isDefinitionStart();
1333
+ };
1334
+
1335
+ // SANY reads an infix or postfix operator alone as its name, but a prefix operator such as - as the
1336
+ // start of an expression.
1337
+ const parseFact = (): Fact => {
1338
+ const start = cursor.index;
1339
+ factStart.index = start;
1340
+ const token = peek();
1341
+ if (
1342
+ token.kind === "Operator" &&
1343
+ !EXPRESSION_PREFIXES.has(token.text) &&
1344
+ isOperatorName(token.text)
1345
+ ) {
1346
+ cursor.index++;
1347
+ return withRange(
1348
+ { kind: "OperatorReference", operator: token.text, location: getLocation(token) },
1349
+ start,
1350
+ );
1351
+ }
1352
+ if (!accept("MODULE")) return parseExpression();
1353
+ return withRange(
1354
+ { kind: "ModuleFact", name: expectIdentifier(), location: getLocation(token) },
1355
+ start,
1356
+ );
1357
+ };
1358
+
1359
+ const declareProofSymbol = (name: string) => proofScopes[proofScopes.length - 1]?.add(name);
1360
+
1361
+ const withProofScope = <Result>(parse: () => Result) => {
1362
+ proofScopes.push(new Set());
1363
+ const result = parse();
1364
+ proofScopes.pop();
1365
+ return result;
1366
+ };
1367
+
1368
+ const isDeclaredSymbol = (name: string) =>
1369
+ proofScopes.some((scope) => scope.has(name)) ||
1370
+ openModules.some(
1371
+ (module) =>
1372
+ module.constants.some((constant) => constant.name === name) ||
1373
+ module.variables.some((variable) => variable.name === name),
1374
+ );
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);
870
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
+
1398
+ const parseDefs = () => {
1399
+ if (isTlapm) return parseSeparated(parseDefinable);
1400
+ const defs = parseSeparated(parseFact);
1401
+ const invalid = defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name));
1402
+ if (invalid) {
1403
+ throw createError("DEF clause entry should describe a defined operator", invalid.location);
1404
+ }
1405
+ return defs;
1406
+ };
1407
+
1408
+ const parseFacts = () => ({
1409
+ facts: isFactAhead() ? parseSeparated(parseFact) : [],
1410
+ defs: accept("DEF") || accept("DEFS") ? parseDefs() : [],
1411
+ });
1412
+
1413
+ const parseUseOrHide = (): UseOrHide => {
1414
+ const start = cursor.index;
1415
+ const keyword = advance();
1416
+ const isOnly = keyword.text === "USE" && accept("ONLY");
1417
+ const keywordRange = getRange(start);
1418
+ const use: UseOrHide = {
1419
+ kind: keyword.text === "USE" ? "Use" : "Hide",
1420
+ isOnly,
1421
+ ...parseFacts(),
1422
+ keyword: keywordRange,
1423
+ location: getLocation(keyword),
1424
+ };
1425
+ const invalid =
1426
+ use.kind === "Hide" && !isTlapm ? use.facts.find((fact) => !isHideFact(fact)) : undefined;
1427
+ if (invalid) {
1428
+ throw createError(
1429
+ "The only expression allowed as a fact in a HIDE is the name of a theorem, assumption, or step",
1430
+ invalid.location,
1431
+ );
1432
+ }
1433
+ return withRange(use, start);
1434
+ };
1435
+
1436
+ const parseNewSymbol = (): NewSymbol => {
1437
+ const start = cursor.index;
1438
+ const location = getLocation(peek());
1439
+ const isNew = accept("NEW");
1440
+ const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
1441
+ if (!isNew && level === undefined) {
1442
+ throw createUnexpected(`Expected a declaration but found ${describe(peek())}`);
1443
+ }
1444
+ if (level !== undefined) advance();
1445
+ if (level === "Variable") {
871
1446
  const name = expectIdentifier();
872
- if (!accept("(")) return { name, arity: 0 };
873
- const placeholders = parseSeparated(() => expect("_"));
874
- expect(")");
875
- return { name, arity: placeholders.length };
876
- });
1447
+ declareProofSymbol(name);
1448
+ return withRange(
1449
+ { kind: "NewSymbol", name, arity: 0, level, set: undefined, location },
1450
+ start,
1451
+ );
1452
+ }
1453
+ const declaration = parseDeclaration();
1454
+ declareProofSymbol(declaration.name);
1455
+ const isBounded = (level ?? "Constant") === "Constant" && isAt("\\in");
1456
+ if (isBounded && declaration.arity > 0) {
1457
+ throw createError("declared symbol with arguments before \\in");
1458
+ }
1459
+ if (isBounded) advance();
1460
+ return withRange(
1461
+ {
1462
+ kind: "NewSymbol",
1463
+ name: declaration.name,
1464
+ arity: declaration.arity,
1465
+ level: level ?? "Constant",
1466
+ set: isBounded ? parseExpression() : undefined,
1467
+ location,
1468
+ },
1469
+ start,
1470
+ );
1471
+ };
877
1472
 
878
- const isUnitStart = (token: Token, next: Token) => {
879
- if (token.column !== 1) return false;
880
- if (token.kind === "Separator" || token.kind === "ModuleEnd" || token.kind === "End")
881
- return true;
882
- if (token.kind === "Keyword") return UNIT_KEYWORDS.has(token.text);
883
- return (
884
- token.kind === "Identifier" && (next.text === "==" || next.text === "(" || next.text === "[")
1473
+ const parseAssumeProve = (): AssumeProve => {
1474
+ const start = cursor.index;
1475
+ const location = getLocation(expect("ASSUME"));
1476
+ const assumptions = parseSeparated(() => {
1477
+ if (isAt("ASSUME")) return withProofScope(parseAssumeProve);
1478
+ if (peek().kind === "Keyword" && NEW_SYMBOL_STARTS.has(peek().text)) return parseNewSymbol();
1479
+ return parseExpression();
1480
+ });
1481
+ expect("PROVE");
1482
+ return withRange(
1483
+ { kind: "AssumeProve", assumptions, conclusion: parseExpression(), location },
1484
+ start,
885
1485
  );
886
1486
  };
887
1487
 
888
- const skipUnit = (start: Token) => {
889
- while (peekRaw().kind !== "End") {
890
- const token = peekRaw();
891
- if (token.line > start.line && isUnitStart(token, peekRaw(1))) return;
892
- cursor.index++;
1488
+ const isNameListAhead = (terminator: string) => {
1489
+ const offset = { value: 0 };
1490
+ while (peek(offset.value).kind === "Identifier" && isAt(",", offset.value + 1)) {
1491
+ offset.value += 2;
893
1492
  }
1493
+ return peek(offset.value).kind === "Identifier" && isAt(terminator, offset.value + 1);
894
1494
  };
895
1495
 
896
- const isProofStart = (index: number, start: Token) => {
897
- const token = tokens[index];
898
- if (token.kind === "End") return true;
899
- if (token.line > start.line && isUnitStart(token, tokens[index + 1])) return true;
900
- if (token.text === "PROOF" || PROOF_WORDS.has(token.text)) return true;
901
- return (
902
- token.text === "<" && tokens[index + 1]?.kind === "Number" && tokens[index + 2]?.text === ">"
1496
+ const declareProofSymbols = (names: string[], bounds: Bound[]) => {
1497
+ names.forEach(declareProofSymbol);
1498
+ bounds.forEach((bound) =>
1499
+ (bound.pattern.kind === "Name" ? [bound.pattern.name] : bound.pattern.names).forEach(
1500
+ declareProofSymbol,
1501
+ ),
903
1502
  );
904
1503
  };
905
1504
 
906
- const parseTheorem = (): Definition | undefined => {
907
- const start = advance();
908
- const isNamed = peek().kind === "Identifier" && isAt("==", 1) && !isAt("ASSUME", 2);
909
- if (!isNamed) {
910
- skipUnit(start);
911
- return undefined;
1505
+ const parseStepBody = (): StepBody => {
1506
+ const token = peek();
1507
+ if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1508
+ if (isAt("INSTANCE")) {
1509
+ return withRange(parseInstance(undefined, [], false, getLocation(token)), cursor.index);
912
1510
  }
913
- const name = expectIdentifier();
914
- advance();
915
- const end = { value: cursor.index };
916
- while (!isProofStart(end.value, start)) end.value++;
917
- cursor.limit = end.value;
918
- const body = parseExpression();
919
- const isComplete = cursor.index === end.value;
920
- cursor.limit = tokens.length;
921
- skipUnit(start);
922
- if (!isComplete) return undefined;
1511
+ if (isAt("DEFINE") || isDefinitionStart()) {
1512
+ const start = cursor.index;
1513
+ accept("DEFINE");
1514
+ const definitions = [parseDefinition(false)];
1515
+ while (isDefinitionStart()) definitions.push(parseDefinition(false));
1516
+ const define: DefineStep = { kind: "Define", definitions, location: getLocation(token) };
1517
+ return withRange(define, start);
1518
+ }
1519
+ if (accept("HAVE")) return { kind: "Have", expression: parseExpression() };
1520
+ if (accept("TAKE")) {
1521
+ const isBounded = isBoundAhead();
1522
+ const names = isBounded ? [] : parseSeparated(expectIdentifier);
1523
+ const bounds = isBounded ? parseBounds() : [];
1524
+ declareProofSymbols(names, bounds);
1525
+ return { kind: "Take", names, bounds };
1526
+ }
1527
+ if (accept("WITNESS")) {
1528
+ return { kind: "Witness", expressions: parseSeparated(() => parseExpression()) };
1529
+ }
1530
+ if (accept("PICK")) {
1531
+ const isNames = isNameListAhead(":");
1532
+ const names = isNames ? parseSeparated(expectIdentifier) : [];
1533
+ const bounds = isNames ? [] : parseBounds();
1534
+ declareProofSymbols(names, bounds);
1535
+ expect(":");
1536
+ return { kind: "Pick", names, bounds, body: parseExpression() };
1537
+ }
1538
+ if (accept("CASE")) return { kind: "Case", expression: parseExpression() };
923
1539
  return {
924
- kind: "Operator",
925
- name,
926
- params: [],
927
- body,
928
- isLocal: false,
929
- isRecursive: false,
930
- location: getLocation(start),
1540
+ kind: "Assert",
1541
+ isSuffices: accept("SUFFICES"),
1542
+ statement: isAt("ASSUME") ? parseAssumeProve() : parseExpression(),
931
1543
  };
932
1544
  };
933
1545
 
1546
+ const parseStepStart = (isQed: boolean) => {
1547
+ const step = peek();
1548
+ if (step.kind !== "Step") {
1549
+ throw createUnexpected(`Expected a step number but found ${describe(step)}`, step);
1550
+ }
1551
+ if (!isCorrectLevel(step)) {
1552
+ throw createError(`${isQed ? "QED step" : "step"}'s number has bad level`, step);
1553
+ }
1554
+ if (getStepLevel(step) < 0 && getStepName(step) !== undefined) {
1555
+ throw createError("<*> and <+> cannot be used for a named step", step);
1556
+ }
1557
+ cursor.index++;
1558
+ return { name: getStepName(step), token: step.text, location: getLocation(step) };
1559
+ };
1560
+
1561
+ // The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
1562
+ // of the enclosing proof.
1563
+ const parseStep = (): ProofStep => {
1564
+ const start = cursor.index;
1565
+ const step = parseStepStart(isAt("QED", 1));
1566
+ const scope = new Set<string>();
1567
+ proofScopes.push(scope);
1568
+ const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1569
+ const head = getRange(start);
1570
+ const hasProof = isProofStart(peek());
1571
+ if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
1572
+ throw createError("Proof of a step that does not take a proof");
1573
+ }
1574
+ const proof = hasProof ? parseProof() : undefined;
1575
+ proofScopes.pop();
1576
+ if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1577
+ return withRange({ ...step, body, head, proof }, start);
1578
+ };
1579
+
1580
+ const parseProofBody = (): Proof => {
1581
+ const location = getLocation(peek());
1582
+ if (isAt("BY") || (isAt("PROOF") && isAt("BY", 1))) {
1583
+ accept("PROOF");
1584
+ advance();
1585
+ const keyword = getRange(cursor.index - 1);
1586
+ return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
1587
+ }
1588
+ const terminal = peek(isAt("PROOF") ? 1 : 0);
1589
+ if (
1590
+ terminal.kind === "Keyword" &&
1591
+ (terminal.text === "OBVIOUS" || terminal.text === "OMITTED")
1592
+ ) {
1593
+ accept("PROOF");
1594
+ advance();
1595
+ const keyword = getRange(cursor.index - 1);
1596
+ return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
1597
+ }
1598
+ accept("PROOF");
1599
+ const steps = [parseStep()];
1600
+ while (steps[steps.length - 1].body.kind !== "Qed") steps.push(parseStep());
1601
+ return { kind: "Steps", level: getProofLevel(), steps, location };
1602
+ };
1603
+
1604
+ const parseProof = (): Proof => {
1605
+ const start = cursor.index;
1606
+ proofLevels.push(-1);
1607
+ const proof = withProofScope(parseProofBody);
1608
+ proofLevels.pop();
1609
+ return withRange(proof, start);
1610
+ };
1611
+
1612
+ const parseTheorem = (module: Module) => {
1613
+ const start = cursor.index;
1614
+ const keyword = advance();
1615
+ const isNamed = peek().kind === "Identifier" && isAt("==", 1);
1616
+ const name = isNamed ? expectIdentifier() : undefined;
1617
+ if (name !== undefined) {
1618
+ factNames.add(name);
1619
+ advance();
1620
+ }
1621
+ const parsed = withProofScope(() => {
1622
+ const statement = isAt("ASSUME") ? parseAssumeProve() : parseExpression();
1623
+ return { statement, proof: isProofStart(peek()) ? parseProof() : undefined };
1624
+ });
1625
+ const statement = parsed.statement;
1626
+ const theorem = withRange(
1627
+ { name, statement, proof: parsed.proof, location: getLocation(keyword) },
1628
+ start,
1629
+ );
1630
+ module.theorems.push(theorem);
1631
+ module.units.push({ kind: "Theorem", theorem });
1632
+ if (name === undefined || statement.kind === "AssumeProve") return;
1633
+ module.definitions.push(
1634
+ withRange(
1635
+ {
1636
+ kind: "Operator",
1637
+ name,
1638
+ params: [],
1639
+ body: statement,
1640
+ isLocal: false,
1641
+ isRecursive: false,
1642
+ location: getLocation(keyword),
1643
+ },
1644
+ start,
1645
+ ),
1646
+ );
1647
+ };
1648
+
934
1649
  const parseAssumption = (): Assumption => {
1650
+ const start = cursor.index;
1651
+ return withRange(parseAssumptionUnit(), start);
1652
+ };
1653
+
1654
+ const parseAssumptionUnit = (): Assumption => {
935
1655
  const keyword = advance();
936
1656
  const location = getLocation(keyword);
937
1657
  const isAxiom = keyword.text === "AXIOM";
938
1658
  if (peek().kind === "Identifier" && isAt("==", 1)) {
939
1659
  const name = expectIdentifier();
1660
+ factNames.add(name);
940
1661
  advance();
941
1662
  return { name, expression: parseExpression(), isAxiom, location };
942
1663
  }
943
1664
  return { name: undefined, expression: parseExpression(), isAxiom, location };
944
1665
  };
945
1666
 
946
- const parseModuleBody = (outer?: OuterScope): Module => {
947
- expect("----");
1667
+ const parseModuleBody = (): Module => {
1668
+ const location = getLocation(expect("----"));
948
1669
  expect("MODULE");
949
1670
  const name = expectIdentifier();
950
1671
  expect("----");
951
1672
  const module: Module = {
952
1673
  name,
953
- outer,
954
1674
  modules: [],
955
- extends: accept("EXTENDS") ? parseSeparated(expectIdentifier) : [],
1675
+ extends: [],
1676
+ extendsRanges: [],
956
1677
  constants: [],
957
1678
  variables: [],
958
1679
  definitions: [],
959
1680
  instances: [],
960
1681
  assumptions: [],
1682
+ location,
1683
+ theorems: [],
1684
+ uses: [],
1685
+ units: [],
961
1686
  };
1687
+ openModules.push(module);
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
+ }
962
1696
  while (true) {
1697
+ const tokenIndex = cursor.index;
1698
+ unitStart.index = tokenIndex;
963
1699
  const token = peek();
964
- if (token.kind === "ModuleEnd" || token.kind === "End") return module;
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
+ }
1708
+ openModules.pop();
1709
+ recursiveScopes.pop();
1710
+ return module;
1711
+ }
965
1712
  if (token.kind === "Separator") {
966
- if (isAt("MODULE", 1)) module.modules.push(parseNestedModule(getOuterScope(module)));
967
- else advance();
1713
+ if (isAt("MODULE", 1)) {
1714
+ const submodule = parseModuleBody();
1715
+ module.modules.push(submodule);
1716
+ module.units.push({ kind: "Module", module: submodule });
1717
+ expect("====");
1718
+ continue;
1719
+ }
1720
+ advance();
968
1721
  continue;
969
1722
  }
970
1723
  switch (token.text) {
971
1724
  case "CONSTANT":
972
- case "CONSTANTS":
1725
+ case "CONSTANTS": {
973
1726
  advance();
974
- module.constants.push(...parseDeclarations());
1727
+ const declarations = parseDeclarations();
1728
+ module.constants.push(...declarations);
1729
+ module.units.push({ kind: "Constants", declarations });
975
1730
  continue;
1731
+ }
976
1732
  case "VARIABLE":
977
- case "VARIABLES":
1733
+ case "VARIABLES": {
978
1734
  advance();
979
- module.variables.push(...parseSeparated(expectIdentifier));
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 });
980
1742
  continue;
1743
+ }
981
1744
  case "ASSUME":
982
1745
  case "ASSUMPTION":
983
1746
  case "AXIOM": {
984
1747
  const assumption = parseAssumption();
985
1748
  module.assumptions.push(assumption);
1749
+ module.units.push({ kind: "Assumption", assumption });
986
1750
  if (assumption.name !== undefined) {
987
1751
  module.definitions.push({
988
1752
  kind: "Operator",
@@ -996,57 +1760,57 @@ export const parseModule = (source: string): Module => {
996
1760
  }
997
1761
  continue;
998
1762
  }
1763
+ case "USE":
1764
+ case "HIDE": {
1765
+ const use = parseUseOrHide();
1766
+ module.uses.push(use);
1767
+ module.units.push({ kind: "Use", use });
1768
+ continue;
1769
+ }
999
1770
  case "RECURSIVE":
1000
1771
  advance();
1001
- parseDeclarations().forEach((declaration) => recursiveNames.add(declaration.name));
1772
+ module.units.push({ kind: "Recursive", declarations: declareRecursive() });
1002
1773
  continue;
1003
1774
  case "INSTANCE":
1004
- module.instances.push(parseInstance(undefined, [], false, getLocation(token)));
1775
+ addDefinitionUnit(
1776
+ module,
1777
+ withRange(parseInstance(undefined, [], false, getLocation(token)), tokenIndex),
1778
+ );
1005
1779
  continue;
1006
1780
  case "LOCAL": {
1007
1781
  advance();
1008
1782
  if (isAt("INSTANCE")) {
1009
- module.instances.push(parseInstance(undefined, [], true, getLocation(token)));
1783
+ addDefinitionUnit(
1784
+ module,
1785
+ withRange(parseInstance(undefined, [], true, getLocation(token)), tokenIndex),
1786
+ );
1010
1787
  continue;
1011
1788
  }
1012
- const unit = parseDefinition(true);
1013
- if (unit.kind === "Instance") module.instances.push(unit);
1014
- else module.definitions.push(unit);
1789
+ addDefinitionUnit(module, parseDefinition(true));
1015
1790
  continue;
1016
1791
  }
1017
1792
  }
1018
1793
  if (token.kind === "Keyword" && THEOREM_KEYWORDS.has(token.text)) {
1019
- const theorem = parseTheorem();
1020
- if (theorem) module.definitions.push(theorem);
1794
+ parseTheorem(module);
1021
1795
  continue;
1022
1796
  }
1023
- const unit = parseDefinition(false);
1024
- if (unit.kind === "Instance") module.instances.push(unit);
1025
- else module.definitions.push(unit);
1797
+ addDefinitionUnit(module, parseDefinition(false));
1026
1798
  }
1027
1799
  };
1028
1800
 
1029
- const getOuterScope = (module: Module): OuterScope => ({
1030
- extends: [...(module.outer?.extends ?? []), ...module.extends],
1031
- symbols: [
1032
- ...(module.outer?.symbols ?? []),
1033
- ...module.constants,
1034
- ...module.variables.map((variable) => ({ name: variable, arity: 0 })),
1035
- ...module.definitions.map((definition) => ({
1036
- name: definition.name,
1037
- arity: definition.kind === "Operator" ? definition.params.length : 0,
1038
- })),
1039
- ],
1040
- });
1041
-
1042
- const parseNestedModule = (outer?: OuterScope) => {
1043
- const nested = parseModuleBody(outer);
1044
- accept("====");
1045
- return nested;
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;
1806
+ }
1807
+ module.definitions.push(unit);
1808
+ module.units.push({ kind: "Definition", definition: unit });
1046
1809
  };
1047
1810
 
1048
- const root = parseModuleBody();
1049
- accept("====");
1050
- while (peekRaw().kind === "Separator") root.modules.push(parseNestedModule());
1051
- return root;
1811
+ return parseModuleBody();
1052
1812
  };
1813
+
1814
+ export const parseModule = (source: string) => parse(source, false);
1815
+
1816
+ export const parseTlapmModule = (source: string) => parse(source, true);