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
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// OCaml's Marshal format, ported from runtime/intern.c and runtime/extern.c, for the values in
|
|
2
|
+
// tlapm's fingerprints file. It writes no sharing, which OCaml reads the same.
|
|
3
|
+
|
|
4
|
+
import { TlaProofError } from "./errors";
|
|
5
|
+
|
|
6
|
+
interface OcamlBlock {
|
|
7
|
+
tag: number;
|
|
8
|
+
fields: OcamlValue[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface OcamlFloat {
|
|
12
|
+
float: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type OcamlValue = number | string | OcamlBlock | OcamlFloat;
|
|
16
|
+
|
|
17
|
+
const MAGIC_NUMBER_SMALL = 0x8495a6be;
|
|
18
|
+
|
|
19
|
+
const CODE = {
|
|
20
|
+
int8: 0x0,
|
|
21
|
+
int16: 0x1,
|
|
22
|
+
int32: 0x2,
|
|
23
|
+
shared8: 0x4,
|
|
24
|
+
shared16: 0x5,
|
|
25
|
+
shared32: 0x6,
|
|
26
|
+
block32: 0x8,
|
|
27
|
+
string8: 0x9,
|
|
28
|
+
string32: 0xa,
|
|
29
|
+
doubleLittle: 0xc,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const PREFIX = { smallInt: 0x40, smallString: 0x20, smallBlock: 0x80 };
|
|
33
|
+
|
|
34
|
+
const HEADER_SIZE = 20;
|
|
35
|
+
|
|
36
|
+
export const block = (tag: number, ...fields: OcamlValue[]): OcamlBlock => ({ tag, fields });
|
|
37
|
+
|
|
38
|
+
export const isBlock = (value: OcamlValue): value is OcamlBlock =>
|
|
39
|
+
typeof value === "object" && "tag" in value;
|
|
40
|
+
|
|
41
|
+
// Returns the value marshaled at offset, and where the next one starts.
|
|
42
|
+
export const readMarshaled = (bytes: Uint8Array, offset: number) => {
|
|
43
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
44
|
+
if (bytes.length < offset + HEADER_SIZE || view.getUint32(offset) !== MAGIC_NUMBER_SMALL)
|
|
45
|
+
throw new TlaProofError("corrupted fingerprint file");
|
|
46
|
+
const end = offset + HEADER_SIZE + view.getUint32(offset + 4);
|
|
47
|
+
const objects: OcamlValue[] = [];
|
|
48
|
+
const state = { position: offset + HEADER_SIZE };
|
|
49
|
+
const take = (length: number) => {
|
|
50
|
+
const start = state.position;
|
|
51
|
+
state.position += length;
|
|
52
|
+
if (state.position > end) throw new TlaProofError("corrupted fingerprint file");
|
|
53
|
+
return start;
|
|
54
|
+
};
|
|
55
|
+
const decoder = new TextDecoder();
|
|
56
|
+
const readString = (length: number) => {
|
|
57
|
+
const start = take(length);
|
|
58
|
+
const value = decoder.decode(bytes.subarray(start, start + length));
|
|
59
|
+
objects.push(value);
|
|
60
|
+
return value;
|
|
61
|
+
};
|
|
62
|
+
const readBlock = (tag: number, size: number): OcamlBlock => {
|
|
63
|
+
const value = block(tag);
|
|
64
|
+
if (size > 0) objects.push(value);
|
|
65
|
+
for (let index = 0; index < size; index++) value.fields.push(read());
|
|
66
|
+
return value;
|
|
67
|
+
};
|
|
68
|
+
const readShared = (distance: number) => objects[objects.length - distance];
|
|
69
|
+
const read = (): OcamlValue => {
|
|
70
|
+
const code = bytes[take(1)];
|
|
71
|
+
if (code >= PREFIX.smallBlock) return readBlock(code & 0xf, (code >> 4) & 0x7);
|
|
72
|
+
if (code >= PREFIX.smallInt) return code & 0x3f;
|
|
73
|
+
if (code >= PREFIX.smallString) return readString(code & 0x1f);
|
|
74
|
+
switch (code) {
|
|
75
|
+
case CODE.int8:
|
|
76
|
+
return view.getInt8(take(1));
|
|
77
|
+
case CODE.int16:
|
|
78
|
+
return view.getInt16(take(2));
|
|
79
|
+
case CODE.int32:
|
|
80
|
+
return view.getInt32(take(4));
|
|
81
|
+
case CODE.shared8:
|
|
82
|
+
return readShared(view.getUint8(take(1)));
|
|
83
|
+
case CODE.shared16:
|
|
84
|
+
return readShared(view.getUint16(take(2)));
|
|
85
|
+
case CODE.shared32:
|
|
86
|
+
return readShared(view.getUint32(take(4)));
|
|
87
|
+
case CODE.block32: {
|
|
88
|
+
const header = view.getUint32(take(4));
|
|
89
|
+
return readBlock(header & 0xff, header >>> 10);
|
|
90
|
+
}
|
|
91
|
+
case CODE.string8:
|
|
92
|
+
return readString(view.getUint8(take(1)));
|
|
93
|
+
case CODE.string32:
|
|
94
|
+
return readString(view.getUint32(take(4)));
|
|
95
|
+
case CODE.doubleLittle: {
|
|
96
|
+
const value = { float: view.getFloat64(take(8), true) };
|
|
97
|
+
objects.push(value);
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
throw new TlaProofError("corrupted fingerprint file");
|
|
102
|
+
};
|
|
103
|
+
return { value: read(), next: end };
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const writeMarshaled = (value: OcamlValue) => {
|
|
107
|
+
const bytes: number[] = [];
|
|
108
|
+
const sizes = { objects: 0, words32: 0, words64: 0 };
|
|
109
|
+
const pushInteger = (integer: number, length: number) => {
|
|
110
|
+
for (let shift = (length - 1) * 8; shift >= 0; shift -= 8)
|
|
111
|
+
bytes.push((integer >>> shift) & 0xff);
|
|
112
|
+
};
|
|
113
|
+
const encoder = new TextEncoder();
|
|
114
|
+
const write = (inner: OcamlValue) => {
|
|
115
|
+
if (typeof inner === "number") {
|
|
116
|
+
if (inner >= 0 && inner < 0x40) return bytes.push(PREFIX.smallInt + inner);
|
|
117
|
+
if (inner >= -0x80 && inner < 0x80) return bytes.push(CODE.int8, inner & 0xff);
|
|
118
|
+
if (inner >= -0x8000 && inner < 0x8000) {
|
|
119
|
+
bytes.push(CODE.int16);
|
|
120
|
+
return pushInteger(inner, 2);
|
|
121
|
+
}
|
|
122
|
+
bytes.push(CODE.int32);
|
|
123
|
+
return pushInteger(inner, 4);
|
|
124
|
+
}
|
|
125
|
+
sizes.objects++;
|
|
126
|
+
if (typeof inner === "string") {
|
|
127
|
+
const encoded = encoder.encode(inner);
|
|
128
|
+
if (encoded.length < 0x20) bytes.push(PREFIX.smallString + encoded.length);
|
|
129
|
+
else if (encoded.length < 0x100) bytes.push(CODE.string8, encoded.length);
|
|
130
|
+
else {
|
|
131
|
+
bytes.push(CODE.string32);
|
|
132
|
+
pushInteger(encoded.length, 4);
|
|
133
|
+
}
|
|
134
|
+
bytes.push(...encoded);
|
|
135
|
+
sizes.words32 += 1 + Math.floor((encoded.length + 4) / 4);
|
|
136
|
+
sizes.words64 += 1 + Math.floor((encoded.length + 8) / 8);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (!isBlock(inner)) {
|
|
140
|
+
const float = new DataView(new ArrayBuffer(8));
|
|
141
|
+
float.setFloat64(0, inner.float, true);
|
|
142
|
+
bytes.push(CODE.doubleLittle, ...new Uint8Array(float.buffer));
|
|
143
|
+
sizes.words32 += 3;
|
|
144
|
+
sizes.words64 += 2;
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const size = inner.fields.length;
|
|
148
|
+
if (size === 0) sizes.objects--;
|
|
149
|
+
else {
|
|
150
|
+
sizes.words32 += 1 + size;
|
|
151
|
+
sizes.words64 += 1 + size;
|
|
152
|
+
}
|
|
153
|
+
if (inner.tag < 16 && size < 8) bytes.push(PREFIX.smallBlock + inner.tag + (size << 4));
|
|
154
|
+
else {
|
|
155
|
+
bytes.push(CODE.block32);
|
|
156
|
+
pushInteger(size * 1024 + inner.tag, 4);
|
|
157
|
+
}
|
|
158
|
+
inner.fields.forEach(write);
|
|
159
|
+
};
|
|
160
|
+
write(value);
|
|
161
|
+
const header = new DataView(new ArrayBuffer(HEADER_SIZE));
|
|
162
|
+
[MAGIC_NUMBER_SMALL, bytes.length, sizes.objects, sizes.words32, sizes.words64].forEach(
|
|
163
|
+
(field, index) => header.setUint32(index * 4, field),
|
|
164
|
+
);
|
|
165
|
+
return new Uint8Array([...new Uint8Array(header.buffer), ...bytes]);
|
|
166
|
+
};
|
package/src/prove/decide-pool.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Worker threads that decide the obligations prove sends them, the earliest obligation first.
|
|
2
2
|
|
|
3
|
-
import { TlaProverError
|
|
3
|
+
import { TlaProverError } from "../errors";
|
|
4
4
|
import { spawnWorker, type WorkerHandle } from "../worker";
|
|
5
|
-
import type { DecideOptions,
|
|
5
|
+
import type { DecideOptions, Decision } from "./verdict";
|
|
6
6
|
|
|
7
7
|
export interface DecideRequest {
|
|
8
8
|
prover: string;
|
|
@@ -10,15 +10,14 @@ export interface DecideRequest {
|
|
|
10
10
|
timeLimitMs: number;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
// A backend's
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
| { error: { name: "TlaProverError" | "TlaUnsupportedError"; message: string } };
|
|
13
|
+
// A backend's decision, or the message of the TlaProverError it threw, which prove turns into a
|
|
14
|
+
// result.
|
|
15
|
+
export type DecideResponse = Decision | { error: string };
|
|
17
16
|
|
|
18
|
-
interface
|
|
17
|
+
interface PendingDecision {
|
|
19
18
|
request: DecideRequest;
|
|
20
19
|
priority: number;
|
|
21
|
-
resolve: (
|
|
20
|
+
resolve: (decision: Decision) => void;
|
|
22
21
|
reject: (error: unknown) => void;
|
|
23
22
|
}
|
|
24
23
|
|
|
@@ -28,15 +27,15 @@ export interface DecidePool {
|
|
|
28
27
|
text: string,
|
|
29
28
|
options: DecideOptions,
|
|
30
29
|
priority: number,
|
|
31
|
-
) => Promise<
|
|
30
|
+
) => Promise<Decision>;
|
|
32
31
|
// Rejects every decision not yet made with reason, and stops the workers.
|
|
33
32
|
terminate: (reason: unknown) => void;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
export const createDecidePool = async (count: number): Promise<DecidePool> => {
|
|
37
36
|
const workers: WorkerHandle[] = [];
|
|
38
|
-
const queue:
|
|
39
|
-
const running = new Map<number,
|
|
37
|
+
const queue: PendingDecision[] = [];
|
|
38
|
+
const running = new Map<number, PendingDecision>();
|
|
40
39
|
const idle: number[] = [];
|
|
41
40
|
const state: { failure: { reason: unknown } | undefined } = { failure: undefined };
|
|
42
41
|
const dispatch = () => {
|
|
@@ -56,12 +55,8 @@ export const createDecidePool = async (count: number): Promise<DecidePool> => {
|
|
|
56
55
|
const decision = running.get(index);
|
|
57
56
|
running.delete(index);
|
|
58
57
|
idle.push(index);
|
|
59
|
-
if ("verdict" in response) decision?.resolve(response
|
|
60
|
-
|
|
61
|
-
const ErrorClass =
|
|
62
|
-
response.error.name === "TlaProverError" ? TlaProverError : TlaUnsupportedError;
|
|
63
|
-
decision?.reject(new ErrorClass(response.error.message));
|
|
64
|
-
}
|
|
58
|
+
if ("verdict" in response) decision?.resolve(response);
|
|
59
|
+
if ("error" in response) decision?.reject(new TlaProverError(response.error));
|
|
65
60
|
dispatch();
|
|
66
61
|
};
|
|
67
62
|
const terminate = (reason: unknown) => {
|
package/src/prove/deciders.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { SmtParseError, TlaProverError } from "../errors";
|
|
|
5
5
|
import { decideIsabelle } from "./isabelle/decide";
|
|
6
6
|
import { decidePtl } from "./ptl/decide";
|
|
7
7
|
import { decideSmt } from "./smt/solver/solver";
|
|
8
|
-
import type { DecideOptions, Verdict } from "./verdict";
|
|
8
|
+
import type { DecideOptions, Decision, Verdict } from "./verdict";
|
|
9
9
|
import { decideZenon } from "./zenon/decide";
|
|
10
10
|
|
|
11
11
|
// Z3 rejects malformed input with an error, and tlapm counts that as a failure.
|
|
@@ -25,3 +25,9 @@ export const DECIDERS: Record<string, (text: string, options: DecideOptions) =>
|
|
|
25
25
|
isabelle: decideIsabelle,
|
|
26
26
|
ls4: decidePtl,
|
|
27
27
|
};
|
|
28
|
+
|
|
29
|
+
export const decide = (prover: string, text: string, options: DecideOptions): Decision => {
|
|
30
|
+
const start = performance.now();
|
|
31
|
+
const verdict = DECIDERS[prover](text, options);
|
|
32
|
+
return { verdict, timeUsedMs: performance.now() - start };
|
|
33
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// What backend/zenon.ml and backend/isabelle.ml share in printing an obligation that Backend.Prep
|
|
2
|
+
// has shipped.
|
|
3
|
+
|
|
4
|
+
import { TlapmBugError, TlaProverError, TlaUnsupportedError } from "../errors";
|
|
5
|
+
import { popNames, type Context } from "../obligations/ctx";
|
|
6
|
+
import { shiftExpr, type Bound, type Builtin, type Expr } from "../obligations/expression";
|
|
7
|
+
|
|
8
|
+
// The temporal operators neither backend has a counterpart for, by the name tlapm reports.
|
|
9
|
+
const TEMPORAL_OPERATORS: Partial<Record<Builtin, [string, number]>> = {
|
|
10
|
+
Leadsto: ["Leadsto", 2],
|
|
11
|
+
ENABLED: ["ENABLED", 1],
|
|
12
|
+
Cdot: ["\\cdot", 2],
|
|
13
|
+
Actplus: ["-+->", 2],
|
|
14
|
+
BoxTrue: ["[]", 1],
|
|
15
|
+
BoxFalse: ["[]", 1],
|
|
16
|
+
Diamond: ["<>", 1],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Why a backend can't print an application of builtin, which it reports as where.
|
|
20
|
+
export const getUnprintableError = (builtin: Builtin, arity: number, where: string) => {
|
|
21
|
+
const isFrontend = builtin === "Prime" || builtin === "StrongPrime" || builtin === "UNCHANGED";
|
|
22
|
+
if (isFrontend && arity === 1) return new TlapmBugError();
|
|
23
|
+
const temporal = TEMPORAL_OPERATORS[builtin];
|
|
24
|
+
return temporal && temporal[1] === arity
|
|
25
|
+
? new TlaProverError(`Unsupported operator \`${temporal[0]}\`.\n`)
|
|
26
|
+
: new TlaProverError(where);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
interface SplitQuantifier {
|
|
30
|
+
first: Bound;
|
|
31
|
+
inner: Expr;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// A quantifier's first bound binds around a quantifier over the others, which move under it. A
|
|
35
|
+
// ditto after the first bound copies its domain.
|
|
36
|
+
export const splitFirstBound = (
|
|
37
|
+
quantifier: "Forall" | "Exists",
|
|
38
|
+
bounds: Bound[],
|
|
39
|
+
body: Expr,
|
|
40
|
+
): SplitQuantifier => {
|
|
41
|
+
const first = bounds[0];
|
|
42
|
+
const rest = bounds.slice(1);
|
|
43
|
+
const undittoed =
|
|
44
|
+
rest[0]?.domain.kind === "Ditto"
|
|
45
|
+
? [{ ...rest[0], domain: first.domain }, ...rest.slice(1)]
|
|
46
|
+
: rest;
|
|
47
|
+
if (undittoed.length === 0) return { first, inner: body };
|
|
48
|
+
return {
|
|
49
|
+
first,
|
|
50
|
+
inner: {
|
|
51
|
+
kind: "Quant",
|
|
52
|
+
quantifier,
|
|
53
|
+
bounds: undittoed.map((other) =>
|
|
54
|
+
other.domain.kind === "Domain"
|
|
55
|
+
? {
|
|
56
|
+
...other,
|
|
57
|
+
domain: { kind: "Domain", expression: shiftExpr(1, other.domain.expression) },
|
|
58
|
+
}
|
|
59
|
+
: other,
|
|
60
|
+
),
|
|
61
|
+
body,
|
|
62
|
+
location: body.location,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// tlapm prints a fact to a formatter that drops its output first, to omit an unsupported one: the
|
|
68
|
+
// fact's print, or the error that omits it once the names it bound are out of scope.
|
|
69
|
+
export const printFact = <Printed>(context: Context, print: () => Printed) => {
|
|
70
|
+
const depth = context.stack.length;
|
|
71
|
+
try {
|
|
72
|
+
return print();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (!(error instanceof TlaUnsupportedError)) throw error;
|
|
75
|
+
popNames(context, context.stack.length - depth);
|
|
76
|
+
return error;
|
|
77
|
+
}
|
|
78
|
+
};
|