tla-ts 0.0.2 → 0.0.3-dev.19c763f

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 (59) hide show
  1. package/README.md +78 -12
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +60 -1
  4. package/dist/index.d.ts +60 -1
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +21 -2
  11. package/dist/run-cli.js +21 -2
  12. package/dist/transfer.cjs +27 -15
  13. package/dist/transfer.js +27 -15
  14. package/package.json +3 -1
  15. package/src/ast.ts +134 -4
  16. package/src/check.ts +230 -62
  17. package/src/cli-options.ts +64 -0
  18. package/src/codegen.ts +9 -19
  19. package/src/dependencies.ts +2 -2
  20. package/src/environment.ts +75 -16
  21. package/src/errors.ts +46 -3
  22. package/src/evaluate.ts +78 -136
  23. package/src/exit-codes.ts +21 -43
  24. package/src/fingerprints.ts +46 -11
  25. package/src/instances.ts +15 -14
  26. package/src/levels.ts +8 -8
  27. package/src/lexer.ts +11 -2
  28. package/src/live-check.ts +710 -0
  29. package/src/liveness.ts +5 -523
  30. package/src/obligation-action.ts +384 -0
  31. package/src/obligation-anon.ts +575 -0
  32. package/src/obligation-deref.ts +637 -0
  33. package/src/obligation-expression.ts +1060 -0
  34. package/src/obligation-format.ts +406 -0
  35. package/src/obligation-generate.ts +1074 -0
  36. package/src/obligation-levels.ts +329 -0
  37. package/src/obligation-module.ts +1042 -0
  38. package/src/obligation-printer.ts +889 -0
  39. package/src/obligation-proof.ts +76 -0
  40. package/src/obligation-source.ts +1084 -0
  41. package/src/obligation-tuples.ts +184 -0
  42. package/src/obligations.ts +316 -0
  43. package/src/parallel-worker.ts +166 -210
  44. package/src/parallel.ts +76 -35
  45. package/src/parser.ts +297 -53
  46. package/src/resolve.ts +16 -15
  47. package/src/run-cli.ts +353 -40
  48. package/src/runtime.ts +3 -1
  49. package/src/spec-description.ts +18 -6
  50. package/src/standard-levels.ts +3 -3
  51. package/src/state-graph.ts +157 -0
  52. package/src/subexpressions.ts +10 -10
  53. package/src/successors.ts +17 -9
  54. package/src/symmetry.ts +14 -2
  55. package/src/tlc-ext.ts +29 -12
  56. package/src/tlc-module.ts +9 -28
  57. package/src/tlc-order.ts +17 -3
  58. package/src/transfer.ts +22 -1
  59. package/src/values.ts +70 -23
package/src/values.ts CHANGED
@@ -4,6 +4,8 @@ import { prettyPrint } from "./pretty-print";
4
4
  export interface ModelValue {
5
5
  kind: "ModelValue";
6
6
  name: string;
7
+ type: string | undefined;
8
+ rank: number;
7
9
  }
8
10
 
9
11
  export interface SetValue {
@@ -233,8 +235,7 @@ const compareSets = (left: SetValue, right: SetValue) => {
233
235
  return 0;
234
236
  };
235
237
 
236
- const getModelValueType = (value: Value) =>
237
- isModelValue(value) && value.name.length > 2 && value.name[1] === "_" ? value.name[0] : undefined;
238
+ const getModelValueType = (value: Value) => (isModelValue(value) ? value.type : undefined);
238
239
 
239
240
  const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) => {
240
241
  const leftType = getModelValueType(left);
@@ -264,6 +265,7 @@ const stringOrder = { ranks: Object.create(null) as Record<string, number>, size
264
265
  export const setStringOrder = (texts: Iterable<string>) => {
265
266
  stringOrder.ranks = Object.create(null);
266
267
  stringOrder.size = 0;
268
+ modelValues.clear();
267
269
  for (const text of texts) rankString(text);
268
270
  };
269
271
 
@@ -349,11 +351,14 @@ export const compareValues = (left: Value, right: Value): number => {
349
351
  const otherSize = Math.max(0, right.high - right.low + 1);
350
352
  return size !== otherSize || size === 0 ? size - otherSize : left.low - right.low;
351
353
  }
354
+ if (leftKind === "ModelValue" && rightKind === "ModelValue") {
355
+ if (left.type !== undefined && right.type !== undefined) {
356
+ checkModelValueTypes(left, right, false);
357
+ }
358
+ return left.rank - right.rank;
359
+ }
352
360
  if (leftKind === "ModelValue" || rightKind === "ModelValue") {
353
361
  checkModelValueTypes(left, right, false);
354
- if (leftKind === "ModelValue" && rightKind === "ModelValue") {
355
- return compareStrings(left.name, right.name);
356
- }
357
362
  return leftKind === "ModelValue" ? -1 : 1;
358
363
  }
359
364
  if (left === ANY_SET || right === ANY_SET) return compareAny(left === ANY_SET ? right : left);
@@ -382,11 +387,16 @@ export const compareValues = (left: Value, right: Value): number => {
382
387
 
383
388
  const modelValues = new Map<string, ModelValue>();
384
389
 
390
+ // A check ranks every model value's name when it creates the value, and creates its own values.
385
391
  export const createModelValue = (name: string): ModelValue => {
386
- rankString(name);
387
392
  const known = modelValues.get(name);
388
393
  if (known) return known;
389
- const value: ModelValue = { kind: "ModelValue", name };
394
+ const value: ModelValue = {
395
+ kind: "ModelValue",
396
+ name,
397
+ type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
398
+ rank: getStringRank(name),
399
+ };
390
400
  modelValues.set(name, value);
391
401
  return value;
392
402
  };
@@ -555,7 +565,7 @@ const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
555
565
 
556
566
  const compareModelValue = (left: ModelValue, right: Value) => {
557
567
  const type = getModelValueType(left);
558
- if (type === undefined) return isModelValue(right) ? compareStrings(left.name, right.name) : -1;
568
+ if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
559
569
  if (!isModelValue(right)) {
560
570
  throw new TlaEvaluationError(
561
571
  `Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
@@ -567,7 +577,7 @@ const compareModelValue = (left: ModelValue, right: Value) => {
567
577
  `Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
568
578
  );
569
579
  }
570
- return compareStrings(left.name, right.name);
580
+ return left.rank - right.rank;
571
581
  };
572
582
 
573
583
  const equalsModelValue = (left: ModelValue, right: Value) => {
@@ -1667,6 +1677,25 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1667
1677
  }
1668
1678
  };
1669
1679
 
1680
+ // A function that agrees with a member of a function set outside some entries is a member when
1681
+ // those entries are.
1682
+ export const isUpdatedElementOf = (element: Value, member: Value, set: Value): boolean => {
1683
+ if (element === member) return true;
1684
+ if (
1685
+ typeof element !== "object" ||
1686
+ typeof member !== "object" ||
1687
+ typeof set !== "object" ||
1688
+ element.kind !== "Function" ||
1689
+ member.kind !== "Function" ||
1690
+ set.kind !== "FunctionSet" ||
1691
+ element.domain !== member.domain
1692
+ )
1693
+ return isElementOf(element, set);
1694
+ return element.range.every((item, index) =>
1695
+ isUpdatedElementOf(item, member.range[index], set.range),
1696
+ );
1697
+ };
1698
+
1670
1699
  export const toFiniteFunction = (value: Value): FunctionValue => {
1671
1700
  if (typeof value === "object" && value.kind === "Function") return value;
1672
1701
  if (typeof value === "object" && value.kind === "LazyFunction") {
@@ -1694,6 +1723,19 @@ const describeMissingArgument = (functionValue: Value, argument: Value) =>
1694
1723
  ? `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`
1695
1724
  : `Attempted to apply function ${formatValue(functionValue)} to ${formatValue(argument)}, which is not in its domain`;
1696
1725
 
1726
+ const findKeyIndex = (domain: Value[], argument: Value) => {
1727
+ const first = domain[0];
1728
+ if (typeof argument === "number" && typeof first === "number") {
1729
+ const offset = argument - first;
1730
+ if (domain[offset] === argument) return offset;
1731
+ }
1732
+ if (domain.length <= 8) {
1733
+ for (let index = 0; index < domain.length; index++)
1734
+ if (domain[index] === argument) return index;
1735
+ }
1736
+ return findIndex(domain, argument);
1737
+ };
1738
+
1697
1739
  export const applyFunction = (
1698
1740
  functionValue: Value,
1699
1741
  argument: Value,
@@ -1714,17 +1756,7 @@ export const applyFunction = (
1714
1756
  `Attempted to apply ${formatValue(functionValue)}, which is not a function`,
1715
1757
  );
1716
1758
  }
1717
- const domain = functionValue.domain;
1718
- const first = domain[0];
1719
- if (typeof argument === "number" && typeof first === "number") {
1720
- const offset = argument - first;
1721
- if (domain[offset] === argument) return functionValue.range[offset];
1722
- }
1723
- if (domain.length <= 8) {
1724
- for (let index = 0; index < domain.length; index++)
1725
- if (domain[index] === argument) return functionValue.range[index];
1726
- }
1727
- const position = findIndex(domain, argument);
1759
+ const position = findKeyIndex(functionValue.domain, argument);
1728
1760
  if (position === -1) {
1729
1761
  throw new TlaEvaluationError(
1730
1762
  describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
@@ -1733,12 +1765,16 @@ export const applyFunction = (
1733
1765
  return functionValue.range[position];
1734
1766
  };
1735
1767
 
1768
+ export const getDomainInterval = (domain: Value[]) =>
1769
+ isIntervalDomain(domain)
1770
+ ? (intervalArrays.get(domain) ?? getIntervalValue(domain.length))
1771
+ : undefined;
1772
+
1736
1773
  export const getDomain = (functionValue: Value): Value => {
1737
1774
  if (typeof functionValue === "object" && functionValue.kind === "LazyFunction")
1738
1775
  return functionValue.domain;
1739
1776
  const domain = toFiniteFunction(functionValue).domain;
1740
- if (!isIntervalDomain(domain)) return createSortedSet(domain);
1741
- return intervalArrays.get(domain) ?? getIntervalValue(domain.length);
1777
+ return getDomainInterval(domain) ?? createSortedSet(domain);
1742
1778
  };
1743
1779
 
1744
1780
  export const updateFunction = (
@@ -1762,7 +1798,7 @@ export const updateFunction = (
1762
1798
  };
1763
1799
  }
1764
1800
  const finite = toFiniteFunction(functionValue);
1765
- const position = findIndex(finite.domain, argument);
1801
+ const position = findKeyIndex(finite.domain, argument);
1766
1802
  if (position === -1) return finite;
1767
1803
  const range = [...finite.range];
1768
1804
  const previous = finite.range[position];
@@ -2080,3 +2116,14 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
2080
2116
  }
2081
2117
  }
2082
2118
  };
2119
+
2120
+ // TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
2121
+ export const formatState = (state: Record<string, Value>) => {
2122
+ const entries = Object.entries(state);
2123
+ return entries
2124
+ .map(
2125
+ ([name, value]) =>
2126
+ `${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
2127
+ )
2128
+ .join("");
2129
+ };