tla-ts 0.0.3 → 0.0.4-dev.0be9567

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 (86) hide show
  1. package/README.md +32 -12
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +162 -8
  6. package/dist/index.d.ts +162 -8
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +2 -2
  9. package/dist/parallel-worker.js +2 -2
  10. package/dist/parallel.cjs +1 -1
  11. package/dist/parallel.js +2 -2
  12. package/dist/run-cli.cjs +45 -18
  13. package/dist/run-cli.js +45 -18
  14. package/dist/transfer.cjs +1393 -18
  15. package/dist/transfer.js +1393 -18
  16. package/package.json +5 -1
  17. package/src/ast.ts +147 -6
  18. package/src/bags.ts +4 -7
  19. package/src/builtins.ts +29 -11
  20. package/src/bundled-modules.ts +3 -3
  21. package/src/canonical.ts +4 -3
  22. package/src/check.ts +1539 -235
  23. package/src/cli-options.ts +11 -5
  24. package/src/cli.ts +8 -1
  25. package/src/codegen.ts +98 -39
  26. package/src/config.ts +254 -181
  27. package/src/dependencies.ts +2 -2
  28. package/src/environment.ts +540 -140
  29. package/src/errors.ts +224 -3
  30. package/src/evaluate.ts +994 -279
  31. package/src/exit-codes.ts +22 -43
  32. package/src/fingerprints.ts +46 -11
  33. package/src/index.ts +1 -0
  34. package/src/instances.ts +19 -16
  35. package/src/java-overrides.ts +104 -25
  36. package/src/levels.ts +191 -45
  37. package/src/lexer.ts +127 -30
  38. package/src/live-check.ts +867 -0
  39. package/src/liveness.ts +218 -572
  40. package/src/obligation-expression.ts +2 -2
  41. package/src/obligation-source.ts +2 -0
  42. package/src/parallel-worker.ts +174 -213
  43. package/src/parallel.ts +131 -39
  44. package/src/parser.ts +456 -147
  45. package/src/pcal-ast.ts +770 -0
  46. package/src/pcal-parse.ts +1523 -0
  47. package/src/pcal-tokenize.ts +516 -0
  48. package/src/pcal-translate.ts +1230 -0
  49. package/src/pcal-validate.ts +454 -0
  50. package/src/randomization.ts +24 -7
  51. package/src/resolve.ts +1481 -426
  52. package/src/run-cli.ts +1212 -143
  53. package/src/runtime.ts +19 -1
  54. package/src/sany-builtins.ts +147 -0
  55. package/src/sany-generator.ts +3128 -0
  56. package/src/sany-levels.ts +1208 -0
  57. package/src/sany-lexer.ts +545 -0
  58. package/src/sany-linter.ts +122 -0
  59. package/src/sany-modules.ts +1398 -0
  60. package/src/sany-nodes.ts +463 -0
  61. package/src/sany-parse-error.ts +2161 -0
  62. package/src/sany-report.ts +356 -0
  63. package/src/sany-selectors.ts +738 -0
  64. package/src/sany-source.ts +248 -0
  65. package/src/sany-xml.ts +1017 -0
  66. package/src/spec-description.ts +42 -11
  67. package/src/standard-modules.ts +203 -148
  68. package/src/standard-ranges.ts +116 -0
  69. package/src/state-graph.ts +180 -0
  70. package/src/subexpressions.ts +231 -171
  71. package/src/substitution-levels.ts +38 -17
  72. package/src/successors.ts +220 -108
  73. package/src/symmetry.ts +194 -54
  74. package/src/tableau.ts +54 -25
  75. package/src/tlc-ext.ts +42 -29
  76. package/src/tlc-messages.ts +329 -0
  77. package/src/tlc-module.ts +52 -43
  78. package/src/tlc-modules.ts +4 -0
  79. package/src/tlc-order.ts +78 -10
  80. package/src/tlc-random.ts +37 -9
  81. package/src/tlc-replay.ts +655 -0
  82. package/src/tlc-summary.ts +20 -5
  83. package/src/tlc-version.ts +18 -0
  84. package/src/transfer.ts +15 -3
  85. package/src/value-sources.ts +29 -0
  86. package/src/values.ts +743 -189
@@ -0,0 +1,3128 @@
1
+ import { TEMPORAL_LEVEL } from "./levels";
2
+ import type { TlcVersion } from "./tlc-version";
3
+ import type {
4
+ AssumeProve,
5
+ Assumption,
6
+ Fact,
7
+ Bound,
8
+ Declaration,
9
+ Definition,
10
+ Expression,
11
+ FunctionDefinition,
12
+ Instance,
13
+ Module,
14
+ OperatorDefinition,
15
+ NewSymbol,
16
+ Proof,
17
+ ProofStep,
18
+ SourceLocation,
19
+ Theorem,
20
+ UseOrHide,
21
+ } from "./ast";
22
+ import { getChildren } from "./ast";
23
+ import {
24
+ TlaMissingModuleError,
25
+ TlaSanyFatalError,
26
+ TlaSyntaxError,
27
+ TlaUnsupportedError,
28
+ } from "./errors";
29
+ import { parseSanyModule } from "./parser";
30
+ import { BUILTIN_LOCATION, createBuiltins, createLevelFields } from "./sany-builtins";
31
+ import { getSany174LexicalError } from "./sany-lexer";
32
+ import { getSanyParseError } from "./sany-parse-error";
33
+ import {
34
+ addSanyMessage,
35
+ CONSTANT_DECL_KIND,
36
+ NEW_CONSTANT_KIND,
37
+ VARIABLE_DECL_KIND,
38
+ addToContext,
39
+ createContext,
40
+ getContextModule,
41
+ formatSanyLocation,
42
+ getContextSymbol,
43
+ getSymbolArity,
44
+ getDeclarations,
45
+ getHashtableValues,
46
+ isAbsent,
47
+ type AssumeNode,
48
+ type AssumeProveNode,
49
+ type Context,
50
+ type DefStepNode,
51
+ type ExprNode,
52
+ type ExprOrOpArgNode,
53
+ type FactNode,
54
+ type FormalParamNode,
55
+ type InstanceNode,
56
+ type LabelNode,
57
+ type LetInNode,
58
+ type LeafProofNode,
59
+ type LevelNode,
60
+ type ModuleNode,
61
+ type NewSymbNode,
62
+ type NonLeafProofNode,
63
+ type OpApplNode,
64
+ type OpArgNode,
65
+ type OpDeclNode,
66
+ type OpDefNode,
67
+ type ProofStepNode,
68
+ type SanyLocation,
69
+ type SanyMessage,
70
+ type Subst,
71
+ type SubstInNode,
72
+ type SymbolNode,
73
+ type TheoremNode,
74
+ type ThmOrAssumpDefNode,
75
+ type UseOrHideNode,
76
+ } from "./sany-nodes";
77
+ import {
78
+ createSelectorResolver,
79
+ getSelectorItems,
80
+ isGeneralId,
81
+ type SelectorItem,
82
+ } from "./sany-selectors";
83
+ import {
84
+ createSourceFile,
85
+ findNameTokens,
86
+ findToken,
87
+ getArgumentSpan,
88
+ getExpressionSpan,
89
+ getMatchingIndex,
90
+ getParenthesizedSpan,
91
+ getPreComments,
92
+ getRangeSpan,
93
+ getSpanLocation,
94
+ getTokenIndex,
95
+ getTokenLocation,
96
+ getTokenSource,
97
+ getUnboxedSource,
98
+ isBoxToken,
99
+ isToken,
100
+ type SourceFile,
101
+ type TokenSpan,
102
+ } from "./sany-source";
103
+ import { SANY_OPERATOR_SYNONYMS } from "./tlc-modules";
104
+
105
+ const SYNONYMS = new Map(
106
+ SANY_OPERATOR_SYNONYMS.flatMap(([canonical, ...synonyms]) =>
107
+ synonyms.map((synonym) => [synonym, canonical]),
108
+ ),
109
+ );
110
+
111
+ // tla2sany.parser.Operators.resolveSynonym, applied to each part of a name like I!\cup.
112
+ const resolveSynonym = (name: string) =>
113
+ name
114
+ .split("!")
115
+ .map((part) => SYNONYMS.get(part) ?? (part === "~" ? "\\lnot" : part))
116
+ .join("!");
117
+
118
+ // The fields of SANY's AssumeProveNode that tell whether a reference to a clause lies in the scope
119
+ // of a declaration in the clauses before it. See AssumeProveNode.java.
120
+ interface AssumeProveScope {
121
+ // AssumeProveNode.inScopeOfDecl: whether any of the first i clauses is a NEW.
122
+ isDeclaredBefore: boolean[];
123
+ // Whether the ASSUME/PROVE is the statement of a named theorem or step, whose suffices and
124
+ // inProof fields decide where its declarations are visible.
125
+ hasGoal: boolean;
126
+ isSuffices: boolean;
127
+ isInProof: boolean;
128
+ }
129
+
130
+ // Where the generator is among nested ASSUME/PROVEs: the depth, whether each level declares a
131
+ // symbol, and the goal and clause a label lies in.
132
+ interface AssumeProveState {
133
+ depth: number;
134
+ isDeclared: boolean[];
135
+ goal: AssumeProveScope | undefined;
136
+ goalClause: number;
137
+ }
138
+
139
+ interface LabelGoal {
140
+ scope: AssumeProveScope;
141
+ clause: number;
142
+ }
143
+
144
+ const NEW_SYMBOL_KINDS = {
145
+ Constant: { declKind: NEW_CONSTANT_KIND, level: 0 },
146
+ Variable: { declKind: 25, level: 1 },
147
+ State: { declKind: 26, level: 1 },
148
+ Action: { declKind: 27, level: 2 },
149
+ Temporal: { declKind: 28, level: 3 },
150
+ };
151
+
152
+ // Whether SANY names an operator by an identifier rather than by a symbol.
153
+ const isIdentifier = (name: string) => /^[A-Za-z0-9_]+$/.test(name);
154
+
155
+ // Whether an operator name selects through an instance, as M!Op does, unlike the infix !!.
156
+ const isSelector = (name: string) => name !== "!!" && name.includes("!");
157
+
158
+ export interface SanySpecification {
159
+ root: ModuleNode;
160
+ modules: ModuleNode[];
161
+ messages: SanyMessage[];
162
+ }
163
+
164
+ export interface SanyGenerationOptions {
165
+ // The name of the root module's file, which SANY requires to match the module's name.
166
+ rootFileName?: string;
167
+ // The path of the root module's file as SANY's caller gives it, which SANY names when it cannot
168
+ // parse the file, rather than the file name it gives the other modules.
169
+ rootFilePath?: string;
170
+ // Called before SANY parses the file of each module, and before it analyzes each external
171
+ // module, in its order.
172
+ onParse?: (name: string) => void;
173
+ onAnalyze?: (name: string) => void;
174
+ // Called after each external module, in the order SANY analyzes them.
175
+ onModule?: (module: ModuleNode, messages: SanyMessage[]) => void;
176
+ // The TLC whose SANY to reproduce, which decides how SANY numbers the nodes it creates.
177
+ version?: TlcVersion;
178
+ // The modules TLC adds to the root module's EXTENDS for its post conditions, which SANY loads
179
+ // after the ones the root module itself extends.
180
+ rootExtendees?: string[];
181
+ }
182
+
183
+ interface SymbolTable {
184
+ contexts: Context[];
185
+ }
186
+
187
+ interface BoundSymbols {
188
+ bounded: FormalParamNode[][];
189
+ tupleOrs: boolean[];
190
+ ranges: ExprNode[];
191
+ }
192
+
193
+ interface OpApplBinding {
194
+ unbounded?: FormalParamNode[];
195
+ bounded?: FormalParamNode[][];
196
+ tupleOrs?: boolean[];
197
+ ranges?: ExprNode[];
198
+ }
199
+
200
+ // A module name that SpecObj.loadSpec has yet to resolve, with the module that imports it and the
201
+ // module of that file whose EXTENDS or INSTANCE names it.
202
+ interface UnresolvedName {
203
+ name: string;
204
+ importer: string;
205
+ namingModule: string;
206
+ }
207
+
208
+ const getTopContext = (table: SymbolTable) => table.contexts[table.contexts.length - 1];
209
+
210
+ const resolveSymbol = (table: SymbolTable, name: string) => {
211
+ for (let index = table.contexts.length - 1; index >= 0; index--) {
212
+ const symbol = getContextSymbol(table.contexts[index], name);
213
+ if (symbol) return symbol;
214
+ }
215
+ return undefined;
216
+ };
217
+
218
+ // SANY's kind of a symbol, which a symbol that takes another's name must share.
219
+ const getSymbolKind = (symbol: SymbolNode) =>
220
+ symbol.kind === "OpDecl"
221
+ ? `${symbol.kind}${symbol.declKind}`
222
+ : symbol.kind === "OpDef"
223
+ ? symbol.defKind
224
+ : symbol.kind;
225
+
226
+ // Context.kindOfNode.
227
+ const getKindName = (symbol: SymbolNode) =>
228
+ symbol.kind === "OpDef" || symbol.kind === "FormalParam" ? "definition" : "declaration";
229
+
230
+ // SymbolNode.sameOriginallyDefinedInModule: two symbols are the same definition of a module without
231
+ // parameters.
232
+ const isSameOrigin = (symbol: SymbolNode, other: SymbolNode) => {
233
+ if (symbol.kind === "OpDef" && other.kind === "OpDef") {
234
+ const source = symbol.source ?? symbol;
235
+ if (source !== (other.source ?? other)) return false;
236
+ return source.module === undefined || isParameterFree(source.module);
237
+ }
238
+ if (symbol.kind === "ThmOrAssumpDef" && other.kind === "ThmOrAssumpDef") {
239
+ const source = symbol.source ?? symbol;
240
+ return source === (other.source ?? other) && isParameterFree(source.module);
241
+ }
242
+ return false;
243
+ };
244
+
245
+ const isParameterFree = (module: ModuleNode) =>
246
+ getDeclarations(module, CONSTANT_DECL_KIND).length === 0 &&
247
+ getDeclarations(module, VARIABLE_DECL_KIND).length === 0;
248
+
249
+ const getLetInstances = (expression: Expression): Instance[] => [
250
+ ...(expression.kind === "Let" ? (expression.instances ?? []) : []),
251
+ ...getChildren(expression).flatMap(getLetInstances),
252
+ ];
253
+
254
+ // The LET instances of a theorem or assumption's statement.
255
+ const getStatementInstances = (statement: Expression | AssumeProve): Instance[] =>
256
+ statement.kind === "AssumeProve"
257
+ ? [...statement.assumptions, statement.conclusion].flatMap((part) =>
258
+ part.kind === "NewSymbol" ? [] : getStatementInstances(part),
259
+ )
260
+ : getLetInstances(statement);
261
+
262
+ // SpecObj.instanceResolvesToInternalModule: SANY finds a module that the root module instantiates
263
+ // among its inner modules only when its first top-level INSTANCE of the module follows the inner
264
+ // module, and otherwise looks for a file.
265
+ const isInternalInstance = (module: Module, name: string) => {
266
+ const seen = new Set<string>();
267
+ for (const unit of module.units) {
268
+ if (unit.kind === "Module") seen.add(unit.module.name);
269
+ if (unit.kind === "Instance" && unit.instance.module === name) return seen.has(name);
270
+ }
271
+ return false;
272
+ };
273
+
274
+ // An entry of SANY's LS stack: the labels of a definition, theorem or label and the parameters
275
+ // bound within it around the expression being generated.
276
+ interface LabelScope {
277
+ labels: Map<string, LabelNode>;
278
+ params: FormalParamNode[][];
279
+ }
280
+
281
+ // The modules a file extends and instantiates, as SANY's ParseUnit collects them, and the module of
282
+ // the file that names each one. SANY looks at a module's own EXTENDS and INSTANCEs before those of
283
+ // its inner modules.
284
+ const getDependencies = (module: Module) => {
285
+ const extendees: string[] = [];
286
+ const instancees: string[] = [];
287
+ const importers = new Map<string, string>();
288
+ // ParseUnit.calculateContextWithinParseUnit: a module knows the inner modules defined before it in
289
+ // the modules enclosing it and the inner modules of the modules it extends, but not itself.
290
+ const visit = (current: Module, context: Map<string, Module>) => {
291
+ const isRoot = current === module;
292
+ const addImports = (names: string[], imports: string[]) =>
293
+ names.forEach((name) => {
294
+ imports.push(name);
295
+ if (!importers.has(name)) importers.set(name, current.name);
296
+ });
297
+ const isExternal = (name: string) => !context.has(name);
298
+ addImports(current.extends.filter(isExternal), extendees);
299
+ const instances = current.units.flatMap((unit) => {
300
+ if (unit.kind === "Instance") return [unit.instance];
301
+ if (unit.kind === "Definition") return getLetInstances(unit.definition.body);
302
+ if (unit.kind === "Theorem" && isRoot) return getStatementInstances(unit.theorem.statement);
303
+ if (unit.kind === "Assumption" && isRoot) return getLetInstances(unit.assumption.expression);
304
+ return [];
305
+ });
306
+ addImports(
307
+ instances
308
+ .map((instance) => instance.module)
309
+ .filter((name) => isExternal(name) && !isInternalInstance(current, name)),
310
+ instancees,
311
+ );
312
+ const peers = new Map(context);
313
+ current.modules.forEach((inner) => {
314
+ const innerContext = new Map(peers);
315
+ // ParseUnit.handleExtensions.
316
+ const extenders = new Set<Module>();
317
+ const addExtendedInnerModules = (extender: Module) => {
318
+ if (extenders.has(extender)) return;
319
+ extenders.add(extender);
320
+ extender.extends.forEach((name) => {
321
+ const extendee = innerContext.get(name);
322
+ if (extendee === undefined) return;
323
+ extendee.modules.forEach((extendeeInner) => {
324
+ if (!innerContext.has(extendeeInner.name)) {
325
+ innerContext.set(extendeeInner.name, extendeeInner);
326
+ }
327
+ });
328
+ addExtendedInnerModules(extendee);
329
+ });
330
+ };
331
+ addExtendedInnerModules(inner);
332
+ visit(inner, innerContext);
333
+ peers.set(inner.name, inner);
334
+ });
335
+ };
336
+ visit(module, new Map());
337
+ return { extendees, instancees, importers };
338
+ };
339
+
340
+ // The expressions that can name an operator, as Generator.generateExprOrOpArg tells them apart.
341
+ const OPERATOR_ARGUMENT_KINDS = new Set<Expression["kind"]>([
342
+ "Identifier",
343
+ "Subexpression",
344
+ "OperatorReference",
345
+ "Lambda",
346
+ ]);
347
+
348
+ // A name such as K(1)!C, whose last part takes no arguments, is a GeneralId too.
349
+ const isOperatorArgument = (argument: Expression) => {
350
+ const items = argument.kind === "Apply" ? getSelectorItems(argument) : undefined;
351
+ return OPERATOR_ARGUMENT_KINDS.has(argument.kind) || (items !== undefined && isGeneralId(items));
352
+ };
353
+
354
+ // The parts of a DEF clause entry that isn't a name, as Generator.genIdToSelector reads them.
355
+ interface DefEntryParts {
356
+ hasChildlessFirstChild: boolean | undefined;
357
+ last: TokenSpan;
358
+ // The kind of SANY's syntax node for the last part, or for a token its text.
359
+ lastKind: string | undefined;
360
+ }
361
+
362
+ // The kind of the syntax node SANY's parser builds for an expression of each kind.
363
+ const SYNTAX_KINDS = new Map<Expression["kind"], string>([
364
+ ["Identifier", "N_GeneralId"],
365
+ ["Subexpression", "N_GeneralId"],
366
+ ["Number", "N_Number"],
367
+ ["Decimal", "N_Real"],
368
+ ["SetEnumeration", "N_SetEnumerate"],
369
+ ["SetFilter", "N_SubsetOf"],
370
+ ["SetMap", "N_SetOfAll"],
371
+ ["Tuple", "N_Tuple"],
372
+ ["FunctionApplication", "N_FcnAppl"],
373
+ ["FunctionConstruction", "N_FcnConst"],
374
+ ["FunctionSet", "N_SetOfFcns"],
375
+ ["Record", "N_RcdConstructor"],
376
+ ["RecordSet", "N_SetOfRcds"],
377
+ ["FieldAccess", "N_RecordComponent"],
378
+ ["Except", "N_Except"],
379
+ ["Prime", "N_PostfixExpr"],
380
+ ["Always", "N_PrefixExpr"],
381
+ ["Eventually", "N_PrefixExpr"],
382
+ ["Enabled", "N_PrefixExpr"],
383
+ ["Unchanged", "N_PrefixExpr"],
384
+ ["Forall", "N_BoundedQuant"],
385
+ ["Exists", "N_BoundedQuant"],
386
+ ["UnboundedQuantifier", "N_UnboundedQuant"],
387
+ ["Choose", "N_UnBoundedOrBoundedChoose"],
388
+ ["If", "N_IfThenElse"],
389
+ ["Case", "N_Case"],
390
+ ["Let", "N_LetIn"],
391
+ ["BoxAction", "N_ActionExpr"],
392
+ ["AngleAction", "N_ActionExpr"],
393
+ ["WeakFairness", "N_FairnessExpr"],
394
+ ["StrongFairness", "N_FairnessExpr"],
395
+ ]);
396
+
397
+ // The syntax nodes whose first child is a node with children of its own. The first child of any
398
+ // other is a token, or a node without children.
399
+ const NESTED_FIRST_CHILD_KINDS = new Set([
400
+ "N_GeneralId",
401
+ "N_OpApplication",
402
+ "N_PrefixExpr",
403
+ "N_InfixExpr",
404
+ "N_PostfixExpr",
405
+ "N_FcnAppl",
406
+ "N_RecordComponent",
407
+ ]);
408
+
409
+ const POSTFIX_OPERATOR_TEXTS = new Set(["'", "^+", "^*", "^#"]);
410
+
411
+ // SANY's syntax node for an operator that appears where an expression is required. The parser reads
412
+ // a prefix operator other than ~ and -. as a LAMBDA.
413
+ const getOperatorNodeKind = (
414
+ expression: Expression & { kind: "OperatorReference" | "Lambda" },
415
+ text: string,
416
+ ) => {
417
+ if (POSTFIX_OPERATOR_TEXTS.has(text)) return "N_GenPostfixOp";
418
+ return expression.kind === "Lambda" || text === "~" || text === "-."
419
+ ? "N_GenNonExpPrefixOp"
420
+ : "N_GenInfixOp";
421
+ };
422
+
423
+ // Builds the semantic graph SANY builds for a specification and its modules, in the order SANY
424
+ // analyzes them: a module after the modules it extends and instantiates.
425
+ export const generateSanySpecification = (
426
+ rootSource: string,
427
+ loadModule: (name: string) => string | undefined,
428
+ options: SanyGenerationOptions = {},
429
+ ): SanySpecification => {
430
+ const messages: SanyMessage[] = [];
431
+ const addError = (location: SanyLocation | string | undefined, text: string) =>
432
+ addSanyMessage(messages, { location, text, isWarning: false });
433
+ const addWarning = (location: SanyLocation | undefined, text: string) =>
434
+ addSanyMessage(messages, { location, text, isWarning: true });
435
+ // TLC 1.7.4's SANY numbers its first node 0, and the master builds create one before.
436
+ const counter = { value: options.version === "1.7.4" ? -1 : 0 };
437
+ const getId = () => ++counter.value;
438
+ // The UID of the nullOAN of the Generator of the module being generated.
439
+ const nullApplication = { id: 0 };
440
+
441
+ // SymbolTable.addSymbol: a symbol joins the innermost context unless its name is taken, which is
442
+ // an error unless the symbols agree in kind and arity, and then a warning unless they are the
443
+ // same definition of a module without parameters.
444
+ const addSymbol = (table: SymbolTable, symbol: SymbolNode) => {
445
+ const binding = resolveSymbol(table, symbol.name);
446
+ if (binding === symbol) return;
447
+ if (binding === undefined) {
448
+ addToContext(getTopContext(table), symbol);
449
+ return;
450
+ }
451
+ const isConflict =
452
+ symbol.kind === "FormalParam" ||
453
+ getSymbolKind(binding) !== getSymbolKind(symbol) ||
454
+ getSymbolArity(binding) !== getSymbolArity(symbol);
455
+ // TLC 1.7.4's SANY reports a conflict before the redefinition of a built-in operator.
456
+ const isBuiltIn = binding.kind === "OpDef" && binding.defKind === "BuiltIn";
457
+ if (isBuiltIn && !(isConflict && options.version === "1.7.4")) {
458
+ addError(
459
+ symbol.location,
460
+ `Symbol ${symbol.name} is a built-in operator, and cannot be redefined.`,
461
+ );
462
+ return;
463
+ }
464
+ if (isConflict) {
465
+ addError(
466
+ symbol.location,
467
+ `Multiply-defined symbol '${symbol.name}': this definition or declaration conflicts \nwith the one at ${formatSanyLocation(binding.location)}.`,
468
+ );
469
+ } else if (!isSameOrigin(symbol, binding)) {
470
+ addWarning(
471
+ symbol.location,
472
+ `Multiple declarations or definitions for symbol ${symbol.name}. \nThis duplicates the one at ${formatSanyLocation(binding.location)}.`,
473
+ );
474
+ }
475
+ };
476
+ // SANY numbers its semantic nodes as it creates them, which the UIDs of its XML export show:
477
+ // Context's static initializer creates the builtins, SANY.frontEndInitialize creates them again
478
+ // for the specification, and the Generator class creates its InAssumeDummyNode. TLC 1.7.4's SANY
479
+ // creates the builtins only for the specification.
480
+ if (options.version !== "1.7.4") createBuiltins(getId);
481
+ const builtins = createBuiltins(getId);
482
+ getId();
483
+ const builtinsByName = new Map(builtins.map((builtin) => [builtin.name, builtin]));
484
+ const externalModules = new Map<string, ModuleNode>();
485
+ const sources = new Map<string, { file: SourceFile; module: Module }>();
486
+
487
+ const parseSource = (name: string, text: string) => {
488
+ const file = (name === options.rootFileName && options.rootFilePath) || `${name}.tla`;
489
+ const getParseFailure = (parseError: string) =>
490
+ new TlaSanyFatalError(`Could not parse module ${name} from file ${file}`, name, parseError);
491
+ const lexicalError = options.version === "1.7.4" ? getSany174LexicalError(text) : undefined;
492
+ if (lexicalError !== undefined) throw getParseFailure(lexicalError);
493
+ // HACK: the parser's errors are not SANY's, so a module that fails to parse gets the report
494
+ // of SANY's parser, and the parser's own report where SANY's parser would accept the module,
495
+ // such as for an error SANY finds after parsing.
496
+ try {
497
+ return parseSanyModule(getUnboxedSource(text));
498
+ } catch (error) {
499
+ if (!(error instanceof TlaSyntaxError)) throw error;
500
+ throw getParseFailure(getSanyParseError(text) ?? `***Parse Error***\n${error.message}`);
501
+ }
502
+ };
503
+
504
+ const getSource = (name: string, text: string | undefined, importer?: string) => {
505
+ const cached = sources.get(name);
506
+ if (cached) return cached;
507
+ if (text === undefined) {
508
+ throw new TlaMissingModuleError(name, importer, options.version !== "1.7.4");
509
+ }
510
+ options.onParse?.(name);
511
+ const module = parseSource(name, text);
512
+ if (module.name !== name) {
513
+ throw new TlaSanyFatalError(
514
+ `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
515
+ undefined,
516
+ );
517
+ }
518
+ const entry = { file: createSourceFile(module.name, text), module };
519
+ sources.set(name, entry);
520
+ return entry;
521
+ };
522
+
523
+ const order: string[] = [];
524
+ const visiting = new Set<string>();
525
+ const path: string[] = [];
526
+ const calculateDependencies = (name: string, text: string | undefined, importer?: string) => {
527
+ // SpecObj.nonCircularityTest.
528
+ const cycleStart = path.indexOf(name);
529
+ if (cycleStart !== -1) {
530
+ throw new TlaSanyFatalError(
531
+ `Circular dependency among .tla files; dependency cycle is:\n\n ${[...path.slice(cycleStart), name].map((file) => `${file}.tla`).join(" --> ")}`,
532
+ undefined,
533
+ );
534
+ }
535
+ if (visiting.has(name)) return;
536
+ visiting.add(name);
537
+ path.push(name);
538
+ const dependencies = getModuleDependencies(name, getSource(name, text, importer).module);
539
+ [...dependencies.extendees, ...dependencies.instancees].forEach((dependency) =>
540
+ calculateDependencies(dependency, loadModule(dependency), name),
541
+ );
542
+ path.pop();
543
+ order.push(name);
544
+ };
545
+ // SpecObj.loadSpec: SANY resolves the first name it finds unresolved searching from the root,
546
+ // each module's extended modules before the modules they import, and the instantiated modules
547
+ // only once it has resolved every extended module. SANY resolves a name in the context of the
548
+ // module that names it, so a module another module loaded is unresolved until SANY reaches it
549
+ // from this one.
550
+ const resolutions = new Set<string>();
551
+ const isResolved = (importer: string, name: string) => resolutions.has(`${importer} ${name}`);
552
+ const findUnresolved = (
553
+ name: string,
554
+ kind: "extendees" | "instancees",
555
+ visited: Set<string>,
556
+ ): UnresolvedName | undefined => {
557
+ const entry = sources.get(name);
558
+ if (!entry || visited.has(name)) return undefined;
559
+ visited.add(name);
560
+ const dependencies = getModuleDependencies(name, entry.module);
561
+ const unresolved = dependencies[kind].find((dependency) => !isResolved(name, dependency));
562
+ if (unresolved !== undefined) {
563
+ const namingModule = dependencies.importers.get(unresolved) ?? name;
564
+ return { name: unresolved, importer: name, namingModule };
565
+ }
566
+ for (const dependency of [...dependencies.extendees, ...dependencies.instancees]) {
567
+ const found = isResolved(name, dependency)
568
+ ? findUnresolved(dependency, kind, visited)
569
+ : undefined;
570
+ if (found) return found;
571
+ }
572
+ return undefined;
573
+ };
574
+ const loadSpecification = (rootName: string) => {
575
+ getSource(rootName, rootSource);
576
+ const findNext = () =>
577
+ findUnresolved(rootName, "extendees", new Set()) ??
578
+ findUnresolved(rootName, "instancees", new Set());
579
+ for (let next = findNext(); next !== undefined; next = findNext()) {
580
+ if (!sources.has(next.name)) {
581
+ getSource(next.name, loadModule(next.name), next.namingModule);
582
+ }
583
+ resolutions.add(`${next.importer} ${next.name}`);
584
+ }
585
+ };
586
+ const rootName = options.rootFileName ?? parseSource("", rootSource).name;
587
+ // ParameterizedSpecObj.findOrCreateParsedUnit adds the post conditions' modules to the root
588
+ // module's own extendees, before those of its inner modules.
589
+ const getModuleDependencies = (name: string, module: Module) => {
590
+ const dependencies = getDependencies(module);
591
+ if (name !== rootName || options.rootExtendees === undefined) return dependencies;
592
+ dependencies.extendees.splice(module.extends.length, 0, ...options.rootExtendees);
593
+ options.rootExtendees.forEach((extendee) => {
594
+ if (!dependencies.importers.has(extendee)) dependencies.importers.set(extendee, name);
595
+ });
596
+ return dependencies;
597
+ };
598
+ loadSpecification(rootName);
599
+ calculateDependencies(rootName, rootSource);
600
+
601
+ const generateModule = (
602
+ ast: Module,
603
+ file: SourceFile,
604
+ parent: ModuleNode | undefined,
605
+ symbolTable: SymbolTable,
606
+ ): ModuleNode => {
607
+ const state = {
608
+ curLevel: 0,
609
+ unresolvedCount: [0],
610
+ unresolvedSum: 0,
611
+ recursiveSectionCount: 0,
612
+ };
613
+ const functions: { name: string; node: OpApplNode }[] = [];
614
+ const exceptStack: OpApplNode[] = [];
615
+ const exceptSpecStack: OpApplNode[] = [];
616
+ const labelScopes: LabelScope[] = [];
617
+ // The contexts in which SANY's $$InAssume marks the scope of an ordinary ASSUME's assumptions.
618
+ const assumeContexts = new WeakSet<Context>();
619
+ const pushLabelScope = () => labelScopes.push({ labels: new Map(), params: [] });
620
+ const popLabels = () => labelScopes.pop()?.labels ?? new Map<string, LabelNode>();
621
+ // Generator.assumeProveDepth, inScopeOfAPDecl, currentGoal and currentGoalClause: where the
622
+ // node being generated lies among the ASSUME/PROVEs of a theorem or step.
623
+ const assumeProveState: AssumeProveState = {
624
+ depth: 0,
625
+ isDeclared: [],
626
+ goal: undefined,
627
+ goalClause: 0,
628
+ };
629
+ const assumeProveScopes = new WeakMap<AssumeProveNode, AssumeProveScope>();
630
+ const labelGoals = new WeakMap<LabelNode, LabelGoal>();
631
+ const endProof = (statement: LevelNode) => {
632
+ const scope = statement.kind === "AssumeProve" ? assumeProveScopes.get(statement) : undefined;
633
+ if (scope) scope.isInProof = false;
634
+ };
635
+
636
+ const getLocation = (expression: Expression) =>
637
+ getSpanLocation(file, getExpressionSpan(file, expression));
638
+
639
+ const getUnitSpan = (location: SourceLocation) =>
640
+ location.range
641
+ ? getRangeSpan(file, location.range)
642
+ : { start: getTokenIndex(file, location), end: getTokenIndex(file, location) };
643
+
644
+ const currentModule: ModuleNode = {
645
+ kind: "Module",
646
+ id: getId(),
647
+ name: ast.name,
648
+ location: getSpanLocation(file, getUnitSpan(ast.location)),
649
+ extendees: [],
650
+ context: getTopContext(symbolTable),
651
+ topLevel: [],
652
+ definitions: [],
653
+ opDefsInRecursiveSection: [],
654
+ isLevelChecked: false,
655
+ levelCorrect: true,
656
+ argLevelParams: new Map(),
657
+ levelConstraints: new Map(),
658
+ argLevelConstraints: new Map(),
659
+ records: [],
660
+ };
661
+ if (parent) parent.definitions.push(currentModule);
662
+
663
+ const resolveModule = (name: string) => {
664
+ for (let index = symbolTable.contexts.length - 1; index >= 0; index--) {
665
+ const module = getContextModule(symbolTable.contexts[index], name);
666
+ if (module?.kind === "Module") return module;
667
+ }
668
+ return externalModules.get(name);
669
+ };
670
+
671
+ const getModule = (name: string) => {
672
+ const module = resolveModule(name);
673
+ if (module === undefined) throw new TlaSyntaxError(`Could not find module ${name}`);
674
+ return module;
675
+ };
676
+
677
+ const withContext = <Result>(context: Context, generate: () => Result) => {
678
+ symbolTable.contexts.push(context);
679
+ const result = generate();
680
+ symbolTable.contexts.pop();
681
+ return result;
682
+ };
683
+
684
+ const createFormalParam = (
685
+ name: string,
686
+ arity: number,
687
+ location: SanyLocation | undefined,
688
+ isAdded: boolean,
689
+ ): FormalParamNode => {
690
+ const param: FormalParamNode = {
691
+ ...createLevelFields(getId()),
692
+ kind: "FormalParam",
693
+ name,
694
+ arity,
695
+ location,
696
+ };
697
+ if (isAdded) addSymbol(symbolTable, param);
698
+ return param;
699
+ };
700
+
701
+ const createOpDef = (
702
+ fields: Partial<OpDefNode> & Pick<OpDefNode, "name" | "location">,
703
+ ): OpDefNode => ({
704
+ ...createLevelFields(getId()),
705
+ kind: "OpDef",
706
+ defKind: "UserDefined",
707
+ arity: fields.params?.length ?? 0,
708
+ params: [],
709
+ body: undefined,
710
+ isLocal: false,
711
+ module: currentModule,
712
+ source: undefined,
713
+ preComments: [],
714
+ isDefined: true,
715
+ inRecursive: false,
716
+ inRecursiveSection: false,
717
+ recursiveSection: -1,
718
+ letInLevel: 0,
719
+ weights: (fields.params ?? []).map(() => 0),
720
+ isLeibnizArg: (fields.params ?? []).map(() => true),
721
+ isLeibniz: true,
722
+ opLevelCond: undefined,
723
+ maxLevels: (fields.params ?? []).map(() => TEMPORAL_LEVEL),
724
+ minMaxLevel: undefined,
725
+ labels: new Map(),
726
+ step: undefined,
727
+ ...fields,
728
+ });
729
+
730
+ const setRecursionFields = (definition: OpDefNode) => {
731
+ definition.letInLevel = state.curLevel;
732
+ if (state.unresolvedSum > 0) {
733
+ definition.recursiveSection = state.recursiveSectionCount;
734
+ definition.inRecursiveSection = state.unresolvedCount[state.curLevel] > 0;
735
+ currentModule.opDefsInRecursiveSection.push(definition);
736
+ }
737
+ };
738
+
739
+ const createOpAppl = (
740
+ operator: SymbolNode,
741
+ operands: (ExprOrOpArgNode | undefined)[],
742
+ location: SanyLocation,
743
+ binding: OpApplBinding = {},
744
+ id = getId(),
745
+ ): OpApplNode => ({
746
+ ...createLevelFields(id),
747
+ kind: "OpAppl",
748
+ operator,
749
+ operands,
750
+ unbounded: binding.unbounded,
751
+ bounded: binding.bounded,
752
+ tupleOrs: binding.tupleOrs,
753
+ ranges: binding.ranges ?? [],
754
+ location,
755
+ });
756
+
757
+ // Generator.nullOAN, which SANY returns where it reported an error and carries on. It has no
758
+ // syntax tree node, so its location is that of the builtins.
759
+ const createNullNode = (): OpApplNode => ({
760
+ ...createOpAppl(getBuiltin("$Nop"), [], BUILTIN_LOCATION, {}, nullApplication.id),
761
+ isNull: true,
762
+ });
763
+
764
+ const getBuiltin = (name: string) => {
765
+ const builtin = builtinsByName.get(name);
766
+ if (!builtin) throw new TlaUnsupportedError(`SANY has no builtin operator ${name}`);
767
+ return builtin;
768
+ };
769
+
770
+ const applyBuiltin = (
771
+ name: string,
772
+ operands: (ExprOrOpArgNode | undefined)[],
773
+ location: SanyLocation,
774
+ binding: OpApplBinding = {},
775
+ ) => createOpAppl(getBuiltin(name), operands, location, binding);
776
+
777
+ const createString = (value: string, location: SanyLocation): ExprNode => ({
778
+ ...createLevelFields(getId()),
779
+ kind: "String",
780
+ value,
781
+ location,
782
+ });
783
+
784
+ const createOpArg = (op: SymbolNode, location: SanyLocation): OpArgNode => ({
785
+ ...createLevelFields(getId()),
786
+ kind: "OpArg",
787
+ op,
788
+ location,
789
+ });
790
+
791
+ const resolveName = (name: string, location: SanyLocation) => {
792
+ const symbol = resolveSymbol(symbolTable, resolveSynonym(name));
793
+ if (symbol === undefined || symbol.kind === "Module") {
794
+ throw new TlaSyntaxError(
795
+ `Unknown operator: \`${name}' at line ${location.beginLine}, column ${location.beginColumn} of module ${location.file}`,
796
+ );
797
+ }
798
+ return symbol;
799
+ };
800
+
801
+ // OpDefNode.match: a definition takes as many arguments as it has parameters, which differs
802
+ // from its arity only where its RECURSIVE declaration has another, and an operator it takes
803
+ // must be given an operator of its arity that takes no operators.
804
+ const matchArguments = (
805
+ operator: SymbolNode,
806
+ operands: ExprOrOpArgNode[],
807
+ location: SanyLocation,
808
+ ) => {
809
+ if (operator.kind !== "OpDef" || operator.defKind !== "UserDefined") return;
810
+ const params = operator.params ?? [];
811
+ if (params.length !== operands.length) {
812
+ addError(
813
+ location,
814
+ `Wrong number of arguments (${operands.length}) given to operator '${operator.name}', \nwhich requires ${params.length} arguments.`,
815
+ );
816
+ return;
817
+ }
818
+ params.forEach((param, index) => {
819
+ const operand = operands[index];
820
+ if (param.arity === 0 && operand.kind === "OpArg") {
821
+ addError(
822
+ location,
823
+ `Operator used in argument number ${index + 1} has incorrect number of arguments.`,
824
+ );
825
+ }
826
+ const isMatching =
827
+ operand.kind === "OpArg" &&
828
+ getSymbolArity(operand.op) === param.arity &&
829
+ !(
830
+ operand.op.kind === "OpDef" &&
831
+ (operand.op.params ?? []).some((parameter) => parameter.arity > 0)
832
+ );
833
+ if (param.arity > 0 && !isMatching) {
834
+ addError(
835
+ location,
836
+ `Argument number ${index + 1} to operator '${operator.name}' \nshould be a ${param.arity}-parameter operator.`,
837
+ );
838
+ }
839
+ });
840
+ };
841
+
842
+ const selectorToNode = createSelectorResolver({
843
+ version: options.version,
844
+ resolveSymbol: (name) => resolveSymbol(symbolTable, name),
845
+ resolveSynonym,
846
+ generateExpression: (expression) => generateExpression(expression),
847
+ generateArgument: (operator, index, argument, location) =>
848
+ generateArgument(operator, index, argument, location),
849
+ createOpAppl: (operator, operands, location) => {
850
+ const node = createOpAppl(operator, operands, location);
851
+ matchArguments(operator, operands, location);
852
+ return node;
853
+ },
854
+ createOpArg,
855
+ createFormalParam: (name, arity, location) => createFormalParam(name, arity, location, false),
856
+ createLambda: (params, body, location) =>
857
+ createOpDef({
858
+ name: "LAMBDA",
859
+ params,
860
+ body,
861
+ location,
862
+ preComments: getPreComments(
863
+ file,
864
+ getTokenIndex(file, { line: location.beginLine, column: location.beginColumn }),
865
+ ),
866
+ }),
867
+ copySubstIn: (prefix, body) =>
868
+ createSubstIn(
869
+ "SubstIn",
870
+ prefix.substs,
871
+ body,
872
+ prefix.instantiatedModule,
873
+ prefix.location,
874
+ prefix.instantiatingModule,
875
+ ),
876
+ getBuiltin,
877
+ addError: (location, text) => {
878
+ addError(location, text);
879
+ return createNullNode();
880
+ },
881
+ isOutOfLabelScope: (label) => {
882
+ const goal = labelGoals.get(label);
883
+ return (
884
+ goal !== undefined &&
885
+ goal.scope.isDeclaredBefore[goal.clause] &&
886
+ goal.scope.isSuffices === goal.scope.isInProof
887
+ );
888
+ },
889
+ isOutOfClauseScope: (assumeProve, position) => {
890
+ const scope = assumeProveScopes.get(assumeProve);
891
+ return (
892
+ scope !== undefined &&
893
+ scope.isDeclaredBefore[position - 1] &&
894
+ (!scope.hasGoal || scope.isSuffices === scope.isInProof)
895
+ );
896
+ },
897
+ });
898
+
899
+ // The parts of a selector at the tokens of the expression that SANY locates them at, from the
900
+ // name or selector itself to its parenthesized arguments.
901
+ const locateSelectorItems = (items: SelectorItem[], span: TokenSpan) => {
902
+ const cursor = { value: span.start };
903
+ return items.map((item): SelectorItem => {
904
+ const text = item.op === "Name" ? item.name : item.op === "Null" ? "(" : String(item.op);
905
+ const index = findToken(file, cursor.value, span.end, text);
906
+ if (index === -1) return item;
907
+ if (item.op === "Null") {
908
+ const close = getMatchingIndex(file, index);
909
+ cursor.value = close + 1;
910
+ return { ...item, location: getSpanLocation(file, { start: index, end: close }) };
911
+ }
912
+ cursor.value = index + 1;
913
+ const location = getTokenLocation(file, index);
914
+ if (item.args === undefined || !isToken(file, index + 1, "(")) return { ...item, location };
915
+ const close = getMatchingIndex(file, index + 1);
916
+ cursor.value = close + 1;
917
+ return {
918
+ ...item,
919
+ location,
920
+ argsLocation: getSpanLocation(file, { start: index + 1, end: close }),
921
+ };
922
+ });
923
+ };
924
+
925
+ const generateSelector = (
926
+ expression: Expression,
927
+ expectedArity: number,
928
+ isFact: boolean,
929
+ isDef: boolean,
930
+ ) => {
931
+ const items = getSelectorItems(expression);
932
+ if (items === undefined)
933
+ throw new TlaSyntaxError(`Expected a name, found ${expression.kind}`);
934
+ return selectorToNode(
935
+ locateSelectorItems(items, getExpressionSpan(file, expression)),
936
+ expectedArity,
937
+ isFact,
938
+ isDef,
939
+ getLocation(expression),
940
+ );
941
+ };
942
+
943
+ const generateSelectorExpression = (expression: Expression) => {
944
+ const node = generateSelector(expression, 0, false, false);
945
+ if (node.kind !== "OpAppl") throw new TlaSyntaxError("Expected an expression");
946
+ return node;
947
+ };
948
+
949
+ const checkRecursion = (node: ExprNode) => {
950
+ if (node.kind !== "OpAppl") return;
951
+ const recursive = functions.find((entry) => entry.name === node.operator.name);
952
+ if (recursive) recursive.node.operator = getBuiltin("$RecursiveFcnSpec");
953
+ };
954
+
955
+ // The bound groups of a quantifier, set filter, set map or function: tla-ts expands
956
+ // \A x, y \in S into a bound per name that shares the set, and SANY keeps one group.
957
+ const getBoundGroups = (bounds: Bound[]) =>
958
+ bounds.reduce<Bound[][]>((groups, bound) => {
959
+ const last = groups[groups.length - 1];
960
+ const isSameGroup =
961
+ last !== undefined &&
962
+ last[0].set === bound.set &&
963
+ bound.pattern.kind === "Name" &&
964
+ last[0].pattern.kind === "Name";
965
+ if (isSameGroup) last.push(bound);
966
+ else groups.push([bound]);
967
+ return groups;
968
+ }, []);
969
+
970
+ // processQuantBoundArgs: SANY generates every bounding set before it declares the symbols,
971
+ // each named at its token between the end of the previous group and its set.
972
+ const generateBounds = (bounds: Bound[], searchStart: number) =>
973
+ generateBoundsWithRanges(
974
+ bounds,
975
+ searchStart,
976
+ getBoundGroups(bounds).map((group) => generateExpression(group[0].set)),
977
+ );
978
+
979
+ // Generator.pushFormalParams: a label in the expression has to name the parameters it binds.
980
+ const generateWithParams = (params: FormalParamNode[], expression: Expression) => {
981
+ const scope = labelScopes.at(-1);
982
+ scope?.params.push(params);
983
+ const node = generateExpression(expression);
984
+ scope?.params.pop();
985
+ return node;
986
+ };
987
+
988
+ const generateLambda = (expression: Expression & { kind: "Lambda" }): OpDefNode => {
989
+ const span = getExpressionSpan(file, expression);
990
+ const nameIndexes = findNameTokens(file, span.start, span.end, expression.params);
991
+ return withContext(createContext(), () => {
992
+ const params = expression.params.map((name, index) =>
993
+ createFormalParam(name, 0, getTokenLocation(file, nameIndexes[index]), true),
994
+ );
995
+ const body = generateWithParams(params, expression.body);
996
+ return createOpDef({
997
+ name: "LAMBDA",
998
+ params,
999
+ body,
1000
+ location: getSpanLocation(file, span),
1001
+ preComments: getPreComments(file, span.start),
1002
+ });
1003
+ });
1004
+ };
1005
+
1006
+ // tla-ts desugars an operator symbol passed as an argument, such as SUBSET or -., into a LAMBDA
1007
+ // without a range.
1008
+ const getOperatorArgumentName = (argument: Expression) =>
1009
+ argument.kind === "OperatorReference"
1010
+ ? argument.operator
1011
+ : argument.kind === "Lambda" && argument.body.kind === "Apply"
1012
+ ? argument.body.operator
1013
+ : file.tokens[getTokenIndex(file, argument.location)].text;
1014
+
1015
+ // Generator.generateOpArg: after an error, SANY gives its nullOpArg.
1016
+ const generateOperatorArgument = (
1017
+ argument: Expression,
1018
+ target: OpDeclNode,
1019
+ ): OpArgNode | OpApplNode => {
1020
+ const location = getLocation(argument);
1021
+ if (argument.kind === "Lambda") {
1022
+ if (argument.location.range !== undefined)
1023
+ return createOpArg(generateLambda(argument), location);
1024
+ return createOpArg(resolveName(getOperatorArgumentName(argument), location), location);
1025
+ }
1026
+ if (!isOperatorArgument(argument)) {
1027
+ addError(
1028
+ location,
1029
+ `Arity ${target.arity} operator (not an expression) is expected \nto substitute for CONSTANT '${target.name}'.`,
1030
+ );
1031
+ return createNullNode();
1032
+ }
1033
+ if (argument.kind === "OperatorReference") {
1034
+ return createOpArg(resolveName(argument.operator, location), location);
1035
+ }
1036
+ const node = generateSelector(argument, target.arity, false, false);
1037
+ if (node.kind === "OpArg" || (node.kind === "OpAppl" && node.isNull === true)) return node;
1038
+ throw new TlaSyntaxError(
1039
+ `Expected an operator argument at line ${location.beginLine}, column ${location.beginColumn}`,
1040
+ );
1041
+ };
1042
+
1043
+ // generateExprOrOpArg: an argument is an operator where the parameter takes arguments. SANY
1044
+ // reports an argument that doesn't fit at the operator's name and carries on.
1045
+ const generateArgument = (
1046
+ operator: SymbolNode,
1047
+ index: number,
1048
+ argument: Expression,
1049
+ operatorLocation: SanyLocation,
1050
+ ): ExprOrOpArgNode => {
1051
+ const isDefinition =
1052
+ operator.kind === "OpDef" &&
1053
+ (operator.defKind === "UserDefined" || operator.defKind === "ModuleInstance");
1054
+ const expectedArity = isDefinition ? (operator.params?.[index]?.arity ?? 0) : 0;
1055
+ if (expectedArity === 0) return generateExpression(argument);
1056
+ const span = getExpressionSpan(file, argument);
1057
+ const location = getSpanLocation(file, span);
1058
+ const position = `${index + 1} (counting from 1)`;
1059
+ const argumentSpan = getArgumentSpan(file, span);
1060
+ if (argumentSpan !== span || !isOperatorArgument(argument)) {
1061
+ addError(
1062
+ getSpanLocation(file, argumentSpan),
1063
+ `An expression appears as argument number ${position} to operator '${operator.name}', in a position an operator is required.`,
1064
+ );
1065
+ return createNullNode();
1066
+ }
1067
+ if (
1068
+ argument.kind === "Identifier" ||
1069
+ argument.kind === "Subexpression" ||
1070
+ argument.kind === "Apply"
1071
+ ) {
1072
+ // The selector names the operator, or reports why not and gives SANY's nullOAN.
1073
+ const node = generateSelector(argument, expectedArity, false, false);
1074
+ if (node.kind === "OpArg" || node.kind === "OpAppl") return node;
1075
+ throw new TlaSyntaxError(
1076
+ `Expected an operator argument at ${formatSanyLocation(location)}`,
1077
+ );
1078
+ }
1079
+ if (argument.kind === "Lambda" && argument.location.range !== undefined) {
1080
+ const lambda = generateLambda(argument);
1081
+ const arity = getSymbolArity(lambda);
1082
+ if (arity === expectedArity) return createOpArg(lambda, location);
1083
+ addError(
1084
+ operatorLocation,
1085
+ `Lambda expression with arity ${arity} used as argument ${index + 1} of operator \`${operator.name}', \nbut an operator of arity ${expectedArity} is required.`,
1086
+ );
1087
+ return createNullNode();
1088
+ }
1089
+ const name = getOperatorArgumentName(argument);
1090
+ const symbol = resolveSymbol(symbolTable, resolveSynonym(name));
1091
+ if (symbol === undefined) {
1092
+ addError(location, `Could not find declaration or definition of symbol '${name}'.`);
1093
+ return createNullNode();
1094
+ }
1095
+ const arity = getSymbolArity(symbol);
1096
+ if (arity === expectedArity) return createOpArg(symbol, location);
1097
+ addError(
1098
+ operatorLocation,
1099
+ `Operator with incorrect arity passed as argument. \nOperator '${symbol.name}' of arity ${arity} is argument number ${position} to operator \`${formatSanyLocation(operator.location)}', \nbut an operator of arity ${expectedArity} was expected.`,
1100
+ );
1101
+ return createNullNode();
1102
+ };
1103
+
1104
+ // A /\ B /\ C nests as ((A /\ B) /\ C) in SANY; tla-ts flattens it.
1105
+ const generateJunction = (expression: Expression & { kind: "And" | "Or" }) => {
1106
+ const location = getLocation(expression);
1107
+ if (expression.isBulleted) {
1108
+ return applyBuiltin(
1109
+ expression.kind === "And" ? "$ConjList" : "$DisjList",
1110
+ expression.operands.map(generateExpression),
1111
+ location,
1112
+ );
1113
+ }
1114
+ const span = getExpressionSpan(file, expression);
1115
+ const name = expression.kind === "And" ? "\\land" : "\\lor";
1116
+ const [first, ...rest] = expression.operands;
1117
+ return rest.reduce<ExprNode>((left, operand, index) => {
1118
+ const operandSpan = getParenthesizedSpan(file, getExpressionSpan(file, operand));
1119
+ const end = index === rest.length - 1 ? span.end : operandSpan.end;
1120
+ return applyBuiltin(
1121
+ name,
1122
+ [left, generateExpression(operand)],
1123
+ getSpanLocation(file, { start: span.start, end }),
1124
+ );
1125
+ }, generateExpression(first));
1126
+ };
1127
+
1128
+ const generateCase = (expression: Expression & { kind: "Case" }) => {
1129
+ const span = getExpressionSpan(file, expression);
1130
+ const arms = expression.arms.map((arm, index) => {
1131
+ const condition = getParenthesizedSpan(file, getExpressionSpan(file, arm.condition));
1132
+ const isLast = index === expression.arms.length - 1 && expression.other === undefined;
1133
+ const body = getParenthesizedSpan(file, getExpressionSpan(file, arm.body));
1134
+ const operands = [generateExpression(arm.condition), generateExpression(arm.body)];
1135
+ return applyBuiltin(
1136
+ "$Pair",
1137
+ operands,
1138
+ getSpanLocation(file, { start: condition.start, end: isLast ? span.end : body.end }),
1139
+ );
1140
+ });
1141
+ const other = expression.other;
1142
+ if (other) {
1143
+ const body = getParenthesizedSpan(file, getExpressionSpan(file, other));
1144
+ const otherIndex = findTokenBefore(body.start, "OTHER");
1145
+ arms.push(
1146
+ applyBuiltin(
1147
+ "$Pair",
1148
+ [undefined, generateExpression(other)],
1149
+ getSpanLocation(file, { start: otherIndex, end: span.end }),
1150
+ ),
1151
+ );
1152
+ }
1153
+ return applyBuiltin("$Case", arms, getSpanLocation(file, span));
1154
+ };
1155
+
1156
+ const findTokenBefore = (index: number, text: string) => {
1157
+ for (let current = index; current >= 0; current--) {
1158
+ if (isToken(file, current, text)) return current;
1159
+ }
1160
+ return index;
1161
+ };
1162
+
1163
+ const generateExcept = (expression: Expression & { kind: "Except" }) => {
1164
+ const span = getExpressionSpan(file, expression);
1165
+ const location = getSpanLocation(file, span);
1166
+ const operands: ExprOrOpArgNode[] = [generateExpression(expression.function)];
1167
+ const exceptNode = applyBuiltin("$Except", operands, location);
1168
+ const functionSpan = getExpressionSpan(file, expression.function);
1169
+ const cursor = { value: findToken(file, functionSpan.end + 1, span.end, "EXCEPT") + 1 };
1170
+ expression.updates.forEach((update) => {
1171
+ const bang = findToken(file, cursor.value, span.end, "!");
1172
+ const valueSpan = getParenthesizedSpan(file, getExpressionSpan(file, update.value));
1173
+ const specLocation = getSpanLocation(file, { start: bang, end: valueSpan.end });
1174
+ const pathCursor = { value: bang + 1 };
1175
+ const components = update.path.map((step): ExprNode => {
1176
+ if (step.kind === "Field") {
1177
+ const nameIndex = findNameOrKeyword(pathCursor.value, step.name);
1178
+ pathCursor.value = nameIndex + 1;
1179
+ return createString(step.name, getTokenLocation(file, nameIndex));
1180
+ }
1181
+ const open = findToken(file, pathCursor.value, span.end, "[");
1182
+ const close = getMatchingIndex(file, open);
1183
+ pathCursor.value = close + 1;
1184
+ if (step.args.length === 1) return generateExpression(step.args[0]);
1185
+ return applyBuiltin(
1186
+ "$Tuple",
1187
+ step.args.map(generateExpression),
1188
+ getSpanLocation(file, { start: open, end: close }),
1189
+ );
1190
+ });
1191
+ const sequence = applyBuiltin("$Seq", components, specLocation);
1192
+ const pair = applyBuiltin("$Pair", [sequence], specLocation);
1193
+ exceptSpecStack.push(pair);
1194
+ exceptStack.push(exceptNode);
1195
+ pair.operands.push(generateExpression(update.value));
1196
+ exceptSpecStack.pop();
1197
+ exceptStack.pop();
1198
+ operands.push(pair);
1199
+ cursor.value = valueSpan.end + 1;
1200
+ });
1201
+ return exceptNode;
1202
+ };
1203
+
1204
+ const findNameOrKeyword = (start: number, name: string) => {
1205
+ for (let index = start; index < file.tokens.length; index++) {
1206
+ if (file.tokens[index].text === name && file.tokens[index].kind !== "String") return index;
1207
+ }
1208
+ return start;
1209
+ };
1210
+
1211
+ const generateRecordFields = (
1212
+ operator: "$RcdConstructor" | "$SetOfRcds",
1213
+ expression: Expression & { kind: "Record" | "RecordSet" },
1214
+ ) => {
1215
+ const span = getExpressionSpan(file, expression);
1216
+ const cursor = { value: span.start + 1 };
1217
+ const pairs = expression.fields.map((field, fieldIndex) => {
1218
+ const nameIndex = findNameOrKeyword(cursor.value, field.name);
1219
+ const valueSpan = getParenthesizedSpan(file, getExpressionSpan(file, field.value));
1220
+ cursor.value = valueSpan.end + 1;
1221
+ if (expression.fields.slice(0, fieldIndex).some((other) => other.name === field.name)) {
1222
+ addError(getTokenLocation(file, nameIndex), "Non-unique fields in constructor.");
1223
+ }
1224
+ return applyBuiltin(
1225
+ "$Pair",
1226
+ [
1227
+ createString(field.name, getTokenLocation(file, nameIndex)),
1228
+ generateExpression(field.value),
1229
+ ],
1230
+ getSpanLocation(file, { start: nameIndex, end: valueSpan.end }),
1231
+ );
1232
+ });
1233
+ const record = applyBuiltin(operator, pairs, getSpanLocation(file, span));
1234
+ currentModule.records.push(record);
1235
+ return record;
1236
+ };
1237
+
1238
+ const getTokenPosition = (index: number) => ({
1239
+ line: file.tokens[index].line,
1240
+ column: file.tokens[index].column,
1241
+ });
1242
+
1243
+ // The declarations of a RECURSIVE statement, which the syntax tree of a LET leaves out:
1244
+ // names, each with (_, ...) for its arguments.
1245
+ const getRecursiveDeclarations = (start: number) => {
1246
+ const declarations: Declaration[] = [];
1247
+ let index = start;
1248
+ while (file.tokens[index]?.kind === "Identifier") {
1249
+ const end = isToken(file, index + 1, "(") ? getMatchingIndex(file, index + 1) : index;
1250
+ const arity = file.tokens
1251
+ .slice(index + 1, end)
1252
+ .filter((token) => token.text === "_").length;
1253
+ declarations.push({
1254
+ name: file.tokens[index].text,
1255
+ arity,
1256
+ location: {
1257
+ ...getTokenPosition(index),
1258
+ range: {
1259
+ module: file.name,
1260
+ start: getTokenPosition(index),
1261
+ end: getTokenPosition(end),
1262
+ },
1263
+ },
1264
+ });
1265
+ if (!isToken(file, end + 1, ",")) break;
1266
+ index = end + 2;
1267
+ }
1268
+ return declarations;
1269
+ };
1270
+
1271
+ // The units of a LET in source order, with its RECURSIVE statements found among its tokens.
1272
+ const getLetUnits = (expression: Expression & { kind: "Let" }) => {
1273
+ const units = [
1274
+ ...expression.definitions.map((definition) => ({
1275
+ unit: { kind: "Definition", definition } satisfies LetUnit,
1276
+ span: getDefinitionSpan(definition),
1277
+ })),
1278
+ ...(expression.instances ?? []).map((instance) => ({
1279
+ unit: { kind: "Instance", instance } satisfies LetUnit,
1280
+ span: getDefinitionSpan(instance),
1281
+ })),
1282
+ ];
1283
+ const letStart = getExpressionSpan(file, expression).start;
1284
+ const bodyStart = getParenthesizedSpan(file, getExpressionSpan(file, expression.body)).start;
1285
+ const recursives: { unit: LetUnit; span: TokenSpan }[] = [];
1286
+ for (let index = letStart + 1; index < bodyStart; index++) {
1287
+ const isInUnit = units.some(
1288
+ (entry) => entry.span.start <= index && index <= entry.span.end,
1289
+ );
1290
+ if (isToken(file, index, "RECURSIVE") && !isInUnit) {
1291
+ recursives.push({
1292
+ unit: { kind: "Recursive", declarations: getRecursiveDeclarations(index + 1) },
1293
+ span: { start: index, end: index },
1294
+ });
1295
+ }
1296
+ }
1297
+ return [...units, ...recursives]
1298
+ .toSorted((first, second) => first.span.start - second.span.start)
1299
+ .map((entry) => entry.unit);
1300
+ };
1301
+
1302
+ const generateLet = (expression: Expression & { kind: "Let" }): LetInNode => {
1303
+ const context = createContext();
1304
+ return withContext(context, () => {
1305
+ state.curLevel++;
1306
+ state.unresolvedCount[state.curLevel] = 0;
1307
+ const opDefs: (OpDefNode | ThmOrAssumpDefNode)[] = [];
1308
+ const instances: InstanceNode[] = [];
1309
+ getLetUnits(expression).forEach((unit) => {
1310
+ if (unit.kind === "Recursive") processRecursive(unit.declarations);
1311
+ else if (unit.kind === "Instance") {
1312
+ processModuleDefinition(unit.instance, opDefs, instances);
1313
+ } else if (unit.definition.kind === "Function") {
1314
+ processFunction(unit.definition, opDefs);
1315
+ } else processOperator(unit.definition, opDefs);
1316
+ });
1317
+ checkForUndefinedRecursiveOps();
1318
+ state.curLevel = Math.max(state.curLevel - 1, 0);
1319
+ const body = generateExpression(expression.body);
1320
+ return {
1321
+ ...createLevelFields(getId()),
1322
+ kind: "LetIn",
1323
+ opDefs,
1324
+ instances,
1325
+ body,
1326
+ context,
1327
+ location: getLocation(expression),
1328
+ };
1329
+ });
1330
+ };
1331
+
1332
+ const generateChoose = (expression: Expression & { kind: "Choose" }) => {
1333
+ const span = getExpressionSpan(file, expression);
1334
+ const location = getSpanLocation(file, span);
1335
+ return withContext(createContext(), () => {
1336
+ const pattern = expression.pattern;
1337
+ const names = pattern.kind === "Name" ? [pattern.name] : pattern.names;
1338
+ if (expression.set === undefined) {
1339
+ const indexes = findNameTokens(file, span.start, span.end, names);
1340
+ // SANY locates the symbol of CHOOSE x : P at the CHOOSE keyword.
1341
+ const params = names.map((name, index) =>
1342
+ createFormalParam(
1343
+ name,
1344
+ 0,
1345
+ getTokenLocation(file, pattern.kind === "Name" ? span.start : indexes[index]),
1346
+ true,
1347
+ ),
1348
+ );
1349
+ const body = generateWithParams(params, expression.body);
1350
+ return applyBuiltin("$UnboundedChoose", [body], location, { unbounded: params });
1351
+ }
1352
+ const range = generateExpression(expression.set);
1353
+ const setSpan = getExpressionSpan(file, expression.set);
1354
+ const indexes = findNameTokens(file, span.start, setSpan.start, names);
1355
+ const params = names.map((name, index) =>
1356
+ createFormalParam(name, 0, getTokenLocation(file, indexes[index]), true),
1357
+ );
1358
+ const body = generateWithParams(params, expression.body);
1359
+ return applyBuiltin("$BoundedChoose", [body], location, {
1360
+ bounded: [params],
1361
+ tupleOrs: [pattern.kind === "Tuple"],
1362
+ ranges: [range],
1363
+ });
1364
+ });
1365
+ };
1366
+
1367
+ // An infix, prefix or postfix operator that no symbol defines, with the token SANY names it by.
1368
+ const getUnresolvedOperator = (expression: Expression & { kind: "Apply" }) => {
1369
+ if (
1370
+ isIdentifier(expression.operator) ||
1371
+ isSelector(expression.operator) ||
1372
+ expression.args.length > 2
1373
+ ) {
1374
+ return undefined;
1375
+ }
1376
+ if (resolveSymbol(symbolTable, resolveSynonym(expression.operator)) !== undefined) {
1377
+ return undefined;
1378
+ }
1379
+ const span = getExpressionSpan(file, expression);
1380
+ // SANY reports an operator applied like a function, as in op(a, b), as an unknown operator.
1381
+ if (
1382
+ file.tokens[span.start].text === expression.operator &&
1383
+ isToken(file, span.start + 1, "(")
1384
+ ) {
1385
+ return undefined;
1386
+ }
1387
+ const firstSpan = getParenthesizedSpan(file, getExpressionSpan(file, expression.args[0]));
1388
+ const isPrefix = expression.args.length === 1 && firstSpan.start !== span.start;
1389
+ const index = isPrefix ? span.start : firstSpan.end + 1;
1390
+ const text = getTokenSource(file, index);
1391
+ return {
1392
+ index,
1393
+ name: isPrefix && text === "-" ? "-." : text,
1394
+ fixity: isPrefix ? "prefix" : expression.args.length === 2 ? "infix" : "postfix",
1395
+ };
1396
+ };
1397
+
1398
+ const generateExpression = (expression: Expression): ExprNode => {
1399
+ const location = getLocation(expression);
1400
+ switch (expression.kind) {
1401
+ case "Number":
1402
+ return {
1403
+ ...createLevelFields(getId()),
1404
+ kind: "Numeral",
1405
+ value: getNumeralValue(expression.text ?? String(expression.value)),
1406
+ location,
1407
+ };
1408
+ case "Decimal": {
1409
+ const [integralPart, fractionalPart] = expression.text.split(".");
1410
+ const mantissa = BigInt(integralPart + fractionalPart);
1411
+ const isLong = mantissa <= 9223372036854775807n;
1412
+ return {
1413
+ ...createLevelFields(getId()),
1414
+ kind: "Decimal",
1415
+ mantissa: mantissa.toString(),
1416
+ exponent: isLong ? String(-fractionalPart.length) : String(fractionalPart.length),
1417
+ integralPart,
1418
+ fractionalPart,
1419
+ location,
1420
+ };
1421
+ }
1422
+ case "String":
1423
+ return createString(expression.value, location);
1424
+ case "Identifier": {
1425
+ const node = generateSelectorExpression(expression);
1426
+ checkRecursion(node);
1427
+ return node;
1428
+ }
1429
+ case "Apply": {
1430
+ const unresolved = getUnresolvedOperator(expression);
1431
+ if (unresolved !== undefined) {
1432
+ addError(
1433
+ getTokenLocation(file, unresolved.index),
1434
+ `Could not find declaration or definition of symbol '${unresolved.name}'.`,
1435
+ );
1436
+ addError(
1437
+ location,
1438
+ `Couldn't resolve ${unresolved.fixity} operator symbol \`${unresolved.name}'.`,
1439
+ );
1440
+ return { ...createNullNode(), isAbsent: true };
1441
+ }
1442
+ const node = generateSelectorExpression(expression);
1443
+ if (isSelector(expression.operator) || isIdentifier(expression.operator)) {
1444
+ checkRecursion(node);
1445
+ }
1446
+ return node;
1447
+ }
1448
+ case "Bracket":
1449
+ addError(
1450
+ getTokenLocation(file, getExpressionSpan(file, expression).start),
1451
+ "Unsupported expression type `['.",
1452
+ );
1453
+ return { ...createNullNode(), isAbsent: true };
1454
+ case "OperatorReference":
1455
+ case "Lambda": {
1456
+ const text = file.tokens[getExpressionSpan(file, expression).start].text;
1457
+ addError(
1458
+ location,
1459
+ text === "LAMBDA"
1460
+ ? "LAMBDA expression used where an expression is required."
1461
+ : `Unsupported expression type \`${getOperatorNodeKind(expression, text)}'.`,
1462
+ );
1463
+ return { ...createNullNode(), isAbsent: true };
1464
+ }
1465
+ case "And":
1466
+ case "Or":
1467
+ return generateJunction(expression);
1468
+ case "Prime":
1469
+ return applyBuiltin("'", [generateExpression(expression.expression)], location);
1470
+ case "Unchanged":
1471
+ return applyBuiltin("UNCHANGED", [generateExpression(expression.expression)], location);
1472
+ case "Enabled":
1473
+ return applyBuiltin("ENABLED", [generateExpression(expression.expression)], location);
1474
+ case "Always":
1475
+ return applyBuiltin("[]", [generateExpression(expression.expression)], location);
1476
+ case "Eventually":
1477
+ return applyBuiltin("<>", [generateExpression(expression.expression)], location);
1478
+ case "If":
1479
+ return applyBuiltin(
1480
+ "$IfThenElse",
1481
+ [
1482
+ generateExpression(expression.condition),
1483
+ generateExpression(expression.consequent),
1484
+ generateExpression(expression.alternative),
1485
+ ],
1486
+ location,
1487
+ );
1488
+ case "Case":
1489
+ return generateCase(expression);
1490
+ case "Let":
1491
+ return generateLet(expression);
1492
+ case "Forall":
1493
+ case "Exists": {
1494
+ const span = getExpressionSpan(file, expression);
1495
+ return withContext(createContext(), () => {
1496
+ const bounds = generateBounds(expression.bounds, span.start);
1497
+ const body = generateWithParams(bounds.bounded.flat(), expression.body);
1498
+ return applyBuiltin(
1499
+ expression.kind === "Forall" ? "$BoundedForall" : "$BoundedExists",
1500
+ [body],
1501
+ location,
1502
+ bounds,
1503
+ );
1504
+ });
1505
+ }
1506
+ case "UnboundedQuantifier": {
1507
+ const span = getExpressionSpan(file, expression);
1508
+ const bodySpan = getExpressionSpan(file, expression.body);
1509
+ const indexes = findNameTokens(file, span.start, bodySpan.start, expression.names);
1510
+ const name = expression.isTemporal
1511
+ ? expression.quantifier === "Forall"
1512
+ ? "$TemporalForall"
1513
+ : "$TemporalExists"
1514
+ : expression.quantifier === "Forall"
1515
+ ? "$UnboundedForall"
1516
+ : "$UnboundedExists";
1517
+ return withContext(createContext(), () => {
1518
+ const params = expression.names.map((paramName, index) =>
1519
+ createFormalParam(paramName, 0, getTokenLocation(file, indexes[index]), true),
1520
+ );
1521
+ const body = generateWithParams(params, expression.body);
1522
+ return applyBuiltin(name, [body], location, { unbounded: params });
1523
+ });
1524
+ }
1525
+ case "Choose":
1526
+ return generateChoose(expression);
1527
+ case "SetEnumeration":
1528
+ return applyBuiltin(
1529
+ "$SetEnumerate",
1530
+ expression.elements.map(generateExpression),
1531
+ location,
1532
+ );
1533
+ case "Tuple":
1534
+ return applyBuiltin("$Tuple", expression.elements.map(generateExpression), location);
1535
+ case "SetFilter": {
1536
+ const range = generateExpression(expression.bound.set);
1537
+ const span = getExpressionSpan(file, expression);
1538
+ return withContext(createContext(), () => {
1539
+ const bounds = generateBoundsWithRanges([expression.bound], span.start, [range]);
1540
+ const body = generateWithParams(bounds.bounded.flat(), expression.predicate);
1541
+ return applyBuiltin("$SubsetOf", [body], location, bounds);
1542
+ });
1543
+ }
1544
+ case "SetMap": {
1545
+ const expressionSpan = getExpressionSpan(file, expression.expression);
1546
+ return withContext(createContext(), () => {
1547
+ const bounds = generateBounds(expression.bounds, expressionSpan.end + 1);
1548
+ const body = generateWithParams(bounds.bounded.flat(), expression.expression);
1549
+ return applyBuiltin("$SetOfAll", [body], location, bounds);
1550
+ });
1551
+ }
1552
+ case "FunctionConstruction": {
1553
+ const span = getExpressionSpan(file, expression);
1554
+ return withContext(createContext(), () => {
1555
+ const bounds = generateBounds(expression.bounds, span.start);
1556
+ const body = generateWithParams(bounds.bounded.flat(), expression.body);
1557
+ return applyBuiltin("$FcnConstructor", [body], location, bounds);
1558
+ });
1559
+ }
1560
+ case "FunctionSet":
1561
+ return applyBuiltin(
1562
+ "$SetOfFcns",
1563
+ [generateExpression(expression.domain), generateExpression(expression.range)],
1564
+ location,
1565
+ );
1566
+ case "Record":
1567
+ return generateRecordFields("$RcdConstructor", expression);
1568
+ case "RecordSet":
1569
+ return generateRecordFields("$SetOfRcds", expression);
1570
+ case "Except":
1571
+ return generateExcept(expression);
1572
+ case "ExceptAt": {
1573
+ const except = exceptStack[exceptStack.length - 1];
1574
+ const component = exceptSpecStack[exceptSpecStack.length - 1];
1575
+ if (except === undefined || component === undefined) {
1576
+ addError(location, "@ used where its meaning is not defined.");
1577
+ return createNullNode();
1578
+ }
1579
+ return {
1580
+ ...createLevelFields(getId()),
1581
+ kind: "At",
1582
+ except,
1583
+ component,
1584
+ location: component.location,
1585
+ };
1586
+ }
1587
+ case "FunctionApplication": {
1588
+ const functionNode = generateExpression(expression.function);
1589
+ // SANY leaves the whole application null, without generating its arguments.
1590
+ if (isAbsent(functionNode)) return functionNode;
1591
+ checkRecursion(functionNode);
1592
+ const argument =
1593
+ expression.args.length === 1
1594
+ ? generateExpression(expression.args[0])
1595
+ : applyBuiltin("$Tuple", expression.args.map(generateExpression), location);
1596
+ return applyBuiltin("$FcnApply", [functionNode, argument], location);
1597
+ }
1598
+ case "FieldAccess": {
1599
+ const record = generateExpression(expression.record);
1600
+ const span = getExpressionSpan(file, expression);
1601
+ return applyBuiltin(
1602
+ "$RcdSelect",
1603
+ [record, createString(expression.field, getTokenLocation(file, span.end))],
1604
+ location,
1605
+ );
1606
+ }
1607
+ case "Product":
1608
+ return applyBuiltin(
1609
+ "$CartesianProd",
1610
+ expression.factors.map(generateExpression),
1611
+ location,
1612
+ );
1613
+ case "BoxAction":
1614
+ case "AngleAction":
1615
+ return applyBuiltin(
1616
+ expression.kind === "BoxAction" ? "$SquareAct" : "$AngleAct",
1617
+ [generateExpression(expression.action), generateExpression(expression.subscript)],
1618
+ location,
1619
+ );
1620
+ case "WeakFairness":
1621
+ case "StrongFairness":
1622
+ return applyBuiltin(
1623
+ expression.kind === "WeakFairness" ? "$WF" : "$SF",
1624
+ [generateExpression(expression.subscript), generateExpression(expression.action)],
1625
+ location,
1626
+ );
1627
+ case "Label":
1628
+ return generateLabel(expression);
1629
+ case "Subexpression":
1630
+ return generateSelectorExpression(expression);
1631
+ }
1632
+ };
1633
+
1634
+ const generateBoundsWithRanges = (
1635
+ bounds: Bound[],
1636
+ searchStart: number,
1637
+ ranges: ExprNode[],
1638
+ ): BoundSymbols => {
1639
+ const groups = getBoundGroups(bounds);
1640
+ const cursor = { value: searchStart };
1641
+ const bounded = groups.map((group) => {
1642
+ const setSpan = getExpressionSpan(file, group[0].set);
1643
+ const names = group.flatMap((bound) =>
1644
+ bound.pattern.kind === "Name" ? [bound.pattern.name] : bound.pattern.names,
1645
+ );
1646
+ const indexes = findNameTokens(file, cursor.value, setSpan.start, names);
1647
+ cursor.value = setSpan.end + 1;
1648
+ return names.map((name, index) =>
1649
+ createFormalParam(name, 0, getTokenLocation(file, indexes[index]), true),
1650
+ );
1651
+ });
1652
+ return {
1653
+ bounded,
1654
+ tupleOrs: groups.map((group) => group[0].pattern.kind === "Tuple"),
1655
+ ranges,
1656
+ };
1657
+ };
1658
+
1659
+ const generateLabel = (expression: Expression & { kind: "Label" }): LabelNode | OpApplNode => {
1660
+ const location = getLocation(expression);
1661
+ const scope = labelScopes.at(-1);
1662
+ if (scope === undefined) {
1663
+ addError(location, "Label not in definition or proof step.");
1664
+ return createNullNode();
1665
+ }
1666
+ // Generator.noLabelsAllowed: no label lies in the scope of a declaration of a nested
1667
+ // ASSUME/PROVE.
1668
+ if (assumeProveState.isDeclared.slice(2, assumeProveState.depth + 1).some(Boolean)) {
1669
+ addError(location, "Label not allowed within scope of declaration in nested ASSUME/PROVE.");
1670
+ return createNullNode();
1671
+ }
1672
+ if (exceptStack.length > 0 && exceptSpecStack.length > 0) {
1673
+ addError(location, "Labels inside EXCEPT clauses are not yet implemented.");
1674
+ return createNullNode();
1675
+ }
1676
+ pushLabelScope();
1677
+ const body = generateExpression(expression.body);
1678
+ const labels = popLabels();
1679
+ const paramIndexes = findNameTokens(
1680
+ file,
1681
+ getExpressionSpan(file, expression).start,
1682
+ getExpressionSpan(file, expression.body).start,
1683
+ expression.params,
1684
+ );
1685
+ const params = expression.params.map((name, index) => {
1686
+ const symbol = resolveSymbol(symbolTable, name);
1687
+ if (symbol?.kind === "FormalParam") return symbol;
1688
+ const paramLocation = getTokenLocation(file, paramIndexes[index]);
1689
+ addError(paramLocation, `Illegal parameter ${name} of label \`${expression.name}'.`);
1690
+ return createFormalParam(name, 0, paramLocation, false);
1691
+ });
1692
+ const label: LabelNode = {
1693
+ ...createLevelFields(getId()),
1694
+ kind: "Label",
1695
+ name: expression.name,
1696
+ params,
1697
+ body,
1698
+ labels,
1699
+ isAssumeProve: false,
1700
+ location,
1701
+ };
1702
+ // Generator.formalParamsEqual: a label names each parameter bound around it once.
1703
+ const named = new Set<FormalParamNode>();
1704
+ params.forEach((param) => {
1705
+ if (named.has(param)) {
1706
+ addError(
1707
+ location,
1708
+ `Repeated formal parameter ${param.name} \nin label \`${label.name}'.`,
1709
+ );
1710
+ }
1711
+ named.add(param);
1712
+ });
1713
+ scope.params.flat().forEach((param) => {
1714
+ if (!named.delete(param)) {
1715
+ addError(location, `Label ${label.name} must contain formal parameter \`${param.name}'.`);
1716
+ }
1717
+ });
1718
+ if (named.size > 0) {
1719
+ const extras = [...named].map((param) => `${param.name} `).join("");
1720
+ addError(location, `Label ${label.name} declares extra parameter(s) ${extras}`);
1721
+ }
1722
+ if (scope.labels.has(label.name)) addError(location, `Duplicate label \`${label.name}'.`);
1723
+ else scope.labels.set(label.name, label);
1724
+ const goal = assumeProveState.goal;
1725
+ if (goal) labelGoals.set(label, { scope: goal, clause: assumeProveState.goalClause });
1726
+ return label;
1727
+ };
1728
+
1729
+ // The parameters of a definition's left-hand side, located as SANY locates them: a name, or a
1730
+ // name with its underscores like Op(_, _).
1731
+ const getParams = (
1732
+ definition: OperatorDefinition | Instance,
1733
+ span: TokenSpan,
1734
+ isAdded: boolean,
1735
+ ) => {
1736
+ const cursor = { value: span.start };
1737
+ return definition.params.map((param) => {
1738
+ const index = param.location
1739
+ ? getTokenIndex(file, param.location)
1740
+ : findNameOrKeyword(cursor.value, param.name);
1741
+ const paramSpan = getOperatorSpan(index, param.arity);
1742
+ cursor.value = paramSpan.end + 1;
1743
+ return createFormalParam(
1744
+ resolveSynonym(param.name),
1745
+ param.arity,
1746
+ getSpanLocation(file, paramSpan),
1747
+ isAdded,
1748
+ );
1749
+ });
1750
+ };
1751
+
1752
+ // The span of a definition or instance, with the LOCAL before it.
1753
+ const getDefinitionSpan = (definition: { location: SourceLocation; isLocal?: boolean }) => {
1754
+ const span = getUnitSpan(definition.location);
1755
+ return definition.isLocal &&
1756
+ !isToken(file, span.start, "LOCAL") &&
1757
+ isToken(file, span.start - 1, "LOCAL")
1758
+ ? { start: span.start - 1, end: span.end }
1759
+ : span;
1760
+ };
1761
+
1762
+ const processOperator = (
1763
+ definition: OperatorDefinition,
1764
+ definitions: (OpDefNode | ThmOrAssumpDefNode)[] | undefined,
1765
+ ) => {
1766
+ const span = getDefinitionSpan(definition);
1767
+ const location = getSpanLocation(file, span);
1768
+ const name = resolveSynonym(definition.name);
1769
+ const paramsSpan = { start: definition.isLocal ? span.start + 1 : span.start, end: span.end };
1770
+ const generated = withContext(createContext(), () => {
1771
+ const params = getParams(definition, paramsSpan, true);
1772
+ const existing = resolveSymbol(symbolTable, name);
1773
+ if (existing?.kind === "OpDef" && existing.inRecursive && !existing.isDefined) {
1774
+ // SANY compares only the arity of the last parameter.
1775
+ const isMatching =
1776
+ existing.params?.length === params.length &&
1777
+ (params.length === 0 || params[params.length - 1].arity === 0);
1778
+ if (existing.letInLevel !== state.curLevel) {
1779
+ addError(location, `Recursive operator ${name} defined at wrong LET/IN level.`);
1780
+ } else if (!isMatching) {
1781
+ addError(
1782
+ location,
1783
+ `Definition of ${existing.name} has different arity than its RECURSIVE declaration.`,
1784
+ );
1785
+ }
1786
+ } else if (existing !== undefined) {
1787
+ addError(location, `Operator ${name} already defined or declared.`);
1788
+ }
1789
+ pushLabelScope();
1790
+ return { params, body: generateExpression(definition.body), existing };
1791
+ });
1792
+ const labels = popLabels();
1793
+ const existing = generated.existing;
1794
+ const params = generated.params;
1795
+ const preComments = getPreComments(file, span.start);
1796
+ if (
1797
+ existing?.kind === "OpDef" &&
1798
+ existing.inRecursive &&
1799
+ !existing.isDefined &&
1800
+ existing.letInLevel === state.curLevel
1801
+ ) {
1802
+ // OpDefNode.setParams keeps the arity of the RECURSIVE declaration.
1803
+ existing.params = params;
1804
+ existing.weights = params.map(() => 0);
1805
+ existing.maxLevels = params.map(() => TEMPORAL_LEVEL);
1806
+ existing.isLeibnizArg = params.map(() => true);
1807
+ existing.labels = labels;
1808
+ endOpDefNode(existing, generated.body, location, preComments);
1809
+ currentModule.definitions.push(existing);
1810
+ definitions?.push(existing);
1811
+ return;
1812
+ }
1813
+ const node = createOpDef({
1814
+ name,
1815
+ params,
1816
+ body: generated.body,
1817
+ isLocal: definition.isLocal,
1818
+ location,
1819
+ preComments,
1820
+ labels,
1821
+ });
1822
+ addSymbol(symbolTable, node);
1823
+ setRecursionFields(node);
1824
+ currentModule.definitions.push(node);
1825
+ definitions?.push(node);
1826
+ };
1827
+
1828
+ const endOpDefNode = (
1829
+ node: OpDefNode,
1830
+ body: ExprNode,
1831
+ location: SanyLocation,
1832
+ preComments: string[],
1833
+ ) => {
1834
+ node.isDefined = true;
1835
+ node.body = body;
1836
+ node.location = location;
1837
+ node.preComments = preComments;
1838
+ if (node.inRecursive) {
1839
+ state.unresolvedCount[state.curLevel]--;
1840
+ state.unresolvedSum--;
1841
+ }
1842
+ };
1843
+
1844
+ const processFunction = (
1845
+ definition: FunctionDefinition,
1846
+ definitions: (OpDefNode | ThmOrAssumpDefNode)[] | undefined,
1847
+ ) => {
1848
+ const span = getDefinitionSpan(definition);
1849
+ const location = getSpanLocation(file, span);
1850
+ const context = createContext();
1851
+ const nameIndex = definition.isLocal ? span.start + 1 : span.start;
1852
+ const bounds = withContext(context, () => generateBounds(definition.bounds, nameIndex + 1));
1853
+ const existing = resolveSymbol(symbolTable, definition.name);
1854
+ const recursionParam = withContext(context, () =>
1855
+ createFormalParam(definition.name, 0, location, existing === undefined),
1856
+ );
1857
+ const node = applyBuiltin("$NonRecursiveFcnSpec", [], location, {
1858
+ unbounded: [recursionParam],
1859
+ ...bounds,
1860
+ });
1861
+ const isPendingRecursive =
1862
+ existing?.kind === "OpDef" && existing.inRecursive && !existing.isDefined;
1863
+ const isRecursive = isPendingRecursive && existing.letInLevel === state.curLevel;
1864
+ if (isRecursive) {
1865
+ if (existing.arity !== 0) {
1866
+ addError(
1867
+ location,
1868
+ `Function ${existing.name} has operator arguments in its RECURSIVE declaration.`,
1869
+ );
1870
+ }
1871
+ } else if (isPendingRecursive) {
1872
+ addError(location, `Recursive function ${definition.name} defined at wrong LET/IN level.`);
1873
+ } else if (existing !== undefined) {
1874
+ addError(location, `Function name \`${definition.name}' already defined or declared.`);
1875
+ }
1876
+ const preComments = getPreComments(file, span.start);
1877
+ // Generator.processFunction's OpDefNode: a new one, or the one the name already has, though
1878
+ // defined, unless that is declared RECURSIVE at another level.
1879
+ const definitionNode =
1880
+ existing === undefined
1881
+ ? createOpDef({
1882
+ name: definition.name,
1883
+ params: [],
1884
+ body: node,
1885
+ isLocal: definition.isLocal,
1886
+ location,
1887
+ preComments,
1888
+ })
1889
+ : existing.kind === "OpDef" && (isRecursive || !isPendingRecursive)
1890
+ ? existing
1891
+ : undefined;
1892
+ // A function whose RECURSIVE declaration has parameters stays undefined.
1893
+ if (isRecursive) {
1894
+ if (existing.arity === 0) endOpDefNode(existing, node, location, preComments);
1895
+ } else if (existing === undefined && definitionNode !== undefined) {
1896
+ addSymbol(symbolTable, definitionNode);
1897
+ setRecursionFields(definitionNode);
1898
+ }
1899
+ if (definitionNode !== undefined) {
1900
+ currentModule.definitions.push(definitionNode);
1901
+ definitions?.push(definitionNode);
1902
+ }
1903
+ functions.unshift({ name: definition.name, node });
1904
+ pushLabelScope();
1905
+ // The bound identifiers are bound in the body only of a function with an OpDefNode.
1906
+ const generateBody = () => generateWithParams(bounds.bounded.flat(), definition.body);
1907
+ const body =
1908
+ definitionNode === undefined ? generateBody() : withContext(context, generateBody);
1909
+ const labels = popLabels();
1910
+ if (definitionNode !== undefined) definitionNode.labels = labels;
1911
+ functions.shift();
1912
+ node.operands = [body];
1913
+ if (node.operator.name === "$NonRecursiveFcnSpec") node.unbounded = undefined;
1914
+ };
1915
+
1916
+ // The module's operators declared in RECURSIVE statements.
1917
+ const recursiveDeclarations: OpDefNode[] = [];
1918
+ const processRecursive = (declarations: Declaration[]) => {
1919
+ if (state.unresolvedSum === 0) state.recursiveSectionCount++;
1920
+ state.unresolvedCount[state.curLevel] += declarations.length;
1921
+ state.unresolvedSum += declarations.length;
1922
+ declarations.forEach((declaration) => {
1923
+ const span = getDeclarationSpan(declaration);
1924
+ // The OpDeclNode buildParameter makes of the declaration to read its name and arity.
1925
+ getId();
1926
+ const params = Array.from({ length: declaration.arity }, () =>
1927
+ createFormalParam("", 0, undefined, false),
1928
+ );
1929
+ const node = createOpDef({
1930
+ name: resolveSynonym(declaration.name),
1931
+ params,
1932
+ location: getSpanLocation(file, span),
1933
+ isDefined: false,
1934
+ inRecursive: true,
1935
+ inRecursiveSection: true,
1936
+ recursiveSection: state.recursiveSectionCount,
1937
+ letInLevel: state.curLevel,
1938
+ });
1939
+ addSymbol(symbolTable, node);
1940
+ recursiveDeclarations.push(node);
1941
+ currentModule.opDefsInRecursiveSection.push(node);
1942
+ });
1943
+ };
1944
+
1945
+ const checkForUndefinedRecursiveOps = () => {
1946
+ if (state.unresolvedCount[state.curLevel] === 0) return;
1947
+ recursiveDeclarations
1948
+ .filter((node) => node.letInLevel === state.curLevel && !node.isDefined)
1949
+ .forEach((node) =>
1950
+ addError(
1951
+ node.location,
1952
+ `Symbol ${node.name} declared in RECURSIVE statement but not defined.`,
1953
+ ),
1954
+ );
1955
+ state.unresolvedSum -= state.unresolvedCount[state.curLevel];
1956
+ };
1957
+
1958
+ // SubstInNode's constructor and processSubst: an implicit substitution for each declaration of
1959
+ // the instantiated module whose name the instantiating module can resolve, in the order of the
1960
+ // instantiated module's Hashtable, then the explicit WITH substitutions.
1961
+ const processSubstitutions = (
1962
+ instance: Instance,
1963
+ instanceeContext: Context,
1964
+ location: SanyLocation,
1965
+ ) => {
1966
+ // The SubstInNode processSubst creates to hold the substitutions, which only their
1967
+ // templates use.
1968
+ getId();
1969
+ const declarations = getHashtableValues(instanceeContext.table).filter(
1970
+ (symbol): symbol is OpDeclNode => symbol.kind === "OpDecl",
1971
+ );
1972
+ const substs: Subst[] = declarations.flatMap((declaration) => {
1973
+ const symbol = resolveSymbol(symbolTable, declaration.name);
1974
+ if (symbol === undefined) return [];
1975
+ const isExpression =
1976
+ declaration.declKind === VARIABLE_DECL_KIND ||
1977
+ (declaration.declKind === CONSTANT_DECL_KIND && declaration.arity === 0);
1978
+ return [
1979
+ {
1980
+ op: declaration,
1981
+ expr: isExpression ? createOpAppl(symbol, [], location) : createOpArg(symbol, location),
1982
+ isImplicit: true,
1983
+ },
1984
+ ];
1985
+ });
1986
+ // Generator.checkSubstitutionForCorrectArity, which SANY locates in its builtins for the
1987
+ // nullOpArg it substitutes after an error.
1988
+ const checkArity = (target: OpDeclNode, expr: ExprOrOpArgNode) => {
1989
+ const arity = expr.kind === "OpArg" ? getSymbolArity(expr.op) : 0;
1990
+ const isNullOpArg = target.arity > 0 && expr.kind === "OpAppl" && expr.isNull === true;
1991
+ if (arity !== target.arity && !isNullOpArg) {
1992
+ addError(
1993
+ expr.location,
1994
+ `An operator must be substituted for symbol '${target.name}', and it must have arity ${target.arity}.`,
1995
+ );
1996
+ }
1997
+ };
1998
+ const span = getDefinitionSpan(instance);
1999
+ // The targets follow WITH, after the parameters of a definition that instantiates.
2000
+ const cursor = { value: findToken(file, span.start, span.end, "WITH") + 1 };
2001
+ instance.substitutions.forEach((substitution) => {
2002
+ const targetIndex = findToken(file, cursor.value, span.end, substitution.name);
2003
+ // The next target follows this substitution's expression, which may name it too.
2004
+ if (targetIndex !== -1) {
2005
+ cursor.value = getExpressionSpan(file, substitution.expression).end + 1;
2006
+ }
2007
+ const target = getContextSymbol(instanceeContext, resolveSynonym(substitution.name));
2008
+ if (target?.kind !== "OpDecl") {
2009
+ addError(
2010
+ targetIndex === -1 ? location : getTokenLocation(file, targetIndex),
2011
+ `Identifier '${targetIndex === -1 ? substitution.name : getTokenSource(file, targetIndex)}' is not a legal target of a substitution. \nA legal target must be a declared CONSTANT or VARIABLE in the module being instantiated. \n(Also, check for warnings about multiple declarations of this same identifier.)`,
2012
+ );
2013
+ return;
2014
+ }
2015
+ const expr =
2016
+ target.arity === 0
2017
+ ? generateExpression(substitution.expression)
2018
+ : generateOperatorArgument(substitution.expression, target);
2019
+ checkArity(target, expr);
2020
+ const existing = substs.find((subst) => subst.op.name === target.name);
2021
+ if (existing && !existing.isImplicit) {
2022
+ addError(
2023
+ getLocation(substitution.expression),
2024
+ `Multiple substitutions for symbol '${target.name}' in substitution.`,
2025
+ );
2026
+ } else if (existing) {
2027
+ existing.expr = expr;
2028
+ existing.isImplicit = false;
2029
+ } else substs.push({ op: target, expr, isImplicit: false });
2030
+ });
2031
+ // TLC 1.7.4's SANY checks the arity of only the substitutions that the INSTANCE writes.
2032
+ substs
2033
+ .filter((subst) => subst.isImplicit && options.version !== "1.7.4")
2034
+ .forEach((subst) => checkArity(subst.op, subst.expr));
2035
+ // SubstInNode.matchAll.
2036
+ declarations
2037
+ .filter((declaration) => !substs.some((subst) => subst.op.name === declaration.name))
2038
+ .forEach((declaration) =>
2039
+ addError(
2040
+ location,
2041
+ `Substitution missing for symbol ${declaration.name} declared at ${formatSanyLocation(declaration.location)} \nand instantiated in module ${currentModule.name}.`,
2042
+ ),
2043
+ );
2044
+ return substs;
2045
+ };
2046
+
2047
+ const createSubstIn = (
2048
+ kind: "SubstIn" | "APSubstIn",
2049
+ substs: Subst[],
2050
+ body: LevelNode,
2051
+ instantiatedModule: ModuleNode,
2052
+ location: SanyLocation,
2053
+ instantiatingModule = currentModule,
2054
+ ): SubstInNode => {
2055
+ if (isAbsent(body)) {
2056
+ addError(
2057
+ location,
2058
+ kind === "SubstIn"
2059
+ ? "Substitution error, probably due to error \nin module being instantiated."
2060
+ : "Substitution error, probably due to error in module being instantiated.",
2061
+ );
2062
+ }
2063
+ return {
2064
+ ...createLevelFields(getId()),
2065
+ kind,
2066
+ substs,
2067
+ body,
2068
+ instantiatingModule,
2069
+ instantiatedModule,
2070
+ location,
2071
+ };
2072
+ };
2073
+
2074
+ const processModuleDefinition = (
2075
+ instance: Instance,
2076
+ definitions: (OpDefNode | ThmOrAssumpDefNode)[] | undefined,
2077
+ instances: InstanceNode[] | undefined,
2078
+ ) => {
2079
+ const span = getDefinitionSpan(instance);
2080
+ const location = getSpanLocation(file, span);
2081
+ const preComments = getPreComments(file, span.start);
2082
+ const name = instance.name ?? "";
2083
+ const parameterContext = createContext();
2084
+ const nameIndex = instance.isLocal ? span.start + 1 : span.start;
2085
+ if (instance.params.length > 0) symbolTable.contexts.push(parameterContext);
2086
+ const args = getParams(
2087
+ instance,
2088
+ { start: nameIndex, end: span.end },
2089
+ instance.params.length > 0,
2090
+ );
2091
+ const instanceeModule = getModule(instance.module);
2092
+ const instanceeContext = instanceeModule.context;
2093
+ const substs = processSubstitutions(instance, instanceeContext, location);
2094
+ if (instance.params.length > 0) symbolTable.contexts.pop();
2095
+ getHashtableValues(instanceeContext.table).forEach((symbol) => {
2096
+ if (symbol.kind !== "OpDef" || symbol.isLocal) return;
2097
+ if (symbol.defKind !== "UserDefined" && symbol.defKind !== "ModuleInstance") return;
2098
+ const params = [...args, ...(symbol.params ?? [])];
2099
+ const qualifiedName = `${name}!${symbol.name}`;
2100
+ const node =
2101
+ symbol.defKind === "UserDefined"
2102
+ ? createOpDef({
2103
+ name: qualifiedName,
2104
+ params,
2105
+ body:
2106
+ substs.length > 0 && symbol.body
2107
+ ? createSubstIn("SubstIn", substs, symbol.body, instanceeModule, location)
2108
+ : symbol.body,
2109
+ isLocal: instance.isLocal,
2110
+ location,
2111
+ source: symbol.source ?? symbol,
2112
+ preComments,
2113
+ labels: symbol.labels,
2114
+ })
2115
+ : createOpDef({
2116
+ defKind: "ModuleInstance",
2117
+ name: qualifiedName,
2118
+ params,
2119
+ isLocal: instance.isLocal,
2120
+ location,
2121
+ module: symbol.module,
2122
+ source: symbol.source ?? symbol,
2123
+ preComments,
2124
+ });
2125
+ addSymbol(symbolTable, node);
2126
+ if (symbol.defKind === "UserDefined") setRecursionFields(node);
2127
+ (definitions ?? currentModule.definitions).push(node);
2128
+ });
2129
+ getHashtableValues(instanceeContext.table).forEach((symbol) => {
2130
+ if (symbol.kind !== "ThmOrAssumpDef" || symbol.isLocal) return;
2131
+ // SANY creates the APSubstInNode before the ThmOrAssumpDefNode that holds it.
2132
+ const body =
2133
+ substs.length > 0
2134
+ ? createSubstIn("APSubstIn", substs, symbol.body, instanceeModule, location)
2135
+ : symbol.body;
2136
+ const node: ThmOrAssumpDefNode = {
2137
+ ...createLevelFields(getId()),
2138
+ kind: "ThmOrAssumpDef",
2139
+ name: `${name}!${symbol.name}`,
2140
+ isTheorem: symbol.isTheorem,
2141
+ body,
2142
+ params: [...args, ...symbol.params],
2143
+ isLocal: instance.isLocal,
2144
+ location,
2145
+ module: currentModule,
2146
+ source: symbol.source ?? symbol,
2147
+ labels: symbol.labels,
2148
+ isLeibnizArg: [],
2149
+ isLeibniz: false,
2150
+ opLevelCond: [],
2151
+ maxLevels: [],
2152
+ minMaxLevel: [],
2153
+ };
2154
+ addSymbol(symbolTable, node);
2155
+ (definitions ?? currentModule.definitions).push(node);
2156
+ });
2157
+ const instanceNode: InstanceNode = {
2158
+ ...createLevelFields(getId()),
2159
+ kind: "Instance",
2160
+ name,
2161
+ isLocal: instance.isLocal,
2162
+ params: args,
2163
+ module: instanceeModule,
2164
+ substs,
2165
+ location,
2166
+ };
2167
+ (instances ?? currentModule.topLevel).push(instanceNode);
2168
+ const moduleDefinition = createOpDef({
2169
+ defKind: "ModuleInstance",
2170
+ name,
2171
+ params: args,
2172
+ isLocal: instance.isLocal,
2173
+ location,
2174
+ preComments,
2175
+ });
2176
+ addSymbol(symbolTable, moduleDefinition);
2177
+ // TLC 1.7.4's SANY leaves the definition out of the definitions of a LET.
2178
+ if (options.version !== "1.7.4") definitions?.push(moduleDefinition);
2179
+ };
2180
+
2181
+ // generateInstance: an INSTANCE step of a proof is located at the step's body and adds no
2182
+ // theorem definitions or instance to the module.
2183
+ const generateInstance = (
2184
+ instance: Instance,
2185
+ isTopLevel = true,
2186
+ stepLocation?: SanyLocation,
2187
+ ) => {
2188
+ const span = getDefinitionSpan(instance);
2189
+ const location = stepLocation ?? getSpanLocation(file, span);
2190
+ const preComments = getPreComments(file, span.start);
2191
+ const instanceeModule = getModule(instance.module);
2192
+ const instanceeContext = instanceeModule.context;
2193
+ const substs = processSubstitutions(instance, instanceeContext, location);
2194
+ const isLocal = instance.isLocal;
2195
+ const isInstanceeParameterFree = isParameterFree(instanceeModule);
2196
+ getHashtableValues(instanceeContext.table).forEach((symbol) => {
2197
+ if (symbol.kind !== "OpDef" || symbol.defKind !== "UserDefined" || symbol.isLocal) return;
2198
+ const copyFrom = (module: ModuleNode | undefined, body: ExprNode | undefined) => {
2199
+ const node = createOpDef({
2200
+ name: symbol.name,
2201
+ params: symbol.params,
2202
+ body,
2203
+ isLocal,
2204
+ location,
2205
+ module,
2206
+ source: symbol.source ?? symbol,
2207
+ preComments,
2208
+ labels: symbol.labels,
2209
+ });
2210
+ addSymbol(symbolTable, node);
2211
+ return node;
2212
+ };
2213
+ const reuse = () => {
2214
+ addSymbol(symbolTable, symbol);
2215
+ return symbol;
2216
+ };
2217
+ const isOriginParameterFree = symbol.module === undefined || isParameterFree(symbol.module);
2218
+ const node = !isInstanceeParameterFree
2219
+ ? isOriginParameterFree
2220
+ ? isLocal
2221
+ ? copyFrom(symbol.module, symbol.body)
2222
+ : reuse()
2223
+ : copyFrom(
2224
+ currentModule,
2225
+ symbol.body &&
2226
+ createSubstIn("SubstIn", substs, symbol.body, instanceeModule, location),
2227
+ )
2228
+ : isLocal
2229
+ ? copyFrom(symbol.module, symbol.body)
2230
+ : reuse();
2231
+ currentModule.definitions.push(node);
2232
+ setRecursionFields(node);
2233
+ });
2234
+ // SANY reuses a theorem of a parameter-free module without adding it to the symbol table
2235
+ // when the instantiated module has parameters.
2236
+ getHashtableValues(instanceeContext.table).forEach((symbol) => {
2237
+ if (symbol.kind !== "ThmOrAssumpDef" || symbol.isLocal) return;
2238
+ const copyFrom = (module: ModuleNode, body: LevelNode) => {
2239
+ const node: ThmOrAssumpDefNode = {
2240
+ ...symbol,
2241
+ ...createLevelFields(getId()),
2242
+ body,
2243
+ isLocal,
2244
+ location,
2245
+ module,
2246
+ source: symbol.source ?? symbol,
2247
+ };
2248
+ addSymbol(symbolTable, node);
2249
+ return node;
2250
+ };
2251
+ const reuse = () => {
2252
+ addSymbol(symbolTable, symbol);
2253
+ return symbol;
2254
+ };
2255
+ const node = !isInstanceeParameterFree
2256
+ ? isParameterFree(symbol.module)
2257
+ ? isLocal
2258
+ ? copyFrom(currentModule, symbol.body)
2259
+ : symbol
2260
+ : copyFrom(
2261
+ currentModule,
2262
+ createSubstIn("APSubstIn", substs, symbol.body, instanceeModule, location),
2263
+ )
2264
+ : isLocal
2265
+ ? copyFrom(symbol.module, symbol.body)
2266
+ : reuse();
2267
+ if (isTopLevel) currentModule.definitions.push(node);
2268
+ });
2269
+ const instanceNode: InstanceNode = {
2270
+ ...createLevelFields(getId()),
2271
+ kind: "Instance",
2272
+ name: undefined,
2273
+ isLocal,
2274
+ params: [],
2275
+ module: instanceeModule,
2276
+ substs,
2277
+ location,
2278
+ };
2279
+ if (isTopLevel) currentModule.topLevel.push(instanceNode);
2280
+ return instanceNode;
2281
+ };
2282
+
2283
+ // SANY leaves out a module it can't resolve, after reporting it at the module's name.
2284
+ const getFactModules = (fact: Fact & { kind: "ModuleFact" }) => {
2285
+ const module =
2286
+ resolveModule(fact.name) ?? (fact.name === currentModule.name ? currentModule : undefined);
2287
+ if (module !== undefined) return [module];
2288
+ addError(
2289
+ getTokenLocation(file, getTokenIndex(file, fact.location) + 1),
2290
+ `Module \`${fact.name}' used without being extended or instantiated.`,
2291
+ );
2292
+ return [];
2293
+ };
2294
+
2295
+ // A fact is a module, or an expression such as the name of a theorem; a DEF names a defined
2296
+ // operator, theorem or assumption, or a module.
2297
+ const generateFacts = (facts: Fact[]) =>
2298
+ facts.flatMap((fact): FactNode[] => {
2299
+ if (fact.kind === "ModuleFact") return getFactModules(fact);
2300
+ const items = getSelectorItems(fact);
2301
+ if (items === undefined || !isGeneralId(items)) return [generateExpression(fact)];
2302
+ const node = generateSelector(fact, 0, true, false);
2303
+ if (node.kind === "ThmOrAssumpDef") throw new TlaSyntaxError("Expected a fact");
2304
+ return [node];
2305
+ });
2306
+
2307
+ const isParenthesized = (span: TokenSpan) =>
2308
+ isToken(file, span.start, "(") && getMatchingIndex(file, span.start) === span.end;
2309
+ const getOperandSpan = (expression: Expression) =>
2310
+ getParenthesizedSpan(file, getExpressionSpan(file, expression));
2311
+ // How SANY's parser shapes an application: as a prefix, infix or postfix operator's, or a name's.
2312
+ const getApplicationKind = (expression: Expression & { kind: "Apply" }, span: TokenSpan) => {
2313
+ if (getOperandSpan(expression.args[0]).start === span.start) {
2314
+ return expression.args.length === 2 ? "N_InfixExpr" : "N_PostfixExpr";
2315
+ }
2316
+ return file.tokens[span.start].kind === "Identifier" ? "N_OpApplication" : "N_PrefixExpr";
2317
+ };
2318
+ // The kind of SANY's syntax node for an expression, or for a token its text.
2319
+ const getSyntaxKind = (expression: Expression, span: TokenSpan) => {
2320
+ if (isParenthesized(span)) return "N_ParenExpr";
2321
+ if (expression.kind === "String") return getTokenSource(file, span.start);
2322
+ if (expression.kind === "Apply") return getApplicationKind(expression, span);
2323
+ return SYNTAX_KINDS.get(expression.kind);
2324
+ };
2325
+ // Whether the first child of an expression's syntax node is a node without children, which
2326
+ // Generator.genIdToSelector takes for an empty prefix of a name. A string is a token itself.
2327
+ const hasChildlessFirstChild = (expression: Expression, span: TokenSpan) => {
2328
+ if (expression.kind === "String" && !isParenthesized(span)) return false;
2329
+ if (expression.kind === "Identifier" && !expression.name.includes("!")) return true;
2330
+ const kind = getSyntaxKind(expression, span);
2331
+ return kind === undefined || NESTED_FIRST_CHILD_KINDS.has(kind) ? undefined : true;
2332
+ };
2333
+ // Generator.genIdToSelector reads the syntax node of a DEF clause entry that isn't a name as
2334
+ // a name: its first child as the name's prefix and its second as the name's last part.
2335
+ const getDefEntryParts = (
2336
+ expression: Expression,
2337
+ span: TokenSpan,
2338
+ ): DefEntryParts | undefined => {
2339
+ const getPart = (operand: Expression, partSpan = getOperandSpan(operand)) => ({
2340
+ last: partSpan,
2341
+ lastKind: getSyntaxKind(operand, partSpan),
2342
+ });
2343
+ if (isParenthesized(span)) {
2344
+ const inner = { start: span.start + 1, end: span.end - 1 };
2345
+ return { hasChildlessFirstChild: false, ...getPart(expression, inner) };
2346
+ }
2347
+ switch (expression.kind) {
2348
+ case "If":
2349
+ case "BoxAction":
2350
+ case "AngleAction":
2351
+ return {
2352
+ hasChildlessFirstChild: false,
2353
+ ...getPart(expression.kind === "If" ? expression.condition : expression.action),
2354
+ };
2355
+ case "Tuple":
2356
+ case "SetEnumeration": {
2357
+ const first = expression.elements[0];
2358
+ return first === undefined
2359
+ ? {
2360
+ hasChildlessFirstChild: false,
2361
+ last: { start: span.end, end: span.end },
2362
+ lastKind: file.tokens[span.end].text,
2363
+ }
2364
+ : { hasChildlessFirstChild: false, ...getPart(first) };
2365
+ }
2366
+ case "Always":
2367
+ case "Eventually":
2368
+ case "Enabled":
2369
+ case "Unchanged":
2370
+ return {
2371
+ hasChildlessFirstChild: true,
2372
+ ...getPart(expression.expression, { start: span.start + 1, end: span.end }),
2373
+ };
2374
+ case "Prime":
2375
+ return {
2376
+ hasChildlessFirstChild: hasChildlessFirstChild(
2377
+ expression.expression,
2378
+ getOperandSpan(expression.expression),
2379
+ ),
2380
+ last: { start: span.end, end: span.end },
2381
+ lastKind: "N_GenPostfixOp",
2382
+ };
2383
+ case "FunctionApplication":
2384
+ case "FieldAccess": {
2385
+ const operand =
2386
+ expression.kind === "FunctionApplication" ? expression.function : expression.record;
2387
+ const operandSpan = getOperandSpan(operand);
2388
+ const next = operandSpan.end + 1;
2389
+ return {
2390
+ hasChildlessFirstChild: hasChildlessFirstChild(operand, operandSpan),
2391
+ last: { start: next, end: next },
2392
+ lastKind: file.tokens[next].text,
2393
+ };
2394
+ }
2395
+ case "Apply": {
2396
+ const kind = getApplicationKind(expression, span);
2397
+ if (kind === "N_OpApplication") {
2398
+ return expression.operator.includes("!")
2399
+ ? undefined
2400
+ : {
2401
+ hasChildlessFirstChild: true,
2402
+ last: { start: span.start + 1, end: span.end },
2403
+ lastKind: "N_OpArgs",
2404
+ };
2405
+ }
2406
+ if (kind === "N_PrefixExpr") {
2407
+ return {
2408
+ hasChildlessFirstChild: true,
2409
+ ...getPart(expression.args[0], { start: span.start + 1, end: span.end }),
2410
+ };
2411
+ }
2412
+ const left = getOperandSpan(expression.args[0]);
2413
+ const operator = left.end + 1;
2414
+ return {
2415
+ hasChildlessFirstChild: hasChildlessFirstChild(expression.args[0], left),
2416
+ last: { start: operator, end: operator },
2417
+ lastKind: kind === "N_InfixExpr" ? "N_GenInfixOp" : "N_GenPostfixOp",
2418
+ };
2419
+ }
2420
+ }
2421
+ return undefined;
2422
+ };
2423
+ // The errors of Generator.genIdToSelector and selectorToNode for a DEF clause entry that isn't
2424
+ // a name, or false where SANY's reading of the entry isn't modeled.
2425
+ const reportDefEntry = (expression: Expression) => {
2426
+ const span = getParenthesizedSpan(file, getExpressionSpan(file, expression));
2427
+ const isName =
2428
+ expression.kind === "Identifier" ||
2429
+ expression.kind === "Subexpression" ||
2430
+ expression.kind === "OperatorReference";
2431
+ if (isName && !isParenthesized(span)) return false;
2432
+ const parts = getDefEntryParts(expression, span);
2433
+ if (parts?.hasChildlessFirstChild === undefined || parts.lastKind === undefined) return false;
2434
+ const lastLocation = getSpanLocation(file, parts.last);
2435
+ if (parts.hasChildlessFirstChild) {
2436
+ addError(getSpanLocation(file, span), "Was expecting a GeneralId.");
2437
+ }
2438
+ if (parts.lastKind === "N_OpArgs") {
2439
+ addError(lastLocation, "Selector `!(...)' should not have argument(s).");
2440
+ } else {
2441
+ addError(lastLocation, "Unexpected token found.");
2442
+ addError(lastLocation, `Need name or step number here, not \`${parts.lastKind}'.`);
2443
+ }
2444
+ addError(getSpanLocation(file, span), "DEF clause entry should describe a defined operator.");
2445
+ return true;
2446
+ };
2447
+
2448
+ const generateDefs = (defs: Fact[]) =>
2449
+ defs.flatMap((fact): UseOrHideNode["defs"] => {
2450
+ if (fact.kind === "ModuleFact") return getFactModules(fact);
2451
+ if (reportDefEntry(fact)) return [];
2452
+ const symbol = generateSelector(
2453
+ fact.kind === "OperatorReference"
2454
+ ? { kind: "Identifier", name: fact.operator, location: fact.location }
2455
+ : fact,
2456
+ -1,
2457
+ false,
2458
+ true,
2459
+ );
2460
+ // SANY rejects the name of a proof step in a DEF clause, and leaves out an entry it rejects.
2461
+ // Errors.addMessage drops the message when selectorToNode already reported it.
2462
+ const isDefinition =
2463
+ symbol.kind === "OpDef" ||
2464
+ (symbol.kind === "ThmOrAssumpDef" && !symbol.name.startsWith("<"));
2465
+ if (!isDefinition) {
2466
+ addError(getLocation(fact), "DEF clause entry should describe a defined operator.");
2467
+ return [];
2468
+ }
2469
+ return [symbol];
2470
+ });
2471
+
2472
+ // SANY's generateUseOrHide rejects a BY, USE, or HIDE with nothing after its keyword before it
2473
+ // reads the facts.
2474
+ const checkKeywordOnly = (
2475
+ statement: UseOrHide | Extract<Proof, { kind: "By" }>,
2476
+ location: SanyLocation,
2477
+ ) => {
2478
+ if (statement.isOnly || statement.facts.length + statement.defs.length > 0) return;
2479
+ addError(location, "Empty BY, USE, or HIDE");
2480
+ };
2481
+
2482
+ const generateUseOrHide = (use: UseOrHide): UseOrHideNode => {
2483
+ const location = getSpanLocation(file, getUnitSpan(use.location));
2484
+ checkKeywordOnly(use, location);
2485
+ const facts = generateFacts(use.facts);
2486
+ const defs = generateDefs(use.defs);
2487
+ if (facts.length + defs.length === 0) addError(location, "Empty USE or HIDE statement.");
2488
+ // UseOrHideNode.factCheck, after SANY generates the node: a HIDE cites facts by name only.
2489
+ if (use.kind === "Hide") {
2490
+ facts
2491
+ .filter((fact) => fact.kind === "OpAppl" && fact.operator.kind !== "ThmOrAssumpDef")
2492
+ .forEach((fact) =>
2493
+ addError(
2494
+ fact.location,
2495
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
2496
+ ),
2497
+ );
2498
+ }
2499
+ return {
2500
+ ...createLevelFields(getId()),
2501
+ kind: "UseOrHide",
2502
+ facts,
2503
+ defs,
2504
+ isOnly: use.isOnly,
2505
+ isHide: use.kind === "Hide",
2506
+ location,
2507
+ };
2508
+ };
2509
+
2510
+ // generateNewSymb: the set is generated before the symbol is declared.
2511
+ const generateNewSymbol = (symbol: NewSymbol): NewSymbNode => {
2512
+ const span = getUnitSpan(symbol.location);
2513
+ const set = symbol.set && generateExpression(symbol.set);
2514
+ const nameIndex = findNameOrKeyword(span.start, symbol.name);
2515
+ const kind = NEW_SYMBOL_KINDS[symbol.level];
2516
+ const declaration: OpDeclNode = {
2517
+ ...createLevelFields(getId()),
2518
+ levelChecked: 1,
2519
+ level: kind.level,
2520
+ kind: "OpDecl",
2521
+ name: resolveSynonym(symbol.name),
2522
+ arity: symbol.arity,
2523
+ declKind: kind.declKind,
2524
+ location: getSpanLocation(file, getOperatorSpan(nameIndex, symbol.arity)),
2525
+ module: currentModule,
2526
+ };
2527
+ addSymbol(symbolTable, declaration);
2528
+ return {
2529
+ ...createLevelFields(getId()),
2530
+ kind: "NewSymb",
2531
+ declaration,
2532
+ set,
2533
+ location: getSpanLocation(file, span),
2534
+ };
2535
+ };
2536
+
2537
+ // generateAssumeProve: an ASSUME/PROVE nested in another one has a context of its own.
2538
+ const generateAssumeProve = (
2539
+ assumeProve: AssumeProve,
2540
+ depth: number,
2541
+ goal?: { isSuffices: boolean },
2542
+ ): AssumeProveNode => {
2543
+ const span = getUnitSpan(assumeProve.location);
2544
+ const isBoxed = isBoxToken(file, span.start - 1);
2545
+ const conclusionStart = getExpressionSpan(file, assumeProve.conclusion).start;
2546
+ const proveIndex = file.tokens.findLastIndex(
2547
+ (token, index) =>
2548
+ index < conclusionStart && token.kind === "Keyword" && token.text === "PROVE",
2549
+ );
2550
+ const assumeLocation = getSpanLocation(file, {
2551
+ start: isBoxed ? span.start - 1 : span.start,
2552
+ end: span.start,
2553
+ });
2554
+ if (isBoxed !== isBoxToken(file, proveIndex - 1)) {
2555
+ addError(
2556
+ assumeLocation,
2557
+ isBoxed
2558
+ ? "[]ASSUME matched by PROVE instead of []PROVE"
2559
+ : "ASSUME matched by []PROVE instead of PROVE",
2560
+ );
2561
+ }
2562
+ // SANY creates the AssumeProveNode, numbering it, before it generates its clauses.
2563
+ const id = getId();
2564
+ if (depth > 1) symbolTable.contexts.push(createContext());
2565
+ if (isBoxed && symbolTable.contexts.some((context) => assumeContexts.has(context))) {
2566
+ addError(
2567
+ assumeLocation,
2568
+ "[]ASSUME used within the scope of an ordinary ASSUME's assumptions",
2569
+ );
2570
+ }
2571
+ if (!isBoxed) assumeContexts.add(getTopContext(symbolTable));
2572
+ const scope: AssumeProveScope = {
2573
+ isDeclaredBefore: [false],
2574
+ hasGoal: goal !== undefined,
2575
+ isSuffices: goal?.isSuffices ?? false,
2576
+ isInProof: true,
2577
+ };
2578
+ assumeProveState.depth = depth;
2579
+ assumeProveState.isDeclared[depth] = false;
2580
+ if (depth === 1) {
2581
+ assumeProveState.goal = goal && scope;
2582
+ assumeProveState.goalClause = 0;
2583
+ }
2584
+ const assumptions = assumeProve.assumptions.map((assumption, index): LevelNode => {
2585
+ const isNew = assumption.kind === "NewSymbol";
2586
+ const node =
2587
+ assumption.kind === "AssumeProve"
2588
+ ? generateAssumeProve(assumption, depth + 1)
2589
+ : isNew
2590
+ ? generateNewSymbol(assumption)
2591
+ : generateExpression(assumption);
2592
+ assumeProveState.depth = depth;
2593
+ if (isNew) assumeProveState.isDeclared[depth] = true;
2594
+ scope.isDeclaredBefore.push(scope.isDeclaredBefore[index] || isNew);
2595
+ if (depth === 1) assumeProveState.goalClause++;
2596
+ return node;
2597
+ });
2598
+ const conclusion = generateExpression(assumeProve.conclusion);
2599
+ if (depth > 1) symbolTable.contexts.pop();
2600
+ assumeProveState.depth = depth - 1;
2601
+ if (depth === 1) assumeProveState.goal = undefined;
2602
+ const node: AssumeProveNode = {
2603
+ ...createLevelFields(id),
2604
+ kind: "AssumeProve",
2605
+ assumptions,
2606
+ goal: conclusion,
2607
+ isSuffices: scope.isSuffices,
2608
+ isBoxed,
2609
+ location: getSpanLocation(file, {
2610
+ start: isBoxed ? span.start - 1 : span.start,
2611
+ end: span.end,
2612
+ }),
2613
+ };
2614
+ assumeProveScopes.set(node, scope);
2615
+ return node;
2616
+ };
2617
+
2618
+ // SANY creates the ThmOrAssumpDefNode of a named theorem or proof step, numbering it, before it
2619
+ // generates what it asserts.
2620
+ const createTheoremDefinition = (
2621
+ id: number,
2622
+ name: string,
2623
+ body: LevelNode,
2624
+ location: SanyLocation,
2625
+ labels: Map<string, LabelNode>,
2626
+ isTheorem = true,
2627
+ ): ThmOrAssumpDefNode => ({
2628
+ ...createLevelFields(id),
2629
+ kind: "ThmOrAssumpDef",
2630
+ name,
2631
+ isTheorem,
2632
+ body,
2633
+ params: [],
2634
+ isLocal: false,
2635
+ location,
2636
+ module: currentModule,
2637
+ source: undefined,
2638
+ labels,
2639
+ isLeibnizArg: [],
2640
+ isLeibniz: false,
2641
+ opLevelCond: [],
2642
+ maxLevels: [],
2643
+ minMaxLevel: [],
2644
+ });
2645
+
2646
+ const generateLeafProof = (proof: Exclude<Proof, { kind: "Steps" }>): LeafProofNode => {
2647
+ const location = getSpanLocation(file, getUnitSpan(proof.location));
2648
+ if (proof.kind === "By") checkKeywordOnly(proof, location);
2649
+ const facts = proof.kind === "By" ? generateFacts(proof.facts) : [];
2650
+ const defs = proof.kind === "By" ? generateDefs(proof.defs) : [];
2651
+ // SANY reads a BY proof's facts and definitions from the UseOrHideNode it makes of them.
2652
+ if (proof.kind === "By") getId();
2653
+ if (proof.kind === "By" && facts.length + defs.length === 0) addError(location, "Empty BY");
2654
+ return {
2655
+ ...createLevelFields(getId()),
2656
+ kind: "LeafProof",
2657
+ facts,
2658
+ defs,
2659
+ isOmitted: proof.kind === "Omitted",
2660
+ isOnly: proof.kind === "By" && proof.isOnly,
2661
+ location,
2662
+ };
2663
+ };
2664
+
2665
+ const generateStepBody = (step: ProofStep, bodySpan: TokenSpan) => {
2666
+ const body = step.body;
2667
+ const bodyLocation = getSpanLocation(file, bodySpan);
2668
+ switch (body.kind) {
2669
+ case "Have":
2670
+ return {
2671
+ node: applyBuiltin("$Have", [generateExpression(body.expression)], bodyLocation),
2672
+ isPick: false,
2673
+ };
2674
+ case "Case":
2675
+ return {
2676
+ node: applyBuiltin("$Pfcase", [generateExpression(body.expression)], bodyLocation),
2677
+ isPick: false,
2678
+ };
2679
+ case "Witness":
2680
+ return {
2681
+ node: applyBuiltin("$Witness", body.expressions.map(generateExpression), bodyLocation),
2682
+ isPick: false,
2683
+ };
2684
+ case "Qed":
2685
+ return { node: applyBuiltin("$Qed", [], bodyLocation), isPick: false };
2686
+ case "Take":
2687
+ case "Pick": {
2688
+ const isPick = body.kind === "Pick";
2689
+ const name = isPick ? "$Pick" : "$Take";
2690
+ if (isPick) symbolTable.contexts.push(createContext());
2691
+ const getOperands = (params: FormalParamNode[]) =>
2692
+ body.kind === "Pick" ? [generateWithParams(params, body.body)] : [];
2693
+ if (body.bounds.length > 0) {
2694
+ const bounds = generateBounds(body.bounds, bodySpan.start);
2695
+ const operands = getOperands(bounds.bounded.flat());
2696
+ return { node: applyBuiltin(name, operands, bodyLocation, bounds), isPick };
2697
+ }
2698
+ const indexes = findNameTokens(file, bodySpan.start, bodySpan.end + 1, body.names);
2699
+ const unbounded = body.names.map((paramName, index) =>
2700
+ createFormalParam(paramName, 0, getTokenLocation(file, indexes[index]), true),
2701
+ );
2702
+ const operands = getOperands(unbounded);
2703
+ return { node: applyBuiltin(name, operands, bodyLocation, { unbounded }), isPick };
2704
+ }
2705
+ }
2706
+ return undefined;
2707
+ };
2708
+
2709
+ // An expression SANY parses as N_InfixExpr: a binary operator between its operands.
2710
+ const isInfixExpression = (expression: Expression) => {
2711
+ if ((expression.kind === "And" || expression.kind === "Or") && !expression.isBulleted)
2712
+ return true;
2713
+ if (expression.kind !== "Apply" || expression.args.length !== 2) return false;
2714
+ const left = getParenthesizedSpan(file, getExpressionSpan(file, expression.args[0]));
2715
+ const start = getExpressionSpan(file, expression).start;
2716
+ return left.start === start && !isToken(file, left.end + 1, "(");
2717
+ };
2718
+
2719
+ // generateProof: a proof's steps share a context, into which the step names, the symbols of
2720
+ // a TAKE or PICK, and the NEW symbols of a SUFFICES ASSUME go.
2721
+ const generateProof = (proof: Proof): LeafProofNode | NonLeafProofNode => {
2722
+ if (proof.kind !== "Steps") return generateLeafProof(proof);
2723
+ const proofContext = createContext();
2724
+ symbolTable.contexts.push(proofContext);
2725
+ const instances: InstanceNode[] = [];
2726
+ const sufficesContexts: Context[] = [];
2727
+ // An assertion step @ = e continues the infix assertion of the step before it, whose right
2728
+ // side the @ stands for.
2729
+ const infixStep: { previousRight: ExprOrOpArgNode | undefined } = {
2730
+ previousRight: undefined,
2731
+ };
2732
+ // A DEFINE, USE, HIDE or INSTANCE step.
2733
+ const generateOtherStep = (
2734
+ body: ProofStep["body"],
2735
+ stepSpan: TokenSpan,
2736
+ bodySpan: TokenSpan,
2737
+ ): DefStepNode | UseOrHideNode | InstanceNode | undefined => {
2738
+ if (body.kind === "Define") {
2739
+ const defs = body.definitions.map((definition) => {
2740
+ const defined: (OpDefNode | ThmOrAssumpDefNode)[] = [];
2741
+ if (definition.kind === "Instance")
2742
+ processModuleDefinition(definition, defined, instances);
2743
+ else if (definition.kind === "Function") processFunction(definition, defined);
2744
+ else processOperator(definition, defined);
2745
+ const last = defined[definition.kind === "Instance" ? defined.length - 1 : 0];
2746
+ if (last?.kind !== "OpDef")
2747
+ throw new TlaSyntaxError("A DEFINE step defines no operator");
2748
+ return last;
2749
+ });
2750
+ return {
2751
+ ...createLevelFields(getId()),
2752
+ kind: "DefStep",
2753
+ defs,
2754
+ location: getSpanLocation(file, bodySpan),
2755
+ };
2756
+ }
2757
+ if (body.kind === "Use" || body.kind === "Hide") {
2758
+ return { ...generateUseOrHide(body), location: getSpanLocation(file, stepSpan) };
2759
+ }
2760
+ if (body.kind === "Instance") {
2761
+ return generateInstance(body, false, getSpanLocation(file, bodySpan));
2762
+ }
2763
+ return undefined;
2764
+ };
2765
+ const steps = proof.steps.map((step): ProofStepNode => {
2766
+ const stepSpan = getUnitSpan(step.location);
2767
+ const headSpan = getRangeSpan(file, step.head);
2768
+ const bodySpan = { start: headSpan.start + 1, end: headSpan.end };
2769
+ const body = step.body;
2770
+ if (step.name !== undefined) pushLabelScope();
2771
+ const definitionId = step.name === undefined ? undefined : getId();
2772
+ if (body.kind !== "Assert") infixStep.previousRight = undefined;
2773
+ const otherStep = generateOtherStep(body, stepSpan, bodySpan);
2774
+ if (otherStep !== undefined) {
2775
+ // SANY puts a NumberedProofStep definition for a named step that asserts nothing in the
2776
+ // symbol table, for a DEF clause to name.
2777
+ if (step.name !== undefined) {
2778
+ addSymbol(
2779
+ symbolTable,
2780
+ createOpDef({
2781
+ defKind: "NumberedProofStep",
2782
+ name: step.name,
2783
+ location: getSpanLocation(file, stepSpan),
2784
+ labels: popLabels(),
2785
+ step: otherStep,
2786
+ }),
2787
+ );
2788
+ }
2789
+ return otherStep;
2790
+ }
2791
+ const isAssumeProve = body.kind === "Assert" && body.statement.kind === "AssumeProve";
2792
+ if (isAssumeProve) infixStep.previousRight = undefined;
2793
+ const isSuffices = body.kind === "Assert" && body.isSuffices;
2794
+ const assumeContext = createContext();
2795
+ const generateAssertion = (statement: Expression | AssumeProve): LevelNode => {
2796
+ if (statement.kind === "AssumeProve") {
2797
+ const node = withContext(assumeContext, () =>
2798
+ generateAssumeProve(
2799
+ statement,
2800
+ 1,
2801
+ step.name === undefined ? undefined : { isSuffices },
2802
+ ),
2803
+ );
2804
+ node.isSuffices = isSuffices;
2805
+ return node;
2806
+ }
2807
+ if (isSuffices) {
2808
+ const expression = generateExpression(statement);
2809
+ return applyBuiltin("$Suffices", [expression], getSpanLocation(file, bodySpan));
2810
+ }
2811
+ const previousRight = infixStep.previousRight;
2812
+ const isAtStep =
2813
+ previousRight !== undefined &&
2814
+ statement.kind === "Apply" &&
2815
+ isInfixExpression(statement) &&
2816
+ statement.args[0].kind === "ExceptAt";
2817
+ // SANY generates an @ step's right side before the $Nop that stands for its @.
2818
+ const atRight = isAtStep ? generateExpression(statement.args[1]) : undefined;
2819
+ const expression = isAtStep
2820
+ ? createOpAppl(
2821
+ resolveName(statement.operator, getLocation(statement)),
2822
+ [applyBuiltin("$Nop", [previousRight], getLocation(statement.args[0])), atRight],
2823
+ getLocation(statement),
2824
+ )
2825
+ : generateExpression(statement);
2826
+ const right = expression.kind === "OpAppl" ? expression.operands[1] : undefined;
2827
+ infixStep.previousRight = isInfixExpression(statement) ? right : undefined;
2828
+ return expression;
2829
+ };
2830
+ const generated =
2831
+ body.kind === "Assert"
2832
+ ? { node: generateAssertion(body.statement), isPick: false }
2833
+ : generateStepBody(step, bodySpan);
2834
+ if (generated === undefined) throw new TlaSyntaxError(`Unexpected proof step ${body.kind}`);
2835
+ const pickContext = generated.isPick ? symbolTable.contexts.pop() : undefined;
2836
+ const definition =
2837
+ step.name === undefined || definitionId === undefined
2838
+ ? undefined
2839
+ : createTheoremDefinition(
2840
+ definitionId,
2841
+ step.name,
2842
+ generated.node,
2843
+ getTokenLocation(file, getTokenIndex(file, step.location)),
2844
+ popLabels(),
2845
+ );
2846
+ if (definition) addSymbol(symbolTable, definition);
2847
+ const isScoped = isAssumeProve && !isSuffices;
2848
+ if (isScoped) symbolTable.contexts.push(assumeContext);
2849
+ const stepProof = step.proof && generateProof(step.proof);
2850
+ if (isScoped) symbolTable.contexts.pop();
2851
+ endProof(generated.node);
2852
+ if (isAssumeProve && isSuffices) {
2853
+ sufficesContexts.push(assumeContext);
2854
+ symbolTable.contexts.push(assumeContext);
2855
+ }
2856
+ pickContext?.symbols.forEach((symbol) => addSymbol(symbolTable, symbol));
2857
+ return {
2858
+ ...createLevelFields(getId()),
2859
+ kind: "Theorem",
2860
+ definition,
2861
+ body: generated.node,
2862
+ proof: stepProof,
2863
+ isSuffices,
2864
+ location: getSpanLocation(file, stepSpan),
2865
+ };
2866
+ });
2867
+ sufficesContexts.forEach(() => {
2868
+ symbolTable.contexts.pop()?.symbols.forEach((symbol) => addToContext(proofContext, symbol));
2869
+ });
2870
+ symbolTable.contexts.pop();
2871
+ return {
2872
+ ...createLevelFields(getId()),
2873
+ kind: "NonLeafProof",
2874
+ steps,
2875
+ instances,
2876
+ proofLevel: proof.level,
2877
+ location: getSpanLocation(file, getUnitSpan(proof.location)),
2878
+ };
2879
+ };
2880
+
2881
+ // processTheorem: a theorem's ASSUME/PROVE has a context of its own, which its proof sees and
2882
+ // its definition does not.
2883
+ const processTheorem = (theorem: Theorem) => {
2884
+ const location = getSpanLocation(file, getUnitSpan(theorem.location));
2885
+ const statement = theorem.statement;
2886
+ const assumeContext = createContext();
2887
+ if (theorem.name !== undefined) pushLabelScope();
2888
+ const definitionId = theorem.name === undefined ? undefined : getId();
2889
+ if (statement.kind === "AssumeProve") symbolTable.contexts.push(assumeContext);
2890
+ const body =
2891
+ statement.kind === "AssumeProve"
2892
+ ? generateAssumeProve(
2893
+ statement,
2894
+ 1,
2895
+ theorem.name === undefined ? undefined : { isSuffices: false },
2896
+ )
2897
+ : generateExpression(statement);
2898
+ const definition =
2899
+ theorem.name === undefined || definitionId === undefined
2900
+ ? undefined
2901
+ : createTheoremDefinition(definitionId, theorem.name, body, location, popLabels());
2902
+ if (definition) {
2903
+ if (statement.kind === "AssumeProve") symbolTable.contexts.pop();
2904
+ addSymbol(symbolTable, definition);
2905
+ currentModule.definitions.push(definition);
2906
+ if (statement.kind === "AssumeProve") symbolTable.contexts.push(assumeContext);
2907
+ }
2908
+ const proof = theorem.proof && generateProof(theorem.proof);
2909
+ if (statement.kind === "AssumeProve") symbolTable.contexts.pop();
2910
+ endProof(body);
2911
+ const node: TheoremNode = {
2912
+ ...createLevelFields(getId()),
2913
+ kind: "Theorem",
2914
+ definition,
2915
+ body,
2916
+ proof,
2917
+ isSuffices: false,
2918
+ location,
2919
+ };
2920
+ currentModule.topLevel.push(node);
2921
+ };
2922
+
2923
+ const processAssumption = (assumption: Assumption) => {
2924
+ const span = getUnitSpan(assumption.location);
2925
+ const location = getSpanLocation(file, span);
2926
+ if (assumption.name !== undefined) pushLabelScope();
2927
+ const body = generateExpression(assumption.expression);
2928
+ const definition =
2929
+ assumption.name === undefined
2930
+ ? undefined
2931
+ : createTheoremDefinition(getId(), assumption.name, body, location, popLabels(), false);
2932
+ if (definition) {
2933
+ addSymbol(symbolTable, definition);
2934
+ currentModule.definitions.push(definition);
2935
+ }
2936
+ const node: AssumeNode = {
2937
+ ...createLevelFields(getId()),
2938
+ kind: "Assume",
2939
+ definition,
2940
+ body,
2941
+ location,
2942
+ };
2943
+ currentModule.topLevel.push(node);
2944
+ };
2945
+
2946
+ // A declared operator or operator parameter is located with its arguments: F(_, _), _ + _,
2947
+ // -. _ or _ ^+.
2948
+ const getOperatorSpan = (start: number, arity: number) => {
2949
+ if (arity === 0) return { start, end: start };
2950
+ if (isToken(file, start, "_")) {
2951
+ return { start, end: isToken(file, start + 2, "_") ? start + 2 : start + 1 };
2952
+ }
2953
+ if (isToken(file, start + 1, "(")) return { start, end: getMatchingIndex(file, start + 1) };
2954
+ return { start, end: isToken(file, start + 1, "_") ? start + 1 : start };
2955
+ };
2956
+
2957
+ const getDeclarationSpan = (declaration: Declaration) =>
2958
+ getOperatorSpan(getUnitSpan(declaration.location).start, declaration.arity);
2959
+
2960
+ const createDeclaration = (declaration: Declaration, declKind: number): OpDeclNode => {
2961
+ const span = getDeclarationSpan(declaration);
2962
+ // Generator.processParameters makes the same check of a constant, of the name of its
2963
+ // declaration's syntax node, which is never @.
2964
+ if (declaration.name === "@" && declKind === VARIABLE_DECL_KIND) {
2965
+ addError(getSpanLocation(file, span), "Attempted to declare '@' as a variable.");
2966
+ }
2967
+ const node: OpDeclNode = {
2968
+ ...createLevelFields(getId()),
2969
+ levelChecked: 1,
2970
+ level: declKind === VARIABLE_DECL_KIND ? 1 : 0,
2971
+ kind: "OpDecl",
2972
+ name: resolveSynonym(declaration.name),
2973
+ arity: declaration.arity,
2974
+ declKind,
2975
+ location: getSpanLocation(file, span),
2976
+ module: currentModule,
2977
+ };
2978
+ if (declKind === CONSTANT_DECL_KIND) {
2979
+ node.levelParams.add(node);
2980
+ node.allParams.add(node);
2981
+ }
2982
+ addSymbol(symbolTable, node);
2983
+ return node;
2984
+ };
2985
+
2986
+ // processExtendsList: the extended module's symbols join this module's context in the order
2987
+ // they joined the extended module's, apart from LOCAL ones and those already present.
2988
+ ast.extends.forEach((name) => {
2989
+ const extendee = getModule(name);
2990
+ currentModule.extendees.push(extendee);
2991
+ const context = getTopContext(symbolTable);
2992
+ extendee.context.symbols.forEach((symbol) => {
2993
+ if (
2994
+ symbol.kind !== "Module" &&
2995
+ symbol.kind !== "FormalParam" &&
2996
+ symbol.kind !== "OpDecl" &&
2997
+ symbol.isLocal
2998
+ ) {
2999
+ return;
3000
+ }
3001
+ const existing =
3002
+ symbol.kind === "Module"
3003
+ ? getContextModule(context, symbol.name)
3004
+ : getContextSymbol(context, symbol.name);
3005
+ if (existing === undefined) addToContext(context, symbol);
3006
+ else if (existing !== symbol && existing.kind !== symbol.kind) {
3007
+ addError(
3008
+ symbol.location,
3009
+ `The ${getKindName(symbol)} of '${symbol.name}' conflicts with \nits ${getKindName(existing)} at ${formatSanyLocation(existing.location)}.`,
3010
+ );
3011
+ } else if (existing !== symbol && !isSameOrigin(existing, symbol)) {
3012
+ // Context.mergeExtendContext names the kind of the existing symbol twice.
3013
+ addWarning(
3014
+ symbol.location,
3015
+ `Warning: the ${getKindName(existing)} of '${symbol.name}' conflicts with \nits ${getKindName(existing)} at ${formatSanyLocation(existing.location)}.`,
3016
+ );
3017
+ }
3018
+ });
3019
+ currentModule.topLevel.push(...extendee.topLevel);
3020
+ });
3021
+
3022
+ // Generator.checkIfInRecursiveSection: only definitions and instances may come between a
3023
+ // RECURSIVE declaration and the definition of what it declares.
3024
+ const checkRecursiveSection = (location: SourceLocation, type: string) => {
3025
+ if (state.unresolvedSum === 0) return;
3026
+ addError(
3027
+ getSpanLocation(file, getUnitSpan(location)),
3028
+ `${type} may not appear within a recursive definition section.`,
3029
+ );
3030
+ };
3031
+
3032
+ ast.units.forEach((unit) => {
3033
+ switch (unit.kind) {
3034
+ case "Variables":
3035
+ checkRecursiveSection(unit.location, "A VARIABLE declaration");
3036
+ unit.declarations.forEach((declaration) =>
3037
+ createDeclaration(declaration, VARIABLE_DECL_KIND),
3038
+ );
3039
+ break;
3040
+ case "Constants":
3041
+ checkRecursiveSection(unit.location, "A declaration");
3042
+ unit.declarations.forEach((declaration) =>
3043
+ createDeclaration(declaration, CONSTANT_DECL_KIND),
3044
+ );
3045
+ break;
3046
+ case "Recursive":
3047
+ processRecursive(unit.declarations);
3048
+ break;
3049
+ case "Definition":
3050
+ if (unit.definition.kind === "Function") processFunction(unit.definition, undefined);
3051
+ else processOperator(unit.definition, undefined);
3052
+ break;
3053
+ case "Instance":
3054
+ if (unit.instance.name === undefined) generateInstance(unit.instance);
3055
+ else processModuleDefinition(unit.instance, undefined, undefined);
3056
+ break;
3057
+ case "Assumption":
3058
+ checkRecursiveSection(unit.assumption.location, "An ASSUME");
3059
+ processAssumption(unit.assumption);
3060
+ break;
3061
+ case "Theorem":
3062
+ checkRecursiveSection(unit.theorem.location, "A THEOREM");
3063
+ processTheorem(unit.theorem);
3064
+ break;
3065
+ case "Use":
3066
+ checkRecursiveSection(unit.use.location, "A USE or HIDE");
3067
+ currentModule.topLevel.push(generateUseOrHide(unit.use));
3068
+ break;
3069
+ case "Module": {
3070
+ checkRecursiveSection(unit.module.location, "A MODULE ");
3071
+ const innerTable = { contexts: [...symbolTable.contexts, createContext()] };
3072
+ const inner = generateModule(unit.module, file, currentModule, innerTable);
3073
+ // SymbolTable.addModule: an inner module's name must not be taken by another module,
3074
+ // inner or external.
3075
+ const binding = resolveModule(inner.name);
3076
+ if (binding === undefined) addToContext(getTopContext(symbolTable), inner);
3077
+ else if (binding !== inner) {
3078
+ addError(
3079
+ inner.location,
3080
+ `Multiply-defined module '${inner.name}': this definition or declaration conflicts \nwith the one at ${formatSanyLocation(binding.location)}.`,
3081
+ );
3082
+ }
3083
+ break;
3084
+ }
3085
+ }
3086
+ });
3087
+ checkForUndefinedRecursiveOps();
3088
+ return currentModule;
3089
+ };
3090
+
3091
+ const modules = order.map((name) => {
3092
+ options.onAnalyze?.(name);
3093
+ // Each module's Generator creates its nullODN, nullOAN, nullOpArg and nullLabelNode.
3094
+ nullApplication.id = Array.from({ length: 4 }, getId)[1];
3095
+ const entry = getSource(name, undefined);
3096
+ const context = createContext();
3097
+ // A module's context starts as a copy of SANY's global context of builtins, whose list of
3098
+ // symbols runs from the last builtin to the first.
3099
+ builtins.toReversed().forEach((builtin) => addToContext(context, builtin));
3100
+ const module = generateModule(entry.module, entry.file, undefined, {
3101
+ contexts: [context],
3102
+ });
3103
+ externalModules.set(name, module);
3104
+ options.onModule?.(module, messages);
3105
+ return module;
3106
+ });
3107
+ return { root: modules[modules.length - 1], modules, messages };
3108
+ };
3109
+
3110
+ type LetUnit =
3111
+ | { kind: "Recursive"; declarations: Declaration[] }
3112
+ | { kind: "Instance"; instance: Instance }
3113
+ | { kind: "Definition"; definition: Definition };
3114
+
3115
+ const getNumeralValue = (text: string) => {
3116
+ const lower = text.toLowerCase();
3117
+ const radix = lower.startsWith("\\b")
3118
+ ? 2
3119
+ : lower.startsWith("\\o")
3120
+ ? 8
3121
+ : lower.startsWith("\\h")
3122
+ ? 16
3123
+ : 10;
3124
+ const digits = radix === 10 ? lower : lower.slice(2);
3125
+ return [...digits]
3126
+ .reduce((value, digit) => value * BigInt(radix) + BigInt(parseInt(digit, radix)), 0n)
3127
+ .toString();
3128
+ };