tla-ts 0.0.8 → 0.0.9-dev.331c500

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.
Files changed (63) hide show
  1. package/README.md +23 -0
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +15 -15
  5. package/dist/deciders.js +48 -48
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +27 -9
  8. package/dist/index.d.ts +27 -9
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove.cjs +30 -30
  13. package/dist/prove.js +32 -32
  14. package/dist/run.cjs +28 -28
  15. package/dist/run.js +31 -31
  16. package/dist/tlapm.cjs +2 -2
  17. package/dist/tlapm.js +1 -1
  18. package/dist/transfer.cjs +30 -33
  19. package/dist/transfer.js +30 -33
  20. package/dist/worker.cjs +4 -4
  21. package/dist/worker.js +4 -4
  22. package/package.json +3 -1
  23. package/src/check.ts +77 -46
  24. package/src/cli/options.ts +2 -1
  25. package/src/cli/tlc.ts +26 -8
  26. package/src/cli/typescript.ts +26 -0
  27. package/src/codegen.ts +0 -5
  28. package/src/dependencies.ts +348 -24
  29. package/src/environment.ts +27 -11
  30. package/src/errors.ts +15 -0
  31. package/src/evaluate.ts +188 -712
  32. package/src/instances.ts +67 -6
  33. package/src/lexer.ts +140 -51
  34. package/src/liveness-checker.ts +6 -3
  35. package/src/liveness.ts +8 -18
  36. package/src/parallel-worker.ts +7 -5
  37. package/src/parallel.ts +18 -9
  38. package/src/prover/smt/solver/solver.ts +31 -27
  39. package/src/sany/generator.ts +9 -7
  40. package/src/sany/linter.ts +30 -21
  41. package/src/search.ts +48 -32
  42. package/src/spec-description.ts +1 -1
  43. package/src/successors.ts +6 -12
  44. package/src/symmetry.ts +32 -143
  45. package/src/tlc/fingerprint.ts +69 -116
  46. package/src/tlc/order.ts +27 -48
  47. package/src/tlc/replay.ts +1 -1
  48. package/src/typescript/actions.ts +494 -0
  49. package/src/typescript/collections.ts +615 -0
  50. package/src/typescript/compile.ts +70 -0
  51. package/src/typescript/context.ts +106 -0
  52. package/src/typescript/emit.ts +122 -0
  53. package/src/typescript/expressions.ts +508 -0
  54. package/src/typescript/formula.ts +160 -0
  55. package/src/typescript/functions.ts +295 -0
  56. package/src/typescript/model.ts +164 -0
  57. package/src/typescript/names.ts +116 -0
  58. package/src/typescript/program.ts +51 -0
  59. package/src/typescript/properties.ts +142 -0
  60. package/src/typescript/scope.ts +117 -0
  61. package/src/typescript/steps.ts +175 -0
  62. package/src/typescript/types.ts +178 -0
  63. package/src/values.ts +151 -85
@@ -0,0 +1,106 @@
1
+ import ts from "@typescript/typescript6";
2
+ import { createNames, type Names } from "./names";
3
+
4
+ export interface Definition {
5
+ name: string;
6
+ // The operator's parameters as TLA+ declares them, such as "x" or "F(_)".
7
+ parameters: string[];
8
+ readsState: boolean;
9
+ }
10
+
11
+ export interface Context {
12
+ checker: ts.TypeChecker;
13
+ names: Names;
14
+ variables: Map<ts.Symbol, string>;
15
+ // Helper functions and constants, compiled when first referenced.
16
+ definitions: Map<ts.Declaration, Definition>;
17
+ // Helpers being compiled or inlined, to detect recursion.
18
+ inProgress: Set<ts.Node>;
19
+ recursive: Set<ts.Declaration>;
20
+ // Rendered definitions, each after the definitions it uses.
21
+ output: string[];
22
+ // String literals of @symmetric types, which become model values.
23
+ modelValues: Map<string, string>;
24
+ // Model values and helper operators that the spec uses.
25
+ prelude: Set<"Null" | "Undefined" | "Rem" | "Quot">;
26
+ }
27
+
28
+ export interface MethodCall extends ts.CallExpression {
29
+ readonly expression: ts.PropertyAccessExpression;
30
+ }
31
+
32
+ export type FunctionNode = ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression;
33
+
34
+ export type HelperDeclaration = ts.FunctionDeclaration | ts.VariableDeclaration;
35
+
36
+ export const createContext = (checker: ts.TypeChecker): Context => ({
37
+ checker,
38
+ names: createNames(),
39
+ variables: new Map(),
40
+ definitions: new Map(),
41
+ inProgress: new Set(),
42
+ recursive: new Set(),
43
+ output: [],
44
+ modelValues: new Map(),
45
+ prelude: new Set(),
46
+ });
47
+
48
+ export const getSymbol = (node: ts.Node, checker: ts.TypeChecker) => {
49
+ const symbol =
50
+ ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node
51
+ ? checker.getShorthandAssignmentValueSymbol(node.parent)
52
+ : checker.getSymbolAtLocation(node);
53
+ return symbol !== undefined && symbol.flags & ts.SymbolFlags.Alias
54
+ ? checker.getAliasedSymbol(symbol)
55
+ : symbol;
56
+ };
57
+
58
+ export const isMethodCall = (node: ts.Node): node is MethodCall =>
59
+ ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression);
60
+
61
+ // The declaration of a constant or function at the top level of a module, which becomes a TLA+
62
+ // definition.
63
+ export const getTopLevelDeclaration = (symbol: ts.Symbol | undefined) => {
64
+ const declaration = symbol?.valueDeclaration;
65
+ if (declaration === undefined) return undefined;
66
+ if (ts.isFunctionDeclaration(declaration) && ts.isSourceFile(declaration.parent)) {
67
+ return declaration;
68
+ }
69
+ const isTopLevelConst =
70
+ ts.isVariableDeclaration(declaration) &&
71
+ ts.isVariableStatement(declaration.parent.parent) &&
72
+ ts.isSourceFile(declaration.parent.parent.parent) &&
73
+ (declaration.parent.flags & ts.NodeFlags.Const) !== 0;
74
+ return isTopLevelConst ? declaration : undefined;
75
+ };
76
+
77
+ // The function a node is, or that a variable declaration initializes.
78
+ export const getFunction = (node: ts.Node | undefined): FunctionNode | undefined => {
79
+ if (node === undefined) return undefined;
80
+ if (ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
81
+ return node;
82
+ }
83
+ return ts.isVariableDeclaration(node) ? getFunction(node.initializer) : undefined;
84
+ };
85
+
86
+ // The function that a callee or callback names: one written in place, or one declared elsewhere.
87
+ export const getCallee = (node: ts.Expression, context: Context) =>
88
+ getFunction(node) ?? getFunction(getSymbol(node, context.checker)?.valueDeclaration);
89
+
90
+ export const translateString = (value: string, context: Context) =>
91
+ context.modelValues.get(value) ?? JSON.stringify(value);
92
+
93
+ // A name, a number, a string, or the @ of an EXCEPT, which costs nothing to repeat.
94
+ const isTrivial = (value: string) => /^(@|[A-Za-z_]\w*'?|-?\d+|"[^"\\]*")$/.test(value);
95
+
96
+ // Binds a value that an expression repeats to a LET definition, so that TLC evaluates it once.
97
+ export const share = (
98
+ value: string,
99
+ base: string,
100
+ context: Context,
101
+ use: (shared: string) => string,
102
+ ) => {
103
+ if (isTrivial(value)) return use(value);
104
+ const name = context.names.getFresh(base);
105
+ return `(LET ${name} == ${value} IN ${use(name)})`;
106
+ };
@@ -0,0 +1,122 @@
1
+ import type { Context } from "./context";
2
+ import { TlaTypeScriptError } from "../errors";
3
+ import type { Model } from "./model";
4
+
5
+ // The parts of a spec and its config, in the order that the model declares them.
6
+ export interface Sections {
7
+ init: string[];
8
+ actions: string[];
9
+ next: string[];
10
+ fairness: string[];
11
+ predicates: string[];
12
+ temporal: string[];
13
+ invariants: string[];
14
+ properties: string[];
15
+ constraints: string[];
16
+ actionConstraints: string[];
17
+ views: string[];
18
+ postconditions: string[];
19
+ assumptions: string[];
20
+ }
21
+
22
+ export interface CompiledSpec {
23
+ name: string;
24
+ source: string;
25
+ config: string;
26
+ }
27
+
28
+ const REM =
29
+ "Rem(a, b) ==\n LET m == IF b < 0 THEN -b ELSE b\n IN IF a >= 0 THEN a % m ELSE -((-a) % m)";
30
+
31
+ const QUOT =
32
+ "Quot(a, b) ==\n LET q == (IF a < 0 THEN -a ELSE a) \\div (IF b < 0 THEN -b ELSE b)\n IN IF (a < 0) = (b < 0) THEN q ELSE -q";
33
+
34
+ export const createSections = (): Sections => ({
35
+ init: [],
36
+ actions: [],
37
+ next: [],
38
+ fairness: [],
39
+ predicates: [],
40
+ temporal: [],
41
+ invariants: [],
42
+ properties: [],
43
+ constraints: [],
44
+ actionConstraints: [],
45
+ views: [],
46
+ postconditions: [],
47
+ assumptions: [],
48
+ });
49
+
50
+ const formatConjunction = (items: string[]) =>
51
+ items.length === 1 ? ` ${items[0]}` : `\n${items.map((item) => ` /\\ ${item}`).join("\n")}`;
52
+
53
+ const formatDisjunction = (items: string[]) =>
54
+ items.length === 0
55
+ ? " FALSE"
56
+ : items.length === 1
57
+ ? ` ${items[0]}`
58
+ : `\n${items.map((item) => ` \\/ ${item}`).join("\n")}`;
59
+
60
+ const formatConfigSection = (keyword: string, values: string[]) =>
61
+ values.length > 0 ? [`${keyword} ${values.join(" ")}`] : [];
62
+
63
+ export const emit = (
64
+ moduleName: string,
65
+ model: Model,
66
+ sections: Sections,
67
+ context: Context,
68
+ ): CompiledSpec => {
69
+ if (sections.views.length > 1) throw new TlaTypeScriptError("Tag only one function @view");
70
+ const recursive = [...context.recursive].map((declaration) => {
71
+ const definition = context.definitions.get(declaration);
72
+ if (definition === undefined || definition.parameters.length === 0) {
73
+ throw new TlaTypeScriptError("A constant can't refer to itself", declaration);
74
+ }
75
+ return `${definition.name}(${definition.parameters.map(() => "_").join(", ")})`;
76
+ });
77
+ const prelude = context.prelude;
78
+ const constants = [
79
+ ...(prelude.has("Null") ? ["Null"] : []),
80
+ ...(prelude.has("Undefined") ? ["Undefined"] : []),
81
+ ...context.modelValues.values(),
82
+ ];
83
+ const variables = model.variables.map((variable) => variable.name);
84
+ const symmetrySets = model.symmetrySets;
85
+ const source = [
86
+ `---- MODULE ${moduleName} ----`,
87
+ "EXTENDS Integers, Sequences, FiniteSets, TLC",
88
+ ...(constants.length > 0 ? [`CONSTANTS ${constants.join(", ")}`] : []),
89
+ ...(variables.length > 0 ? [`VARIABLES ${variables.join(", ")}`] : []),
90
+ `vars == <<${variables.join(", ")}>>`,
91
+ ...(recursive.length > 0 ? [`RECURSIVE ${recursive.join(", ")}`] : []),
92
+ ...(prelude.has("Rem") ? [REM] : []),
93
+ ...(prelude.has("Quot") ? [QUOT] : []),
94
+ ...context.output,
95
+ `Init ==${formatConjunction(sections.init.length > 0 ? sections.init : ["TRUE"])}`,
96
+ ...sections.actions,
97
+ `Next ==${formatDisjunction(sections.next)}`,
98
+ `Spec ==${formatConjunction(["Init", "[][Next]_vars", ...sections.fairness])}`,
99
+ ...sections.predicates,
100
+ ...sections.temporal,
101
+ ...sections.assumptions.map((assumption) => `ASSUME ${assumption}`),
102
+ ...(symmetrySets.length > 0
103
+ ? [`Perms == ${symmetrySets.map((set) => `Permutations(${set})`).join(" \\cup ")}`]
104
+ : []),
105
+ "====",
106
+ ].join("\n");
107
+ const config = [
108
+ "SPECIFICATION Spec",
109
+ ...(constants.length > 0
110
+ ? ["CONSTANTS", ...constants.map((constant) => ` ${constant} = ${constant}`)]
111
+ : []),
112
+ ...formatConfigSection("INVARIANTS", sections.invariants),
113
+ ...formatConfigSection("PROPERTIES", sections.properties),
114
+ ...formatConfigSection("CONSTRAINTS", sections.constraints),
115
+ ...formatConfigSection("ACTION_CONSTRAINTS", sections.actionConstraints),
116
+ ...formatConfigSection("VIEW", sections.views),
117
+ ...formatConfigSection("POSTCONDITIONS", sections.postconditions),
118
+ ...(symmetrySets.length > 0 ? ["SYMMETRY Perms"] : []),
119
+ ...(model.isDeadlockChecked ? [] : ["CHECK_DEADLOCK FALSE"]),
120
+ ].join("\n");
121
+ return { name: moduleName, source, config };
122
+ };