tla-ts 0.0.3-dev.33ae522 → 0.0.3-dev.3504663
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/dist/index.cjs +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +13 -13
- package/dist/run-cli.js +16 -16
- package/dist/transfer.cjs +24 -16
- package/dist/transfer.js +24 -16
- package/package.json +1 -1
- package/src/ast.ts +8 -1
- package/src/check.ts +5 -2
- package/src/environment.ts +38 -21
- package/src/errors.ts +14 -3
- package/src/evaluate.ts +3 -3
- package/src/fingerprints.ts +2 -1
- package/src/levels.ts +124 -16
- package/src/parser.ts +52 -11
- package/src/resolve.ts +339 -39
- package/src/run-cli.ts +3 -2
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/standard-modules.ts +2 -2
- package/src/subexpressions.ts +25 -13
- package/src/substitution-levels.ts +38 -17
- package/src/values.ts +70 -8
package/src/standard-modules.ts
CHANGED
|
@@ -135,8 +135,8 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
135
135
|
}),
|
|
136
136
|
...SET_OPERATORS.map((operator) =>
|
|
137
137
|
defineOperator(operator, 2, (args) => {
|
|
138
|
-
const left =
|
|
139
|
-
const right =
|
|
138
|
+
const left = getValue(args, 0, operator);
|
|
139
|
+
const right = getValue(args, 1, operator);
|
|
140
140
|
if (isReducible(left) && isReducible(right) && !isMixedSet(left) && !isMixedSet(right)) {
|
|
141
141
|
if (operator === "\\cup" && isEmptySet(left)) return right;
|
|
142
142
|
const merge =
|
package/src/subexpressions.ts
CHANGED
|
@@ -11,7 +11,13 @@ import {
|
|
|
11
11
|
} from "./ast";
|
|
12
12
|
import { TlaSyntaxError } from "./errors";
|
|
13
13
|
import { getSourceName, mapChildren, mapDefinition } from "./instances";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
getArity,
|
|
16
|
+
getBoundNames,
|
|
17
|
+
getOperandIndex,
|
|
18
|
+
getPatternNames,
|
|
19
|
+
LOCAL_RESOLUTION,
|
|
20
|
+
} from "./resolve";
|
|
15
21
|
|
|
16
22
|
type Lookup = (name: string) => Definition | undefined;
|
|
17
23
|
|
|
@@ -37,12 +43,6 @@ const getBoundSets = (bounds: Bound[]) =>
|
|
|
37
43
|
.filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
|
|
38
44
|
.map((bound) => bound.set);
|
|
39
45
|
|
|
40
|
-
const getOperandIndex = (operand: number | "<<" | ">>", count: number) => {
|
|
41
|
-
if (typeof operand === "number") return operand >= 1 && operand <= count ? operand - 1 : -1;
|
|
42
|
-
if (operand === "<<") return count > 0 ? 0 : -1;
|
|
43
|
-
return count === 2 ? 1 : -1;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
46
|
const getOperands = (expression: Expression): Expression[] | undefined => {
|
|
47
47
|
switch (expression.kind) {
|
|
48
48
|
case "Apply":
|
|
@@ -393,15 +393,22 @@ const containsReference = (expression: Expression): boolean =>
|
|
|
393
393
|
(expression.kind === "Apply" && expression.operator.includes("!")) ||
|
|
394
394
|
getChildren(expression).some(containsReference);
|
|
395
395
|
|
|
396
|
-
|
|
396
|
+
// A reference into a theorem whose statement is an ASSUME/PROVE stays for name resolution to check.
|
|
397
|
+
const resolveSubexpressions = (
|
|
398
|
+
expression: Expression,
|
|
399
|
+
lookup: Lookup,
|
|
400
|
+
goals: ReadonlySet<string>,
|
|
401
|
+
): Expression => {
|
|
397
402
|
if (!containsReference(expression)) return expression;
|
|
398
403
|
if (expression.kind === "Let") {
|
|
399
404
|
const local: Lookup = (name) =>
|
|
400
405
|
expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
|
|
401
|
-
return mapChildren(expression, (child) => resolveSubexpressions(child, local));
|
|
406
|
+
return mapChildren(expression, (child) => resolveSubexpressions(child, local, goals));
|
|
402
407
|
}
|
|
403
|
-
const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, lookup));
|
|
408
|
+
const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, lookup, goals));
|
|
404
409
|
if (mapped.kind === "Subexpression") {
|
|
410
|
+
const first = mapped.selectors[0];
|
|
411
|
+
if (first.kind === "Name" && goals.has(first.name)) return mapped;
|
|
405
412
|
return selectSubexpression(mapped.selectors, lookup, mapped.location, []);
|
|
406
413
|
}
|
|
407
414
|
const name =
|
|
@@ -425,7 +432,7 @@ export const resolveInstanceSubexpressions = (instance: Instance, module: Module
|
|
|
425
432
|
...instance,
|
|
426
433
|
substitutions: instance.substitutions.map((substitution) => ({
|
|
427
434
|
name: substitution.name,
|
|
428
|
-
expression: resolveSubexpressions(substitution.expression, lookup),
|
|
435
|
+
expression: resolveSubexpressions(substitution.expression, lookup, new Set()),
|
|
429
436
|
})),
|
|
430
437
|
};
|
|
431
438
|
};
|
|
@@ -436,14 +443,19 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
|
|
|
436
443
|
module.definitions.some((definition) => containsReference(definition.body)) ||
|
|
437
444
|
module.assumptions.some((assumption) => containsReference(assumption.expression));
|
|
438
445
|
if (!hasReferences) return module;
|
|
446
|
+
const goals = new Set(
|
|
447
|
+
module.theorems.flatMap((theorem) =>
|
|
448
|
+
theorem.name !== undefined && theorem.statement.kind === "AssumeProve" ? [theorem.name] : [],
|
|
449
|
+
),
|
|
450
|
+
);
|
|
439
451
|
return {
|
|
440
452
|
...module,
|
|
441
453
|
definitions: module.definitions.map((definition) =>
|
|
442
|
-
mapDefinition(definition, (child) => resolveSubexpressions(child, lookup)),
|
|
454
|
+
mapDefinition(definition, (child) => resolveSubexpressions(child, lookup, goals)),
|
|
443
455
|
),
|
|
444
456
|
assumptions: module.assumptions.map((assumption) => ({
|
|
445
457
|
...assumption,
|
|
446
|
-
expression: resolveSubexpressions(assumption.expression, lookup),
|
|
458
|
+
expression: resolveSubexpressions(assumption.expression, lookup, goals),
|
|
447
459
|
})),
|
|
448
460
|
};
|
|
449
461
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { Declaration, Definition, Expression, SourceLocation } from "./ast";
|
|
2
|
-
import {
|
|
1
|
+
import type { Declaration, Definition, Expression, SourceLocation, Theorem } from "./ast";
|
|
2
|
+
import { createSanyError, formatRange } from "./errors";
|
|
3
3
|
import {
|
|
4
|
+
ACTION_LEVEL,
|
|
5
|
+
analyzeAssumeProve,
|
|
4
6
|
createLevelAnalyzer,
|
|
5
7
|
getArgumentKey,
|
|
6
8
|
MAX_LEVEL,
|
|
@@ -24,6 +26,7 @@ interface ModuleContext {
|
|
|
24
26
|
variables: string[];
|
|
25
27
|
definitions: Definition[];
|
|
26
28
|
assumptions: Expression[];
|
|
29
|
+
theorems: Theorem[];
|
|
27
30
|
getDefinition: (name: string) => Definition | undefined;
|
|
28
31
|
}
|
|
29
32
|
|
|
@@ -46,10 +49,7 @@ interface Substituted {
|
|
|
46
49
|
const getMaxLevel = (operator: OperatorLevels, index: number) =>
|
|
47
50
|
operator.maxLevels[index] ?? MAX_LEVEL;
|
|
48
51
|
|
|
49
|
-
export const
|
|
50
|
-
checks: InstanceCheck[],
|
|
51
|
-
getModule: (name: string) => ModuleContext,
|
|
52
|
-
) => {
|
|
52
|
+
export const createInstanceChecker = (getModule: (name: string) => ModuleContext) => {
|
|
53
53
|
const analyzers = new Map<string, ReturnType<typeof createLevelAnalyzer>>();
|
|
54
54
|
const getAnalyzer = (name: string) => {
|
|
55
55
|
const cached = analyzers.get(name);
|
|
@@ -101,17 +101,38 @@ export const checkInstances = (
|
|
|
101
101
|
definition.formals.every((formal) => formal.name !== operator && formal.name !== param),
|
|
102
102
|
),
|
|
103
103
|
);
|
|
104
|
+
const addAssumedLevels = (data: ReturnType<typeof analyzeAssumeProve>) => {
|
|
105
|
+
addLevels(data, () => true);
|
|
106
|
+
module.constants.forEach((constant) => {
|
|
107
|
+
if (data.params.has(constant.name)) {
|
|
108
|
+
putConstraint(levels.constraints, constant.name, ACTION_LEVEL);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
};
|
|
104
112
|
module.assumptions.forEach((assumption) =>
|
|
105
|
-
|
|
113
|
+
addAssumedLevels(analyzer.analyze(assumption, new Map())),
|
|
114
|
+
);
|
|
115
|
+
module.theorems.forEach((theorem) =>
|
|
116
|
+
theorem.statement.kind === "AssumeProve"
|
|
117
|
+
? addAssumedLevels(analyzeAssumeProve(theorem.statement, new Map(), analyzer))
|
|
118
|
+
: addLevels(analyzer.analyze(theorem.statement, new Map()), () => true),
|
|
106
119
|
);
|
|
107
120
|
moduleLevels.set(name, levels);
|
|
108
121
|
return levels;
|
|
109
122
|
};
|
|
110
123
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
124
|
+
return (check: InstanceCheck) => {
|
|
125
|
+
const errors = new Map<string, { location: string; message: string }>();
|
|
126
|
+
// SANY reports each error at the whole instance.
|
|
127
|
+
const location = formatRange(
|
|
128
|
+
check.location.range ?? {
|
|
129
|
+
module: check.instancer,
|
|
130
|
+
start: check.location,
|
|
131
|
+
end: check.location,
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
const addMessage = (message: string) =>
|
|
135
|
+
errors.set(`${location}\n${message}`, { location, message });
|
|
115
136
|
const instancee = getModule(check.module);
|
|
116
137
|
const levels = getModuleLevels(check.module);
|
|
117
138
|
const analyzer = getAnalyzer(check.instancer);
|
|
@@ -141,14 +162,14 @@ export const checkInstances = (
|
|
|
141
162
|
];
|
|
142
163
|
});
|
|
143
164
|
const getLevelMessage = (name: string, level: number) =>
|
|
144
|
-
`Level error in instantiating module '${check.module}'
|
|
165
|
+
`Level error in instantiating module '${check.module}':\nThe level of the expression or operator substituted for '${name}' \nmust be at most ${level}.`;
|
|
145
166
|
substituted.forEach((item) => {
|
|
146
167
|
if (!levels.isConstant && item.level > item.maximumLevel) {
|
|
147
168
|
addMessage(getLevelMessage(item.name, item.maximumLevel));
|
|
148
169
|
}
|
|
149
170
|
if (!item.isLeibniz) {
|
|
150
171
|
addMessage(
|
|
151
|
-
`Error in instantiating module '${check.module}'
|
|
172
|
+
`Error in instantiating module '${check.module}':\n A non-Leibniz operator substituted for '${item.name}'.`,
|
|
152
173
|
);
|
|
153
174
|
}
|
|
154
175
|
});
|
|
@@ -162,7 +183,7 @@ export const checkInstances = (
|
|
|
162
183
|
const minimum = levels.argConstraints.get(getArgumentKey(item.name, index));
|
|
163
184
|
if (minimum !== undefined && getMaxLevel(operator, index) < minimum) {
|
|
164
185
|
addMessage(
|
|
165
|
-
`Level error in instantiating module '${check.module}'
|
|
186
|
+
`Level error in instantiating module '${check.module}':\nThe level of the argument ${index + 1} of the operator ${operator.name} \nmust be at least ${minimum}.`,
|
|
166
187
|
);
|
|
167
188
|
}
|
|
168
189
|
});
|
|
@@ -175,10 +196,10 @@ export const checkInstances = (
|
|
|
175
196
|
const maximum = getMaxLevel(operator, parameter.index);
|
|
176
197
|
if (argument.level > maximum) {
|
|
177
198
|
addMessage(
|
|
178
|
-
`Level error when instantiating module '${check.module}'
|
|
199
|
+
`Level error when instantiating module '${check.module}':\nThe level of the argument ${parameter.index} of the operator ${parameter.operator}' \nmust be at most ${maximum}.`,
|
|
179
200
|
);
|
|
180
201
|
}
|
|
181
202
|
});
|
|
182
|
-
|
|
183
|
-
|
|
203
|
+
if (errors.size > 0) throw createSanyError([...errors.values()]);
|
|
204
|
+
};
|
|
184
205
|
};
|
package/src/values.ts
CHANGED
|
@@ -1199,6 +1199,41 @@ const advanceCombination = (indices: number[], length: number) => {
|
|
|
1199
1199
|
return false;
|
|
1200
1200
|
};
|
|
1201
1201
|
|
|
1202
|
+
// TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that finds
|
|
1203
|
+
// its witness early never builds the rest. For a power set too large to build, the elements are
|
|
1204
|
+
// computed in that order as a loop reads them.
|
|
1205
|
+
export const getLazySubsets = (value: Value): Value[] | undefined => {
|
|
1206
|
+
if (typeof value !== "object" || value.kind !== "Subset" || isOverriddenSet(value.base)) {
|
|
1207
|
+
return undefined;
|
|
1208
|
+
}
|
|
1209
|
+
const base = toFiniteSet(value.base).elements;
|
|
1210
|
+
if (base.length <= MAXIMUM_ENUMERATED_SUBSET_BASE) return undefined;
|
|
1211
|
+
const cursor = { index: -1, indices: [0] };
|
|
1212
|
+
const getSubset = (index: number) => {
|
|
1213
|
+
if (index <= cursor.index) cursor.index = -1;
|
|
1214
|
+
while (cursor.index < index) {
|
|
1215
|
+
if (cursor.index === -1) cursor.indices = [];
|
|
1216
|
+
else if (!advanceCombination(cursor.indices, base.length)) {
|
|
1217
|
+
cursor.indices = Array.from({ length: cursor.indices.length + 1 }, (_, item) => item);
|
|
1218
|
+
}
|
|
1219
|
+
cursor.index++;
|
|
1220
|
+
}
|
|
1221
|
+
return createSortedSet(cursor.indices.map((item) => base[item]));
|
|
1222
|
+
};
|
|
1223
|
+
const length = 2 ** base.length;
|
|
1224
|
+
const isIndex = (property: string | symbol) =>
|
|
1225
|
+
typeof property === "string" && /^\d+$/.test(property) && Number(property) < length;
|
|
1226
|
+
return new Proxy<Value[]>([], {
|
|
1227
|
+
get: (target, property, receiver) => {
|
|
1228
|
+
if (property === "length") return length;
|
|
1229
|
+
return isIndex(property)
|
|
1230
|
+
? getSubset(Number(property))
|
|
1231
|
+
: Reflect.get(target, property, receiver);
|
|
1232
|
+
},
|
|
1233
|
+
has: (target, property) => isIndex(property) || Reflect.has(target, property),
|
|
1234
|
+
});
|
|
1235
|
+
};
|
|
1236
|
+
|
|
1202
1237
|
const getSortedSubsets = (base: Value[]) => {
|
|
1203
1238
|
const subsets: Value[] = [];
|
|
1204
1239
|
for (let size = 0; size <= base.length; size++) {
|
|
@@ -1291,7 +1326,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1291
1326
|
case "SetOperation": {
|
|
1292
1327
|
if (
|
|
1293
1328
|
value.operator === "\\cup" &&
|
|
1294
|
-
(
|
|
1329
|
+
(!isTlcEnumerable(value.left) || !isTlcEnumerable(value.right))
|
|
1295
1330
|
) {
|
|
1296
1331
|
throw new TlaEvaluationError(
|
|
1297
1332
|
`Attempted to enumerate S \\cup T when S:\n${formatValue(value.left)}\nand T:\n${formatValue(value.right)}\nare not both enumerable`,
|
|
@@ -1299,14 +1334,14 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1299
1334
|
}
|
|
1300
1335
|
if (
|
|
1301
1336
|
value.operator === "\\cap" &&
|
|
1302
|
-
|
|
1303
|
-
|
|
1337
|
+
!isTlcEnumerable(value.left) &&
|
|
1338
|
+
!isTlcEnumerable(value.right)
|
|
1304
1339
|
) {
|
|
1305
1340
|
throw new TlaEvaluationError(
|
|
1306
1341
|
`Attempted to enumerate S \\cap T when neither S:\n${formatValue(value.left)}\nnor T:\n${formatValue(value.right)}\nis enumerable`,
|
|
1307
1342
|
);
|
|
1308
1343
|
}
|
|
1309
|
-
if (value.operator === "\\" &&
|
|
1344
|
+
if (value.operator === "\\" && !isTlcEnumerable(value.left)) {
|
|
1310
1345
|
throw new TlaEvaluationError(
|
|
1311
1346
|
`Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
|
|
1312
1347
|
);
|
|
@@ -1393,14 +1428,14 @@ export const getStreamedElements = (value: Value): Value[] => {
|
|
|
1393
1428
|
}
|
|
1394
1429
|
if (value.kind !== "SetOperation") return toFiniteSet(value).elements;
|
|
1395
1430
|
if (value.operator === "\\cup") {
|
|
1396
|
-
return
|
|
1431
|
+
return !isTlcEnumerable(value.left) || !isTlcEnumerable(value.right)
|
|
1397
1432
|
? toFiniteSet(value).elements
|
|
1398
1433
|
: [...getStreamedElements(value.left), ...getStreamedElements(value.right)];
|
|
1399
1434
|
}
|
|
1400
1435
|
const isLeftEnumerated = value.operator === "\\" || isEnumerable(value.left);
|
|
1401
1436
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1402
1437
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1403
|
-
if (
|
|
1438
|
+
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements;
|
|
1404
1439
|
return getStreamedElements(enumerated).filter(
|
|
1405
1440
|
(element) => isElementOf(element, other) === (value.operator === "\\cap"),
|
|
1406
1441
|
);
|
|
@@ -1959,10 +1994,15 @@ const getElementContribution = (element: Value) =>
|
|
|
1959
1994
|
const getEntryContribution = (key: Value, value: Value) =>
|
|
1960
1995
|
finalizeHash((Math.imul(getValueHash(key), 0x27d4eb2d) + getValueHash(value)) | 0);
|
|
1961
1996
|
|
|
1997
|
+
const unenumeratedSets = { isHashed: false };
|
|
1998
|
+
|
|
1962
1999
|
const computeValueHash = (value: ObjectValue) => {
|
|
1963
2000
|
if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
|
|
1964
2001
|
if (isSetValue(value)) {
|
|
1965
|
-
if (!isEnumerable(value))
|
|
2002
|
+
if (!isEnumerable(value)) {
|
|
2003
|
+
unenumeratedSets.isHashed = true;
|
|
2004
|
+
return hashString(getValueKey(value));
|
|
2005
|
+
}
|
|
1966
2006
|
const elements = toFiniteSet(value).elements;
|
|
1967
2007
|
let hash = SET_HASH_SEED;
|
|
1968
2008
|
for (let index = 0; index < elements.length; index++) {
|
|
@@ -1994,6 +2034,25 @@ export const getValueHash = (value: Value): number => {
|
|
|
1994
2034
|
return hash;
|
|
1995
2035
|
};
|
|
1996
2036
|
|
|
2037
|
+
const fingerprintLikeTlc = (value: Value): void => {
|
|
2038
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2039
|
+
if (isOverriddenSet(value)) {
|
|
2040
|
+
throw new TlaEvaluationError(
|
|
2041
|
+
`TLC has found a state in which the value of a variable contains ${formatValue(value)}`,
|
|
2042
|
+
);
|
|
2043
|
+
}
|
|
2044
|
+
if (isSetValue(value)) return toFiniteSet(value).elements.forEach(fingerprintLikeTlc);
|
|
2045
|
+
const functionValue = toFiniteFunction(value);
|
|
2046
|
+
functionValue.domain.forEach(fingerprintLikeTlc);
|
|
2047
|
+
functionValue.range.forEach(fingerprintLikeTlc);
|
|
2048
|
+
};
|
|
2049
|
+
|
|
2050
|
+
// TLC enumerates every set in a state to fingerprint it, so it fails on a set it can't enumerate.
|
|
2051
|
+
// A state holds one only if hashing it met one, so states go unchecked until then.
|
|
2052
|
+
export const assertFingerprintable = (state: Value[]) => {
|
|
2053
|
+
if (unenumeratedSets.isHashed) state.forEach(fingerprintLikeTlc);
|
|
2054
|
+
};
|
|
2055
|
+
|
|
1997
2056
|
const valueFingerprints = new WeakMap<ObjectValue, number>();
|
|
1998
2057
|
|
|
1999
2058
|
const finalizeFingerprint = (hash: number) => {
|
|
@@ -2020,7 +2079,10 @@ const getEntryFingerprint = (key: Value, value: Value) =>
|
|
|
2020
2079
|
const computeValueFingerprint = (value: ObjectValue) => {
|
|
2021
2080
|
if (value.kind === "ModelValue") return finalizeFingerprint(fingerprintString(value.name) ^ 0x3c);
|
|
2022
2081
|
if (isSetValue(value)) {
|
|
2023
|
-
if (!isEnumerable(value))
|
|
2082
|
+
if (!isEnumerable(value)) {
|
|
2083
|
+
unenumeratedSets.isHashed = true;
|
|
2084
|
+
return fingerprintString(getValueKey(value));
|
|
2085
|
+
}
|
|
2024
2086
|
const elements = toFiniteSet(value).elements;
|
|
2025
2087
|
let fingerprint = 0x1b873593;
|
|
2026
2088
|
for (let index = 0; index < elements.length; index++) {
|