tla-ts 0.0.10-dev.1ebc153bd → 0.0.10-dev.22f7435d3

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 (108) hide show
  1. package/dist/deciders.cjs +31 -31
  2. package/dist/deciders.js +30 -30
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +6 -2
  5. package/dist/index.d.ts +6 -2
  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 +68 -68
  10. package/dist/prove.js +61 -61
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +44 -44
  13. package/dist/transfer.cjs +38 -38
  14. package/dist/transfer.js +29 -29
  15. package/dist/worker.cjs +7 -7
  16. package/dist/worker.js +7 -7
  17. package/package.json +1 -1
  18. package/src/bags.ts +5 -2
  19. package/src/builtins.ts +4 -3
  20. package/src/check-failure.ts +9 -6
  21. package/src/check-trace.ts +116 -64
  22. package/src/check.ts +343 -166
  23. package/src/cli/tlc-output.ts +1 -0
  24. package/src/cli/tlc.ts +3 -2
  25. package/src/codegen.ts +23 -8
  26. package/src/dependencies.ts +5 -4
  27. package/src/dfid.ts +13 -8
  28. package/src/environment.ts +1 -0
  29. package/src/evaluate.ts +58 -23
  30. package/src/exit-codes.ts +27 -13
  31. package/src/java-hash.ts +14 -0
  32. package/src/liveness-checker.ts +131 -81
  33. package/src/liveness.ts +48 -10
  34. package/src/parser.ts +6 -1
  35. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  36. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  37. package/src/prover/smt/solver/asserted-formulas.ts +109 -63
  38. package/src/prover/smt/solver/ast.ts +5 -3
  39. package/src/prover/smt/solver/auf-solver.ts +23 -12
  40. package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
  41. package/src/prover/smt/solver/bound-simplifier.ts +10 -11
  42. package/src/prover/smt/solver/card2bv.ts +34 -15
  43. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -23
  44. package/src/prover/smt/solver/context.ts +44 -11
  45. package/src/prover/smt/solver/dependent-expr-state.ts +19 -13
  46. package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
  47. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  48. package/src/prover/smt/solver/elim-uncnstr.ts +1 -1
  49. package/src/prover/smt/solver/fd-solver.ts +146 -39
  50. package/src/prover/smt/solver/internalizer.ts +1 -1
  51. package/src/prover/smt/solver/lp/lp-settings.ts +4 -0
  52. package/src/prover/smt/solver/maxcore.ts +2 -1
  53. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  54. package/src/prover/smt/solver/model-checker.ts +1 -1
  55. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  56. package/src/prover/smt/solver/model-generator.ts +82 -42
  57. package/src/prover/smt/solver/muz/dl-context.ts +12 -40
  58. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  59. package/src/prover/smt/solver/muz/dl-rule.ts +6 -7
  60. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  61. package/src/prover/smt/solver/muz/horn-tactic.ts +14 -1
  62. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  63. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  64. package/src/prover/smt/solver/opt-context.ts +12 -4
  65. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  66. package/src/prover/smt/solver/opt-solver.ts +4 -3
  67. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  68. package/src/prover/smt/solver/poly-rewriter.ts +1 -0
  69. package/src/prover/smt/solver/proto-model.ts +4 -2
  70. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  71. package/src/prover/smt/solver/qsat.ts +23 -8
  72. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  73. package/src/prover/smt/solver/rewriter.ts +31 -25
  74. package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
  75. package/src/prover/smt/solver/sat/inc-sat-solver.ts +56 -28
  76. package/src/prover/smt/solver/sat/sat-tactic.ts +13 -4
  77. package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
  78. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  79. package/src/prover/smt/solver/smt-kernel.ts +39 -4
  80. package/src/prover/smt/solver/smt-setup.ts +28 -4
  81. package/src/prover/smt/solver/smtlib-parser.ts +31 -4
  82. package/src/prover/smt/solver/smtlib-script.ts +219 -82
  83. package/src/prover/smt/solver/solve-eqs.ts +24 -17
  84. package/src/prover/smt/solver/solver.ts +18 -3
  85. package/src/prover/smt/solver/tactic.ts +132 -78
  86. package/src/prover/smt/solver/theory-bv.ts +11 -1
  87. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  88. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  89. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -0
  90. package/src/prover/smt/solver/theory-lra-propagate.ts +7 -1
  91. package/src/prover/smt/solver/theory-lra.ts +1 -0
  92. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  93. package/src/resolve.ts +446 -167
  94. package/src/runtime.ts +4 -0
  95. package/src/search.ts +161 -92
  96. package/src/simulate.ts +165 -102
  97. package/src/spec-description.ts +3 -4
  98. package/src/standard-modules.ts +56 -22
  99. package/src/subexpressions.ts +168 -49
  100. package/src/successors.ts +6 -3
  101. package/src/symmetry.ts +1 -0
  102. package/src/tlc/random.ts +2 -1
  103. package/src/tlc/replay.ts +22 -14
  104. package/src/tlc/tlc-operators.ts +7 -7
  105. package/src/tlc/tlcext-operators.ts +26 -10
  106. package/src/tlc/trace-spec.ts +6 -20
  107. package/src/typed-tier.ts +9 -4
  108. package/src/values.ts +95 -57
@@ -53,9 +53,8 @@ import { isMbqiByFeatures, isMbqiByLogic } from "./smt-setup";
53
53
  import { isSupportedLogic, logicHasSeq } from "./smt-logics";
54
54
  import { getVerdict, solveAssertions, solveAssuming } from "./solver";
55
55
  import { getConsequences } from "./smt-consequences";
56
- import { getFdConsequences } from "./fd-solver";
56
+ import { checkFdSolver, getFdConsequences } from "./fd-solver";
57
57
  import { SmtKernel } from "./smt-kernel";
58
- import { Mus } from "./mus";
59
58
  import { RefVector } from "./ref-vector";
60
59
  import { IncSatSolver } from "./sat/inc-sat-solver";
61
60
  import { mkThSolver } from "./euf/th-solvers";
@@ -67,6 +66,7 @@ import { L_FALSE, L_TRUE, L_UNDEF } from "./lbool";
67
66
  import type { DatalogContext } from "./muz/dl-context";
68
67
  import { createDatalogContext } from "./muz/horn-tactic";
69
68
  import { printModel } from "./model-printer";
69
+ import { ModelEvaluator } from "./model-evaluator";
70
70
  import { FuncInterp, getManagerSomeValue, ProtoModel } from "./proto-model";
71
71
  import {
72
72
  applyTactic,
@@ -106,11 +106,9 @@ const UNPORTED_COMMANDS = new Set([
106
106
  "ff-certify",
107
107
  "ff-check-native-proof",
108
108
  "ff-integrated-certify",
109
- "get-assignment",
110
109
  "get-interpolant",
111
110
  "get-proof",
112
111
  "get-proof-graph",
113
- "get-unsat-assumptions",
114
112
  "get-user-tactics",
115
113
  "help",
116
114
  "help-simplifier",
@@ -178,6 +176,7 @@ const SIMPLIFY_PARAMETERS = new Set([
178
176
  "som",
179
177
  "sort_sums",
180
178
  "arith_lhs",
179
+ "arith_ineq_lhs",
181
180
  "gcd_rounding",
182
181
  "hi_div0",
183
182
  "mul2concat",
@@ -267,6 +266,17 @@ const getSimplifyParams = (items: SExpr[]) => {
267
266
  return params;
268
267
  };
269
268
 
269
+ const GET_INFO_KEYWORDS = [
270
+ ":reason-unknown",
271
+ ":status",
272
+ ":version",
273
+ ":authors",
274
+ ":error-behavior",
275
+ ":parameters",
276
+ ":rlimit",
277
+ ":assertion-stack-levels",
278
+ ];
279
+
270
280
  const APPLY_PARAMETERS = new Map([
271
281
  ["print", true],
272
282
  ["print_model_converter", true],
@@ -344,6 +354,7 @@ interface Scope {
344
354
  assertions: number;
345
355
  objectives: number;
346
356
  softTrail: number;
357
+ rlimit: number;
347
358
  }
348
359
 
349
360
  interface CheckResult {
@@ -414,6 +425,7 @@ export const runSmtlibScript = (
414
425
  isPrintingSuccess: false,
415
426
  isProducingModels: true,
416
427
  isProducingUnsatCores: false,
428
+ isProducingUnsatAssumptions: false,
417
429
  isProducingProofs: false,
418
430
  isExitingOnError: false,
419
431
  isInteractive: false,
@@ -550,14 +562,15 @@ export const runSmtlibScript = (
550
562
  return false;
551
563
  }
552
564
  if (error instanceof SmtDefaultError) {
553
- optimizer.release();
554
- setCheck(L_UNDEF, { isOptimizing: true, reasonUnknown: "unknown" });
565
+ setCheck(L_UNDEF, { isFromTactic: true, reasonUnknown: optimizer.getReasonUnknown() });
555
566
  throw new SmtParseError(error.message, end);
556
567
  }
557
568
  if (!(error instanceof SmtCanceledError || isStackOverflow(error))) throw error;
558
569
  const reasonUnknown = optimizer.getReasonUnknown();
559
570
  optimizer.release();
560
571
  setCheck(L_UNDEF, { isOptimizing: true, reasonUnknown });
572
+ if (error instanceof SmtCanceledError)
573
+ throw new SmtParseError(manager.limit.getCancelMessage(), end);
561
574
  print("unknown");
562
575
  return true;
563
576
  } finally {
@@ -571,21 +584,22 @@ export const runSmtlibScript = (
571
584
  };
572
585
  // The port keeps answering with the smt kernel where it lacks a piece of the logic's strategy.
573
586
  const solveWithStrategy = () => {
574
- const goal = getGoal();
575
- const strategy =
576
- settings.parameters.get("tactic.default_tactic") ??
577
- getLogicStrategy(state.logic, goal.isProofsEnabled);
578
587
  try {
588
+ if (isUsingFdSolver())
589
+ return {
590
+ ...checkFdSolver(state.manager, getSatParams(settings.parameters), state.assertions),
591
+ context: undefined,
592
+ };
593
+ const goal = getGoal();
594
+ const strategy =
595
+ settings.parameters.get("tactic.default_tactic") ??
596
+ getLogicStrategy(state.logic, goal.isProofsEnabled);
579
597
  const checked = checkSatUsing(compileInternalTactic(getTacticEnvironment(), strategy), goal);
580
598
  return {
581
599
  result: checked.status,
582
600
  context: undefined,
583
601
  model: checked.model,
584
- // The finite-domain solver is an inc_sat_solver, whose reason stays "no reason given" after an answer.
585
- reasonUnknown:
586
- isUsingFdSolver() && checked.status !== L_UNDEF
587
- ? "no reason given"
588
- : checked.reasonUnknown,
602
+ reasonUnknown: checked.reasonUnknown,
589
603
  };
590
604
  } catch (error) {
591
605
  if (!(error instanceof SmtUnsupportedError)) throw error;
@@ -626,6 +640,7 @@ export const runSmtlibScript = (
626
640
  isMbqi: settings.isMbqi || (!isSetUp && isMbqiByLogic(getSetup())),
627
641
  mbqiMaxIterations: settings.mbqiMaxIterations,
628
642
  isFindingMacros: settings.isFindingMacros,
643
+ isCoreMinimizing: settings.parameters.get("smt.core.minimize") === "true",
629
644
  setup: getSetup(),
630
645
  });
631
646
  // mk_special_solver_for_logic gives these logics the finite-domain solver, incremental or not.
@@ -663,11 +678,12 @@ export const runSmtlibScript = (
663
678
  setCheck(solved.result, {
664
679
  context: solved.context,
665
680
  isOptimizing,
666
- isFromTactic: isFromTactic && !isUsingFdSolver(),
681
+ isFromTactic,
667
682
  model: solved.model,
668
683
  core: solved.core ?? solved.context?.unsatCore ?? [],
669
684
  reasonUnknown: getReasonUnknown(solved, deadline),
670
685
  });
686
+ if (isFromTactic && isUsingFdSolver()) solved.model?.decRef();
671
687
  print(getVerdict(solved.result));
672
688
  } catch (error) {
673
689
  if (
@@ -679,13 +695,14 @@ export const runSmtlibScript = (
679
695
  )
680
696
  throw error;
681
697
  const context = isKernelChecking ? state.kernel?.context : undefined;
698
+ const reasonUnknown = isKernelChecking ? state.satSolver?.reasonUnknown : undefined;
682
699
  setCheck(L_UNDEF, {
683
700
  isOptimizing,
684
- isFromTactic: !isKernelChecking && !isUsingFdSolver(),
701
+ isFromTactic: !isKernelChecking,
685
702
  reasonUnknown:
686
703
  error instanceof SmtDefaultError
687
704
  ? error.message
688
- : getReasonUnknown({ result: L_UNDEF, context, reasonUnknown: undefined }, deadline),
705
+ : getReasonUnknown({ result: L_UNDEF, context, reasonUnknown }, deadline),
689
706
  });
690
707
  print("unknown");
691
708
  } finally {
@@ -703,7 +720,9 @@ export const runSmtlibScript = (
703
720
  throw check.isFromTactic
704
721
  ? new SmtParseError("model is not available", list.end)
705
722
  : new SmtUnsupportedError("the model of a canceled search");
706
- const model = check.context?.getModel() ?? new ProtoModel(state.manager);
723
+ const context = check.context;
724
+ const model = context === undefined ? new ProtoModel(state.manager) : context.getModel();
725
+ if (model === undefined) throw new SmtParseError("model is not available", list.end);
707
726
  model.incRef();
708
727
  check.model = model;
709
728
  return model;
@@ -755,6 +774,13 @@ export const runSmtlibScript = (
755
774
  useSatSolver((solver) => solver.push());
756
775
  }
757
776
  };
777
+ const initManager = () => {
778
+ if (state.isProofsEnabled !== undefined) return;
779
+ state.isProofsEnabled = settings.parameters.get("proof") === "true";
780
+ if (state.logic === undefined || logicHasSeq(state.logic)) state.manager.initSeqPlugin();
781
+ if (state.logic === undefined) state.reader.registerList();
782
+ resetKernel();
783
+ };
758
784
  // The parts of inc_sat_solver that are not ported leave its answers to the smt kernel.
759
785
  const useSatSolver = <Result>(action: (solver: IncSatSolver) => Result) => {
760
786
  const solver = state.satSolver;
@@ -989,10 +1015,15 @@ export const runSmtlibScript = (
989
1015
  `Invalid function name. Expecting sort list starting with '(' to disambiguate function name got ${domain.lastId}`,
990
1016
  domain,
991
1017
  );
992
- const sorts = domain.items.map((sort) => state.reader.parseSort(sort));
1018
+ const sorts = domain.items.map((sort) =>
1019
+ state.reader.parseSort(
1020
+ sort,
1021
+ "Invalid function name. Expecting sort list starting with '(' to disambiguate function name",
1022
+ ),
1023
+ );
993
1024
  if (reference.items[2] === undefined)
994
1025
  throw new SmtParseError("Invalid function name invalid sort, unexpected ')'", reference.end);
995
- const range = state.reader.parseSort(reference.items[2]);
1026
+ const range = state.reader.parseSort(reference.items[2], "Invalid function name");
996
1027
  const follower = reference.items[3] ?? reference.end;
997
1028
  const found = state.reader.getFuncs(name.text).find((decl) => {
998
1029
  const funcDecl = state.manager.funcDecls[decl];
@@ -1011,6 +1042,76 @@ export const runSmtlibScript = (
1011
1042
  throw new SmtUnsupportedError("a function declaration reference with trailing terms");
1012
1043
  return found;
1013
1044
  };
1045
+ // get_info_cmd prints in set_next_arg, before the parser checks the rest of the command.
1046
+ const printInfo = (list: SmtList, keyword: string) => {
1047
+ if (keyword === ":reason-unknown") {
1048
+ const check = state.check;
1049
+ const reasonUnknown =
1050
+ check === undefined
1051
+ ? "state of the most recent check-sat command is not known"
1052
+ : check.reasonUnknown;
1053
+ if (reasonUnknown === undefined)
1054
+ throw new SmtUnsupportedError("the information :reason-unknown");
1055
+ print(`(:reason-unknown "${reasonUnknown.replaceAll('"', '\\"')}")`);
1056
+ return;
1057
+ }
1058
+ if (keyword === ":rlimit") {
1059
+ initManager();
1060
+ print(`(:rlimit ${state.manager.limit.count})`);
1061
+ return;
1062
+ }
1063
+ if (keyword === ":error-behavior") {
1064
+ print(
1065
+ `(:error-behavior ${settings.isExitingOnError ? "immediate-exit" : "continued-execution"})`,
1066
+ );
1067
+ return;
1068
+ }
1069
+ if (keyword === ":name") {
1070
+ print('(:name "Z3")');
1071
+ return;
1072
+ }
1073
+ if (keyword === ":authors") {
1074
+ print(
1075
+ '(:authors "Leonardo de Moura, Nikolaj Bjorner, Lev Nachmanson and Christoph Wintersteiger")',
1076
+ );
1077
+ return;
1078
+ }
1079
+ if (keyword === ":version") {
1080
+ print('(:version "5.3.2")');
1081
+ return;
1082
+ }
1083
+ if (keyword === ":status") {
1084
+ print(`(:status ${getVerdict(settings.status)})`);
1085
+ return;
1086
+ }
1087
+ if (keyword === ":assertion-stack-levels") {
1088
+ print(`(:assertion-stack-levels ${state.scopes.length})`);
1089
+ return;
1090
+ }
1091
+ if (keyword === ":all-statistics" || keyword === ":parameters")
1092
+ throw new SmtUnsupportedError(`the information ${keyword}`);
1093
+ if (keyword !== ":?") {
1094
+ print("unsupported");
1095
+ printDiagnostic(`; ${keyword} line: ${list.line} position: ${list.column}`);
1096
+ }
1097
+ print("; Suppported get-info parameters:");
1098
+ for (const supported of GET_INFO_KEYWORDS) print(`; (get-info ${supported})`);
1099
+ };
1100
+ const printCore = () => {
1101
+ const check = state.check;
1102
+ if (check?.core === undefined)
1103
+ throw new SmtUnsupportedError("the unsat core of a tactic that drops dependencies");
1104
+ const kernel = state.kernel;
1105
+ if (kernel === undefined || check.context !== kernel.context) {
1106
+ print(`(${check.core.map(format).join(" ")})`);
1107
+ return;
1108
+ }
1109
+ state.manager.limit.restart(Date.now() + options.timeLimitMs);
1110
+ const core = new RefVector(state.manager);
1111
+ kernel.getUnsatCore(core);
1112
+ print(`(${core.items.map(format).join(" ")})`);
1113
+ core.reset();
1114
+ };
1014
1115
  const runCommand = new Map<string, (list: SmtList) => void>([
1015
1116
  [
1016
1117
  "assert",
@@ -1062,7 +1163,9 @@ export const runSmtlibScript = (
1062
1163
  throw new SmtParseError("at least 2 arguments expected", list.end);
1063
1164
  const decl = state.manager.mkFuncDecl(
1064
1165
  name.text,
1065
- domain.items.map((sort) => state.reader.parseSort(sort)),
1166
+ domain.items.map((sort) =>
1167
+ state.reader.parseSort(sort, "expecting sort list starting with '('"),
1168
+ ),
1066
1169
  SORT_BOOL,
1067
1170
  );
1068
1171
  state.reader.insertFunc(decl, list.end);
@@ -1080,7 +1183,7 @@ export const runSmtlibScript = (
1080
1183
  list.end,
1081
1184
  );
1082
1185
  const manager = state.manager;
1083
- const range = state.reader.parseSort(sort);
1186
+ const range = state.reader.parseSort(sort, "invalid command argument, sort expected");
1084
1187
  const decl = manager.mkFuncDecl(name.text, [], range);
1085
1188
  state.reader.insertFunc(decl, list.end);
1086
1189
  getDatalog().registerVariable(manager.mkApp(decl, [], range));
@@ -1094,7 +1197,7 @@ export const runSmtlibScript = (
1094
1197
  const name = list.items[2];
1095
1198
  getDatalog().addRule(
1096
1199
  state.reader.parseFormula(rule, rule),
1097
- isSymbol(name) ? name.text : "",
1200
+ isSymbol(name) ? name.text : undefined,
1098
1201
  );
1099
1202
  },
1100
1203
  ],
@@ -1146,6 +1249,7 @@ export const runSmtlibScript = (
1146
1249
  state.manager.limit.push(settings.rlimit);
1147
1250
  try {
1148
1251
  const checked = checkSatUsing(tactic, goal);
1252
+ const reasonUnknown = checked.reasonUnknown ?? getCancelMessage(deadline);
1149
1253
  setCheck(checked.status, {
1150
1254
  isFromTactic: true,
1151
1255
  model: checked.model,
@@ -1153,9 +1257,13 @@ export const runSmtlibScript = (
1153
1257
  builder.isTrackingDependencies || !hasNames()
1154
1258
  ? getDependencyValues(checked.core)
1155
1259
  : undefined,
1156
- reasonUnknown: checked.reasonUnknown ?? getCancelMessage(deadline),
1260
+ reasonUnknown,
1157
1261
  });
1158
- print(getVerdict(checked.status));
1262
+ print(
1263
+ checked.isConversionCanceled
1264
+ ? `(error "tactic failed: ${reasonUnknown}")`
1265
+ : getVerdict(checked.status),
1266
+ );
1159
1267
  } finally {
1160
1268
  state.manager.limit.pop();
1161
1269
  }
@@ -1281,11 +1389,21 @@ export const runSmtlibScript = (
1281
1389
  assertions: state.assertions.length,
1282
1390
  objectives: state.objectives.length,
1283
1391
  softTrail: state.softTrail.length,
1392
+ rlimit: settings.rlimit,
1284
1393
  });
1285
1394
  state.reader.push();
1286
1395
  state.isIncremental = true;
1287
- state.kernel?.push();
1288
- useSatSolver((solver) => solver.push());
1396
+ const limit = state.manager.limit;
1397
+ limit.push(settings.rlimit);
1398
+ try {
1399
+ state.kernel?.push();
1400
+ useSatSolver((solver) => solver.push());
1401
+ } catch (error) {
1402
+ if (!(error instanceof SmtCanceledError)) throw error;
1403
+ throw new SmtParseError(limit.getCancelMessage(), list.end);
1404
+ } finally {
1405
+ limit.pop();
1406
+ }
1289
1407
  }
1290
1408
  if (list.items[2] !== undefined)
1291
1409
  throw new SmtParseError("invalid push command, ')' expected", list.items[2]);
@@ -1309,6 +1427,7 @@ export const runSmtlibScript = (
1309
1427
  useSatSolver((solver) => solver.pop(count));
1310
1428
  state.reader.pop(count);
1311
1429
  restoreAssertions(scope.assertions);
1430
+ settings.rlimit = scope.rlimit;
1312
1431
  resetOptimizer();
1313
1432
  releaseObjectives(scope.objectives, scope.softTrail);
1314
1433
  state.scopes.length -= count;
@@ -1334,7 +1453,12 @@ export const runSmtlibScript = (
1334
1453
  restoreAssertions(0);
1335
1454
  resetOptimizer();
1336
1455
  releaseObjectives(0, 0);
1337
- state.scopes = state.scopes.map(() => ({ assertions: 0, objectives: 0, softTrail: 0 }));
1456
+ state.scopes = state.scopes.map((scope) => ({
1457
+ ...scope,
1458
+ assertions: 0,
1459
+ objectives: 0,
1460
+ softTrail: 0,
1461
+ }));
1338
1462
  if (state.isProofsEnabled !== undefined) resetKernel();
1339
1463
  state.isIncremental = state.scopes.length > 0;
1340
1464
  state.hasChecked = false;
@@ -1417,6 +1541,7 @@ export const runSmtlibScript = (
1417
1541
  if (isPrintingDependencies && !builder.isTrackingDependencies && hasNames())
1418
1542
  throw new SmtUnsupportedError("the dependencies of a tactic that drops dependencies");
1419
1543
  state.manager.limit.restart(Date.now() + options.timeLimitMs);
1544
+ state.manager.limit.push(settings.rlimit);
1420
1545
  const goals = (() => {
1421
1546
  try {
1422
1547
  return applyTactic(tactic, input);
@@ -1424,6 +1549,8 @@ export const runSmtlibScript = (
1424
1549
  if (!(error instanceof SmtTacticError)) throw error;
1425
1550
  print(`(error "tactic failed: ${error.message}")`);
1426
1551
  return undefined;
1552
+ } finally {
1553
+ state.manager.limit.pop();
1427
1554
  }
1428
1555
  })();
1429
1556
  // apply_tactic_cmd releases the result goals and then the input goal.
@@ -1540,32 +1667,23 @@ export const runSmtlibScript = (
1540
1667
  "unsat core construction is not enabled, use command (set-option :produce-unsat-cores true)",
1541
1668
  list.end,
1542
1669
  );
1543
- const check = state.check;
1544
- if (check === undefined || check.status !== L_FALSE)
1670
+ if (state.check?.status !== L_FALSE)
1545
1671
  throw new SmtParseError("unsat core is not available", list.end);
1546
- if (check.core === undefined)
1547
- throw new SmtUnsupportedError("the unsat core of a tactic that drops dependencies");
1548
- const kernel = state.kernel;
1549
- if (
1550
- kernel === undefined ||
1551
- check.context !== kernel.context ||
1552
- settings.parameters.get("smt.core.minimize") !== "true"
1553
- ) {
1554
- print(`(${check.core.map(format).join(" ")})`);
1555
- return;
1556
- }
1557
- const mus = new Mus(state.manager, {
1558
- checkSat: (assumptions) => kernel.check(assumptions),
1559
- getUnsatCore: () => kernel.context.unsatCore,
1560
- });
1561
- const core = [...kernel.context.unsatCore];
1562
- for (const literal of core) mus.addSoft(literal);
1563
- const minimized = new RefVector(state.manager);
1564
- state.manager.limit.restart(Date.now() + options.timeLimitMs);
1565
- const status = mus.getMus(minimized);
1566
- print(`(${(status === L_TRUE ? minimized.items : core).map(format).join(" ")})`);
1567
- minimized.reset();
1568
- mus.reset();
1672
+ printCore();
1673
+ },
1674
+ ],
1675
+ [
1676
+ "get-unsat-assumptions",
1677
+ (list) => {
1678
+ checkArgumentCount(list, 0);
1679
+ if (!settings.isProducingUnsatAssumptions)
1680
+ throw new SmtParseError(
1681
+ "unsat assumptions construction is not enabled, use command (set-option :produce-unsat-assumptions true)",
1682
+ list.end,
1683
+ );
1684
+ if (state.check?.status !== L_FALSE)
1685
+ throw new SmtParseError("unsat assumptions is not available", list.end);
1686
+ printCore();
1569
1687
  },
1570
1688
  ],
1571
1689
  [
@@ -1581,8 +1699,11 @@ export const runSmtlibScript = (
1581
1699
  if (term === -1) throw new SmtParseError("no arguments passed to eval", list.end);
1582
1700
  const manager = state.manager;
1583
1701
  manager.incRef(term);
1584
- const value = model.eval(term, false);
1702
+ const evaluator = new ModelEvaluator(manager, model);
1703
+ const value = evaluator.rewrite(term);
1585
1704
  display(value);
1705
+ // eval_cmd frees its model_evaluator before the value, and the parser frees the term last.
1706
+ evaluator.release();
1586
1707
  manager.decRef(value);
1587
1708
  manager.decRef(term);
1588
1709
  },
@@ -1626,6 +1747,23 @@ export const runSmtlibScript = (
1626
1747
  print(")");
1627
1748
  },
1628
1749
  ],
1750
+ [
1751
+ "get-assignment",
1752
+ (list) => {
1753
+ checkArgumentCount(list, 0);
1754
+ const manager = state.manager;
1755
+ const model = getModel(list);
1756
+ const assignments = state.reader.getConstantMacros().flatMap((macro) => {
1757
+ if (manager.sorts[macro.body] !== SORT_BOOL) return [];
1758
+ const value = model.eval(macro.body, true);
1759
+ const assignment = `(${getQuotedName(macro.name)} ${manager.isTrue(value)})`;
1760
+ const isDecided = manager.isTrue(value) || manager.isFalse(value);
1761
+ manager.decRef(value);
1762
+ return isDecided ? [assignment] : [];
1763
+ });
1764
+ print(`(${assignments.join(" ")})`);
1765
+ },
1766
+ ],
1629
1767
  [
1630
1768
  "get-objectives",
1631
1769
  () => {
@@ -1695,7 +1833,13 @@ export const runSmtlibScript = (
1695
1833
  "dbg-some-value",
1696
1834
  (list) => {
1697
1835
  display(
1698
- getManagerSomeValue(state.manager, state.reader.parseSort(list.items[1] ?? list.end)),
1836
+ getManagerSomeValue(
1837
+ state.manager,
1838
+ state.reader.parseSort(
1839
+ list.items[1] ?? list.end,
1840
+ "invalid command argument, sort expected",
1841
+ ),
1842
+ ),
1699
1843
  );
1700
1844
  },
1701
1845
  ],
@@ -1746,8 +1890,19 @@ export const runSmtlibScript = (
1746
1890
  const optionName = `:${getParameterName(keyword)}`;
1747
1891
  const value = list.items[2];
1748
1892
  const isTrue = isSymbol(value, "true");
1893
+ if (
1894
+ (keyword === ":produce-proofs" ||
1895
+ keyword === ":produce-unsat-cores" ||
1896
+ keyword === ":produce-unsat-assumptions") &&
1897
+ state.assertions.length > 0
1898
+ )
1899
+ throw new SmtParseError(
1900
+ `error setting '${keyword}', option value cannot be modified after assertions have been added`,
1901
+ value ?? list.end,
1902
+ );
1749
1903
  if (keyword === ":produce-models") settings.isProducingModels = isTrue;
1750
1904
  if (keyword === ":produce-unsat-cores") settings.isProducingUnsatCores = isTrue;
1905
+ if (keyword === ":produce-unsat-assumptions") settings.isProducingUnsatAssumptions = isTrue;
1751
1906
  if (keyword === ":produce-proofs") settings.isProducingProofs = isTrue;
1752
1907
  if (keyword === ":interactive-mode" || keyword === ":produce-assertions") {
1753
1908
  if (state.isProofsEnabled !== undefined)
@@ -1824,6 +1979,7 @@ export const runSmtlibScript = (
1824
1979
  [":print-success", settings.isPrintingSuccess],
1825
1980
  [":produce-models", settings.isProducingModels],
1826
1981
  [":produce-unsat-cores", settings.isProducingUnsatCores],
1982
+ [":produce-unsat-assumptions", settings.isProducingUnsatAssumptions],
1827
1983
  [":interactive-mode", settings.isInteractive],
1828
1984
  [":produce-assertions", settings.isInteractive],
1829
1985
  [":int-real-coercions", true],
@@ -1843,26 +1999,12 @@ export const runSmtlibScript = (
1843
1999
  "get-info",
1844
2000
  (list) => {
1845
2001
  const info = list.items[1];
1846
- if (info?.kind !== "keyword")
1847
- throw new SmtParseError("invalid command argument, keyword expected", info ?? list.end);
1848
- const check = state.check;
1849
- const reasonUnknown =
1850
- check === undefined
1851
- ? "state of the most recent check-sat command is not known"
1852
- : check.reasonUnknown;
1853
- if (info.text === ":reason-unknown" && reasonUnknown !== undefined) {
1854
- print(`(:reason-unknown "${reasonUnknown.replaceAll('"', '\\"')}")`);
1855
- return;
1856
- }
1857
- if (info.text === ":rlimit") {
1858
- print(`(:rlimit ${state.manager.limit.count})`);
1859
- return;
1860
- }
1861
- if (info.text !== ":error-behavior")
1862
- throw new SmtUnsupportedError(`the information ${info.text}`);
1863
- print(
1864
- `(:error-behavior ${settings.isExitingOnError ? "immediate-exit" : "continued-execution"})`,
1865
- );
2002
+ if (info === undefined)
2003
+ throw new SmtParseError("invalid command, argument(s) missing", list.end);
2004
+ if (info.kind !== "keyword")
2005
+ throw new SmtParseError("invalid command argument, keyword expected", info);
2006
+ printInfo(list, info.text);
2007
+ checkArgumentCount(list, 1);
1866
2008
  },
1867
2009
  ],
1868
2010
  [
@@ -1937,12 +2079,7 @@ export const runSmtlibScript = (
1937
2079
  }
1938
2080
  // Z3's scoped_timer ends with the command that set it, so a command starts without one.
1939
2081
  state.manager.limit.restart(Infinity);
1940
- if (!MANAGERLESS_COMMANDS.has(head.text) && state.isProofsEnabled === undefined) {
1941
- state.isProofsEnabled = settings.parameters.get("proof") === "true";
1942
- if (state.logic === undefined || logicHasSeq(state.logic)) state.manager.initSeqPlugin();
1943
- if (state.logic === undefined) state.reader.registerList();
1944
- resetKernel();
1945
- }
2082
+ if (!MANAGERLESS_COMMANDS.has(head.text)) initManager();
1946
2083
  try {
1947
2084
  command(list);
1948
2085
  } catch (error) {
@@ -36,7 +36,7 @@ import { getSubterms } from "./recfun-decl-plugin";
36
36
  import { RefVector } from "./ref-vector";
37
37
  import { Rewriter, type RewriterOptions } from "./rewriter";
38
38
  import { RewriterTpl } from "./rewriter-tpl";
39
- import { finishSimplifier } from "./dependent-expr-state";
39
+ import { DependentExprState, finishSimplifier } from "./dependent-expr-state";
40
40
 
41
41
  interface SolveEqsOptions {
42
42
  isTheorySolver: boolean;
@@ -108,6 +108,7 @@ export class SolveEqs {
108
108
  private orPositive = new Set<number>();
109
109
  private orNegative = new Set<number>();
110
110
  private rewriter: Rewriter;
111
+ private state: DependentExprState;
111
112
 
112
113
  constructor(
113
114
  private manager: AstManager,
@@ -119,6 +120,7 @@ export class SolveEqs {
119
120
  this.bounds = new BoundManager(manager);
120
121
  this.substitution = new ObjMap((expr) => manager.hashes[expr]);
121
122
  this.rewriter = new Rewriter(manager, options.rewriter);
123
+ this.state = new DependentExprState(manager, goal);
122
124
  }
123
125
 
124
126
  private pin(expr: number) {
@@ -516,12 +518,15 @@ export class SolveEqs {
516
518
 
517
519
  private getEqs() {
518
520
  const eqs: DependentEq[] = [];
519
- const forms = this.goal.forms.items;
520
- if (!this.goal.isProofsEnabled) for (const form of forms) this.getArithEqs(form, eqs);
521
- for (const form of forms) this.getBasicEqs(form, eqs);
521
+ const state = this.state;
522
+ if (!this.goal.isProofsEnabled)
523
+ state.forEachIndex((index) => this.getArithEqs(state.get(index), eqs));
524
+ state.forEachIndex((index) => this.getBasicEqs(state.get(index), eqs));
522
525
  if (!this.goal.isProofsEnabled) {
523
- for (const form of forms) this.getBvEqs(form, eqs);
524
- for (const form of forms) this.checkArrayEqs(form);
526
+ state.forEachIndex((index) => this.getBvEqs(state.get(index), eqs));
527
+ state.forEachIndex((index) => this.checkArrayEqs(state.get(index)));
528
+ // ff_extract_eq finds no equations without finite fields, but its pass still counts steps.
529
+ state.forEachIndex(() => {});
525
530
  }
526
531
  return eqs;
527
532
  }
@@ -593,7 +598,7 @@ export class SolveEqs {
593
598
  const replacer = new SubstReplacer(manager, this.substitution);
594
599
  this.substIds.sort((first, second) => this.id2level[second] - this.id2level[first]);
595
600
  for (const id of this.substIds) {
596
- manager.limit.checkpoint();
601
+ if (!manager.limit.inc()) break;
597
602
  const eq = this.next[id][0];
598
603
  const definition = replacer.rewrite(eq.term);
599
604
  this.pinned.pushOwned(definition);
@@ -605,19 +610,19 @@ export class SolveEqs {
605
610
  }
606
611
 
607
612
  private applySubst() {
608
- if (this.substIds.length === 0) return;
609
613
  const manager = this.manager;
614
+ if (!manager.limit.inc() || this.substIds.length === 0) return;
610
615
  const replacer = new SubstReplacer(manager, this.substitution);
611
616
  const goal = this.goal;
612
- const size = goal.forms.size;
613
- for (let index = 0; index < size && !goal.isInconsistent; index++) {
614
- const form = goal.forms.items[index];
617
+ const state = this.state;
618
+ state.forEachIndex((index) => {
619
+ const form = state.get(index);
615
620
  const replaced = replacer.rewrite(form);
616
621
  const simplified = this.rewriter.rewrite(replaced);
617
622
  manager.decRef(replaced);
618
623
  if (simplified !== form) goal.update(index, simplified);
619
624
  manager.decRef(simplified);
620
- }
625
+ });
621
626
  replacer.release();
622
627
  }
623
628
 
@@ -655,12 +660,13 @@ export class SolveEqs {
655
660
  this.markPositive(form);
656
661
  this.bounds.add(form);
657
662
  }
658
- for (let count = 0; count < 20; count++) {
663
+ const limit = this.manager.limit;
664
+ let count = 0;
665
+ do {
659
666
  this.runRound(() => this.getEqs());
660
- if (this.substIds.length === 0) break;
661
- this.manager.limit.checkpoint();
662
- }
663
- if (this.options.isContextSolve && !goal.isProofsEnabled)
667
+ count++;
668
+ } while (this.substIds.length > 0 && count < 20 && limit.inc());
669
+ if (limit.inc() && this.options.isContextSolve && !goal.isProofsEnabled)
664
670
  this.runRound(() => this.collectNestedEqualities());
665
671
  this.eqTerms.reset();
666
672
  finishSimplifier(goal, this.definitions);
@@ -914,6 +920,7 @@ export class SolveEqs {
914
920
  }
915
921
 
916
922
  release() {
923
+ this.state.release();
917
924
  this.rewriter.release();
918
925
  this.pinned.reset();
919
926
  this.eqTerms.reset();