tla-ts 0.0.4-dev.8fefa42 → 0.0.4-dev.a11a30a
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 +27 -130
- package/dist/deciders.cjs +1 -1
- package/dist/deciders.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -3
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +506 -25
- package/dist/prove.js +506 -25
- package/dist/run-cli.cjs +35 -28
- package/dist/run-cli.js +35 -28
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/package.json +1 -1
- package/src/obligations/action.ts +56 -99
- package/src/obligations/anon.ts +37 -41
- package/src/obligations/deref.ts +49 -90
- package/src/obligations/expression.ts +63 -49
- package/src/obligations/format.ts +7 -8
- package/src/obligations/generate.ts +60 -155
- package/src/obligations/index.ts +6 -4
- package/src/obligations/levels.ts +20 -32
- package/src/obligations/module.ts +93 -127
- package/src/obligations/pickle.ts +1 -3
- package/src/obligations/pragma.ts +14 -14
- package/src/obligations/printer.ts +17 -21
- package/src/obligations/proof.ts +0 -1
- package/src/obligations/source.ts +72 -73
- package/src/obligations/temporal.ts +29 -27
- package/src/obligations/tuples.ts +37 -53
- package/src/ocaml-marshal.ts +166 -0
- package/src/prove/decide-pool.ts +12 -17
- package/src/prove/deciders.ts +7 -1
- package/src/prove/first-order.ts +78 -0
- package/src/prove/isabelle/encode.ts +96 -127
- package/src/prove/ocaml.ts +1 -1
- package/src/prove/prep/action.ts +39 -51
- package/src/prove/prep/defs.ts +19 -13
- package/src/prove/prep/elab.ts +1 -1
- package/src/prove/prep/level-comparison.ts +15 -38
- package/src/prove/prep/pragma.ts +4 -4
- package/src/prove/prove-worker.ts +4 -11
- package/src/prove/prove.ts +230 -149
- package/src/prove/ptl/encode.ts +5 -6
- package/src/prove/smt/axioms.ts +78 -79
- package/src/prove/smt/data.ts +16 -45
- package/src/prove/smt/encode.ts +18 -23
- package/src/prove/smt/extensionality.ts +14 -12
- package/src/prove/smt/flatten.ts +3 -18
- package/src/prove/smt/format.ts +14 -3
- package/src/prove/smt/instances.ts +4 -5
- package/src/prove/smt/rewrite.ts +36 -48
- package/src/prove/smt/simplify.ts +10 -10
- package/src/prove/tlapm/tlapm.ts +13 -13
- package/src/prove/verdict.ts +5 -0
- package/src/prove/zenon/encode.ts +36 -101
- package/src/run-cli.ts +264 -115
- package/src/tlapm-cache.ts +236 -0
|
@@ -18,6 +18,7 @@ import type {
|
|
|
18
18
|
import { TlaProofError, TlaUnsupportedError } from "../errors";
|
|
19
19
|
import {
|
|
20
20
|
at,
|
|
21
|
+
getFact,
|
|
21
22
|
type Bound,
|
|
22
23
|
type Builtin,
|
|
23
24
|
type Defn,
|
|
@@ -35,7 +36,6 @@ import {
|
|
|
35
36
|
expandFcn,
|
|
36
37
|
expandQuant,
|
|
37
38
|
expandSetOf,
|
|
38
|
-
expandSetSt,
|
|
39
39
|
getUnbounded,
|
|
40
40
|
type TuplyBound,
|
|
41
41
|
} from "./tuples";
|
|
@@ -81,9 +81,7 @@ const BUILTINS: Record<string, Builtin> = {
|
|
|
81
81
|
STRING: "STRING",
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
const getCanonicalName = (name: string, isPrefix = false) =>
|
|
86
|
-
isPrefix && name === "-" ? "-." : lookupOperator(name).name;
|
|
84
|
+
const getCanonicalName = (name: string) => lookupOperator(name).name;
|
|
87
85
|
|
|
88
86
|
const STEP_PATTERN = /^<(\d+|\*|\+)>([A-Za-z0-9_]*)\.*$/;
|
|
89
87
|
|
|
@@ -203,6 +201,9 @@ const regroup = (expression: Extract<Expression, { kind: "Apply" }>): Expression
|
|
|
203
201
|
|
|
204
202
|
const TUPLY_FAILURE = "use instead the class`E_visit.iter_concrete`";
|
|
205
203
|
|
|
204
|
+
const isDitto = (bounds: QuantifierExpression["bounds"], index: number) =>
|
|
205
|
+
index > 0 && bounds[index - 1].set === bounds[index].set;
|
|
206
|
+
|
|
206
207
|
export const createSource = (file: string, text: string, counter: Counter, isExpanded: boolean) => {
|
|
207
208
|
const lines = text.split("\n");
|
|
208
209
|
|
|
@@ -248,24 +249,16 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
248
249
|
return method ? at({ kind: "With", expression, method }, expression) : expression;
|
|
249
250
|
};
|
|
250
251
|
|
|
251
|
-
const
|
|
252
|
+
const getPointLocus = (location: SourceLocation, length: number): Locus => ({
|
|
252
253
|
file,
|
|
253
254
|
startLine: location.line,
|
|
254
255
|
startColumn: location.column,
|
|
255
256
|
endLine: location.line,
|
|
256
|
-
endColumn: location.column +
|
|
257
|
+
endColumn: location.column + length,
|
|
257
258
|
});
|
|
258
259
|
|
|
259
260
|
const getLocus = (location: SourceLocation): Locus =>
|
|
260
|
-
location.range
|
|
261
|
-
? getRangeLocus(location.range)
|
|
262
|
-
: {
|
|
263
|
-
file,
|
|
264
|
-
startLine: location.line,
|
|
265
|
-
startColumn: location.column,
|
|
266
|
-
endLine: location.line,
|
|
267
|
-
endColumn: location.column + 1,
|
|
268
|
-
};
|
|
261
|
+
location.range ? getRangeLocus(location.range) : getPointLocus(location, 1);
|
|
269
262
|
|
|
270
263
|
const getOperator = (name: string, location: Locus | undefined): Expr => {
|
|
271
264
|
const canonical = getCanonicalName(name);
|
|
@@ -321,11 +314,10 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
321
314
|
bounds.map((bound, index) => {
|
|
322
315
|
if (bound.pattern.kind === "Tuple")
|
|
323
316
|
throw new TlaProofError("Unexpected tuple declaration", { failure: TUPLY_FAILURE });
|
|
324
|
-
const isDitto = index > 0 && bounds[index - 1].set === bound.set;
|
|
325
317
|
return {
|
|
326
318
|
name: bound.pattern.name,
|
|
327
319
|
level: "Constant",
|
|
328
|
-
domain: isDitto
|
|
320
|
+
domain: isDitto(bounds, index)
|
|
329
321
|
? { kind: "Ditto" }
|
|
330
322
|
: { kind: "Domain", expression: convertExpression(bound.set, isExcept) },
|
|
331
323
|
};
|
|
@@ -339,18 +331,17 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
339
331
|
const domain = convertExpression(bound.set, isExcept);
|
|
340
332
|
if (bound.pattern.kind === "Tuple")
|
|
341
333
|
return { kind: "Tuple", names: bound.pattern.names, domain };
|
|
342
|
-
const isDitto = index > 0 && bounds[index - 1].set === bound.set;
|
|
343
334
|
return {
|
|
344
335
|
kind: "Name",
|
|
345
336
|
name: bound.pattern.name,
|
|
346
|
-
domain: isDitto ? { kind: "Ditto" } : { kind: "Domain", expression: domain },
|
|
337
|
+
domain: isDitto(bounds, index) ? { kind: "Ditto" } : { kind: "Domain", expression: domain },
|
|
347
338
|
};
|
|
348
339
|
});
|
|
349
340
|
|
|
350
|
-
const expandTuples = (
|
|
341
|
+
const expandTuples = (expanded: Expr, location: Locus) => {
|
|
351
342
|
if (!isExpanded)
|
|
352
343
|
throw new TlaProofError("Unexpected tuple declaration", { failure: TUPLY_FAILURE });
|
|
353
|
-
return at(
|
|
344
|
+
return at(expanded, { location });
|
|
354
345
|
};
|
|
355
346
|
|
|
356
347
|
const hasTuple = (bounds: QuantifierExpression["bounds"]) =>
|
|
@@ -493,9 +484,14 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
493
484
|
case "Forall":
|
|
494
485
|
case "Exists":
|
|
495
486
|
if (hasTuple(expression.bounds)) {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
487
|
+
return expandTuples(
|
|
488
|
+
expandQuant(
|
|
489
|
+
expression.kind,
|
|
490
|
+
getTuplyBounds(expression.bounds, isExcept),
|
|
491
|
+
recurse(expression.body),
|
|
492
|
+
),
|
|
493
|
+
location,
|
|
494
|
+
);
|
|
499
495
|
}
|
|
500
496
|
return at(
|
|
501
497
|
{
|
|
@@ -518,10 +514,14 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
518
514
|
}
|
|
519
515
|
case "Choose":
|
|
520
516
|
if (expression.pattern.kind === "Tuple") {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
517
|
+
return expandTuples(
|
|
518
|
+
expandChoose(
|
|
519
|
+
{ kind: "Choose", domain: expression.set && recurse(expression.set) },
|
|
520
|
+
expression.pattern.names,
|
|
521
|
+
recurse(expression.body),
|
|
522
|
+
),
|
|
523
|
+
location,
|
|
524
|
+
);
|
|
525
525
|
}
|
|
526
526
|
return at(
|
|
527
527
|
{
|
|
@@ -536,10 +536,14 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
536
536
|
return at({ kind: "SetEnum", elements: expression.elements.map(recurse) }, { location });
|
|
537
537
|
case "SetFilter":
|
|
538
538
|
if (expression.bound.pattern.kind === "Tuple") {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
539
|
+
return expandTuples(
|
|
540
|
+
expandChoose(
|
|
541
|
+
{ kind: "SetSt", domain: recurse(expression.bound.set) },
|
|
542
|
+
expression.bound.pattern.names,
|
|
543
|
+
recurse(expression.predicate),
|
|
544
|
+
),
|
|
545
|
+
location,
|
|
546
|
+
);
|
|
543
547
|
}
|
|
544
548
|
return at(
|
|
545
549
|
{
|
|
@@ -552,9 +556,13 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
552
556
|
);
|
|
553
557
|
case "SetMap":
|
|
554
558
|
if (hasTuple(expression.bounds)) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
559
|
+
return expandTuples(
|
|
560
|
+
expandSetOf(
|
|
561
|
+
recurse(expression.expression),
|
|
562
|
+
getTuplyBounds(expression.bounds, isExcept),
|
|
563
|
+
),
|
|
564
|
+
location,
|
|
565
|
+
);
|
|
558
566
|
}
|
|
559
567
|
return at(
|
|
560
568
|
{
|
|
@@ -579,9 +587,10 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
579
587
|
);
|
|
580
588
|
case "FunctionConstruction":
|
|
581
589
|
if (hasTuple(expression.bounds)) {
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
590
|
+
return expandTuples(
|
|
591
|
+
expandFcn(getTuplyBounds(expression.bounds, isExcept), recurse(expression.body)),
|
|
592
|
+
location,
|
|
593
|
+
);
|
|
585
594
|
}
|
|
586
595
|
return at(
|
|
587
596
|
{
|
|
@@ -768,17 +777,17 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
768
777
|
location,
|
|
769
778
|
};
|
|
770
779
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
definition.kind === "Operator" && definition.params.length === 1 && definition.name === "-"
|
|
774
|
-
|
|
780
|
+
// A prefix minus is the operator -.
|
|
781
|
+
const name =
|
|
782
|
+
definition.kind === "Operator" && definition.params.length === 1 && definition.name === "-"
|
|
783
|
+
? "-."
|
|
784
|
+
: getCanonicalName(definition.name);
|
|
775
785
|
const body = convertExpression(definition.body, isExcept);
|
|
776
786
|
if (definition.kind === "Function" && hasTuple(definition.bounds)) {
|
|
777
|
-
const bounds = getTuplyBounds(definition.bounds, isExcept);
|
|
778
787
|
return {
|
|
779
788
|
kind: "Operator",
|
|
780
789
|
name,
|
|
781
|
-
body: expandTuples(
|
|
790
|
+
body: expandTuples(expandFcn(getTuplyBounds(definition.bounds, isExcept), body), location),
|
|
782
791
|
location,
|
|
783
792
|
};
|
|
784
793
|
}
|
|
@@ -800,7 +809,9 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
800
809
|
params: definition.params.map((param) => ({
|
|
801
810
|
name: getCanonicalName(param.name),
|
|
802
811
|
shape: param.arity,
|
|
803
|
-
...(param.location && {
|
|
812
|
+
...(param.location && {
|
|
813
|
+
location: getPointLocus(param.location, param.name.length),
|
|
814
|
+
}),
|
|
804
815
|
})),
|
|
805
816
|
body,
|
|
806
817
|
},
|
|
@@ -834,24 +845,14 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
834
845
|
return {
|
|
835
846
|
context: statement.assumptions.map((assumption): Hyp => {
|
|
836
847
|
if (assumption.kind === "NewSymbol") return convertNewSymbol(assumption);
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
const expression = convertFactLabel(assumption);
|
|
848
|
-
return {
|
|
849
|
-
kind: "Fact",
|
|
850
|
-
expression,
|
|
851
|
-
visibility: "Visible",
|
|
852
|
-
time: "Now",
|
|
853
|
-
location: expression.location,
|
|
854
|
-
};
|
|
848
|
+
const expression =
|
|
849
|
+
assumption.kind === "AssumeProve"
|
|
850
|
+
? at(
|
|
851
|
+
{ kind: "Sequent", sequent: convertSequent(assumption) },
|
|
852
|
+
{ location: getLocus(assumption.location) },
|
|
853
|
+
)
|
|
854
|
+
: convertFactLabel(assumption);
|
|
855
|
+
return getFact(expression, "Visible", "Now", expression.location);
|
|
855
856
|
}),
|
|
856
857
|
active: convertExpression(statement.conclusion, false),
|
|
857
858
|
};
|
|
@@ -897,12 +898,10 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
897
898
|
defs: use.defs.map(convertDef),
|
|
898
899
|
});
|
|
899
900
|
|
|
900
|
-
const getStepNumber = (token: RegExpExecArray | null, level: number): StepNumber =>
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
: { kind: "Named", level, label, isAnonymous: false };
|
|
905
|
-
};
|
|
901
|
+
const getStepNumber = (token: RegExpExecArray | null, level: number): StepNumber =>
|
|
902
|
+
token?.[2]
|
|
903
|
+
? { kind: "Named", level, label: token[2], isAnonymous: false }
|
|
904
|
+
: { kind: "Unnamed", level, id: getUnique(counter) };
|
|
906
905
|
|
|
907
906
|
// to_proof: a missing proof is implicitly omitted where the enclosing step is.
|
|
908
907
|
const convertProof = (
|
|
@@ -970,7 +969,6 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
970
969
|
const proof = convertProof(step.proof, level + 1, head);
|
|
971
970
|
return { ...make(proof), location: mergeLoci(head, proof.location), step: number };
|
|
972
971
|
};
|
|
973
|
-
const plain = (converted: Step): Step => ({ ...converted, location: head, step: number });
|
|
974
972
|
const withStepMethod = (converted: Step): Step => ({
|
|
975
973
|
...converted,
|
|
976
974
|
...withMethod(step.head),
|
|
@@ -998,10 +996,12 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
998
996
|
case "Instance":
|
|
999
997
|
throw new TlaProofError("Unexpected keyword INSTANCE");
|
|
1000
998
|
case "Define":
|
|
1001
|
-
return
|
|
999
|
+
return {
|
|
1002
1000
|
kind: "Define",
|
|
1003
1001
|
definitions: body.definitions.map((definition) => convertDefinition(definition)),
|
|
1004
|
-
|
|
1002
|
+
location: head,
|
|
1003
|
+
step: number,
|
|
1004
|
+
};
|
|
1005
1005
|
case "Have":
|
|
1006
1006
|
return withStepMethod({
|
|
1007
1007
|
kind: "Have",
|
|
@@ -1128,7 +1128,6 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
|
|
|
1128
1128
|
return {
|
|
1129
1129
|
kind: "Mutate",
|
|
1130
1130
|
mode: unit.use.kind,
|
|
1131
|
-
isSuppressed: false,
|
|
1132
1131
|
usable: convertUsable(unit.use),
|
|
1133
1132
|
location: getUseLocus(unit.use),
|
|
1134
1133
|
};
|
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
adjoin,
|
|
6
|
+
adjoinAll,
|
|
6
7
|
applyBuiltin,
|
|
7
8
|
at,
|
|
8
9
|
getFresh,
|
|
10
|
+
getLocalDefn,
|
|
9
11
|
getScopeAt,
|
|
10
12
|
mapChildren,
|
|
11
13
|
type Builtin,
|
|
@@ -22,6 +24,19 @@ const isApplied = (expression: Expr, builtins: Builtin[]) =>
|
|
|
22
24
|
|
|
23
25
|
const unchanged = (expression: Expr) => applyBuiltin("UNCHANGED", [expression], expression);
|
|
24
26
|
|
|
27
|
+
const getEquality = (expression: Expr) =>
|
|
28
|
+
applyBuiltin("Eq", [applyBuiltin("Prime", [expression], expression), expression], expression);
|
|
29
|
+
|
|
30
|
+
// E_tla_norm.rewrite_unch: UNCHANGED <<x, <<y>>>> as x' = x /\ y' = y.
|
|
31
|
+
export const rewriteUnchanged = (expression: Expr): Expr => {
|
|
32
|
+
const flatten = (inner: Expr): Expr[] =>
|
|
33
|
+
inner.kind === "Tuple" ? inner.elements.flatMap(flatten) : [inner];
|
|
34
|
+
const items = flatten(expression);
|
|
35
|
+
if (items.length === 0) return at({ kind: "Internal", builtin: "TRUE" }, expression);
|
|
36
|
+
if (items.length === 1) return getEquality(items[0]);
|
|
37
|
+
return at({ kind: "List", bullet: "And", items: items.map(getEquality) }, expression);
|
|
38
|
+
};
|
|
39
|
+
|
|
25
40
|
// Each visitor rewrites the outermost match and leaves the rest of the tree as it is.
|
|
26
41
|
const rewrite =
|
|
27
42
|
(match: (expression: Expr, recurse: (inner: Expr) => Expr) => Expr | undefined) =>
|
|
@@ -90,18 +105,12 @@ export const expandAction = rewrite((expression, recurse) => {
|
|
|
90
105
|
return applyBuiltin(expression.modal === "Box" ? "BoxTrue" : "Diamond", [step], expression);
|
|
91
106
|
});
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
applyBuiltin("Eq", [applyBuiltin("Prime", [element], element), element], element),
|
|
100
|
-
);
|
|
101
|
-
if (equalities.length === 0) return at({ kind: "Internal", builtin: "TRUE" }, expression);
|
|
102
|
-
if (equalities.length === 1) return equalities[0];
|
|
103
|
-
return at({ kind: "List", bullet: "And", items: equalities }, expression);
|
|
104
|
-
});
|
|
108
|
+
// Only getTime uses this, so the locations rewriteUnchanged gives do not matter.
|
|
109
|
+
const expandUnchanged = rewrite((expression) =>
|
|
110
|
+
expression.kind === "Apply" && isApplied(expression, ["UNCHANGED"])
|
|
111
|
+
? rewriteUnchanged(expression.args[0])
|
|
112
|
+
: undefined,
|
|
113
|
+
);
|
|
105
114
|
|
|
106
115
|
const TEMPORAL_BUILTINS: Builtin[] = [
|
|
107
116
|
"Prime",
|
|
@@ -131,10 +140,10 @@ const isConstantHyp = (scope: Scope | undefined, hyp: Hyp) => {
|
|
|
131
140
|
}
|
|
132
141
|
};
|
|
133
142
|
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
(inner, name) => adjoin(inner, getFresh(name, "Constant")),
|
|
143
|
+
const bindConstants = (scope: Scope | undefined, names: string[]) =>
|
|
144
|
+
adjoinAll(
|
|
137
145
|
scope,
|
|
146
|
+
names.map((name) => getFresh(name, "Constant")),
|
|
138
147
|
);
|
|
139
148
|
|
|
140
149
|
export const isConstant = (scope: Scope | undefined, expression: Expr): boolean => {
|
|
@@ -167,7 +176,7 @@ export const isConstant = (scope: Scope | undefined, expression: Expr): boolean
|
|
|
167
176
|
return false;
|
|
168
177
|
case "Lambda":
|
|
169
178
|
return isConstant(
|
|
170
|
-
|
|
179
|
+
bindConstants(
|
|
171
180
|
scope,
|
|
172
181
|
expression.params.map((param) => param.name),
|
|
173
182
|
),
|
|
@@ -196,13 +205,7 @@ export const isConstant = (scope: Scope | undefined, expression: Expr): boolean
|
|
|
196
205
|
case "Let": {
|
|
197
206
|
const inner = expression.definitions.reduce<{ scope: Scope | undefined; isGood: boolean }>(
|
|
198
207
|
(state, definition) => ({
|
|
199
|
-
scope: adjoin(state.scope,
|
|
200
|
-
kind: "Defn",
|
|
201
|
-
definition,
|
|
202
|
-
whereDefined: "User",
|
|
203
|
-
visibility: "Visible",
|
|
204
|
-
isLocal: true,
|
|
205
|
-
}),
|
|
208
|
+
scope: adjoin(state.scope, getLocalDefn(definition)),
|
|
206
209
|
isGood:
|
|
207
210
|
state.isGood &&
|
|
208
211
|
definition.kind === "Operator" &&
|
|
@@ -221,7 +224,7 @@ export const isConstant = (scope: Scope | undefined, expression: Expr): boolean
|
|
|
221
224
|
(bound) => bound.domain.kind !== "Domain" || recurse(bound.domain.expression),
|
|
222
225
|
) &&
|
|
223
226
|
isConstant(
|
|
224
|
-
|
|
227
|
+
bindConstants(
|
|
225
228
|
scope,
|
|
226
229
|
expression.bounds.map((bound) => bound.name),
|
|
227
230
|
),
|
|
@@ -233,7 +236,7 @@ export const isConstant = (scope: Scope | undefined, expression: Expr): boolean
|
|
|
233
236
|
case "SetSt":
|
|
234
237
|
return (
|
|
235
238
|
(expression.domain === undefined || recurse(expression.domain)) &&
|
|
236
|
-
isConstant(
|
|
239
|
+
isConstant(bindConstants(scope, [expression.name]), expression.body)
|
|
237
240
|
);
|
|
238
241
|
case "If":
|
|
239
242
|
return (
|
|
@@ -281,8 +284,7 @@ const isClosed = (scope: Scope | undefined, expression: Expr, isBox: boolean): b
|
|
|
281
284
|
if (expression.kind === "List")
|
|
282
285
|
return expression.items.every((item) => isClosed(scope, item, isBox));
|
|
283
286
|
if (expression.kind === "Sequent") {
|
|
284
|
-
|
|
285
|
-
return isClosed(inner, expression.sequent.active, isBox);
|
|
287
|
+
return isClosed(adjoinAll(scope, expression.sequent.context), expression.sequent.active, isBox);
|
|
286
288
|
}
|
|
287
289
|
if (expression.kind !== "Apply") return false;
|
|
288
290
|
const args = expression.args;
|
|
@@ -63,74 +63,58 @@ export const expandQuant = (
|
|
|
63
63
|
else groups.push([bound]);
|
|
64
64
|
});
|
|
65
65
|
return groups.reduceRight((inner: Expr, group): Expr => {
|
|
66
|
+
const quantify = (quantified: Bound[], quantifiedBody = inner): Expr => ({
|
|
67
|
+
kind: "Quant",
|
|
68
|
+
quantifier,
|
|
69
|
+
bounds: quantified,
|
|
70
|
+
body: quantifiedBody,
|
|
71
|
+
location: inner.location,
|
|
72
|
+
});
|
|
66
73
|
const first = group[0];
|
|
67
74
|
if (first.kind === "Name") {
|
|
68
|
-
return
|
|
69
|
-
kind
|
|
70
|
-
|
|
71
|
-
bounds: group.flatMap((bound) => (bound.kind === "Name" ? [getNamedBound(bound)] : [])),
|
|
72
|
-
body: inner,
|
|
73
|
-
location: inner.location,
|
|
74
|
-
};
|
|
75
|
+
return quantify(
|
|
76
|
+
group.flatMap((bound) => (bound.kind === "Name" ? [getNamedBound(bound)] : [])),
|
|
77
|
+
);
|
|
75
78
|
}
|
|
76
79
|
const factors = getFactors(first.domain, first.names);
|
|
77
|
-
if (factors)
|
|
78
|
-
return {
|
|
79
|
-
kind: "Quant",
|
|
80
|
-
quantifier,
|
|
81
|
-
bounds: getFactorBounds(first.names, factors),
|
|
82
|
-
body: inner,
|
|
83
|
-
location: inner.location,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
80
|
+
if (factors) return quantify(getFactorBounds(first.names, factors));
|
|
86
81
|
const membership: Expr = {
|
|
87
82
|
kind: "Apply",
|
|
88
83
|
operator: { kind: "Internal", builtin: quantifier === "Exists" ? "Mem" : "Notmem" },
|
|
89
84
|
args: [getNamesTuple(first.names), first.domain],
|
|
90
85
|
};
|
|
91
|
-
return {
|
|
92
|
-
kind: "
|
|
93
|
-
quantifier,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
kind: "List",
|
|
97
|
-
bullet: quantifier === "Exists" ? "And" : "Or",
|
|
98
|
-
items: [membership, inner],
|
|
99
|
-
},
|
|
100
|
-
location: inner.location,
|
|
101
|
-
};
|
|
86
|
+
return quantify(getUnbounded(first.names), {
|
|
87
|
+
kind: "List",
|
|
88
|
+
bullet: quantifier === "Exists" ? "And" : "Or",
|
|
89
|
+
items: [membership, inner],
|
|
90
|
+
});
|
|
102
91
|
}, body);
|
|
103
92
|
};
|
|
104
93
|
|
|
105
94
|
// CHOOSE <<x, y>> \in A \X B: P binds t \in A \X B with x == t[1], y == t[2], and any other
|
|
106
|
-
// domain binds t with \E x, y: t = <<x, y>> /\ P.
|
|
107
|
-
const
|
|
108
|
-
|
|
95
|
+
// domain binds t with \E x, y: t = <<x, y>> /\ P. {<<x, y>> \in S: P} binds t the same way.
|
|
96
|
+
export const expandChoose = (
|
|
97
|
+
binder: { kind: "Choose"; domain: Expr | undefined } | { kind: "SetSt"; domain: Expr },
|
|
109
98
|
names: string[],
|
|
110
|
-
domain: Expr | undefined,
|
|
111
99
|
body: Expr,
|
|
112
|
-
): Expr =>
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
kind: "
|
|
121
|
-
|
|
122
|
-
|
|
100
|
+
): Expr => {
|
|
101
|
+
const name = `${binder.kind.toLowerCase()}#${names[0]}`;
|
|
102
|
+
return {
|
|
103
|
+
...binder,
|
|
104
|
+
name,
|
|
105
|
+
body: getFactors(binder.domain, names)
|
|
106
|
+
? { kind: "Let", definitions: getItemDefinitions(name, names), body }
|
|
107
|
+
: {
|
|
108
|
+
kind: "Quant",
|
|
109
|
+
quantifier: "Exists",
|
|
110
|
+
bounds: getUnbounded(names),
|
|
111
|
+
body: {
|
|
112
|
+
kind: "List",
|
|
113
|
+
bullet: "And",
|
|
114
|
+
items: [getEquality(getOpaque(name), getNamesTuple(names)), body],
|
|
115
|
+
},
|
|
123
116
|
},
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
export const expandChoose = (names: string[], domain: Expr | undefined, body: Expr): Expr => {
|
|
127
|
-
const name = `choose#${names[0]}`;
|
|
128
|
-
return { kind: "Choose", name, domain, body: getChooseBody(name, names, domain, body) };
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
export const expandSetSt = (names: string[], domain: Expr, body: Expr): Expr => {
|
|
132
|
-
const name = `setst#${names[0]}`;
|
|
133
|
-
return { kind: "SetSt", name, domain, body: getChooseBody(name, names, domain, body) };
|
|
117
|
+
};
|
|
134
118
|
};
|
|
135
119
|
|
|
136
120
|
// A set or function constructor binds each tuple as fcn#x, with its names defined in a LET around
|
|
@@ -157,8 +141,8 @@ const expandConstructorBounds = (bounds: TuplyBound[], value: Expr, isSetOf: boo
|
|
|
157
141
|
kind: "Operator",
|
|
158
142
|
name: choice,
|
|
159
143
|
body: expandChoose(
|
|
144
|
+
{ kind: "Choose", domain: undefined },
|
|
160
145
|
bound.names,
|
|
161
|
-
undefined,
|
|
162
146
|
getEquality(getOpaque(name), getNamesTuple(bound.names)),
|
|
163
147
|
),
|
|
164
148
|
},
|