tla-ts 0.0.8 → 0.0.9-dev.4ce0657
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 +23 -0
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +15 -15
- package/dist/deciders.js +48 -48
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +27 -9
- package/dist/index.d.ts +27 -9
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +17 -17
- package/dist/prove.js +32 -32
- package/dist/run.cjs +28 -28
- package/dist/run.js +31 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +30 -33
- package/dist/transfer.js +30 -33
- package/dist/worker.cjs +4 -4
- package/dist/worker.js +4 -4
- package/package.json +3 -1
- package/src/check.ts +77 -46
- package/src/cli/options.ts +2 -1
- package/src/cli/tlc.ts +26 -8
- package/src/cli/typescript.ts +26 -0
- package/src/codegen.ts +0 -5
- package/src/dependencies.ts +348 -24
- package/src/environment.ts +27 -11
- package/src/errors.ts +15 -0
- package/src/evaluate.ts +188 -712
- package/src/instances.ts +67 -6
- package/src/lexer.ts +140 -51
- package/src/liveness-checker.ts +6 -3
- package/src/liveness.ts +8 -18
- package/src/parallel-worker.ts +6 -3
- package/src/parallel.ts +15 -7
- package/src/prover/smt/solver/solver.ts +31 -27
- package/src/sany/generator.ts +9 -7
- package/src/sany/linter.ts +30 -21
- package/src/search.ts +48 -32
- package/src/spec-description.ts +1 -1
- package/src/successors.ts +6 -12
- package/src/symmetry.ts +32 -143
- package/src/tlc/fingerprint.ts +69 -116
- package/src/tlc/order.ts +27 -48
- package/src/tlc/replay.ts +1 -1
- package/src/typescript/actions.ts +494 -0
- package/src/typescript/collections.ts +615 -0
- package/src/typescript/compile.ts +70 -0
- package/src/typescript/context.ts +106 -0
- package/src/typescript/emit.ts +122 -0
- package/src/typescript/expressions.ts +508 -0
- package/src/typescript/formula.ts +160 -0
- package/src/typescript/functions.ts +295 -0
- package/src/typescript/model.ts +164 -0
- package/src/typescript/names.ts +116 -0
- package/src/typescript/program.ts +51 -0
- package/src/typescript/properties.ts +141 -0
- package/src/typescript/scope.ts +117 -0
- package/src/typescript/steps.ts +175 -0
- package/src/typescript/types.ts +178 -0
- package/src/values.ts +126 -82
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import ts from "@typescript/typescript6";
|
|
2
|
+
import {
|
|
3
|
+
translateArrayConstructor,
|
|
4
|
+
translateFunctionConstructor,
|
|
5
|
+
translateMethod,
|
|
6
|
+
translateNew,
|
|
7
|
+
translateSet,
|
|
8
|
+
translateSize,
|
|
9
|
+
} from "./collections";
|
|
10
|
+
import {
|
|
11
|
+
type Context,
|
|
12
|
+
getFunction,
|
|
13
|
+
getSymbol,
|
|
14
|
+
getTopLevelDeclaration,
|
|
15
|
+
isMethodCall,
|
|
16
|
+
share,
|
|
17
|
+
translateString,
|
|
18
|
+
} from "./context";
|
|
19
|
+
import { TlaTypeScriptError } from "../errors";
|
|
20
|
+
import { wrap, wrapEquated } from "./formula";
|
|
21
|
+
import { getDefinition, translateHelperCall, translateLocalCall } from "./functions";
|
|
22
|
+
import type { Scope } from "./scope";
|
|
23
|
+
import { getKind, isBooleanLike, isNumericKey } from "./types";
|
|
24
|
+
|
|
25
|
+
interface Field {
|
|
26
|
+
key: string;
|
|
27
|
+
value: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const BINARY_OPERATORS = new Map([
|
|
31
|
+
[ts.SyntaxKind.MinusToken, "-"],
|
|
32
|
+
[ts.SyntaxKind.AsteriskToken, "*"],
|
|
33
|
+
[ts.SyntaxKind.AsteriskAsteriskToken, "^"],
|
|
34
|
+
[ts.SyntaxKind.EqualsEqualsEqualsToken, "="],
|
|
35
|
+
[ts.SyntaxKind.ExclamationEqualsEqualsToken, "#"],
|
|
36
|
+
[ts.SyntaxKind.LessThanToken, "<"],
|
|
37
|
+
[ts.SyntaxKind.LessThanEqualsToken, "<="],
|
|
38
|
+
[ts.SyntaxKind.GreaterThanToken, ">"],
|
|
39
|
+
[ts.SyntaxKind.GreaterThanEqualsToken, ">="],
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
const ORDERING_OPERATORS = new Set([
|
|
43
|
+
ts.SyntaxKind.LessThanToken,
|
|
44
|
+
ts.SyntaxKind.LessThanEqualsToken,
|
|
45
|
+
ts.SyntaxKind.GreaterThanToken,
|
|
46
|
+
ts.SyntaxKind.GreaterThanEqualsToken,
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
const LOGICAL_OPERATORS = new Map([
|
|
50
|
+
[ts.SyntaxKind.AmpersandAmpersandToken, "/\\"],
|
|
51
|
+
[ts.SyntaxKind.BarBarToken, "\\/"],
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
// The key of an element access, which counts from 1 in a sequence where JavaScript counts from 0.
|
|
55
|
+
export const translateKey = (node: ts.ElementAccessExpression, scope: Scope, context: Context) => {
|
|
56
|
+
const key = node.argumentExpression;
|
|
57
|
+
if (getKind(node.expression, context) !== "sequence") {
|
|
58
|
+
return translateExpression(key, scope, context);
|
|
59
|
+
}
|
|
60
|
+
return ts.isNumericLiteral(key)
|
|
61
|
+
? String(Number(key.text) + 1)
|
|
62
|
+
: `${wrap(translateExpression(key, scope, context))} + 1`;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const translateArrayLiteral = (node: ts.ArrayLiteralExpression, scope: Scope, context: Context) => {
|
|
66
|
+
// Runs of plain elements become tuples, and each spread array stays a sequence of its own.
|
|
67
|
+
const parts = node.elements.reduce<(string | string[])[]>((result, element) => {
|
|
68
|
+
if (!ts.isSpreadElement(element)) {
|
|
69
|
+
const value = translateExpression(element, scope, context);
|
|
70
|
+
const last = result.at(-1);
|
|
71
|
+
return Array.isArray(last)
|
|
72
|
+
? [...result.slice(0, -1), [...last, value]]
|
|
73
|
+
: [...result, [value]];
|
|
74
|
+
}
|
|
75
|
+
if (getKind(element.expression, context) !== "sequence") {
|
|
76
|
+
throw new TlaTypeScriptError(
|
|
77
|
+
"Sets have no order, so spread one only into every(), some(), filter(), map(), or find()",
|
|
78
|
+
element,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return [...result, translateExpression(element.expression, scope, context)];
|
|
82
|
+
}, []);
|
|
83
|
+
const sequences = parts.map((part) => (Array.isArray(part) ? `<<${part.join(", ")}>>` : part));
|
|
84
|
+
if (sequences.length === 0) return "<<>>";
|
|
85
|
+
return sequences.length === 1 ? sequences[0] : sequences.map(wrap).join(" \\o ");
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const getField = (key: string) => (isNumericKey(key) ? `[${key}]` : `.${key}`);
|
|
89
|
+
|
|
90
|
+
const formatRecord = (fields: Field[]) =>
|
|
91
|
+
fields.every((field) => isNumericKey(field.key))
|
|
92
|
+
? fields.map((field) => `(${field.key} :> ${field.value})`).join(" @@ ")
|
|
93
|
+
: `[${fields.map((field) => `${field.key} |-> ${field.value}`).join(", ")}]`;
|
|
94
|
+
|
|
95
|
+
const translateObjectLiteral = (
|
|
96
|
+
node: ts.ObjectLiteralExpression,
|
|
97
|
+
scope: Scope,
|
|
98
|
+
context: Context,
|
|
99
|
+
) => {
|
|
100
|
+
const checker = context.checker;
|
|
101
|
+
const getKey = (property: ts.ObjectLiteralElementLike) =>
|
|
102
|
+
property.name === undefined || ts.isComputedPropertyName(property.name)
|
|
103
|
+
? undefined
|
|
104
|
+
: property.name.getText().replace(/^["']|["']$/g, "");
|
|
105
|
+
const getValue = (property: ts.ObjectLiteralElementLike) => {
|
|
106
|
+
if (ts.isPropertyAssignment(property)) {
|
|
107
|
+
return translateExpression(property.initializer, scope, context);
|
|
108
|
+
}
|
|
109
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
110
|
+
return translateExpression(property.name, scope, context);
|
|
111
|
+
}
|
|
112
|
+
throw new TlaTypeScriptError("Unsupported object member", property);
|
|
113
|
+
};
|
|
114
|
+
const spreads = node.properties.filter(ts.isSpreadAssignment);
|
|
115
|
+
if (spreads.length === 0) {
|
|
116
|
+
if (node.properties.length === 0) {
|
|
117
|
+
throw new TlaTypeScriptError("TLA+ has no empty record, so use a Map", node);
|
|
118
|
+
}
|
|
119
|
+
return formatRecord(
|
|
120
|
+
node.properties.map((property) => {
|
|
121
|
+
const key = getKey(property);
|
|
122
|
+
if (key === undefined) {
|
|
123
|
+
throw new TlaTypeScriptError("Computed keys need a spread base", property);
|
|
124
|
+
}
|
|
125
|
+
return { key, value: getValue(property) };
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
// { ...base, a: 1 } updates fields that base already has with EXCEPT.
|
|
130
|
+
const base = node.properties[0];
|
|
131
|
+
const updates = node.properties.slice(1);
|
|
132
|
+
if (ts.isSpreadAssignment(base) && spreads.length === 1) {
|
|
133
|
+
const baseType = checker.getTypeAtLocation(base.expression);
|
|
134
|
+
const value = translateExpression(base.expression, scope, context);
|
|
135
|
+
if (updates.length === 0) return value;
|
|
136
|
+
const isUpdate = updates.every((property) => {
|
|
137
|
+
const key = getKey(property);
|
|
138
|
+
return key === undefined || baseType.getProperty(key) !== undefined;
|
|
139
|
+
});
|
|
140
|
+
if (isUpdate) {
|
|
141
|
+
const changes = updates.map((property) => {
|
|
142
|
+
const key = getKey(property);
|
|
143
|
+
if (key !== undefined) return `!${getField(key)} = ${wrapEquated(getValue(property))}`;
|
|
144
|
+
if (property.name === undefined || !ts.isComputedPropertyName(property.name)) {
|
|
145
|
+
throw new TlaTypeScriptError("Unsupported object member", property);
|
|
146
|
+
}
|
|
147
|
+
return `![${translateExpression(property.name.expression, scope, context)}] = ${wrapEquated(getValue(property))}`;
|
|
148
|
+
});
|
|
149
|
+
return `[${wrap(value)} EXCEPT ${changes.join(", ")}]`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Otherwise each field takes its value from the last member that sets it.
|
|
153
|
+
return formatRecord(
|
|
154
|
+
checker
|
|
155
|
+
.getTypeAtLocation(node)
|
|
156
|
+
.getProperties()
|
|
157
|
+
.map((property) => {
|
|
158
|
+
const key = property.name;
|
|
159
|
+
const member = node.properties.findLast((candidate) =>
|
|
160
|
+
ts.isSpreadAssignment(candidate)
|
|
161
|
+
? checker.getTypeAtLocation(candidate.expression).getProperty(key) !== undefined
|
|
162
|
+
: getKey(candidate) === key,
|
|
163
|
+
);
|
|
164
|
+
if (
|
|
165
|
+
member === undefined ||
|
|
166
|
+
(!ts.isSpreadAssignment(member) && getKey(member) === undefined)
|
|
167
|
+
) {
|
|
168
|
+
throw new TlaTypeScriptError(`Can't tell which value ${key} takes`, node);
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
key,
|
|
172
|
+
value: ts.isSpreadAssignment(member)
|
|
173
|
+
? `${wrap(translateExpression(member.expression, scope, context))}${getField(key)}`
|
|
174
|
+
: getValue(member),
|
|
175
|
+
};
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const translateMath = (node: ts.CallExpression, method: string, scope: Scope, context: Context) => {
|
|
181
|
+
const translate = (inner: ts.Expression) => wrap(translateExpression(inner, scope, context));
|
|
182
|
+
const argument = node.arguments[0];
|
|
183
|
+
if ((method === "floor" || method === "trunc") && node.arguments.length === 1) {
|
|
184
|
+
const division = ts.isParenthesizedExpression(argument) ? argument.expression : argument;
|
|
185
|
+
if (
|
|
186
|
+
!ts.isBinaryExpression(division) ||
|
|
187
|
+
division.operatorToken.kind !== ts.SyntaxKind.SlashToken
|
|
188
|
+
) {
|
|
189
|
+
throw new TlaTypeScriptError(
|
|
190
|
+
`Math.${method} only takes a division, as in Math.${method}(a / b)`,
|
|
191
|
+
node,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const left = translate(division.left);
|
|
195
|
+
const right = translate(division.right);
|
|
196
|
+
if (method === "floor") return `${left} \\div ${right}`;
|
|
197
|
+
context.prelude.add("Quot");
|
|
198
|
+
return `Quot(${left}, ${right})`;
|
|
199
|
+
}
|
|
200
|
+
if (method === "abs" && node.arguments.length === 1) {
|
|
201
|
+
return share(
|
|
202
|
+
translate(argument),
|
|
203
|
+
"value",
|
|
204
|
+
context,
|
|
205
|
+
(value) => `IF ${value} < 0 THEN -${value} ELSE ${value}`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if ((method === "min" || method === "max") && node.arguments.length > 0) {
|
|
209
|
+
const comparison = method === "min" ? "<=" : ">=";
|
|
210
|
+
if (node.arguments.length === 1 && ts.isSpreadElement(argument)) {
|
|
211
|
+
const set = wrap(translateSet(argument.expression, scope, context));
|
|
212
|
+
const candidate = context.names.getFresh("candidate");
|
|
213
|
+
const other = context.names.getFresh("other");
|
|
214
|
+
return `CHOOSE ${candidate} \\in ${set} : \\A ${other} \\in ${set} : ${candidate} ${comparison} ${other}`;
|
|
215
|
+
}
|
|
216
|
+
return node.arguments
|
|
217
|
+
.map(translate)
|
|
218
|
+
.reduce((result, inner) =>
|
|
219
|
+
share(result, "best", context, (best) =>
|
|
220
|
+
share(
|
|
221
|
+
inner,
|
|
222
|
+
"value",
|
|
223
|
+
context,
|
|
224
|
+
(value) => `IF ${best} ${comparison} ${value} THEN ${best} ELSE ${value}`,
|
|
225
|
+
),
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
throw new TlaTypeScriptError(`Unsupported call: Math.${method}`, node);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const translateCall = (
|
|
233
|
+
node: ts.CallExpression,
|
|
234
|
+
scope: Scope,
|
|
235
|
+
context: Context,
|
|
236
|
+
isDefined: boolean,
|
|
237
|
+
) => {
|
|
238
|
+
const symbol = getSymbol(node.expression, context.checker);
|
|
239
|
+
const helper = getTopLevelDeclaration(symbol);
|
|
240
|
+
if (helper !== undefined) return translateHelperCall(helper, node, scope, context);
|
|
241
|
+
if (!isMethodCall(node)) {
|
|
242
|
+
const operator = symbol === undefined ? undefined : scope.bindings.get(symbol);
|
|
243
|
+
if (operator === undefined) return translateLocalCall(node, scope, context);
|
|
244
|
+
const values = node.arguments.map((argument) => translateExpression(argument, scope, context));
|
|
245
|
+
return `${operator}(${values.join(", ")})`;
|
|
246
|
+
}
|
|
247
|
+
const receiver = node.expression.expression;
|
|
248
|
+
const method = node.expression.name.text;
|
|
249
|
+
const global = ts.isIdentifier(receiver) ? receiver.text : undefined;
|
|
250
|
+
if (global === "Math") return translateMath(node, method, scope, context);
|
|
251
|
+
const constructed =
|
|
252
|
+
global === "Object" && method === "fromEntries"
|
|
253
|
+
? translateFunctionConstructor(node.arguments[0], scope, context)
|
|
254
|
+
: translateArrayConstructor(node, scope, context);
|
|
255
|
+
return constructed ?? translateMethod(node, scope, context, isDefined);
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// A value as a string, as JavaScript converts it in a template or beside a string in +.
|
|
259
|
+
const translateToString = (node: ts.Expression, scope: Scope, context: Context) => {
|
|
260
|
+
const value = translateExpression(node, scope, context);
|
|
261
|
+
return getKind(node, context) === "string" ? value : `ToString(${value})`;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const translateTemplate = (node: ts.TemplateExpression, scope: Scope, context: Context) => {
|
|
265
|
+
const parts = [
|
|
266
|
+
...(node.head.text === "" ? [] : [JSON.stringify(node.head.text)]),
|
|
267
|
+
...node.templateSpans.flatMap((span) => {
|
|
268
|
+
const value = translateToString(span.expression, scope, context);
|
|
269
|
+
return span.literal.text === "" ? [value] : [value, JSON.stringify(span.literal.text)];
|
|
270
|
+
}),
|
|
271
|
+
];
|
|
272
|
+
return parts.length === 1 ? parts[0] : parts.map(wrap).join(" \\o ");
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const translateBinary = (node: ts.BinaryExpression, scope: Scope, context: Context) => {
|
|
276
|
+
const kind = node.operatorToken.kind;
|
|
277
|
+
const translate = (inner: ts.Expression) => wrap(translateExpression(inner, scope, context));
|
|
278
|
+
const isString =
|
|
279
|
+
getKind(node.left, context) === "string" || getKind(node.right, context) === "string";
|
|
280
|
+
const logical = LOGICAL_OPERATORS.get(kind);
|
|
281
|
+
if (logical !== undefined) {
|
|
282
|
+
if (!isBooleanLike(node.left, context) || !isBooleanLike(node.right, context)) {
|
|
283
|
+
throw new TlaTypeScriptError(
|
|
284
|
+
`${node.operatorToken.getText()} needs booleans, so compare explicitly`,
|
|
285
|
+
node,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
return `${translate(node.left)} ${logical} ${translate(node.right)}`;
|
|
289
|
+
}
|
|
290
|
+
if (kind === ts.SyntaxKind.PlusToken && isString) {
|
|
291
|
+
const left = wrap(translateToString(node.left, scope, context));
|
|
292
|
+
return `${left} \\o ${wrap(translateToString(node.right, scope, context))}`;
|
|
293
|
+
}
|
|
294
|
+
if (kind === ts.SyntaxKind.PlusToken) return `${translate(node.left)} + ${translate(node.right)}`;
|
|
295
|
+
if (kind === ts.SyntaxKind.PercentToken) {
|
|
296
|
+
context.prelude.add("Rem");
|
|
297
|
+
return `Rem(${translateExpression(node.left, scope, context)}, ${translateExpression(node.right, scope, context)})`;
|
|
298
|
+
}
|
|
299
|
+
if (kind === ts.SyntaxKind.QuestionQuestionToken) {
|
|
300
|
+
context.prelude.add("Null");
|
|
301
|
+
context.prelude.add("Undefined");
|
|
302
|
+
return share(
|
|
303
|
+
translate(node.left),
|
|
304
|
+
"value",
|
|
305
|
+
context,
|
|
306
|
+
(value) => `IF ${value} \\in {Null, Undefined} THEN ${translate(node.right)} ELSE ${value}`,
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
if (kind === ts.SyntaxKind.InKeyword) {
|
|
310
|
+
return `${translate(node.left)} \\in DOMAIN ${translate(node.right)}`;
|
|
311
|
+
}
|
|
312
|
+
if (kind === ts.SyntaxKind.SlashToken) {
|
|
313
|
+
throw new TlaTypeScriptError("Write division as Math.floor(a / b) or Math.trunc(a / b)", node);
|
|
314
|
+
}
|
|
315
|
+
if (ORDERING_OPERATORS.has(kind) && isString) {
|
|
316
|
+
throw new TlaTypeScriptError("TLA+ strings have no order, so compare them with === only", node);
|
|
317
|
+
}
|
|
318
|
+
const operator = BINARY_OPERATORS.get(kind);
|
|
319
|
+
if (operator === undefined) {
|
|
320
|
+
throw new TlaTypeScriptError(
|
|
321
|
+
`Unsupported operator: ${node.operatorToken.getText()}`,
|
|
322
|
+
node.operatorToken,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
return `${translate(node.left)} ${operator} ${translate(node.right)}`;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// A field of the state before the step, which is the variable's unprimed value.
|
|
329
|
+
const getPreviousVariable = (name: string, node: ts.Node, context: Context) => {
|
|
330
|
+
const variable = [...context.variables].find((entry) => entry[0].name === name);
|
|
331
|
+
if (variable === undefined) throw new TlaTypeScriptError(`Unknown variable: ${name}`, node);
|
|
332
|
+
return variable[1];
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const translateEnumMember = (node: ts.Node, symbol: ts.Symbol, context: Context) => {
|
|
336
|
+
const declaration = symbol.valueDeclaration;
|
|
337
|
+
const value =
|
|
338
|
+
declaration !== undefined && ts.isEnumMember(declaration)
|
|
339
|
+
? context.checker.getConstantValue(declaration)
|
|
340
|
+
: undefined;
|
|
341
|
+
if (typeof value === "string") return translateString(value, context);
|
|
342
|
+
if (typeof value === "number" && Number.isInteger(value)) return String(value);
|
|
343
|
+
throw new TlaTypeScriptError("Unsupported enum member", node);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// The value that an enum or a top-level declaration gives a name.
|
|
347
|
+
const translateDeclared = (
|
|
348
|
+
node: ts.Node,
|
|
349
|
+
symbol: ts.Symbol | undefined,
|
|
350
|
+
scope: Scope,
|
|
351
|
+
context: Context,
|
|
352
|
+
) => {
|
|
353
|
+
if (symbol !== undefined && symbol.flags & ts.SymbolFlags.EnumMember) {
|
|
354
|
+
return translateEnumMember(node, symbol, context);
|
|
355
|
+
}
|
|
356
|
+
const declaration = getTopLevelDeclaration(symbol);
|
|
357
|
+
if (declaration === undefined) return undefined;
|
|
358
|
+
// A function passes as its operator's name, and a constant as its value.
|
|
359
|
+
return getFunction(declaration) === undefined
|
|
360
|
+
? translateHelperCall(declaration, undefined, scope, context)
|
|
361
|
+
: getDefinition(declaration, context).name;
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const translateIdentifier = (node: ts.Identifier, scope: Scope, context: Context) => {
|
|
365
|
+
if (node.text === "undefined") {
|
|
366
|
+
context.prelude.add("Undefined");
|
|
367
|
+
return "Undefined";
|
|
368
|
+
}
|
|
369
|
+
const symbol = getSymbol(node, context.checker);
|
|
370
|
+
const binding = symbol === undefined ? undefined : scope.bindings.get(symbol);
|
|
371
|
+
if (binding !== undefined) return binding;
|
|
372
|
+
if (symbol !== undefined && symbol === scope.previous) {
|
|
373
|
+
return formatRecord(
|
|
374
|
+
context.checker
|
|
375
|
+
.getTypeAtLocation(node)
|
|
376
|
+
.getProperties()
|
|
377
|
+
.map((property) => ({
|
|
378
|
+
key: property.name,
|
|
379
|
+
value: getPreviousVariable(property.name, node, context),
|
|
380
|
+
})),
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
const declared = translateDeclared(node, symbol, scope, context);
|
|
384
|
+
if (declared !== undefined) return declared;
|
|
385
|
+
if (symbol !== undefined && context.variables.has(symbol)) {
|
|
386
|
+
throw new TlaTypeScriptError(`${node.text} can't be read here`, node);
|
|
387
|
+
}
|
|
388
|
+
throw new TlaTypeScriptError(`Unknown name: ${node.text}`, node);
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const translatePropertyAccess = (
|
|
392
|
+
node: ts.PropertyAccessExpression,
|
|
393
|
+
scope: Scope,
|
|
394
|
+
context: Context,
|
|
395
|
+
) => {
|
|
396
|
+
if (node.questionDotToken !== undefined) {
|
|
397
|
+
throw new TlaTypeScriptError("Optional chaining isn't supported", node);
|
|
398
|
+
}
|
|
399
|
+
const declared = translateDeclared(node, getSymbol(node, context.checker), scope, context);
|
|
400
|
+
if (declared !== undefined) return declared;
|
|
401
|
+
const receiver = node.expression;
|
|
402
|
+
const name = node.name.text;
|
|
403
|
+
if (
|
|
404
|
+
scope.previous !== undefined &&
|
|
405
|
+
ts.isIdentifier(receiver) &&
|
|
406
|
+
getSymbol(receiver, context.checker) === scope.previous
|
|
407
|
+
) {
|
|
408
|
+
return getPreviousVariable(name, node, context);
|
|
409
|
+
}
|
|
410
|
+
const size =
|
|
411
|
+
name === "length" || name === "size" ? translateSize(receiver, scope, context) : undefined;
|
|
412
|
+
return size ?? `${wrap(translateExpression(receiver, scope, context))}.${name}`;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const translateElementAccess = (
|
|
416
|
+
node: ts.ElementAccessExpression,
|
|
417
|
+
scope: Scope,
|
|
418
|
+
context: Context,
|
|
419
|
+
) => {
|
|
420
|
+
const value = wrap(translateExpression(node.expression, scope, context));
|
|
421
|
+
return `${value}[${translateKey(node, scope, context)}]`;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
export const translateCondition = (node: ts.Expression, scope: Scope, context: Context) => {
|
|
425
|
+
if (!isBooleanLike(node, context)) {
|
|
426
|
+
throw new TlaTypeScriptError("Conditions must be booleans, so compare explicitly", node);
|
|
427
|
+
}
|
|
428
|
+
return translateExpression(node, scope, context);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
// Negates a condition, cancelling ! and turning a negated || into a conjunction of guards, so that
|
|
432
|
+
// TLC sees the same formula as a hand-written spec and counts the same successors.
|
|
433
|
+
export const translateNegatedCondition = (
|
|
434
|
+
node: ts.Expression,
|
|
435
|
+
scope: Scope,
|
|
436
|
+
context: Context,
|
|
437
|
+
): string => {
|
|
438
|
+
if (ts.isParenthesizedExpression(node)) {
|
|
439
|
+
return translateNegatedCondition(node.expression, scope, context);
|
|
440
|
+
}
|
|
441
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.ExclamationToken) {
|
|
442
|
+
return translateCondition(node.operand, scope, context);
|
|
443
|
+
}
|
|
444
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.BarBarToken) {
|
|
445
|
+
const left = translateNegatedCondition(node.left, scope, context);
|
|
446
|
+
const right = translateNegatedCondition(node.right, scope, context);
|
|
447
|
+
return `${wrap(left)} /\\ ${wrap(right)}`;
|
|
448
|
+
}
|
|
449
|
+
return `~${wrap(translateCondition(node, scope, context))}`;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
// An expression as a TLA+ expression. Under a non-null assertion, isDefined skips the check that a
|
|
453
|
+
// lookup such as map.get(key) found a value.
|
|
454
|
+
export const translateExpression = (
|
|
455
|
+
node: ts.Expression,
|
|
456
|
+
scope: Scope,
|
|
457
|
+
context: Context,
|
|
458
|
+
isDefined = false,
|
|
459
|
+
): string => {
|
|
460
|
+
if (
|
|
461
|
+
ts.isParenthesizedExpression(node) ||
|
|
462
|
+
ts.isAsExpression(node) ||
|
|
463
|
+
ts.isSatisfiesExpression(node) ||
|
|
464
|
+
ts.isTypeAssertionExpression(node)
|
|
465
|
+
) {
|
|
466
|
+
return translateExpression(node.expression, scope, context, isDefined);
|
|
467
|
+
}
|
|
468
|
+
if (ts.isNonNullExpression(node)) {
|
|
469
|
+
return translateExpression(node.expression, scope, context, true);
|
|
470
|
+
}
|
|
471
|
+
if (ts.isNumericLiteral(node)) {
|
|
472
|
+
if (!Number.isInteger(Number(node.text))) {
|
|
473
|
+
throw new TlaTypeScriptError(`Only integers are supported: ${node.text}`, node);
|
|
474
|
+
}
|
|
475
|
+
return String(Number(node.text));
|
|
476
|
+
}
|
|
477
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
478
|
+
return translateString(node.text, context);
|
|
479
|
+
}
|
|
480
|
+
if (ts.isTemplateExpression(node)) return translateTemplate(node, scope, context);
|
|
481
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword) return "TRUE";
|
|
482
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword) return "FALSE";
|
|
483
|
+
if (node.kind === ts.SyntaxKind.NullKeyword) {
|
|
484
|
+
context.prelude.add("Null");
|
|
485
|
+
return "Null";
|
|
486
|
+
}
|
|
487
|
+
if (ts.isIdentifier(node)) return translateIdentifier(node, scope, context);
|
|
488
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.ExclamationToken) {
|
|
489
|
+
if (!isBooleanLike(node.operand, context)) {
|
|
490
|
+
throw new TlaTypeScriptError("! needs a boolean, so compare explicitly", node);
|
|
491
|
+
}
|
|
492
|
+
return `~${wrap(translateExpression(node.operand, scope, context))}`;
|
|
493
|
+
}
|
|
494
|
+
if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.MinusToken) {
|
|
495
|
+
return `-${wrap(translateExpression(node.operand, scope, context))}`;
|
|
496
|
+
}
|
|
497
|
+
if (ts.isBinaryExpression(node)) return translateBinary(node, scope, context);
|
|
498
|
+
if (ts.isConditionalExpression(node)) {
|
|
499
|
+
return `IF ${translateCondition(node.condition, scope, context)} THEN ${translateExpression(node.whenTrue, scope, context)} ELSE ${translateExpression(node.whenFalse, scope, context)}`;
|
|
500
|
+
}
|
|
501
|
+
if (ts.isArrayLiteralExpression(node)) return translateArrayLiteral(node, scope, context);
|
|
502
|
+
if (ts.isObjectLiteralExpression(node)) return translateObjectLiteral(node, scope, context);
|
|
503
|
+
if (ts.isPropertyAccessExpression(node)) return translatePropertyAccess(node, scope, context);
|
|
504
|
+
if (ts.isElementAccessExpression(node)) return translateElementAccess(node, scope, context);
|
|
505
|
+
if (ts.isCallExpression(node)) return translateCall(node, scope, context, isDefined);
|
|
506
|
+
if (ts.isNewExpression(node)) return translateNew(node, scope, context);
|
|
507
|
+
throw new TlaTypeScriptError(`Unsupported expression: ${node.getText()}`, node);
|
|
508
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// A TLA+ expression with the structure tla-ts generates for function bodies. Keeping the
|
|
2
|
+
// structure until rendering lets disabled paths simplify away and keeps bulleted lists aligned.
|
|
3
|
+
export type Formula = TextFormula | DisabledFormula | AndFormula | IfFormula | LetFormula;
|
|
4
|
+
|
|
5
|
+
interface TextFormula {
|
|
6
|
+
kind: "text";
|
|
7
|
+
text: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// A path through an action that assigns nothing, which TLA+ writes as FALSE.
|
|
11
|
+
interface DisabledFormula {
|
|
12
|
+
kind: "disabled";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface AndFormula {
|
|
16
|
+
kind: "and";
|
|
17
|
+
items: Formula[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface IfFormula {
|
|
21
|
+
kind: "if";
|
|
22
|
+
condition: string;
|
|
23
|
+
whenTrue: Formula;
|
|
24
|
+
whenFalse: Formula;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface LetFormula {
|
|
28
|
+
kind: "let";
|
|
29
|
+
definitions: string[];
|
|
30
|
+
body: Formula;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const DISABLED: Formula = { kind: "disabled" };
|
|
34
|
+
|
|
35
|
+
export const text = (value: string): Formula => ({ kind: "text", text: value });
|
|
36
|
+
|
|
37
|
+
export const and = (items: Formula[]): Formula => {
|
|
38
|
+
const flattened = items.flatMap((item) => (item.kind === "and" ? item.items : [item]));
|
|
39
|
+
if (flattened.some((item) => item.kind === "disabled")) return DISABLED;
|
|
40
|
+
const kept = flattened.filter((item) => item.kind !== "text" || item.text !== "TRUE");
|
|
41
|
+
if (kept.length === 0) return text("TRUE");
|
|
42
|
+
return kept.length === 1 ? kept[0] : { kind: "and", items: kept };
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const ifThenElse = (
|
|
46
|
+
condition: string,
|
|
47
|
+
whenTrue: Formula,
|
|
48
|
+
whenFalse: Formula,
|
|
49
|
+
negated = `~${wrap(condition)}`,
|
|
50
|
+
): Formula => {
|
|
51
|
+
if (whenTrue.kind === "disabled" && whenFalse.kind === "disabled") return DISABLED;
|
|
52
|
+
if (whenFalse.kind === "disabled") return and([text(condition), whenTrue]);
|
|
53
|
+
if (whenTrue.kind === "disabled") return and([text(negated), whenFalse]);
|
|
54
|
+
return { kind: "if", condition, whenTrue, whenFalse };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const letIn = (definitions: string[], body: Formula): Formula => {
|
|
58
|
+
if (body.kind === "disabled" || definitions.length === 0) return body;
|
|
59
|
+
if (body.kind === "let") return letIn([...definitions, ...body.definitions], body.body);
|
|
60
|
+
return { kind: "let", definitions, body };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const prefixLines = (lines: string[], first: string, rest: string) =>
|
|
64
|
+
lines.map((line, index) => `${index === 0 ? first : rest}${line}`);
|
|
65
|
+
|
|
66
|
+
const renderLines = (formula: Formula): string[] => {
|
|
67
|
+
if (formula.kind === "disabled") return ["FALSE"];
|
|
68
|
+
if (formula.kind === "text") return formula.text.split("\n");
|
|
69
|
+
if (formula.kind === "and") {
|
|
70
|
+
return formula.items.flatMap((item) => prefixLines(renderLines(item), "/\\ ", " "));
|
|
71
|
+
}
|
|
72
|
+
if (formula.kind === "if") {
|
|
73
|
+
return [
|
|
74
|
+
...prefixLines(formula.condition.split("\n"), "IF ", " "),
|
|
75
|
+
...prefixLines(renderLines(formula.whenTrue), "THEN ", " "),
|
|
76
|
+
...prefixLines(renderLines(formula.whenFalse), "ELSE ", " "),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
return [
|
|
80
|
+
...formula.definitions.flatMap((definition, index) =>
|
|
81
|
+
prefixLines(definition.split("\n"), index === 0 ? "LET " : " ", " "),
|
|
82
|
+
),
|
|
83
|
+
...prefixLines(renderLines(formula.body), "IN ", " "),
|
|
84
|
+
];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const render = (formula: Formula) => renderLines(formula).join("\n");
|
|
88
|
+
|
|
89
|
+
const OPENING = new Set(["(", "[", "{"]);
|
|
90
|
+
const CLOSING = new Set([")", "]", "}"]);
|
|
91
|
+
|
|
92
|
+
interface Scan {
|
|
93
|
+
depth: number;
|
|
94
|
+
isQuoted: boolean;
|
|
95
|
+
isEscaped: boolean;
|
|
96
|
+
isSkipped: boolean;
|
|
97
|
+
surface: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The expression without anything inside brackets, tuples, or strings, which leaves the operators
|
|
101
|
+
// that apply to the whole expression.
|
|
102
|
+
const getSurface = (expression: string) =>
|
|
103
|
+
[...expression].reduce<Scan>(
|
|
104
|
+
(scan, character, index) => {
|
|
105
|
+
if (scan.isSkipped) return { ...scan, isSkipped: false };
|
|
106
|
+
if (scan.isQuoted) {
|
|
107
|
+
return {
|
|
108
|
+
...scan,
|
|
109
|
+
isQuoted: scan.isEscaped || character !== '"',
|
|
110
|
+
isEscaped: !scan.isEscaped && character === "\\",
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const pair = expression.slice(index, index + 2);
|
|
114
|
+
if (pair === "<<") return { ...scan, depth: scan.depth + 1, isSkipped: true };
|
|
115
|
+
if (pair === ">>" && scan.depth > 0) {
|
|
116
|
+
return { ...scan, depth: scan.depth - 1, isSkipped: true };
|
|
117
|
+
}
|
|
118
|
+
if (character === '"') return { ...scan, isQuoted: true };
|
|
119
|
+
if (OPENING.has(character)) return { ...scan, depth: scan.depth + 1 };
|
|
120
|
+
if (CLOSING.has(character)) return { ...scan, depth: scan.depth - 1 };
|
|
121
|
+
return scan.depth === 0 ? { ...scan, surface: scan.surface + character } : scan;
|
|
122
|
+
},
|
|
123
|
+
{ depth: 0, isQuoted: false, isEscaped: false, isSkipped: false, surface: "" },
|
|
124
|
+
).surface;
|
|
125
|
+
|
|
126
|
+
// Whether an expression is a single term, so that it can be an operand without parentheses.
|
|
127
|
+
const isAtomic = (expression: string) =>
|
|
128
|
+
expression !== "" &&
|
|
129
|
+
!/^[~-]/.test(expression) &&
|
|
130
|
+
!expression.includes("\n") &&
|
|
131
|
+
!getSurface(expression).includes(" ");
|
|
132
|
+
|
|
133
|
+
export const wrap = (expression: string) => (isAtomic(expression) ? expression : `(${expression})`);
|
|
134
|
+
|
|
135
|
+
// Operators that bind no tighter than =, so that x = a = b would be ambiguous.
|
|
136
|
+
const LOOSE_OPERATORS = [
|
|
137
|
+
" = ",
|
|
138
|
+
" # ",
|
|
139
|
+
" < ",
|
|
140
|
+
" > ",
|
|
141
|
+
" <= ",
|
|
142
|
+
" >= ",
|
|
143
|
+
" \\in ",
|
|
144
|
+
" \\notin ",
|
|
145
|
+
" \\subseteq ",
|
|
146
|
+
" /\\ ",
|
|
147
|
+
" \\/ ",
|
|
148
|
+
" => ",
|
|
149
|
+
" ~> ",
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
// Parenthesizes the value on the right of = when it is itself a comparison or a logical formula.
|
|
153
|
+
export const wrapEquated = (expression: string) =>
|
|
154
|
+
expression.startsWith("~") ||
|
|
155
|
+
LOOSE_OPERATORS.some((operator) => getSurface(expression).includes(operator))
|
|
156
|
+
? `(${expression})`
|
|
157
|
+
: expression;
|
|
158
|
+
|
|
159
|
+
export const indent = (expression: string, prefix: string) =>
|
|
160
|
+
expression.split("\n").join(`\n${prefix}`);
|