tla-ts 0.0.3-dev.a57ca4f → 0.0.3-dev.b0d11a3
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.d.cts +22 -22
- package/dist/index.d.ts +22 -22
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +1 -1
- package/dist/transfer.cjs +21 -18
- package/dist/transfer.js +21 -18
- package/package.json +1 -1
- package/src/check.ts +85 -16
- package/src/codegen.ts +6 -3
- package/src/environment.ts +25 -14
- package/src/errors.ts +26 -0
- package/src/evaluate.ts +39 -7
- package/src/live-check.ts +129 -70
- package/src/liveness.ts +3 -5
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/successors.ts +35 -18
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-module.ts +6 -6
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/values.ts +88 -13
package/src/tableau.ts
CHANGED
|
@@ -162,22 +162,39 @@ const containsAction = (expression: LiveExpression): boolean => {
|
|
|
162
162
|
}
|
|
163
163
|
};
|
|
164
164
|
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
165
|
+
const termKeys = new WeakMap<LiveExpression, string>();
|
|
166
|
+
const atoms = { count: 0 };
|
|
167
|
+
|
|
168
|
+
// A term's structure as a string in which each atom stands for itself, so equal terms share a key.
|
|
169
|
+
export const getTermKey = (expression: LiveExpression): string => {
|
|
170
|
+
const cached = termKeys.get(expression);
|
|
171
|
+
if (cached !== undefined) return cached;
|
|
172
|
+
const key = (() => {
|
|
173
|
+
switch (expression.kind) {
|
|
174
|
+
case "Boolean":
|
|
175
|
+
return String(expression.value);
|
|
176
|
+
case "State":
|
|
177
|
+
case "Enabled":
|
|
178
|
+
case "Action":
|
|
179
|
+
return `#${atoms.count++}`;
|
|
180
|
+
case "And":
|
|
181
|
+
case "Or":
|
|
182
|
+
return `${expression.kind}(${expression.operands.map(getTermKey).join(",")})`;
|
|
183
|
+
default:
|
|
184
|
+
return `${expression.kind}(${getTermKey(expression.body)})`;
|
|
185
|
+
}
|
|
186
|
+
})();
|
|
187
|
+
termKeys.set(expression, key);
|
|
188
|
+
return key;
|
|
177
189
|
};
|
|
178
190
|
|
|
179
|
-
const
|
|
180
|
-
|
|
191
|
+
const isEqual = (left: LiveExpression, right: LiveExpression) =>
|
|
192
|
+
left === right || getTermKey(left) === getTermKey(right);
|
|
193
|
+
|
|
194
|
+
const isMember = (items: LiveExpression[], expression: LiveExpression) => {
|
|
195
|
+
const key = getTermKey(expression);
|
|
196
|
+
return items.some((item) => item === expression || getTermKey(item) === key);
|
|
197
|
+
};
|
|
181
198
|
|
|
182
199
|
const getDual = (kind: LiveJunction["kind"]) => (kind === "And" ? "Or" : "And");
|
|
183
200
|
|
|
@@ -272,7 +289,7 @@ const toDisjunctiveNormalForm = (expression: LiveExpression): LiveExpression =>
|
|
|
272
289
|
total.value *= normal.operands.length;
|
|
273
290
|
if (total.value > MAXIMUM_DISJUNCTS) {
|
|
274
291
|
throw new TlaEvaluationError(
|
|
275
|
-
"TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form",
|
|
292
|
+
"TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form.",
|
|
276
293
|
);
|
|
277
294
|
}
|
|
278
295
|
}
|
|
@@ -446,13 +463,14 @@ const getTriples = (
|
|
|
446
463
|
return parts ? [{ whole: term, first: parts.first, second: parts.second }] : [];
|
|
447
464
|
});
|
|
448
465
|
|
|
449
|
-
|
|
450
|
-
|
|
466
|
+
// A particle is a set of terms, so its key ignores order and repeats.
|
|
467
|
+
const getParticleKey = (particle: LiveExpression[]) =>
|
|
468
|
+
[...new Set(particle.map(getTermKey))].sort().join("|");
|
|
451
469
|
|
|
452
|
-
const unionParticles = (left: LiveExpression[][], right: LiveExpression[][]) =>
|
|
453
|
-
|
|
454
|
-
...right
|
|
455
|
-
|
|
470
|
+
const unionParticles = (left: LiveExpression[][], right: LiveExpression[][]) => {
|
|
471
|
+
const rightKeys = new Set(right.map(getParticleKey));
|
|
472
|
+
return [...left.filter((particle) => !rightKeys.has(getParticleKey(particle))), ...right];
|
|
473
|
+
};
|
|
456
474
|
|
|
457
475
|
const getParticleClosure = (terms: LiveExpression[]) => {
|
|
458
476
|
const closure = getPositiveClosure(terms);
|
|
@@ -516,14 +534,23 @@ const createTableauNode = (particle: LiveExpression[]): TableauNode => ({
|
|
|
516
534
|
const createTableau = (formula: LiveExpression) => {
|
|
517
535
|
const nodes = getParticleClosure([formula]).map(createTableauNode);
|
|
518
536
|
const initialCount = nodes.length;
|
|
537
|
+
const indices = new Map<string, number>();
|
|
538
|
+
nodes.forEach((node, index) => {
|
|
539
|
+
const key = getParticleKey(node.particle);
|
|
540
|
+
if (!indices.has(key)) indices.set(key, index);
|
|
541
|
+
});
|
|
519
542
|
for (let index = 0; index < nodes.length; index++) {
|
|
520
543
|
const implied = nodes[index].particle.flatMap((term) =>
|
|
521
544
|
term.kind === "Next" ? [term.body] : [],
|
|
522
545
|
);
|
|
523
546
|
for (const particle of getParticleClosure(implied)) {
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
547
|
+
const key = getParticleKey(particle);
|
|
548
|
+
const existing = indices.get(key);
|
|
549
|
+
if (existing === undefined) {
|
|
550
|
+
indices.set(key, nodes.length);
|
|
551
|
+
nodes.push(createTableauNode(particle));
|
|
552
|
+
}
|
|
553
|
+
nodes[index].successors.push(existing ?? nodes.length - 1);
|
|
527
554
|
}
|
|
528
555
|
}
|
|
529
556
|
return { nodes, initialCount };
|
package/src/tlc-module.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TlaModuleError } from "./errors";
|
|
1
|
+
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { EXIT_STATUS } from "./exit-codes";
|
|
3
3
|
import type { Builtin } from "./evaluate";
|
|
4
4
|
import { prettyPrint } from "./pretty-print";
|
|
@@ -48,7 +48,7 @@ const getRuntimeValue = (
|
|
|
48
48
|
state: (Value | undefined)[] | undefined,
|
|
49
49
|
) => {
|
|
50
50
|
if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
|
|
51
|
-
throw new
|
|
51
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
52
52
|
}
|
|
53
53
|
const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
|
|
54
54
|
switch (key) {
|
|
@@ -104,11 +104,11 @@ const getRuntimeValue = (
|
|
|
104
104
|
}
|
|
105
105
|
if (key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
106
106
|
const value = getNamedRegisters(runtime).get(key);
|
|
107
|
-
if (value === undefined) throw new
|
|
107
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
108
108
|
return value;
|
|
109
109
|
}
|
|
110
110
|
if (key.startsWith("-D")) return rankString(runtime.properties.get(key.slice(2)) ?? key);
|
|
111
|
-
throw new
|
|
111
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
112
112
|
};
|
|
113
113
|
|
|
114
114
|
const getPermutations = (elements: Value[]): Value[][] =>
|
|
@@ -197,11 +197,11 @@ export const createTlcOperators = (
|
|
|
197
197
|
const key = getValue(args, 0, "TLCGet");
|
|
198
198
|
if (typeof key === "number" && key >= 0) {
|
|
199
199
|
const value = runtime.registers.get(key);
|
|
200
|
-
if (value === undefined) throw new
|
|
200
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
201
201
|
return value;
|
|
202
202
|
}
|
|
203
203
|
if (typeof key !== "string") {
|
|
204
|
-
throw new
|
|
204
|
+
throw new TlaEvaluationError(
|
|
205
205
|
`The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
206
206
|
);
|
|
207
207
|
}
|
package/src/tlc-modules.ts
CHANGED
|
@@ -1013,6 +1013,10 @@ export const TLC_JAVA_METHODS = new Map([
|
|
|
1013
1013
|
"RandomElement",
|
|
1014
1014
|
"public static tlc2.value.impl.Value tlc2.module.TLC.RandomElement(tlc2.value.impl.Value)",
|
|
1015
1015
|
],
|
|
1016
|
+
[
|
|
1017
|
+
"TLCGet",
|
|
1018
|
+
"public static tlc2.value.impl.Value tlc2.module.TLCGetSet.TLCGetEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
|
|
1019
|
+
],
|
|
1016
1020
|
[
|
|
1017
1021
|
"TLCEval",
|
|
1018
1022
|
"public static tlc2.value.impl.Value tlc2.module.TLCEval.tlcEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
|
package/src/tlc-order.ts
CHANGED
|
@@ -278,9 +278,13 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
|
|
|
278
278
|
(name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name);
|
|
279
279
|
};
|
|
280
280
|
|
|
281
|
-
// The variables in the order
|
|
282
|
-
//
|
|
283
|
-
export const
|
|
281
|
+
// The constants or variables in the order of ModuleNode.getConstantDecls or getVariableDecls: the
|
|
282
|
+
// reverse of the root module's context enumeration. TLC compares a state's variables in this order.
|
|
283
|
+
export const getTlcDeclarationOrder = (
|
|
284
|
+
source: string,
|
|
285
|
+
loadModule: ModuleLoader,
|
|
286
|
+
kind: "constant" | "variable",
|
|
287
|
+
): string[] => {
|
|
284
288
|
const getParsed = createModuleSet(source, loadModule);
|
|
285
289
|
const getUnits = (unit: ParsedModule) => {
|
|
286
290
|
const tokens = unit.tokens;
|
|
@@ -400,7 +404,7 @@ export const getTlcVariableOrder = (source: string, loadModule: ModuleLoader): s
|
|
|
400
404
|
};
|
|
401
405
|
return getContext(undefined)
|
|
402
406
|
.enumerate()
|
|
403
|
-
.filter((entry) => entry.kind ===
|
|
407
|
+
.filter((entry) => entry.kind === kind)
|
|
404
408
|
.map((entry) => entry.name)
|
|
405
409
|
.reverse();
|
|
406
410
|
};
|
package/src/tlc-random.ts
CHANGED
|
@@ -181,8 +181,10 @@ const createShiftTable = (length: number) => {
|
|
|
181
181
|
|
|
182
182
|
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
183
183
|
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
184
|
-
// The fingerprinter caches each distinct variable value for one pass over the stored states.
|
|
185
|
-
|
|
184
|
+
// The fingerprinter caches each distinct variable value for one pass over the stored states. It
|
|
185
|
+
// reads a state's variables in `order` and writes each fingerprint's low and high words into
|
|
186
|
+
// `words`, an Int32Array over a little-endian BigInt64Array.
|
|
187
|
+
export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
186
188
|
const shiftTables = new Map<number, Int32Array>();
|
|
187
189
|
const indices = new Map<Value, number>();
|
|
188
190
|
const contributions: number[] = [];
|
|
@@ -204,11 +206,11 @@ export const createTlcFingerprinter = () => {
|
|
|
204
206
|
shiftTables.set(length, table);
|
|
205
207
|
return table;
|
|
206
208
|
};
|
|
207
|
-
return (state: Value[]) => {
|
|
209
|
+
return (state: Value[], position: number) => {
|
|
208
210
|
const outer = { high, low };
|
|
209
211
|
const fingerprint = { high: INITIAL_HIGH, low: INITIAL_LOW };
|
|
210
|
-
for (const
|
|
211
|
-
const contribution = getContribution(
|
|
212
|
+
for (const index of order) {
|
|
213
|
+
const contribution = getContribution(state[index]);
|
|
212
214
|
const table = getShiftTable(contributions[contribution]);
|
|
213
215
|
const extended = {
|
|
214
216
|
high: contributions[contribution + 1],
|
|
@@ -225,7 +227,8 @@ export const createTlcFingerprinter = () => {
|
|
|
225
227
|
}
|
|
226
228
|
high = outer.high;
|
|
227
229
|
low = outer.low;
|
|
228
|
-
|
|
230
|
+
words[2 * position] = fingerprint.low;
|
|
231
|
+
words[2 * position + 1] = fingerprint.high;
|
|
229
232
|
};
|
|
230
233
|
};
|
|
231
234
|
|
package/src/tlc-summary.ts
CHANGED
|
@@ -87,11 +87,26 @@ export const getFingerprintDistance = (
|
|
|
87
87
|
const probability = getOptimisticProbability(distinctStates, generatedStates);
|
|
88
88
|
if (probability < ACTUAL_PROBABILITY_THRESHOLD) return undefined;
|
|
89
89
|
const fingerprints = getFingerprints().sort();
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
// The low and high words of each fingerprint. A gap whose high words differ by at most 2^20 is
|
|
91
|
+
// below 2^53, so a number holds it exactly and only wider gaps need a BigInt.
|
|
92
|
+
const words = new Int32Array(fingerprints.buffer);
|
|
93
|
+
const nearest = { narrow: Number.POSITIVE_INFINITY, wide: 0x7fff_ffff_ffff_ffffn };
|
|
94
|
+
for (let index = 1; index < fingerprints.length; index++) {
|
|
95
|
+
const high = words[2 * index + 1];
|
|
96
|
+
const previousHigh = words[2 * index - 1];
|
|
97
|
+
if (high < 0 !== previousHigh < 0) continue;
|
|
98
|
+
const highGap = high - previousHigh;
|
|
99
|
+
if (highGap <= 0x10_0000) {
|
|
100
|
+
const gap = highGap * 0x1_0000_0000 + (words[2 * index] >>> 0) - (words[2 * index - 2] >>> 0);
|
|
101
|
+
nearest.narrow = Math.min(nearest.narrow, gap);
|
|
102
|
+
} else {
|
|
103
|
+
const gap = fingerprints[index] - fingerprints[index - 1];
|
|
104
|
+
if (gap < nearest.wide) nearest.wide = gap;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (nearest.narrow === Number.POSITIVE_INFINITY) return nearest.wide;
|
|
108
|
+
const narrow = BigInt(nearest.narrow);
|
|
109
|
+
return narrow < nearest.wide ? narrow : nearest.wide;
|
|
95
110
|
};
|
|
96
111
|
|
|
97
112
|
export const getOutDegreeSummary = (buckets: number[]) => {
|
package/src/values.ts
CHANGED
|
@@ -378,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
378
378
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
379
379
|
);
|
|
380
380
|
}
|
|
381
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
381
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
382
382
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
383
383
|
}
|
|
384
384
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -871,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
|
871
871
|
}
|
|
872
872
|
};
|
|
873
873
|
|
|
874
|
+
// TLC sorts a set by comparing each element with the ones before it, so the later element converts
|
|
875
|
+
// a lazy set it compares and caches it.
|
|
874
876
|
const isStrictlySorted = (elements: Value[]) => {
|
|
875
877
|
for (let index = 1; index < elements.length; index++) {
|
|
876
|
-
if (compareValues(elements[index
|
|
878
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
877
879
|
}
|
|
878
880
|
return true;
|
|
879
881
|
};
|
|
@@ -1222,9 +1224,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1222
1224
|
if (isOverriddenSet(value.base)) getCardinality(value.base);
|
|
1223
1225
|
const base = toFiniteSet(value.base).elements;
|
|
1224
1226
|
if (base.length > MAXIMUM_ENUMERATED_SUBSET_BASE) {
|
|
1225
|
-
throw new TlaEvaluationError(
|
|
1226
|
-
`SUBSET of a set with ${base.length} elements is too large to enumerate`,
|
|
1227
|
-
);
|
|
1227
|
+
throw new TlaEvaluationError(describeOverflow(value));
|
|
1228
1228
|
}
|
|
1229
1229
|
return getSortedSubsets(base);
|
|
1230
1230
|
}
|
|
@@ -1328,6 +1328,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1328
1328
|
};
|
|
1329
1329
|
|
|
1330
1330
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1331
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1331
1332
|
|
|
1332
1333
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1333
1334
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1336,6 +1337,13 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1336
1337
|
}
|
|
1337
1338
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1338
1339
|
if (cached) return cached;
|
|
1340
|
+
const set =
|
|
1341
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
|
|
1342
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1343
|
+
return set;
|
|
1344
|
+
};
|
|
1345
|
+
|
|
1346
|
+
const enumerateFiniteSet = (value: Value) => {
|
|
1339
1347
|
const elements = enumerateSet(value);
|
|
1340
1348
|
const isSorted =
|
|
1341
1349
|
typeof value === "object" &&
|
|
@@ -1346,8 +1354,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1346
1354
|
value.kind === "ProductSet" ||
|
|
1347
1355
|
value.kind === "FunctionSet" ||
|
|
1348
1356
|
value.kind === "Subset");
|
|
1349
|
-
|
|
1350
|
-
|
|
1357
|
+
return isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1358
|
+
};
|
|
1359
|
+
|
|
1360
|
+
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1361
|
+
// compared that way still streams the sets it combines.
|
|
1362
|
+
const toComparedSet = (value: Value) => {
|
|
1363
|
+
const isStreamed =
|
|
1364
|
+
typeof value === "object" &&
|
|
1365
|
+
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1366
|
+
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1367
|
+
const compared = comparedSets.get(value);
|
|
1368
|
+
if (compared) return compared;
|
|
1369
|
+
const set = enumerateFiniteSet(value);
|
|
1370
|
+
comparedSets.set(value, set);
|
|
1351
1371
|
return set;
|
|
1352
1372
|
};
|
|
1353
1373
|
|
|
@@ -1730,10 +1750,61 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
|
|
|
1730
1750
|
return functionValue;
|
|
1731
1751
|
};
|
|
1732
1752
|
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1753
|
+
// TLC applies a tuple, a record and a function over an interval with their own classes, and each
|
|
1754
|
+
// reports a missing argument in its own words.
|
|
1755
|
+
const getFunctionClass = (functionValue: FunctionValue) => {
|
|
1756
|
+
const domain = functionValue.domain;
|
|
1757
|
+
if (domain.length === 0) return lambdaFunctions.has(functionValue) ? "FcnRcdValue" : "TupleValue";
|
|
1758
|
+
if (domain === intervalDomains[domain.length]) return "TupleValue";
|
|
1759
|
+
if (intervalArrays.has(domain)) return "IntervalFcnRcdValue";
|
|
1760
|
+
return domain.every((key) => typeof key === "string") ? "RecordValue" : "FcnRcdValue";
|
|
1761
|
+
};
|
|
1762
|
+
|
|
1763
|
+
const describeNotInDomain = (functionValue: FunctionValue, argument: Value) =>
|
|
1764
|
+
`Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument ${prettyPrint(formatValue(argument))}, which is not in the domain of the function.`;
|
|
1765
|
+
|
|
1766
|
+
const describeNonIntegerArgument = (argument: Value) =>
|
|
1767
|
+
`Attempted to apply function with integer domain to the non-integer argument ${prettyPrint(formatValue(argument))}`;
|
|
1768
|
+
|
|
1769
|
+
const describeMissingArgument = (
|
|
1770
|
+
functionValue: FunctionValue | LazyFunctionValue,
|
|
1771
|
+
argument: Value,
|
|
1772
|
+
) => {
|
|
1773
|
+
if (lambdaFunctions.has(functionValue) || functionValue.kind === "LazyFunction")
|
|
1774
|
+
return `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`;
|
|
1775
|
+
const functionClass = getFunctionClass(functionValue);
|
|
1776
|
+
if (functionClass === "TupleValue")
|
|
1777
|
+
return typeof argument === "number"
|
|
1778
|
+
? `Attempted to access index ${argument} of tuple\n${prettyPrint(formatValue(functionValue))}\nwhich is out of bounds.`
|
|
1779
|
+
: `Attempted to access tuple at a non integral index: ${prettyPrint(formatValue(argument))}`;
|
|
1780
|
+
if (functionClass === "RecordValue")
|
|
1781
|
+
return typeof argument === "string"
|
|
1782
|
+
? `Attempted to access nonexistent field '${argument}' of record\n${prettyPrint(formatValue(functionValue))}`
|
|
1783
|
+
: `Attempted to access record by a non-string argument: ${prettyPrint(formatValue(argument))}`;
|
|
1784
|
+
return functionClass === "IntervalFcnRcdValue" && typeof argument !== "number"
|
|
1785
|
+
? describeNonIntegerArgument(argument)
|
|
1786
|
+
: describeNotInDomain(functionValue, argument);
|
|
1787
|
+
};
|
|
1788
|
+
|
|
1789
|
+
// TLC selects a field of a value that isn't a record from the function the value converts to,
|
|
1790
|
+
// which keeps a tuple's domain as an interval.
|
|
1791
|
+
export const describeMissingField = (functionValue: FunctionValue, field: string) =>
|
|
1792
|
+
getFunctionClass(functionValue) === "FcnRcdValue"
|
|
1793
|
+
? describeNotInDomain(functionValue, field)
|
|
1794
|
+
: describeNonIntegerArgument(field);
|
|
1795
|
+
|
|
1796
|
+
export const isRecordValue = (functionValue: FunctionValue) =>
|
|
1797
|
+
getFunctionClass(functionValue) === "RecordValue";
|
|
1798
|
+
|
|
1799
|
+
// TLC checks the type of an argument to a tuple or a record before it looks the argument up, so
|
|
1800
|
+
// it never compares the argument with a key of another type.
|
|
1801
|
+
const isForeignKey = (domain: Value[], argument: Value) => {
|
|
1802
|
+
const first = domain[0];
|
|
1803
|
+
return typeof first === "number"
|
|
1804
|
+
? typeof argument !== "number" &&
|
|
1805
|
+
(domain === intervalDomains[domain.length] || intervalArrays.has(domain))
|
|
1806
|
+
: typeof first === "string" && typeof argument !== "string";
|
|
1807
|
+
};
|
|
1737
1808
|
|
|
1738
1809
|
const findKeyIndex = (domain: Value[], argument: Value) => {
|
|
1739
1810
|
const first = domain[0];
|
|
@@ -1752,6 +1823,7 @@ export const applyFunction = (
|
|
|
1752
1823
|
functionValue: Value,
|
|
1753
1824
|
argument: Value,
|
|
1754
1825
|
describeMissing?: (functionValue: FunctionValue) => string,
|
|
1826
|
+
describeNonFunction?: (value: Value) => string,
|
|
1755
1827
|
): Value => {
|
|
1756
1828
|
if (typeof functionValue === "object" && functionValue.kind === "LazyFunction") {
|
|
1757
1829
|
if (!isElementOf(argument, functionValue.domain)) {
|
|
@@ -1765,10 +1837,13 @@ export const applyFunction = (
|
|
|
1765
1837
|
}
|
|
1766
1838
|
if (typeof functionValue !== "object" || functionValue.kind !== "Function") {
|
|
1767
1839
|
throw new TlaEvaluationError(
|
|
1768
|
-
|
|
1840
|
+
describeNonFunction?.(functionValue) ??
|
|
1841
|
+
`Attempted to apply ${formatValue(functionValue)}, which is not a function`,
|
|
1769
1842
|
);
|
|
1770
1843
|
}
|
|
1771
|
-
const position =
|
|
1844
|
+
const position = isForeignKey(functionValue.domain, argument)
|
|
1845
|
+
? -1
|
|
1846
|
+
: findKeyIndex(functionValue.domain, argument);
|
|
1772
1847
|
if (position === -1) {
|
|
1773
1848
|
throw new TlaEvaluationError(
|
|
1774
1849
|
describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
|