tla-ts 0.0.2 → 0.0.3-dev.252cf06
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -11
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +52 -1
- package/dist/index.d.ts +52 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +21 -2
- package/dist/run-cli.js +21 -2
- package/dist/transfer.cjs +19 -15
- package/dist/transfer.js +19 -15
- package/package.json +3 -1
- package/src/ast.ts +134 -4
- package/src/check.ts +120 -40
- package/src/cli-options.ts +58 -0
- package/src/codegen.ts +1 -3
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +10 -10
- package/src/errors.ts +42 -1
- package/src/evaluate.ts +57 -114
- package/src/exit-codes.ts +20 -30
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +12 -11
- package/src/levels.ts +8 -8
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +5 -523
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +166 -210
- package/src/parallel.ts +73 -35
- package/src/parser.ts +297 -53
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +304 -29
- package/src/runtime.ts +1 -1
- package/src/spec-description.ts +7 -2
- package/src/standard-levels.ts +3 -3
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +17 -9
- package/src/symmetry.ts +14 -2
- package/src/tlc-ext.ts +2 -10
- package/src/tlc-order.ts +17 -3
- package/src/transfer.ts +22 -1
- package/src/values.ts +59 -23
package/src/errors.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { EXIT_STATUS, getExitCode, setModuleExitCode } from "./exit-codes";
|
|
2
2
|
|
|
3
|
+
// How TLC reports a RuntimeException it did not expect, such as one raised while computing next
|
|
4
|
+
// states.
|
|
5
|
+
export const UNEXPECTED_EXCEPTION =
|
|
6
|
+
"TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ";
|
|
7
|
+
|
|
3
8
|
export class TlaError extends Error {
|
|
4
9
|
constructor(message: string, cause?: unknown) {
|
|
5
10
|
super(message, { cause });
|
|
@@ -7,10 +12,20 @@ export class TlaError extends Error {
|
|
|
7
12
|
}
|
|
8
13
|
}
|
|
9
14
|
|
|
15
|
+
export interface SyntaxFailure {
|
|
16
|
+
reason: string;
|
|
17
|
+
line: number;
|
|
18
|
+
column: number;
|
|
19
|
+
isEnd: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
export class TlaSyntaxError extends TlaError {
|
|
11
|
-
|
|
23
|
+
failure: SyntaxFailure | undefined;
|
|
24
|
+
|
|
25
|
+
constructor(message: string, cause?: unknown, failure?: SyntaxFailure) {
|
|
12
26
|
super(message, cause);
|
|
13
27
|
this.name = "TlaSyntaxError";
|
|
28
|
+
this.failure = failure;
|
|
14
29
|
}
|
|
15
30
|
}
|
|
16
31
|
|
|
@@ -38,6 +53,32 @@ export class TlaModuleError extends TlaEvaluationError {
|
|
|
38
53
|
}
|
|
39
54
|
}
|
|
40
55
|
|
|
56
|
+
export interface TlapmReport {
|
|
57
|
+
failure?: string;
|
|
58
|
+
exception?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class TlaProofError extends TlaError {
|
|
62
|
+
failure: string | undefined;
|
|
63
|
+
exception: string;
|
|
64
|
+
|
|
65
|
+
constructor(message: string, report: TlapmReport = {}) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.name = "TlaProofError";
|
|
68
|
+
this.failure = report.exception === undefined ? (report.failure ?? message) : undefined;
|
|
69
|
+
this.exception = report.exception ?? `Failure(${JSON.stringify(this.failure)})`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// tlapm fails one of its own assertions, which it reports as a bug in itself. It keeps the name
|
|
74
|
+
// TlaProofError, and index.ts doesn't export it.
|
|
75
|
+
export class TlapmBugError extends TlaProofError {
|
|
76
|
+
constructor(exception = "Assert_failure") {
|
|
77
|
+
super("Oops, this seems to be a bug in TLAPM.", { exception });
|
|
78
|
+
this.name = "TlaProofError";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
41
82
|
export class TlaUnsupportedError extends TlaError {
|
|
42
83
|
constructor(message: string, cause?: unknown) {
|
|
43
84
|
super(message, cause);
|
package/src/evaluate.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type DecimalExpression,
|
|
7
|
+
type Definition,
|
|
8
|
+
type Expression,
|
|
9
|
+
type FunctionDefinition,
|
|
10
|
+
type NumberExpression,
|
|
11
|
+
type OperatorParam,
|
|
12
|
+
type RecordField,
|
|
13
|
+
type SourceRange,
|
|
12
14
|
} from "./ast";
|
|
13
15
|
import { getKindString } from "./builtins";
|
|
14
16
|
import { TlaEvaluationError } from "./errors";
|
|
@@ -262,96 +264,6 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
262
264
|
return value;
|
|
263
265
|
};
|
|
264
266
|
|
|
265
|
-
export const getChildren = (expression: Expression): Expression[] => {
|
|
266
|
-
switch (expression.kind) {
|
|
267
|
-
case "Number":
|
|
268
|
-
case "Decimal":
|
|
269
|
-
case "String":
|
|
270
|
-
case "Identifier":
|
|
271
|
-
case "OperatorReference":
|
|
272
|
-
case "ExceptAt":
|
|
273
|
-
return [];
|
|
274
|
-
case "Apply":
|
|
275
|
-
return expression.args;
|
|
276
|
-
case "Lambda":
|
|
277
|
-
return [expression.body];
|
|
278
|
-
case "And":
|
|
279
|
-
case "Or":
|
|
280
|
-
return expression.operands;
|
|
281
|
-
case "Prime":
|
|
282
|
-
case "Unchanged":
|
|
283
|
-
case "Enabled":
|
|
284
|
-
case "Always":
|
|
285
|
-
case "Eventually":
|
|
286
|
-
return [expression.expression];
|
|
287
|
-
case "If":
|
|
288
|
-
return [expression.condition, expression.consequent, expression.alternative];
|
|
289
|
-
case "Case":
|
|
290
|
-
return [
|
|
291
|
-
...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
|
|
292
|
-
...(expression.other ? [expression.other] : []),
|
|
293
|
-
];
|
|
294
|
-
case "Let":
|
|
295
|
-
return [
|
|
296
|
-
...expression.definitions.flatMap((definition) =>
|
|
297
|
-
definition.kind === "Function"
|
|
298
|
-
? [...definition.bounds.map((bound) => bound.set), definition.body]
|
|
299
|
-
: [definition.body],
|
|
300
|
-
),
|
|
301
|
-
expression.body,
|
|
302
|
-
];
|
|
303
|
-
case "Forall":
|
|
304
|
-
case "Exists":
|
|
305
|
-
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
306
|
-
case "UnboundedQuantifier":
|
|
307
|
-
return [expression.body];
|
|
308
|
-
case "Choose":
|
|
309
|
-
return [...(expression.set ? [expression.set] : []), expression.body];
|
|
310
|
-
case "SetEnumeration":
|
|
311
|
-
case "Tuple":
|
|
312
|
-
return expression.elements;
|
|
313
|
-
case "SetFilter":
|
|
314
|
-
return [expression.bound.set, expression.predicate];
|
|
315
|
-
case "SetMap":
|
|
316
|
-
return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
|
|
317
|
-
case "FunctionConstruction":
|
|
318
|
-
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
319
|
-
case "FunctionSet":
|
|
320
|
-
return [expression.domain, expression.range];
|
|
321
|
-
case "Record":
|
|
322
|
-
case "RecordSet":
|
|
323
|
-
return expression.fields.map((field) => field.value);
|
|
324
|
-
case "Except":
|
|
325
|
-
return [
|
|
326
|
-
expression.function,
|
|
327
|
-
...expression.updates.flatMap((update) => [
|
|
328
|
-
...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
|
|
329
|
-
update.value,
|
|
330
|
-
]),
|
|
331
|
-
];
|
|
332
|
-
case "FunctionApplication":
|
|
333
|
-
return [expression.function, ...expression.args];
|
|
334
|
-
case "FieldAccess":
|
|
335
|
-
return [expression.record];
|
|
336
|
-
case "Product":
|
|
337
|
-
return expression.factors;
|
|
338
|
-
case "BoxAction":
|
|
339
|
-
case "AngleAction":
|
|
340
|
-
return [expression.action, expression.subscript];
|
|
341
|
-
case "WeakFairness":
|
|
342
|
-
case "StrongFairness":
|
|
343
|
-
return [expression.subscript, expression.action];
|
|
344
|
-
case "Label":
|
|
345
|
-
return [expression.body];
|
|
346
|
-
case "Subexpression":
|
|
347
|
-
return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
export const isLocallyResolved = (expression: Expression) =>
|
|
352
|
-
(expression.kind === "Identifier" || expression.kind === "Apply") &&
|
|
353
|
-
expression.resolution?.kind === "Local";
|
|
354
|
-
|
|
355
267
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
356
268
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
357
269
|
|
|
@@ -734,7 +646,9 @@ const compileArgumentBinder = (argument: Expression, environment: Environment):
|
|
|
734
646
|
if (global === undefined) {
|
|
735
647
|
return (context) => {
|
|
736
648
|
const binding = lookupScope(context.scope, argument.name);
|
|
737
|
-
return binding?.kind === "Value"
|
|
649
|
+
return binding?.kind === "Value" || binding?.kind === "Thunk"
|
|
650
|
+
? binding
|
|
651
|
+
: createThunk(argument, context);
|
|
738
652
|
};
|
|
739
653
|
}
|
|
740
654
|
}
|
|
@@ -887,14 +801,29 @@ const IMPURE_BUILTINS = new Set([
|
|
|
887
801
|
export const isImpureBuiltin = (name: string, environment: Environment) =>
|
|
888
802
|
IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
889
803
|
|
|
890
|
-
|
|
804
|
+
interface Impurity {
|
|
805
|
+
builtins: Set<string>;
|
|
806
|
+
definitions: WeakMap<Definition, boolean>;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const ANY_EFFECT: Impurity = { builtins: IMPURE_BUILTINS, definitions: new WeakMap() };
|
|
810
|
+
|
|
811
|
+
// An Assert that held holds again, so evaluating it twice looks the same as evaluating it once.
|
|
812
|
+
const REPEATED_EFFECT: Impurity = {
|
|
813
|
+
builtins: new Set([...IMPURE_BUILTINS].filter((name) => name !== "Assert")),
|
|
814
|
+
definitions: new WeakMap(),
|
|
815
|
+
};
|
|
891
816
|
|
|
892
817
|
const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
|
|
893
818
|
definition.kind === "Operator" &&
|
|
894
819
|
definition.params.length === 0 &&
|
|
895
820
|
isConstantLevel(definition, environment);
|
|
896
821
|
|
|
897
|
-
const callsImpureBuiltin = (
|
|
822
|
+
const callsImpureBuiltin = (
|
|
823
|
+
expression: Expression,
|
|
824
|
+
environment: Environment,
|
|
825
|
+
impurity: Impurity,
|
|
826
|
+
): boolean => {
|
|
898
827
|
const name =
|
|
899
828
|
expression.kind === "Identifier"
|
|
900
829
|
? expression.name
|
|
@@ -903,29 +832,35 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
|
|
|
903
832
|
: undefined;
|
|
904
833
|
if (name !== undefined && !isLocallyResolved(expression)) {
|
|
905
834
|
const definition = getDefinition(name, environment);
|
|
906
|
-
|
|
835
|
+
const isImpureCall =
|
|
836
|
+
impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
837
|
+
if (!definition && isImpureCall) return true;
|
|
907
838
|
if (
|
|
908
839
|
definition &&
|
|
909
840
|
!isEvaluatedOnce(definition, environment) &&
|
|
910
|
-
isImpureDefinition(definition, environment)
|
|
841
|
+
isImpureDefinition(definition, environment, impurity)
|
|
911
842
|
) {
|
|
912
843
|
return true;
|
|
913
844
|
}
|
|
914
845
|
}
|
|
915
|
-
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
|
|
846
|
+
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
|
|
916
847
|
};
|
|
917
848
|
|
|
918
849
|
// TLC evaluates a constant definition without parameters once, so only other definitions pass
|
|
919
850
|
// on the side effects and random choices of the builtins they call.
|
|
920
|
-
const isImpureDefinition = (
|
|
921
|
-
|
|
851
|
+
const isImpureDefinition = (
|
|
852
|
+
definition: Definition,
|
|
853
|
+
environment: Environment,
|
|
854
|
+
impurity = ANY_EFFECT,
|
|
855
|
+
): boolean => {
|
|
856
|
+
const cached = impurity.definitions.get(definition);
|
|
922
857
|
if (cached !== undefined) return cached;
|
|
923
|
-
|
|
858
|
+
impurity.definitions.set(definition, false);
|
|
924
859
|
const isImpure =
|
|
925
|
-
callsImpureBuiltin(definition.body, environment) ||
|
|
860
|
+
callsImpureBuiltin(definition.body, environment, impurity) ||
|
|
926
861
|
(definition.kind === "Function" &&
|
|
927
|
-
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
|
|
928
|
-
|
|
862
|
+
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
|
|
863
|
+
impurity.definitions.set(definition, isImpure);
|
|
929
864
|
return isImpure;
|
|
930
865
|
};
|
|
931
866
|
|
|
@@ -939,6 +874,12 @@ export const isImpure = (environment: Environment) =>
|
|
|
939
874
|
isImpureDefinition(definition, environment),
|
|
940
875
|
);
|
|
941
876
|
|
|
877
|
+
// Whether evaluating a definition again can show, such as by printing twice.
|
|
878
|
+
export const hasRepeatedEffects = (environment: Environment) =>
|
|
879
|
+
[...environment.definitions.values()].some((definition) =>
|
|
880
|
+
isImpureDefinition(definition, environment, REPEATED_EFFECT),
|
|
881
|
+
);
|
|
882
|
+
|
|
942
883
|
const closedConstants = new WeakMap<Expression, boolean>();
|
|
943
884
|
|
|
944
885
|
export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
|
|
@@ -1755,7 +1696,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1755
1696
|
if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
|
|
1756
1697
|
}
|
|
1757
1698
|
if (other) return other(context);
|
|
1758
|
-
throw new TlaEvaluationError(
|
|
1699
|
+
throw new TlaEvaluationError(
|
|
1700
|
+
`Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
1701
|
+
);
|
|
1759
1702
|
};
|
|
1760
1703
|
}
|
|
1761
1704
|
case "Let": {
|
package/src/exit-codes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LivenessChecker } from "./
|
|
1
|
+
import type { LivenessChecker } from "./live-check";
|
|
2
2
|
|
|
3
3
|
// TLC's exit statuses, from EC.ExitStatus.
|
|
4
4
|
export const EXIT_STATUS = {
|
|
@@ -39,37 +39,27 @@ export const setModuleExitCode = (error: Error, code: number) => {
|
|
|
39
39
|
export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
|
|
40
40
|
|
|
41
41
|
// TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
|
|
42
|
-
|
|
43
|
-
create: () => LivenessChecker | undefined,
|
|
44
|
-
): LivenessChecker | undefined => {
|
|
42
|
+
const withErrorExitCode = <Result>(run: () => Result) => {
|
|
45
43
|
try {
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
checker && {
|
|
49
|
-
addInitialState: (index, state) => {
|
|
50
|
-
try {
|
|
51
|
-
checker.addInitialState(index, state);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
addState: (index, state, successors) => {
|
|
57
|
-
try {
|
|
58
|
-
return checker.addState(index, state, successors);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
findViolation: () => {
|
|
64
|
-
try {
|
|
65
|
-
return checker.findViolation();
|
|
66
|
-
} catch (error) {
|
|
67
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
}
|
|
71
|
-
);
|
|
44
|
+
return run();
|
|
72
45
|
} catch (error) {
|
|
73
46
|
throw setExitCode(error, EXIT_STATUS.error);
|
|
74
47
|
}
|
|
75
48
|
};
|
|
49
|
+
|
|
50
|
+
export const withLivenessExitCode = (
|
|
51
|
+
create: () => LivenessChecker | undefined,
|
|
52
|
+
): LivenessChecker | undefined => {
|
|
53
|
+
const checker = withErrorExitCode(create);
|
|
54
|
+
return (
|
|
55
|
+
checker && {
|
|
56
|
+
addInitialState: (index, state) =>
|
|
57
|
+
withErrorExitCode(() => checker.addInitialState(index, state)),
|
|
58
|
+
addState: (index, state, successors) =>
|
|
59
|
+
withErrorExitCode(() => checker.addState(index, state, successors)),
|
|
60
|
+
findViolation: () => withErrorExitCode(() => checker.findViolation()),
|
|
61
|
+
getViolatedProperties: (path, cycleStart) =>
|
|
62
|
+
withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
};
|
package/src/fingerprints.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { getValueFingerprint, getValueHash, type Value } from "./values";
|
|
2
2
|
|
|
3
3
|
const EMPTY = 0;
|
|
4
|
-
const TOMBSTONE = 1;
|
|
5
4
|
const INITIAL_CAPACITY = 1 << 12;
|
|
6
5
|
|
|
7
6
|
interface FingerprintTable {
|
|
8
7
|
insert: (first: number, second: number) => boolean;
|
|
9
|
-
remove: (first: number, second: number) => void;
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
const mix = (hash: number) => {
|
|
@@ -26,15 +24,13 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
|
|
|
26
24
|
fingerprint[1] = mix(second);
|
|
27
25
|
};
|
|
28
26
|
|
|
29
|
-
export const getFingerprintOwner = (second: number, count: number) => (second >>> 0) % count;
|
|
30
|
-
|
|
31
27
|
export const createFingerprintTable = (): FingerprintTable => {
|
|
32
28
|
const table = { slots: new Int32Array(INITIAL_CAPACITY * 2), used: 0 };
|
|
33
29
|
const findSlot = (slots: Int32Array, first: number, second: number) => {
|
|
34
30
|
const mask = slots.length / 2 - 1;
|
|
35
31
|
for (let index = first & mask; ; index = (index + 1) & mask) {
|
|
36
32
|
const stored = slots[index * 2];
|
|
37
|
-
if (stored === EMPTY
|
|
33
|
+
if (stored === EMPTY) return index;
|
|
38
34
|
if (stored === first && slots[index * 2 + 1] === second) return index;
|
|
39
35
|
}
|
|
40
36
|
};
|
|
@@ -59,11 +55,50 @@ export const createFingerprintTable = (): FingerprintTable => {
|
|
|
59
55
|
table.used++;
|
|
60
56
|
return true;
|
|
61
57
|
},
|
|
62
|
-
remove: (first, second) => {
|
|
63
|
-
const slot = findSlot(table.slots, first, second);
|
|
64
|
-
if (table.slots[slot * 2] !== first) return;
|
|
65
|
-
table.slots[slot * 2] = EMPTY;
|
|
66
|
-
table.slots[slot * 2 + 1] = TOMBSTONE;
|
|
67
|
-
},
|
|
68
58
|
};
|
|
69
59
|
};
|
|
60
|
+
|
|
61
|
+
// Workers share one table of 64-bit fingerprints. The first word is never 0, so 0 marks an empty slot.
|
|
62
|
+
export interface SharedFingerprints {
|
|
63
|
+
slots: BigInt64Array;
|
|
64
|
+
used: Int32Array;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const INITIAL_SHARED_CAPACITY = 1 << 10;
|
|
68
|
+
|
|
69
|
+
const insertSharedKey = (slots: BigInt64Array, key: bigint) => {
|
|
70
|
+
const mask = slots.length - 1;
|
|
71
|
+
for (let index = Number(key >> 32n) & mask; ; index = (index + 1) & mask) {
|
|
72
|
+
const stored = Atomics.load(slots, index);
|
|
73
|
+
if (stored === key) return false;
|
|
74
|
+
if (stored !== 0n) continue;
|
|
75
|
+
const previous = Atomics.compareExchange(slots, index, 0n, key);
|
|
76
|
+
if (previous === 0n) return true;
|
|
77
|
+
if (previous === key) return false;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const createSharedFingerprints = (): SharedFingerprints => ({
|
|
82
|
+
slots: new BigInt64Array(new SharedArrayBuffer(INITIAL_SHARED_CAPACITY * 8)),
|
|
83
|
+
used: new Int32Array(new SharedArrayBuffer(4)),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const insertSharedFingerprint = (
|
|
87
|
+
table: SharedFingerprints,
|
|
88
|
+
first: number,
|
|
89
|
+
second: number,
|
|
90
|
+
) => {
|
|
91
|
+
const key = BigInt.asIntN(64, (BigInt(first) << 32n) | BigInt(second >>> 0));
|
|
92
|
+
if (!insertSharedKey(table.slots, key)) return false;
|
|
93
|
+
Atomics.add(table.used, 0, 1);
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const isSharedFingerprintsFull = (table: SharedFingerprints) =>
|
|
98
|
+
Atomics.load(table.used, 0) * 2 >= table.slots.length;
|
|
99
|
+
|
|
100
|
+
export const growSharedFingerprints = (table: SharedFingerprints): SharedFingerprints => {
|
|
101
|
+
const slots = new BigInt64Array(new SharedArrayBuffer(table.slots.byteLength * 2));
|
|
102
|
+
for (const key of table.slots) if (key !== 0n) insertSharedKey(slots, key);
|
|
103
|
+
return { slots, used: table.used };
|
|
104
|
+
};
|
package/src/instances.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type Definition,
|
|
7
|
+
type Expression,
|
|
8
|
+
type Instance,
|
|
9
|
+
type Module,
|
|
10
|
+
type OperatorParam,
|
|
11
|
+
type SourceLocation,
|
|
12
|
+
type Theorem,
|
|
11
13
|
} from "./ast";
|
|
12
14
|
import { TlaSyntaxError } from "./errors";
|
|
13
|
-
import { getChildren, isLocallyResolved } from "./evaluate";
|
|
14
15
|
import { getArity, getBoundNames, getPatternNames, isBefore } from "./resolve";
|
|
15
16
|
|
|
16
17
|
export const substitutedExpressions = new WeakSet<Expression>();
|
package/src/levels.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type AssumeProve,
|
|
4
|
+
type Definition,
|
|
5
|
+
type Expression,
|
|
6
|
+
type Module,
|
|
7
|
+
type OperatorParam,
|
|
8
|
+
type SourceLocation,
|
|
8
9
|
} from "./ast";
|
|
9
10
|
import { TlaSyntaxError } from "./errors";
|
|
10
|
-
import { getChildren } from "./evaluate";
|
|
11
11
|
import { mapChildren } from "./instances";
|
|
12
12
|
import { getBoundNames, getPatternNames } from "./resolve";
|
|
13
13
|
import { STANDARD_LEVELS } from "./standard-levels";
|
package/src/lexer.ts
CHANGED
|
@@ -345,6 +345,8 @@ export const tokenize = (source: string): Token[] => {
|
|
|
345
345
|
if (!headerMatch)
|
|
346
346
|
throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
|
|
347
347
|
const tokens: Token[] = [];
|
|
348
|
+
// Each name is one string object, so looking a name up in a scope compares pointers.
|
|
349
|
+
const names = new Map<string, string>();
|
|
348
350
|
const prefixLines = source.slice(0, headerMatch.index).split("\n");
|
|
349
351
|
const cursor = {
|
|
350
352
|
index: headerMatch.index,
|
|
@@ -380,7 +382,12 @@ export const tokenize = (source: string): Token[] => {
|
|
|
380
382
|
}
|
|
381
383
|
};
|
|
382
384
|
const createError = (message: string) =>
|
|
383
|
-
new TlaSyntaxError(`${message} at line ${cursor.line}, column ${getColumn()}
|
|
385
|
+
new TlaSyntaxError(`${message} at line ${cursor.line}, column ${getColumn()}`, undefined, {
|
|
386
|
+
reason: message,
|
|
387
|
+
line: cursor.line,
|
|
388
|
+
column: getColumn(),
|
|
389
|
+
isEnd: cursor.index >= source.length,
|
|
390
|
+
});
|
|
384
391
|
|
|
385
392
|
while (cursor.index < source.length) {
|
|
386
393
|
const character = source[cursor.index];
|
|
@@ -476,7 +483,9 @@ export const tokenize = (source: string): Token[] => {
|
|
|
476
483
|
pushToken("Keyword", word.slice(0, 3), 3);
|
|
477
484
|
continue;
|
|
478
485
|
}
|
|
479
|
-
|
|
486
|
+
const name = names.get(word) ?? word;
|
|
487
|
+
names.set(name, name);
|
|
488
|
+
pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
|
|
480
489
|
continue;
|
|
481
490
|
}
|
|
482
491
|
const stepLength = matchStep(source, cursor.index);
|