tla-ts 0.0.10-dev.e3e5dd576 → 0.0.10-dev.e5c8dc315

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 (60) hide show
  1. package/dist/deciders.cjs +1 -1
  2. package/dist/deciders.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +10 -10
  8. package/dist/prove.js +57 -57
  9. package/dist/run.cjs +26 -26
  10. package/dist/run.js +32 -32
  11. package/dist/transfer.cjs +94 -94
  12. package/dist/transfer.js +83 -83
  13. package/package.json +1 -1
  14. package/src/ast.ts +16 -0
  15. package/src/check-trace.ts +43 -42
  16. package/src/check.ts +2 -1
  17. package/src/coverage.ts +66 -18
  18. package/src/environment.ts +34 -30
  19. package/src/evaluate.ts +57 -39
  20. package/src/instances.ts +17 -14
  21. package/src/liveness.ts +4 -0
  22. package/src/prover/smt/solver/ackermannize-bv-tactic.ts +3 -2
  23. package/src/prover/smt/solver/bit-blast-tactic.ts +1 -1
  24. package/src/prover/smt/solver/bv1-blast.ts +1 -1
  25. package/src/prover/smt/solver/card2bv.ts +1 -1
  26. package/src/prover/smt/solver/ctx-simplify.ts +8 -2
  27. package/src/prover/smt/solver/diff-neq.ts +8 -6
  28. package/src/prover/smt/solver/dt2bv-tactic.ts +10 -8
  29. package/src/prover/smt/solver/elim-uncnstr.ts +26 -20
  30. package/src/prover/smt/solver/eq2bv-tactic.ts +15 -6
  31. package/src/prover/smt/solver/fd-solver.ts +1 -1
  32. package/src/prover/smt/solver/fix-dl-var.ts +1 -1
  33. package/src/prover/smt/solver/fm-tactic.ts +9 -7
  34. package/src/prover/smt/solver/fpa2bv-tactic.ts +1 -1
  35. package/src/prover/smt/solver/lia2pb.ts +1 -1
  36. package/src/prover/smt/solver/nlqsat.ts +1 -0
  37. package/src/prover/smt/solver/normalize-bounds.ts +2 -2
  38. package/src/prover/smt/solver/occf.ts +1 -1
  39. package/src/prover/smt/solver/pb2bv-tactic.ts +9 -3
  40. package/src/prover/smt/solver/purify-arith.ts +51 -49
  41. package/src/prover/smt/solver/reduce-args.ts +2 -2
  42. package/src/prover/smt/solver/sat/goal2sat.ts +12 -2
  43. package/src/prover/smt/solver/sat/inc-sat-solver.ts +21 -5
  44. package/src/prover/smt/solver/sat/sat-tactic.ts +51 -32
  45. package/src/prover/smt/solver/sat/sat2goal.ts +4 -3
  46. package/src/prover/smt/solver/smtlib-script.ts +6 -4
  47. package/src/prover/smt/solver/solver.ts +0 -1
  48. package/src/prover/smt/solver/tactic.ts +40 -5
  49. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -0
  50. package/src/prover/smt/solver/tseitin-cnf.ts +14 -5
  51. package/src/resolve.ts +54 -28
  52. package/src/runtime.ts +14 -1
  53. package/src/sany/generator-proofs.ts +1 -0
  54. package/src/sany/selectors.ts +1 -0
  55. package/src/search.ts +3 -1
  56. package/src/simulate.ts +26 -17
  57. package/src/spec-description.ts +8 -2
  58. package/src/subexpressions.ts +138 -45
  59. package/src/successors.ts +13 -7
  60. package/src/tlc/replay.ts +80 -3
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  getChildren,
3
+ getFunctionConstruction,
3
4
  type AssumeProve,
4
5
  type Bound,
5
6
  type Declaration,
@@ -64,6 +65,7 @@ interface SelectingName {
64
65
  }
65
66
 
66
67
  const declaredArities = new WeakMap<Definition, number>();
68
+ const letDefinitions = new WeakSet<Definition>();
67
69
 
68
70
  const resolveDefinition = (
69
71
  definition: Definition,
@@ -195,6 +197,51 @@ const createApplication = (selection: Selection, body: Expression): Expression =
195
197
  };
196
198
  };
197
199
 
200
+ // SANY expects an operator argument only of a user-defined operator, which a standard module's built-in operators stand for.
201
+ const getCalleeParams = (
202
+ expression: Expression & { kind: "Apply" },
203
+ callee: Definition | undefined,
204
+ ) =>
205
+ callee?.kind === "Operator"
206
+ ? callee.params
207
+ : STANDARD_LEVELS.get(expression.operator.slice(expression.operator.lastIndexOf("!") + 1))
208
+ ?.formals;
209
+
210
+ const getOperatorArgument = (operand: Expression, arity: number): Expression => {
211
+ if (operand.kind !== "Identifier" && operand.kind !== "OperatorReference") return operand;
212
+ const operator = operand.kind === "Identifier" ? operand.name : operand.operator;
213
+ const params = Array.from({ length: arity }, (_value, index) => `argument${index}`);
214
+ return {
215
+ kind: "Lambda",
216
+ params,
217
+ body: {
218
+ kind: "Apply",
219
+ operator,
220
+ args: params.map((name) => ({
221
+ kind: "Identifier",
222
+ name,
223
+ location: operand.location,
224
+ resolution: LOCAL_RESOLUTION,
225
+ })),
226
+ location: operand.location,
227
+ ...(operand.kind === "Identifier" && { resolution: operand.resolution }),
228
+ },
229
+ operator,
230
+ location: operand.location,
231
+ };
232
+ };
233
+
234
+ const getDefinitionBody = (definition: Definition, isLet: boolean) => {
235
+ if (definition.kind === "Function") return getFunctionConstruction(definition, isLet);
236
+ const body = definition.body;
237
+ if (body.kind !== "Let" || body.location.range || body.body.kind !== "Identifier") return body;
238
+ const name = body.body.name;
239
+ const recursive = body.definitions.find((local) => local.name === name);
240
+ return recursive?.kind === "Function"
241
+ ? { ...body, body: getFunctionConstruction(recursive, true) }
242
+ : body;
243
+ };
244
+
198
245
  const applyDefinition = (definition: Definition, selection: Selection) => {
199
246
  const location = selection.location;
200
247
  const params =
@@ -237,7 +284,6 @@ const selectSubexpression = (
237
284
  let selectorIndex = 0;
238
285
  let operatorIndex = -1;
239
286
  let current: Expression | undefined;
240
- let unwrappedSelection: Expression | undefined;
241
287
  let isFailed = false;
242
288
  let generatedArguments = 0;
243
289
  const nulls: NullSelection[] = [];
@@ -270,6 +316,15 @@ const selectSubexpression = (
270
316
  return undefined;
271
317
  };
272
318
 
319
+ const findCallee = (name: string) =>
320
+ selection.lets.flat().findLast((definition) => definition.name === name) ?? lookup(name);
321
+
322
+ const enterScopes = (node: Expression): Expression => {
323
+ if (node.kind !== "Let" || node.location.range) return node;
324
+ selection.lets.push(node.definitions);
325
+ return enterScopes(node.body);
326
+ };
327
+
273
328
  const findDefinition = (local: Definition[] | undefined) => {
274
329
  const start = reference.selectorRanges?.[selectorIndex];
275
330
  const names: string[] = [];
@@ -346,23 +401,21 @@ const selectSubexpression = (
346
401
  `Subexpression of \`${found.name}' used inside the operator's definition.`,
347
402
  );
348
403
  }
349
- if (definition.kind !== "Operator") {
350
- throw createError(`Cannot select a subexpression of ${definition.name}`, location);
351
- }
352
- selection.params.push(...definition.params);
404
+ selection.params.push(...(definition.kind === "Operator" ? definition.params : []));
353
405
  selection.args.push(...found.args);
354
- current = definition.body;
406
+ current = getDefinitionBody(definition, local !== undefined || letDefinitions.has(definition));
355
407
  operatorIndex = selectorIndex;
356
408
  selectorIndex++;
357
409
  return undefined;
358
410
  };
359
411
 
360
412
  const followLabels = () => {
413
+ let owner = current;
361
414
  while (selectorIndex < selectors.length && !isFailed) {
362
415
  const selector = selectors[selectorIndex];
363
416
  if (selector.kind !== "Name") return;
364
417
  const range = reference.selectorRanges?.[selectorIndex];
365
- const found = current && findLabel(current, selector.name, [], []);
418
+ const found = owner && findLabel(owner, selector.name, [], []);
366
419
  const missing = `Cannot find label \`${selector.name}'.`;
367
420
  if (!found) return report(range, missing);
368
421
  found.applications.forEach((application) => checkNull(application, missing, true));
@@ -377,7 +430,9 @@ const selectSubexpression = (
377
430
  selection.params.push(...params.map((name) => ({ name, arity: 0 })));
378
431
  selection.args.push(...selector.args);
379
432
  applications.push(getExpressionArguments(selector.name, range, selector.args));
380
- current = found.label.body;
433
+ const next = selectors[selectorIndex + 1];
434
+ owner = found.label.body;
435
+ current = next === undefined || next.kind === "Name" ? found.label : found.label.body;
381
436
  selectorIndex++;
382
437
  }
383
438
  };
@@ -391,19 +446,8 @@ const selectSubexpression = (
391
446
  report(range, noOperands);
392
447
  return;
393
448
  }
394
- if (selections.has(current) && current.kind !== "Lambda" && current !== unwrappedSelection) {
395
- checkNull(current, noOperands, true);
396
- unwrappedSelection = current;
397
- current =
398
- current.kind === "Let" &&
399
- current.body.kind === "Apply" &&
400
- current.body.operator === SELECTION_NAME
401
- ? current.body
402
- : current.kind === "Selection"
403
- ? current
404
- : // SANY wraps a selection without parameters in $Nop, whose only operand is the selection.
405
- { kind: "Tuple", elements: [current], location: current.location };
406
- }
449
+ if (selections.has(current)) checkNull(current, noOperands, true);
450
+ current = enterScopes(current);
407
451
  if (current.kind === "Label") {
408
452
  current = current.body;
409
453
  selectOperand(selector);
@@ -419,6 +463,27 @@ const selectSubexpression = (
419
463
  current = current.body;
420
464
  return;
421
465
  }
466
+ if (current.kind === "Lambda" && current.operator !== undefined) {
467
+ report(range, "Trying to select subexpression of an operator argument.");
468
+ return;
469
+ }
470
+ if (current.kind === "Lambda" && selector.kind !== "Bound" && selector.kind !== "At") {
471
+ // Deviation from SANY: SANY names a numbered selector here by its syntax node, N_StructOp; tla-ts names it by its number, as the CLI does.
472
+ const name = selector.kind === "Operand" ? selector.operand : getSelectorName(selector);
473
+ report(range, `Cannot use !${name} to select subexpression of a LAMBDA.`);
474
+ return;
475
+ }
476
+ if (
477
+ current.kind === "Lambda" &&
478
+ selector.kind === "Bound" &&
479
+ selector.args.length !== current.params.length
480
+ ) {
481
+ report(
482
+ range,
483
+ `Selector with ${selector.args.length} arguments used for LAMBDA expression taking ${current.params.length} arguments.`,
484
+ );
485
+ return;
486
+ }
422
487
  const binder = getBinder(current);
423
488
  if (binder && (selector.kind === "Bound" || selector.kind === "At")) {
424
489
  if (selector.kind === "Bound" && selector.args.length !== binder.names.length) {
@@ -493,12 +558,17 @@ const selectSubexpression = (
493
558
  report(range, nonexistent);
494
559
  return;
495
560
  }
496
- const operand = operands?.[getOperandIndex(selector.operand, operands.length)];
561
+ const index = getOperandIndex(selector.operand, operands?.length ?? 0);
562
+ const operand = operands?.[index];
497
563
  if (!operand) {
498
564
  report(range, nonexistent);
499
565
  return;
500
566
  }
501
- current = operand;
567
+ const arity =
568
+ current.kind === "Apply"
569
+ ? (getCalleeParams(current, findCallee(current.operator))?.[index]?.arity ?? 0)
570
+ : 0;
571
+ current = arity > 0 ? getOperatorArgument(operand, arity) : operand;
502
572
  };
503
573
 
504
574
  const first = enterDefinition(undefined);
@@ -509,6 +579,7 @@ const selectSubexpression = (
509
579
  const selector = selectors[selectorIndex];
510
580
  if (selector.kind === "Name") {
511
581
  while (current?.kind === "Label") current = current.body;
582
+ current = current && enterScopes(current);
512
583
  if (current?.kind !== "Let") {
513
584
  report(
514
585
  reference.selectorRanges?.[selectorIndex - 1],
@@ -566,6 +637,10 @@ const selectSubexpression = (
566
637
  }
567
638
  if (isFailed) return reference;
568
639
  if (!current) throw createError("Subexpression selection failed", location);
640
+ if (current.kind === "Lambda" && !isOperator) {
641
+ report(location.range, "Selected operator argument when expression expected.");
642
+ return reference;
643
+ }
569
644
  const params = selection.params.length;
570
645
  const args = selection.args.length;
571
646
  if (params !== args) {
@@ -576,7 +651,10 @@ const selectSubexpression = (
576
651
  }
577
652
  deferToExpression(location.range, message);
578
653
  }
579
- const body = wrapLets(current, selection.lets, location);
654
+ const body =
655
+ current.kind === "Lambda"
656
+ ? { ...current, body: wrapLets(current.body, selection.lets, location) }
657
+ : wrapLets(current, selection.lets, location);
580
658
  const share = (expression: Expression): Expression => ({
581
659
  kind: "Selection",
582
660
  expression,
@@ -591,7 +669,7 @@ const selectSubexpression = (
591
669
  selection,
592
670
  body.kind !== "Lambda"
593
671
  ? share(body)
594
- : { ...body, body: selections.has(body) ? body.body : share(body.body), location },
672
+ : { ...body, body: selections.has(current) ? body.body : share(body.body), location },
595
673
  );
596
674
  nullSelections.set(selected, nulls);
597
675
  if (expressionError) operatorSelectionErrors.set(selected, expressionError);
@@ -625,6 +703,7 @@ const resolveSubexpressions = (
625
703
  if (!containsReference(expression)) return expression;
626
704
  if (expression.kind === "Let") {
627
705
  if (expression.recursive) setDeclaredArities(expression.recursive, expression.definitions);
706
+ expression.definitions.forEach((definition) => letDefinitions.add(definition));
628
707
  const resolved: Definition[] = [];
629
708
  const local: Lookup = (name) =>
630
709
  resolved.find((definition) => definition.name === name) ??
@@ -632,11 +711,13 @@ const resolveSubexpressions = (
632
711
  lookup(name);
633
712
  const isLocalGoal = (name: string) =>
634
713
  !expression.definitions.some((definition) => definition.name === name) && isGoal(name);
635
- expression.definitions.forEach((definition) =>
636
- resolved.push(
637
- resolveDefinition(definition, (child) => resolveSubexpressions(child, local, isLocalGoal)),
638
- ),
639
- );
714
+ expression.definitions.forEach((definition) => {
715
+ const resolvedDefinition = resolveDefinition(definition, (child) =>
716
+ resolveSubexpressions(child, local, isLocalGoal),
717
+ );
718
+ letDefinitions.add(resolvedDefinition);
719
+ resolved.push(resolvedDefinition);
720
+ });
640
721
  const mapped = {
641
722
  ...expression,
642
723
  definitions: resolved,
@@ -651,15 +732,10 @@ const resolveSubexpressions = (
651
732
  }
652
733
  : mapped;
653
734
  }
654
- const callee = expression.kind === "Apply" ? lookup(expression.operator) : undefined;
655
- // SANY expects an operator argument only of a user-defined operator, which a standard module's built-in operators stand for.
656
735
  const calleeParams =
657
- callee?.kind === "Operator"
658
- ? callee.params
659
- : expression.kind === "Apply"
660
- ? STANDARD_LEVELS.get(expression.operator.slice(expression.operator.lastIndexOf("!") + 1))
661
- ?.formals
662
- : undefined;
736
+ expression.kind === "Apply"
737
+ ? getCalleeParams(expression, lookup(expression.operator))
738
+ : undefined;
663
739
  const rebuilt =
664
740
  expression.kind === "Apply"
665
741
  ? {
@@ -846,15 +922,23 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
846
922
  return { ...body, facts: resolveFacts(body.facts), defs: resolveDefs(body.defs) };
847
923
  case "Instance":
848
924
  return resolveInstance(body, lookup, isProofGoal);
849
- case "Define":
925
+ case "Define": {
926
+ const defined: Definition[] = [];
927
+ const local = createProofResolver(
928
+ (name) => defined.find((definition) => definition.name === name) ?? lookup(name),
929
+ );
850
930
  return {
851
931
  ...body,
852
- definitions: body.definitions.map((definition) =>
853
- definition.kind === "Instance"
854
- ? resolveInstance(definition, lookup, isProofGoal)
855
- : resolveDefinition(definition, resolveInProof),
856
- ),
932
+ definitions: body.definitions.map((definition) => {
933
+ if (definition.kind === "Instance") {
934
+ return resolveInstance(definition, local.lookup, local.isProofGoal);
935
+ }
936
+ const resolved = resolveDefinition(definition, local.resolveInProof);
937
+ defined.push(resolved);
938
+ return resolved;
939
+ }),
857
940
  };
941
+ }
858
942
  case "Have":
859
943
  case "Case":
860
944
  return { ...body, expression: resolveInProof(body.expression) };
@@ -897,7 +981,16 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
897
981
  }),
898
982
  };
899
983
  };
900
- return { lookup, resolveFacts, resolveDefs, resolveStatement, resolveStep, resolveProof };
984
+ return {
985
+ lookup,
986
+ isProofGoal,
987
+ resolveInProof,
988
+ resolveFacts,
989
+ resolveDefs,
990
+ resolveStatement,
991
+ resolveStep,
992
+ resolveProof,
993
+ };
901
994
  };
902
995
  const hasReferences =
903
996
  module.definitions.some((definition) => containsReference(definition.body)) ||
package/src/successors.ts CHANGED
@@ -68,13 +68,18 @@ export interface Successor {
68
68
  action: string | undefined;
69
69
  }
70
70
 
71
+ export interface SuccessorGenerator {
72
+ random: JavaRandom;
73
+ kept: number;
74
+ }
75
+
71
76
  interface Run {
72
77
  environment: Environment;
73
78
  isInit: boolean;
74
79
  isEnabled: boolean;
75
80
  current: Value[] | undefined;
76
81
  isStopped: boolean;
77
- generator: JavaRandom | undefined;
82
+ generator: SuccessorGenerator | undefined;
78
83
  successors: number;
79
84
  onUnsatisfied?: (state: Value[], conjunct: Expression) => void;
80
85
  }
@@ -526,7 +531,7 @@ const compileMembership = (
526
531
  run.environment.runtime.version,
527
532
  )) {
528
533
  onComplete(assign(partial, index, element), action);
529
- if (run.successors > 0 || run.isStopped) return;
534
+ if (run.generator.kept > 0 || run.isStopped) return;
530
535
  }
531
536
  }
532
537
  for (let position = 0; position < elements.length && !run.isStopped; position++) {
@@ -882,8 +887,8 @@ const compileRandomChoice =
882
887
  (run, scope, partial, action, isFixed, onComplete) => {
883
888
  const generator = run.generator;
884
889
  if (!generator) throw new Error("A random choice needs the run's generator");
885
- const start = Math.floor(generator.nextDouble() * choices.length);
886
- const prime = nextPrime(generator);
890
+ const start = Math.floor(generator.random.nextDouble() * choices.length);
891
+ const prime = nextPrime(generator.random);
887
892
  for (let attempt = 0; attempt < choices.length; attempt++) {
888
893
  choices[(start + attempt * prime) % choices.length](
889
894
  run,
@@ -893,7 +898,7 @@ const compileRandomChoice =
893
898
  isFixed,
894
899
  onComplete,
895
900
  );
896
- if (run.successors > 0 || run.isStopped) return;
901
+ if (generator.kept > 0 || run.isStopped) return;
897
902
  }
898
903
  };
899
904
 
@@ -917,7 +922,7 @@ const compileRandomExists = (
917
922
  const visit = (position: number, boundScope: Scope | undefined): boolean => {
918
923
  if (position < 0) {
919
924
  body(run, boundScope, partial, action, isFixed, onComplete);
920
- return run.successors > 0 || run.isStopped;
925
+ return (run.generator?.kept ?? 0) > 0 || run.isStopped;
921
926
  }
922
927
  const pattern = expression.bounds[position].pattern;
923
928
  return orders[position].some((element) =>
@@ -928,6 +933,7 @@ const compileRandomExists = (
928
933
  };
929
934
  };
930
935
 
936
+ // Deviation from TLC: Tool#getNextStates in -generate throws an UnsupportedOperationException at a CASE, which tla-ts takes as the model checker does.
931
937
  const compileRandomAction = (expression: Expression, environment: Environment) => {
932
938
  if (expression.kind === "Exists") return compileRandomExists(expression, environment);
933
939
  if (expression.kind !== "Or") return undefined;
@@ -1312,7 +1318,7 @@ export const forEachSuccessor = (
1312
1318
  environment: Environment,
1313
1319
  onSuccessor: (successor: Successor) => boolean,
1314
1320
  scope?: Scope,
1315
- generator?: JavaRandom,
1321
+ generator?: SuccessorGenerator,
1316
1322
  onUnsatisfied?: Run["onUnsatisfied"],
1317
1323
  onIncomplete?: (state: State, action: string | undefined) => void,
1318
1324
  ) => {
package/src/tlc/replay.ts CHANGED
@@ -3,6 +3,7 @@ import { TlaEvaluationError } from "../errors";
3
3
  import { isConstantLevel } from "../dependencies";
4
4
  import {
5
5
  chargeContexts,
6
+ chargeRandomOrder,
6
7
  countCoverage,
7
8
  createChargedEnumerator,
8
9
  getCoverageNode,
@@ -17,6 +18,7 @@ import {
17
18
  evaluate,
18
19
  forEachBoundElement,
19
20
  freezeCallStack,
21
+ getContextElements,
20
22
  getLazyFunction,
21
23
  getQuantifierElements,
22
24
  mapBoundSets,
@@ -39,7 +41,9 @@ import {
39
41
  type Value,
40
42
  } from "../values";
41
43
  import { substitutionFrames } from "../instances";
44
+ import type { SuccessorGenerator } from "../successors";
42
45
  import { MESSAGE_CODE } from "./messages";
46
+ import { getRandomSubset, nextPrime } from "./random";
43
47
 
44
48
  interface ReplayAction {
45
49
  expression: Expression;
@@ -63,6 +67,7 @@ interface Replay {
63
67
  current: Value[] | undefined;
64
68
  onState: (state: Value[]) => void;
65
69
  onIncomplete: ((state: State) => boolean) | undefined;
70
+ generator: SuccessorGenerator | undefined;
66
71
  }
67
72
 
68
73
  const withFrame = <Result>(replay: Replay, expression: Expression, run: () => Result): Result => {
@@ -462,7 +467,10 @@ const visitAssignment = (
462
467
  return proceedWithValue(replay, items, partial, index, value, node);
463
468
  }
464
469
  const set: Value = expression.operator === "\\subseteq" ? { kind: "Subset", base: value } : value;
465
- if (typeof set === "object" && set !== value) replay.callStack?.sources.set(set, source);
470
+ if (typeof set === "object" && set !== value) {
471
+ replay.callStack?.sources.set(set, source);
472
+ replay.coverage?.values.set(set, node);
473
+ }
466
474
  if (assigned !== undefined) {
467
475
  return (
468
476
  evaluateAt(replay, node, expression, context) === true &&
@@ -472,6 +480,14 @@ const visitAssignment = (
472
480
  if (!isTlcEnumerable(set))
473
481
  throw new TlaEvaluationError("The right side of \\IN is not enumerable.");
474
482
  const elements = getStreamedElements(set);
483
+ const generator = replay.generator;
484
+ if (generator) {
485
+ const order = getRandomOrder(replay, set, elements);
486
+ for (let position = 0; position < order.length; position++) {
487
+ const isProduced = proceed(replay, items, assign(partial, index, order[position]), node);
488
+ if (generator.kept > 0) return isProduced;
489
+ }
490
+ }
475
491
  const enumerator =
476
492
  replay.coverage && createChargedEnumerator(replay.coverage, set, elements, false);
477
493
  const isProduced = elements.reduce<boolean>((isProduced, element) => {
@@ -514,6 +530,13 @@ export const getEnabledFailure = (expression: Expression) => {
514
530
  return undefined;
515
531
  };
516
532
 
533
+ const getRandomOrder = (replay: Replay, set: Value, elements: Value[]) => {
534
+ const runtime = replay.environment.runtime;
535
+ const order = getRandomSubset(elements.length, set, runtime.getRandom(), runtime.version);
536
+ if (replay.coverage) chargeRandomOrder(replay.coverage, set, elements.length, order);
537
+ return order;
538
+ };
539
+
517
540
  const getBindings = (
518
541
  replay: Replay,
519
542
  expression: Expression & { kind: "Exists" | "Forall" },
@@ -591,7 +614,39 @@ const visitApplication = (
591
614
  }
592
615
  case "Or": {
593
616
  const operands = expression.operands;
594
- if (expression.isBulleted || operands.length === 2) {
617
+ const isWhole = expression.isBulleted || operands.length === 2;
618
+ const generator = replay.generator;
619
+ if (generator) {
620
+ const count = isWhole ? operands.length : 2;
621
+ const start = Math.floor(generator.random.nextDouble() * count);
622
+ const prime = nextPrime(generator.random);
623
+ let isProduced = false;
624
+ for (let attempt = 0; attempt < count; attempt++) {
625
+ const choice = (start + attempt * prime) % count;
626
+ const isChoiceProduced =
627
+ isWhole || choice === 1
628
+ ? visit(
629
+ replay,
630
+ operands[isWhole ? choice : operands.length - 1],
631
+ items,
632
+ scope,
633
+ partial,
634
+ node,
635
+ )
636
+ : visitAt(
637
+ replay,
638
+ getPrefix(expression, operands.length - 1),
639
+ items,
640
+ scope,
641
+ partial,
642
+ getPrefixNode(replay, node),
643
+ );
644
+ isProduced = isChoiceProduced || isProduced;
645
+ if (generator.kept > 0) return isProduced;
646
+ }
647
+ return isProduced;
648
+ }
649
+ if (isWhole) {
595
650
  return operands.reduce(
596
651
  (isProduced, operand) =>
597
652
  visit(replay, operand, items, scope, partial, node) || isProduced,
@@ -617,6 +672,25 @@ const visitApplication = (
617
672
  return isPrefixProduced || isLastProduced;
618
673
  }
619
674
  case "Exists": {
675
+ const generator = replay.generator;
676
+ if (generator) {
677
+ const version = replay.environment.runtime.version;
678
+ const orders = runAt(replay, node, () =>
679
+ mapBoundSets(expression.bounds, (set) => evaluate(set, context)),
680
+ ).map((set, position) =>
681
+ getRandomOrder(
682
+ replay,
683
+ set,
684
+ getContextElements(set, expression.bounds[position].set, version),
685
+ ),
686
+ );
687
+ let isProduced = false;
688
+ forEachBoundElement(expression.bounds, orders, context.scope, (bound) => {
689
+ isProduced = visit(replay, expression.body, items, bound, partial, node) || isProduced;
690
+ return generator.kept > 0;
691
+ });
692
+ return isProduced;
693
+ }
620
694
  const bound = getBindings(replay, expression, context, node);
621
695
  return bound.bindings.reduce((isProduced, scope) => {
622
696
  bound.charge?.();
@@ -793,6 +867,7 @@ const createReplay = (
793
867
  current: Value[] | undefined,
794
868
  onState: (state: Value[]) => void,
795
869
  onIncomplete?: (state: State) => boolean,
870
+ generator?: SuccessorGenerator,
796
871
  ): Replay => ({
797
872
  environment,
798
873
  callStack: environment.callStack,
@@ -803,6 +878,7 @@ const createReplay = (
803
878
  current,
804
879
  onState,
805
880
  onIncomplete,
881
+ generator,
806
882
  });
807
883
 
808
884
  const ENABLED_FOUND = new Error("The replay found a successor");
@@ -880,8 +956,9 @@ export const walkSuccessors = (
880
956
  environment: Environment,
881
957
  onState: (state: Value[]) => void,
882
958
  onIncomplete?: (state: State) => boolean,
959
+ generator?: SuccessorGenerator,
883
960
  ) => {
884
- const replay = createReplay(environment, "next", state, onState, onIncomplete);
961
+ const replay = createReplay(environment, "next", state, onState, onIncomplete, generator);
885
962
  visit(replay, action.expression, undefined, action.scope, getEmptyState(environment), node);
886
963
  };
887
964