/
smt_model_finder.cpp
3425 lines (3015 loc) · 135 KB
/
smt_model_finder.cpp
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) 2006 Microsoft Corporation
Module Name:
smt_model_finder.cpp
Abstract:
Model finding goodies for universally quantified formulas.
Author:
Leonardo de Moura (leonardo) 2010-12-17.
Revision History:
--*/
#include "util/backtrackable_set.h"
#include "ast/ast_util.h"
#include "ast/macros/macro_util.h"
#include "ast/arith_decl_plugin.h"
#include "ast/bv_decl_plugin.h"
#include "ast/array_decl_plugin.h"
#include "ast/normal_forms/pull_quant.h"
#include "ast/rewriter/var_subst.h"
#include "ast/for_each_expr.h"
#include "ast/ast_pp.h"
#include "ast/ast_ll_pp.h"
#include "ast/well_sorted.h"
#include "ast/ast_smt2_pp.h"
#include "model/model_pp.h"
#include "smt/smt_model_finder.h"
#include "smt/smt_context.h"
#include "tactic/tactic_exception.h"
namespace smt {
namespace mf {
// -----------------------------------
//
// Auxiliary stuff
//
// -----------------------------------
// Append the new elements of v2 into v1. v2 should not be used after this operation, since it may suffer destructive updates.
template<typename T>
void dappend(ptr_vector<T> & v1, ptr_vector<T> & v2) {
if (v2.empty())
return;
if (v1.empty()) {
v1.swap(v2);
return;
}
for (T* t : v2) {
if (!v1.contains(t))
v1.push_back(t);
}
v2.finalize();
}
class evaluator {
public:
virtual expr * eval(expr * n, bool model_completion) = 0;
};
// -----------------------------------
//
// Instantiation sets
//
// -----------------------------------
/**
\brief Instantiation sets are the S_{k,j} sets in the Complete quantifier instantiation paper.
*/
class instantiation_set {
ast_manager & m;
obj_map<expr, unsigned> m_elems; // and the associated generation
obj_map<expr, expr *> m_inv;
expr_mark m_visited;
public:
instantiation_set(ast_manager & m):m(m) {}
~instantiation_set() {
for (auto const& kv : m_elems) {
m.dec_ref(kv.m_key);
}
m_elems.reset();
}
obj_map<expr, unsigned> const & get_elems() const { return m_elems; }
void insert(expr * n, unsigned generation) {
if (m_elems.contains(n) || contains_model_value(n))
return;
TRACE("model_finder", tout << mk_pp(n, m) << "\n";);
m.inc_ref(n);
m_elems.insert(n, generation);
SASSERT(!m.is_model_value(n));
}
void remove(expr * n) {
// We can only remove n if it is in m_elems, AND m_inv was not initialized yet.
SASSERT(m_elems.contains(n));
SASSERT(m_inv.empty());
m_elems.erase(n);
m.dec_ref(n);
}
void display(std::ostream & out) const {
for (auto const& kv : m_elems) {
out << mk_bounded_pp(kv.m_key, m) << " [" << kv.m_value << "]\n";
}
out << "inverse:\n";
for (auto const& kv : m_inv) {
out << mk_bounded_pp(kv.m_key, m) << " -> " << mk_bounded_pp(kv.m_value, m) << "\n";
}
}
expr * get_inv(expr * v) const {
expr * t = nullptr;
m_inv.find(v, t);
return t;
}
unsigned get_generation(expr * t) const {
unsigned gen = 0;
m_elems.find(t, gen);
return gen;
}
void mk_inverse(evaluator & ev) {
for (auto const& kv : m_elems) {
expr * t = kv.m_key;
SASSERT(!contains_model_value(t));
unsigned gen = kv.m_value;
expr * t_val = ev.eval(t, true);
if (!t_val) break;
TRACE("model_finder", tout << mk_pp(t, m) << " " << mk_pp(t_val, m) << "\n";);
expr * old_t = nullptr;
if (m_inv.find(t_val, old_t)) {
unsigned old_t_gen = 0;
SASSERT(m_elems.contains(old_t));
m_elems.find(old_t, old_t_gen);
if (gen < old_t_gen) {
m_inv.insert(t_val, t);
}
}
else {
m_inv.insert(t_val, t);
}
}
}
obj_map<expr, expr *> const & get_inv_map() const {
return m_inv;
}
struct is_model_value {};
void operator()(expr *n) {
if (m.is_model_value(n)) {
throw is_model_value();
}
}
bool contains_model_value(expr* n) {
if (m.is_model_value(n)) {
return true;
}
if (is_app(n) && to_app(n)->get_num_args() == 0) {
return false;
}
m_visited.reset();
try {
for_each_expr(*this, m_visited, n);
}
catch (const is_model_value &) {
return true;
}
return false;
}
};
/**
During model construction time,
we solve several constraints that impose restrictions
on how the model for the ground formulas may be extended to
a model to the relevant universal quantifiers.
The class node and its subclasses are used to solve
these constraints.
*/
// -----------------------------------
//
// nodes
//
// -----------------------------------
/**
\brief Base class used to solve model construction constraints.
*/
class node {
unsigned m_id;
node * m_find;
unsigned m_eqc_size;
sort * m_sort; // sort of the elements in the instantiation set.
bool m_mono_proj; // relevant for integers & reals & bit-vectors
bool m_signed_proj; // relevant for bit-vectors.
ptr_vector<node> m_avoid_set;
ptr_vector<expr> m_exceptions;
instantiation_set * m_set;
expr * m_else;
func_decl * m_proj;
public:
node(unsigned id, sort * s):
m_id(id),
m_find(nullptr),
m_eqc_size(1),
m_sort(s),
m_mono_proj(false),
m_signed_proj(false),
m_set(nullptr),
m_else(nullptr),
m_proj(nullptr) {
}
~node() {
if (m_set)
dealloc(m_set);
}
unsigned get_id() const { return m_id; }
sort * get_sort() const { return m_sort; }
bool is_root() const { return m_find == nullptr; }
node * get_root() const {
node * curr = const_cast<node*>(this);
while (!curr->is_root()) {
curr = curr->m_find;
}
SASSERT(curr->is_root());
return curr;
}
void merge(node * other) {
node * r1 = get_root();
node * r2 = other->get_root();
SASSERT(r1->m_set == 0);
SASSERT(r2->m_set == 0);
SASSERT(r1->get_sort() == r2->get_sort());
if (r1 == r2)
return;
if (r1->m_eqc_size > r2->m_eqc_size)
std::swap(r1, r2);
r1->m_find = r2;
r2->m_eqc_size += r1->m_eqc_size;
if (r1->m_mono_proj)
r2->m_mono_proj = true;
if (r1->m_signed_proj)
r2->m_signed_proj = true;
dappend(r2->m_avoid_set, r1->m_avoid_set);
dappend(r2->m_exceptions, r1->m_exceptions);
}
void insert_avoid(node * n) {
ptr_vector<node> & as = get_root()->m_avoid_set;
if (!as.contains(n))
as.push_back(n);
}
void insert_exception(expr * n) {
ptr_vector<expr> & ex = get_root()->m_exceptions;
if (!ex.contains(n))
ex.push_back(n);
}
void set_mono_proj() {
get_root()->m_mono_proj = true;
}
bool is_mono_proj() const {
return get_root()->m_mono_proj;
}
void set_signed_proj() {
get_root()->m_signed_proj = true;
}
bool is_signed_proj() const {
return get_root()->m_signed_proj;
}
void mk_instantiation_set(ast_manager & m) {
SASSERT(is_root());
SASSERT(!m_set);
m_set = alloc(instantiation_set, m);
}
void insert(expr * n, unsigned generation) {
SASSERT(is_ground(n));
get_root()->m_set->insert(n, generation);
}
void display(std::ostream & out, ast_manager & m) const {
if (is_root()) {
out << "root node ------\n";
out << "@" << m_id << " mono: " << m_mono_proj << " signed: " << m_signed_proj << ", sort: " << mk_pp(m_sort, m) << "\n";
out << "avoid-set: ";
for (node* n : m_avoid_set) {
out << "@" << n->get_root()->get_id() << " ";
}
out << "\n";
out << "exceptions: ";
for (expr * e : m_exceptions) {
out << mk_bounded_pp(e, m) << " ";
}
out << "\n";
if (m_else)
out << "else: " << mk_pp(m_else, m, 6) << "\n";
if (m_proj)
out << "projection: " << m_proj->get_name() << "\n";
if (m_set) {
out << "instantiation-set:\n";
m_set->display(out);
}
out << "----------------\n";
}
else {
out << "@" << m_id << " -> @" << get_root()->get_id() << "\n";
}
}
instantiation_set const * get_instantiation_set() const { return get_root()->m_set; }
instantiation_set * get_instantiation_set() { return get_root()->m_set; }
ptr_vector<expr> const & get_exceptions() const { return get_root()->m_exceptions; }
ptr_vector<node> const & get_avoid_set() const { return get_root()->m_avoid_set; }
// return true if m_avoid_set.contains(this)
bool must_avoid_itself() const {
node * r = get_root();
for (node* n : m_avoid_set) {
if (r == n->get_root())
return true;
}
return false;
}
void set_else(expr * e) {
SASSERT(!is_mono_proj());
SASSERT(get_root()->m_else == 0);
get_root()->m_else = e;
}
expr * get_else() const {
return get_root()->m_else;
}
void set_proj(func_decl * f) {
SASSERT(get_root()->m_proj == 0);
get_root()->m_proj = f;
}
func_decl * get_proj() const {
return get_root()->m_proj;
}
};
typedef std::pair<ast *, unsigned> ast_idx_pair;
typedef pair_hash<obj_ptr_hash<ast>, unsigned_hash> ast_idx_pair_hash;
typedef map<ast_idx_pair, node *, ast_idx_pair_hash, default_eq<ast_idx_pair> > key2node;
/**
\brief Auxiliary class for processing the "Almost uninterpreted fragment" described in the paper:
Complete instantiation for quantified SMT formulas
The idea is to create node objects based on the information produced by the quantifier_analyzer.
*/
class auf_solver : public evaluator {
ast_manager & m;
arith_util m_arith;
bv_util m_bv;
array_util m_array;
ptr_vector<node> m_nodes;
unsigned m_next_node_id;
key2node m_uvars;
key2node m_A_f_is;
context * m_context;
// Mapping from sort to auxiliary constant.
// This auxiliary constant is used as a "witness" that is asserted as different from a
// finite number of terms.
// It is only safe to use this constant for infinite sorts.
obj_map<sort, app *> m_sort2k;
expr_ref_vector m_ks; // range of m_sort2k
// Support for evaluating expressions in the current model.
proto_model * m_model;
obj_map<expr, expr *> m_eval_cache[2];
expr_ref_vector m_eval_cache_range;
ptr_vector<node> m_root_nodes;
expr_ref_vector * m_new_constraints;
void reset_sort2k() {
m_sort2k.reset();
m_ks.reset();
}
void reset_eval_cache() {
m_eval_cache[0].reset();
m_eval_cache[1].reset();
m_eval_cache_range.reset();
}
node * mk_node(key2node & map, ast * n, unsigned i, sort * s) {
node * r = nullptr;
ast_idx_pair k(n, i);
if (map.find(k, r)) {
SASSERT(r->get_sort() == s);
return r;
}
r = alloc(node, m_next_node_id, s);
m_next_node_id++;
map.insert(k, r);
m_nodes.push_back(r);
return r;
}
void display_key2node(std::ostream & out, key2node const & m) const {
for (auto const& kv : m) {
ast * a = kv.m_key.first;
unsigned i = kv.m_key.second;
node * n = kv.m_value;
out << "#" << a->get_id() << ":" << i << " -> @" << n->get_id() << "\n";
}
}
void display_A_f_is(std::ostream & out) const {
for (auto const& kv : m_A_f_is) {
func_decl * f = static_cast<func_decl*>(kv.m_key.first);
unsigned i = kv.m_key.second;
node * n = kv.m_value;
out << f->get_name() << ":" << i << " -> @" << n->get_id() << "\n";
}
}
void flush_nodes() {
std::for_each(m_nodes.begin(), m_nodes.end(), delete_proc<node>());
}
public:
auf_solver(ast_manager & m):
m(m),
m_arith(m),
m_bv(m),
m_array(m),
m_next_node_id(0),
m_context(nullptr),
m_ks(m),
m_model(nullptr),
m_eval_cache_range(m),
m_new_constraints(nullptr) {
}
virtual ~auf_solver() {
flush_nodes();
reset_eval_cache();
}
void set_context(context * ctx) {
SASSERT(m_context==0);
m_context = ctx;
}
ast_manager & get_manager() const { return m; }
void reset() {
flush_nodes();
m_nodes.reset();
m_next_node_id = 0;
m_uvars.reset();
m_A_f_is.reset();
m_root_nodes.reset();
reset_sort2k();
}
void set_model(proto_model * m) {
reset_eval_cache();
m_model = m;
}
proto_model * get_model() const {
SASSERT(m_model);
return m_model;
}
node * get_uvar(quantifier * q, unsigned i) {
SASSERT(i < q->get_num_decls());
sort * s = q->get_decl_sort(q->get_num_decls() - i - 1);
return mk_node(m_uvars, q, i, s);
}
node * get_A_f_i(func_decl * f, unsigned i) {
SASSERT(i < f->get_arity());
sort * s = f->get_domain(i);
return mk_node(m_A_f_is, f, i, s);
}
instantiation_set const * get_uvar_inst_set(quantifier * q, unsigned i) const {
//SASSERT(!has_quantifiers(q->get_expr()));
ast_idx_pair k(q, i);
node * r = nullptr;
if (m_uvars.find(k, r))
return r->get_instantiation_set();
return nullptr;
}
void mk_instantiation_sets() {
for (node* curr : m_nodes) {
if (curr->is_root()) {
curr->mk_instantiation_set(m);
}
}
}
// For each instantiation_set, remove entries that do not evaluate to values.
void cleanup_instantiation_sets() {
ptr_vector<expr> to_delete;
for (node * curr : m_nodes) {
if (curr->is_root()) {
instantiation_set * s = curr->get_instantiation_set();
to_delete.reset();
obj_map<expr, unsigned> const & elems = s->get_elems();
for (auto const& kv : elems) {
expr * n = kv.m_key;
expr * n_val = eval(n, true);
if (!n_val || !m.is_value(n_val))
to_delete.push_back(n);
}
for (expr* e : to_delete) {
s->remove(e);
}
}
}
}
void display_nodes(std::ostream & out) const {
display_key2node(out, m_uvars);
display_A_f_is(out);
for (node* n : m_nodes) {
n->display(out, m);
}
}
expr * eval(expr * n, bool model_completion) override {
expr * r = nullptr;
if (m_eval_cache[model_completion].find(n, r)) {
return r;
}
expr_ref tmp(m);
if (!m_model->eval(n, tmp, model_completion)) {
r = nullptr;
TRACE("model_finder", tout << "eval\n" << mk_pp(n, m) << "\n-----> null\n";);
}
else {
r = tmp;
TRACE("model_finder", tout << "eval\n" << mk_pp(n, m) << "\n----->\n" << mk_pp(r, m) << "\n";);
}
m_eval_cache[model_completion].insert(n, r);
m_eval_cache_range.push_back(r);
return r;
}
private:
/**
\brief Collect the interpretations of n->get_exceptions()
and the interpretations of the m_else of nodes in n->get_avoid_set()
*/
void collect_exceptions_values(node * n, ptr_buffer<expr> & r) {
ptr_vector<expr> const & exceptions = n->get_exceptions();
ptr_vector<node> const & avoid_set = n->get_avoid_set();
for (expr* e : exceptions) {
expr * val = eval(e, true);
SASSERT(val != nullptr);
r.push_back(val);
}
for (node* a : avoid_set) {
node * n = a->get_root();
if (!n->is_mono_proj() && n->get_else() != nullptr) {
expr * val = eval(n->get_else(), true);
SASSERT(val != nullptr);
r.push_back(val);
}
}
}
/**
\brief Return an expr t from the instantiation set of \c n s.t. forall e in n.get_exceptions()
eval(t) != eval(e) and forall m in n.get_avoid_set() eval(t) != eval(m.get_else())
If there t1 and t2 satisfying this condition, break ties using the generation of them.
Return 0 if such t does not exist.
*/
expr * pick_instance_diff_exceptions(node * n, ptr_buffer<expr> const & ex_vals) {
instantiation_set const * s = n->get_instantiation_set();
obj_map<expr, unsigned> const & elems = s->get_elems();
expr * t_result = nullptr;
unsigned gen_result = UINT_MAX;
for (auto const& kv : elems) {
expr * t = kv.m_key;
unsigned gen = kv.m_value;
expr * t_val = eval(t, true);
SASSERT(t_val != nullptr);
bool found = false;
for (expr* v : ex_vals) {
if (!m.are_distinct(t_val, v)) {
found = true;
break;
}
}
if (!found && (t_result == nullptr || gen < gen_result)) {
t_result = t;
gen_result = gen;
}
}
return t_result;
}
// we should not assume that uninterpreted sorts are infinite in benchmarks with quantifiers.
bool is_infinite(sort * s) const { return !m.is_uninterp(s) && s->is_infinite(); }
/**
\brief Return a fresh constant k that is used as a witness for elements that must be different from
a set of values.
*/
app * get_k_for(sort * s) {
TRACE("model_finder", tout << sort_ref(s, m) << "\n";);
SASSERT(is_infinite(s));
app * r = nullptr;
if (m_sort2k.find(s, r))
return r;
r = m.mk_fresh_const("k", s);
m_model->register_aux_decl(r->get_decl());
m_sort2k.insert(s, r);
m_ks.push_back(r);
return r;
}
/**
\brief Get the interpretation for k in m_model.
If m_model does not provide an interpretation for k, then
create a fresh one.
Remark: this method uses get_fresh_value, so it may fail.
*/
expr * get_k_interp(app * k) {
sort * s = m.get_sort(k);
SASSERT(is_infinite(s));
func_decl * k_decl = k->get_decl();
expr * r = m_model->get_const_interp(k_decl);
if (r != nullptr)
return r;
r = m_model->get_fresh_value(s);
if (r == nullptr)
return nullptr;
m_model->register_decl(k_decl, r);
SASSERT(m_model->get_const_interp(k_decl) == r);
TRACE("model_finder", tout << mk_pp(r, m) << "\n";);
return r;
}
/**
\brief Assert k to be different from the set of exceptions.
It invokes get_k_interp that may fail.
*/
bool assert_k_diseq_exceptions(app * k, ptr_vector<expr> const & exceptions) {
TRACE("assert_k_diseq_exceptions", tout << "assert_k_diseq_exceptions, " << "k: " << mk_pp(k, m) << "\nexceptions:\n";
for (expr * e : exceptions) tout << mk_pp(e, m) << "\n";);
expr * k_interp = get_k_interp(k);
if (k_interp == nullptr)
return false;
for (expr * ex : exceptions) {
expr * ex_val = eval(ex, true);
if (!m.are_distinct(k_interp, ex_val)) {
SASSERT(m_new_constraints);
// This constraint cannot be asserted into m_context during model construction.
// We must save it, and assert it during a restart.
m_new_constraints->push_back(m.mk_not(m.mk_eq(k, ex)));
}
}
return true;
}
void set_projection_else(node * n) {
TRACE("model_finder", n->display(tout, m););
SASSERT(n->is_root());
SASSERT(!n->is_mono_proj());
instantiation_set const * s = n->get_instantiation_set();
ptr_vector<expr> const & exceptions = n->get_exceptions();
ptr_vector<node> const & avoid_set = n->get_avoid_set();
obj_map<expr, unsigned> const & elems = s->get_elems();
if (elems.empty()) return;
if (!exceptions.empty() || !avoid_set.empty()) {
ptr_buffer<expr> ex_vals;
collect_exceptions_values(n, ex_vals);
expr * e = pick_instance_diff_exceptions(n, ex_vals);
if (e != nullptr) {
n->set_else(e);
return;
}
sort * s = n->get_sort();
TRACE("model_finder", tout << "trying to create k for " << mk_pp(s, m) << ", is_infinite: " << is_infinite(s) << "\n";);
if (is_infinite(s)) {
app * k = get_k_for(s);
if (assert_k_diseq_exceptions(k, exceptions)) {
n->insert(k, 0); // add k to the instantiation set
n->set_else(k);
return;
}
}
// TBD: add support for the else of bitvectors.
// Idea: get the term t with the minimal interpretation and use t - 1.
}
n->set_else((*(elems.begin())).m_key);
}
/**
\brief If m_mono_proj is true and n is int or bv, then for each e in n->get_exceptions(),
we must add e-1 and e+1 to the instantiation set.
If sort->get_sort() is real, then we do nothing and hope for the best.
*/
void add_mono_exceptions(node * n) {
SASSERT(n->is_mono_proj());
sort * s = n->get_sort();
arith_rewriter arw(m);
bv_rewriter brw(m);
ptr_vector<expr> const & exceptions = n->get_exceptions();
expr_ref e_minus_1(m), e_plus_1(m);
if (m_arith.is_int(s)) {
expr_ref one(m_arith.mk_int(1), m);
arith_rewriter arith_rw(m);
for (expr * e : exceptions) {
arith_rw.mk_sub(e, one, e_minus_1);
arith_rw.mk_add(e, one, e_plus_1);
TRACE("mf_simp_bug", tout << "e:\n" << mk_ismt2_pp(e, m) << "\none:\n" << mk_ismt2_pp(one, m) << "\n";);
// Note: exceptions come from quantifiers bodies. So, they have generation 0.
n->insert(e_plus_1, 0);
n->insert(e_minus_1, 0);
}
}
else if (m_bv.is_bv_sort(s)) {
expr_ref one(m_bv.mk_numeral(rational(1), s), m);
bv_rewriter bv_rw(m);
for (expr * e : exceptions) {
bv_rw.mk_add(e, one, e_plus_1);
bv_rw.mk_sub(e, one, e_minus_1);
TRACE("mf_simp_bug", tout << "e:\n" << mk_ismt2_pp(e, m) << "\none:\n" << mk_ismt2_pp(one, m) << "\n";);
// Note: exceptions come from quantifiers bodies. So, they have generation 0.
n->insert(e_plus_1, 0);
n->insert(e_minus_1, 0);
}
}
else {
return;
}
}
void get_instantiation_set_values(node * n, ptr_buffer<expr> & values) {
instantiation_set const * s = n->get_instantiation_set();
obj_hashtable<expr> already_found;
obj_map<expr, unsigned> const & elems = s->get_elems();
for (auto const& kv : elems) {
expr * t = kv.m_key;
expr * t_val = eval(t, true);
if (t_val && !already_found.contains(t_val)) {
values.push_back(t_val);
already_found.insert(t_val);
}
}
TRACE("model_finder_bug", tout << "values for the instantiation_set of @" << n->get_id() << "\n";
for (expr * v : values) {
tout << mk_pp(v, m) << "\n";
});
}
template<class T>
struct numeral_lt {
T& m_util;
numeral_lt(T& a): m_util(a) {}
bool operator()(expr* e1, expr* e2) {
rational v1, v2;
if (m_util.is_numeral(e1, v1) && m_util.is_numeral(e2, v2)) {
return v1 < v2;
}
else {
return e1->get_id() < e2->get_id();
}
}
};
struct signed_bv_lt {
bv_util& m_bv;
unsigned m_bv_size;
signed_bv_lt(bv_util& bv, unsigned sz):m_bv(bv), m_bv_size(sz) {}
bool operator()(expr * e1, expr * e2) {
rational v1, v2;
if (m_bv.is_numeral(e1, v1) && m_bv.is_numeral(e2, v2)) {
v1 = m_bv.norm(v1, m_bv_size, true);
v2 = m_bv.norm(v2, m_bv_size, true);
return v1 < v2;
}
else {
return e1->get_id() < e2->get_id();
}
}
};
void sort_values(node * n, ptr_buffer<expr> & values) {
sort * s = n->get_sort();
if (m_arith.is_int(s) || m_arith.is_real(s)) {
std::sort(values.begin(), values.end(), numeral_lt<arith_util>(m_arith));
}
else if (!n->is_signed_proj()) {
std::sort(values.begin(), values.end(), numeral_lt<bv_util>(m_bv));
}
else {
std::sort(values.begin(), values.end(), signed_bv_lt(m_bv, m_bv.get_bv_size(s)));
}
}
void mk_mono_proj(node * n) {
add_mono_exceptions(n);
ptr_buffer<expr> values;
get_instantiation_set_values(n, values);
if (values.empty()) return;
sort_values(n, values);
sort * s = n->get_sort();
bool is_arith = m_arith.is_int(s) || m_arith.is_real(s);
bool is_signed = n->is_signed_proj();
unsigned sz = values.size();
SASSERT(sz > 0);
expr * pi = values[sz - 1];
expr_ref var(m);
var = m.mk_var(0, s);
for (unsigned i = sz - 1; i >= 1; i--) {
expr_ref c(m);
if (is_arith)
c = m_arith.mk_lt(var, values[i]);
else if (!is_signed)
c = m.mk_not(m_bv.mk_ule(values[i], var));
else
c = m.mk_not(m_bv.mk_sle(values[i], var));
pi = m.mk_ite(c, values[i-1], pi);
}
func_interp * rpi = alloc(func_interp, m, 1);
rpi->set_else(pi);
func_decl * p = m.mk_fresh_func_decl(1, &s, s);
TRACE("model_finder", tout << expr_ref(pi, m) << "\n";);
m_model->register_aux_decl(p, rpi);
n->set_proj(p);
}
void mk_simple_proj(node * n) {
TRACE("model_finder", n->display(tout, m););
set_projection_else(n);
ptr_buffer<expr> values;
get_instantiation_set_values(n, values);
sort * s = n->get_sort();
func_decl * p = m.mk_fresh_func_decl(1, &s, s);
func_interp * pi = alloc(func_interp, m, 1);
m_model->register_aux_decl(p, pi);
if (n->get_else()) {
expr * else_val = eval(n->get_else(), true);
pi->set_else(else_val);
}
for (expr * v : values) {
pi->insert_new_entry(&v, v);
}
n->set_proj(p);
}
void mk_projections() {
for (node * n : m_root_nodes) {
SASSERT(n->is_root());
if (n->is_mono_proj())
mk_mono_proj(n);
else
mk_simple_proj(n);
}
}
/**
\brief Store in r the partial functions that have A_f_i nodes.
*/
void collect_partial_funcs(func_decl_set & r) {
for (auto const& kv : m_A_f_is) {
func_decl * f = to_func_decl(kv.m_key.first);
if (!r.contains(f)) {
func_interp * fi = m_model->get_func_interp(f);
if (fi == nullptr) {
fi = alloc(func_interp, m, f->get_arity());
TRACE("model_finder", tout << "register " << f->get_name() << "\n";);
m_model->register_decl(f, fi);
SASSERT(fi->is_partial());
}
if (fi->is_partial()) {
r.insert(f);
}
}
}
}
/**
\brief Make sorts associated with nodes that must avoid themselves finite.
Only uninterpreted sorts are considered.
This is a trick to be able to handle atoms of the form X = Y
where X and Y are variables. See paper "Complete Quantifier Instantiation"
for more details.
*/
void mk_sorts_finite() {
for (node * n : m_root_nodes) {
SASSERT(n->is_root());
sort * s = n->get_sort();
if (m.is_uninterp(s) &&
// Making all uninterpreted sorts finite.
// n->must_avoid_itself() &&
!m_model->is_finite(s)) {
m_model->freeze_universe(s);
}
}
}
void add_elem_to_empty_inst_sets() {
obj_map<sort, expr*> sort2elems;
ptr_vector<node> need_fresh;
for (node * n : m_root_nodes) {
SASSERT(n->is_root());
instantiation_set const * s = n->get_instantiation_set();
TRACE("model_finder", s->display(tout););
obj_map<expr, unsigned> const & elems = s->get_elems();
if (elems.empty()) {
// The method get_some_value cannot be used if n->get_sort() is an uninterpreted sort or is a sort built using uninterpreted sorts
// (e.g., (Array S S) where S is uninterpreted). The problem is that these sorts do not have a fixed interpretation.
// Moreover, a model assigns an arbitrary interpretation to these sorts using "model_values" a model value.
// If these module values "leak" inside the logical context, they may affect satisfiability.
//
sort * ns = n->get_sort();
if (m.is_fully_interp(ns)) {
n->insert(m_model->get_some_value(ns), 0);
}
else {
need_fresh.push_back(n);
}
}
else {
sort2elems.insert(n->get_sort(), elems.begin()->m_key);
}
}
expr_ref_vector trail(m);
for (node * n : need_fresh) {
expr * e;
sort* s = n->get_sort();
if (!sort2elems.find(s, e)) {
e = m.mk_fresh_const("elem", s);
trail.push_back(e);
sort2elems.insert(s, e);
}
n->insert(e, 0);
TRACE("model_finder", tout << "fresh constant: " << mk_pp(e, m) << "\n";);
}
}
/**
\brief Store in m_root_nodes the roots from m_nodes.
*/
void collect_root_nodes() {
m_root_nodes.reset();
for (node * n : m_nodes) {
if (n->is_root())
m_root_nodes.push_back(n);