-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMainSolver.cc
More file actions
750 lines (635 loc) · 26.2 KB
/
MainSolver.cc
File metadata and controls
750 lines (635 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
* Copyright (c) 2012 - 2022, Antti Hyvarinen <antti.hyvarinen@gmail.com>
* Copyright (c) 2022 - 2024, Martin Blicha <martin.blicha@gmail.com>
*
* SPDX-License-Identifier: MIT
*
*/
#include "MainSolver.h"
#include <common/ApiException.h>
#include <itehandler/IteHandler.h>
#include <logics/ArrayTheory.h>
#include <logics/LATheory.h>
#include <logics/UFLATheory.h>
#include <models/ModelBuilder.h>
#include <rewriters/Substitutor.h>
#include <simplifiers/BoolRewriting.h>
#include <smtsolvers/GhostSMTSolver.h>
#include <smtsolvers/LookaheadSMTSolver.h>
#include <tsolvers/IDLTHandler.h>
#include <tsolvers/LATHandler.h>
#include <tsolvers/RDLTHandler.h>
#include <unsatcores/UnsatCoreBuilder.h>
namespace opensmt {
MainSolver::MainSolver(Logic & logic, SMTConfig & conf, std::string name)
: theory(createTheory(logic, conf)),
term_mapper(new TermMapper(logic)),
thandler(new THandler(getTheory(), *term_mapper)),
smt_solver(createInnerSolver(conf, *thandler)),
termNames(conf),
logic(thandler->getLogic()),
pmanager(logic),
config(conf),
ts(logic, *term_mapper),
solver_name{std::move(name)} {
conf.setUsedForInitiliazation();
initialize();
}
MainSolver::MainSolver(std::unique_ptr<Theory> th, std::unique_ptr<TermMapper> tm, std::unique_ptr<THandler> thd,
std::unique_ptr<SimpSMTSolver> ss, Logic & logic, SMTConfig & conf, std::string name)
: theory(std::move(th)),
term_mapper(std::move(tm)),
thandler(std::move(thd)),
smt_solver(std::move(ss)),
termNames(conf),
logic(thandler->getLogic()),
pmanager(logic),
config(conf),
ts(logic, *term_mapper),
solver_name{std::move(name)} {
conf.setUsedForInitiliazation();
initialize();
}
void MainSolver::initialize() {
frames.push();
frameTerms.push(logic.getTerm_true());
preprocessor.initialize();
smt_solver->initialize();
pair<CRef, CRef> iorefs{CRef_Undef, CRef_Undef};
smt_solver->addOriginalSMTClause({term_mapper->getOrCreateLit(logic.getTerm_true())}, iorefs);
if (iorefs.first != CRef_Undef) { pmanager.addClauseClassMask(iorefs.first, 1); }
smt_solver->addOriginalSMTClause({~term_mapper->getOrCreateLit(logic.getTerm_false())}, iorefs);
if (iorefs.first != CRef_Undef) { pmanager.addClauseClassMask(iorefs.first, 1); }
}
void MainSolver::push() {
bool alreadyUnsat = isLastFrameUnsat();
frames.push();
preprocessor.push();
decisionPreferences.pushScope();
frameTerms.push(newFrameTerm(frames.last().getId()));
termNames.pushScope();
if (alreadyUnsat) { rememberLastFrameUnsat(); }
}
bool MainSolver::pop() {
if (getAssertionLevel() == 0) { return false; }
if (trackPartitions()) {
ipartitions_t mask = 0;
for (PTRef partition : getAssertionsAtCurrentLevel()) {
auto index = pmanager.getPartitionIndex(partition);
assert(index != -1);
setbit(mask, static_cast<unsigned int>(index));
}
pmanager.invalidatePartitions(mask);
}
frames.pop();
preprocessor.pop();
assert(decisionPreferences.size() >= smt_solver->userBranchLitsSize());
if (decisionPreferences.size() == smt_solver->userBranchLitsSize()) {
decisionPreferences.popScope([this](PTRef /*pref*/) { smt_solver->popUserBranchLit(); });
} else {
decisionPreferences.popScope();
}
assert(decisionPreferences.size() >= smt_solver->userBranchLitsSize());
termNames.popScope();
// goes back to frames.frameCount()-1 only if a formula is added via insertFormula
firstNotSimplifiedFrame = std::min(firstNotSimplifiedFrame, frames.frameCount());
if (not isLastFrameUnsat()) { getSMTSolver().restoreOK(); }
return true;
}
std::size_t MainSolver::getAssertionLevel() const {
assert(frames.frameCount() >= 1);
return frames.frameCount() - 1;
}
void MainSolver::insertFormula(PTRef fla) {
if (logic.getSortRef(fla) != logic.getSort_bool()) {
throw ApiException("Top-level assertion sort must be Bool, got " + logic.sortToString(logic.getSortRef(fla)));
}
// TODO: Move this to preprocessing of the formulas
fla = IteHandler(logic, getPartitionManager().getNofPartitions()).rewrite(fla);
if (trackPartitions()) {
// MB: Important for HiFrog! partition index is the index of the formula in an virtual array of inserted
// formulas,
// thus we need the old value of count. TODO: Find a good interface for this so it cannot be broken this
// easily
unsigned int partition_index = insertedFormulasCount++;
pmanager.assignTopLevelPartitionIndex(partition_index, fla);
assert(pmanager.getPartitionIndex(fla) != -1);
} else {
++insertedFormulasCount;
}
frames.add(fla);
firstNotSimplifiedFrame = std::min(firstNotSimplifiedFrame, frames.frameCount() - 1);
}
bool MainSolver::tryAddNamedAssertion(PTRef fla, std::string const & name) {
bool const success = tryAddTermNameFor(fla, name);
if (not success) { return false; }
addAssertion(fla);
return true;
}
bool MainSolver::tryAddTermNameFor(PTRef fla, std::string const & name) {
return termNames.tryInsert(name, fla);
}
void MainSolver::addDecisionPreference(PTRef fla) {
if (logic.getSortRef(fla) != logic.getSort_bool()) {
throw ApiException("Decision preference sort must be Bool, got " + logic.sortToString(logic.getSortRef(fla)));
}
if (logic.isConstant(fla)) { return; }
decisionPreferences.push(fla);
}
void MainSolver::resetDecisionPreferences() {
decisionPreferences.clear();
smt_solver->clearUserBranchLits();
}
sstat MainSolver::simplifyFormulas() {
status = s_Undef;
for (std::size_t i = firstNotSimplifiedFrame; i < frames.frameCount(); ++i) {
auto & frame = frames[i];
FrameId const frameId = frame.getId();
PreprocessingContext context{.frameCount = i, .perPartition = trackPartitions()};
preprocessor.prepareForProcessingFrame(i);
firstNotSimplifiedFrame = i + 1;
if (not context.perPartition) {
PTRef frameFormula = preprocessFormulasDefault(frame.formulas, context);
status = giveToSolver(frameFormula, frameId);
} else {
vec<PTRef> processedFormulas = preprocessFormulasPerPartition(frame.formulas, context);
for (PTRef fla : processedFormulas) {
status = giveToSolver(fla, frameId);
if (status == s_False) { break; }
}
}
if (status == s_False) { break; }
for (PTRef pref : decisionPreferences.scope(i)) {
giveDecisionPreferenceToSMTSolver(pref, frameId, context);
}
}
if (status == s_False) {
assert(firstNotSimplifiedFrame > 0);
rememberUnsatFrame(firstNotSimplifiedFrame - 1);
}
return status;
}
PTRef MainSolver::preprocessFormulasDefault(vec<PTRef> const & frameFormulas, PreprocessingContext const & context) {
assert(not context.perPartition);
if (frameFormulas.size() == 0) { return logic.getTerm_true(); }
PTRef frameFormula = logic.mkAnd(frameFormulas);
return preprocessFormula(frameFormula, context);
}
vec<PTRef> MainSolver::preprocessFormulasPerPartition(vec<PTRef> const & frameFormulas,
PreprocessingContext const & context) {
assert(context.perPartition);
if (frameFormulas.size() == 0) { return {}; }
vec<PTRef> processedFormulas;
for (PTRef fla : frameFormulas) {
PTRef processed = preprocessFormulaBeforeFinalTheoryPreprocessing(fla, context);
processedFormulas.push(processed);
}
assert(processedFormulas.size() == frameFormulas.size());
assert(processedFormulas.size() > 0);
if (std::all_of(processedFormulas.begin(), processedFormulas.end(),
[&](PTRef fla) { return fla == logic.getTerm_true(); })) {
return {};
}
preprocessFormulaDoFinalTheoryPreprocessing(context);
for (PTRef & fla : processedFormulas) {
if (fla == logic.getTerm_true()) { continue; }
fla = preprocessFormulaAfterFinalTheoryPreprocessing(fla, context);
}
return processedFormulas;
}
PTRef MainSolver::preprocessFormula(PTRef fla, PreprocessingContext const & context) {
PTRef processed = preprocessFormulaBeforeFinalTheoryPreprocessing(fla, context);
preprocessFormulaDoFinalTheoryPreprocessing(context);
processed = preprocessFormulaAfterFinalTheoryPreprocessing(processed, context);
return processed;
}
PTRef MainSolver::preprocessFormulaBeforeFinalTheoryPreprocessing(PTRef fla, PreprocessingContext const & context) {
bool const perPartition = context.perPartition;
PTRef processed = fla;
if (not perPartition) {
if (context.frameCount > 0) { processed = applyLearntSubstitutions(processed); }
processed = theory->preprocessBeforeSubstitutions(processed, context);
processed = substitutionPass(processed, context);
}
processed = theory->preprocessAfterSubstitutions(processed, context);
if (perPartition) {
pmanager.transferPartitionMembership(fla, processed);
} else if (logic.isFalse(processed)) {
return logic.getTerm_false();
}
preprocessor.addPreprocessedFormula(processed);
return processed;
}
void MainSolver::preprocessFormulaDoFinalTheoryPreprocessing(PreprocessingContext const &) {
theory->afterPreprocessing(preprocessor.getPreprocessedFormulas());
}
PTRef MainSolver::preprocessFormulaAfterFinalTheoryPreprocessing(PTRef fla, PreprocessingContext const & context) {
bool const perPartition = context.perPartition;
PTRef processed = fla;
assert(not perPartition or pmanager.getPartitionIndex(processed) != -1);
// Optimize the dag for cnfization
if (logic.isBooleanOperator(processed)) {
processed = rewriteMaxArity(processed);
if (perPartition) { pmanager.transferPartitionMembership(fla, processed); }
}
if (perPartition) {
assert(pmanager.getPartitionIndex(processed) != -1);
pmanager.propagatePartitionMask(processed);
}
return processed;
}
vec<PTRef> MainSolver::getCurrentAssertions() const {
vec<PTRef> assertions;
size_t const cnt = frames.frameCount();
for (size_t i = 0; i < cnt; ++i) {
for (PTRef fla : frames[i].formulas) {
assertions.push(fla);
}
}
return assertions;
}
vec<PTRef> const & MainSolver::getAssertionsAtLevel(std::size_t level) const {
assert(level <= getAssertionLevel());
return frames[level].formulas;
}
void MainSolver::printCurrentAssertionsAsQuery() const {
char * base_name = config.dump_query_name();
if (base_name == NULL)
printCurrentAssertionsAsQuery(std::cout);
else {
char * s_file_name;
int chars_written = asprintf(&s_file_name, "%s-%d.smt2", base_name, check_called);
(void)chars_written;
std::ofstream stream;
stream.open(s_file_name);
printCurrentAssertionsAsQuery(stream);
stream.close();
free(s_file_name);
}
free(base_name);
}
void MainSolver::printCurrentAssertionsAsQuery(std::ostream & s) const {
logic.dumpHeaderToFile(s);
for (std::size_t i = 0; i < frames.frameCount(); ++i) {
if (i > 0) s << "(push 1)\n";
for (PTRef assertion : frames[i].formulas) {
logic.dumpFormulaToFile(s, assertion);
}
}
logic.dumpChecksatToFile(s);
}
// Replace subtrees consisting only of ands / ors with a single and / or term.
// Search a maximal section of the tree consisting solely of ands / ors. The
// root of this subtree is called and / or root. Collect the subtrees rooted at
// the leaves of this section, and create a new and / or term with the leaves as
// arguments and the parent of the and / or tree as the parent.
//
// However, we will do this in a clever way so that if a certain
// term appears as a child in more than one term, we will not flatten
// that structure.
//
PTRef MainSolver::rewriteMaxArity(PTRef root) {
return opensmt::rewriteMaxArityClassic(logic, root);
}
void MainSolver::giveDecisionPreferenceToSMTSolver(PTRef pref, FrameId frameId, PreprocessingContext const & pcontext) {
assert(logic.getSortRef(pref) == logic.getSort_bool());
assert(not logic.isConstant(pref));
// Ignores substitutions ..
Lit l = [&] {
if (term_mapper->hasLit(pref)) { return giveExistingDecisionPreferenceToSMTSolver(pref); }
if (logic.isBoolVarLiteral(pref)) { return giveBoolVarDecisionPreferenceToSMTSolver(pref); }
return giveAnyDecisionPreferenceToSMTSolver(pref, frameId, pcontext);
}();
decisionPreferenceToLitMap.emplace(pref, l);
}
Lit MainSolver::giveExistingDecisionPreferenceToSMTSolver(PTRef pref) {
assert(term_mapper->hasLit(pref));
++existingDecisionPreferencesGivenToSMTSolverCount;
return term_mapper->getLit(pref);
}
Lit MainSolver::giveBoolVarDecisionPreferenceToSMTSolver(PTRef pref) {
assert(logic.isBoolVarLiteral(pref));
Lit l = term_mapper->getOrCreateLit(pref);
Var v = var(l);
smt_solver->addVar(v);
assert(term_mapper->getLit(pref) == l);
assert(term_mapper->getVar(pref) == var(l));
++boolVarDecisionPreferencesGivenToSMTSolverCount;
return l;
}
Lit MainSolver::giveAnyDecisionPreferenceToSMTSolver(PTRef pref, FrameId frameId,
PreprocessingContext const & pcontext) {
assert(not term_mapper->hasLit(pref));
assert(not logic.isBoolVarLiteral(pref));
// Cannot preprocess pref unconditionally - may result in a conflict
auto name = std::string{".pref"} + std::to_string(pref.x);
PTRef decisionVarTerm = logic.mkBoolVar(name.c_str());
Lit l = term_mapper->getOrCreateLit(decisionVarTerm);
PTRef condTerm = logic.mkImpl(decisionVarTerm, pref);
assert(not pcontext.perPartition);
PTRef processed = preprocessFormula(condTerm, pcontext);
assert(not logic.isConstant(processed));
assert(not logic.isBoolVarLiteral(processed));
[[maybe_unused]]
sstat status = giveToSolver(processed, frameId);
assert(status == s_Undef);
assert(term_mapper->getLit(decisionVarTerm) == l);
assert(term_mapper->getVar(decisionVarTerm) == var(l));
++otherDecisionPreferencesGivenToSMTSolverCount;
return l;
}
std::unique_ptr<Model> MainSolver::getModel() {
if (!config.produce_models()) { throw ApiException("Producing models is not enabled"); }
if (status != s_True) { throw ApiException("Model cannot be created if solver is not in SAT state"); }
ModelBuilder modelBuilder{logic};
smt_solver->fillBooleanVars(modelBuilder);
thandler->fillTheoryFunctions(modelBuilder);
return modelBuilder.build();
}
void MainSolver::printResolutionProofSMT2() const {
printResolutionProofSMT2(std::cout);
}
void MainSolver::printResolutionProofSMT2(std::ostream & os) const {
assert(smt_solver);
if (!smt_solver->logsResolutionProof()) { throw ApiException("Proofs are not tracked"); }
if (status != s_False) { throw ApiException("Proof cannot be created if solver is not in UNSAT state"); }
return smt_solver->printResolutionProofSMT2(os);
}
std::unique_ptr<UnsatCore> MainSolver::getUnsatCore() const {
if (not config.produce_unsat_cores()) { throw ApiException("Producing unsat cores is not enabled"); }
if (status != s_False) { throw ApiException("Unsat core cannot be extracted if solver is not in UNSAT state"); }
UnsatCoreBuilder unsatCoreBuilder{*this};
return unsatCoreBuilder.build();
}
lbool MainSolver::getTermValue(PTRef tr) const {
if (logic.getSortRef(tr) != logic.getSort_bool()) { return l_Undef; }
if (not term_mapper->hasLit(tr)) { return l_Undef; }
Lit l = term_mapper->getLit(tr);
auto val = smt_solver->modelValue(l);
assert(val != l_Undef);
return val;
}
std::unique_ptr<InterpolationContext> MainSolver::getInterpolationContext() {
if (!config.produce_inter()) { throw ApiException("Producing interpolants is not enabled"); }
if (status != s_False) {
throw ApiException("Interpolation context cannot be created if solver is not in UNSAT state");
}
return std::make_unique<InterpolationContext>(config, *theory, *term_mapper, getSMTSolver().getResolutionProof(),
pmanager);
}
sstat MainSolver::giveToSolver(PTRef root, FrameId push_id) {
struct ClauseCallBack : public Cnfizer::ClauseCallBack {
std::vector<vec<Lit>> clauses;
void operator()(vec<Lit> && c) override { clauses.push_back(std::move(c)); }
};
if (root == logic.getTerm_true()) { return s_Undef; }
ClauseCallBack callBack;
ts.setClauseCallBack(&callBack);
ts.Cnfizer::cnfize(root, push_id);
bool const keepPartitionsSeparate = trackPartitions();
Lit frameLit;
if (push_id != 0) { frameLit = term_mapper->getOrCreateLit(frameTerms[push_id]); }
int partitionIndex = keepPartitionsSeparate ? pmanager.getPartitionIndex(root) : -1;
for (auto & clause : callBack.clauses) {
if (push_id != 0) { clause.push(frameLit); }
pair<CRef, CRef> iorefs{CRef_Undef, CRef_Undef};
bool res = smt_solver->addOriginalSMTClause(std::move(clause), iorefs);
if (keepPartitionsSeparate) {
CRef ref = iorefs.first;
if (ref != CRef_Undef) {
ipartitions_t parts = 0;
assert(partitionIndex != -1);
setbit(parts, static_cast<unsigned int>(partitionIndex));
pmanager.addClauseClassMask(ref, parts);
}
}
if (not res) { return s_False; }
}
return s_Undef;
}
sstat MainSolver::check() {
++check_called;
if (config.timeQueries()) {
printf("; %s query time so far: %f\n", solver_name.c_str(), query_timer.getTime());
StopWatch sw(query_timer);
}
if (isLastFrameUnsat()) { return s_False; }
sstat rval = simplifyFormulas();
if (config.dump_query()) printCurrentAssertionsAsQuery();
if (rval == s_Undef) {
try {
rval = solve();
} catch (std::overflow_error const & error) { rval = s_Error; }
if (rval == s_False) {
assert(not smt_solver->isOK());
rememberUnsatFrame(smt_solver->getConflictFrame());
}
}
return rval;
}
sstat MainSolver::solve() {
if (!smt_solver->isOK()) { return s_False; }
smt_solver->clearUserBranchLits();
for (PTRef pref : decisionPreferences) {
//++ probably just use vector
smt_solver->pushUserBranchLit(decisionPreferenceToLitMap[pref]);
}
// FIXME: Find a better way to deal with Bools in UF
for (PTRef tr : logic.propFormulasAppearingInUF) {
Lit l = term_mapper->getOrCreateLit(tr);
smt_solver->addVar(var(l));
}
vec<FrameId> en_frames;
for (std::size_t i = 0; i < frames.frameCount(); ++i) {
en_frames.push(frames[i].getId());
}
status = solve_(en_frames);
if (status == s_True && config.produce_models()) thandler->computeModel();
smt_solver->clearSearch();
return status;
}
sstat MainSolver::solve_(vec<FrameId> const & enabledFrames) {
assert(frameTerms.size() > 0 and frameTerms[0] == getLogic().getTerm_true());
vec<Lit> assumps;
// Initialize so that by default frames are disabled
for (PTRef tr : frameTerms) {
assumps.push(term_mapper->getOrCreateLit(tr));
}
// Enable the terms which are listed in enabledFrames
// At this point assumps has the same size as frame_terms and the
// elements are in the same order. We simply invert the
// corresponding literals
uint32_t prevId = UINT32_MAX;
for (FrameId fid : enabledFrames) {
assumps[fid] = ~assumps[fid];
smt_solver->mapEnabledFrameIdToVar(var(assumps[fid]), fid, prevId);
}
// Drop the assumption variable for the base frame (it is at the first place)
for (int i = 1; i < assumps.size(); ++i) {
assumps[i - 1] = assumps[i];
}
assumps.pop();
return smt_solver->solve(assumps, !config.isIncremental(), config.isIncremental());
}
std::unique_ptr<SimpSMTSolver> MainSolver::createInnerSolver(SMTConfig & config, THandler & thandler) {
if (config.sat_pure_lookahead()) {
return std::make_unique<LookaheadSMTSolver>(config, thandler);
} else if (config.use_ghost_vars()) {
return std::make_unique<GhostSMTSolver>(config, thandler);
} else if (config.sat_picky()) {
return std::make_unique<LookaheadSMTSolver>(config, thandler);
} else {
return std::make_unique<SimpSMTSolver>(config, thandler);
}
}
std::unique_ptr<Theory> MainSolver::createTheory(Logic & logic, SMTConfig & config) {
Logic_t logicType = logic.getLogic();
Theory * theory = nullptr;
switch (logicType) {
case Logic_t::QF_UF:
case Logic_t::QF_BOOL: {
theory = new UFTheory(config, logic);
break;
}
case Logic_t::QF_AX: {
theory = new ArrayTheory(config, logic);
break;
}
case Logic_t::QF_LRA: {
ArithLogic & lraLogic = dynamic_cast<ArithLogic &>(logic);
theory = new LATheory<ArithLogic, LATHandler>(config, lraLogic);
break;
}
case Logic_t::QF_LIA: {
ArithLogic & liaLogic = dynamic_cast<ArithLogic &>(logic);
theory = new LATheory<ArithLogic, LATHandler>(config, liaLogic);
break;
}
case Logic_t::QF_RDL: {
ArithLogic & lraLogic = dynamic_cast<ArithLogic &>(logic);
theory = new LATheory<ArithLogic, RDLTHandler>(config, lraLogic);
break;
}
case Logic_t::QF_IDL: {
ArithLogic & liaLogic = dynamic_cast<ArithLogic &>(logic);
theory = new LATheory<ArithLogic, IDLTHandler>(config, liaLogic);
break;
}
case Logic_t::QF_UFRDL:
case Logic_t::QF_UFIDL:
case Logic_t::QF_UFLRA:
case Logic_t::QF_UFLIA:
case Logic_t::QF_ALRA:
case Logic_t::QF_ALIA:
case Logic_t::QF_AUFLRA:
case Logic_t::QF_AUFLIA:
case Logic_t::QF_AUFLIRA: {
ArithLogic & laLogic = dynamic_cast<ArithLogic &>(logic);
theory = new UFLATheory(config, laLogic);
break;
}
case Logic_t::UNDEF:
throw ApiException{"Error in creating reasoning engine: Engine type not specified"};
break;
default:
assert(false);
throw std::logic_error{"Unreachable code - error in logic selection"};
};
return std::unique_ptr<Theory>(theory);
}
PTRef MainSolver::applyLearntSubstitutions(PTRef fla) {
Logic::SubstMap knownSubst = preprocessor.getCurrentSubstitutions();
PTRef res = Substitutor(getLogic(), knownSubst).rewrite(fla);
return res;
}
PTRef MainSolver::substitutionPass(PTRef fla, PreprocessingContext const & context) {
if (not config.do_substitutions()) { return fla; }
auto res = computeSubstitutions(fla);
vec<PTRef> args;
auto const & entries = res.usedSubstitution.getKeys();
for (auto entry : entries) {
auto target = res.usedSubstitution[entry];
args.push(logic.mkEq(entry, target));
}
args.push(res.result);
PTRef withSubs = logic.mkAnd(std::move(args));
preprocessor.setSubstitutions(context.frameCount, std::move(res.usedSubstitution));
return withSubs;
}
MainSolver::SubstitutionResult MainSolver::computeSubstitutions(PTRef fla) {
SubstitutionResult result;
assert(getConfig().do_substitutions() and not getSMTSolver().logsResolutionProof());
PTRef root = fla;
Logic::SubstMap allsubsts;
while (true) {
// update the current simplification formula
PTRef simp_formula = root;
// l_True : exists and is valid
// l_False : exists but has been disabled to break symmetries
MapWithKeys<PTRef, lbool, PTRefHash> new_units;
vec<PtAsgn> current_units_vec;
bool rval = logic.getNewFacts(simp_formula, new_units);
if (not rval) { return SubstitutionResult{{}, logic.getTerm_false()}; }
// Add the newly obtained units to the list of all substitutions
// Clear the previous units
auto const & new_units_vec = new_units.getKeys();
for (PTRef key : new_units_vec) {
current_units_vec.push(PtAsgn{key, new_units[key]});
}
auto [res, newsubsts] = logic.retrieveSubstitutions(current_units_vec);
logic.substitutionsTransitiveClosure(newsubsts);
// remember the substitutions for models
for (PTRef key : newsubsts.getKeys()) {
if (!allsubsts.has(key)) {
auto const target = newsubsts[key];
allsubsts.insert(key, target);
}
}
if (res != l_Undef) root = (res == l_True ? logic.getTerm_true() : logic.getTerm_false());
PTRef new_root = Substitutor(logic, newsubsts).rewrite(root);
bool cont = new_root != root;
root = new_root;
if (!cont) break;
}
logic.substitutionsTransitiveClosure(allsubsts);
result.result = root;
result.usedSubstitution = std::move(allsubsts);
return result;
}
void MainSolver::Preprocessor::initialize() {
substitutions.push();
}
void MainSolver::Preprocessor::prepareForProcessingFrame(std::size_t frameIndex) {
assert(frameIndex < solverFrameCount);
while (internalFrameCount <= frameIndex) {
pushInternal();
}
}
void MainSolver::Preprocessor::push() {
assert(solverFrameCount >= internalFrameCount);
++solverFrameCount;
}
void MainSolver::Preprocessor::pop() {
assert(solverFrameCount >= internalFrameCount);
--solverFrameCount;
if (solverFrameCount >= internalFrameCount) { return; }
popInternal();
assert(solverFrameCount == internalFrameCount);
}
void MainSolver::Preprocessor::pushInternal() {
++internalFrameCount;
substitutions.push();
preprocessedFormulas.pushScope();
}
void MainSolver::Preprocessor::popInternal() {
--internalFrameCount;
substitutions.pop();
preprocessedFormulas.popScope();
}
void MainSolver::Preprocessor::addPreprocessedFormula(PTRef fla) {
preprocessedFormulas.push(fla);
}
span<PTRef const> MainSolver::Preprocessor::getPreprocessedFormulas() const {
return {preprocessedFormulas.data(), static_cast<uint32_t>(preprocessedFormulas.size())};
}
} // namespace opensmt