tla-ts 0.0.10-dev.a4b81b6d0 → 0.0.10-dev.a5705bfbf

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 (76) hide show
  1. package/dist/deciders.cjs +31 -31
  2. package/dist/deciders.js +2 -2
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +7 -1
  5. package/dist/index.d.ts +7 -1
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +1 -1
  9. package/dist/prove.cjs +42 -42
  10. package/dist/prove.js +30 -30
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +42 -42
  13. package/dist/transfer.cjs +31 -29
  14. package/dist/transfer.js +43 -41
  15. package/dist/worker.cjs +4 -4
  16. package/dist/worker.js +4 -4
  17. package/package.json +1 -1
  18. package/src/ast.ts +10 -1
  19. package/src/check-failure.ts +5 -6
  20. package/src/check-trace.ts +4 -4
  21. package/src/check.ts +78 -90
  22. package/src/cli/tlc-output.ts +16 -2
  23. package/src/cli/tlc.ts +2 -2
  24. package/src/codegen.ts +10 -16
  25. package/src/coverage.ts +190 -1
  26. package/src/dependencies.ts +14 -2
  27. package/src/dfid.ts +80 -27
  28. package/src/environment.ts +30 -1
  29. package/src/evaluate.ts +223 -47
  30. package/src/exit-codes.ts +3 -4
  31. package/src/instances.ts +11 -1
  32. package/src/levels.ts +48 -7
  33. package/src/liveness-checker.ts +123 -70
  34. package/src/liveness.ts +4 -1
  35. package/src/obligations/source.ts +2 -0
  36. package/src/parser.ts +3 -0
  37. package/src/prover/smt/solver/card2bv.ts +7 -4
  38. package/src/prover/smt/solver/context.ts +20 -4
  39. package/src/prover/smt/solver/dependent-expr-state.ts +18 -12
  40. package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
  41. package/src/prover/smt/solver/fd-solver.ts +146 -39
  42. package/src/prover/smt/solver/maxcore.ts +2 -1
  43. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  44. package/src/prover/smt/solver/model-checker.ts +1 -1
  45. package/src/prover/smt/solver/muz/dl-context.ts +12 -40
  46. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  47. package/src/prover/smt/solver/muz/dl-rule.ts +6 -7
  48. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  49. package/src/prover/smt/solver/muz/horn-tactic.ts +14 -1
  50. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  51. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  52. package/src/prover/smt/solver/opt-context.ts +1 -0
  53. package/src/prover/smt/solver/opt-solver.ts +1 -1
  54. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  55. package/src/prover/smt/solver/qsat.ts +7 -2
  56. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  57. package/src/prover/smt/solver/sat/inc-sat-solver.ts +45 -26
  58. package/src/prover/smt/solver/sat/sat-tactic.ts +4 -2
  59. package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
  60. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  61. package/src/prover/smt/solver/smt-kernel.ts +9 -2
  62. package/src/prover/smt/solver/smtlib-parser.ts +6 -2
  63. package/src/prover/smt/solver/smtlib-script.ts +197 -73
  64. package/src/prover/smt/solver/tactic.ts +64 -37
  65. package/src/resolve.ts +143 -39
  66. package/src/sany/generator-expressions.ts +2 -0
  67. package/src/sany/generator-proofs.ts +1 -0
  68. package/src/search.ts +109 -23
  69. package/src/simulate.ts +114 -124
  70. package/src/spec-description.ts +3 -0
  71. package/src/subexpressions.ts +168 -63
  72. package/src/successors.ts +30 -10
  73. package/src/tlc/replay.ts +45 -29
  74. package/src/trace-liveness.ts +529 -0
  75. package/src/typed-tier.ts +14 -4
  76. package/src/values.ts +15 -1
package/src/simulate.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { CheckOptions, TraceStep, Violation } from "./check";
2
+ import type { CoverageOutput } from "./coverage";
2
3
  import {
3
4
  isStackOverflow,
4
5
  TlaEvaluationError,
@@ -13,10 +14,17 @@ import {
13
14
  import type { State } from "./evaluate";
14
15
  import { EXIT_STATUS, getMessageExitStatus, withLivenessChecksExitCode } from "./exit-codes";
15
16
  import { getHashMapOrder } from "./java-hash";
16
- import { createLivenessChecks, type LivenessChecks, type SearchGraph } from "./liveness-checker";
17
- import { getIncompleteSuccessorMessage, prepareSearch, reportAssumptionViolation } from "./search";
17
+ import { createLivenessChecks } from "./liveness-checker";
18
+ import {
19
+ addActionCoverage,
20
+ getIncompleteSuccessorMessage,
21
+ prepareSearch,
22
+ reportAssumptionViolation,
23
+ STOP_WALK,
24
+ walkCoveredSuccessors,
25
+ } from "./search";
18
26
  import { getActionLabel, toActionValue, type SpecAction } from "./spec-description";
19
- import { forEachInitialState, forEachSuccessor } from "./successors";
27
+ import { forEachInitialState, forEachSuccessor, type Successor } from "./successors";
20
28
  import {
21
29
  formatInnerMessage,
22
30
  formatNestedMessage,
@@ -27,8 +35,10 @@ import {
27
35
  isHeadedError,
28
36
  MESSAGE_CODE,
29
37
  withUnexpectedException,
38
+ type FailureMessage,
30
39
  } from "./tlc/messages";
31
40
  import { createJavaRandom, nextPrime, type JavaRandom } from "./tlc/random";
41
+ import { replayInitialStates } from "./tlc/replay";
32
42
  import {
33
43
  createActionRecord,
34
44
  createCounterExample,
@@ -67,6 +77,7 @@ export interface SimulationStatistics {
67
77
  export type SimulationOutput =
68
78
  | { kind: "message"; code: number; isError: boolean; message: string; toolMessage: string }
69
79
  | { kind: "behavior"; trace: TraceStep[]; isLastStateOnly: boolean }
80
+ | CoverageOutput
70
81
  | { kind: "progress"; statistics: SimulationStatistics }
71
82
  | { kind: "summary"; statistics: SimulationStatistics };
72
83
 
@@ -87,6 +98,7 @@ interface WorkerError {
87
98
  isException: boolean;
88
99
  isSummarized: boolean;
89
100
  violation?: Violation;
101
+ failures?: FailureMessage[];
90
102
  counterExample?: Value;
91
103
  }
92
104
 
@@ -130,65 +142,6 @@ const getTrace = (last: TraceNode, order: number[]) => {
130
142
  return trace;
131
143
  };
132
144
 
133
- // Deviation from TLC: LiveCheck1 gives the initial state no stuttering step into a tableau node that isn't initial, so it misses a behavior that stutters there.
134
- const findTemporalViolation = (
135
- trace: TraceNode[],
136
- getFingerprint: (state: Value[]) => bigint,
137
- checks: LivenessChecks,
138
- ) => {
139
- const fingerprints = trace.map((node) => getFingerprint(node.state));
140
- const indices = new Map<bigint, number>();
141
- const states: Value[][] = [];
142
- const successors: number[][] = [];
143
- const stateFingerprints: number[] = [];
144
- fingerprints.forEach((fingerprint, position) => {
145
- if (!indices.has(fingerprint)) {
146
- indices.set(fingerprint, states.length);
147
- states.push(trace[position].state);
148
- stateFingerprints.push(Number(BigInt.asIntN(32, fingerprint)));
149
- successors.push([]);
150
- }
151
- if (position === 0) return;
152
- const source = indices.get(fingerprints[position - 1]) ?? 0;
153
- const target = indices.get(fingerprint) ?? 0;
154
- if (source !== target && !successors[source].includes(target)) {
155
- successors[source].push(target);
156
- }
157
- });
158
- const stateSuccessors = successors.map((targets) =>
159
- targets.map((target) => ({ index: target, state: states[target] })),
160
- );
161
- const search: SearchGraph<Value[]> = {
162
- getFingerprint: (successor) => stateFingerprints[successor.index],
163
- forEachActionSuccessors: (index, _state, onSuccessors) => onSuccessors(stateSuccessors[index]),
164
- };
165
- // TLC's LiveCheck1 checks the behavior against one solution after another, up to the first it violates.
166
- for (let position = 0; position < checks.solutions.length; position++) {
167
- const checker = checks.createChecker(search, [checks.solutions[position]]);
168
- checker.addInitialState(0, states[0]);
169
- states.forEach((state, index) => checker.addState(index, state, stateSuccessors[index]));
170
- const lasso = checker.findViolation();
171
- if (!lasso) continue;
172
- const getLength = (path: number[]) =>
173
- path.filter((index, position) => position === 0 || index !== path[position - 1]).length;
174
- const path = [...lasso.prefix, ...lasso.cycle];
175
- const length = getLength(path);
176
- const cyclePosition = getLength(path.slice(0, lasso.prefix.length + 1));
177
- const isStuttering =
178
- fingerprints[length - 1] === getFingerprint(states[path[lasso.prefix.length]]);
179
- return {
180
- length,
181
- isStuttering,
182
- loopStart: isStuttering ? length - 1 : cyclePosition - 1,
183
- properties: checker.getViolatedProperties(
184
- trace.slice(0, length).map((node) => node.state),
185
- cyclePosition - 1,
186
- ),
187
- };
188
- }
189
- return undefined;
190
- };
191
-
192
145
  const createScheduler = (
193
146
  scheduler: SimulationOptions["scheduler"],
194
147
  nextActions: SpecAction[],
@@ -272,11 +225,20 @@ export const simulate = (options: SimulationOptions): number => {
272
225
  };
273
226
  const reportMessage = (code: number, text: string, toolText = text, isError = true) =>
274
227
  output({ kind: "message", code, isError, message: text, toolMessage: toolText });
275
- const printSummary = () => output({ kind: "summary", statistics: { ...statistics } });
228
+ const reportCoverage = () => {
229
+ const report = prepared.getCoverage();
230
+ if (report) output({ kind: "coverage", coverage: report });
231
+ };
232
+ const printSummary = () => {
233
+ reportCoverage();
234
+ output({ kind: "summary", statistics: { ...statistics } });
235
+ };
276
236
  const nodes = new WeakMap<(Value | undefined)[], TraceNode>();
277
237
  const description = prepared.getDescription();
278
238
  const initActions = description.inits;
279
239
  const nextActions = description.actions;
240
+ const coverage = prepared.coverage;
241
+ const covered = coverage && addActionCoverage(coverage, environment, description);
280
242
  let traceCount = 0;
281
243
  let currentTrace = 0;
282
244
  let evaluatedAction: SpecAction | undefined;
@@ -441,7 +403,13 @@ export const simulate = (options: SimulationOptions): number => {
441
403
  return false;
442
404
  };
443
405
  try {
444
- if (init) forEachInitialState(init, environment, addGenerated, search.spec.scope, addGenerated);
406
+ if (covered) {
407
+ replayInitialStates(covered.inits, environment, addGenerated, (partial) =>
408
+ addGenerated([...partial]),
409
+ );
410
+ } else if (init) {
411
+ forEachInitialState(init, environment, addGenerated, search.spec.scope, addGenerated);
412
+ }
445
413
  } catch (error) {
446
414
  return reportInitialException(error, undefined);
447
415
  }
@@ -532,61 +500,77 @@ export const simulate = (options: SimulationOptions): number => {
532
500
  isException: false,
533
501
  isSummarized: true,
534
502
  });
535
- const getSuccessors = (current: TraceNode, action: SpecAction) => {
503
+ const getSuccessors = (current: TraceNode, index: number) => {
504
+ const action = nextActions[index];
505
+ const coveredAction = covered?.actions[index];
536
506
  const successors: TraceNode[] = [];
537
507
  let failure: WorkerError | undefined;
538
508
  runtime.deferred = undefined;
539
509
  evaluatedAction = action;
510
+ const visit = (successor: Successor) => {
511
+ const node = toNode(successor.state, action, current);
512
+ node.deferred = runtime.deferred;
513
+ runtime.deferred = undefined;
514
+ statistics.generatedStates++;
515
+ try {
516
+ const violated = search.findViolatedInvariant(node.state, current.state);
517
+ if (violated) {
518
+ failure = toWorkerError(
519
+ MESSAGE_CODE.invariantViolatedBehavior,
520
+ `Invariant ${violated.name} is violated.`,
521
+ node,
522
+ );
523
+ return true;
524
+ }
525
+ const property = search.findViolatedActionProperty(current.state, node.state);
526
+ if (property) {
527
+ failure = toWorkerError(
528
+ MESSAGE_CODE.actionPropertyViolatedBehavior,
529
+ `Action property ${property.name} is violated.`,
530
+ node,
531
+ );
532
+ return true;
533
+ }
534
+ } catch (error) {
535
+ if (!(error instanceof TlaEvaluationError) || !isHeadedError(error)) throw error;
536
+ const text = getFailureMessage(error, true);
537
+ failure = {
538
+ ...toWorkerError(text.code, text.message, node),
539
+ toolMessage: text.toolMessage,
540
+ };
541
+ return true;
542
+ }
543
+ if (!search.satisfiesConstraints(node.state, current.state)) return false;
544
+ search.trackStatePossible(node.state);
545
+ if (!search.satisfiesActionConstraints(current.state, node.state)) return false;
546
+ search.trackActionPossible(current.state, node.state);
547
+ if (successors.length === getSetBound()) throw new TlaTooManyStatesError();
548
+ if (coverage && coveredAction) coverage.secondary[coveredAction.node]++;
549
+ successors.push(node);
550
+ return false;
551
+ };
540
552
  try {
541
- forEachSuccessor(
542
- action.expression,
543
- current.state,
544
- environment,
545
- (successor) => {
546
- const node = toNode(successor.state, action, current);
547
- node.deferred = runtime.deferred;
548
- runtime.deferred = undefined;
549
- statistics.generatedStates++;
550
- try {
551
- const violated = search.findViolatedInvariant(node.state, current.state);
552
- if (violated) {
553
- failure = toWorkerError(
554
- MESSAGE_CODE.invariantViolatedBehavior,
555
- `Invariant ${violated.name} is violated.`,
556
- node,
557
- );
558
- return true;
559
- }
560
- const property = search.findViolatedActionProperty(current.state, node.state);
561
- if (property) {
562
- failure = toWorkerError(
563
- MESSAGE_CODE.actionPropertyViolatedBehavior,
564
- `Action property ${property.name} is violated.`,
565
- node,
566
- );
567
- return true;
568
- }
569
- } catch (error) {
570
- if (!(error instanceof TlaEvaluationError) || !isHeadedError(error)) throw error;
571
- const text = getFailureMessage(error, true);
572
- failure = {
573
- ...toWorkerError(text.code, text.message, node),
574
- toolMessage: text.toolMessage,
575
- };
576
- return true;
577
- }
578
- if (!search.satisfiesConstraints(node.state, current.state)) return false;
579
- search.trackStatePossible(node.state);
580
- if (!search.satisfiesActionConstraints(current.state, node.state)) return false;
581
- search.trackActionPossible(current.state, node.state);
582
- if (successors.length === getSetBound()) throw new TlaTooManyStatesError();
583
- successors.push(node);
584
- return false;
585
- },
586
- action.scope,
587
- options.isGenerating ? workerRandom : undefined,
588
- );
553
+ if (coverage && coveredAction && !options.isGenerating) {
554
+ walkCoveredSuccessors(coverage, coveredAction, current.state, environment, (state) => {
555
+ if (visit({ state, action: action.name })) throw STOP_WALK;
556
+ });
557
+ } else {
558
+ forEachSuccessor(
559
+ action.expression,
560
+ current.state,
561
+ environment,
562
+ coverage && coveredAction
563
+ ? (successor) => {
564
+ coverage.primary[coveredAction.node]++;
565
+ return visit(successor);
566
+ }
567
+ : visit,
568
+ action.scope,
569
+ options.isGenerating ? workerRandom : undefined,
570
+ );
571
+ }
589
572
  } catch (error) {
573
+ if (error === STOP_WALK) return { successors, error: failure };
590
574
  if (!(error instanceof TlaIncompleteSuccessorError)) throw error;
591
575
  const node = toNode(
592
576
  error.state.map((value) => value ?? createModelValue("null")),
@@ -619,13 +603,16 @@ export const simulate = (options: SimulationOptions): number => {
619
603
  for (let position = 0; position < trace.length; position++) {
620
604
  trace[position].state.forEach(markNormalized);
621
605
  }
622
- const found = findTemporalViolation(trace, runtime.getFingerprint, livenessChecks);
606
+ const found = livenessChecks.findTraceViolation(
607
+ trace.map((node) => node.state),
608
+ trace.map((node) => runtime.getFingerprint(node.state)),
609
+ );
623
610
  if (!found) return undefined;
624
- const back = trace[found.length];
625
- const lasso = trace.slice(0, found.length);
611
+ const back = found.backPosition === undefined ? undefined : trace[found.backPosition];
612
+ const lasso = found.positions.map((position) => trace[position]);
613
+ const loopStart = back ? found.cyclePosition - 1 : lasso.length - 1;
626
614
  const steps = toTraceSteps(lasso);
627
- const loopAction =
628
- found.isStuttering || !back ? createActionRecord(undefined) : toActionValue(back.action);
615
+ const loopAction = back ? toActionValue(back.action) : createActionRecord(undefined);
629
616
  return {
630
617
  ...toWorkerError(MESSAGE_CODE.temporalPropertyViolated, "", last),
631
618
  isException: true,
@@ -633,13 +620,11 @@ export const simulate = (options: SimulationOptions): number => {
633
620
  name: found.properties[0],
634
621
  properties: found.properties,
635
622
  trace: steps,
636
- loopStart: found.loopStart,
623
+ loopStart,
637
624
  loopLabel: back && getActionLabel(back.action, options.version, back.level === 1),
638
625
  },
639
- counterExample: getCounterExample(lasso, steps, {
640
- start: found.loopStart,
641
- action: loopAction,
642
- }),
626
+ failures: found.failures.map((failure) => toException(failure, last)),
627
+ counterExample: getCounterExample(lasso, steps, { start: loopStart, action: loopAction }),
643
628
  };
644
629
  };
645
630
  const simulateTrace = (): WorkerError | "stopped" | undefined => {
@@ -650,7 +635,7 @@ export const simulate = (options: SimulationOptions): number => {
650
635
  const prime = nextPrime(workerRandom);
651
636
  let successors: TraceNode[] = [];
652
637
  for (let attempt = 0; attempt < nextActions.length; attempt++) {
653
- const result = getSuccessors(currentNode, nextActions[actionIndex]);
638
+ const result = getSuccessors(currentNode, actionIndex);
654
639
  // Deviation from TLC: TLCSet("exit") interrupts TLC's SimulationWorker, whose BlockingQueue#put then drops a violation found in the same step.
655
640
  if (result.error) return result.error;
656
641
  successors = result.successors;
@@ -733,6 +718,9 @@ export const simulate = (options: SimulationOptions): number => {
733
718
  const reported = printed === -1 ? errors : errors.slice(0, printed + 1);
734
719
  reported.forEach((error) => {
735
720
  if (error.violation) {
721
+ error.failures?.forEach((failure) =>
722
+ reportMessage(failure.code, failure.message, failure.toolMessage),
723
+ );
736
724
  options.onProgress?.({
737
725
  kind: "violation",
738
726
  status: "liveness-violation",
@@ -761,6 +749,8 @@ export const simulate = (options: SimulationOptions): number => {
761
749
  : EXIT_STATUS.success;
762
750
  const status = Math.max(errorStatus, postconditionStatus);
763
751
  output({ kind: "progress", statistics: { ...statistics } });
752
+ // TLC's ProgressReport prints the coverage again with its last progress unless -coverage spans more progress reports.
753
+ if (options.coverage !== undefined && options.coverage <= 1) reportCoverage();
764
754
  if (status === EXIT_STATUS.success) printSummary();
765
755
  return status;
766
756
  };
@@ -274,6 +274,8 @@ const getActions = (
274
274
  );
275
275
  case "Label":
276
276
  return getActions(expression.body, scope, closure, environment, instanceActions);
277
+ case "Selection":
278
+ return getActions(expression.expression, scope, closure, environment, instanceActions);
277
279
  case "Or":
278
280
  return expression.operands.flatMap((operand) =>
279
281
  getActions(operand, scope, closure, environment, instanceActions),
@@ -525,6 +527,7 @@ export const getSubscriptVariables = (
525
527
  return expression.elements.every((element) => collect(element, innerScope));
526
528
  }
527
529
  if (expression.kind === "Label") return collect(expression.body, innerScope);
530
+ if (expression.kind === "Selection") return collect(expression.expression, innerScope);
528
531
  const unwrapped = unwrapBinding(expression, innerScope, environment);
529
532
  if (unwrapped) return collect(unwrapped.expression, unwrapped.scope);
530
533
  const argument =