tla-ts 0.0.3-dev.a85c0bd → 0.0.3-dev.ab9c809

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.
package/src/config.ts CHANGED
@@ -74,27 +74,96 @@ const KEYWORDS = new Set([
74
74
 
75
75
  const SYMBOLS = ["<-", "<<", ">>", "=", "{", "}", ",", "[", "]", "(", ")"];
76
76
 
77
+ const SINGLE_KEYWORDS = {
78
+ INIT: "init",
79
+ NEXT: "next",
80
+ SPECIFICATION: "specification",
81
+ VIEW: "view",
82
+ SYMMETRY: "symmetry",
83
+ ALIAS: "alias",
84
+ _PERIODIC: "periodic",
85
+ _RL_REWARD: "rlReward",
86
+ } as const;
87
+
88
+ const LIST_KEYWORDS = {
89
+ INVARIANT: "invariants",
90
+ INVARIANTS: "invariants",
91
+ PROPERTY: "properties",
92
+ PROPERTIES: "properties",
93
+ CONSTRAINT: "constraints",
94
+ CONSTRAINTS: "constraints",
95
+ ACTION_CONSTRAINT: "actionConstraints",
96
+ ACTION_CONSTRAINTS: "actionConstraints",
97
+ POSTCONDITION: "postconditions",
98
+ POSTCONDITIONS: "postconditions",
99
+ _POSSIBLE: "possible",
100
+ } as const;
101
+
102
+ const isSingleKeyword = (text: string): text is keyof typeof SINGLE_KEYWORDS =>
103
+ Object.hasOwn(SINGLE_KEYWORDS, text);
104
+
105
+ const isListKeyword = (text: string): text is keyof typeof LIST_KEYWORDS =>
106
+ Object.hasOwn(LIST_KEYWORDS, text);
107
+
108
+ // JavaCC's TokenMgrError.addEscapes, which TLC's lexical errors use.
109
+ const escapeLexeme = (text: string) =>
110
+ [...text]
111
+ .map((character) => {
112
+ const escaped = { "\b": "\\b", "\t": "\\t", "\n": "\\n", "\f": "\\f", "\r": "\\r" }[
113
+ character
114
+ ];
115
+ if (escaped) return escaped;
116
+ if (character === '"' || character === "'" || character === "\\") return `\\${character}`;
117
+ const code = character.codePointAt(0) ?? 0;
118
+ return code < 0x20 || code > 0x7e ? `\\u${code.toString(16).padStart(4, "0")}` : character;
119
+ })
120
+ .join("");
121
+
122
+ // TLC reads a configuration with SANY's lexer, which reports a character it cannot read as a
123
+ // JavaCC lexical error.
124
+ const createLexicalError = (line: number, column: number, found: string | undefined, after = "") =>
125
+ new TlaConfigError(
126
+ `TLC found an error in the configuration file:\nLexical error at line ${line}, column ${column}. Encountered: ${found === undefined ? "<EOF> " : `"${escapeLexeme(found)}" (${found.codePointAt(0)}), `}after : "${escapeLexeme(after)}"`,
127
+ );
128
+
129
+ const createConfigFileError = (line: number, detail: string) =>
130
+ new TlaConfigError(`TLC found an error in the configuration file at line ${line}\n${detail}`);
131
+
77
132
  const tokenizeConfig = (source: string) => {
133
+ // TLC appends a newline so that a trailing \* comment ends.
134
+ const text = `${source}\n`;
78
135
  const tokens: ConfigToken[] = [];
79
- const cursor = { index: 0, line: 1 };
80
- while (cursor.index < source.length) {
81
- const rest = source.slice(cursor.index);
136
+ const cursor = { index: 0, line: 1, lineStart: 0 };
137
+ // Moves past text, keeping the line and where it starts.
138
+ const skip = (length: number) => {
139
+ const skipped = text.slice(cursor.index, cursor.index + length);
140
+ const lastBreak = skipped.lastIndexOf("\n");
141
+ if (lastBreak !== -1) {
142
+ cursor.line += skipped.split("\n").length - 1;
143
+ cursor.lineStart = cursor.index + lastBreak + 1;
144
+ }
145
+ cursor.index += length;
146
+ };
147
+ const getEndOfInputError = (after: string) =>
148
+ createLexicalError(cursor.line + 1, 0, undefined, after);
149
+ while (cursor.index < text.length) {
150
+ const rest = text.slice(cursor.index);
82
151
  const whitespace = /^\s+/.exec(rest)?.[0];
83
152
  if (whitespace) {
84
- cursor.line += whitespace.split("\n").length - 1;
85
- cursor.index += whitespace.length;
153
+ skip(whitespace.length);
86
154
  continue;
87
155
  }
88
156
  if (rest.startsWith("\\*")) {
89
- const lineEnd = rest.indexOf("\n");
90
- cursor.index += lineEnd === -1 ? rest.length : lineEnd;
157
+ cursor.index += rest.indexOf("\n");
91
158
  continue;
92
159
  }
93
160
  if (rest.startsWith("(*")) {
94
161
  const end = rest.indexOf("*)");
95
- if (end === -1) throw new TlaConfigError(`Unterminated comment at line ${cursor.line}`);
96
- cursor.line += rest.slice(0, end).split("\n").length - 1;
97
- cursor.index += end + 2;
162
+ if (end === -1) {
163
+ cursor.line += rest.split("\n").length - 2;
164
+ throw getEndOfInputError("");
165
+ }
166
+ skip(end + 2);
98
167
  continue;
99
168
  }
100
169
  const number = /^\d+(?![A-Za-z_])/.exec(rest)?.[0];
@@ -109,8 +178,18 @@ const tokenizeConfig = (source: string) => {
109
178
  cursor.index += word.length;
110
179
  continue;
111
180
  }
112
- const string = /^"((?:[^"\\]|\\.)*)"/.exec(rest);
113
- if (string) {
181
+ if (rest.startsWith('"')) {
182
+ const string = /^"((?:[^"\\\n]|\\.)*)"/.exec(rest);
183
+ if (!string) {
184
+ const lineEnd = rest.indexOf("\n");
185
+ if (lineEnd === rest.length - 1) throw getEndOfInputError(rest);
186
+ throw createLexicalError(
187
+ cursor.line,
188
+ cursor.index + lineEnd - cursor.lineStart + 1,
189
+ "\n",
190
+ rest.slice(0, lineEnd),
191
+ );
192
+ }
114
193
  tokens.push({ kind: "String", text: string[1], line: cursor.line });
115
194
  cursor.index += string[0].length;
116
195
  continue;
@@ -121,18 +200,18 @@ const tokenizeConfig = (source: string) => {
121
200
  cursor.index += symbol.length;
122
201
  continue;
123
202
  }
124
- const operator = matchOperator(source, cursor.index);
125
- if (!operator)
126
- throw new TlaConfigError(
127
- `Unexpected character ${JSON.stringify(rest[0])} at line ${cursor.line}`,
128
- );
203
+ const operator = matchOperator(text, cursor.index);
204
+ if (!operator) {
205
+ throw createLexicalError(cursor.line, cursor.index - cursor.lineStart + 1, rest[0]);
206
+ }
129
207
  tokens.push({ kind: "Word", text: operator.text, line: cursor.line });
130
208
  cursor.index += operator.length;
131
209
  }
132
- tokens.push({ kind: "End", text: "", line: cursor.line });
210
+ tokens.push({ kind: "End", text: "", line: cursor.line - 1 });
133
211
  return tokens;
134
212
  };
135
213
 
214
+ // TLC's ModelConfig.parse, which takes any token for a name and stops a list at the next keyword.
136
215
  export const parseConfig = (source: string): ModelConfig => {
137
216
  const tokens = tokenizeConfig(source);
138
217
  const cursor = { index: 0 };
@@ -155,183 +234,158 @@ export const parseConfig = (source: string): ModelConfig => {
155
234
  periodic: undefined,
156
235
  rlReward: undefined,
157
236
  };
158
-
159
237
  const deadlock = { isSet: false };
160
- const peek = () => tokens[cursor.index];
161
- const advance = () => tokens[cursor.index++];
238
+ const advance = () => tokens[Math.min(cursor.index++, tokens.length - 1)];
162
239
  const isKeyword = (token: ConfigToken) => token.kind === "Word" && KEYWORDS.has(token.text);
163
- const expect = (text: string) => {
164
- const token = advance();
165
- if (token.text !== text) {
166
- throw new TlaConfigError(
167
- `Expected ${JSON.stringify(text)} but found ${JSON.stringify(token.text)} at line ${token.line}`,
168
- );
169
- }
170
- };
171
- const expectName = () => {
172
- const token = advance();
173
- if (token.kind !== "Word" || isKeyword(token)) {
174
- throw new TlaConfigError(
175
- `Expected a name but found ${JSON.stringify(token.text)} at line ${token.line}`,
176
- );
177
- }
178
- return token.text;
179
- };
180
- const isNameAhead = () => peek().kind === "Word" && !isKeyword(peek());
181
- const parseNames = () => {
182
- const names: string[] = [];
183
- while (isNameAhead()) names.push(advance().text);
184
- return names;
185
- };
186
- const setSingle = (
187
- key:
188
- | "init"
189
- | "next"
190
- | "specification"
191
- | "symmetry"
192
- | "view"
193
- | "alias"
194
- | "periodic"
195
- | "rlReward",
196
- keyword: string,
197
- ) => {
198
- if (config[key] !== undefined) throw new TlaConfigError(`The keyword ${keyword} appears twice`);
199
- config[key] = expectName();
200
- };
201
240
 
202
- const parseValue = (): ConfigValue => {
203
- const token = advance();
204
- if (token.kind === "Number") return { kind: "Number", value: Number(token.text) };
205
- if (token.kind === "String") return { kind: "String", value: token.text };
206
- if (token.text === "TRUE" || token.text === "FALSE")
207
- return { kind: "Boolean", value: token.text === "TRUE" };
208
- if (token.text === "{") {
241
+ const parseValue = (first: ConfigToken): ConfigValue => {
242
+ if (first.kind === "Number") return { kind: "Number", value: Number(first.text) };
243
+ if (first.kind === "String") return { kind: "String", value: first.text };
244
+ if (first.text === "TRUE" || first.text === "FALSE") {
245
+ return { kind: "Boolean", value: first.text === "TRUE" };
246
+ }
247
+ if (first.text === "{") {
209
248
  const elements: ConfigValue[] = [];
210
- if (peek().text !== "}") elements.push(parseValue());
211
- while (peek().text === ",") {
212
- advance();
213
- elements.push(parseValue());
249
+ let token = advance();
250
+ if (token.text !== "}") {
251
+ for (;;) {
252
+ elements.push(parseValue(token));
253
+ token = advance();
254
+ if (token.text !== ",") break;
255
+ token = advance();
256
+ }
214
257
  }
215
- expect("}");
258
+ if (token.text !== "}")
259
+ throw createConfigFileError(token.line, "It was expecting }, but did not find it.");
216
260
  return { kind: "Set", elements };
217
261
  }
218
- if (token.kind === "Word" && !isKeyword(token)) return { kind: "ModelValue", name: token.text };
219
- throw new TlaConfigError(
220
- `Expected a value but found ${JSON.stringify(token.text)} at line ${token.line}`,
221
- );
262
+ if (first.kind !== "End") return { kind: "ModelValue", name: first.text };
263
+ throw createConfigFileError(first.line, "It was expecting a value, but did not find it.");
222
264
  };
223
265
 
224
- const parseModuleScope = () => {
225
- if (peek().text !== "[") return undefined;
226
- advance();
227
- const module = expectName();
228
- expect("]");
229
- return module;
266
+ const parseModuleScope = (operator: string) => {
267
+ const name = advance();
268
+ if (name.kind === "End") {
269
+ throw createConfigFileError(name.line, `Expected an identifier after ${operator}[.`);
270
+ }
271
+ const close = advance();
272
+ if (close.text !== "]") {
273
+ throw createConfigFileError(close.line, "It was expecting ], but did not find it.");
274
+ }
275
+ return name.text;
230
276
  };
231
277
 
232
- const parseConstants = () => {
233
- while (isNameAhead()) {
234
- const name = advance().text;
235
- if (peek().text === "(") {
236
- advance();
237
- const args: ConfigValue[] = [];
238
- while (peek().text !== ")") {
239
- args.push(parseValue());
240
- if (peek().text !== ")") expect(",");
278
+ // Returns the token after the constants, which starts the next section.
279
+ const parseConstants = (line: number) => {
280
+ for (;;) {
281
+ const first = advance();
282
+ if (first.kind === "End" || isKeyword(first)) return first;
283
+ const name = { text: first.text };
284
+ let token = advance();
285
+ while (token.text === "!") {
286
+ name.text = `${name.text}!${advance().text}`;
287
+ token = advance();
288
+ }
289
+ if (token.text === "<-") {
290
+ token = advance();
291
+ if (token.text === "[") {
292
+ const module = parseModuleScope("<-");
293
+ const target = advance();
294
+ if (target.kind === "End") {
295
+ throw createConfigFileError(target.line, "Expected an identifier after <-[mod].");
296
+ }
297
+ config.substitutions.push({ name: name.text, target: target.text, module });
298
+ continue;
299
+ }
300
+ if (token.kind === "End") {
301
+ throw createConfigFileError(token.line, "Expected an identifier after <-.");
241
302
  }
242
- advance();
243
- expect("=");
244
- const module = parseModuleScope();
245
- config.constants.push({ name, value: parseValue(), args, ...(module && { module }) });
303
+ config.substitutions.push({ name: name.text, target: token.text });
246
304
  continue;
247
305
  }
248
- if (peek().text === "<-") {
249
- advance();
250
- const module = parseModuleScope();
251
- config.substitutions.push({ name, target: expectName(), ...(module && { module }) });
252
- continue;
306
+ const args: ConfigValue[] = [];
307
+ if (token.text === "(") {
308
+ for (;;) {
309
+ args.push(parseValue(advance()));
310
+ token = advance();
311
+ if (token.text !== ",") break;
312
+ }
313
+ if (token.text !== ")") throw createConfigFileError(line, "");
314
+ token = advance();
253
315
  }
254
- expect("=");
255
- const module = parseModuleScope();
256
- config.constants.push({ name, value: parseValue(), ...(module && { module }) });
316
+ if (token.text !== "=") {
317
+ throw createConfigFileError(token.line, "It was expecting = or <-, but did not find it.");
318
+ }
319
+ token = advance();
320
+ const module = token.text === "[" ? parseModuleScope("=") : undefined;
321
+ const value = parseValue(module === undefined ? token : advance());
322
+ config.constants.push({
323
+ name: name.text,
324
+ value,
325
+ ...(args.length > 0 && { args }),
326
+ ...(module && { module }),
327
+ });
257
328
  }
258
329
  };
259
330
 
260
- while (peek().kind !== "End") {
261
- const token = advance();
262
- switch (token.text) {
263
- case "CONSTANT":
264
- case "CONSTANTS":
265
- parseConstants();
266
- break;
267
- case "INIT":
268
- setSingle("init", token.text);
269
- break;
270
- case "NEXT":
271
- setSingle("next", token.text);
272
- break;
273
- case "SPECIFICATION":
274
- setSingle("specification", token.text);
275
- break;
276
- case "SYMMETRY":
277
- setSingle("symmetry", token.text);
278
- break;
279
- case "VIEW":
280
- setSingle("view", token.text);
281
- break;
282
- case "INVARIANT":
283
- case "INVARIANTS":
284
- config.invariants.push(...parseNames());
285
- break;
286
- case "PROPERTY":
287
- case "PROPERTIES":
288
- config.properties.push(...parseNames());
289
- break;
290
- case "CONSTRAINT":
291
- case "CONSTRAINTS":
292
- config.constraints.push(...parseNames());
293
- break;
294
- case "ACTION_CONSTRAINT":
295
- case "ACTION_CONSTRAINTS":
296
- config.actionConstraints.push(...parseNames());
297
- break;
298
- case "ALIAS":
299
- setSingle("alias", token.text);
300
- break;
301
- case "POSTCONDITION":
302
- case "POSTCONDITIONS":
303
- config.postconditions.push(...parseNames());
304
- break;
305
- case "_POSSIBLE":
306
- config.possible.push(...parseNames());
307
- break;
308
- case "_PERIODIC":
309
- setSingle("periodic", token.text);
310
- break;
311
- case "_RL_REWARD":
312
- setSingle("rlReward", token.text);
313
- break;
314
- case "CHECK_DEADLOCK": {
315
- if (deadlock.isSet) throw new TlaConfigError("The keyword CHECK_DEADLOCK appears twice");
316
- deadlock.isSet = true;
317
- const value = advance().text;
318
- if (value !== "TRUE" && value !== "FALSE") {
319
- throw new TlaConfigError(
320
- `CHECK_DEADLOCK expects TRUE or FALSE but found ${JSON.stringify(value)}`,
321
- );
322
- }
323
- config.checkDeadlock = value === "TRUE";
324
- break;
331
+ let token = advance();
332
+ while (token.kind !== "End") {
333
+ const keyword = token.text;
334
+ const line = token.line;
335
+ if (isSingleKeyword(keyword)) {
336
+ const name = advance();
337
+ if (name.kind === "End") {
338
+ throw createConfigFileError(
339
+ line,
340
+ `The keyword ${keyword} was not followed by an identifier.`,
341
+ );
342
+ }
343
+ const key = SINGLE_KEYWORDS[keyword];
344
+ if (config[key] !== undefined) {
345
+ throw createConfigFileError(line, `The keyword ${keyword} appeared twice.`);
346
+ }
347
+ config[key] = name.text;
348
+ token = advance();
349
+ } else if (isListKeyword(keyword)) {
350
+ const names = config[LIST_KEYWORDS[keyword]];
351
+ token = advance();
352
+ while (token.kind !== "End" && !isKeyword(token)) {
353
+ names.push(token.text);
354
+ token = advance();
355
+ }
356
+ } else if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
357
+ token = parseConstants(line);
358
+ } else if (keyword === "CHECK_DEADLOCK") {
359
+ const value = advance();
360
+ if (value.kind === "End") {
361
+ throw createConfigFileError(
362
+ line,
363
+ "The keyword CHECK_DEADLOCK was not followed by an identifier.",
364
+ );
325
365
  }
326
- default:
327
- throw new TlaConfigError(`Unexpected ${JSON.stringify(token.text)} at line ${token.line}`);
366
+ if (value.text !== "TRUE" && value.text !== "FALSE") {
367
+ throw createConfigFileError(
368
+ value.line,
369
+ "It was expecting TRUE or FALSE, but did not find it.",
370
+ );
371
+ }
372
+ if (deadlock.isSet) {
373
+ throw createConfigFileError(line, "The keyword CHECK_DEADLOCK appeared twice.");
374
+ }
375
+ deadlock.isSet = true;
376
+ config.checkDeadlock = value.text === "TRUE";
377
+ token = advance();
378
+ } else {
379
+ throw createConfigFileError(line, "It was expecting a keyword, but did not find it.");
328
380
  }
329
381
  }
330
382
  if (
331
383
  config.specification !== undefined &&
332
384
  (config.init !== undefined || config.next !== undefined)
333
385
  ) {
334
- throw new TlaConfigError("SPECIFICATION cannot be combined with INIT or NEXT");
386
+ throw new TlaConfigError(
387
+ "The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.",
388
+ );
335
389
  }
336
390
  return config;
337
391
  };
@@ -12,7 +12,13 @@ import {
12
12
  import { BUNDLED_MODULES } from "./bundled-modules";
13
13
  import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
- import { TlaConfigError, TlaEvaluationError, TlaModuleError, TlaSyntaxError } from "./errors";
15
+ import {
16
+ createSanyError,
17
+ TlaConfigError,
18
+ TlaEvaluationError,
19
+ TlaModuleError,
20
+ TlaSyntaxError,
21
+ } from "./errors";
16
22
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
17
23
  import {
18
24
  createBigNumberError,
@@ -43,10 +49,11 @@ import {
43
49
  type Symbols,
44
50
  } from "./resolve";
45
51
  import { createRuntime } from "./runtime";
52
+ import { parseModuleFile } from "./sany-parse-error";
46
53
  import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
47
54
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
48
55
  import { checkModuleLevels, createLevelAnalyzer } from "./levels";
49
- import { checkInstances, type InstanceCheck } from "./substitution-levels";
56
+ import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
50
57
  import { getTlcDeclarationOrder } from "./tlc-order";
51
58
  import {
52
59
  checkInteger,
@@ -223,6 +230,8 @@ export const createEnvironment = (
223
230
  const runtime = createRuntime(config.checkDeadlock, options.seed);
224
231
  const linkedModules = new Map<string, LinkedModule>();
225
232
  const nestedModules = new Map<string, LinkedModule>();
233
+ // Each module by name, from the time its level checks run.
234
+ const checkedModules = new Map<string, LinkedModule>();
226
235
  const linkedList: LinkedModule[] = [];
227
236
  const dependencies = new Map<string, Set<string>>();
228
237
  const loading: Set<string>[] = [];
@@ -237,7 +246,23 @@ export const createEnvironment = (
237
246
  const copyCount = { value: 0 };
238
247
  const createName = (name: string) => `${name}#${copyCount.value++}`;
239
248
  const instanceChecks: InstanceCheck[] = [];
249
+ const checkInstance = createInstanceChecker((name) => {
250
+ const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
251
+ return {
252
+ constants: linked.constants,
253
+ variables: linked.variables,
254
+ definitions: [
255
+ ...new Set(
256
+ [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
257
+ ),
258
+ ],
259
+ assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
260
+ theorems: linked.module.theorems,
261
+ getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
262
+ };
263
+ });
240
264
  const instanceActions = new Map<string, InstanceAction>();
265
+ const qualifiedNames = new Map<string, string>();
241
266
 
242
267
  const addStandardModule = (name: string): Symbols | undefined => {
243
268
  const cached = standardExports.get(name);
@@ -260,7 +285,11 @@ export const createEnvironment = (
260
285
 
261
286
  const getStandardSymbols = (name: string) => {
262
287
  const symbols = addStandardModule(name);
263
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
288
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
289
+ if (symbols) useModule(name, dependencies);
290
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
291
+ // overrides the arithmetic of Naturals.
292
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
264
293
  return symbols;
265
294
  };
266
295
 
@@ -289,13 +318,25 @@ export const createEnvironment = (
289
318
  BUNDLED_MODULES.get(name) ??
290
319
  extractModule(options.source, name);
291
320
  if (source === undefined)
292
- throw new TlaSyntaxError(
293
- `Cannot find source file for module ${name} imported in module ${importer}.`,
321
+ throw createSanyError(
322
+ [
323
+ {
324
+ location: `In module ${importer}`,
325
+ message: `Cannot find source file for module ${name} imported in module ${importer}.`,
326
+ },
327
+ ],
328
+ true,
294
329
  );
295
- const module = parseModule(source);
330
+ const module = parseModuleFile(source, name);
296
331
  if (module.name !== name) {
297
- throw new TlaSyntaxError(
298
- `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
332
+ throw createSanyError(
333
+ [
334
+ {
335
+ location: "Unknown location",
336
+ message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
337
+ },
338
+ ],
339
+ true,
299
340
  );
300
341
  }
301
342
  return rejectModuleLiterals(module);
@@ -407,6 +448,7 @@ export const createEnvironment = (
407
448
  if (declarations.length > 0) {
408
449
  instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
409
450
  }
451
+ qualifiedNames.set(key, `${instance.name}!${name}`);
410
452
  symbols.set(`${instance.name}!${name}`, {
411
453
  key,
412
454
  arity: symbol.arity + instance.params.length,
@@ -587,6 +629,7 @@ export const createEnvironment = (
587
629
 
588
630
  const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
589
631
  const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
632
+ const firstCheck = instanceChecks.length;
590
633
  const visible: Symbols = new Map([
591
634
  ...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
592
635
  operator.name,
@@ -729,11 +772,6 @@ export const createEnvironment = (
729
772
  visible.set(definition.name, symbol);
730
773
  if (!definition.isLocal) exported.set(definition.name, symbol);
731
774
  });
732
- checkModuleLevels(module, {
733
- constants: new Set(constants.map((constant) => constant.name)),
734
- variables: new Set(variables),
735
- getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
736
- });
737
775
  const linked = {
738
776
  module,
739
777
  constants,
@@ -744,6 +782,21 @@ export const createEnvironment = (
744
782
  modules,
745
783
  extended,
746
784
  };
785
+ if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
786
+ // SANY checks the instances of a module with its definitions and theorems, so it reports their
787
+ // errors together.
788
+ checkModuleLevels(
789
+ module,
790
+ {
791
+ constants: new Set(constants.map((constant) => constant.name)),
792
+ variables: new Set(variables),
793
+ getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
794
+ getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
795
+ },
796
+ instanceChecks
797
+ .splice(firstCheck)
798
+ .map((check) => ({ location: check.location, check: () => checkInstance(check) })),
799
+ );
747
800
  linkedList.push(linked);
748
801
  return linked;
749
802
  };
@@ -938,20 +991,6 @@ export const createEnvironment = (
938
991
  constantLevels: new Map(),
939
992
  evaluators: new WeakMap(),
940
993
  };
941
- checkInstances(instanceChecks, (name) => {
942
- const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
943
- return {
944
- constants: linked.constants,
945
- variables: linked.variables,
946
- definitions: [
947
- ...new Set(
948
- [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
949
- ),
950
- ],
951
- assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
952
- getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
953
- };
954
- });
955
994
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
956
995
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
957
996
  // a minimum level as state-level, so the analyzer takes them for variables.