tla-ts 0.0.2 → 0.0.3-dev.2032e26
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 +51 -1
- package/dist/index.d.ts +51 -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 +20 -2
- package/dist/run-cli.js +20 -2
- package/dist/transfer.cjs +19 -15
- package/dist/transfer.js +19 -15
- package/package.json +3 -1
- package/src/ast.ts +43 -3
- package/src/check.ts +51 -13
- package/src/cli-options.ts +58 -0
- package/src/errors.ts +42 -1
- package/src/evaluate.ts +6 -2
- package/src/fingerprints.ts +46 -11
- package/src/lexer.ts +11 -2
- 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/run-cli.ts +295 -27
- package/src/standard-levels.ts +3 -3
- package/src/successors.ts +5 -2
- package/src/symmetry.ts +14 -2
- package/src/tlc-order.ts +17 -3
- package/src/transfer.ts +22 -1
- package/src/values.ts +59 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tla-ts",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3-dev.2032e26",
|
|
4
4
|
"description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"formal verification",
|
|
@@ -71,6 +71,8 @@
|
|
|
71
71
|
"conformance:sany": "tsx scripts/proof-conformance.ts",
|
|
72
72
|
"conformance:record": "tsx scripts/record-tlc.ts",
|
|
73
73
|
"conformance:record-sany": "tsx scripts/record-sany.ts",
|
|
74
|
+
"conformance:record-tlapm": "tsx scripts/record-tlapm.ts",
|
|
75
|
+
"conformance:tlapm": "tsx scripts/tlapm-conformance.ts",
|
|
74
76
|
"publint": "publint",
|
|
75
77
|
"test": "vp test",
|
|
76
78
|
"typecheck": "tsc --noEmit"
|
package/src/ast.ts
CHANGED
|
@@ -44,6 +44,9 @@ export interface ApplyExpression {
|
|
|
44
44
|
kind: "Apply";
|
|
45
45
|
operator: string;
|
|
46
46
|
args: Expression[];
|
|
47
|
+
// The number of args each part of a name like F(x)!l(y) takes, when a part before the last takes
|
|
48
|
+
// some.
|
|
49
|
+
argumentCounts?: number[];
|
|
47
50
|
location: SourceLocation;
|
|
48
51
|
resolution?: LocalResolution;
|
|
49
52
|
}
|
|
@@ -64,6 +67,7 @@ export interface LambdaExpression {
|
|
|
64
67
|
export interface JunctionExpression {
|
|
65
68
|
kind: "And" | "Or";
|
|
66
69
|
operands: Expression[];
|
|
70
|
+
isBulleted?: boolean;
|
|
67
71
|
location: SourceLocation;
|
|
68
72
|
}
|
|
69
73
|
|
|
@@ -129,6 +133,7 @@ export interface QuantifierExpression {
|
|
|
129
133
|
|
|
130
134
|
export interface UnboundedQuantifierExpression {
|
|
131
135
|
kind: "UnboundedQuantifier";
|
|
136
|
+
quantifier: "Forall" | "Exists";
|
|
132
137
|
names: string[];
|
|
133
138
|
isTemporal?: boolean;
|
|
134
139
|
body: Expression;
|
|
@@ -317,6 +322,7 @@ export type Expression =
|
|
|
317
322
|
export interface OperatorParam {
|
|
318
323
|
name: string;
|
|
319
324
|
arity: number;
|
|
325
|
+
location?: SourceLocation;
|
|
320
326
|
}
|
|
321
327
|
|
|
322
328
|
export interface OperatorDefinition {
|
|
@@ -368,6 +374,8 @@ export interface Instance {
|
|
|
368
374
|
substitutions: InstanceSubstitution[];
|
|
369
375
|
isLocal: boolean;
|
|
370
376
|
location: SourceLocation;
|
|
377
|
+
instanceRange: SourceRange;
|
|
378
|
+
definitionRange?: SourceRange;
|
|
371
379
|
}
|
|
372
380
|
|
|
373
381
|
export interface NewSymbol {
|
|
@@ -388,18 +396,33 @@ export interface AssumeProve {
|
|
|
388
396
|
|
|
389
397
|
export type Fact = Expression | { kind: "ModuleFact"; name: string; location: SourceLocation };
|
|
390
398
|
|
|
399
|
+
// keyword is the range of the USE or HIDE token and any ONLY after it.
|
|
391
400
|
export interface UseOrHide {
|
|
392
401
|
kind: "Use" | "Hide";
|
|
393
402
|
isOnly: boolean;
|
|
394
403
|
facts: Fact[];
|
|
395
404
|
defs: Fact[];
|
|
405
|
+
keyword: SourceRange;
|
|
396
406
|
location: SourceLocation;
|
|
397
407
|
}
|
|
398
408
|
|
|
409
|
+
// keyword is the range of the BY, OBVIOUS, or OMITTED token.
|
|
399
410
|
export type Proof =
|
|
400
|
-
| { kind: "Obvious" | "Omitted"; location: SourceLocation }
|
|
401
|
-
| {
|
|
402
|
-
|
|
411
|
+
| { kind: "Obvious" | "Omitted"; keyword: SourceRange; location: SourceLocation }
|
|
412
|
+
| {
|
|
413
|
+
kind: "By";
|
|
414
|
+
isOnly: boolean;
|
|
415
|
+
facts: Fact[];
|
|
416
|
+
defs: Fact[];
|
|
417
|
+
keyword: SourceRange;
|
|
418
|
+
location: SourceLocation;
|
|
419
|
+
}
|
|
420
|
+
| {
|
|
421
|
+
kind: "Steps";
|
|
422
|
+
level: number;
|
|
423
|
+
steps: ProofStep[];
|
|
424
|
+
location: SourceLocation;
|
|
425
|
+
};
|
|
403
426
|
|
|
404
427
|
export interface DefineStep {
|
|
405
428
|
kind: "Define";
|
|
@@ -419,9 +442,12 @@ export type StepBody =
|
|
|
419
442
|
| { kind: "Assert"; isSuffices: boolean; statement: Expression | AssumeProve }
|
|
420
443
|
| { kind: "Qed" };
|
|
421
444
|
|
|
445
|
+
// head is the range of the step number and its statement, without the proof.
|
|
422
446
|
export interface ProofStep {
|
|
423
447
|
name: string | undefined;
|
|
448
|
+
token: string;
|
|
424
449
|
body: StepBody;
|
|
450
|
+
head: SourceRange;
|
|
425
451
|
proof: Proof | undefined;
|
|
426
452
|
location: SourceLocation;
|
|
427
453
|
}
|
|
@@ -433,10 +459,23 @@ export interface Theorem {
|
|
|
433
459
|
location: SourceLocation;
|
|
434
460
|
}
|
|
435
461
|
|
|
462
|
+
// The units of a module in source order, for consumers that need the order across kinds.
|
|
463
|
+
export type ModuleUnit =
|
|
464
|
+
| { kind: "Constants"; declarations: Declaration[] }
|
|
465
|
+
| { kind: "Variables"; declarations: Declaration[] }
|
|
466
|
+
| { kind: "Recursive"; declarations: Declaration[] }
|
|
467
|
+
| { kind: "Definition"; definition: Definition }
|
|
468
|
+
| { kind: "Instance"; instance: Instance }
|
|
469
|
+
| { kind: "Assumption"; assumption: Assumption }
|
|
470
|
+
| { kind: "Theorem"; theorem: Theorem }
|
|
471
|
+
| { kind: "Use"; use: UseOrHide }
|
|
472
|
+
| { kind: "Module"; module: Module };
|
|
473
|
+
|
|
436
474
|
export interface Module {
|
|
437
475
|
name: string;
|
|
438
476
|
modules: Module[];
|
|
439
477
|
extends: string[];
|
|
478
|
+
extendsRanges: SourceRange[];
|
|
440
479
|
constants: Declaration[];
|
|
441
480
|
variables: Declaration[];
|
|
442
481
|
definitions: Definition[];
|
|
@@ -445,4 +484,5 @@ export interface Module {
|
|
|
445
484
|
location: SourceLocation;
|
|
446
485
|
theorems: Theorem[];
|
|
447
486
|
uses: UseOrHide[];
|
|
487
|
+
units: ModuleUnit[];
|
|
448
488
|
}
|
package/src/check.ts
CHANGED
|
@@ -2,7 +2,13 @@ import type { Assumption, Definition, Expression, SourceLocation } from "./ast";
|
|
|
2
2
|
import { parseConfig, type ConfigValue, type ModelConfig } from "./config";
|
|
3
3
|
import { getReadVariables } from "./dependencies";
|
|
4
4
|
import { createEnvironment } from "./environment";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
getErrorExitCode,
|
|
7
|
+
TlaConfigError,
|
|
8
|
+
TlaEvaluationError,
|
|
9
|
+
TlaModuleError,
|
|
10
|
+
UNEXPECTED_EXCEPTION,
|
|
11
|
+
} from "./errors";
|
|
6
12
|
import { EXIT_STATUS, setExitCode, setNextStatePhase, withLivenessExitCode } from "./exit-codes";
|
|
7
13
|
import {
|
|
8
14
|
describeRange,
|
|
@@ -13,6 +19,7 @@ import {
|
|
|
13
19
|
getDefinition,
|
|
14
20
|
isConstantLevel,
|
|
15
21
|
isImpure,
|
|
22
|
+
isLocallyResolved,
|
|
16
23
|
type Context,
|
|
17
24
|
type Environment,
|
|
18
25
|
type Scope,
|
|
@@ -54,6 +61,7 @@ import {
|
|
|
54
61
|
getValueHash,
|
|
55
62
|
isFunctionValue,
|
|
56
63
|
isSameValue,
|
|
64
|
+
isUpdatedElementOf,
|
|
57
65
|
setStringOrder,
|
|
58
66
|
toFiniteFunction,
|
|
59
67
|
type Value,
|
|
@@ -139,6 +147,13 @@ interface NamedExpression {
|
|
|
139
147
|
|
|
140
148
|
interface StateCheck extends NamedExpression {
|
|
141
149
|
variables: number[] | undefined;
|
|
150
|
+
membership: Membership | undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// A conjunct `x \in S` whose S reads no variables.
|
|
154
|
+
interface Membership {
|
|
155
|
+
variable: number;
|
|
156
|
+
set: Expression;
|
|
142
157
|
}
|
|
143
158
|
|
|
144
159
|
interface ActionProperty {
|
|
@@ -481,14 +496,32 @@ const getPossiblePredicates = (names: string[], environment: Environment) =>
|
|
|
481
496
|
return { name, expression, scope: undefined };
|
|
482
497
|
});
|
|
483
498
|
|
|
499
|
+
const getMembership = (
|
|
500
|
+
expression: Expression,
|
|
501
|
+
variables: number[] | undefined,
|
|
502
|
+
environment: Environment,
|
|
503
|
+
): Membership | undefined => {
|
|
504
|
+
if (expression.kind !== "Apply" || expression.operator !== "\\in" || variables?.length !== 1)
|
|
505
|
+
return undefined;
|
|
506
|
+
const element = expression.args[0];
|
|
507
|
+
if (element.kind !== "Identifier" || isLocallyResolved(element)) return undefined;
|
|
508
|
+
return environment.variableIndex.get(element.name) === variables[0]
|
|
509
|
+
? { variable: variables[0], set: expression.args[1] }
|
|
510
|
+
: undefined;
|
|
511
|
+
};
|
|
512
|
+
|
|
484
513
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
485
514
|
named.flatMap((item) =>
|
|
486
|
-
getConjuncts(item.expression, environment).map((expression) =>
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
515
|
+
getConjuncts(item.expression, environment).map((expression) => {
|
|
516
|
+
const variables = getReadVariables(expression, environment);
|
|
517
|
+
return {
|
|
518
|
+
name: item.name,
|
|
519
|
+
expression,
|
|
520
|
+
scope: item.scope,
|
|
521
|
+
variables,
|
|
522
|
+
membership: getMembership(expression, variables, environment),
|
|
523
|
+
};
|
|
524
|
+
}),
|
|
492
525
|
);
|
|
493
526
|
|
|
494
527
|
const isUnchangedFor = (stateCheck: StateCheck, state: Value[], parent: Value[] | undefined) => {
|
|
@@ -743,10 +776,16 @@ export const createSearch = (
|
|
|
743
776
|
invariants.find((invariant) => {
|
|
744
777
|
if (isUnchangedFor(invariant, state, parent)) return false;
|
|
745
778
|
try {
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
779
|
+
const context = getContext(environment, state, undefined, invariant.scope);
|
|
780
|
+
const membership = invariant.membership;
|
|
781
|
+
// The parent satisfies every invariant, so only the entries the step changed can fail.
|
|
782
|
+
return parent && membership
|
|
783
|
+
? !isUpdatedElementOf(
|
|
784
|
+
state[membership.variable],
|
|
785
|
+
parent[membership.variable],
|
|
786
|
+
evaluate(membership.set, context),
|
|
787
|
+
)
|
|
788
|
+
: !holdsLikeTlc(invariant.expression, context);
|
|
750
789
|
} catch (error) {
|
|
751
790
|
// TLC reports a failure only on a state after the initial states as an invariant failure.
|
|
752
791
|
if (!parent) throw error;
|
|
@@ -795,8 +834,7 @@ export const createSearch = (
|
|
|
795
834
|
holdsLikeTlc(
|
|
796
835
|
constraint.expression,
|
|
797
836
|
getContext(environment, state, next),
|
|
798
|
-
(expression) =>
|
|
799
|
-
`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: ${getExpectedBoolean(expression, environment)}`,
|
|
837
|
+
(expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
|
|
800
838
|
),
|
|
801
839
|
),
|
|
802
840
|
trackStatePossible:
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface OptionSpec {
|
|
2
|
+
flag: string;
|
|
3
|
+
isRequired?: boolean;
|
|
4
|
+
isRepeatable?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ParsedOption {
|
|
8
|
+
name: string;
|
|
9
|
+
values: string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ParsedArgs {
|
|
13
|
+
options: ParsedOption[];
|
|
14
|
+
positionals: string[];
|
|
15
|
+
unknown: string[];
|
|
16
|
+
incomplete: ParsedOption | undefined;
|
|
17
|
+
has: (name: string) => boolean;
|
|
18
|
+
get: (name: string) => string[] | undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const formatOption = (spec: OptionSpec) => {
|
|
22
|
+
const text = spec.isRequired ? spec.flag : `[${spec.flag}]`;
|
|
23
|
+
return spec.isRepeatable ? `${text}...` : text;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Each <name> in a flag such as "--toolbox <start> <end>" is one value the option takes, and
|
|
27
|
+
// {options} in the usage stands for the options.
|
|
28
|
+
export const defineCommand = (usage: string, specs: OptionSpec[]) => {
|
|
29
|
+
const arities = new Map(
|
|
30
|
+
specs.map((spec) => {
|
|
31
|
+
const [name, ...values] = spec.flag.split(" ");
|
|
32
|
+
return [name, values.length];
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
const parse = (args: string[]): ParsedArgs => {
|
|
36
|
+
const options: ParsedOption[] = [];
|
|
37
|
+
const positionals: string[] = [];
|
|
38
|
+
const unknown: string[] = [];
|
|
39
|
+
for (let index = 0; index < args.length; index++) {
|
|
40
|
+
const arity = arities.get(args[index]);
|
|
41
|
+
if (arity === undefined) {
|
|
42
|
+
(args[index].startsWith("-") ? unknown : positionals).push(args[index]);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
options.push({ name: args[index], values: args.slice(index + 1, index + 1 + arity) });
|
|
46
|
+
index += arity;
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
options,
|
|
50
|
+
positionals,
|
|
51
|
+
unknown,
|
|
52
|
+
incomplete: options.find((option) => option.values.length < (arities.get(option.name) ?? 0)),
|
|
53
|
+
has: (name) => options.some((option) => option.name === name),
|
|
54
|
+
get: (name) => options.findLast((option) => option.name === name)?.values,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
return { usage: usage.replace("{options}", specs.map(formatOption).join(" ")), parse };
|
|
58
|
+
};
|
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
|
@@ -734,7 +734,9 @@ const compileArgumentBinder = (argument: Expression, environment: Environment):
|
|
|
734
734
|
if (global === undefined) {
|
|
735
735
|
return (context) => {
|
|
736
736
|
const binding = lookupScope(context.scope, argument.name);
|
|
737
|
-
return binding?.kind === "Value"
|
|
737
|
+
return binding?.kind === "Value" || binding?.kind === "Thunk"
|
|
738
|
+
? binding
|
|
739
|
+
: createThunk(argument, context);
|
|
738
740
|
};
|
|
739
741
|
}
|
|
740
742
|
}
|
|
@@ -1755,7 +1757,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1755
1757
|
if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
|
|
1756
1758
|
}
|
|
1757
1759
|
if (other) return other(context);
|
|
1758
|
-
throw new TlaEvaluationError(
|
|
1760
|
+
throw new TlaEvaluationError(
|
|
1761
|
+
`Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
1762
|
+
);
|
|
1759
1763
|
};
|
|
1760
1764
|
}
|
|
1761
1765
|
case "Let": {
|
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/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);
|