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,236 @@
|
|
|
1
|
+
// tlapm's fingerprints file, ported from src/backend/fpfile.ml.
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
cpSync,
|
|
5
|
+
existsSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
rmSync,
|
|
11
|
+
statSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import { basename, join } from "node:path";
|
|
15
|
+
import { version } from "../package.json";
|
|
16
|
+
import { TlaProofError } from "./errors";
|
|
17
|
+
import { block, isBlock, readMarshaled, writeMarshaled, type OcamlValue } from "./ocaml-marshal";
|
|
18
|
+
import type { ProofAttempt } from "./prove/prove";
|
|
19
|
+
|
|
20
|
+
// One optional result per prover, in V13.str's order.
|
|
21
|
+
export type FingerprintTable = Map<string, OcamlValue[]>;
|
|
22
|
+
|
|
23
|
+
const MAGIC_NUMBER = 20210223;
|
|
24
|
+
|
|
25
|
+
const RECORD_SIZE = 23;
|
|
26
|
+
|
|
27
|
+
// Each prover's field in V13.str and constructor in V13.meth.
|
|
28
|
+
const PROVERS: Record<string, { field: number; tag: number }> = {
|
|
29
|
+
zenon: { field: 1, tag: 1 },
|
|
30
|
+
isabelle: { field: 2, tag: 0 },
|
|
31
|
+
smt: { field: 10, tag: 8 },
|
|
32
|
+
z3: { field: 11, tag: 9 },
|
|
33
|
+
ls4: { field: 17, tag: 16 },
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const FAIL_METHOD = 5;
|
|
37
|
+
|
|
38
|
+
const REASONS = ["false", "timeout"];
|
|
39
|
+
|
|
40
|
+
// Stored as the tlapm, Zenon and Isabelle versions, which --safefp compares.
|
|
41
|
+
const VERSION = `tla-ts ${version}`;
|
|
42
|
+
|
|
43
|
+
const CHECKED_VERSIONS = new Map([
|
|
44
|
+
[PROVERS.zenon.field, 3],
|
|
45
|
+
[PROVERS.isabelle.field, 4],
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
const HISTORY_SIZE = 10;
|
|
49
|
+
|
|
50
|
+
const getField = (value: OcamlValue | undefined, index: number) =>
|
|
51
|
+
value !== undefined && isBlock(value) ? value.fields[index] : undefined;
|
|
52
|
+
|
|
53
|
+
const getFloat = (value: OcamlValue | undefined) =>
|
|
54
|
+
value !== undefined && typeof value === "object" && "float" in value ? value.float : Infinity;
|
|
55
|
+
|
|
56
|
+
// Hashtbl.hash on a string.
|
|
57
|
+
const hashKey = (key: string) => {
|
|
58
|
+
const bytes = new TextEncoder().encode(key);
|
|
59
|
+
const rotate = (value: number, shift: number) => (value << shift) | (value >>> (32 - shift));
|
|
60
|
+
const mixed = Array.from(
|
|
61
|
+
{ length: Math.ceil(bytes.length / 4) },
|
|
62
|
+
(_, index) =>
|
|
63
|
+
bytes[index * 4] |
|
|
64
|
+
(bytes[index * 4 + 1] << 8) |
|
|
65
|
+
(bytes[index * 4 + 2] << 16) |
|
|
66
|
+
(bytes[index * 4 + 3] << 24),
|
|
67
|
+
).reduce((hash, word) => {
|
|
68
|
+
const data = Math.imul(rotate(Math.imul(word, 0xcc9e2d51), 15), 0x1b873593);
|
|
69
|
+
return (Math.imul(rotate(hash ^ data, 13), 5) + 0xe6546b64) | 0;
|
|
70
|
+
}, 0);
|
|
71
|
+
const first = Math.imul(mixed ^ bytes.length ^ ((mixed ^ bytes.length) >>> 16), 0x85ebca6b);
|
|
72
|
+
const second = Math.imul(first ^ (first >>> 13), 0xc2b2ae35);
|
|
73
|
+
return (second ^ (second >>> 16)) & 0x3fffffff;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const readBucket = (entry: OcamlValue | undefined): [OcamlValue, OcamlValue][] =>
|
|
77
|
+
entry !== undefined && isBlock(entry)
|
|
78
|
+
? [[entry.fields[0], entry.fields[1]], ...readBucket(entry.fields[2])]
|
|
79
|
+
: [];
|
|
80
|
+
|
|
81
|
+
// Fpfile.load_fingerprints. Each bucket lists the latest fingerprint first.
|
|
82
|
+
export const loadFingerprints = (table: FingerprintTable, path: string, isSafe: boolean) => {
|
|
83
|
+
const bytes = readFileSync(path);
|
|
84
|
+
const magic = readMarshaled(bytes, 0);
|
|
85
|
+
const fp = readMarshaled(bytes, magic.next).value;
|
|
86
|
+
if (magic.value !== MAGIC_NUMBER || !isBlock(fp) || fp.tag !== 0)
|
|
87
|
+
throw new TlaProofError("corrupted fingerprint file");
|
|
88
|
+
const buckets = getField(getField(fp, 0), 1);
|
|
89
|
+
const isTrusted = (sti: OcamlValue, field: number) => {
|
|
90
|
+
const index = CHECKED_VERSIONS.get(field);
|
|
91
|
+
return !isSafe || index === undefined || getField(getField(sti, 0), index) === VERSION;
|
|
92
|
+
};
|
|
93
|
+
table.clear();
|
|
94
|
+
(buckets !== undefined && isBlock(buckets) ? buckets.fields : [])
|
|
95
|
+
.flatMap((bucket) => readBucket(bucket).toReversed())
|
|
96
|
+
.forEach(([key, record]) => {
|
|
97
|
+
if (typeof key !== "string" || !isBlock(record))
|
|
98
|
+
throw new TlaProofError("corrupted fingerprint file");
|
|
99
|
+
table.set(
|
|
100
|
+
key,
|
|
101
|
+
record.fields.map((sti, field) => (isTrusted(sti, field) ? sti : 0)),
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const decodeResult = (prover: string, sti: OcamlValue | undefined): ProofAttempt | undefined => {
|
|
107
|
+
const status = getField(sti, 0);
|
|
108
|
+
const outcome = getField(status, 0);
|
|
109
|
+
const argument = getField(getField(status, 1), 0);
|
|
110
|
+
if (outcome === undefined || outcome === 1) return undefined;
|
|
111
|
+
const seconds = getFloat(getField(prover === "isabelle" ? getField(argument, 0) : argument, 0));
|
|
112
|
+
const tactic = prover === "isabelle" ? getField(argument, 1) : undefined;
|
|
113
|
+
const reason = getField(getField(outcome, 0), 0);
|
|
114
|
+
return {
|
|
115
|
+
method: {
|
|
116
|
+
prover,
|
|
117
|
+
timeLimitMs: seconds * 1000,
|
|
118
|
+
...(typeof tactic === "string" && { tactic }),
|
|
119
|
+
},
|
|
120
|
+
isProved: outcome === 0,
|
|
121
|
+
isReused: false,
|
|
122
|
+
...(reason !== undefined && {
|
|
123
|
+
reason: typeof reason === "number" ? REASONS[reason] : String(getField(reason, 0)),
|
|
124
|
+
}),
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// Fpfile.fp_writes, whose date counts months from 0.
|
|
129
|
+
const encodeResult = (attempt: ProofAttempt) => {
|
|
130
|
+
const now = new Date();
|
|
131
|
+
const method = attempt.method;
|
|
132
|
+
const timeout = { float: method.rlimit ?? method.timeLimitMs / 1000 };
|
|
133
|
+
const argument =
|
|
134
|
+
method.prover === "zenon"
|
|
135
|
+
? block(0, timeout, FAIL_METHOD)
|
|
136
|
+
: method.prover === "isabelle"
|
|
137
|
+
? block(0, block(0, timeout), method.tactic ?? "auto")
|
|
138
|
+
: block(0, timeout);
|
|
139
|
+
const reasonIndex = REASONS.indexOf(attempt.reason ?? "");
|
|
140
|
+
const reason =
|
|
141
|
+
attempt.reason === undefined
|
|
142
|
+
? 0
|
|
143
|
+
: block(0, reasonIndex === -1 ? block(0, attempt.reason) : reasonIndex);
|
|
144
|
+
return block(
|
|
145
|
+
0,
|
|
146
|
+
block(
|
|
147
|
+
0,
|
|
148
|
+
block(
|
|
149
|
+
0,
|
|
150
|
+
attempt.isProved ? 0 : block(0, reason),
|
|
151
|
+
block(PROVERS[method.prover].tag, argument),
|
|
152
|
+
),
|
|
153
|
+
block(
|
|
154
|
+
0,
|
|
155
|
+
now.getFullYear(),
|
|
156
|
+
now.getMonth(),
|
|
157
|
+
now.getDate(),
|
|
158
|
+
now.getHours(),
|
|
159
|
+
now.getMinutes(),
|
|
160
|
+
now.getSeconds(),
|
|
161
|
+
),
|
|
162
|
+
VERSION,
|
|
163
|
+
VERSION,
|
|
164
|
+
VERSION,
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const encodeTable = (table: FingerprintTable) => {
|
|
170
|
+
const length = 2 ** Math.max(9, Math.ceil(Math.log2(table.size / 2)));
|
|
171
|
+
const buckets: OcamlValue[] = Array.from({ length }, () => 0);
|
|
172
|
+
table.forEach((record, fingerprint) => {
|
|
173
|
+
const index = hashKey(fingerprint) & (length - 1);
|
|
174
|
+
buckets[index] = block(0, fingerprint, block(0, ...record), buckets[index]);
|
|
175
|
+
});
|
|
176
|
+
return block(0, block(0, table.size, block(0, ...buckets), 0, 512));
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const pad = (value: number) => String(value).padStart(2, "0");
|
|
180
|
+
|
|
181
|
+
// Fpfile.fp_init.
|
|
182
|
+
export const saveHistory = (path: string, sources: string[]) => {
|
|
183
|
+
const now = new Date();
|
|
184
|
+
const history = `${path}.history`;
|
|
185
|
+
const directory = join(
|
|
186
|
+
history,
|
|
187
|
+
`${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}_${pad(now.getHours())}_${pad(now.getMinutes())}_${pad(now.getSeconds())}`,
|
|
188
|
+
);
|
|
189
|
+
mkdirSync(directory, { recursive: true });
|
|
190
|
+
[path, ...sources]
|
|
191
|
+
.filter((file) => existsSync(file))
|
|
192
|
+
.forEach((file) => cpSync(file, join(directory, basename(file)), { preserveTimestamps: true }));
|
|
193
|
+
readdirSync(history)
|
|
194
|
+
.map((name) => join(history, name))
|
|
195
|
+
.sort((left, right) => statSync(right).mtimeMs - statSync(left).mtimeMs)
|
|
196
|
+
.slice(HISTORY_SIZE)
|
|
197
|
+
.forEach((old) => rmSync(old, { recursive: true, force: true }));
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// Fpfile.query: what each prover concluded for each fingerprint. Saving them, as fp_writes,
|
|
201
|
+
// add_to_table and fp_close_and_consolidate do, keeps each record and result loaded unchanged, with
|
|
202
|
+
// its date, versions and other provers' results.
|
|
203
|
+
export const getKnownResults = (table: FingerprintTable) => {
|
|
204
|
+
const storedResults = new Map<ProofAttempt, OcamlValue>();
|
|
205
|
+
const storedRecords = new Map<Map<string, ProofAttempt>, OcamlValue[]>();
|
|
206
|
+
const results = new Map(
|
|
207
|
+
[...table].map(([fingerprint, record]) => {
|
|
208
|
+
const attempts = new Map(
|
|
209
|
+
Object.entries(PROVERS).flatMap(([prover, known]) => {
|
|
210
|
+
const result = decodeResult(prover, getField(record[known.field], 0));
|
|
211
|
+
if (!result) return [];
|
|
212
|
+
storedResults.set(result, record[known.field]);
|
|
213
|
+
return [[prover, result]];
|
|
214
|
+
}),
|
|
215
|
+
);
|
|
216
|
+
storedRecords.set(attempts, record);
|
|
217
|
+
return [fingerprint, attempts];
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
const save = (path: string) => {
|
|
221
|
+
table.clear();
|
|
222
|
+
results.forEach((attempts, fingerprint) => {
|
|
223
|
+
const record = [...(storedRecords.get(attempts) ?? Array<OcamlValue>(RECORD_SIZE).fill(0))];
|
|
224
|
+
attempts.forEach((attempt, prover) => {
|
|
225
|
+
record[PROVERS[prover].field] = storedResults.get(attempt) ?? encodeResult(attempt);
|
|
226
|
+
});
|
|
227
|
+
table.set(fingerprint, record);
|
|
228
|
+
});
|
|
229
|
+
writeFileSync(
|
|
230
|
+
`${path}.tmp`,
|
|
231
|
+
Buffer.concat([writeMarshaled(MAGIC_NUMBER), writeMarshaled(encodeTable(table))]),
|
|
232
|
+
);
|
|
233
|
+
renameSync(`${path}.tmp`, path);
|
|
234
|
+
};
|
|
235
|
+
return { results, save };
|
|
236
|
+
};
|