tla-ts 0.0.4-dev.a297123 → 0.0.4-dev.e9adbb1
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.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 +45 -45
- package/dist/run-cli.js +45 -45
- 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 +5 -3
- package/src/evaluate.ts +54 -19
- package/src/run-cli.ts +10 -10
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +24 -5
- package/src/tlc-random.ts +55 -32
- package/src/tlc-version.ts +8 -7
- package/src/values.ts +80 -35
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
|
};
|
|
@@ -314,7 +314,9 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
|
|
|
314
314
|
: isReadOnly(expression.function)
|
|
315
315
|
? generate(expression.function, keepLazyFrame)
|
|
316
316
|
: generateDelegate(generator, expression.function, keepLazyFrame);
|
|
317
|
-
|
|
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)}))`;
|
|
318
320
|
const describeMissing = getMissingArgumentDescriber(generator.environment.runtime.version);
|
|
319
321
|
const error = describeMissing ? `, ${addReference(generator, describeMissing)}` : "";
|
|
320
322
|
if (expression.args.length === 1)
|
|
@@ -387,7 +389,7 @@ export const generateEvaluator = (
|
|
|
387
389
|
};
|
|
388
390
|
const body = generateExpression(generator, expression, frame);
|
|
389
391
|
const referenceNames = generator.references.map((_, index) => `reference${index}`);
|
|
390
|
-
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}; };`;
|
|
391
393
|
runtime.value ??= createRuntime();
|
|
392
394
|
const helpers = runtime.value;
|
|
393
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 };
|
|
@@ -847,7 +851,7 @@ const forceThunk = (thunk: Thunk, context: Context) => {
|
|
|
847
851
|
const isSameState = thunk.state === context.state && thunk.next === context.next;
|
|
848
852
|
if (isSameState && thunk.cache !== undefined) return thunk.cache;
|
|
849
853
|
const value = evaluate(thunk.expression, withScope(context, thunk.scope));
|
|
850
|
-
if (isSameState) thunk.cache = value;
|
|
854
|
+
if (isSameState && (!thunk.isStreamed || isSettledValue(value))) thunk.cache = value;
|
|
851
855
|
return value;
|
|
852
856
|
};
|
|
853
857
|
|
|
@@ -968,6 +972,7 @@ const createThunk = (expression: Expression, context: Context): Thunk => ({
|
|
|
968
972
|
state: context.state,
|
|
969
973
|
next: context.next,
|
|
970
974
|
cache: undefined,
|
|
975
|
+
isStreamed: false,
|
|
971
976
|
});
|
|
972
977
|
|
|
973
978
|
export const findGlobalName = (name: string, environment: Environment): GlobalName | undefined => {
|
|
@@ -1027,6 +1032,7 @@ export const bindOperatorArguments = (
|
|
|
1027
1032
|
closure: Closure,
|
|
1028
1033
|
args: Expression[],
|
|
1029
1034
|
context: Context,
|
|
1035
|
+
binders?: ArgumentBinder[],
|
|
1030
1036
|
): Scope | undefined => {
|
|
1031
1037
|
if (args.length !== closure.params.length) {
|
|
1032
1038
|
throw new TlaEvaluationError(
|
|
@@ -1039,18 +1045,36 @@ export const bindOperatorArguments = (
|
|
|
1039
1045
|
const binding: Binding =
|
|
1040
1046
|
param.arity > 0
|
|
1041
1047
|
? { kind: "Operator", operator: evaluateOperatorArgument(args[index], context) }
|
|
1042
|
-
: getArgumentBinder(args[index], context.environment)(context);
|
|
1048
|
+
: (binders?.[index] ?? getArgumentBinder(args[index], context.environment))(context);
|
|
1043
1049
|
scope = { name: param.name, binding, parent: scope };
|
|
1044
1050
|
}
|
|
1045
1051
|
return scope;
|
|
1046
1052
|
};
|
|
1047
1053
|
|
|
1048
|
-
|
|
1054
|
+
// The body a call site last called and its evaluator, as a site mostly calls one closure.
|
|
1055
|
+
interface CalledBody {
|
|
1056
|
+
body: Expression;
|
|
1057
|
+
evaluator: Evaluator;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
const callOperator = (
|
|
1061
|
+
operator: Operator,
|
|
1062
|
+
args: Expression[],
|
|
1063
|
+
context: Context,
|
|
1064
|
+
called: { entry: CalledBody | undefined },
|
|
1065
|
+
binders: ArgumentBinder[],
|
|
1066
|
+
) => {
|
|
1049
1067
|
if (operator.kind === "Closure") {
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1068
|
+
const bodyContext = withScope(context, bindOperatorArguments(operator, args, context, binders));
|
|
1069
|
+
if (context.environment.callStack) return evaluate(operator.body, bodyContext);
|
|
1070
|
+
const entry =
|
|
1071
|
+
called.entry?.body === operator.body
|
|
1072
|
+
? called.entry
|
|
1073
|
+
: (called.entry = {
|
|
1074
|
+
body: operator.body,
|
|
1075
|
+
evaluator: getEntryEvaluator(operator.body, context.environment),
|
|
1076
|
+
});
|
|
1077
|
+
return entry.evaluator(bodyContext);
|
|
1054
1078
|
}
|
|
1055
1079
|
if (operator.kind === "Operator") return operator.call(args.map((arg) => evaluate(arg, context)));
|
|
1056
1080
|
if (operator.isLazy) return operator.evaluate(getLazyArguments(args, context), context);
|
|
@@ -1064,10 +1088,16 @@ const callOperator = (operator: Operator, args: Expression[], context: Context)
|
|
|
1064
1088
|
);
|
|
1065
1089
|
};
|
|
1066
1090
|
|
|
1091
|
+
// The LET definitions that a reference evaluates again, and those that cache only a settled value.
|
|
1092
|
+
interface ReevaluatedDefinitions {
|
|
1093
|
+
impure: ReadonlySet<Definition>;
|
|
1094
|
+
streamed: ReadonlySet<Definition>;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1067
1097
|
export const bindDefinitions = (
|
|
1068
1098
|
definitions: Definition[],
|
|
1069
1099
|
context: Context,
|
|
1070
|
-
reevaluated?:
|
|
1100
|
+
reevaluated?: ReevaluatedDefinitions,
|
|
1071
1101
|
): Scope | undefined =>
|
|
1072
1102
|
definitions.reduce<Scope | undefined>((scope, definition) => {
|
|
1073
1103
|
const node: Scope = {
|
|
@@ -1089,10 +1119,11 @@ export const bindDefinitions = (
|
|
|
1089
1119
|
state: context.state,
|
|
1090
1120
|
next: context.next,
|
|
1091
1121
|
cache: undefined,
|
|
1122
|
+
isStreamed: false,
|
|
1092
1123
|
};
|
|
1093
1124
|
return node;
|
|
1094
1125
|
}
|
|
1095
|
-
if (definition.params.length > 0 || reevaluated?.has(definition)) {
|
|
1126
|
+
if (definition.params.length > 0 || reevaluated?.impure.has(definition)) {
|
|
1096
1127
|
node.binding = {
|
|
1097
1128
|
kind: "Operator",
|
|
1098
1129
|
operator: {
|
|
@@ -1112,6 +1143,7 @@ export const bindDefinitions = (
|
|
|
1112
1143
|
state: context.state,
|
|
1113
1144
|
next: context.next,
|
|
1114
1145
|
cache: undefined,
|
|
1146
|
+
isStreamed: reevaluated?.streamed.has(definition) ?? false,
|
|
1115
1147
|
};
|
|
1116
1148
|
return node;
|
|
1117
1149
|
}, context.scope);
|
|
@@ -1228,14 +1260,15 @@ const STREAMED_SET: Impurity = {
|
|
|
1228
1260
|
// TLC's next-state generator does not bind LET definitions to values, so each reference to one
|
|
1229
1261
|
// evaluates it again: one that calls an impure builtin repeats its effects, and a set one builds
|
|
1230
1262
|
// with UNION or \cup starts out uncached.
|
|
1231
|
-
export const getReevaluatedDefinitions = (
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
),
|
|
1238
|
-
)
|
|
1263
|
+
export const getReevaluatedDefinitions = (
|
|
1264
|
+
definitions: Definition[],
|
|
1265
|
+
environment: Environment,
|
|
1266
|
+
): ReevaluatedDefinitions => ({
|
|
1267
|
+
impure: new Set(definitions.filter((definition) => isImpureDefinition(definition, environment))),
|
|
1268
|
+
streamed: new Set(
|
|
1269
|
+
definitions.filter((definition) => isImpureDefinition(definition, environment, STREAMED_SET)),
|
|
1270
|
+
),
|
|
1271
|
+
});
|
|
1239
1272
|
|
|
1240
1273
|
export const isImpure = (environment: Environment) =>
|
|
1241
1274
|
[...environment.definitions.values()].some((definition) =>
|
|
@@ -1519,7 +1552,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1519
1552
|
|
|
1520
1553
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1521
1554
|
|
|
1522
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1555
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1523
1556
|
if (environment.callStack) return getEvaluator(expression, environment);
|
|
1524
1557
|
const cached = entryEvaluators.get(expression);
|
|
1525
1558
|
if (cached) return cached;
|
|
@@ -1986,9 +2019,11 @@ const compileApply = (
|
|
|
1986
2019
|
);
|
|
1987
2020
|
};
|
|
1988
2021
|
}
|
|
2022
|
+
const called = { entry: undefined as CalledBody | undefined };
|
|
2023
|
+
const binders = args.map((arg) => getArgumentBinder(arg, environment));
|
|
1989
2024
|
return (context) => {
|
|
1990
2025
|
const operator = resolveOperator(expression.operator, context);
|
|
1991
|
-
if (operator) return callOperator(operator, args, context);
|
|
2026
|
+
if (operator) return callOperator(operator, args, context, called, binders);
|
|
1992
2027
|
const definition = environment.definitions.get(expression.operator);
|
|
1993
2028
|
if (definition?.kind === "Function") {
|
|
1994
2029
|
throw new TlaEvaluationError(`${expression.operator} is a function; apply it with brackets`);
|
package/src/run-cli.ts
CHANGED
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
writeFileSync,
|
|
9
9
|
} from "node:fs";
|
|
10
10
|
import { spawnSync } from "node:child_process";
|
|
11
|
-
import { createHash } from "node:crypto";
|
|
12
11
|
import { basename, delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
|
13
12
|
import { arch, availableParallelism, release, tmpdir, type } from "node:os";
|
|
14
13
|
import { pathToFileURL } from "node:url";
|
|
@@ -59,6 +58,7 @@ import type { SanySpecification } from "./sany-generator";
|
|
|
59
58
|
import { SANY_174_STANDARD_MODULES, SANY_STANDARD_MODULES } from "./sany-modules";
|
|
60
59
|
import { getSanyModuleName, getSanyReport, type SanyModuleOrigin } from "./sany-report";
|
|
61
60
|
import { exportSanyXml, XML_EXPORT_EXIT_CODE, type XmlExportOptions } from "./sany-xml";
|
|
61
|
+
import { getTlcFingerprint } from "./tlc-random";
|
|
62
62
|
import { getTlcVersion, type TlcVersion } from "./tlc-version";
|
|
63
63
|
import {
|
|
64
64
|
createMessagePrinter,
|
|
@@ -705,9 +705,9 @@ const getStandardModulesUrl = (jar: string) =>
|
|
|
705
705
|
`jar:file:${pathToFileURL(resolve(jar)).pathname}!/tla2sany/StandardModules/`;
|
|
706
706
|
|
|
707
707
|
// Java's Files.createTempDirectory names a directory by a random unsigned long. tla-ts derives it
|
|
708
|
-
// from what the directory holds, so that each run of a spec prints the same
|
|
709
|
-
|
|
710
|
-
|
|
708
|
+
// from TLC's fingerprint of what the directory holds, so that each run of a spec prints the same
|
|
709
|
+
// files.
|
|
710
|
+
const getTemporaryName = (contents: string) => getTlcFingerprint([contents]).toString();
|
|
711
711
|
|
|
712
712
|
// The file TLC's SANY says it parses each module from: the real path of a module's own file, or a
|
|
713
713
|
// copy in a temporary directory of a module from the root module's file or from the jar on the
|
|
@@ -1247,7 +1247,7 @@ const checkSpecs = async (args: string[], java: JavaProperties) => {
|
|
|
1247
1247
|
? path
|
|
1248
1248
|
: (java.libraries.map((directory) => join(directory, path)).find(existsSync) ?? path);
|
|
1249
1249
|
const paths = [...new Set(specPaths.map(findSpec))];
|
|
1250
|
-
const tlcVersion =
|
|
1250
|
+
const tlcVersion = java.tlcVersion ?? "master";
|
|
1251
1251
|
return runEach(
|
|
1252
1252
|
paths,
|
|
1253
1253
|
(path, printer) => {
|
|
@@ -1488,9 +1488,6 @@ interface JavaProperties {
|
|
|
1488
1488
|
const getJar = (java: JavaProperties) =>
|
|
1489
1489
|
java.classPath?.split(delimiter).find((entry) => entry.endsWith(".jar"));
|
|
1490
1490
|
|
|
1491
|
-
// The TLC that -Dtla-ts.tlc names, or else the one of the jar on the class path.
|
|
1492
|
-
const getJavaTlcVersion = (java: JavaProperties) => java.tlcVersion ?? getTlcVersion(getJar(java));
|
|
1493
|
-
|
|
1494
1491
|
interface JavaLaunch {
|
|
1495
1492
|
mainClass: string;
|
|
1496
1493
|
args: string[];
|
|
@@ -1525,7 +1522,7 @@ const runJavaClass = (launch: JavaLaunch) => {
|
|
|
1525
1522
|
if (launch.mainClass === "tlc2.TLC") return checkSpecs(launch.args, launch.java);
|
|
1526
1523
|
if (launch.mainClass === "tla2sany.xml.XMLExporter") {
|
|
1527
1524
|
return reportErrors(() =>
|
|
1528
|
-
|
|
1525
|
+
launch.java.tlcVersion === "1.7.4"
|
|
1529
1526
|
? exportXml174(launch.args, getJar(launch.java))
|
|
1530
1527
|
: exportXml(launch.args),
|
|
1531
1528
|
);
|
|
@@ -1539,7 +1536,10 @@ const runJavaClass = (launch: JavaLaunch) => {
|
|
|
1539
1536
|
const run = async (args: string[]) => {
|
|
1540
1537
|
if (args[0] === "prove") return reportErrors(() => prove(args.slice(1)));
|
|
1541
1538
|
const launch = getJavaLaunch(args);
|
|
1542
|
-
|
|
1539
|
+
if (launch === undefined) return checkSpecs(args, { libraries: [] });
|
|
1540
|
+
// The TLC that -Dtla-ts.tlc names, or else the one of the jar on the class path.
|
|
1541
|
+
launch.java.tlcVersion ??= await getTlcVersion(getJar(launch.java));
|
|
1542
|
+
return runJavaClass(launch);
|
|
1543
1543
|
};
|
|
1544
1544
|
|
|
1545
1545
|
export const runCli = async () => {
|
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/tlc-version.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { strFromU8, unzipSync } from "fflate";
|
|
3
2
|
|
|
4
3
|
// The TLC whose output tla-ts reproduces: the 1.7.4 release, or the master builds that tla-ts
|
|
5
4
|
// follows for any other tla2tools.jar.
|
|
@@ -7,12 +6,14 @@ export type TlcVersion = "1.7.4" | "master";
|
|
|
7
6
|
|
|
8
7
|
const MANIFEST = "META-INF/MANIFEST.MF";
|
|
9
8
|
|
|
10
|
-
// The version of a tla2tools.jar, from the Git tag that its manifest records.
|
|
11
|
-
|
|
9
|
+
// The version of a tla2tools.jar, from the Git tag that its manifest records. fflate loads
|
|
10
|
+
// node:worker_threads, so only a run with a jar imports it.
|
|
11
|
+
export const getTlcVersion = async (jar: string | undefined): Promise<TlcVersion> => {
|
|
12
12
|
if (jar === undefined || !existsSync(jar)) return "master";
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const fflate = await import("fflate");
|
|
14
|
+
const manifest = fflate.unzipSync(readFileSync(jar), {
|
|
15
|
+
filter: (file) => file.name === MANIFEST,
|
|
16
|
+
})[MANIFEST];
|
|
17
|
+
const tag = manifest && /^X-Git-Tag: *(\S*)/m.exec(fflate.strFromU8(manifest))?.[1];
|
|
17
18
|
return tag === "v1.7.4" ? "1.7.4" : "master";
|
|
18
19
|
};
|