tla-ts 0.0.3-dev.6a30eee → 0.0.3-dev.6d0aeb6

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 (54) hide show
  1. package/README.md +5 -1
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +58 -22
  4. package/dist/index.d.ts +58 -22
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +1 -1
  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 +20 -18
  11. package/dist/run-cli.js +22 -20
  12. package/dist/transfer.cjs +40 -15
  13. package/dist/transfer.js +40 -15
  14. package/package.json +1 -1
  15. package/src/ast.ts +101 -1
  16. package/src/builtins.ts +9 -3
  17. package/src/check.ts +473 -100
  18. package/src/cli-options.ts +11 -5
  19. package/src/codegen.ts +15 -22
  20. package/src/config.ts +224 -170
  21. package/src/dependencies.ts +2 -2
  22. package/src/environment.ts +158 -49
  23. package/src/errors.ts +93 -3
  24. package/src/evaluate.ts +214 -147
  25. package/src/exit-codes.ts +21 -14
  26. package/src/fingerprints.ts +2 -1
  27. package/src/instances.ts +16 -15
  28. package/src/levels.ts +181 -45
  29. package/src/live-check.ts +132 -70
  30. package/src/liveness.ts +118 -24
  31. package/src/parallel-worker.ts +2 -2
  32. package/src/parallel.ts +5 -2
  33. package/src/parser.ts +71 -19
  34. package/src/resolve.ts +490 -95
  35. package/src/run-cli.ts +104 -18
  36. package/src/runtime.ts +2 -0
  37. package/src/sany-lexer.ts +615 -0
  38. package/src/sany-parse-error.ts +3143 -0
  39. package/src/spec-description.ts +19 -8
  40. package/src/standard-modules.ts +54 -45
  41. package/src/state-graph.ts +157 -0
  42. package/src/subexpressions.ts +35 -23
  43. package/src/substitution-levels.ts +38 -17
  44. package/src/successors.ts +176 -36
  45. package/src/symmetry.ts +16 -6
  46. package/src/tableau.ts +51 -24
  47. package/src/tlc-ext.ts +29 -12
  48. package/src/tlc-module.ts +14 -33
  49. package/src/tlc-modules.ts +4 -0
  50. package/src/tlc-order.ts +8 -4
  51. package/src/tlc-random.ts +9 -6
  52. package/src/tlc-summary.ts +20 -5
  53. package/src/value-sources.ts +29 -0
  54. package/src/values.ts +242 -61
package/src/levels.ts CHANGED
@@ -1,15 +1,23 @@
1
- import type {
2
- AssumeProve,
3
- Definition,
4
- Expression,
5
- Module,
6
- OperatorParam,
7
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type AssumeProve,
4
+ type Definition,
5
+ type Expression,
6
+ type Module,
7
+ type OperatorParam,
8
+ type Proof,
9
+ type ProofStep,
10
+ type SourceLocation,
8
11
  } from "./ast";
9
- import { TlaSyntaxError } from "./errors";
10
- import { getChildren } from "./evaluate";
12
+ import {
13
+ createSanyError,
14
+ createSemanticError,
15
+ formatRange,
16
+ TlaSyntaxError,
17
+ type SanyError,
18
+ } from "./errors";
11
19
  import { mapChildren } from "./instances";
12
- import { getBoundNames, getPatternNames } from "./resolve";
20
+ import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
13
21
  import { STANDARD_LEVELS } from "./standard-levels";
14
22
 
15
23
  interface LevelData {
@@ -34,6 +42,7 @@ interface LevelContext {
34
42
  constants: Set<string>;
35
43
  variables: Set<string>;
36
44
  getDefinition: (name: string) => Definition | undefined;
45
+ getQualifiedName?: (name: string) => string | undefined;
37
46
  }
38
47
 
39
48
  type ScopeEntry =
@@ -176,8 +185,13 @@ export const ACTION_LEVEL = 2;
176
185
 
177
186
  export const TEMPORAL_LEVEL = 3;
178
187
 
179
- const createLevelError = (message: string, location: SourceLocation) =>
180
- new TlaSyntaxError(`${message} at line ${location.line}, column ${location.column}`);
188
+ // SANY reports a level error at the range of the expression it rejects.
189
+ const createLevelError = (message: string, expression: Expression) =>
190
+ expression.location.range
191
+ ? createSemanticError(`${message}.`, expression.location.range)
192
+ : new TlaSyntaxError(
193
+ `${message} at line ${expression.location.line}, column ${expression.location.column}`,
194
+ );
181
195
 
182
196
  const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
183
197
 
@@ -201,7 +215,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
201
215
  if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
202
216
  const definition = context.getDefinition(name);
203
217
  if (definition) {
204
- return { kind: "Defined", levels: analyzeDefinition(definition, new Map()) };
218
+ const levels = analyzeDefinition(definition, new Map());
219
+ // SANY names a definition of an instance by its qualified name, as in I!Foo.
220
+ const qualifiedName = context.getQualifiedName?.(name);
221
+ return {
222
+ kind: "Defined",
223
+ levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
224
+ };
205
225
  }
206
226
  const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
207
227
  if (signature) return { kind: "Defined", levels: signature };
@@ -237,13 +257,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
237
257
  if (operand.operator || operand.declared) return;
238
258
  if (operand.data.level > getMaxLevel(operator, index)) {
239
259
  throw new TlaSyntaxError(
240
- `Level error in applying operator ${operator.name}: The level of argument ${index + 1} exceeds the maximum level allowed by the operator.`,
260
+ `Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
241
261
  );
242
262
  }
243
263
  });
244
264
  if (ranges.some((range) => range.level > ACTION_LEVEL)) {
245
265
  throw new TlaSyntaxError(
246
- `Level error in applying operator ${operator.name}: The level of the range for a bounded variable exceeds the maximum level allowed by the operator.`,
266
+ `Level error in applying operator ${operator.name}:\nThe level of the range for a bounded variable exceeds the maximum level allowed by the operator`,
247
267
  );
248
268
  }
249
269
  const getOperand = (formal: string) => {
@@ -444,10 +464,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
444
464
  ? applyDeclared(resolved.name, resolved.level, operands)
445
465
  : applyDefined(resolved.levels, operands);
446
466
 
447
- const checkJunction = (operands: Operand[], location: SourceLocation) => {
467
+ const checkJunction = (operands: Operand[], expression: Expression) => {
448
468
  const levels = operands.map((operand) => operand.data.level);
449
469
  if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
450
- throw createLevelError("Formula has both temporal formula and action as arguments", location);
470
+ throw createLevelError(
471
+ "Formula has both temporal formula and action as arguments",
472
+ expression,
473
+ );
451
474
  }
452
475
  };
453
476
 
@@ -456,10 +479,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
456
479
  if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
457
480
  throw createLevelError(
458
481
  "Action used where only temporal formula or state predicate allowed",
459
- expression.location,
482
+ expression,
460
483
  );
461
484
  }
462
- if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression.location);
485
+ if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
463
486
  };
464
487
 
465
488
  const checkTemporal = (
@@ -473,7 +496,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
473
496
  expression.kind === "Always"
474
497
  ? "[] followed by action not of form [A]_v"
475
498
  : "<> followed by action not of form <<A>>_v",
476
- expression.location,
499
+ expression,
477
500
  );
478
501
  };
479
502
 
@@ -501,7 +524,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
501
524
  return data;
502
525
  };
503
526
 
527
+ // SANY reports a level error of an operator's arguments at the application.
504
528
  const analyze = (expression: Expression, scope: Scope): LevelData => {
529
+ try {
530
+ return analyzeNode(expression, scope);
531
+ } catch (error) {
532
+ if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
533
+ throw createLevelError(error.message, expression);
534
+ }
535
+ };
536
+
537
+ const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
505
538
  switch (expression.kind) {
506
539
  case "Number":
507
540
  case "Decimal":
@@ -521,7 +554,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
521
554
  case "And":
522
555
  case "Or": {
523
556
  const operands = toOperands(expression.operands, scope);
524
- checkJunction(operands, expression.location);
557
+ checkJunction(operands, expression);
525
558
  return applyDefined(JUNCTION_LEVELS, operands);
526
559
  }
527
560
  case "Prime":
@@ -561,13 +594,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
561
594
  expression.bounds.map((bound) => bound.set),
562
595
  getBoundNames(expression.bounds),
563
596
  );
564
- if (
565
- data.level === TEMPORAL_LEVEL &&
566
- expression.bounds.some((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
567
- ) {
597
+ const actionBound =
598
+ data.level === TEMPORAL_LEVEL
599
+ ? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
600
+ : undefined;
601
+ if (actionBound) {
568
602
  throw createLevelError(
569
603
  "Action-level bound of quantified temporal formula",
570
- expression.location,
604
+ actionBound.set,
571
605
  );
572
606
  }
573
607
  return data;
@@ -817,34 +851,136 @@ export const createLevelAnalyzer = (context: LevelContext) => {
817
851
  return { analyze, analyzeDefinition, toOperand };
818
852
  };
819
853
 
820
- const analyzeAssumeProve = (
854
+ export const analyzeAssumeProve = (
821
855
  assumeProve: AssumeProve,
822
856
  scope: Scope,
823
857
  analyzer: ReturnType<typeof createLevelAnalyzer>,
824
- ) => {
858
+ ): LevelData => {
825
859
  const inner = new Map(scope);
826
- assumeProve.assumptions.forEach((assumption) => {
860
+ const parts = assumeProve.assumptions.flatMap((assumption) => {
827
861
  if (assumption.kind === "NewSymbol") {
828
- if (assumption.set) analyzer.analyze(assumption.set, inner);
862
+ const set = assumption.set && analyzer.analyze(assumption.set, inner);
829
863
  inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
830
- return;
864
+ return set ? [set] : [];
831
865
  }
832
- if (assumption.kind === "AssumeProve") analyzeAssumeProve(assumption, inner, analyzer);
833
- else analyzer.analyze(assumption, inner);
866
+ return [
867
+ assumption.kind === "AssumeProve"
868
+ ? analyzeAssumeProve(assumption, inner, analyzer)
869
+ : analyzer.analyze(assumption, inner),
870
+ ];
834
871
  });
835
- analyzer.analyze(assumeProve.conclusion, inner);
872
+ parts.push(analyzer.analyze(assumeProve.conclusion, inner));
873
+ const data = createData(Math.max(...parts.map((part) => part.level)));
874
+ parts.forEach((part) => {
875
+ part.params.forEach((param) => data.params.add(param));
876
+ part.constraints.forEach((value, key) => putConstraint(data.constraints, key, value));
877
+ part.argConstraints.forEach((value, key) => putArgConstraint(data.argConstraints, key, value));
878
+ part.argParams.forEach((key) => data.argParams.add(key));
879
+ });
880
+ return data;
836
881
  };
837
882
 
838
- // SANY checks the level of every definition, assumption, and theorem of a module.
839
- export const checkModuleLevels = (module: Module, context: LevelContext) => {
883
+ // SANY checks the level of every definition, assumption, theorem, and instance of a module.
884
+ export const checkModuleLevels = (
885
+ module: Module,
886
+ context: LevelContext,
887
+ instances: { location: SourceLocation; check: () => void }[],
888
+ ) => {
840
889
  const analyzer = createLevelAnalyzer(context);
841
- module.definitions
842
- .filter((definition) => !definition.name.includes("!"))
843
- .forEach((definition) => analyzer.analyzeDefinition(definition, new Map()));
844
- module.assumptions.forEach((assumption) => analyzer.analyze(assumption.expression, new Map()));
845
- module.theorems.forEach((theorem) =>
846
- theorem.statement.kind === "AssumeProve"
847
- ? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
848
- : analyzer.analyze(theorem.statement, new Map()),
849
- );
890
+ const getLevel = (expressions: Expression[]) =>
891
+ Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
892
+ const getStepLevel = (step: ProofStep) => {
893
+ const body = step.body;
894
+ if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
895
+ if (body.kind === "Witness") return getLevel(body.expressions);
896
+ if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
897
+ if (body.kind === "Pick") {
898
+ return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
899
+ }
900
+ if (body.kind !== "Assert") return 0;
901
+ return body.statement.kind === "AssumeProve"
902
+ ? analyzeAssumeProve(body.statement, new Map(), analyzer).level
903
+ : getLevel([body.statement]);
904
+ };
905
+ // SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
906
+ const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
907
+ proof?.kind !== "Steps"
908
+ ? []
909
+ : proof.steps.flatMap((step) => {
910
+ const location = formatRange(step.statement);
911
+ const isConstant = () => getStepLevel(step) === 0;
912
+ if (
913
+ step.body.kind === "Have" ||
914
+ step.body.kind === "Take" ||
915
+ step.body.kind === "Witness"
916
+ ) {
917
+ return isConstant()
918
+ ? []
919
+ : [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
920
+ }
921
+ if (step.body.kind === "Case") {
922
+ return [
923
+ ...(isConstant()
924
+ ? []
925
+ : [{ location, message: "Non-constant CASE for temporal goal." }]),
926
+ ...checkTemporalGoal(step.proof),
927
+ ];
928
+ }
929
+ return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
930
+ });
931
+ // SANY checks each step as a theorem of its own after the steps of its proof.
932
+ const checkProof = (proof: Proof | undefined): SanyError[] =>
933
+ proof?.kind !== "Steps"
934
+ ? []
935
+ : proof.steps.flatMap((step) => {
936
+ const body = step.body;
937
+ const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
938
+ const pickErrors =
939
+ body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
940
+ ? body.bounds
941
+ .filter((bound) => getLevel([bound.set]) !== 0)
942
+ .flatMap((bound) =>
943
+ bound.set.location.range
944
+ ? [
945
+ {
946
+ location: formatRange(bound.set.location.range),
947
+ message: "Non-constant bound of temporal PICK.",
948
+ },
949
+ ]
950
+ : [],
951
+ )
952
+ : [];
953
+ return [
954
+ ...checkProof(step.proof),
955
+ ...pickErrors,
956
+ ...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
957
+ ];
958
+ });
959
+ collectSemanticErrors([
960
+ ...module.definitions
961
+ .filter((definition) => !definition.name.includes("!"))
962
+ .map((definition) => ({
963
+ location: definition.location,
964
+ check: () => analyzer.analyzeDefinition(definition, new Map()),
965
+ })),
966
+ ...module.assumptions.map((assumption) => ({
967
+ location: assumption.location,
968
+ check: () => analyzer.analyze(assumption.expression, new Map()),
969
+ })),
970
+ ...module.theorems.map((theorem) => ({
971
+ location: theorem.location,
972
+ check: () => {
973
+ const data =
974
+ theorem.statement.kind === "AssumeProve"
975
+ ? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
976
+ : analyzer.analyze(theorem.statement, new Map());
977
+ const errors = [
978
+ ...checkProof(theorem.proof),
979
+ ...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
980
+ ];
981
+ if (errors.length > 0) throw createSanyError(errors);
982
+ },
983
+ })),
984
+ ...instances,
985
+ ]);
850
986
  };