tla-ts 0.0.8 → 0.0.9-dev.0dd11f695

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 (119) hide show
  1. package/README.md +30 -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 +40 -40
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +98 -19
  8. package/dist/index.d.ts +98 -19
  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-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +1 -1
  20. package/dist/transfer.cjs +128 -106
  21. package/dist/transfer.js +128 -106
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +605 -311
  30. package/src/cli/options.ts +12 -3
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlc-arguments.ts +646 -0
  35. package/src/cli/tlc-usage.ts +362 -0
  36. package/src/cli/tlc.ts +571 -185
  37. package/src/cli/typescript.ts +26 -0
  38. package/src/cli/xml-exporter.ts +4 -10
  39. package/src/codegen.ts +190 -51
  40. package/src/config.ts +101 -68
  41. package/src/coverage.ts +855 -0
  42. package/src/dependencies.ts +340 -24
  43. package/src/dfid.ts +636 -0
  44. package/src/environment.ts +289 -109
  45. package/src/errors.ts +142 -45
  46. package/src/evaluate.ts +734 -980
  47. package/src/exit-codes.ts +37 -1
  48. package/src/index.ts +3 -0
  49. package/src/instances.ts +85 -9
  50. package/src/java-overrides.ts +2 -12
  51. package/src/json-reader.ts +456 -0
  52. package/src/json.ts +262 -139
  53. package/src/levels.ts +2 -3
  54. package/src/lexer.ts +150 -70
  55. package/src/liveness-checker.ts +10 -18
  56. package/src/liveness.ts +122 -64
  57. package/src/parallel-worker.ts +14 -10
  58. package/src/parallel.ts +41 -17
  59. package/src/parser.ts +436 -90
  60. package/src/pcal/ast.ts +7 -28
  61. package/src/pcal/parse.ts +11 -39
  62. package/src/pcal/tokenize.ts +4 -20
  63. package/src/pcal/translate.ts +10 -42
  64. package/src/pcal/validate.ts +3 -21
  65. package/src/pretty-print.ts +1 -2
  66. package/src/prover/smt/solver/solver.ts +31 -27
  67. package/src/randomization.ts +84 -35
  68. package/src/resolve.ts +18 -60
  69. package/src/runtime.ts +11 -4
  70. package/src/sany/builtins.ts +3 -4
  71. package/src/sany/generator.ts +128 -176
  72. package/src/sany/levels.ts +6 -30
  73. package/src/sany/lexer.ts +7 -23
  74. package/src/sany/linter.ts +31 -29
  75. package/src/sany/modules.ts +1 -5
  76. package/src/sany/nodes.ts +4 -28
  77. package/src/sany/parse-error.ts +44 -100
  78. package/src/sany/report.ts +25 -40
  79. package/src/sany/selectors.ts +8 -22
  80. package/src/sany/source.ts +8 -15
  81. package/src/sany/xml.ts +11 -42
  82. package/src/search.ts +550 -150
  83. package/src/simulate.ts +694 -0
  84. package/src/spec-description.ts +167 -49
  85. package/src/standard-definitions.ts +2 -3
  86. package/src/standard-modules.ts +264 -68
  87. package/src/state-graph.ts +51 -18
  88. package/src/subexpressions.ts +31 -5
  89. package/src/successors.ts +269 -85
  90. package/src/symmetry.ts +59 -147
  91. package/src/tableau.ts +1 -2
  92. package/src/tlc/fingerprint.ts +226 -151
  93. package/src/tlc/jar-data.ts +113 -6
  94. package/src/tlc/messages.ts +122 -37
  95. package/src/tlc/order.ts +56 -82
  96. package/src/tlc/random.ts +16 -16
  97. package/src/tlc/replay.ts +564 -212
  98. package/src/tlc/summary.ts +2 -4
  99. package/src/tlc/tlc-operators.ts +81 -36
  100. package/src/tlc/tlcext-operators.ts +138 -117
  101. package/src/tlc/trace-spec.ts +273 -0
  102. package/src/tlc/value-stream.ts +143 -0
  103. package/src/tlc/version.ts +2 -4
  104. package/src/typescript/actions.ts +494 -0
  105. package/src/typescript/collections.ts +615 -0
  106. package/src/typescript/compile.ts +70 -0
  107. package/src/typescript/context.ts +106 -0
  108. package/src/typescript/emit.ts +122 -0
  109. package/src/typescript/expressions.ts +508 -0
  110. package/src/typescript/formula.ts +160 -0
  111. package/src/typescript/functions.ts +295 -0
  112. package/src/typescript/model.ts +164 -0
  113. package/src/typescript/names.ts +116 -0
  114. package/src/typescript/program.ts +51 -0
  115. package/src/typescript/properties.ts +148 -0
  116. package/src/typescript/scope.ts +117 -0
  117. package/src/typescript/steps.ts +175 -0
  118. package/src/typescript/types.ts +178 -0
  119. package/src/values.ts +910 -318
package/src/json.ts CHANGED
@@ -1,210 +1,333 @@
1
1
  import {
2
- createJavaOverrideError,
3
2
  createModuleArgumentError,
3
+ defineLazyOperator,
4
4
  defineStateOperator,
5
5
  getJavaClassName,
6
+ getThunk,
6
7
  getValue,
8
+ isOutOfRange,
9
+ overrideWithJava,
7
10
  } from "./builtins";
8
- import type { Builtin } from "./evaluate";
11
+ import { TlaEvaluationError, TlaUnsupportedError } from "./errors";
12
+ import type { Builtin, BuiltinArgument, Context } from "./evaluate";
13
+ import { readJson, type JsonElement } from "./json-reader";
9
14
  import {
15
+ applyFunction,
10
16
  createRecord,
11
17
  createTuple,
12
18
  formatValue,
13
19
  getSequenceElements,
20
+ getPrintedPairs,
14
21
  isFunctionValue,
15
- isSetValue,
22
+ isTlcEnumerable,
16
23
  toFiniteFunction,
17
24
  toFiniteSet,
25
+ type FunctionValue,
18
26
  type Value,
19
27
  rankString,
20
28
  } from "./values";
21
29
 
22
30
  export interface FileSystem {
23
31
  readFile: ((path: string) => string | undefined) | undefined;
24
- writeFile: ((path: string, content: string) => void) | undefined;
32
+ writeFile: ((path: string, content: string | Uint8Array) => void) | undefined;
33
+ makeDirectory: ((path: string) => void) | undefined;
25
34
  }
26
35
 
27
- interface ParseContext {
28
- source?: string;
29
- }
36
+ const OPEN_OPTIONS = new Set([
37
+ "READ",
38
+ "WRITE",
39
+ "APPEND",
40
+ "TRUNCATE_EXISTING",
41
+ "CREATE",
42
+ "CREATE_NEW",
43
+ "DELETE_ON_CLOSE",
44
+ "SPARSE",
45
+ "SYNC",
46
+ "DSYNC",
47
+ ]);
48
+
49
+ const UTF_8_NAMES = new Set(["utf-8", "utf8", "unicode-1-1-utf-8"]);
50
+
51
+ const STANDARD_CHARSETS = new Set(["us-ascii", "iso-8859-1", "utf-16", "utf-16be", "utf-16le"]);
52
+
53
+ const toRecord = (value: Value) => {
54
+ if (!isFunctionValue(value)) return undefined;
55
+ const functionValue = toFiniteFunction(value);
56
+ const fields = functionValue.domain.flatMap((key, index): [string, Value][] =>
57
+ typeof key === "string" ? [[key, functionValue.range[index]]] : [],
58
+ );
59
+ return fields.length === functionValue.domain.length ? createRecord(fields) : undefined;
60
+ };
30
61
 
31
- const TO_JSON_SIGNATURE =
32
- "public static tlc2.value.impl.StringValue tlc2.module.Json.toJson(tlc2.value.IValue) throws java.io.IOException";
33
- const TO_JSON_ARRAY_SIGNATURE =
34
- "public static tlc2.value.impl.StringValue tlc2.module.Json.toJsonArray(tlc2.value.IValue) throws java.io.IOException";
35
- const TO_JSON_OBJECT_SIGNATURE =
36
- "public static tlc2.value.impl.StringValue tlc2.module.Json.toJsonObject(tlc2.value.IValue) throws java.io.IOException";
37
- const SERIALIZE_SIGNATURE =
38
- "public static synchronized tlc2.value.impl.BoolValue tlc2.module.Json.serialize(tlc2.value.impl.StringValue,tlc2.value.impl.Value) throws java.io.IOException";
39
- const ND_SERIALIZE_SIGNATURE =
40
- "public static synchronized tlc2.value.impl.BoolValue tlc2.module.Json.ndSerialize(tlc2.value.impl.StringValue,tlc2.value.impl.Value) throws java.io.IOException";
41
- const DESERIALIZE_SIGNATURE =
42
- "public static tlc2.value.IValue tlc2.module.Json.deserialize(tlc2.value.impl.StringValue) throws java.io.IOException";
43
- const ND_DESERIALIZE_SIGNATURE =
44
- "public static tlc2.value.IValue tlc2.module.Json.ndDeserialize(tlc2.value.impl.StringValue) throws java.io.IOException";
45
-
46
- const isRecord = (value: Value) => {
47
- if (!isFunctionValue(value)) return false;
48
- const domain = toFiniteFunction(value).domain;
49
- return domain.length > 0 && domain.every((key) => typeof key === "string");
62
+ const createClassCastError = (value: Value, javaClass: string) => {
63
+ const valueClass = `tlc2.value.impl.${getJavaClassName(value)}`;
64
+ const targetClass = `tlc2.value.impl.${javaClass}`;
65
+ return new TlaEvaluationError(
66
+ `class ${valueClass} cannot be cast to class ${targetClass} (${valueClass} and ${targetClass} are in unnamed module of loader 'app')`,
67
+ );
50
68
  };
51
69
 
52
- const createUnsupportedError = (method: string, value: Value) =>
53
- createJavaOverrideError(
54
- method,
70
+ const createUnsupportedError = (value: Value) =>
71
+ new TlaEvaluationError(
55
72
  `Cannot convert value: unsupported value type tlc2.value.impl.${getJavaClassName(value)}`,
56
73
  );
57
74
 
58
75
  const toJsonString = (text: string) =>
59
76
  JSON.stringify(text).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029");
60
77
 
61
- const toSetJson = (value: Value, method: string) => {
62
- if (typeof value === "object" && (value.kind === "InfiniteSet" || value.kind === "SequenceSet")) {
63
- throw createUnsupportedError(method, value);
64
- }
65
- return `[${toFiniteSet(value)
66
- .elements.map((element) => toJson(element, method))
67
- .join(",")}]`;
68
- };
78
+ const toArrayJson = (elements: Value[]) => `[${elements.map(toJson).join(",")}]`;
69
79
 
70
- const toFunctionJson = (value: Value, method: string) => {
71
- const functionValue = toFiniteFunction(value);
72
- const elements = getSequenceElements(functionValue);
73
- if (elements) return `[${elements.map((element) => toJson(element, method)).join(",")}]`;
74
- return `{${functionValue.domain
80
+ const toObjectJson = (functionValue: FunctionValue) =>
81
+ `{${getPrintedPairs(functionValue)
75
82
  .map(
76
- (key, index) =>
77
- `${toJsonString(typeof key === "string" ? key : formatValue(key))}:${toJson(functionValue.range[index], method)}`,
83
+ (pair) =>
84
+ `${toJsonString(typeof pair[0] === "string" ? pair[0] : formatValue(pair[0]))}:${toJson(pair[1])}`,
78
85
  )
79
86
  .join(",")}}`;
87
+
88
+ const toFunctionJson = (value: Value) => {
89
+ const functionValue = toFiniteFunction(value);
90
+ const elements = getSequenceElements(functionValue);
91
+ return elements ? toArrayJson(elements) : toObjectJson(functionValue);
80
92
  };
81
93
 
82
- const toJson = (value: Value, method: string): string => {
94
+ const isFunctionClass = (className: string) =>
95
+ className === "FcnRcdValue" || className === "FcnLambdaValue";
96
+
97
+ const toJson = (value: Value): string => {
83
98
  if (typeof value === "string") return toJsonString(value);
84
99
  if (typeof value !== "object") return String(value);
85
100
  if (value.kind === "ModelValue") return toJsonString(value.name);
86
- if (isSetValue(value)) return toSetJson(value, method);
87
- return toFunctionJson(value, method);
101
+ const className = getJavaClassName(value);
102
+ if (className === "RecordValue") return toObjectJson(toFiniteFunction(value));
103
+ if (className === "TupleValue") return toArrayJson(toFiniteFunction(value).range);
104
+ if (isFunctionClass(className)) return toFunctionJson(value);
105
+ if (isTlcEnumerable(value)) return toArrayJson(toFiniteSet(value).elements);
106
+ throw createUnsupportedError(value);
88
107
  };
89
108
 
90
109
  const toJsonArray = (value: Value) => {
91
- if (isSetValue(value)) return toSetJson(value, TO_JSON_ARRAY_SIGNATURE);
92
- if (!isFunctionValue(value) || isRecord(value))
93
- throw createUnsupportedError(TO_JSON_ARRAY_SIGNATURE, value);
94
- return toFunctionJson(value, TO_JSON_ARRAY_SIGNATURE);
110
+ const className = getJavaClassName(value);
111
+ if (className === "TupleValue") return toArrayJson(toFiniteFunction(value).range);
112
+ if (isFunctionClass(className)) return toFunctionJson(value);
113
+ if (isTlcEnumerable(value)) return toArrayJson(toFiniteSet(value).elements);
114
+ throw createUnsupportedError(value);
95
115
  };
96
116
 
97
117
  const toJsonObject = (value: Value) => {
98
- if (!isFunctionValue(value)) throw createUnsupportedError(TO_JSON_OBJECT_SIGNATURE, value);
99
- return toFunctionJson(value, TO_JSON_OBJECT_SIGNATURE);
100
- };
101
-
102
- // TLC reads a JSON number with Gson's intValue, which truncates the exact decimal toward zero and
103
- // keeps its low 32 bits, so 3000000000 becomes -1294967296 and 1.5 becomes 1.
104
- const toInteger = (source: string) => {
105
- const [, sign, whole = "0", fraction = "", exponent = "0"] =
106
- /^(-?)(\d+)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(source) ?? [];
107
- const digits = BigInt(whole + fraction);
108
- const shift = Number(exponent) - fraction.length;
109
- const magnitude = shift >= 0 ? digits * 10n ** BigInt(shift) : digits / 10n ** BigInt(-shift);
110
- return Number(BigInt.asIntN(32, sign === "-" ? -magnitude : magnitude));
118
+ const className = getJavaClassName(value);
119
+ if (className === "RecordValue") return toObjectJson(toFiniteFunction(value));
120
+ if (className === "TupleValue") {
121
+ return `{${toFiniteFunction(value)
122
+ .range.map((element, index) => `"${index}":${toJson(element)}`)
123
+ .join(",")}}`;
124
+ }
125
+ if (isFunctionClass(className)) return toFunctionJson(value);
126
+ throw createUnsupportedError(value);
111
127
  };
112
128
 
113
- const fromJson = (json: unknown, method: string): Value => {
114
- if (typeof json === "boolean" || typeof json === "number") return json;
115
- if (typeof json === "string") return rankString(json);
116
- if (Array.isArray(json)) return createTuple(json.map((element) => fromJson(element, method)));
117
- if (json !== null && typeof json === "object") {
129
+ const fromJson = (element: JsonElement): Value => {
130
+ if (element === null) {
131
+ throw new TlaEvaluationError("Cannot convert value: unsupported JSON value null");
132
+ }
133
+ if (typeof element === "boolean") return element;
134
+ if (typeof element === "string") return rankString(element);
135
+ if (Array.isArray(element)) return createTuple(element.map(fromJson));
136
+ if (element instanceof Map) {
118
137
  return createRecord(
119
- Object.entries(json).map(([key, field]): [string, Value] => [
120
- rankString(key),
121
- fromJson(field, method),
122
- ]),
138
+ [...element].map((member): [string, Value] => [rankString(member[0]), fromJson(member[1])]),
123
139
  );
124
140
  }
125
- throw createJavaOverrideError(
126
- method,
127
- `Cannot convert value: unsupported JSON value ${String(json)}`,
128
- );
129
- };
130
-
131
- const parseJson = (text: string, method: string) => {
132
- if (text.trim() === "") return fromJson(null, method);
133
- try {
134
- return fromJson(
135
- JSON.parse(text, (_, value: unknown, context?: ParseContext) =>
136
- typeof value === "number" ? toInteger(context?.source ?? String(value)) : value,
137
- ),
138
- method,
141
+ const value = Number(element.literal);
142
+ if (!/^-?\d+$/.test(element.literal) || isOutOfRange(value)) {
143
+ throw new TlaEvaluationError(
144
+ `Cannot convert value: expected a 32-bit integer JSON number, but found ${element.literal}`,
139
145
  );
140
- } catch (error) {
141
- if (!(error instanceof SyntaxError)) throw error;
142
- throw createJavaOverrideError(method, error.message);
143
146
  }
147
+ return value || 0;
144
148
  };
145
149
 
146
- const getPath = (value: Value, method: string) => {
150
+ export const getPath = (value: Value) => {
147
151
  if (typeof value !== "string") {
148
- throw createJavaOverrideError(
149
- method,
152
+ throw new TlaEvaluationError(
150
153
  `Cannot cast tlc2.value.impl.${getJavaClassName(value)} to tlc2.value.impl.StringValue`,
151
154
  );
152
155
  }
153
156
  return value;
154
157
  };
155
158
 
156
- const readText = (files: FileSystem, path: string, method: string) => {
157
- const text = files.readFile?.(path);
158
- if (text === undefined) {
159
- throw createJavaOverrideError(method, `${path} (No such file or directory)`);
159
+ const getStringOption = (options: Value, field: string) => {
160
+ const value = applyFunction(options, field);
161
+ if (typeof value !== "string") throw createClassCastError(value, "StringValue");
162
+ return value;
163
+ };
164
+
165
+ const checkCharset = (charset: string) => {
166
+ if (UTF_8_NAMES.has(charset.toLowerCase())) return;
167
+ if (STANDARD_CHARSETS.has(charset.toLowerCase())) {
168
+ throw new TlaUnsupportedError(`IOUtils!Serialize does not support the charset ${charset}`);
160
169
  }
170
+ throw new TlaEvaluationError(
171
+ /^[a-zA-Z\d][\w.:+-]*$/.test(charset)
172
+ ? `java.nio.charset.UnsupportedCharsetException: ${charset}`
173
+ : `java.nio.charset.IllegalCharsetNameException: ${charset}`,
174
+ );
175
+ };
176
+
177
+ // Files.newBufferedWriter with the given StandardOpenOption names, which writes the text over the
178
+ // start of a file that it neither truncates nor appends to.
179
+ const writeWithOptions = (files: FileSystem, path: string, names: string[], text: string) => {
180
+ const unknown = names.find((name) => !OPEN_OPTIONS.has(name));
181
+ if (unknown !== undefined) {
182
+ throw new TlaEvaluationError(
183
+ `java.lang.IllegalArgumentException: No enum constant java.nio.file.StandardOpenOption.${unknown}`,
184
+ );
185
+ }
186
+ if (names.includes("READ")) {
187
+ throw new TlaEvaluationError("java.lang.IllegalArgumentException: READ not allowed");
188
+ }
189
+ const options = new Set(names.length === 0 ? ["CREATE", "TRUNCATE_EXISTING"] : names);
190
+ if (options.has("APPEND") && options.has("TRUNCATE_EXISTING")) {
191
+ throw new TlaEvaluationError(
192
+ "java.lang.IllegalArgumentException: APPEND + TRUNCATE_EXISTING not allowed",
193
+ );
194
+ }
195
+ if (options.has("DELETE_ON_CLOSE")) {
196
+ throw new TlaUnsupportedError("IOUtils!Serialize does not support DELETE_ON_CLOSE");
197
+ }
198
+ const existing = files.readFile?.(path);
199
+ if (existing === undefined && !options.has("CREATE") && !options.has("CREATE_NEW")) {
200
+ throw new TlaEvaluationError(`java.nio.file.NoSuchFileException: ${path}`);
201
+ }
202
+ if (existing !== undefined && options.has("CREATE_NEW")) {
203
+ throw new TlaEvaluationError(`java.nio.file.FileAlreadyExistsException: ${path}`);
204
+ }
205
+ if (existing === undefined || options.has("TRUNCATE_EXISTING")) {
206
+ files.writeFile?.(path, text);
207
+ return;
208
+ }
209
+ files.writeFile?.(
210
+ path,
211
+ options.has("APPEND") ? existing + text : text + existing.slice(text.length),
212
+ );
213
+ };
214
+
215
+ // Java's File#getParentFile has no parent for a file name without a directory.
216
+ const makeParentDirectory = (files: FileSystem, path: string) => {
217
+ const separator = path.lastIndexOf("/");
218
+ if (separator !== -1) files.makeDirectory?.(path.slice(0, Math.max(separator, 1)));
219
+ };
220
+
221
+ const readText = (files: FileSystem, path: string) => {
222
+ const text = files.readFile?.(path);
223
+ if (text === undefined) throw new TlaEvaluationError(`${path} (No such file or directory)`);
161
224
  return text;
162
225
  };
163
226
 
164
227
  export const createJsonOperators = (files: FileSystem): Builtin[] => [
165
- defineStateOperator("ToJson", 1, (args) =>
166
- rankString(toJson(getValue(args, 0, "ToJson"), TO_JSON_SIGNATURE)),
228
+ overrideWithJava(
229
+ defineStateOperator("ToJson", 1, (args) => rankString(toJson(getValue(args, 0, "ToJson")))),
230
+ "public static tlc2.value.impl.StringValue tlc2.module.Json.toJson(tlc2.value.IValue) throws java.io.IOException",
167
231
  ),
168
- defineStateOperator("ToJsonArray", 1, (args) =>
169
- rankString(toJsonArray(getValue(args, 0, "ToJsonArray"))),
232
+ overrideWithJava(
233
+ defineStateOperator("ToJsonArray", 1, (args) =>
234
+ rankString(toJsonArray(getValue(args, 0, "ToJsonArray"))),
235
+ ),
236
+ "public static tlc2.value.impl.StringValue tlc2.module.Json.toJsonArray(tlc2.value.IValue) throws java.io.IOException",
170
237
  ),
171
- defineStateOperator("ToJsonObject", 1, (args) =>
172
- rankString(toJsonObject(getValue(args, 0, "ToJsonObject"))),
238
+ overrideWithJava(
239
+ defineStateOperator("ToJsonObject", 1, (args) =>
240
+ rankString(toJsonObject(getValue(args, 0, "ToJsonObject"))),
241
+ ),
242
+ "public static tlc2.value.impl.StringValue tlc2.module.Json.toJsonObject(tlc2.value.IValue) throws java.io.IOException",
243
+ ),
244
+ overrideWithJava(
245
+ defineStateOperator("JsonSerialize", 2, (args) => {
246
+ const path = getPath(getValue(args, 0, "JsonSerialize"));
247
+ const value = getValue(args, 1, "JsonSerialize");
248
+ if (!getSequenceElements(value) && !toRecord(value)) {
249
+ throw createModuleArgumentError("second", "JsonSerialize", "sequence or record", value);
250
+ }
251
+ makeParentDirectory(files, path);
252
+ files.writeFile?.(path, toJson(value));
253
+ return true;
254
+ }),
255
+ "public static synchronized tlc2.value.impl.BoolValue tlc2.module.Json.serialize(tlc2.value.impl.StringValue,tlc2.value.impl.Value) throws java.io.IOException",
256
+ ),
257
+ overrideWithJava(
258
+ defineStateOperator("ndJsonSerialize", 2, (args) => {
259
+ const path = getPath(getValue(args, 0, "ndJsonSerialize"));
260
+ const value = getValue(args, 1, "ndJsonSerialize");
261
+ const elements = getSequenceElements(value);
262
+ if (!elements) {
263
+ throw createModuleArgumentError("second", "ndJsonSerialize", "sequence", value);
264
+ }
265
+ makeParentDirectory(files, path);
266
+ files.writeFile?.(path, elements.map((element) => `${toJson(element)}\n`).join(""));
267
+ return true;
268
+ }),
269
+ "public static synchronized tlc2.value.impl.BoolValue tlc2.module.Json.ndSerialize(tlc2.value.impl.StringValue,tlc2.value.impl.Value) throws java.io.IOException",
270
+ ),
271
+ overrideWithJava(
272
+ defineStateOperator("JsonDeserialize", 1, (args) =>
273
+ fromJson(readJson(readText(files, getPath(getValue(args, 0, "JsonDeserialize"))))),
274
+ ),
275
+ "public static tlc2.value.IValue tlc2.module.Json.deserialize(tlc2.value.impl.StringValue) throws java.io.IOException",
276
+ ),
277
+ overrideWithJava(
278
+ defineStateOperator("ndJsonDeserialize", 1, (args) =>
279
+ createTuple(
280
+ readText(files, getPath(getValue(args, 0, "ndJsonDeserialize")))
281
+ .split(/\r\n|\r|\n/)
282
+ // Java's String#trim strips every character up to the space.
283
+ .map((line) => line.replace(/^[^!-\uffff]+|[^!-\uffff]+$/g, ""))
284
+ .filter((line) => line !== "")
285
+ .map((line) => fromJson(readJson(line))),
286
+ ),
287
+ ),
288
+ "public static tlc2.value.IValue tlc2.module.Json.ndDeserialize(tlc2.value.impl.StringValue) throws java.io.IOException",
289
+ ),
290
+ ];
291
+
292
+ // TLC's Json overrides IOUtils!Serialize, as the CommunityModules define it, for the NDJSON format
293
+ // and falls back to its definition for any other.
294
+ export const createIoUtilsOperators = (
295
+ files: FileSystem,
296
+ evaluateReplaced: (name: string, args: BuiltinArgument[], context: Context | undefined) => Value,
297
+ ): Builtin[] => [
298
+ overrideWithJava(
299
+ defineLazyOperator("Serialize", 3, (args, context) => {
300
+ const third = getThunk(args, 2, "Serialize")();
301
+ const options = toRecord(third);
302
+ if (!options) throw createModuleArgumentError("third", "ndJsonSerialize", "sequence", third);
303
+ if (getStringOption(options, "format") !== "NDJSON") {
304
+ return evaluateReplaced("Serialize", args, context);
305
+ }
306
+ const payload = getThunk(args, 0, "Serialize")();
307
+ const elements = getSequenceElements(payload);
308
+ if (!elements) throw createModuleArgumentError("first", "Serialize", "sequence", payload);
309
+ const path = getThunk(args, 1, "Serialize")();
310
+ if (typeof path !== "string") {
311
+ throw createModuleArgumentError("second", "ndJsonSerialize", "sequence", path);
312
+ }
313
+ const openOptions = applyFunction(options, "openOptions");
314
+ if (getJavaClassName(openOptions) !== "TupleValue") {
315
+ throw createClassCastError(openOptions, "TupleValue");
316
+ }
317
+ const charset = getStringOption(options, "charset");
318
+ const names = toFiniteFunction(openOptions).range.map((option) => {
319
+ if (typeof option !== "string") throw createClassCastError(option, "StringValue");
320
+ return option;
321
+ });
322
+ checkCharset(charset);
323
+ writeWithOptions(
324
+ files,
325
+ path,
326
+ names,
327
+ elements.map((element) => `${toJson(element)}\n`).join(""),
328
+ );
329
+ return true;
330
+ }),
331
+ "public static synchronized tlc2.value.impl.Value tlc2.module.Json.textSerialize(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
173
332
  ),
174
- defineStateOperator("JsonSerialize", 2, (args) => {
175
- const path = getPath(getValue(args, 0, "JsonSerialize"), SERIALIZE_SIGNATURE);
176
- const value = getValue(args, 1, "JsonSerialize");
177
- if (!getSequenceElements(value) && !isRecord(value)) {
178
- throw createModuleArgumentError("second", "JsonSerialize", "sequence or record", value);
179
- }
180
- files.writeFile?.(path, toJson(value, SERIALIZE_SIGNATURE));
181
- return true;
182
- }),
183
- defineStateOperator("ndJsonSerialize", 2, (args) => {
184
- const path = getPath(getValue(args, 0, "ndJsonSerialize"), ND_SERIALIZE_SIGNATURE);
185
- const value = getValue(args, 1, "ndJsonSerialize");
186
- const elements = getSequenceElements(value);
187
- if (!elements) {
188
- throw createModuleArgumentError("second", "ndJsonSerialize", "sequence", value);
189
- }
190
- files.writeFile?.(
191
- path,
192
- elements.map((element) => `${toJson(element, ND_SERIALIZE_SIGNATURE)}\n`).join(""),
193
- );
194
- return true;
195
- }),
196
- defineStateOperator("JsonDeserialize", 1, (args) => {
197
- const path = getPath(getValue(args, 0, "JsonDeserialize"), DESERIALIZE_SIGNATURE);
198
- return parseJson(readText(files, path, DESERIALIZE_SIGNATURE), DESERIALIZE_SIGNATURE);
199
- }),
200
- defineStateOperator("ndJsonDeserialize", 1, (args) => {
201
- const path = getPath(getValue(args, 0, "ndJsonDeserialize"), ND_DESERIALIZE_SIGNATURE);
202
- return createTuple(
203
- readText(files, path, ND_DESERIALIZE_SIGNATURE)
204
- .split(/\r\n|\r|\n/)
205
- .map((line) => line.trim())
206
- .filter((line) => line !== "")
207
- .map((line) => parseJson(line, ND_DESERIALIZE_SIGNATURE)),
208
- );
209
- }),
210
333
  ];
package/src/levels.ts CHANGED
@@ -61,7 +61,6 @@ interface Operand {
61
61
  declared?: OperatorParam;
62
62
  }
63
63
 
64
- // The levels of tla2sany.semantic.LevelConstants.
65
64
  export const CONSTANT_LEVEL = 0;
66
65
  export const VARIABLE_LEVEL = 1;
67
66
  export const ACTION_LEVEL = 2;
@@ -185,7 +184,6 @@ const bindFormals = (scope: Scope, formals: OperatorParam[]) =>
185
184
 
186
185
  const toFormals = (names: string[]) => names.map((name) => ({ name, arity: 0 }));
187
186
 
188
- // SANY reports a level error at the range of the expression it rejects.
189
187
  const createLevelError = (message: string, expression: Expression) =>
190
188
  expression.location.range
191
189
  ? createSemanticError(`${message}.`, expression.location.range)
@@ -436,6 +434,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
436
434
  data.params.add(name);
437
435
  return { data, declared: { name, arity: resolved.arity } };
438
436
  }
437
+ if (expression.kind === "OperatorReference") return { data: createData() };
439
438
  }
440
439
  return { data: analyze(expression, scope) };
441
440
  };
@@ -905,7 +904,7 @@ export const checkModuleLevels = (
905
904
  ? analyzeAssumeProve(body.statement, new Map(), analyzer).level
906
905
  : getLevel([body.statement]);
907
906
  };
908
- // SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
907
+ // The proof of a temporal goal may only assume constants.
909
908
  const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
910
909
  proof?.kind !== "Steps"
911
910
  ? []