tla-ts 0.0.4-dev.b254db0 → 0.0.4-dev.ca40dfe
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 +31 -10
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +27 -27
- package/dist/deciders.js +34 -34
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +12 -14
- package/dist/index.d.ts +12 -14
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +37 -27
- package/dist/prove.js +39 -29
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +76 -86
- package/dist/transfer.js +73 -83
- package/dist/worker.cjs +6 -6
- package/dist/worker.js +6 -6
- package/package.json +1 -1
- package/src/ast.ts +2 -2
- package/src/bags.ts +22 -23
- package/src/builtins.ts +16 -8
- package/src/check.ts +131 -1154
- package/src/{cli-options.ts → cli/options.ts} +54 -9
- package/src/cli/run.ts +63 -0
- package/src/cli/shared.ts +106 -0
- package/src/cli/tlapm.ts +374 -0
- package/src/cli/tlc.ts +941 -0
- package/src/cli/xml-exporter.ts +221 -0
- package/src/cli.ts +1 -1
- package/src/codegen.ts +17 -15
- package/src/config.ts +5 -5
- package/src/dependencies.ts +4 -6
- package/src/environment.ts +55 -60
- package/src/errors.ts +12 -36
- package/src/evaluate.ts +81 -101
- package/src/exit-codes.ts +1 -1
- package/src/fingerprints.ts +3 -3
- package/src/instances.ts +56 -46
- package/src/java-overrides.ts +79 -41
- package/src/json.ts +31 -36
- package/src/levels.ts +10 -13
- package/src/{live-check.ts → liveness-checker.ts} +169 -142
- package/src/liveness.ts +38 -34
- package/src/obligations/action.ts +9 -45
- package/src/obligations/{anon.ts → anonymize.ts} +13 -25
- package/src/obligations/equality.ts +260 -0
- package/src/obligations/expression.ts +42 -517
- package/src/obligations/generate.ts +88 -778
- package/src/obligations/index.ts +20 -22
- package/src/obligations/levels.ts +36 -41
- package/src/obligations/module.ts +88 -708
- package/src/obligations/normalize.ts +554 -0
- package/src/obligations/operators.ts +176 -0
- package/src/obligations/pragma.ts +21 -15
- package/src/obligations/printer.ts +81 -188
- package/src/obligations/proof.ts +0 -1
- package/src/obligations/regroup.ts +100 -0
- package/src/obligations/simplify.ts +668 -0
- package/src/obligations/source.ts +25 -131
- package/src/obligations/{deref.ts → subexpression.ts} +48 -56
- package/src/obligations/substitution.ts +261 -0
- package/src/obligations/temporal.ts +28 -26
- package/src/obligations/tuples.ts +37 -53
- package/src/parallel-worker.ts +37 -33
- package/src/parallel.ts +30 -24
- package/src/parser.ts +67 -74
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
- package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
- package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
- package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
- package/src/prove/decide-pool.ts +7 -12
- package/src/{obligations/pickle.ts → prove/first-order.ts} +79 -2
- package/src/prove/isabelle/decide.ts +3 -6
- package/src/prove/isabelle/encode.ts +35 -93
- package/src/prove/isabelle/isar/inner-syntax.ts +1 -1
- package/src/prove/isabelle/isar/method.ts +3 -3
- package/src/prove/isabelle/isar/type-check.ts +1 -1
- package/src/prove/isabelle/provers/blast/blast.ts +1 -1
- package/src/prove/isabelle/provers/blast/branch.ts +1 -1
- package/src/prove/isabelle/provers/blast/term.ts +3 -4
- package/src/prove/isabelle/provers/clasimp.ts +1 -1
- package/src/prove/isabelle/provers/classical.ts +3 -3
- package/src/prove/isabelle/provers/hypsubst.ts +1 -9
- package/src/prove/isabelle/pure/conv.ts +1 -1
- package/src/prove/isabelle/pure/drule.ts +2 -2
- package/src/prove/isabelle/pure/envir.ts +3 -35
- package/src/prove/isabelle/pure/logic.ts +4 -76
- package/src/prove/isabelle/pure/name.ts +1 -13
- package/src/prove/isabelle/pure/net.ts +2 -49
- package/src/prove/isabelle/pure/pattern.ts +3 -22
- package/src/prove/isabelle/pure/raw-simplifier.ts +3 -18
- package/src/prove/isabelle/pure/seq.ts +0 -23
- package/src/prove/isabelle/pure/simplifier.ts +2 -8
- package/src/prove/isabelle/pure/table.ts +1 -8
- package/src/prove/isabelle/pure/tactic.ts +5 -113
- package/src/prove/isabelle/pure/tactical.ts +1 -84
- package/src/prove/isabelle/pure/term-ord.ts +1 -55
- package/src/prove/isabelle/pure/term.ts +8 -128
- package/src/prove/isabelle/pure/thm.ts +4 -27
- package/src/prove/isabelle/pure/type.ts +0 -12
- package/src/prove/isabelle/pure/unify.ts +1 -1
- package/src/prove/isabelle/tla/classical-setup.ts +1 -1
- package/src/prove/ocaml.ts +1 -1
- package/src/prove/prep/action.ts +23 -36
- package/src/prove/prep/enabled-axioms.ts +2 -2
- package/src/prove/prep/fingerprint.ts +3 -3
- package/src/prove/prep/level-comparison.ts +9 -37
- package/src/prove/prep/md5.ts +2 -9
- package/src/prove/prep/normalize.ts +309 -0
- package/src/prove/prove-worker.ts +2 -5
- package/src/prove/prove.ts +98 -111
- package/src/prove/ptl/encode.ts +19 -31
- package/src/prove/smt/axioms.ts +45 -58
- package/src/prove/smt/encode.ts +124 -15
- package/src/prove/smt/instances.ts +2 -3
- package/src/prove/smt/rewrite.ts +24 -31
- package/src/prove/smt/solver/ast-util.ts +0 -56
- package/src/prove/smt/solver/case-split-queue.ts +1 -1
- package/src/prove/smt/solver/enode.ts +1 -1
- package/src/prove/smt/solver/lp/dep-intervals.ts +0 -14
- package/src/prove/smt/solver/model-macro-solver.ts +1 -1
- package/src/prove/smt/solver/nlsat/algebraic.ts +3 -3
- package/src/prove/smt/solver/nlsat/atoms.ts +1 -1
- package/src/prove/smt/solver/nlsat/explain.ts +1 -1
- package/src/prove/smt/solver/nlsat/interval-set.ts +1 -1
- package/src/prove/smt/solver/nlsat/levelwise.ts +4 -7
- package/src/prove/smt/solver/nlsat/polynomial.ts +4 -4
- package/src/prove/smt/solver/nlsat/upolynomial.ts +2 -2
- package/src/prove/smt/solver/pattern-inference.ts +1 -1
- package/src/prove/smt/solver/relevancy.ts +2 -2
- package/src/prove/smt/solver/rewriter.ts +1 -1
- package/src/prove/smt/solver/solver.ts +2 -2
- package/src/{tlapm-cache.ts → prove/tlapm/cache.ts} +11 -11
- package/src/{ocaml-marshal.ts → prove/tlapm/ocaml-marshal.ts} +1 -1
- package/src/prove/verdict.ts +0 -5
- package/src/prove/zenon/decide.ts +5 -11
- package/src/prove/zenon/encode.ts +21 -80
- package/src/prove/zenon/eqrel.ts +1 -1
- package/src/prove/zenon/equality-rules.ts +1 -1
- package/src/prove/zenon/expr.ts +2 -2
- package/src/prove/zenon/index-table.ts +1 -1
- package/src/prove/zenon/isar.ts +1 -1
- package/src/prove/zenon/llproof.ts +1 -1
- package/src/prove/zenon/mltoll.ts +1 -1
- package/src/prove/zenon/node.ts +1 -1
- package/src/prove/zenon/search.ts +1 -1
- package/src/prove/zenon/tla/terms.ts +1 -20
- package/src/prove/zenon/tla/tla.ts +1 -1
- package/src/randomization.ts +19 -24
- package/src/resolve.ts +15 -22
- package/src/runtime.ts +8 -8
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +38 -55
- package/src/{sany-levels.ts → sany/levels.ts} +45 -48
- package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +16 -17
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
- package/src/{sany-source.ts → sany/source.ts} +6 -7
- package/src/{sany-xml.ts → sany/xml.ts} +5 -6
- package/src/search.ts +969 -0
- package/src/spec-description.ts +26 -20
- package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
- package/src/standard-modules.ts +16 -20
- package/src/state-graph.ts +9 -8
- package/src/substitution-levels.ts +2 -2
- package/src/successors.ts +16 -19
- package/src/symmetry.ts +1 -1
- package/src/tableau.ts +6 -7
- package/src/tlc/fingerprint.ts +214 -0
- package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +3 -213
- package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
- package/src/transfer.ts +32 -32
- package/src/values.ts +72 -67
- package/src/worker.ts +1 -4
- package/dist/run-cli.cjs +0 -45
- package/dist/run-cli.js +0 -45
- package/src/canonical.ts +0 -40
- package/src/obligations/ctx.ts +0 -64
- package/src/prove/prep/defs.ts +0 -33
- package/src/prove/prep/elab.ts +0 -156
- package/src/prove/prep/enabled.ts +0 -43
- package/src/prove/prep/flatten.ts +0 -70
- package/src/prove/prep/trivial.ts +0 -22
- package/src/prove/smt/axiomatize.ts +0 -69
- package/src/prove/smt/simplify.ts +0 -57
- package/src/run-cli.ts +0 -1806
- package/src/standard-ranges.ts +0 -116
- package/src/value-sources.ts +0 -29
- /package/src/prove/prep/{pragma.ts → methods.ts} +0 -0
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
- /package/src/{tlc-version.ts → tlc/version.ts} +0 -0
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
// A port of tlapm's Module.Elab: normalization anonymizes a module unit by unit and expands each
|
|
2
|
+
// instance into the units of the module it instantiates.
|
|
3
|
+
|
|
4
|
+
import { TlaProofError, TlapmBugError } from "../errors";
|
|
5
|
+
import { lambdify } from "./action";
|
|
6
|
+
import type { Anonymizer } from "./anonymize";
|
|
7
|
+
import { isEqualHyp } from "./equality";
|
|
8
|
+
import {
|
|
9
|
+
adjoinAll,
|
|
10
|
+
at,
|
|
11
|
+
exprifySequent,
|
|
12
|
+
findNamed,
|
|
13
|
+
getAtPrefix,
|
|
14
|
+
getFact,
|
|
15
|
+
getFresh,
|
|
16
|
+
getLocationText,
|
|
17
|
+
getLocusPrefix,
|
|
18
|
+
getScopeHyps,
|
|
19
|
+
mapAccumulate,
|
|
20
|
+
type Defn,
|
|
21
|
+
type Expr,
|
|
22
|
+
type InstanceDefinition,
|
|
23
|
+
type Locus,
|
|
24
|
+
type Scope,
|
|
25
|
+
type Sequent,
|
|
26
|
+
type Warn,
|
|
27
|
+
} from "./expression";
|
|
28
|
+
import {
|
|
29
|
+
bindTheoremName,
|
|
30
|
+
compareNames,
|
|
31
|
+
getHypSize,
|
|
32
|
+
getUnitHyps,
|
|
33
|
+
substituteUnits,
|
|
34
|
+
type ModuleUnit,
|
|
35
|
+
type Mule,
|
|
36
|
+
} from "./module";
|
|
37
|
+
import { getUnique, type Counter, type Proof } from "./proof";
|
|
38
|
+
import type { createSimplifier } from "./simplify";
|
|
39
|
+
import {
|
|
40
|
+
consAll,
|
|
41
|
+
consSubstitution,
|
|
42
|
+
shift,
|
|
43
|
+
shiftExpr,
|
|
44
|
+
substituteExpr,
|
|
45
|
+
substituteHyp,
|
|
46
|
+
substituteSequent,
|
|
47
|
+
type Substitution,
|
|
48
|
+
} from "./substitution";
|
|
49
|
+
import { getScopeTime } from "./temporal";
|
|
50
|
+
|
|
51
|
+
// latestModules is the module context of the latest anonymization of an axiom, theorem or USE,
|
|
52
|
+
// which tlapm's anonymizer keeps using for the instances inside later definitions.
|
|
53
|
+
export interface Pipeline {
|
|
54
|
+
anonymizer: Anonymizer;
|
|
55
|
+
simplify: ReturnType<typeof createSimplifier>;
|
|
56
|
+
salts: Counter;
|
|
57
|
+
latestModules: Map<string, Mule>;
|
|
58
|
+
onWarning: Warn;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const lambdifyTheorem = (
|
|
62
|
+
pipeline: Pipeline,
|
|
63
|
+
scope: Scope | undefined,
|
|
64
|
+
name: string | undefined,
|
|
65
|
+
sequent: Sequent,
|
|
66
|
+
) => {
|
|
67
|
+
try {
|
|
68
|
+
const lambdified = lambdify(pipeline.anonymizer, pipeline.onWarning, scope, {
|
|
69
|
+
kind: "Sequent",
|
|
70
|
+
sequent,
|
|
71
|
+
});
|
|
72
|
+
if (lambdified.kind !== "Sequent") throw new TlapmBugError();
|
|
73
|
+
return lambdified.sequent;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (!(error instanceof TlaProofError) || error.failure === undefined) throw error;
|
|
76
|
+
const location = sequent.active.location;
|
|
77
|
+
const message = `${error.failure}\nCould not instantiate the theorem ${name === undefined ? "" : `named: ${name}, `}at: ${getLocationText(location)}`;
|
|
78
|
+
pipeline.onWarning({ text: `${getAtPrefix(location)}[ERROR]${message}`, channel: "stdout" });
|
|
79
|
+
pipeline.onWarning({ text: message, channel: "toolboxOnly" });
|
|
80
|
+
return { context: [], active: at({ kind: "Internal", builtin: "TRUE" }, sequent.active) };
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const lambdifyUnit = (pipeline: Pipeline, scope: Scope | undefined, unit: ModuleUnit) => {
|
|
85
|
+
if (unit.kind === "Theorem") {
|
|
86
|
+
return { ...unit, sequent: lambdifyTheorem(pipeline, scope, unit.name, unit.sequent) };
|
|
87
|
+
}
|
|
88
|
+
if (unit.kind !== "Definition") return unit;
|
|
89
|
+
const definition = unit.definition;
|
|
90
|
+
if (definition.kind !== "Operator" && definition.kind !== "Bpragma") return unit;
|
|
91
|
+
const body = lambdify(pipeline.anonymizer, pipeline.onWarning, scope, definition.body);
|
|
92
|
+
return { ...unit, definition: { ...definition, body } };
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// lambdify_enabled_cdot: the ENABLED and \cdot of each definition and theorem statement bind
|
|
96
|
+
// their variables before instantiation substitutes for them. A theorem that cannot be lambdified
|
|
97
|
+
// states TRUE.
|
|
98
|
+
const lambdifyUnits = (
|
|
99
|
+
pipeline: Pipeline,
|
|
100
|
+
scope: Scope | undefined,
|
|
101
|
+
units: ModuleUnit[],
|
|
102
|
+
): [Scope | undefined, ModuleUnit[]] =>
|
|
103
|
+
mapAccumulate(scope, units, (current, unit): [Scope | undefined, ModuleUnit] => {
|
|
104
|
+
if (unit.kind === "Submod") {
|
|
105
|
+
const [inner, body] = lambdifyUnits(pipeline, current, unit.module.body);
|
|
106
|
+
return [inner, { ...unit, module: { ...unit.module, body } }];
|
|
107
|
+
}
|
|
108
|
+
const lambdified = lambdifyUnit(pipeline, current, unit);
|
|
109
|
+
return [adjoinAll(current, getUnitHyps(lambdified)), lambdified];
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// apply_subst: each parameter declaration of the instancee gives way to its substitute.
|
|
113
|
+
const applyParameters = (units: ModuleUnit[], substitutes: Map<string, Expr>) => {
|
|
114
|
+
const body: ModuleUnit[] = [];
|
|
115
|
+
let size = 0;
|
|
116
|
+
let rest = units;
|
|
117
|
+
while (rest.length > 0) {
|
|
118
|
+
const [unit, ...tail] = rest;
|
|
119
|
+
const split =
|
|
120
|
+
unit.kind === "Constants"
|
|
121
|
+
? {
|
|
122
|
+
name: unit.declarations[0].name,
|
|
123
|
+
remaining: { ...unit, declarations: unit.declarations.slice(1) },
|
|
124
|
+
}
|
|
125
|
+
: unit.kind === "Variables"
|
|
126
|
+
? { name: unit.names[0], remaining: { ...unit, names: unit.names.slice(1) } }
|
|
127
|
+
: undefined;
|
|
128
|
+
if (split) {
|
|
129
|
+
const substitute = substitutes.get(split.name);
|
|
130
|
+
if (!substitute) throw new TlaProofError(`Parameter ${split.name} has no substitute`);
|
|
131
|
+
const isEmpty = getHypSize(split.remaining) === 0;
|
|
132
|
+
rest = substituteUnits(
|
|
133
|
+
consSubstitution(shiftExpr(size, substitute), shift(0)),
|
|
134
|
+
isEmpty ? tail : [split.remaining, ...tail],
|
|
135
|
+
);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
body.push(unit);
|
|
139
|
+
size += getHypSize(unit);
|
|
140
|
+
rest = tail;
|
|
141
|
+
}
|
|
142
|
+
return body;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// resub_for: references to the instancee's own units become applications to the instance
|
|
146
|
+
// arguments, which the definitions now bind themselves.
|
|
147
|
+
const getLocalSubstitution = (
|
|
148
|
+
count: number,
|
|
149
|
+
argumentCount: number,
|
|
150
|
+
location: Locus | undefined,
|
|
151
|
+
): Substitution => {
|
|
152
|
+
const args = Array.from({ length: argumentCount }, (_item, index) =>
|
|
153
|
+
at({ kind: "Ix", index: argumentCount - index }, { location }),
|
|
154
|
+
);
|
|
155
|
+
const parameters = Array.from({ length: argumentCount }, (_item, index) =>
|
|
156
|
+
at({ kind: "Ix", index: index + 1 }, { location }),
|
|
157
|
+
);
|
|
158
|
+
const locals = Array.from({ length: count }, (_item, index): Expr => {
|
|
159
|
+
const operator = at({ kind: "Ix", index: index + argumentCount + 1 }, { location });
|
|
160
|
+
return argumentCount > 0 ? at({ kind: "Apply", operator, args }, { location }) : operator;
|
|
161
|
+
});
|
|
162
|
+
return consAll([...locals, ...parameters].reverse(), shift(count + argumentCount));
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const addParameters = (expression: Expr, args: string[]): Expr => {
|
|
166
|
+
const params = args.map((name) => ({ name, shape: 0 }));
|
|
167
|
+
if (expression.kind === "Lambda")
|
|
168
|
+
return { ...expression, params: [...params, ...expression.params] };
|
|
169
|
+
return args.length === 0
|
|
170
|
+
? expression
|
|
171
|
+
: at({ kind: "Lambda", params, body: expression }, expression);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// localize: names the instancee's units under the instance name, salting its local definitions, and
|
|
175
|
+
// abstracts each over the instance arguments.
|
|
176
|
+
const localize = (
|
|
177
|
+
pipeline: Pipeline,
|
|
178
|
+
units: ModuleUnit[],
|
|
179
|
+
prefix: string,
|
|
180
|
+
instance: InstanceDefinition,
|
|
181
|
+
isLocal: boolean,
|
|
182
|
+
location: Locus | undefined,
|
|
183
|
+
) => {
|
|
184
|
+
const getLocalizedName = (name: string) => (prefix === "" ? name : `${prefix}!${name}`);
|
|
185
|
+
const argumentCount = instance.args.length;
|
|
186
|
+
const body: ModuleUnit[] = [];
|
|
187
|
+
let size = 0;
|
|
188
|
+
let rest = units;
|
|
189
|
+
let isComplained = argumentCount === 0;
|
|
190
|
+
const complain = (text: string) => {
|
|
191
|
+
if (isComplained) return;
|
|
192
|
+
isComplained = true;
|
|
193
|
+
pipeline.onWarning({ text, channel: "stderr" });
|
|
194
|
+
};
|
|
195
|
+
while (rest.length > 0) {
|
|
196
|
+
const [unit, ...tail] = rest;
|
|
197
|
+
rest = tail;
|
|
198
|
+
const localized = (expression: Expr) =>
|
|
199
|
+
addParameters(
|
|
200
|
+
substituteExpr(getLocalSubstitution(size, argumentCount, location), expression),
|
|
201
|
+
instance.args,
|
|
202
|
+
);
|
|
203
|
+
switch (unit.kind) {
|
|
204
|
+
case "Definition": {
|
|
205
|
+
const definition = unit.definition;
|
|
206
|
+
if (definition.kind !== "Operator" && definition.kind !== "Bpragma") {
|
|
207
|
+
throw new TlaProofError("Module.Elab.localize");
|
|
208
|
+
}
|
|
209
|
+
const name = unit.isLocal
|
|
210
|
+
? `${definition.name}$${getUnique(pipeline.salts)}`
|
|
211
|
+
: definition.name;
|
|
212
|
+
body.push({
|
|
213
|
+
...unit,
|
|
214
|
+
definition: {
|
|
215
|
+
...definition,
|
|
216
|
+
name: getLocalizedName(name),
|
|
217
|
+
body: localized(definition.body),
|
|
218
|
+
},
|
|
219
|
+
whereDefined: unit.isLocal ? "Builtin" : unit.whereDefined,
|
|
220
|
+
isLocal: unit.isLocal || isLocal,
|
|
221
|
+
});
|
|
222
|
+
size += 1;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
case "Axiom": {
|
|
226
|
+
complain(
|
|
227
|
+
`${getLocusPrefix(location)}Instancing produces parametric axioms.\nSuch axioms will be forcibly hidden and cannot be cited.\n(This warning appears at-most once per INSTANCE declaration.)`,
|
|
228
|
+
);
|
|
229
|
+
const axiom: ModuleUnit = {
|
|
230
|
+
...unit,
|
|
231
|
+
name: unit.name === undefined ? undefined : getLocalizedName(unit.name),
|
|
232
|
+
expression: localized(unit.expression),
|
|
233
|
+
};
|
|
234
|
+
body.push(axiom);
|
|
235
|
+
size += getHypSize(axiom);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
case "Theorem": {
|
|
239
|
+
complain(
|
|
240
|
+
`${getAtPrefix(location)}Warning: Instancing produces parametric theorems.\nSuch theorems will be forcibly hidden and cannot be cited.\n(This warning appears at-most once per INSTANCE declaration.)`,
|
|
241
|
+
);
|
|
242
|
+
const expression = localized(at(exprifySequent(unit.sequent), unit));
|
|
243
|
+
body.push({
|
|
244
|
+
...unit,
|
|
245
|
+
name: unit.name === undefined ? undefined : getLocalizedName(unit.name),
|
|
246
|
+
sequent:
|
|
247
|
+
expression.kind === "Sequent"
|
|
248
|
+
? expression.sequent
|
|
249
|
+
: { context: [], active: expression },
|
|
250
|
+
proof: { kind: "Omitted", omission: "Elsewhere", location: unit.location },
|
|
251
|
+
});
|
|
252
|
+
size += 1;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
case "Mutate":
|
|
256
|
+
if (unit.mode === "Use") rest = substituteUnits(shift(-unit.usable.facts.length), rest);
|
|
257
|
+
continue;
|
|
258
|
+
case "Submod":
|
|
259
|
+
continue;
|
|
260
|
+
default:
|
|
261
|
+
throw new TlaProofError("Module.Elab.localize");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return body;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// localize_axioms: an axiom becomes a hypothesis of each theorem after it, and a named one also a
|
|
268
|
+
// definition.
|
|
269
|
+
const localizeAxioms = (units: ModuleUnit[]) => {
|
|
270
|
+
const body: ModuleUnit[] = [];
|
|
271
|
+
let rest = units;
|
|
272
|
+
while (rest.length > 0) {
|
|
273
|
+
const [unit, ...tail] = rest;
|
|
274
|
+
if (unit.kind !== "Axiom") {
|
|
275
|
+
body.push(unit);
|
|
276
|
+
rest = tail;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const location = unit.location;
|
|
280
|
+
if (unit.name !== undefined) {
|
|
281
|
+
body.push({
|
|
282
|
+
kind: "Definition",
|
|
283
|
+
definition: { kind: "Operator", name: unit.name, body: unit.expression, location },
|
|
284
|
+
whereDefined: { kind: "Proof", time: "Always" },
|
|
285
|
+
visibility: "Visible",
|
|
286
|
+
isLocal: false,
|
|
287
|
+
location,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const hyp = getFact(
|
|
291
|
+
unit.name === undefined ? shiftExpr(1, unit.expression) : at({ kind: "Ix", index: 2 }, unit),
|
|
292
|
+
"Visible",
|
|
293
|
+
"Always",
|
|
294
|
+
location,
|
|
295
|
+
);
|
|
296
|
+
let distance = 0;
|
|
297
|
+
const inserted = tail.map((next): ModuleUnit => {
|
|
298
|
+
const size = distance;
|
|
299
|
+
distance += getHypSize(next);
|
|
300
|
+
if (next.kind !== "Theorem") return next;
|
|
301
|
+
const sequent = substituteSequent(shift(1), next.sequent);
|
|
302
|
+
return {
|
|
303
|
+
...next,
|
|
304
|
+
sequent: { ...sequent, context: [substituteHyp(shift(size), hyp), ...sequent.context] },
|
|
305
|
+
axiomCount: next.axiomCount + 1,
|
|
306
|
+
};
|
|
307
|
+
});
|
|
308
|
+
rest = substituteUnits(shift(-1), inserted);
|
|
309
|
+
}
|
|
310
|
+
return body;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const getParameters = (module: Mule) =>
|
|
314
|
+
new Set(
|
|
315
|
+
module.body.flatMap((unit) =>
|
|
316
|
+
unit.kind === "Constants"
|
|
317
|
+
? unit.declarations.map((declaration) => declaration.name)
|
|
318
|
+
: unit.kind === "Variables"
|
|
319
|
+
? unit.names
|
|
320
|
+
: [],
|
|
321
|
+
),
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
// Module.Elab.instantiate: the units of an instantiated module, in the context of the instance.
|
|
325
|
+
const instantiate = (
|
|
326
|
+
pipeline: Pipeline,
|
|
327
|
+
modules: Map<string, Mule>,
|
|
328
|
+
scope: Scope | undefined,
|
|
329
|
+
instance: InstanceDefinition,
|
|
330
|
+
isLocal: boolean,
|
|
331
|
+
prefix: string,
|
|
332
|
+
location: Locus | undefined,
|
|
333
|
+
) => {
|
|
334
|
+
const module = modules.get(instance.module);
|
|
335
|
+
if (!module) throw new TlaProofError(`Module "${instance.module}" unknown`);
|
|
336
|
+
const parameters = getParameters(module);
|
|
337
|
+
for (const substitution of instance.substitutions) {
|
|
338
|
+
if (!parameters.has(substitution.name)) {
|
|
339
|
+
throw new TlaProofError(
|
|
340
|
+
`Parameter ${substitution.name} occurs in the statement \`INSTANCE ${instance.module} WITH\`, and is not declared in the module ${instance.module}`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const instanceScope = adjoinAll(
|
|
345
|
+
scope,
|
|
346
|
+
instance.args.map((name) => getFresh(name, "Unknown")),
|
|
347
|
+
);
|
|
348
|
+
const substitutes = new Map(
|
|
349
|
+
instance.substitutions.map((entry) => [entry.name, entry.expression]),
|
|
350
|
+
);
|
|
351
|
+
for (const name of [...parameters].sort(compareNames)) {
|
|
352
|
+
if (substitutes.has(name)) continue;
|
|
353
|
+
if (!findNamed(instanceScope, name)) {
|
|
354
|
+
throw new TlaProofError(
|
|
355
|
+
`Required parameter ${name} for module ${instance.module} could not be inferred.`,
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
substitutes.set(name, at({ kind: "Opaque", name }, { location }));
|
|
359
|
+
}
|
|
360
|
+
const anonymized = new Map(
|
|
361
|
+
[...substitutes].map(([name, expression]) => [
|
|
362
|
+
name,
|
|
363
|
+
pipeline.anonymizer.expr(instanceScope, expression),
|
|
364
|
+
]),
|
|
365
|
+
);
|
|
366
|
+
const body = substituteUnits(
|
|
367
|
+
shift((instanceScope?.size ?? 0) - (module.depth ?? 0)),
|
|
368
|
+
module.body.map((unit): ModuleUnit =>
|
|
369
|
+
unit.kind === "Theorem"
|
|
370
|
+
? {
|
|
371
|
+
...unit,
|
|
372
|
+
proof: { kind: "Omitted", omission: "Implicit", location: unit.proof.location },
|
|
373
|
+
}
|
|
374
|
+
: unit,
|
|
375
|
+
),
|
|
376
|
+
);
|
|
377
|
+
const [, lambdified] = lambdifyUnits(pipeline, instanceScope, body);
|
|
378
|
+
return localizeAxioms(
|
|
379
|
+
localize(
|
|
380
|
+
pipeline,
|
|
381
|
+
applyParameters(lambdified, anonymized),
|
|
382
|
+
prefix,
|
|
383
|
+
instance,
|
|
384
|
+
isLocal,
|
|
385
|
+
location,
|
|
386
|
+
),
|
|
387
|
+
);
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// make_defs: the definitions among the units an INSTANCE inside a LET or DEFINE stands for.
|
|
391
|
+
export const expandInstance = (
|
|
392
|
+
pipeline: Pipeline,
|
|
393
|
+
scope: Scope | undefined,
|
|
394
|
+
definition: Extract<Defn, { kind: "Instance" }>,
|
|
395
|
+
): Defn[] => {
|
|
396
|
+
const name = definition.instance.module;
|
|
397
|
+
if (!pipeline.latestModules.has(name)) {
|
|
398
|
+
pipeline.onWarning({
|
|
399
|
+
text: `${getLocusPrefix(definition.instance.location)}Module ${JSON.stringify(name)} unknown\n`,
|
|
400
|
+
channel: "toolbox",
|
|
401
|
+
});
|
|
402
|
+
throw new TlaProofError(`Module ${JSON.stringify(name)} unknown`, {
|
|
403
|
+
failure: "Module.Elab.anon#defns",
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
const units = instantiate(
|
|
407
|
+
pipeline,
|
|
408
|
+
pipeline.latestModules,
|
|
409
|
+
scope,
|
|
410
|
+
definition.instance,
|
|
411
|
+
true,
|
|
412
|
+
definition.name,
|
|
413
|
+
definition.location,
|
|
414
|
+
);
|
|
415
|
+
const definitions: Defn[] = [];
|
|
416
|
+
let rest = units;
|
|
417
|
+
while (rest.length > 0) {
|
|
418
|
+
const [unit, ...tail] = rest;
|
|
419
|
+
if (unit.kind !== "Definition") {
|
|
420
|
+
rest = substituteUnits(shift(-getHypSize(unit)), tail);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
definitions.push(unit.definition);
|
|
424
|
+
rest = tail;
|
|
425
|
+
}
|
|
426
|
+
return definitions;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// Module.Elab.normalize: anonymizes each unit in the context of the ones before it, expands
|
|
430
|
+
// instances, merges repeated definitions, and simplifies the proofs of important modules. The
|
|
431
|
+
// result also joins the modules that later instances can name.
|
|
432
|
+
export const normalize = (
|
|
433
|
+
pipeline: Pipeline,
|
|
434
|
+
modules: Map<string, Mule>,
|
|
435
|
+
scope: Scope | undefined,
|
|
436
|
+
module: Mule,
|
|
437
|
+
): Mule => {
|
|
438
|
+
const body: ModuleUnit[] = [];
|
|
439
|
+
let current = scope;
|
|
440
|
+
const emit = (unit: ModuleUnit) => {
|
|
441
|
+
body.push(unit);
|
|
442
|
+
current = adjoinAll(current, getUnitHyps(unit));
|
|
443
|
+
};
|
|
444
|
+
const isRedundant = (name: string | undefined) =>
|
|
445
|
+
name !== undefined && findNamed(current, name) !== undefined;
|
|
446
|
+
// Instantiated units arrive anonymized.
|
|
447
|
+
const queue = module.body.map((unit) => ({ unit, isAnonymized: false }));
|
|
448
|
+
for (let index = 0; index < queue.length; index++) {
|
|
449
|
+
const unit = queue[index].unit;
|
|
450
|
+
const expand = (instance: InstanceDefinition, isLocal: boolean, prefix: string) => {
|
|
451
|
+
const units = instantiate(
|
|
452
|
+
pipeline,
|
|
453
|
+
modules,
|
|
454
|
+
current,
|
|
455
|
+
instance,
|
|
456
|
+
isLocal,
|
|
457
|
+
prefix,
|
|
458
|
+
unit.location,
|
|
459
|
+
);
|
|
460
|
+
queue.splice(index + 1, 0, ...units.map((inner) => ({ unit: inner, isAnonymized: true })));
|
|
461
|
+
};
|
|
462
|
+
switch (unit.kind) {
|
|
463
|
+
case "Anoninst":
|
|
464
|
+
expand(unit.instance, unit.isLocal, "");
|
|
465
|
+
continue;
|
|
466
|
+
case "Definition": {
|
|
467
|
+
const definition = unit.definition;
|
|
468
|
+
if (definition.kind === "Instance") {
|
|
469
|
+
expand(definition.instance, unit.isLocal, definition.name);
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
const anonymized =
|
|
473
|
+
definition.kind === "Recursive" || queue[index].isAnonymized
|
|
474
|
+
? definition
|
|
475
|
+
: pipeline.anonymizer.defn(current, definition);
|
|
476
|
+
const normalized: ModuleUnit = { ...unit, definition: anonymized };
|
|
477
|
+
if (definition.kind === "Recursive") {
|
|
478
|
+
emit(normalized);
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
const hyp = getUnitHyps(normalized)[0];
|
|
482
|
+
const found = findNamed(current, definition.name);
|
|
483
|
+
if (found?.hyp.kind === "Defn") {
|
|
484
|
+
const isMerged =
|
|
485
|
+
found.hyp.definition.kind === "Recursive" ||
|
|
486
|
+
isEqualHyp(substituteHyp(shift(found.depth + 1), found.hyp), hyp);
|
|
487
|
+
if (isMerged) {
|
|
488
|
+
const rest = queue.slice(index + 1);
|
|
489
|
+
const substituted = substituteUnits(
|
|
490
|
+
consSubstitution(at({ kind: "Ix", index: found.depth + 1 }, unit), shift(0)),
|
|
491
|
+
rest.map((entry) => entry.unit),
|
|
492
|
+
);
|
|
493
|
+
queue.splice(
|
|
494
|
+
index + 1,
|
|
495
|
+
rest.length,
|
|
496
|
+
...rest.map((entry, offset) => ({ ...entry, unit: substituted[offset] })),
|
|
497
|
+
);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
emit(normalized);
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
case "Recursives":
|
|
505
|
+
case "Constants":
|
|
506
|
+
case "Variables":
|
|
507
|
+
emit(unit);
|
|
508
|
+
continue;
|
|
509
|
+
case "Axiom":
|
|
510
|
+
if (isRedundant(unit.name)) continue;
|
|
511
|
+
pipeline.latestModules = new Map(modules);
|
|
512
|
+
emit({ ...unit, expression: pipeline.anonymizer.expr(current, unit.expression) });
|
|
513
|
+
continue;
|
|
514
|
+
case "Theorem": {
|
|
515
|
+
const isDuplicate = isRedundant(unit.name);
|
|
516
|
+
const name = isDuplicate ? `(redundant)${unit.name}` : unit.name;
|
|
517
|
+
const proof: Proof = isDuplicate
|
|
518
|
+
? { kind: "Obvious", location: unit.proof.location }
|
|
519
|
+
: unit.proof;
|
|
520
|
+
pipeline.latestModules = new Map(modules);
|
|
521
|
+
const sequent = pipeline.anonymizer.sequent(current, unit.sequent);
|
|
522
|
+
const binding = bindTheoremName(name, sequent, unit.location);
|
|
523
|
+
const proofScope = adjoinAll(
|
|
524
|
+
adjoinAll(
|
|
525
|
+
current,
|
|
526
|
+
binding.hyps.map((hyp) => ({ ...hyp, isConstnessUnset: true })),
|
|
527
|
+
),
|
|
528
|
+
binding.sequent.context,
|
|
529
|
+
);
|
|
530
|
+
const anonymized = pipeline.anonymizer.proof(proofScope, proof);
|
|
531
|
+
const simplified = module.isImportant
|
|
532
|
+
? pipeline.simplify(
|
|
533
|
+
getScopeHyps(proofScope),
|
|
534
|
+
binding.sequent.active,
|
|
535
|
+
anonymized,
|
|
536
|
+
getScopeTime(binding.sequent.context, "Always"),
|
|
537
|
+
)
|
|
538
|
+
: anonymized;
|
|
539
|
+
emit({ ...unit, name, sequent, proof: simplified });
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
case "Mutate":
|
|
543
|
+
pipeline.latestModules = new Map(modules);
|
|
544
|
+
emit({ ...unit, usable: pipeline.anonymizer.usable(current, unit.usable) });
|
|
545
|
+
continue;
|
|
546
|
+
case "Submod":
|
|
547
|
+
emit({ ...unit, module: normalize(pipeline, modules, current, unit.module) });
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
const normalized = { ...module, body, depth: scope?.size ?? 0 };
|
|
552
|
+
modules.set(module.name, normalized);
|
|
553
|
+
return normalized;
|
|
554
|
+
};
|