tla-ts 0.0.4-dev.e4c2d0e → 0.0.4-dev.f352c6c
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/run-cli.cjs +1 -1
- package/dist/run-cli.js +1 -1
- package/dist/transfer.cjs +27 -27
- package/dist/transfer.js +27 -27
- package/package.json +1 -1
- package/src/check.ts +1 -0
- package/src/codegen.ts +26 -7
- package/src/evaluate.ts +35 -13
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +24 -5
- package/src/tlc-random.ts +55 -32
- package/src/values.ts +77 -38
package/package.json
CHANGED
package/src/check.ts
CHANGED
package/src/codegen.ts
CHANGED
|
@@ -177,7 +177,7 @@ const generateQuantifier = (
|
|
|
177
177
|
}, test);
|
|
178
178
|
const parameters = outer.map((local) => `, ${local}`).join("");
|
|
179
179
|
generator.helpers.push(
|
|
180
|
-
`function ${name}(context, state, next${parameters}) { let primed; ${declarations.join(" ")} ${loops} return ${isForall}; }`,
|
|
180
|
+
`function ${name}(context, state, next${parameters}) { let primed; let applied; ${declarations.join(" ")} ${loops} return ${isForall}; }`,
|
|
181
181
|
);
|
|
182
182
|
return `${name}(${frame.context}, ${frame.state}, ${frame.isPrimed ? "undefined" : frame.next}${parameters})`;
|
|
183
183
|
};
|
|
@@ -247,6 +247,23 @@ const isConstructionFree = (
|
|
|
247
247
|
}
|
|
248
248
|
};
|
|
249
249
|
|
|
250
|
+
// Whether the generated code evaluates an expression only in the context its frame names. A primed
|
|
251
|
+
// expression doesn't, since it shares the generated function's primed context.
|
|
252
|
+
const isReadOnly = (expression: Expression): boolean => {
|
|
253
|
+
switch (expression.kind) {
|
|
254
|
+
case "Identifier":
|
|
255
|
+
case "Number":
|
|
256
|
+
case "String":
|
|
257
|
+
return true;
|
|
258
|
+
case "FieldAccess":
|
|
259
|
+
return isReadOnly(expression.record);
|
|
260
|
+
case "FunctionApplication":
|
|
261
|
+
return [expression.function, ...expression.args].every(isReadOnly);
|
|
262
|
+
default:
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
250
267
|
const generateExpression = (generator: Generator, expression: Expression, frame: Frame): string => {
|
|
251
268
|
const generate = (node: Expression, nodeFrame = frame) =>
|
|
252
269
|
generateExpression(generator, node, nodeFrame);
|
|
@@ -287,17 +304,19 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
|
|
|
287
304
|
case "FunctionApplication": {
|
|
288
305
|
keepLazy(expression.function);
|
|
289
306
|
// The interpreter evaluates a function that may construct one with EvalControl.KeepLazy.
|
|
307
|
+
const keepLazyFrame = { ...frame, context: `keepLazyContext(${frame.context})` };
|
|
290
308
|
const evaluatedFunction = isConstructionFree(
|
|
291
309
|
expression.function,
|
|
292
310
|
frame,
|
|
293
311
|
generator.environment,
|
|
294
312
|
)
|
|
295
313
|
? generate(expression.function)
|
|
296
|
-
:
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
314
|
+
: isReadOnly(expression.function)
|
|
315
|
+
? generate(expression.function, keepLazyFrame)
|
|
316
|
+
: generateDelegate(generator, expression.function, keepLazyFrame);
|
|
317
|
+
// Tool#evalAppl checks that it applies a function before it evaluates the argument. The check
|
|
318
|
+
// is written out, as calling expectFunction for each application costs more.
|
|
319
|
+
const functionCode = `(applied = ${evaluatedFunction}, typeof applied === "object" && (applied.kind === "Function" || applied.kind === "LazyFunction") ? applied : expectFunction(applied, ${addReference(generator, expression)}))`;
|
|
301
320
|
const describeMissing = getMissingArgumentDescriber(generator.environment.runtime.version);
|
|
302
321
|
const error = describeMissing ? `, ${addReference(generator, describeMissing)}` : "";
|
|
303
322
|
if (expression.args.length === 1)
|
|
@@ -370,7 +389,7 @@ export const generateEvaluator = (
|
|
|
370
389
|
};
|
|
371
390
|
const body = generateExpression(generator, expression, frame);
|
|
372
391
|
const referenceNames = generator.references.map((_, index) => `reference${index}`);
|
|
373
|
-
const source = `${generator.helpers.join("\n")}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${body}; };`;
|
|
392
|
+
const source = `${generator.helpers.join("\n")}\nreturn (context) => { const state = context.state; const next = context.next; let primed; let applied; return ${body}; };`;
|
|
374
393
|
runtime.value ??= createRuntime();
|
|
375
394
|
const helpers = runtime.value;
|
|
376
395
|
const helperNames = Object.keys(helpers);
|
package/src/evaluate.ts
CHANGED
|
@@ -54,6 +54,7 @@ import {
|
|
|
54
54
|
markLambdaFunction,
|
|
55
55
|
isReducibleSet,
|
|
56
56
|
isSetValue,
|
|
57
|
+
isSettledValue,
|
|
57
58
|
MAXIMUM_INTEGER,
|
|
58
59
|
MINIMUM_INTEGER,
|
|
59
60
|
toFiniteFunction,
|
|
@@ -113,6 +114,9 @@ export interface Thunk {
|
|
|
113
114
|
state: State | undefined;
|
|
114
115
|
next: State | undefined;
|
|
115
116
|
cache: Value | undefined;
|
|
117
|
+
// Whether the expression may build a set that streams until TLC caches it, so that the thunk
|
|
118
|
+
// caches only a settled value.
|
|
119
|
+
isStreamed: boolean;
|
|
116
120
|
}
|
|
117
121
|
|
|
118
122
|
type Binding = { kind: "Value"; value: Value } | Thunk | { kind: "Operator"; operator: Operator };
|
|
@@ -380,7 +384,15 @@ export const expectFunction = (value: Value, application: Expression) => {
|
|
|
380
384
|
};
|
|
381
385
|
|
|
382
386
|
export const toKeepLazyContext = (context: Context): Context =>
|
|
383
|
-
context.isKeepLazy
|
|
387
|
+
context.isKeepLazy
|
|
388
|
+
? context
|
|
389
|
+
: {
|
|
390
|
+
environment: context.environment,
|
|
391
|
+
scope: context.scope,
|
|
392
|
+
state: context.state,
|
|
393
|
+
next: context.next,
|
|
394
|
+
isKeepLazy: true,
|
|
395
|
+
};
|
|
384
396
|
|
|
385
397
|
const withScope = (context: Context, scope: Scope | undefined): Context => ({
|
|
386
398
|
environment: context.environment,
|
|
@@ -839,7 +851,7 @@ const forceThunk = (thunk: Thunk, context: Context) => {
|
|
|
839
851
|
const isSameState = thunk.state === context.state && thunk.next === context.next;
|
|
840
852
|
if (isSameState && thunk.cache !== undefined) return thunk.cache;
|
|
841
853
|
const value = evaluate(thunk.expression, withScope(context, thunk.scope));
|
|
842
|
-
if (isSameState) thunk.cache = value;
|
|
854
|
+
if (isSameState && (!thunk.isStreamed || isSettledValue(value))) thunk.cache = value;
|
|
843
855
|
return value;
|
|
844
856
|
};
|
|
845
857
|
|
|
@@ -960,6 +972,7 @@ const createThunk = (expression: Expression, context: Context): Thunk => ({
|
|
|
960
972
|
state: context.state,
|
|
961
973
|
next: context.next,
|
|
962
974
|
cache: undefined,
|
|
975
|
+
isStreamed: false,
|
|
963
976
|
});
|
|
964
977
|
|
|
965
978
|
export const findGlobalName = (name: string, environment: Environment): GlobalName | undefined => {
|
|
@@ -1056,10 +1069,16 @@ const callOperator = (operator: Operator, args: Expression[], context: Context)
|
|
|
1056
1069
|
);
|
|
1057
1070
|
};
|
|
1058
1071
|
|
|
1072
|
+
// The LET definitions that a reference evaluates again, and those that cache only a settled value.
|
|
1073
|
+
interface ReevaluatedDefinitions {
|
|
1074
|
+
impure: ReadonlySet<Definition>;
|
|
1075
|
+
streamed: ReadonlySet<Definition>;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1059
1078
|
export const bindDefinitions = (
|
|
1060
1079
|
definitions: Definition[],
|
|
1061
1080
|
context: Context,
|
|
1062
|
-
reevaluated?:
|
|
1081
|
+
reevaluated?: ReevaluatedDefinitions,
|
|
1063
1082
|
): Scope | undefined =>
|
|
1064
1083
|
definitions.reduce<Scope | undefined>((scope, definition) => {
|
|
1065
1084
|
const node: Scope = {
|
|
@@ -1081,10 +1100,11 @@ export const bindDefinitions = (
|
|
|
1081
1100
|
state: context.state,
|
|
1082
1101
|
next: context.next,
|
|
1083
1102
|
cache: undefined,
|
|
1103
|
+
isStreamed: false,
|
|
1084
1104
|
};
|
|
1085
1105
|
return node;
|
|
1086
1106
|
}
|
|
1087
|
-
if (definition.params.length > 0 || reevaluated?.has(definition)) {
|
|
1107
|
+
if (definition.params.length > 0 || reevaluated?.impure.has(definition)) {
|
|
1088
1108
|
node.binding = {
|
|
1089
1109
|
kind: "Operator",
|
|
1090
1110
|
operator: {
|
|
@@ -1104,6 +1124,7 @@ export const bindDefinitions = (
|
|
|
1104
1124
|
state: context.state,
|
|
1105
1125
|
next: context.next,
|
|
1106
1126
|
cache: undefined,
|
|
1127
|
+
isStreamed: reevaluated?.streamed.has(definition) ?? false,
|
|
1107
1128
|
};
|
|
1108
1129
|
return node;
|
|
1109
1130
|
}, context.scope);
|
|
@@ -1220,14 +1241,15 @@ const STREAMED_SET: Impurity = {
|
|
|
1220
1241
|
// TLC's next-state generator does not bind LET definitions to values, so each reference to one
|
|
1221
1242
|
// evaluates it again: one that calls an impure builtin repeats its effects, and a set one builds
|
|
1222
1243
|
// with UNION or \cup starts out uncached.
|
|
1223
|
-
export const getReevaluatedDefinitions = (
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
),
|
|
1230
|
-
)
|
|
1244
|
+
export const getReevaluatedDefinitions = (
|
|
1245
|
+
definitions: Definition[],
|
|
1246
|
+
environment: Environment,
|
|
1247
|
+
): ReevaluatedDefinitions => ({
|
|
1248
|
+
impure: new Set(definitions.filter((definition) => isImpureDefinition(definition, environment))),
|
|
1249
|
+
streamed: new Set(
|
|
1250
|
+
definitions.filter((definition) => isImpureDefinition(definition, environment, STREAMED_SET)),
|
|
1251
|
+
),
|
|
1252
|
+
});
|
|
1231
1253
|
|
|
1232
1254
|
export const isImpure = (environment: Environment) =>
|
|
1233
1255
|
[...environment.definitions.values()].some((definition) =>
|
|
@@ -1511,7 +1533,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1511
1533
|
|
|
1512
1534
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1513
1535
|
|
|
1514
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1536
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1515
1537
|
if (environment.callStack) return getEvaluator(expression, environment);
|
|
1516
1538
|
const cached = entryEvaluators.get(expression);
|
|
1517
1539
|
if (cached) return cached;
|
package/src/standard-modules.ts
CHANGED
|
@@ -162,8 +162,11 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
162
162
|
: differenceSorted;
|
|
163
163
|
// The merge compares the elements, which TLC tests for membership one by one instead.
|
|
164
164
|
return computeLikeTlcOnFailure(
|
|
165
|
-
() => merge(toFiniteSet(
|
|
166
|
-
() =>
|
|
165
|
+
(finiteLeft, finiteRight) => merge(toFiniteSet(finiteLeft), toFiniteSet(finiteRight)),
|
|
166
|
+
(reducibleLeft, reducibleRight) =>
|
|
167
|
+
combineReducible(operator, reducibleLeft, reducibleRight),
|
|
168
|
+
left,
|
|
169
|
+
right,
|
|
167
170
|
);
|
|
168
171
|
}
|
|
169
172
|
if (isReducibleSet(left)) return combineReducible(operator, left, right);
|
package/src/successors.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
toKeepLazyContext,
|
|
26
26
|
getDefinition,
|
|
27
27
|
getContextElements,
|
|
28
|
+
getEntryEvaluator,
|
|
28
29
|
getReevaluatedDefinitions,
|
|
29
30
|
lookupScope,
|
|
30
31
|
resolveApplyOperator,
|
|
@@ -32,6 +33,7 @@ import {
|
|
|
32
33
|
toUnspecifiedSubscriptError,
|
|
33
34
|
type Context,
|
|
34
35
|
type Environment,
|
|
36
|
+
type Evaluator,
|
|
35
37
|
type Scope,
|
|
36
38
|
type State,
|
|
37
39
|
} from "./evaluate";
|
|
@@ -129,12 +131,24 @@ const getIfConditionMessage = (run: Run, value: Value, expression: Expression) =
|
|
|
129
131
|
return `In computing ${getComputation(run)}, a non-boolean expression (${kind}) was used as the condition of an IF.${run.isInit ? "\n" : ""}${range}`;
|
|
130
132
|
};
|
|
131
133
|
|
|
132
|
-
const holds = (run: Run, expression: Expression, context: Context, isWalked = true) =>
|
|
133
|
-
|
|
134
|
+
const holds = (run: Run, expression: Expression, context: Context, isWalked = true) =>
|
|
135
|
+
holdsValue(run, expression, evaluate(expression, context), isWalked);
|
|
136
|
+
|
|
137
|
+
const holdsValue = (run: Run, expression: Expression, value: Value, isWalked = true) => {
|
|
134
138
|
if (typeof value === "boolean") return value;
|
|
135
139
|
throw createRunError(run, getNonBooleanMessage(run, value, expression, isWalked));
|
|
136
140
|
};
|
|
137
141
|
|
|
142
|
+
const evaluateCached = (
|
|
143
|
+
evaluators: (Evaluator | undefined)[],
|
|
144
|
+
index: number,
|
|
145
|
+
expression: Expression,
|
|
146
|
+
context: Context,
|
|
147
|
+
) =>
|
|
148
|
+
context.environment.callStack
|
|
149
|
+
? evaluate(expression, context)
|
|
150
|
+
: (evaluators[index] ??= getEntryEvaluator(expression, context.environment))(context);
|
|
151
|
+
|
|
138
152
|
const isAssignmentOperator = (operator: string) =>
|
|
139
153
|
operator === "=" || operator === "\\in" || operator === "\\subseteq";
|
|
140
154
|
|
|
@@ -357,6 +371,8 @@ const compileConjunction = (
|
|
|
357
371
|
isInit: boolean,
|
|
358
372
|
): Runner => {
|
|
359
373
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
374
|
+
const evaluators: (Evaluator | undefined)[] = [];
|
|
375
|
+
const restEvaluators: (Evaluator | undefined)[] = [];
|
|
360
376
|
const runFrom = (
|
|
361
377
|
start: number,
|
|
362
378
|
run: Run,
|
|
@@ -372,7 +388,9 @@ const compileConjunction = (
|
|
|
372
388
|
if (index > 0 && isShortcut && isComplete(current)) {
|
|
373
389
|
context ??= getContext(run, scope, current);
|
|
374
390
|
for (let rest = index; rest < conjuncts.length; rest++) {
|
|
375
|
-
|
|
391
|
+
const expression = conjuncts[rest].expression;
|
|
392
|
+
const value = evaluateCached(restEvaluators, rest, expression, context);
|
|
393
|
+
if (!holdsValue(run, expression, value, false)) return;
|
|
376
394
|
}
|
|
377
395
|
onComplete(current, action);
|
|
378
396
|
return;
|
|
@@ -380,12 +398,13 @@ const compileConjunction = (
|
|
|
380
398
|
const conjunct = conjuncts[index];
|
|
381
399
|
if (conjunct.kind === "Guard") {
|
|
382
400
|
context ??= getContext(run, scope, current);
|
|
383
|
-
|
|
401
|
+
const value = evaluateCached(evaluators, index, conjunct.expression, context);
|
|
402
|
+
if (!holdsValue(run, conjunct.expression, value)) return;
|
|
384
403
|
continue;
|
|
385
404
|
}
|
|
386
405
|
if (conjunct.kind === "Assign") {
|
|
387
406
|
context ??= getContext(run, scope, current);
|
|
388
|
-
const value =
|
|
407
|
+
const value = evaluateCached(evaluators, index, conjunct.value, context);
|
|
389
408
|
const assigned = current[conjunct.index];
|
|
390
409
|
if (assigned === undefined) {
|
|
391
410
|
current = assign(current, conjunct.index, value);
|
package/src/tlc-random.ts
CHANGED
|
@@ -182,24 +182,14 @@ const createShiftTable = (length: number) => {
|
|
|
182
182
|
|
|
183
183
|
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
184
184
|
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
185
|
-
// The fingerprinter caches each distinct
|
|
186
|
-
//
|
|
187
|
-
// `words`, an Int32Array over a
|
|
185
|
+
// The fingerprinter caches each distinct function or set it meets, nested ones included, for one
|
|
186
|
+
// pass over the stored states, as states share most of their values. It reads a state's variables
|
|
187
|
+
// in `order` and writes each fingerprint's low and high words into `words`, an Int32Array over a
|
|
188
|
+
// little-endian BigInt64Array.
|
|
188
189
|
export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
189
190
|
const shiftTables = new Map<number, Int32Array>();
|
|
190
191
|
const indices = new Map<Value, number>();
|
|
191
192
|
const contributions: number[] = [];
|
|
192
|
-
const getContribution = (value: Value) => {
|
|
193
|
-
const cached = indices.get(value);
|
|
194
|
-
if (cached !== undefined) return cached;
|
|
195
|
-
const length = getFingerprintLength(value);
|
|
196
|
-
high = 0;
|
|
197
|
-
low = 0;
|
|
198
|
-
extendValue(value);
|
|
199
|
-
indices.set(value, contributions.length);
|
|
200
|
-
contributions.push(length, high, low);
|
|
201
|
-
return contributions.length - 3;
|
|
202
|
-
};
|
|
203
193
|
const getShiftTable = (length: number) => {
|
|
204
194
|
const cached = shiftTables.get(length);
|
|
205
195
|
if (cached) return cached;
|
|
@@ -207,29 +197,62 @@ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
|
207
197
|
shiftTables.set(length, table);
|
|
208
198
|
return table;
|
|
209
199
|
};
|
|
210
|
-
|
|
200
|
+
// Extends the fingerprint by a value's bytes and returns how many there are.
|
|
201
|
+
const extendElement = (value: Value): number => {
|
|
202
|
+
if (typeof value !== "object" || isModelValue(value)) {
|
|
203
|
+
extendValue(value);
|
|
204
|
+
return getFingerprintLength(value);
|
|
205
|
+
}
|
|
206
|
+
const contribution = getContribution(value);
|
|
207
|
+
const shifted = { high, low };
|
|
208
|
+
const table = getShiftTable(contributions[contribution]);
|
|
209
|
+
high = contributions[contribution + 1];
|
|
210
|
+
low = contributions[contribution + 2];
|
|
211
|
+
for (let position = 0; position < 8; position++) {
|
|
212
|
+
const word = position < 4 ? shifted.low : shifted.high;
|
|
213
|
+
const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
|
|
214
|
+
high ^= table[entry];
|
|
215
|
+
low ^= table[entry + 1];
|
|
216
|
+
}
|
|
217
|
+
return contributions[contribution];
|
|
218
|
+
};
|
|
219
|
+
const getContribution = (value: Value) => {
|
|
220
|
+
const cached = indices.get(value);
|
|
221
|
+
if (cached !== undefined) return cached;
|
|
211
222
|
const outer = { high, low };
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
|
|
223
|
-
extended.high ^= table[entry];
|
|
224
|
-
extended.low ^= table[entry + 1];
|
|
223
|
+
high = 0;
|
|
224
|
+
low = 0;
|
|
225
|
+
const length = { value: 5 };
|
|
226
|
+
if (isFunctionValue(value)) {
|
|
227
|
+
const functionValue = toFiniteFunction(value);
|
|
228
|
+
extendByte(FCN_RCD_VALUE);
|
|
229
|
+
extendInt(functionValue.domain.length);
|
|
230
|
+
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
231
|
+
length.value += extendElement(functionValue.domain[index]);
|
|
232
|
+
length.value += extendElement(functionValue.range[index]);
|
|
225
233
|
}
|
|
226
|
-
|
|
227
|
-
|
|
234
|
+
} else {
|
|
235
|
+
const elements = toFiniteSet(value).elements;
|
|
236
|
+
extendByte(SET_ENUM_VALUE);
|
|
237
|
+
extendInt(elements.length);
|
|
238
|
+
for (const element of elements) length.value += extendElement(element);
|
|
228
239
|
}
|
|
240
|
+
const contribution = contributions.length;
|
|
241
|
+
indices.set(value, contribution);
|
|
242
|
+
contributions.push(length.value, high, low);
|
|
243
|
+
high = outer.high;
|
|
244
|
+
low = outer.low;
|
|
245
|
+
return contribution;
|
|
246
|
+
};
|
|
247
|
+
return (state: Value[], position: number) => {
|
|
248
|
+
const outer = { high, low };
|
|
249
|
+
high = INITIAL_HIGH;
|
|
250
|
+
low = INITIAL_LOW;
|
|
251
|
+
for (const index of order) extendElement(state[index]);
|
|
252
|
+
words[2 * position] = low;
|
|
253
|
+
words[2 * position + 1] = high;
|
|
229
254
|
high = outer.high;
|
|
230
255
|
low = outer.low;
|
|
231
|
-
words[2 * position] = fingerprint.low;
|
|
232
|
-
words[2 * position + 1] = fingerprint.high;
|
|
233
256
|
};
|
|
234
257
|
};
|
|
235
258
|
|
package/src/values.ts
CHANGED
|
@@ -234,7 +234,13 @@ const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
|
|
|
234
234
|
const rightRange = right.range;
|
|
235
235
|
if (leftRange === rightRange) return 0;
|
|
236
236
|
for (let index = 0; index < leftRange.length; index++) {
|
|
237
|
-
const
|
|
237
|
+
const leftValue = leftRange[index];
|
|
238
|
+
const rightValue = rightRange[index];
|
|
239
|
+
// V8 can't inline the recursive compareValues here, so two numbers skip the call.
|
|
240
|
+
const valueComparison =
|
|
241
|
+
typeof leftValue === "number" && typeof rightValue === "number"
|
|
242
|
+
? leftValue - rightValue
|
|
243
|
+
: compareValues(leftValue, rightValue);
|
|
238
244
|
if (valueComparison !== 0) return valueComparison;
|
|
239
245
|
}
|
|
240
246
|
return 0;
|
|
@@ -1025,10 +1031,7 @@ export const markNormalized = (value: Value): void => {
|
|
|
1025
1031
|
};
|
|
1026
1032
|
|
|
1027
1033
|
export const isEqualLikeTlc = (left: Value, right: Value) =>
|
|
1028
|
-
computeLikeTlcOnFailure(
|
|
1029
|
-
() => valuesEqual(left, right),
|
|
1030
|
-
() => equalsLikeTlc(left, right),
|
|
1031
|
-
);
|
|
1034
|
+
computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
|
|
1032
1035
|
|
|
1033
1036
|
const isStrictlySorted = (elements: Value[]) => {
|
|
1034
1037
|
for (let index = 1; index < elements.length; index++) {
|
|
@@ -1052,9 +1055,11 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
1052
1055
|
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
1053
1056
|
// evaluates.
|
|
1054
1057
|
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
1055
|
-
computeLikeTlcOnFailure<SetValue | MixedSetValue>(
|
|
1056
|
-
|
|
1057
|
-
() => ({ kind: "MixedSet", elements }),
|
|
1058
|
+
computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
|
|
1059
|
+
createSet,
|
|
1060
|
+
(mixed) => ({ kind: "MixedSet", elements: mixed }),
|
|
1061
|
+
elements,
|
|
1062
|
+
undefined,
|
|
1058
1063
|
);
|
|
1059
1064
|
|
|
1060
1065
|
export const EMPTY_SET = createSortedSet([]);
|
|
@@ -1596,6 +1601,38 @@ const getCombinedSets = (value: ObjectValue) =>
|
|
|
1596
1601
|
? [value.range]
|
|
1597
1602
|
: undefined;
|
|
1598
1603
|
|
|
1604
|
+
// Whether a value TLC builds alike at each evaluation and that enumerating, comparing or
|
|
1605
|
+
// fingerprinting it leaves alike, so that one evaluation can stand for the next. It holds no UNION or
|
|
1606
|
+
// \cup, which streams until TLC caches it, no lambda, which fingerprinting turns into a record, and
|
|
1607
|
+
// no record a comparison reorders.
|
|
1608
|
+
export const isSettledValue = (value: Value): boolean => {
|
|
1609
|
+
if (typeof value !== "object") return true;
|
|
1610
|
+
switch (value.kind) {
|
|
1611
|
+
case "ModelValue":
|
|
1612
|
+
case "Interval":
|
|
1613
|
+
return true;
|
|
1614
|
+
case "Set":
|
|
1615
|
+
return value.elements.every(isSettledValue);
|
|
1616
|
+
case "Function":
|
|
1617
|
+
return (
|
|
1618
|
+
value.functionClass !== "lambda" &&
|
|
1619
|
+
value.writtenKeys === undefined &&
|
|
1620
|
+
value.domain.every(isSettledValue) &&
|
|
1621
|
+
value.range.every(isSettledValue)
|
|
1622
|
+
);
|
|
1623
|
+
case "SetOperation":
|
|
1624
|
+
return (
|
|
1625
|
+
value.operator !== "\\cup" && isSettledValue(value.left) && isSettledValue(value.right)
|
|
1626
|
+
);
|
|
1627
|
+
case "FilteredSet":
|
|
1628
|
+
return isSettledValue(value.base);
|
|
1629
|
+
case "ProductSet":
|
|
1630
|
+
return value.factors.every(isSettledValue);
|
|
1631
|
+
default:
|
|
1632
|
+
return false;
|
|
1633
|
+
}
|
|
1634
|
+
};
|
|
1635
|
+
|
|
1599
1636
|
// Whether a set streams its elements in another order than the normalized set, so that a set of
|
|
1600
1637
|
// tuples, records, or functions it is a component of has to stream too.
|
|
1601
1638
|
const isStreamedApart = (value: Value): boolean => {
|
|
@@ -1870,17 +1907,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
|
|
|
1870
1907
|
};
|
|
1871
1908
|
|
|
1872
1909
|
// Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
|
|
1873
|
-
// the failure's leftovers in the replay's trace go first.
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1910
|
+
// the failure's leftovers in the replay's trace go first. Both paths take their operands as
|
|
1911
|
+
// arguments, so a hot caller passes functions it doesn't allocate.
|
|
1912
|
+
export const computeLikeTlcOnFailure = <Left, Right, Result>(
|
|
1913
|
+
compute: (left: Left, right: Right) => Result,
|
|
1914
|
+
computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
|
|
1915
|
+
left: Left,
|
|
1916
|
+
right: Right,
|
|
1877
1917
|
): Result => {
|
|
1878
1918
|
const sets = setTrace.sets;
|
|
1879
1919
|
const callStack = setTrace.callStack;
|
|
1880
1920
|
const depth = sets?.length ?? 0;
|
|
1881
1921
|
const trace = callStack && { ...callStack, length: callStack.expressions.length };
|
|
1882
1922
|
try {
|
|
1883
|
-
return compute();
|
|
1923
|
+
return compute(left, right);
|
|
1884
1924
|
} catch (error) {
|
|
1885
1925
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1886
1926
|
if (sets) sets.length = depth;
|
|
@@ -1889,7 +1929,7 @@ export const computeLikeTlcOnFailure = <Result>(
|
|
|
1889
1929
|
callStack.isFrozen = trace.isFrozen;
|
|
1890
1930
|
callStack.fingerprint = trace.fingerprint;
|
|
1891
1931
|
}
|
|
1892
|
-
return computeLikeTlc(error);
|
|
1932
|
+
return computeLikeTlc(left, right, error);
|
|
1893
1933
|
}
|
|
1894
1934
|
};
|
|
1895
1935
|
|
|
@@ -1902,19 +1942,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1902
1942
|
return isElement;
|
|
1903
1943
|
};
|
|
1904
1944
|
|
|
1945
|
+
const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
|
|
1946
|
+
|
|
1947
|
+
const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
|
|
1948
|
+
normalizedSets.has(set)
|
|
1949
|
+
? searchLikeTlc(element, set.elements).isFound
|
|
1950
|
+
: isElementOfLikeTlc(element, set.elements);
|
|
1951
|
+
|
|
1905
1952
|
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
1906
1953
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1907
1954
|
switch (set.kind) {
|
|
1908
1955
|
case "Set":
|
|
1909
1956
|
// SetEnumValue.member compares the element with the set's.
|
|
1910
1957
|
cacheSet(element);
|
|
1911
|
-
return computeLikeTlcOnFailure(
|
|
1912
|
-
() => findIndex(set.elements, element) !== -1,
|
|
1913
|
-
() =>
|
|
1914
|
-
normalizedSets.has(set)
|
|
1915
|
-
? searchLikeTlc(element, set.elements).isFound
|
|
1916
|
-
: isElementOfLikeTlc(element, set.elements),
|
|
1917
|
-
);
|
|
1958
|
+
return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
|
|
1918
1959
|
case "MixedSet":
|
|
1919
1960
|
return isElementOfLikeTlc(element, set.elements);
|
|
1920
1961
|
case "Interval":
|
|
@@ -2107,17 +2148,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
|
2107
2148
|
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
2108
2149
|
};
|
|
2109
2150
|
|
|
2110
|
-
|
|
2151
|
+
// A key the domain holds itself, which TLC finds without a comparison that can fail.
|
|
2152
|
+
const findIdenticalKey = (domain: Value[], argument: Value) => {
|
|
2111
2153
|
const first = domain[0];
|
|
2112
2154
|
if (typeof argument === "number" && typeof first === "number") {
|
|
2113
2155
|
const offset = argument - first;
|
|
2114
2156
|
if (domain[offset] === argument) return offset;
|
|
2115
2157
|
}
|
|
2116
|
-
|
|
2117
|
-
for (let index = 0; index < domain.length; index++)
|
|
2118
|
-
if (domain[index] === argument) return index;
|
|
2119
|
-
}
|
|
2120
|
-
return findIndex(domain, argument);
|
|
2158
|
+
return domain.length <= 8 ? domain.indexOf(argument) : -1;
|
|
2121
2159
|
};
|
|
2122
2160
|
|
|
2123
2161
|
export const createNonIntegerKeyError = (argument: Value) =>
|
|
@@ -2129,6 +2167,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
|
|
|
2129
2167
|
// binary search with compareTo.
|
|
2130
2168
|
const LINEAR_SEARCH_LIMIT = 32;
|
|
2131
2169
|
|
|
2170
|
+
const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
|
|
2171
|
+
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2172
|
+
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2173
|
+
} else {
|
|
2174
|
+
findIndex(domain, argument, compareLikeTlc);
|
|
2175
|
+
}
|
|
2176
|
+
throw error;
|
|
2177
|
+
};
|
|
2178
|
+
|
|
2132
2179
|
// TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
|
|
2133
2180
|
// wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
|
|
2134
2181
|
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
@@ -2149,17 +2196,9 @@ const findKeyIndexLikeTlc = (
|
|
|
2149
2196
|
throw createNonIntegerKeyError(argument);
|
|
2150
2197
|
}
|
|
2151
2198
|
}
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2156
|
-
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2157
|
-
} else {
|
|
2158
|
-
findIndex(domain, argument, compareLikeTlc);
|
|
2159
|
-
}
|
|
2160
|
-
throw error;
|
|
2161
|
-
},
|
|
2162
|
-
);
|
|
2199
|
+
const identical = findIdenticalKey(domain, argument);
|
|
2200
|
+
if (identical !== -1) return identical;
|
|
2201
|
+
return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
|
|
2163
2202
|
};
|
|
2164
2203
|
|
|
2165
2204
|
export const applyFunction = (
|