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
@@ -43,8 +43,48 @@ const extendByte = (byte: number) => {
43
43
  high = (high >>> 8) ^ BYTE_MOD_HIGH[index];
44
44
  };
45
45
 
46
- const extendInt = (value: number) => {
47
- for (let shift = 0; shift < 32; shift += 8) extendByte(value >>> shift);
46
+ // Extending is linear over GF(2), so extending by four bytes at once shifts the fingerprint by a
47
+ // word and adds, for each of the four, the fingerprint from zero of that byte followed by the zero
48
+ // bytes that come after it: SLICE_HIGH[k] and SLICE_LOW[k] hold them for k zero bytes after.
49
+ const createSliceTables = () => {
50
+ const highs = [BYTE_MOD_HIGH];
51
+ const lows = [BYTE_MOD_LOW];
52
+ for (let zeros = 1; zeros < 4; zeros++) {
53
+ const previousHigh = highs[zeros - 1];
54
+ const previousLow = lows[zeros - 1];
55
+ const sliceHigh = new Int32Array(256);
56
+ const sliceLow = new Int32Array(256);
57
+ for (let byte = 0; byte < 256; byte++) {
58
+ const index = previousLow[byte] & 0xff;
59
+ sliceLow[byte] =
60
+ ((previousLow[byte] >>> 8) | (previousHigh[byte] << 24)) ^ BYTE_MOD_LOW[index];
61
+ sliceHigh[byte] = (previousHigh[byte] >>> 8) ^ BYTE_MOD_HIGH[index];
62
+ }
63
+ highs.push(sliceHigh);
64
+ lows.push(sliceLow);
65
+ }
66
+ return { highs, lows };
67
+ };
68
+
69
+ const SLICES = createSliceTables();
70
+ const SLICE_HIGH_0 = SLICES.highs[0];
71
+ const SLICE_HIGH_1 = SLICES.highs[1];
72
+ const SLICE_HIGH_2 = SLICES.highs[2];
73
+ const SLICE_HIGH_3 = SLICES.highs[3];
74
+ const SLICE_LOW_0 = SLICES.lows[0];
75
+ const SLICE_LOW_1 = SLICES.lows[1];
76
+ const SLICE_LOW_2 = SLICES.lows[2];
77
+ const SLICE_LOW_3 = SLICES.lows[3];
78
+
79
+ // Extends the fingerprint by the four bytes of a word, lowest first.
80
+ const extendInt = (word: number) => {
81
+ const mixed = low ^ word;
82
+ const first = mixed & 0xff;
83
+ const second = (mixed >>> 8) & 0xff;
84
+ const third = (mixed >>> 16) & 0xff;
85
+ const fourth = mixed >>> 24;
86
+ low = high ^ SLICE_LOW_3[first] ^ SLICE_LOW_2[second] ^ SLICE_LOW_1[third] ^ SLICE_LOW_0[fourth];
87
+ high = SLICE_HIGH_3[first] ^ SLICE_HIGH_2[second] ^ SLICE_HIGH_1[third] ^ SLICE_HIGH_0[fourth];
48
88
  };
49
89
 
50
90
  const BOOL_VALUE = 0;
@@ -71,9 +111,16 @@ const extendScalar = (value: boolean | number | string | ModelValue) => {
71
111
  if (typeof value === "string") {
72
112
  extendByte(STRING_VALUE);
73
113
  extendInt(value.length);
74
- for (let index = 0; index < value.length; index++) {
75
- extendByte(value.charCodeAt(index));
114
+ const whole = value.length & ~3;
115
+ for (let index = 0; index < whole; index += 4) {
116
+ extendInt(
117
+ (value.charCodeAt(index) & 0xff) |
118
+ ((value.charCodeAt(index + 1) & 0xff) << 8) |
119
+ ((value.charCodeAt(index + 2) & 0xff) << 16) |
120
+ ((value.charCodeAt(index + 3) & 0xff) << 24),
121
+ );
76
122
  }
123
+ for (let index = whole; index < value.length; index++) extendByte(value.charCodeAt(index));
77
124
  return 5 + value.length;
78
125
  }
79
126
  extendByte(MODEL_VALUE);
@@ -114,116 +161,22 @@ export const getTlcFingerprint = (state: Value[]) => {
114
161
  return fingerprint;
115
162
  };
116
163
 
117
- // The fingerprint after `length` zero bytes, as one table per byte of the fingerprint it shifts.
118
- const createShiftTable = (length: number) => {
119
- const table = new Int32Array(8 * 256 * 2);
120
- for (let bit = 0; bit < 64; bit++) {
121
- high = bit < 32 ? 0 : 1 << (bit - 32);
122
- low = bit < 32 ? 1 << bit : 0;
123
- for (let step = 0; step < length; step++) extendByte(0);
124
- const position = bit >> 3;
125
- const mask = 1 << (bit & 7);
126
- for (let byte = mask; byte < 2 * mask; byte++) {
127
- const entry = (position * 256 + byte) * 2;
128
- const rest = (position * 256 + byte - mask) * 2;
129
- table[entry] = table[rest] ^ high;
130
- table[entry + 1] = table[rest + 1] ^ low;
131
- }
132
- }
133
- return table;
134
- };
135
-
136
- // The functions and sets a fingerprinter caches before it starts over, which bounds the values it
137
- // keeps alive.
138
- const CACHED_VALUE_LIMIT = 1 << 22;
139
-
140
- // Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
141
- // XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
142
- // The fingerprinter caches each distinct function or set it meets, nested ones included, across
143
- // every batch of states it fingerprints, as states share most of their values, even states of
144
- // different levels. It reads a state's variables in `order`.
145
- export const createTlcFingerprinter = (order: number[], limit = CACHED_VALUE_LIMIT) => {
146
- const shiftTables = new Map<number, Int32Array>();
147
- const ids = new Map<Value, number>();
148
- // The byte count and fingerprint from zero of each cached value, three numbers per id, and the
149
- // shift table for its byte count.
150
- const contributions: number[] = [];
151
- const contributionShiftTables: Int32Array[] = [];
152
- const getShiftTable = (length: number) => {
153
- const cached = shiftTables.get(length);
154
- if (cached) return cached;
155
- const table = createShiftTable(length);
156
- shiftTables.set(length, table);
157
- return table;
158
- };
159
- // Extends the fingerprint by a value's bytes and returns how many there are.
160
- const extendElement = (value: Value): number => {
161
- if (typeof value !== "object" || isModelValue(value)) return extendScalar(value);
162
- const id = getId(value);
163
- const table = contributionShiftTables[id];
164
- const shiftedHigh = high;
165
- const shiftedLow = low;
166
- high = contributions[3 * id + 1];
167
- low = contributions[3 * id + 2];
168
- for (let position = 0; position < 8; position++) {
169
- const word = position < 4 ? shiftedLow : shiftedHigh;
170
- const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
171
- high ^= table[entry];
172
- low ^= table[entry + 1];
173
- }
174
- return contributions[3 * id];
175
- };
176
- const getId = (value: Value) => {
177
- const cached = ids.get(value);
178
- if (cached !== undefined) return cached;
179
- const outerHigh = high;
180
- const outerLow = low;
181
- high = 0;
182
- low = 0;
183
- const length = { value: 5 };
184
- if (isFunctionValue(value)) {
185
- const functionValue = toFiniteFunction(value);
186
- extendByte(FCN_RCD_VALUE);
187
- extendInt(functionValue.domain.length);
188
- for (let index = 0; index < functionValue.domain.length; index++) {
189
- length.value += extendElement(functionValue.domain[index]);
190
- length.value += extendElement(functionValue.range[index]);
191
- }
192
- } else {
193
- const elements = toFiniteSet(value).elements;
194
- extendByte(SET_ENUM_VALUE);
195
- extendInt(elements.length);
196
- for (const element of elements) length.value += extendElement(element);
197
- }
198
- const id = contributionShiftTables.length;
199
- ids.set(value, id);
200
- contributions.push(length.value, high, low);
201
- contributionShiftTables.push(getShiftTable(length.value));
202
- high = outerHigh;
203
- low = outerLow;
204
- return id;
205
- };
206
- return (states: Value[][]) => {
207
- const fingerprints = new BigInt64Array(states.length);
208
- // Each fingerprint's low and high words, as a BigInt64Array is little-endian.
209
- const words = new Int32Array(fingerprints.buffer);
210
- const outerHigh = high;
211
- const outerLow = low;
212
- states.forEach((state, position) => {
213
- if (ids.size >= limit) {
214
- shiftTables.clear();
215
- ids.clear();
216
- contributions.length = 0;
217
- contributionShiftTables.length = 0;
218
- }
219
- high = INITIAL_HIGH;
220
- low = INITIAL_LOW;
221
- for (const index of order) extendElement(state[index]);
222
- words[2 * position] = low;
223
- words[2 * position + 1] = high;
224
- });
225
- high = outerHigh;
226
- low = outerLow;
227
- return fingerprints;
228
- };
164
+ // TLCStateMut#fingerPrint of each state's variables in TLC's variable order, as the fingerprint
165
+ // store reads them.
166
+ export const getTlcFingerprints = (states: Value[][], order: number[]) => {
167
+ const fingerprints = new BigInt64Array(states.length);
168
+ // Each fingerprint's low and high words, as a BigInt64Array is little-endian.
169
+ const words = new Int32Array(fingerprints.buffer);
170
+ const outerHigh = high;
171
+ const outerLow = low;
172
+ states.forEach((state, position) => {
173
+ high = INITIAL_HIGH;
174
+ low = INITIAL_LOW;
175
+ for (const index of order) extendValue(state[index]);
176
+ words[2 * position] = low;
177
+ words[2 * position + 1] = high;
178
+ });
179
+ high = outerHigh;
180
+ low = outerLow;
181
+ return fingerprints;
229
182
  };
package/src/tlc/order.ts CHANGED
@@ -38,7 +38,6 @@ interface ParsedModule {
38
38
  tokens: Token[];
39
39
  start: number;
40
40
  end: number;
41
- isTlcSource: boolean;
42
41
  getModule: () => Module;
43
42
  }
44
43
 
@@ -126,7 +125,8 @@ const getDeclaredName = (tokens: Token[], start: number) => {
126
125
  return { name: first.text === "-" ? "-." : getCanonicalName(first.text), end: start + 2 };
127
126
  }
128
127
  if (second?.text !== "(") return { name: first.text, end: start + 1 };
129
- const close = tokens.findIndex((token, index) => index > start && token.text === ")");
128
+ let close = start + 2;
129
+ while (close < tokens.length && tokens[close].text !== ")") close++;
130
130
  return { name: first.text, end: close + 1 };
131
131
  };
132
132
 
@@ -268,6 +268,8 @@ interface ModuleText {
268
268
  getModule: () => Module;
269
269
  }
270
270
 
271
+ // A module's source is read into tokens once, and parsed from them once, for every order and for
272
+ // the environment that evaluates it.
271
273
  const readModuleText = (text: string | undefined): ModuleText => {
272
274
  const tokens = text === undefined ? [] : tokenize(text);
273
275
  const parsed = { module: undefined as Module | undefined };
@@ -279,23 +281,21 @@ const readModuleText = (text: string | undefined): ModuleText => {
279
281
  };
280
282
  };
281
283
 
282
- // TLC's own module sources never change, so every check shares their tokens and parse.
283
- const tlcModuleTexts = new Map<string, ModuleText>();
284
-
285
- const getTlcModuleText = (name: string, text: string): ModuleText => {
286
- const known = tlcModuleTexts.get(name);
287
- if (known) return known;
288
- const read = readModuleText(text);
289
- tlcModuleTexts.set(name, read);
290
- return read;
291
- };
292
-
293
284
  // The modules TLC reads for a spec: the root module, TLC's standard modules, the files the loader
294
285
  // returns, the modules that follow the root module, and the modules nested in any of these. A
295
286
  // module without a source has no tokens.
296
287
  const createModuleSet = (source: string, loadModule: ModuleLoader) => {
297
288
  const files = new Map<string, ParsedModule>();
298
289
  const nestedModules = new Map<string, ParsedModule>();
290
+ // The read of each module text, so that the environment parses a text the order read only once.
291
+ const texts = new Map<string, ModuleText>();
292
+ const readText = (text: string | undefined) => {
293
+ const known = text === undefined ? undefined : texts.get(text);
294
+ if (known) return known;
295
+ const read = readModuleText(text);
296
+ if (text !== undefined) texts.set(text, read);
297
+ return read;
298
+ };
299
299
  const getFile = (name: string | undefined) => {
300
300
  const cached = files.get(name ?? "");
301
301
  if (cached) return cached;
@@ -306,52 +306,32 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
306
306
  TLC_MODULE_SOURCES.get(name) ??
307
307
  loadModule?.(name) ??
308
308
  extractModule(source, name));
309
- const isTlcSource =
310
- name !== undefined && text !== undefined && text === TLC_MODULE_SOURCES.get(name);
311
- const { tokens, ranges, getModule } = isTlcSource
312
- ? getTlcModuleText(name, text)
313
- : readModuleText(text);
309
+ const { tokens, ranges, getModule } = readText(text);
314
310
  const range = ranges.get(getModuleName(tokens) ?? "") ?? { start: 0, end: 0 };
315
- const file = { tokens, ...range, isTlcSource, getModule };
311
+ const file = { tokens, ...range, getModule };
316
312
  files.set(name ?? "", file);
317
313
  ranges.forEach((nestedRange, nestedName) => {
318
314
  if (nestedRange.start === range.start || nestedModules.has(nestedName)) return;
319
315
  nestedModules.set(nestedName, {
320
316
  tokens,
321
317
  ...nestedRange,
322
- isTlcSource: false,
323
318
  getModule: () => findModule(file.getModule().modules, nestedName) ?? file.getModule(),
324
319
  });
325
320
  });
326
321
  return file;
327
322
  };
328
323
  getFile(undefined);
329
- return (name: string | undefined): ParsedModule =>
330
- (name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name);
324
+ return {
325
+ getParsed: (name: string | undefined): ParsedModule =>
326
+ (name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name),
327
+ parseText: (text: string) => readText(text).getModule(),
328
+ };
331
329
  };
332
330
 
333
331
  // SANY's Context of each module, which the root module has under undefined.
334
- type ModuleSet = ReturnType<typeof createModuleSet>;
335
-
336
- // The Context of one of TLC's own modules is the same in every check when every module it extends
337
- // or instantiates is also TLC's own.
338
- const tlcContexts = new Map<string, SymbolTable>();
332
+ type ModuleSet = ReturnType<typeof createModuleSet>["getParsed"];
339
333
 
340
334
  const createContexts = (getParsed: ModuleSet) => {
341
- const sharedModules = new Map<string, boolean>();
342
- const isShared = (name: string): boolean => {
343
- const known = sharedModules.get(name);
344
- if (known !== undefined) return known;
345
- sharedModules.set(name, false);
346
- const unit = getParsed(name);
347
- const module = unit.isTlcSource ? unit.getModule() : undefined;
348
- const shared =
349
- module !== undefined &&
350
- module.extends.every((extended) => isShared(extended)) &&
351
- module.instances.every((instance) => isShared(instance.module));
352
- sharedModules.set(name, shared);
353
- return shared;
354
- };
355
335
  const getUnits = (unit: ParsedModule) => {
356
336
  const tokens = unit.tokens;
357
337
  const module = unit.getModule();
@@ -452,8 +432,7 @@ const createContexts = (getParsed: ModuleSet) => {
452
432
 
453
433
  const contexts = new Map<string, SymbolTable>();
454
434
  const getContext = (name: string | undefined): SymbolTable => {
455
- const isTlcContext = name !== undefined && isShared(name);
456
- const cached = contexts.get(name ?? "") ?? (isTlcContext ? tlcContexts.get(name) : undefined);
435
+ const cached = contexts.get(name ?? "");
457
436
  if (cached) return cached;
458
437
  const table = createSymbolTable();
459
438
  contexts.set(name ?? "", table);
@@ -468,7 +447,6 @@ const createContexts = (getParsed: ModuleSet) => {
468
447
  );
469
448
  table.ownStart = table.inserted.length;
470
449
  getUnits(unit).forEach((item) => item.apply(table));
471
- if (isTlcContext) tlcContexts.set(name, table);
472
450
  return table;
473
451
  };
474
452
  return {
@@ -586,8 +564,6 @@ const getStringOrder = (getParsed: ModuleSet, config: ModelConfig): string[] =>
586
564
  };
587
565
 
588
566
  export interface TlcOrder {
589
- // The root module, parsed once for the order and the environment.
590
- getRootModule: () => Module;
591
567
  // Every string TLC interns before model checking starts.
592
568
  getStringOrder: (config: ModelConfig) => string[];
593
569
  // The variables or constants of the root module in the reverse of its context enumeration, as
@@ -599,16 +575,18 @@ export interface TlcOrder {
599
575
  // adds the definitions of the module it instantiates in the order that module's Hashtable
600
576
  // enumerates them.
601
577
  createDefinitionOrder: () => (name: string | undefined) => string[];
578
+ // Parses a module's source, once for the order and the environment.
579
+ parseModule: (source: string) => Module;
602
580
  }
603
581
 
604
582
  // One check reads the spec's modules once, and every order shares their tokens, parses and
605
583
  // contexts.
606
584
  export const createTlcOrder = (source: string, loadModule: ModuleLoader): TlcOrder => {
607
- const getParsed = createModuleSet(source, loadModule);
585
+ const modules = createModuleSet(source, loadModule);
586
+ const getParsed = modules.getParsed;
608
587
  let contexts: ReturnType<typeof createContexts> | undefined;
609
588
  const getContexts = () => (contexts ??= createContexts(getParsed));
610
589
  return {
611
- getRootModule: () => getParsed(undefined).getModule(),
612
590
  getStringOrder: (config) => getStringOrder(getParsed, config),
613
591
  getDeclarationOrder: (kind) =>
614
592
  getContexts()
@@ -617,6 +595,7 @@ export const createTlcOrder = (source: string, loadModule: ModuleLoader): TlcOrd
617
595
  .filter((entry) => entry.kind === kind)
618
596
  .map((entry) => entry.name)
619
597
  .reverse(),
598
+ parseModule: modules.parseText,
620
599
  createDefinitionOrder: () => {
621
600
  const { getContext, getOwnEntries } = getContexts();
622
601
  // The root module's context reads every file of the spec, and so finds the modules nested
package/src/tlc/replay.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Expression, SourceLocation } from "../ast";
2
2
  import { TlaEvaluationError } from "../errors";
3
+ import { isConstantLevel } from "../dependencies";
3
4
  import {
4
5
  bindDefinitions,
5
6
  bindLazyArguments,
@@ -9,7 +10,6 @@ import {
9
10
  freezeCallStack,
10
11
  getLazyFunction,
11
12
  toKeepLazyContext,
12
- isConstantLevel,
13
13
  lookupScope,
14
14
  resolveApplyOperator,
15
15
  resolveGlobalOperator,