tla-ts 0.0.3-dev.07eab97 → 0.0.3-dev.0e500e5

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.
@@ -0,0 +1,116 @@
1
+ import type { SourceRange } from "./ast";
2
+
3
+ // Where the modules in TLC's tla2sany/StandardModules define the operators that tla-ts implements
4
+ // natively, which SANY reports when a spec defines one of them again.
5
+ const RANGES: Record<string, Record<string, [number, number, number, number]>> = {
6
+ Naturals: {
7
+ Nat: [14, 1, 14, 16],
8
+ "+": [15, 1, 15, 19],
9
+ "-": [17, 1, 17, 33],
10
+ "*": [18, 1, 18, 17],
11
+ "^": [19, 1, 19, 19],
12
+ "<": [20, 1, 20, 19],
13
+ ">": [21, 1, 21, 19],
14
+ "<=": [22, 1, 22, 19],
15
+ ">=": [23, 1, 23, 19],
16
+ "%": [33, 1, 33, 20],
17
+ "\\div": [34, 1, 34, 20],
18
+ "..": [35, 1, 35, 20],
19
+ },
20
+ Integers: {
21
+ Int: [18, 1, 18, 12],
22
+ "-.": [22, 1, 22, 13],
23
+ },
24
+ Sequences: {
25
+ Seq: [20, 1, 20, 41],
26
+ Len: [25, 1, 25, 44],
27
+ "\\o": [30, 1, 31, 76],
28
+ Append: [36, 1, 36, 26],
29
+ Head: [41, 1, 41, 15],
30
+ Tail: [42, 1, 42, 61],
31
+ SubSeq: [48, 1, 48, 50],
32
+ SelectSeq: [53, 1, 65, 14],
33
+ },
34
+ FiniteSets: {
35
+ IsFiniteSet: [14, 1, 19, 68],
36
+ Cardinality: [21, 1, 27, 11],
37
+ },
38
+ Bags: {
39
+ IsABag: [20, 1, 24, 41],
40
+ BagToSet: [26, 1, 26, 23],
41
+ SetToBag: [31, 1, 31, 30],
42
+ BagIn: [36, 1, 36, 31],
43
+ EmptyBag: [41, 1, 41, 24],
44
+ "\\oplus": [43, 1, 49, 46],
45
+ "\\ominus": [51, 1, 60, 50],
46
+ BagUnion: [74, 1, 79, 58],
47
+ "\\sqsubseteq": [81, 1, 87, 42],
48
+ SubBag: [89, 1, 110, 11],
49
+ BagOfAll: [122, 1, 130, 66],
50
+ BagCardinality: [132, 1, 138, 8],
51
+ CopiesIn: [140, 1, 145, 33],
52
+ },
53
+ TLC: {
54
+ Print: [17, 1, 17, 22],
55
+ PrintT: [18, 1, 18, 19],
56
+ Assert: [24, 1, 25, 54],
57
+ JavaTime: [26, 1, 26, 32],
58
+ TLCGet: [75, 1, 75, 28],
59
+ TLCSet: [76, 1, 76, 20],
60
+ ":>": [78, 1, 78, 27],
61
+ "@@": [79, 1, 80, 50],
62
+ Permutations: [81, 1, 82, 54],
63
+ SortSeq: [88, 1, 92, 40],
64
+ RandomElement: [110, 1, 110, 41],
65
+ Any: [119, 1, 119, 22],
66
+ ToString: [121, 1, 121, 58],
67
+ TLCEval: [145, 1, 145, 15],
68
+ },
69
+ TLCExt: {
70
+ AssertEq: [14, 1, 20, 14],
71
+ AssertError: [23, 1, 26, 72],
72
+ TLCGetOrDefault: [30, 1, 31, 17],
73
+ TLCGetAndSet: [33, 1, 35, 57],
74
+ Trace: [41, 1, 57, 6],
75
+ CounterExample: [61, 1, 75, 8],
76
+ ToTrace: [77, 1, 87, 12],
77
+ TLCModelValue: [91, 1, 120, 29],
78
+ TLCDefer: [122, 1, 134, 6],
79
+ TLCNoOp: [138, 1, 145, 5],
80
+ PickSuccessor: [148, 1, 162, 37],
81
+ TLCCache: [166, 1, 173, 12],
82
+ TLCFP: [175, 1, 181, 24],
83
+ TLCEvalDefinition: [185, 1, 190, 6],
84
+ },
85
+ Randomization: {
86
+ RandomSubset: [30, 1, 30, 64],
87
+ RandomSetOfSubsets: [48, 1, 49, 67],
88
+ TestRandomSetOfSubsets: [61, 1, 62, 71],
89
+ },
90
+ Json: {
91
+ ToJson: [46, 1, 55, 37],
92
+ ToJsonArray: [57, 1, 64, 42],
93
+ ToJsonObject: [66, 1, 73, 43],
94
+ JsonSerialize: [75, 1, 80, 6],
95
+ JsonDeserialize: [82, 1, 87, 19],
96
+ ndJsonSerialize: [89, 1, 95, 6],
97
+ ndJsonDeserialize: [97, 1, 103, 19],
98
+ },
99
+ _Possible: {
100
+ _Track: [10, 1, 17, 38],
101
+ _Counts: [24, 1, 30, 14],
102
+ _CheckName: [32, 1, 35, 35],
103
+ _PrintCounts: [37, 1, 37, 31],
104
+ },
105
+ };
106
+
107
+ export const getStandardRange = (module: string, name: string): SourceRange | undefined => {
108
+ const range = RANGES[module]?.[name];
109
+ return (
110
+ range && {
111
+ module,
112
+ start: { line: range[0], column: range[1] },
113
+ end: { line: range[2], column: range[3] },
114
+ }
115
+ );
116
+ };
@@ -11,7 +11,13 @@ import {
11
11
  } from "./ast";
12
12
  import { TlaSyntaxError } from "./errors";
13
13
  import { getSourceName, mapChildren, mapDefinition } from "./instances";
14
- import { getArity, getBoundNames, getPatternNames, LOCAL_RESOLUTION } from "./resolve";
14
+ import {
15
+ getArity,
16
+ getBoundNames,
17
+ getOperandIndex,
18
+ getPatternNames,
19
+ LOCAL_RESOLUTION,
20
+ } from "./resolve";
15
21
 
16
22
  type Lookup = (name: string) => Definition | undefined;
17
23
 
@@ -37,12 +43,6 @@ const getBoundSets = (bounds: Bound[]) =>
37
43
  .filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
38
44
  .map((bound) => bound.set);
39
45
 
40
- const getOperandIndex = (operand: number | "<<" | ">>", count: number) => {
41
- if (typeof operand === "number") return operand >= 1 && operand <= count ? operand - 1 : -1;
42
- if (operand === "<<") return count > 0 ? 0 : -1;
43
- return count === 2 ? 1 : -1;
44
- };
45
-
46
46
  const getOperands = (expression: Expression): Expression[] | undefined => {
47
47
  switch (expression.kind) {
48
48
  case "Apply":
@@ -393,15 +393,22 @@ const containsReference = (expression: Expression): boolean =>
393
393
  (expression.kind === "Apply" && expression.operator.includes("!")) ||
394
394
  getChildren(expression).some(containsReference);
395
395
 
396
- const resolveSubexpressions = (expression: Expression, lookup: Lookup): Expression => {
396
+ // A reference into a theorem whose statement is an ASSUME/PROVE stays for name resolution to check.
397
+ const resolveSubexpressions = (
398
+ expression: Expression,
399
+ lookup: Lookup,
400
+ goals: ReadonlySet<string>,
401
+ ): Expression => {
397
402
  if (!containsReference(expression)) return expression;
398
403
  if (expression.kind === "Let") {
399
404
  const local: Lookup = (name) =>
400
405
  expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
401
- return mapChildren(expression, (child) => resolveSubexpressions(child, local));
406
+ return mapChildren(expression, (child) => resolveSubexpressions(child, local, goals));
402
407
  }
403
- const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, lookup));
408
+ const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, lookup, goals));
404
409
  if (mapped.kind === "Subexpression") {
410
+ const first = mapped.selectors[0];
411
+ if (first.kind === "Name" && goals.has(first.name)) return mapped;
405
412
  return selectSubexpression(mapped.selectors, lookup, mapped.location, []);
406
413
  }
407
414
  const name =
@@ -425,7 +432,7 @@ export const resolveInstanceSubexpressions = (instance: Instance, module: Module
425
432
  ...instance,
426
433
  substitutions: instance.substitutions.map((substitution) => ({
427
434
  name: substitution.name,
428
- expression: resolveSubexpressions(substitution.expression, lookup),
435
+ expression: resolveSubexpressions(substitution.expression, lookup, new Set()),
429
436
  })),
430
437
  };
431
438
  };
@@ -436,14 +443,19 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
436
443
  module.definitions.some((definition) => containsReference(definition.body)) ||
437
444
  module.assumptions.some((assumption) => containsReference(assumption.expression));
438
445
  if (!hasReferences) return module;
446
+ const goals = new Set(
447
+ module.theorems.flatMap((theorem) =>
448
+ theorem.name !== undefined && theorem.statement.kind === "AssumeProve" ? [theorem.name] : [],
449
+ ),
450
+ );
439
451
  return {
440
452
  ...module,
441
453
  definitions: module.definitions.map((definition) =>
442
- mapDefinition(definition, (child) => resolveSubexpressions(child, lookup)),
454
+ mapDefinition(definition, (child) => resolveSubexpressions(child, lookup, goals)),
443
455
  ),
444
456
  assumptions: module.assumptions.map((assumption) => ({
445
457
  ...assumption,
446
- expression: resolveSubexpressions(assumption.expression, lookup),
458
+ expression: resolveSubexpressions(assumption.expression, lookup, goals),
447
459
  })),
448
460
  };
449
461
  };