tla-ts 0.0.3 → 0.0.4-dev.2be6058
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/README.md +32 -12
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +160 -8
- package/dist/index.d.ts +160 -8
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +45 -18
- package/dist/run-cli.js +45 -18
- package/dist/transfer.cjs +1393 -18
- package/dist/transfer.js +1393 -18
- package/package.json +5 -1
- package/src/ast.ts +147 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1539 -235
- package/src/cli-options.ts +11 -5
- package/src/cli.ts +8 -1
- package/src/codegen.ts +77 -37
- package/src/config.ts +254 -181
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +540 -140
- package/src/errors.ts +224 -3
- package/src/evaluate.ts +797 -246
- package/src/exit-codes.ts +22 -43
- package/src/fingerprints.ts +46 -11
- package/src/index.ts +1 -0
- package/src/instances.ts +19 -16
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +191 -45
- package/src/lexer.ts +127 -30
- package/src/live-check.ts +867 -0
- package/src/liveness.ts +218 -572
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +174 -213
- package/src/parallel.ts +131 -39
- package/src/parser.ts +456 -147
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1481 -426
- package/src/run-cli.ts +1212 -143
- package/src/runtime.ts +19 -1
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +356 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +42 -11
- package/src/standard-modules.ts +200 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +180 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +219 -107
- package/src/symmetry.ts +194 -54
- package/src/tableau.ts +54 -25
- package/src/tlc-ext.ts +42 -29
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +52 -43
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +78 -10
- package/src/tlc-random.ts +37 -9
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +731 -184
package/src/environment.ts
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type Assumption,
|
|
4
|
+
type Declaration,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type Instance,
|
|
8
|
+
type Module,
|
|
9
|
+
type OperatorParam,
|
|
10
|
+
type SourceLocation,
|
|
10
11
|
} from "./ast";
|
|
11
12
|
import { BUNDLED_MODULES } from "./bundled-modules";
|
|
12
13
|
import type { ConfigValue, ModelConfig } from "./config";
|
|
13
14
|
import type { FileSystem } from "./json";
|
|
14
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
createSanyError,
|
|
17
|
+
createMissingModuleError,
|
|
18
|
+
formatRange,
|
|
19
|
+
formatSanyWarnings,
|
|
20
|
+
TlaConfigError,
|
|
21
|
+
TlaEvaluationError,
|
|
22
|
+
TlaSyntaxError,
|
|
23
|
+
type SanyError,
|
|
24
|
+
} from "./errors";
|
|
15
25
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
16
26
|
import {
|
|
17
27
|
createBigNumberError,
|
|
18
28
|
createRealNumberError,
|
|
19
|
-
|
|
20
|
-
|
|
29
|
+
evaluate,
|
|
30
|
+
getDefinition,
|
|
21
31
|
isTlcInteger,
|
|
22
32
|
type Builtin,
|
|
23
33
|
type Environment,
|
|
@@ -28,24 +38,41 @@ import {
|
|
|
28
38
|
renameLocals,
|
|
29
39
|
withLetInstances,
|
|
30
40
|
} from "./instances";
|
|
31
|
-
import { overrideDefinitions } from "./java-overrides";
|
|
41
|
+
import { overrideDefinitions, type JavaOverride } from "./java-overrides";
|
|
32
42
|
import { extractModule, parseModule } from "./parser";
|
|
33
43
|
import {
|
|
34
|
-
checkRedefinitions,
|
|
35
44
|
getArity,
|
|
45
|
+
getParamArities,
|
|
36
46
|
isBefore,
|
|
47
|
+
isTheoremDefinition,
|
|
37
48
|
KEY_SEPARATOR,
|
|
38
49
|
LOCAL_RESOLUTION,
|
|
50
|
+
MODULE_START,
|
|
39
51
|
resolveModule,
|
|
40
52
|
resolveSubstitutions,
|
|
53
|
+
type ModuleReport,
|
|
41
54
|
type ModuleSymbol,
|
|
55
|
+
type ModuleUnit,
|
|
56
|
+
type SymbolKind,
|
|
42
57
|
type Symbols,
|
|
43
58
|
} from "./resolve";
|
|
44
59
|
import { createRuntime } from "./runtime";
|
|
45
|
-
import {
|
|
60
|
+
import { getSanyStandardModules } from "./sany-modules";
|
|
61
|
+
import { USER_STANDARD_MODULES } from "./tlc-modules";
|
|
62
|
+
import { getSanyParseError } from "./sany-parse-error";
|
|
63
|
+
import {
|
|
64
|
+
CORE_OPERATORS,
|
|
65
|
+
getStandardModule,
|
|
66
|
+
getTraceSpecArithmetic,
|
|
67
|
+
STANDARD_DEPENDENCIES,
|
|
68
|
+
} from "./standard-modules";
|
|
69
|
+
import { STANDARD_LEVELS } from "./standard-levels";
|
|
70
|
+
import { getStandardRange } from "./standard-ranges";
|
|
46
71
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
47
|
-
import { checkModuleLevels } from "./levels";
|
|
48
|
-
import {
|
|
72
|
+
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
73
|
+
import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
|
|
74
|
+
import { isEvalException } from "./tlc-messages";
|
|
75
|
+
import { createTlcDefinitionOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
49
76
|
import {
|
|
50
77
|
checkInteger,
|
|
51
78
|
createModelValue,
|
|
@@ -53,7 +80,9 @@ import {
|
|
|
53
80
|
formatValue,
|
|
54
81
|
valuesEqual,
|
|
55
82
|
type Value,
|
|
83
|
+
markNormalized,
|
|
56
84
|
} from "./values";
|
|
85
|
+
import type { TlcVersion } from "./tlc-version";
|
|
57
86
|
|
|
58
87
|
interface EnvironmentOptions {
|
|
59
88
|
source: string;
|
|
@@ -62,6 +91,15 @@ interface EnvironmentOptions {
|
|
|
62
91
|
print: (text: string) => void;
|
|
63
92
|
files: FileSystem;
|
|
64
93
|
seed?: bigint;
|
|
94
|
+
// Links the modules without a model, as SANY does, so a declared constant needs no value.
|
|
95
|
+
isUnconfigured?: boolean;
|
|
96
|
+
isTool?: boolean;
|
|
97
|
+
// TLC loads Integers for the binary trace of its trace exploration spec, and Integers' Java
|
|
98
|
+
// methods then compute the arithmetic of Naturals.
|
|
99
|
+
isTraceRecorded?: boolean;
|
|
100
|
+
isSanyReported?: boolean;
|
|
101
|
+
isWarningEnabled?: boolean;
|
|
102
|
+
version?: TlcVersion;
|
|
65
103
|
}
|
|
66
104
|
|
|
67
105
|
interface Specification {
|
|
@@ -69,6 +107,12 @@ interface Specification {
|
|
|
69
107
|
assumptions: Assumption[];
|
|
70
108
|
variableLocations: SourceLocation[];
|
|
71
109
|
instanceActions: Map<string, InstanceAction>;
|
|
110
|
+
// The definitions TLC computes when it starts, in the order it computes them.
|
|
111
|
+
startupDefinitions: Definition[];
|
|
112
|
+
rootConstants: Declaration[];
|
|
113
|
+
rootSymbols: Symbols;
|
|
114
|
+
// The methods of the modules' classes, in the order TLC loads the classes.
|
|
115
|
+
javaOverrides: JavaOverride[];
|
|
72
116
|
}
|
|
73
117
|
|
|
74
118
|
export interface InstanceAction {
|
|
@@ -104,25 +148,73 @@ interface LinkedModule {
|
|
|
104
148
|
exported: Symbols;
|
|
105
149
|
modules: Map<string, LinkedModule>;
|
|
106
150
|
extended: LinkedModule[];
|
|
151
|
+
facts: Set<string>;
|
|
107
152
|
}
|
|
108
153
|
|
|
154
|
+
// errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
|
|
155
|
+
// the modules in its file, which SANY runs only when it reports no error.
|
|
109
156
|
interface ModuleScope {
|
|
110
157
|
id: string;
|
|
111
158
|
symbols: Symbols;
|
|
112
159
|
modules: Map<string, LinkedModule>;
|
|
113
160
|
nested: Module[];
|
|
161
|
+
errors: SanyError[];
|
|
162
|
+
warnings: SanyError[];
|
|
163
|
+
levelChecks: (() => void)[];
|
|
114
164
|
}
|
|
115
165
|
|
|
116
|
-
const getDeclarations = (linked: LinkedModule) => [
|
|
166
|
+
const getDeclarations = (linked: LinkedModule): Declaration[] => [
|
|
117
167
|
...linked.constants,
|
|
118
|
-
...linked.variables.map((variable) => ({
|
|
168
|
+
...linked.variables.map((variable, index) => ({
|
|
169
|
+
name: variable,
|
|
170
|
+
arity: 0,
|
|
171
|
+
location: linked.variableLocations[index],
|
|
172
|
+
})),
|
|
119
173
|
];
|
|
120
174
|
|
|
175
|
+
// Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
|
|
176
|
+
// does in TLC: SANY's message, then a fatal error that names the module.
|
|
177
|
+
const parseModuleFile = (source: string, name: string) => {
|
|
178
|
+
try {
|
|
179
|
+
return parseModule(source);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
if (!(error instanceof TlaSyntaxError)) throw error;
|
|
182
|
+
const parseError = getSanyParseError(source);
|
|
183
|
+
if (parseError === undefined) throw error;
|
|
184
|
+
throw createSanyError(
|
|
185
|
+
[
|
|
186
|
+
{
|
|
187
|
+
location: `In module ${name}`,
|
|
188
|
+
message: `Could not parse module ${name} from file ${name}.tla`,
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
true,
|
|
192
|
+
parseError,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
121
197
|
const createModuleRedefinedError = (module: Module) =>
|
|
122
198
|
new TlaSyntaxError(
|
|
123
199
|
`Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
|
|
124
200
|
);
|
|
125
201
|
|
|
202
|
+
const getModuleRedefinition = (module: Module, existing: Module): SanyError | undefined =>
|
|
203
|
+
module.location.range &&
|
|
204
|
+
existing.location.range && {
|
|
205
|
+
location: formatRange(module.location.range),
|
|
206
|
+
message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
|
|
210
|
+
// declaration.
|
|
211
|
+
const getNodeWord = (kind: SymbolKind) =>
|
|
212
|
+
["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
|
|
213
|
+
|
|
214
|
+
// The Java class of the node SANY binds a symbol to.
|
|
215
|
+
const getNodeClass = (kind: SymbolKind) =>
|
|
216
|
+
kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
|
|
217
|
+
|
|
126
218
|
const createLocalFunction = (definition: Definition, key: string): Definition => ({
|
|
127
219
|
kind: "Operator",
|
|
128
220
|
name: key,
|
|
@@ -212,13 +304,21 @@ export const createEnvironment = (
|
|
|
212
304
|
const root = parseModule(options.source);
|
|
213
305
|
const definitions = new Map<string, Definition>();
|
|
214
306
|
const parameterized = new Map<string, boolean>();
|
|
307
|
+
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
308
|
+
const copySources = new Map<string, { source: string; prefix: number }>();
|
|
309
|
+
const substitutedDefinitions = new Set<Definition>();
|
|
215
310
|
const builtins = new Map<string, Builtin>(
|
|
216
311
|
CORE_OPERATORS.map((operator) => [operator.name, operator]),
|
|
217
312
|
);
|
|
218
313
|
const standardExports = new Map<string, Symbols>();
|
|
219
314
|
const runtime = createRuntime(config.checkDeadlock, options.seed);
|
|
315
|
+
runtime.isWarningEnabled = options.isWarningEnabled ?? true;
|
|
316
|
+
runtime.isTool = options.isTool ?? false;
|
|
317
|
+
runtime.version = options.version ?? "master";
|
|
220
318
|
const linkedModules = new Map<string, LinkedModule>();
|
|
221
319
|
const nestedModules = new Map<string, LinkedModule>();
|
|
320
|
+
// Each module by name, from the time its level checks run.
|
|
321
|
+
const checkedModules = new Map<string, LinkedModule>();
|
|
222
322
|
const linkedList: LinkedModule[] = [];
|
|
223
323
|
const dependencies = new Map<string, Set<string>>();
|
|
224
324
|
const loading: Set<string>[] = [];
|
|
@@ -233,13 +333,35 @@ export const createEnvironment = (
|
|
|
233
333
|
const copyCount = { value: 0 };
|
|
234
334
|
const createName = (name: string) => `${name}#${copyCount.value++}`;
|
|
235
335
|
const instanceChecks: InstanceCheck[] = [];
|
|
336
|
+
// The warnings SANY reports for the spec.
|
|
337
|
+
const warnings: SanyError[] = [];
|
|
338
|
+
const checkInstance = createInstanceChecker((name) => {
|
|
339
|
+
const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
|
|
340
|
+
return {
|
|
341
|
+
constants: linked.constants,
|
|
342
|
+
variables: linked.variables,
|
|
343
|
+
definitions: [
|
|
344
|
+
...new Set(
|
|
345
|
+
[...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
|
|
346
|
+
),
|
|
347
|
+
],
|
|
348
|
+
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
349
|
+
theorems: linked.module.theorems,
|
|
350
|
+
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
351
|
+
};
|
|
352
|
+
});
|
|
236
353
|
const instanceActions = new Map<string, InstanceAction>();
|
|
354
|
+
const instanceNames = new Map<string, string>();
|
|
355
|
+
const qualifiedNames = new Map<string, string>();
|
|
237
356
|
|
|
238
357
|
const addStandardModule = (name: string): Symbols | undefined => {
|
|
239
358
|
const cached = standardExports.get(name);
|
|
240
359
|
if (cached) return cached;
|
|
241
|
-
const standardModule = getStandardModule(name, options.print, runtime, options.files);
|
|
360
|
+
const standardModule = getStandardModule(name, options.print, runtime, options.files, builtins);
|
|
242
361
|
if (!standardModule) return undefined;
|
|
362
|
+
// TLC loads the modules a standard module instantiates too, and the Java methods of each
|
|
363
|
+
// replace the operators of every module, as those of Integers replace those of Naturals.
|
|
364
|
+
STANDARD_DEPENDENCIES.get(name)?.forEach(addStandardModule);
|
|
243
365
|
const exported: Symbols = new Map();
|
|
244
366
|
standardModule.extends.forEach((extended) =>
|
|
245
367
|
addStandardModule(extended)?.forEach((symbol, symbolName) =>
|
|
@@ -248,15 +370,28 @@ export const createEnvironment = (
|
|
|
248
370
|
);
|
|
249
371
|
standardModule.operators.forEach((operator) => {
|
|
250
372
|
builtins.set(operator.name, operator);
|
|
251
|
-
exported.set(operator.name, {
|
|
373
|
+
exported.set(operator.name, {
|
|
374
|
+
key: operator.name,
|
|
375
|
+
arity: operator.arity,
|
|
376
|
+
params: STANDARD_LEVELS.get(operator.name)?.formals.map((formal) => formal.arity),
|
|
377
|
+
kind: "Definition",
|
|
378
|
+
range: getStandardRange(name, operator.name) ?? exported.get(operator.name)?.range,
|
|
379
|
+
});
|
|
252
380
|
});
|
|
253
381
|
standardExports.set(name, exported);
|
|
254
382
|
return exported;
|
|
255
383
|
};
|
|
256
384
|
|
|
257
385
|
const getStandardSymbols = (name: string) => {
|
|
386
|
+
// SANY reads a module from the user's directories before its standard modules.
|
|
387
|
+
if (USER_STANDARD_MODULES.has(name) && options.loadModule?.(name) !== undefined)
|
|
388
|
+
return undefined;
|
|
258
389
|
const symbols = addStandardModule(name);
|
|
259
|
-
|
|
390
|
+
const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
|
|
391
|
+
if (symbols) useModule(name, dependencies);
|
|
392
|
+
// TLC loads the Java class of each module a standard module uses, so one that uses Integers
|
|
393
|
+
// overrides the arithmetic of Naturals.
|
|
394
|
+
if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
|
|
260
395
|
return symbols;
|
|
261
396
|
};
|
|
262
397
|
|
|
@@ -284,14 +419,17 @@ export const createEnvironment = (
|
|
|
284
419
|
options.loadModule?.(name) ??
|
|
285
420
|
BUNDLED_MODULES.get(name) ??
|
|
286
421
|
extractModule(options.source, name);
|
|
287
|
-
if (source === undefined)
|
|
288
|
-
|
|
289
|
-
`Cannot find source file for module ${name} imported in module ${importer}.`,
|
|
290
|
-
);
|
|
291
|
-
const module = parseModule(source);
|
|
422
|
+
if (source === undefined) throw createMissingModuleError(name, importer);
|
|
423
|
+
const module = parseModuleFile(source, name);
|
|
292
424
|
if (module.name !== name) {
|
|
293
|
-
throw
|
|
294
|
-
|
|
425
|
+
throw createSanyError(
|
|
426
|
+
[
|
|
427
|
+
{
|
|
428
|
+
location: "Unknown location",
|
|
429
|
+
message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
|
|
430
|
+
},
|
|
431
|
+
],
|
|
432
|
+
true,
|
|
295
433
|
);
|
|
296
434
|
}
|
|
297
435
|
return rejectModuleLiterals(module);
|
|
@@ -307,6 +445,8 @@ export const createEnvironment = (
|
|
|
307
445
|
}
|
|
308
446
|
const closure = new Set<string>();
|
|
309
447
|
const nested: Module[] = [];
|
|
448
|
+
const errors: SanyError[] = [];
|
|
449
|
+
const levelChecks: (() => void)[] = [];
|
|
310
450
|
linking.push(parsed.name);
|
|
311
451
|
loading.push(closure);
|
|
312
452
|
const linked = linkModule(parsed, {
|
|
@@ -314,11 +454,19 @@ export const createEnvironment = (
|
|
|
314
454
|
symbols: new Map(),
|
|
315
455
|
modules: new Map(),
|
|
316
456
|
nested,
|
|
457
|
+
errors,
|
|
458
|
+
warnings,
|
|
459
|
+
levelChecks,
|
|
317
460
|
});
|
|
318
461
|
loading.pop();
|
|
319
462
|
linking.pop();
|
|
463
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
320
464
|
const duplicate = nested.find((module) => closure.has(module.name));
|
|
465
|
+
const loaded = duplicate && linkedModules.get(duplicate.name);
|
|
466
|
+
const redefinition = loaded && getModuleRedefinition(duplicate, loaded.module);
|
|
467
|
+
if (redefinition) throw createSanyError([redefinition], false, undefined, warnings);
|
|
321
468
|
if (duplicate) throw createModuleRedefinedError(duplicate);
|
|
469
|
+
levelChecks.forEach((check) => check());
|
|
322
470
|
return { linked, closure };
|
|
323
471
|
};
|
|
324
472
|
|
|
@@ -329,6 +477,17 @@ export const createEnvironment = (
|
|
|
329
477
|
return cached;
|
|
330
478
|
}
|
|
331
479
|
const top = linkTopLevel(loadModule(name, importer));
|
|
480
|
+
// TLC replaces each definition of a module that has a standard module's name with the Java
|
|
481
|
+
// method of that name, so a user's Bags.tla keeps the definitions that Bags has no method for.
|
|
482
|
+
const standardModule = getSanyStandardModules(runtime.version).has(name)
|
|
483
|
+
? getStandardModule(name, options.print, runtime, options.files, builtins)
|
|
484
|
+
: undefined;
|
|
485
|
+
standardModule?.operators.forEach((operator) => {
|
|
486
|
+
const symbol = top.linked.visible.get(operator.name);
|
|
487
|
+
if (symbol?.kind !== "Definition" || symbol.arity !== operator.arity) return;
|
|
488
|
+
builtins.set(symbol.key, operator);
|
|
489
|
+
definitions.delete(symbol.key);
|
|
490
|
+
});
|
|
332
491
|
linkedModules.set(name, top.linked);
|
|
333
492
|
dependencies.set(name, top.closure);
|
|
334
493
|
useModule(name, top.closure);
|
|
@@ -338,23 +497,16 @@ export const createEnvironment = (
|
|
|
338
497
|
const instantiate = (
|
|
339
498
|
instance: Instance,
|
|
340
499
|
module: Module,
|
|
341
|
-
unit:
|
|
342
|
-
imported: Symbols;
|
|
343
|
-
importedAt: Map<string, SourceLocation>;
|
|
344
|
-
definitions: Map<string, Definition>;
|
|
345
|
-
getKey: (definition: Definition) => string;
|
|
346
|
-
},
|
|
500
|
+
unit: ModuleUnit,
|
|
347
501
|
isParameterFree: boolean,
|
|
348
502
|
nested: LinkedModule | undefined,
|
|
349
503
|
): Symbols => {
|
|
350
504
|
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
351
|
-
if (standardSymbols)
|
|
352
|
-
if (instance.name === undefined) return standardSymbols;
|
|
353
|
-
return new Map(
|
|
354
|
-
[...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
|
|
355
|
-
);
|
|
356
|
-
}
|
|
505
|
+
if (standardSymbols) return getStandardInstanceSymbols(instance, standardSymbols);
|
|
357
506
|
const linked = nested ?? getLinkedModule(instance.module, module.name);
|
|
507
|
+
linked.facts.forEach((name) =>
|
|
508
|
+
unit.facts.add(instance.name === undefined ? name : `${instance.name}!${name}`),
|
|
509
|
+
);
|
|
358
510
|
const declarations = getDeclarations(linked);
|
|
359
511
|
const suffix = `#${copyCount.value++}`;
|
|
360
512
|
const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
|
|
@@ -379,35 +531,83 @@ export const createEnvironment = (
|
|
|
379
531
|
variables: new Set(linked.variables),
|
|
380
532
|
getDefinition: (key) => definitions.get(key),
|
|
381
533
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
382
|
-
addDefinition: (name, create) => {
|
|
534
|
+
addDefinition: (source, name, create) => {
|
|
383
535
|
const key = createName(`${name}@${module.name}`);
|
|
384
|
-
|
|
536
|
+
const definition = create(key);
|
|
537
|
+
definitions.set(key, definition);
|
|
538
|
+
if (declarations.length > 0) substitutedDefinitions.add(definition);
|
|
539
|
+
if (instance.name !== undefined) {
|
|
540
|
+
instanceNames.set(key, `${instance.name}!${instanceNames.get(source) ?? name}`);
|
|
541
|
+
}
|
|
385
542
|
parameterized.set(key, !isParameterFree);
|
|
543
|
+
copySources.set(key, { source, prefix: instance.params.length });
|
|
386
544
|
return key;
|
|
387
545
|
},
|
|
388
546
|
createName,
|
|
389
547
|
});
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
symbols.set(name, { key, arity: symbol.arity });
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
const key = instantiation.getQualifiedKey(symbol.key);
|
|
402
|
-
if (declarations.length > 0) {
|
|
403
|
-
instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
|
|
404
|
-
}
|
|
405
|
-
symbols.set(`${instance.name}!${name}`, {
|
|
406
|
-
key,
|
|
407
|
-
arity: symbol.arity + instance.params.length,
|
|
408
|
-
});
|
|
548
|
+
return getInstanceSymbols(instance, linked.exported, declarations, (key, name) => {
|
|
549
|
+
const copy =
|
|
550
|
+
instance.name === undefined
|
|
551
|
+
? instantiation.getKey(key)
|
|
552
|
+
: instantiation.getQualifiedKey(key);
|
|
553
|
+
if (declarations.length > 0) instanceActions.set(copy, { name, location: instance.location });
|
|
554
|
+
if (instance.name !== undefined) qualifiedNames.set(copy, name);
|
|
555
|
+
return copy;
|
|
409
556
|
});
|
|
410
|
-
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
const getStandardInstanceSymbols = (instance: Instance, standardSymbols: Symbols): Symbols =>
|
|
560
|
+
instance.name === undefined
|
|
561
|
+
? standardSymbols
|
|
562
|
+
: new Map(
|
|
563
|
+
[...standardSymbols].map(([name, symbol]) => [
|
|
564
|
+
`${instance.name}!${name}`,
|
|
565
|
+
{ key: symbol.key, arity: symbol.arity, params: symbol.params, kind: symbol.kind },
|
|
566
|
+
]),
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
// The names an INSTANCE adds for the definitions of the module it instantiates, each with the key
|
|
570
|
+
// that getKey gives it. SANY binds a copy of a definition that depends on the parameters at the
|
|
571
|
+
// INSTANCE.
|
|
572
|
+
const getInstanceSymbols = (
|
|
573
|
+
instance: Instance,
|
|
574
|
+
exported: Symbols,
|
|
575
|
+
declarations: Declaration[],
|
|
576
|
+
getKey: (key: string, name: string) => string,
|
|
577
|
+
): Symbols => {
|
|
578
|
+
const declared = new Set(declarations.map((declaration) => declaration.name));
|
|
579
|
+
return new Map(
|
|
580
|
+
[...exported]
|
|
581
|
+
.filter(([name]) => !declared.has(name))
|
|
582
|
+
.map(([name, symbol]): [string, ModuleSymbol] => {
|
|
583
|
+
if (instance.name === undefined) {
|
|
584
|
+
return [
|
|
585
|
+
name,
|
|
586
|
+
{
|
|
587
|
+
key: getKey(symbol.key, name),
|
|
588
|
+
arity: symbol.arity,
|
|
589
|
+
params: symbol.params,
|
|
590
|
+
kind: symbol.kind,
|
|
591
|
+
range:
|
|
592
|
+
parameterized.get(symbol.key) === true ? instance.location.range : symbol.range,
|
|
593
|
+
},
|
|
594
|
+
];
|
|
595
|
+
}
|
|
596
|
+
const qualified = `${instance.name}!${name}`;
|
|
597
|
+
return [
|
|
598
|
+
qualified,
|
|
599
|
+
{
|
|
600
|
+
key: getKey(symbol.key, qualified),
|
|
601
|
+
arity: symbol.arity + instance.params.length,
|
|
602
|
+
params: symbol.params && [
|
|
603
|
+
...instance.params.map((param) => param.arity),
|
|
604
|
+
...symbol.params,
|
|
605
|
+
],
|
|
606
|
+
kind: symbol.kind,
|
|
607
|
+
},
|
|
608
|
+
];
|
|
609
|
+
}),
|
|
610
|
+
);
|
|
411
611
|
};
|
|
412
612
|
|
|
413
613
|
const getLocalArguments = (params: OperatorParam[], location: SourceLocation) =>
|
|
@@ -544,7 +744,7 @@ export const createEnvironment = (
|
|
|
544
744
|
variables: new Set(linked.variables),
|
|
545
745
|
getDefinition: (key) => definitions.get(key),
|
|
546
746
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
547
|
-
addDefinition: (name, create) => {
|
|
747
|
+
addDefinition: (_source, name, create) => {
|
|
548
748
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
|
549
749
|
const definition = create(key);
|
|
550
750
|
copies.push(
|
|
@@ -580,31 +780,70 @@ export const createEnvironment = (
|
|
|
580
780
|
return definition ? [{ ...definition, name }] : [];
|
|
581
781
|
});
|
|
582
782
|
|
|
783
|
+
const javaOverrides = new Map<string, JavaOverride[]>();
|
|
583
784
|
const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
|
|
584
|
-
const
|
|
785
|
+
const overridden = overrideDefinitions(
|
|
786
|
+
unlinked,
|
|
787
|
+
options.loadClass?.(unlinked.name),
|
|
788
|
+
unlinked === root,
|
|
789
|
+
);
|
|
790
|
+
javaOverrides.set(unlinked.name, overridden.overrides);
|
|
791
|
+
const parsed = overridden.module;
|
|
792
|
+
const firstCheck = instanceChecks.length;
|
|
585
793
|
const visible: Symbols = new Map([
|
|
586
794
|
...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
|
|
587
795
|
operator.name,
|
|
588
|
-
{ key: operator.name, arity: operator.arity },
|
|
796
|
+
{ key: operator.name, arity: operator.arity, kind: "Builtin" },
|
|
589
797
|
]),
|
|
590
798
|
...scope.symbols,
|
|
591
799
|
]);
|
|
800
|
+
// Where an INSTANCE first made each name it adds visible.
|
|
801
|
+
const importedAt = new Map<string, SourceLocation>();
|
|
802
|
+
const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
|
|
803
|
+
const reports = [extendsReport];
|
|
804
|
+
// Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
|
|
805
|
+
// and warns of two definitions or two declarations but rejects a definition and a declaration.
|
|
806
|
+
const mergeExtended = (symbols: Symbols) =>
|
|
807
|
+
symbols.forEach((symbol, name) => {
|
|
808
|
+
const current = exported.get(name);
|
|
809
|
+
if (!current) {
|
|
810
|
+
visible.set(name, symbol);
|
|
811
|
+
exported.set(name, symbol);
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
if (
|
|
815
|
+
!current.range ||
|
|
816
|
+
!symbol.range ||
|
|
817
|
+
formatRange(current.range) === formatRange(symbol.range)
|
|
818
|
+
) {
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
const location = formatRange(symbol.range);
|
|
822
|
+
const existing = formatRange(current.range);
|
|
823
|
+
if (getNodeClass(current.kind) === getNodeClass(symbol.kind)) {
|
|
824
|
+
extendsReport.warnings.push({
|
|
825
|
+
location,
|
|
826
|
+
message: `Warning: the ${getNodeWord(current.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
827
|
+
});
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
extendsReport.errors.push({
|
|
831
|
+
location,
|
|
832
|
+
message: `The ${getNodeWord(symbol.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
833
|
+
});
|
|
834
|
+
});
|
|
592
835
|
const exported: Symbols = new Map();
|
|
836
|
+
const facts = new Set<string>();
|
|
593
837
|
const modules = new Map<string, LinkedModule>();
|
|
594
838
|
const extended: LinkedModule[] = [];
|
|
595
839
|
const constants = [...parsed.constants];
|
|
596
840
|
const variables = parsed.variables.map((variable) => variable.name);
|
|
597
841
|
const variableLocations = parsed.variables.map((variable) => variable.location);
|
|
598
|
-
const addSymbols = (symbols: Symbols, isExported: boolean) =>
|
|
599
|
-
symbols.forEach((symbol, name) => {
|
|
600
|
-
visible.set(name, symbol);
|
|
601
|
-
if (isExported) exported.set(name, symbol);
|
|
602
|
-
});
|
|
603
842
|
parsed.extends.forEach((name) => {
|
|
604
843
|
const nested = scope.modules.get(name);
|
|
605
844
|
const standardSymbols = nested ? undefined : getStandardSymbols(name);
|
|
606
845
|
if (standardSymbols) {
|
|
607
|
-
|
|
846
|
+
mergeExtended(standardSymbols);
|
|
608
847
|
return;
|
|
609
848
|
}
|
|
610
849
|
const linked = nested ?? getLinkedModule(name, parsed.name);
|
|
@@ -618,61 +857,117 @@ export const createEnvironment = (
|
|
|
618
857
|
variables.push(variable);
|
|
619
858
|
variableLocations.push(linked.variableLocations[index]);
|
|
620
859
|
});
|
|
621
|
-
|
|
860
|
+
mergeExtended(linked.exported);
|
|
861
|
+
linked.facts.forEach((name) => facts.add(name));
|
|
622
862
|
linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
|
|
623
863
|
extended.push(linked);
|
|
624
864
|
});
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
865
|
+
const bound = new Map(visible);
|
|
866
|
+
const declarations = [
|
|
867
|
+
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
868
|
+
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
869
|
+
];
|
|
870
|
+
const getDeclarationSymbol = (declaration: (typeof declarations)[number]): ModuleSymbol => ({
|
|
871
|
+
key: declaration.name,
|
|
872
|
+
arity: declaration.arity,
|
|
873
|
+
kind: declaration.kind,
|
|
874
|
+
range: declaration.location.range,
|
|
875
|
+
});
|
|
876
|
+
declarations.forEach((declaration) =>
|
|
877
|
+
exported.set(declaration.name, getDeclarationSymbol(declaration)),
|
|
634
878
|
);
|
|
879
|
+
const getDefinitionSymbol = (definition: Definition): ModuleSymbol => ({
|
|
880
|
+
key: getKey(definition),
|
|
881
|
+
arity: getArity(definition),
|
|
882
|
+
params: getParamArities(definition),
|
|
883
|
+
kind: isTheoremDefinition(parsed, definition) ? "Theorem" : "Definition",
|
|
884
|
+
range: definition.location.range,
|
|
885
|
+
});
|
|
635
886
|
const isParameterFree = constants.length === 0 && variables.length === 0;
|
|
636
887
|
const getKey = (definition: Definition) =>
|
|
637
888
|
parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
|
|
638
|
-
const
|
|
639
|
-
const
|
|
889
|
+
const instanced = new Map<Instance, Symbols>();
|
|
890
|
+
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
891
|
+
const isInstancedAfterDefinition = (nested: Module) => {
|
|
892
|
+
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
893
|
+
return first !== undefined && isBefore(nested.location, first.location);
|
|
894
|
+
};
|
|
895
|
+
const getLetModule = (instance: Instance) => {
|
|
896
|
+
const own = parsed.modules.find((nested) => nested.name === instance.module);
|
|
897
|
+
if (!own) return getVisibleModules().get(instance.module);
|
|
898
|
+
return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
|
|
899
|
+
};
|
|
900
|
+
const getUnit = (module: Module): ModuleUnit => ({
|
|
640
901
|
imported: visible,
|
|
641
|
-
|
|
642
|
-
|
|
902
|
+
getInstance: (instance) => {
|
|
903
|
+
const nested = getLetModule(instance);
|
|
904
|
+
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
905
|
+
if (standardSymbols) {
|
|
906
|
+
return {
|
|
907
|
+
declarations: [],
|
|
908
|
+
symbols: getStandardInstanceSymbols(instance, standardSymbols),
|
|
909
|
+
instancer: parsed.name,
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
const linked = nested ?? getLinkedModule(instance.module, parsed.name);
|
|
913
|
+
const declarations = getDeclarations(linked);
|
|
914
|
+
return {
|
|
915
|
+
declarations,
|
|
916
|
+
symbols: getInstanceSymbols(instance, linked.exported, declarations, (key) => key),
|
|
917
|
+
instancer: parsed.name,
|
|
918
|
+
};
|
|
919
|
+
},
|
|
920
|
+
bound,
|
|
921
|
+
// SANY keeps the first of two definitions of a name.
|
|
922
|
+
definitions: new Map(
|
|
923
|
+
module.definitions.toReversed().map((definition) => [definition.name, definition]),
|
|
924
|
+
),
|
|
643
925
|
getKey,
|
|
926
|
+
instanced,
|
|
927
|
+
facts,
|
|
928
|
+
reports,
|
|
929
|
+
warnings: scope.warnings,
|
|
644
930
|
});
|
|
645
|
-
|
|
931
|
+
// SANY keeps the first of two symbols of a name.
|
|
646
932
|
const getSymbolsBefore = (location: SourceLocation): Symbols =>
|
|
647
|
-
|
|
648
|
-
...
|
|
649
|
-
...[
|
|
650
|
-
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
651
|
-
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
652
|
-
]
|
|
933
|
+
[
|
|
934
|
+
...declarations
|
|
653
935
|
.filter((declaration) => isBefore(declaration.location, location))
|
|
654
936
|
.map((declaration): [string, ModuleSymbol] => [
|
|
655
937
|
declaration.name,
|
|
656
|
-
|
|
938
|
+
getDeclarationSymbol(declaration),
|
|
657
939
|
]),
|
|
658
940
|
...parsed.definitions
|
|
659
941
|
.filter((definition) => isBefore(definition.location, location))
|
|
660
942
|
.map((definition): [string, ModuleSymbol] => [
|
|
661
943
|
definition.name,
|
|
662
|
-
|
|
944
|
+
getDefinitionSymbol(definition),
|
|
663
945
|
]),
|
|
664
|
-
]
|
|
946
|
+
].reduce(
|
|
947
|
+
(symbols, [name, symbol]) => (symbols.has(name) ? symbols : symbols.set(name, symbol)),
|
|
948
|
+
new Map(visible),
|
|
949
|
+
);
|
|
950
|
+
// Generator.generateModule reports the errors of a nested module and goes on, and then
|
|
951
|
+
// SymbolTable.addModule reports a module already defined.
|
|
665
952
|
const linkNested = (nested: Module) => {
|
|
666
953
|
const visibleModules = getVisibleModules();
|
|
667
|
-
|
|
954
|
+
const existing = visibleModules.get(nested.name);
|
|
955
|
+
const redefinition = existing && getModuleRedefinition(nested, existing.module);
|
|
956
|
+
if (existing && !redefinition) throw createModuleRedefinedError(nested);
|
|
668
957
|
scope.nested.push(nested);
|
|
958
|
+
const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
|
|
959
|
+
reports.push(report);
|
|
669
960
|
const linked = linkModule(nested, {
|
|
670
961
|
id: `${scope.id}/${nested.name}`,
|
|
671
962
|
symbols: getSymbolsBefore(nested.location),
|
|
672
963
|
modules: visibleModules,
|
|
673
964
|
nested: scope.nested,
|
|
965
|
+
errors: report.errors,
|
|
966
|
+
warnings: report.warnings,
|
|
967
|
+
levelChecks: scope.levelChecks,
|
|
674
968
|
});
|
|
675
|
-
|
|
969
|
+
if (redefinition) report.errors.push(redefinition);
|
|
970
|
+
if (!existing) modules.set(nested.name, linked);
|
|
676
971
|
if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
|
|
677
972
|
};
|
|
678
973
|
[
|
|
@@ -686,10 +981,13 @@ export const createEnvironment = (
|
|
|
686
981
|
isParameterFree,
|
|
687
982
|
getVisibleModules().get(instance.module),
|
|
688
983
|
);
|
|
689
|
-
|
|
690
|
-
|
|
984
|
+
instanced.set(instance, symbols);
|
|
985
|
+
symbols.forEach((symbol, name) => {
|
|
986
|
+
if (visible.has(name)) return;
|
|
987
|
+
importedAt.set(name, instance.location);
|
|
988
|
+
visible.set(name, symbol);
|
|
989
|
+
if (!instance.isLocal) exported.set(name, symbol);
|
|
691
990
|
});
|
|
692
|
-
addSymbols(symbols, !instance.isLocal);
|
|
693
991
|
},
|
|
694
992
|
})),
|
|
695
993
|
...parsed.modules.map((nested) => ({
|
|
@@ -699,36 +997,30 @@ export const createEnvironment = (
|
|
|
699
997
|
]
|
|
700
998
|
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
701
999
|
.forEach((unit) => unit.link());
|
|
702
|
-
checkRedefinitions(parsed, getUnit(parsed));
|
|
703
|
-
const isInstancedAfterDefinition = (nested: Module) => {
|
|
704
|
-
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
705
|
-
return first !== undefined && isBefore(nested.location, first.location);
|
|
706
|
-
};
|
|
707
|
-
const getLetModule = (instance: Instance) => {
|
|
708
|
-
const own = parsed.modules.find((nested) => nested.name === instance.module);
|
|
709
|
-
if (!own) return getVisibleModules().get(instance.module);
|
|
710
|
-
return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
|
|
711
|
-
};
|
|
712
1000
|
const module = resolveModuleSubexpressions(
|
|
713
1001
|
withLetInstances(parsed, (instance, lets) =>
|
|
714
1002
|
instantiateLocally(instance, lets, parsed.name, getLetModule(instance)),
|
|
715
1003
|
),
|
|
716
1004
|
getAvailableDefinitions(visible),
|
|
717
1005
|
);
|
|
718
|
-
resolveModule(module, getUnit(module));
|
|
1006
|
+
scope.errors.push(...resolveModule(module, getUnit(module)));
|
|
719
1007
|
module.definitions.forEach((definition) => {
|
|
720
1008
|
const key = getKey(definition);
|
|
721
1009
|
definitions.set(key, definition);
|
|
722
1010
|
parameterized.set(key, !isParameterFree);
|
|
723
|
-
const symbol =
|
|
1011
|
+
const symbol = getDefinitionSymbol(definition);
|
|
1012
|
+
const importedLocation = importedAt.get(definition.name);
|
|
1013
|
+
// SANY binds the name of a fact to a fact the module imports before it.
|
|
1014
|
+
if (
|
|
1015
|
+
symbol.kind === "Theorem" &&
|
|
1016
|
+
visible.get(definition.name)?.kind === "Theorem" &&
|
|
1017
|
+
(importedLocation === undefined || isBefore(importedLocation, definition.location))
|
|
1018
|
+
) {
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
724
1021
|
visible.set(definition.name, symbol);
|
|
725
1022
|
if (!definition.isLocal) exported.set(definition.name, symbol);
|
|
726
1023
|
});
|
|
727
|
-
checkModuleLevels(module, {
|
|
728
|
-
constants: new Set(constants.map((constant) => constant.name)),
|
|
729
|
-
variables: new Set(variables),
|
|
730
|
-
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
731
|
-
});
|
|
732
1024
|
const linked = {
|
|
733
1025
|
module,
|
|
734
1026
|
constants,
|
|
@@ -738,12 +1030,36 @@ export const createEnvironment = (
|
|
|
738
1030
|
exported,
|
|
739
1031
|
modules,
|
|
740
1032
|
extended,
|
|
1033
|
+
facts: new Set([
|
|
1034
|
+
...facts,
|
|
1035
|
+
...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
1036
|
+
]),
|
|
741
1037
|
};
|
|
1038
|
+
if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
|
|
1039
|
+
// SANY checks the instances of a module with its definitions and theorems, so it reports their
|
|
1040
|
+
// errors together.
|
|
1041
|
+
const checks = instanceChecks
|
|
1042
|
+
.splice(firstCheck)
|
|
1043
|
+
.map((check) => ({ location: check.location, check: () => checkInstance(check) }));
|
|
1044
|
+
scope.levelChecks.push(() =>
|
|
1045
|
+
checkModuleLevels(
|
|
1046
|
+
module,
|
|
1047
|
+
{
|
|
1048
|
+
constants: new Set(constants.map((constant) => constant.name)),
|
|
1049
|
+
variables: new Set(variables),
|
|
1050
|
+
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
1051
|
+
getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
|
|
1052
|
+
},
|
|
1053
|
+
checks,
|
|
1054
|
+
warnings,
|
|
1055
|
+
),
|
|
1056
|
+
);
|
|
742
1057
|
linkedList.push(linked);
|
|
743
1058
|
return linked;
|
|
744
1059
|
};
|
|
745
1060
|
|
|
746
1061
|
const linkedRoot = linkTopLevel(root).linked;
|
|
1062
|
+
if (warnings.length > 0 && !options.isSanyReported) options.print(formatSanyWarnings(warnings));
|
|
747
1063
|
const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
|
|
748
1064
|
linkedRoot.visible.forEach((symbol, name) => {
|
|
749
1065
|
const definition = definitions.get(symbol.key);
|
|
@@ -785,6 +1101,7 @@ export const createEnvironment = (
|
|
|
785
1101
|
.filter((assignment) => assignment.args === undefined)
|
|
786
1102
|
.forEach((assignment) => {
|
|
787
1103
|
const value = toValue(assignment.value);
|
|
1104
|
+
markNormalized(value);
|
|
788
1105
|
getScopedKeys(assignment).forEach((key) => {
|
|
789
1106
|
const arity = getKeyArity(key);
|
|
790
1107
|
if (arity === 0) constants.set(key, value);
|
|
@@ -872,11 +1189,58 @@ export const createEnvironment = (
|
|
|
872
1189
|
);
|
|
873
1190
|
}
|
|
874
1191
|
});
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1192
|
+
// TLC sets a module-scoped entry on the body of the module's definition, which every instance of
|
|
1193
|
+
// the module shares.
|
|
1194
|
+
const scopedKeys = new Set(
|
|
1195
|
+
[...config.constants, ...config.substitutions]
|
|
1196
|
+
.filter((item) => item.module !== undefined)
|
|
1197
|
+
.flatMap(getScopedKeys),
|
|
1198
|
+
);
|
|
1199
|
+
const getOverride = (
|
|
1200
|
+
key: string,
|
|
1201
|
+
prefix: number,
|
|
1202
|
+
): { key: string; prefix: number } | undefined => {
|
|
1203
|
+
if (scopedKeys.has(key)) return { key, prefix };
|
|
1204
|
+
const copy = copySources.get(key);
|
|
1205
|
+
return copy && getOverride(copy.source, prefix + copy.prefix);
|
|
1206
|
+
};
|
|
1207
|
+
copySources.forEach((copy, key) => {
|
|
1208
|
+
const override = getOverride(copy.source, copy.prefix);
|
|
1209
|
+
const definition = definitions.get(key);
|
|
1210
|
+
if (!override || !definition) return;
|
|
1211
|
+
const params = definition.kind === "Operator" ? definition.params : [];
|
|
1212
|
+
const alias = createLocalAlias(
|
|
1213
|
+
key,
|
|
1214
|
+
override.key,
|
|
1215
|
+
params,
|
|
1216
|
+
params.slice(override.prefix),
|
|
1217
|
+
definition.location,
|
|
1218
|
+
);
|
|
1219
|
+
definitions.set(key, alias);
|
|
1220
|
+
linkedRoot.visible.forEach((symbol, name) => {
|
|
1221
|
+
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
1222
|
+
});
|
|
879
1223
|
});
|
|
1224
|
+
const unassigned = declaredConstants.filter(
|
|
1225
|
+
(name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
|
|
1226
|
+
);
|
|
1227
|
+
if (unassigned.length > 0 && !options.isUnconfigured) {
|
|
1228
|
+
// TLC reports the first constant it finds unassigned in SANY's order of declarations.
|
|
1229
|
+
const name =
|
|
1230
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
|
|
1231
|
+
unassigned.includes(constant),
|
|
1232
|
+
) ?? unassigned[0];
|
|
1233
|
+
throw new TlaConfigError(
|
|
1234
|
+
`The constant parameter ${name} is not assigned a value by the configuration file.`,
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
if (options.isTraceRecorded) {
|
|
1239
|
+
builtins.forEach((builtin, name) => {
|
|
1240
|
+
const replacement = getTraceSpecArithmetic(builtin);
|
|
1241
|
+
if (replacement) builtins.set(name, replacement);
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
880
1244
|
|
|
881
1245
|
const environment: Environment = {
|
|
882
1246
|
runtime,
|
|
@@ -884,6 +1248,7 @@ export const createEnvironment = (
|
|
|
884
1248
|
builtins,
|
|
885
1249
|
constants,
|
|
886
1250
|
substitutions,
|
|
1251
|
+
instanceNames,
|
|
887
1252
|
variables,
|
|
888
1253
|
variableIndex: getVariableIndex(
|
|
889
1254
|
variables,
|
|
@@ -891,38 +1256,73 @@ export const createEnvironment = (
|
|
|
891
1256
|
),
|
|
892
1257
|
definitionValues: new Map(),
|
|
893
1258
|
constantLevels: new Map(),
|
|
1259
|
+
substitutedDefinitions,
|
|
894
1260
|
evaluators: new WeakMap(),
|
|
895
1261
|
};
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
907
|
-
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
908
|
-
};
|
|
1262
|
+
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
1263
|
+
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
1264
|
+
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
1265
|
+
const analyzer = createLevelAnalyzer({
|
|
1266
|
+
constants: new Set(constants.keys()),
|
|
1267
|
+
variables: new Set([
|
|
1268
|
+
...variables,
|
|
1269
|
+
...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
|
|
1270
|
+
]),
|
|
1271
|
+
getDefinition: (name) => getDefinition(name, environment),
|
|
909
1272
|
});
|
|
1273
|
+
const getEvaluationError = (body: Expression) => {
|
|
1274
|
+
try {
|
|
1275
|
+
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
1276
|
+
return undefined;
|
|
1277
|
+
} catch (error) {
|
|
1278
|
+
if (error instanceof TlaEvaluationError) return error;
|
|
1279
|
+
throw error;
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
910
1282
|
substitutions.forEach((target, name) => {
|
|
911
1283
|
const definition = definitions.get(target);
|
|
912
|
-
|
|
1284
|
+
const error =
|
|
913
1285
|
declaredConstants.includes(name) &&
|
|
914
|
-
definition &&
|
|
915
|
-
|
|
916
|
-
|
|
1286
|
+
definition?.kind === "Operator" &&
|
|
1287
|
+
definition.params.length === 0 &&
|
|
1288
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0
|
|
1289
|
+
? getEvaluationError(definition.body)
|
|
1290
|
+
: undefined;
|
|
1291
|
+
if (error) {
|
|
1292
|
+
// TLC's SpecProcessor adds the message of a TLCRuntimeException, not of an EvalException.
|
|
1293
|
+
const addendum = isEvalException(error) ? "" : ` - specifically: ${error.message}`;
|
|
917
1294
|
throw new TlaConfigError(
|
|
918
|
-
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
1295
|
+
`The configuration file substitutes constant ${name} with non-constant ${target}${addendum}`,
|
|
919
1296
|
);
|
|
920
1297
|
}
|
|
921
1298
|
});
|
|
1299
|
+
// TLC 1.7.4 processes the whole spec before it creates its model checker, which TLCGet reads.
|
|
1300
|
+
if (runtime.version !== "1.7.4") runtime.isStarted = true;
|
|
1301
|
+
// TLC computes the constant definitions of each module in the order SANY finishes the modules,
|
|
1302
|
+
// except those of a module that an INSTANCE gives parameters to.
|
|
1303
|
+
const instantiated = new Set(
|
|
1304
|
+
linkedList.flatMap((linked) => linked.module.instances.map((instance) => instance.module)),
|
|
1305
|
+
);
|
|
1306
|
+
const getDefinitionOrder = createTlcDefinitionOrder(options.source, options.loadModule);
|
|
922
1307
|
return {
|
|
923
1308
|
environment,
|
|
924
1309
|
assumptions,
|
|
925
1310
|
variableLocations: linkedRoot.variableLocations,
|
|
926
1311
|
instanceActions,
|
|
1312
|
+
startupDefinitions: linkedList
|
|
1313
|
+
.filter(
|
|
1314
|
+
(linked) => !instantiated.has(linked.module.name) || getDeclarations(linked).length === 0,
|
|
1315
|
+
)
|
|
1316
|
+
.flatMap((linked) =>
|
|
1317
|
+
getDefinitionOrder(linked === linkedRoot ? undefined : linked.module.name).flatMap(
|
|
1318
|
+
(name) => {
|
|
1319
|
+
const definition = definitions.get(linked.visible.get(name)?.key ?? name);
|
|
1320
|
+
return definition ? [definition] : [];
|
|
1321
|
+
},
|
|
1322
|
+
),
|
|
1323
|
+
),
|
|
1324
|
+
rootConstants: linkedRoot.constants,
|
|
1325
|
+
rootSymbols: linkedRoot.visible,
|
|
1326
|
+
javaOverrides: linkedList.flatMap((linked) => javaOverrides.get(linked.module.name) ?? []),
|
|
927
1327
|
};
|
|
928
1328
|
};
|