-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathast.cpp
3521 lines (3155 loc) · 123 KB
/
ast.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:
ast.cpp
Abstract:
Expression DAG
Author:
Leonardo de Moura (leonardo) 2006-09-28.
Revision History:
--*/
#include <sstream>
#include <cstring>
#include "ast/ast.h"
#include "ast/ast_pp.h"
#include "ast/ast_ll_pp.h"
#include "util/buffer.h"
#include "util/warning.h"
#include "util/string_buffer.h"
#include "ast/ast_util.h"
#include "ast/ast_smt2_pp.h"
#include "ast/array_decl_plugin.h"
#include "ast/arith_decl_plugin.h"
#include "ast/ast_translation.h"
#include "util/z3_version.h"
#include <iostream>
// -----------------------------------
//
// parameter
//
// -----------------------------------
parameter::~parameter() {
if (auto p = std::get_if<rational*>(&m_val)) {
dealloc(*p);
}
if (auto p = std::get_if<zstring*>(&m_val)) {
dealloc(*p);
}
}
parameter::parameter(parameter const& other) : m_val(other.m_val) {
if (auto p = std::get_if<rational*>(&m_val)) {
m_val = alloc(rational, **p);
}
if (auto p = std::get_if<zstring*>(&m_val)) {
m_val = alloc(zstring, **p);
}
}
void parameter::init_eh(ast_manager & m) {
if (is_ast()) {
m.inc_ref(get_ast());
}
}
void parameter::del_eh(ast_manager & m, family_id fid) {
if (is_ast()) {
m.dec_ref(get_ast());
}
else if (is_external()) {
SASSERT(fid != null_family_id);
decl_plugin * plugin = m.get_plugin(fid);
if (plugin) {
plugin->del(*this);
}
}
}
bool parameter::operator==(parameter const & p) const {
if (get_kind() != p.get_kind()) return false;
switch (get_kind()) {
case PARAM_RATIONAL: return get_rational() == p.get_rational();
case PARAM_ZSTRING: return get_zstring() == p.get_zstring();
default: return m_val == p.m_val;
}
}
unsigned parameter::hash() const {
unsigned b = 0;
switch (get_kind()) {
case PARAM_INT: b = get_int(); break;
case PARAM_AST: b = get_ast()->hash(); break;
case PARAM_SYMBOL: b = get_symbol().hash(); break;
case PARAM_RATIONAL: b = get_rational().hash(); break;
case PARAM_DOUBLE: b = static_cast<unsigned>(get_double()); break;
case PARAM_ZSTRING: b = get_zstring().hash(); break;
case PARAM_EXTERNAL: b = get_ext_id(); break;
}
return b;
}
std::ostream& parameter::display(std::ostream& out) const {
switch (get_kind()) {
case PARAM_INT: return out << get_int();
case PARAM_SYMBOL: return out << get_symbol();
case PARAM_RATIONAL: return out << get_rational();
case PARAM_AST: return out << '#' << get_ast()->get_id();
case PARAM_DOUBLE: return out << get_double();
case PARAM_EXTERNAL: return out << '@' << get_ext_id();
case PARAM_ZSTRING: return out << get_zstring();
default:
UNREACHABLE();
return out;
}
}
void display_parameters(std::ostream & out, unsigned n, parameter const * p) {
if (n > 0) {
out << "[";
for (unsigned i = 0; i < n; i ++)
out << p[i] << (i < n-1 ? ":" : "");
out << "]";
}
}
// -----------------------------------
//
// family_manager
//
// -----------------------------------
family_id family_manager::mk_family_id(symbol const & s) {
family_id r;
if (m_families.find(s, r)) {
return r;
}
r = m_next_id;
m_next_id++;
m_families.insert(s, r);
m_names.push_back(s);
return r;
}
family_id family_manager::get_family_id(symbol const & s) const {
family_id r;
if (m_families.find(s, r))
return r;
else
return null_family_id;
}
bool family_manager::has_family(symbol const & s) const {
return m_families.contains(s);
}
// -----------------------------------
//
// decl_info
//
// -----------------------------------
decl_info::decl_info(family_id family_id, decl_kind k, unsigned num_parameters,
parameter const * parameters, bool private_params):
m_family_id(family_id),
m_kind(k),
m_parameters(num_parameters, const_cast<parameter *>(parameters)),
m_private_parameters(private_params) {
}
void decl_info::init_eh(ast_manager & m) {
for (parameter & p : m_parameters) {
p.init_eh(m);
}
}
void decl_info::del_eh(ast_manager & m) {
for (parameter & p : m_parameters) {
p.del_eh(m, m_family_id);
}
}
struct decl_info_child_hash_proc {
unsigned operator()(decl_info const * info, unsigned idx) const { return info->get_parameter(idx).hash(); }
};
unsigned decl_info::hash() const {
unsigned a = m_family_id;
unsigned b = m_kind;
unsigned c = get_num_parameters() == 0 ? 0 : get_composite_hash<decl_info const *, default_kind_hash_proc<decl_info const *>, decl_info_child_hash_proc>(this, get_num_parameters());
mix(a, b, c);
return c;
}
bool decl_info::operator==(decl_info const & info) const {
return m_family_id == info.m_family_id && m_kind == info.m_kind &&
m_parameters == info.m_parameters;
}
std::ostream & operator<<(std::ostream & out, decl_info const & info) {
out << ":fid " << info.get_family_id() << " :decl-kind " << info.get_decl_kind() << " :parameters (";
for (unsigned i = 0; i < info.get_num_parameters(); i++) {
if (i > 0) out << " ";
out << info.get_parameter(i);
}
out << ")";
return out;
}
// -----------------------------------
//
// sort_size
//
// -----------------------------------
std::ostream& operator<<(std::ostream& out, sort_size const & ss) {
if (ss.is_infinite()) { return out << "infinite"; }
else if (ss.is_very_big()) { return out << "very-big"; }
else { SASSERT(ss.is_finite()); return out << ss.size(); }
}
// -----------------------------------
//
// sort_info
//
// -----------------------------------
std::ostream & operator<<(std::ostream & out, sort_info const & info) {
operator<<(out, static_cast<decl_info const&>(info));
return out << " :size " << info.get_num_elements();
}
// -----------------------------------
//
// func_decl_info
//
// -----------------------------------
func_decl_info::func_decl_info(family_id family_id, decl_kind k, unsigned num_parameters, parameter const * parameters):
decl_info(family_id, k, num_parameters, parameters),
m_left_assoc(false),
m_right_assoc(false),
m_flat_associative(false),
m_commutative(false),
m_chainable(false),
m_pairwise(false),
m_injective(false),
m_idempotent(false),
m_skolem(false),
m_lambda(false),
m_polymorphic(false) {
}
bool func_decl_info::operator==(func_decl_info const & info) const {
return decl_info::operator==(info) &&
m_left_assoc == info.m_left_assoc &&
m_right_assoc == info.m_right_assoc &&
m_flat_associative == info.m_flat_associative &&
m_commutative == info.m_commutative &&
m_chainable == info.m_chainable &&
m_pairwise == info.m_pairwise &&
m_injective == info.m_injective &&
m_skolem == info.m_skolem &&
m_lambda == info.m_lambda;
}
std::ostream & operator<<(std::ostream & out, func_decl_info const & info) {
operator<<(out, static_cast<decl_info const&>(info));
if (info.is_left_associative()) out << " :left-assoc ";
if (info.is_right_associative()) out << " :right-assoc ";
if (info.is_flat_associative()) out << " :flat-associative ";
if (info.is_commutative()) out << " :commutative ";
if (info.is_chainable()) out << " :chainable ";
if (info.is_pairwise()) out << " :pairwise ";
if (info.is_injective()) out << " :injective ";
if (info.is_idempotent()) out << " :idempotent ";
if (info.is_skolem()) out << " :skolem ";
if (info.is_lambda()) out << " :lambda ";
if (info.is_polymorphic()) out << " :polymorphic ";
return out;
}
// -----------------------------------
//
// ast
//
// -----------------------------------
static char const * g_ast_kind_names[] = {"application", "variable", "quantifier", "sort", "function declaration" };
char const * get_ast_kind_name(ast_kind k) {
return g_ast_kind_names[k];
}
// -----------------------------------
//
// func_decl
//
// -----------------------------------
func_decl::func_decl(symbol const & name, unsigned arity, sort * const * domain, sort * range, func_decl_info * info):
decl(AST_FUNC_DECL, name, info),
m_arity(arity),
m_range(range) {
if (arity != 0)
memcpy(const_cast<sort **>(get_domain()), domain, sizeof(sort *) * arity);
}
// -----------------------------------
//
// application
//
// -----------------------------------
app::app(func_decl * decl, unsigned num_args, expr * const * args):
expr(AST_APP),
m_decl(decl),
m_num_args(num_args) {
for (unsigned i = 0; i < num_args; i++)
m_args[i] = args[i];
}
// -----------------------------------
//
// quantifier
//
// -----------------------------------
quantifier::quantifier(quantifier_kind k, unsigned num_decls, sort * const * decl_sorts, symbol const * decl_names, expr * body, sort* s,
int weight, symbol const & qid, symbol const & skid, unsigned num_patterns, expr * const * patterns,
unsigned num_no_patterns, expr * const * no_patterns):
expr(AST_QUANTIFIER),
m_kind(k),
m_num_decls(num_decls),
m_expr(body),
m_sort(s),
m_depth(::get_depth(body) + 1),
m_weight(weight),
m_has_unused_vars(true),
m_has_labels(::has_labels(body)),
m_qid(qid),
m_skid(skid),
m_num_patterns(num_patterns),
m_num_no_patterns(num_no_patterns) {
SASSERT(m_num_patterns == 0 || m_num_no_patterns == 0);
memcpy(const_cast<sort **>(get_decl_sorts()), decl_sorts, sizeof(sort *) * num_decls);
memcpy(const_cast<symbol*>(get_decl_names()), decl_names, sizeof(symbol) * num_decls);
if (num_patterns != 0)
memcpy(const_cast<expr **>(get_patterns()), patterns, sizeof(expr *) * num_patterns);
if (num_no_patterns != 0)
memcpy(const_cast<expr **>(get_no_patterns()), no_patterns, sizeof(expr *) * num_no_patterns);
}
quantifier::quantifier(unsigned num_decls, sort * const * decl_sorts, symbol const * decl_names, expr * body, sort* s):
expr(AST_QUANTIFIER),
m_kind(lambda_k),
m_num_decls(num_decls),
m_expr(body),
m_sort(s),
m_depth(::get_depth(body) + 1),
m_weight(1),
m_has_unused_vars(true),
m_has_labels(::has_labels(body)),
m_qid(symbol()),
m_skid(symbol()),
m_num_patterns(0),
m_num_no_patterns(0) {
memcpy(const_cast<sort **>(get_decl_sorts()), decl_sorts, sizeof(sort *) * num_decls);
memcpy(const_cast<symbol*>(get_decl_names()), decl_names, sizeof(symbol) * num_decls);
}
// -----------------------------------
//
// Auxiliary functions
//
// -----------------------------------
sort* expr::get_sort() const {
switch (get_kind()) {
case AST_APP:
return to_app(this)->get_decl()->get_range();
case AST_VAR:
return to_var(this)->_get_sort();
case AST_QUANTIFIER:
return to_quantifier(this)->_get_sort();
default:
UNREACHABLE();
return nullptr;
}
}
// -----------------------------------
//
// AST hash-consing
//
// -----------------------------------
unsigned get_node_size(ast const * n) {
switch(n->get_kind()) {
case AST_SORT: return to_sort(n)->get_size();
case AST_FUNC_DECL: return to_func_decl(n)->get_size();
case AST_APP: return to_app(n)->get_size();
case AST_VAR: return to_var(n)->get_size();
case AST_QUANTIFIER: return to_quantifier(n)->get_size();
default: UNREACHABLE();
}
return 0;
}
bool compare_nodes(ast const * n1, ast const * n2) {
if (n1->get_kind() != n2->get_kind()) {
return false;
}
switch (n1->get_kind()) {
case AST_SORT:
if ((to_sort(n1)->get_info() == nullptr) != (to_sort(n2)->get_info() == nullptr)) {
return false;
}
if (to_sort(n1)->get_info() != nullptr && !(*to_sort(n1)->get_info() == *to_sort(n2)->get_info())) {
return false;
}
return to_sort(n1)->get_name() == to_sort(n2)->get_name();
case AST_FUNC_DECL:
if ((to_func_decl(n1)->get_info() == nullptr) != (to_func_decl(n2)->get_info() == nullptr)) {
return false;
}
if (to_func_decl(n1)->get_info() != nullptr && !(*to_func_decl(n1)->get_info() == *to_func_decl(n2)->get_info())) {
return false;
}
return
to_func_decl(n1)->get_name() == to_func_decl(n2)->get_name() &&
to_func_decl(n1)->get_arity() == to_func_decl(n2)->get_arity() &&
to_func_decl(n1)->get_range() == to_func_decl(n2)->get_range() &&
compare_arrays(to_func_decl(n1)->get_domain(),
to_func_decl(n2)->get_domain(),
to_func_decl(n1)->get_arity());
case AST_APP:
return
to_app(n1)->get_decl() == to_app(n2)->get_decl() &&
to_app(n1)->get_num_args() == to_app(n2)->get_num_args() &&
compare_arrays(to_app(n1)->get_args(), to_app(n2)->get_args(), to_app(n1)->get_num_args());
case AST_VAR:
return
to_var(n1)->get_idx() == to_var(n2)->get_idx() &&
to_var(n1)->get_sort() == to_var(n2)->get_sort();
case AST_QUANTIFIER: {
quantifier const* q1 = to_quantifier(n1);
quantifier const* q2 = to_quantifier(n2);
return
q1->get_kind() == q2->get_kind() &&
q1->get_num_decls() == q2->get_num_decls() &&
q1->get_expr() == q2->get_expr() &&
q1->get_weight() == q2->get_weight() &&
q1->get_num_patterns() == q2->get_num_patterns() &&
compare_arrays(q1->get_decl_sorts(),
q2->get_decl_sorts(),
q1->get_num_decls()) &&
compare_arrays(q1->get_decl_names(),
q2->get_decl_names(),
q1->get_num_decls()) &&
((q1->get_qid().is_numerical() && q2->get_qid().is_numerical()) ||
(q1->get_qid() == q2->get_qid())) &&
compare_arrays(q1->get_patterns(),
q2->get_patterns(),
q1->get_num_patterns()) &&
q1->get_num_no_patterns() == q2->get_num_no_patterns() &&
compare_arrays(q1->get_no_patterns(),
q2->get_no_patterns(),
q1->get_num_no_patterns());
}
default:
UNREACHABLE();
}
return false;
}
template<typename T>
inline unsigned ast_array_hash(T * const * array, unsigned size, unsigned init_value) {
switch (size) {
case 0:
return init_value;
case 1:
return combine_hash(array[0]->hash(), init_value);
case 2:
return combine_hash(combine_hash(array[0]->hash(), array[1]->hash()),
init_value);
case 3:
return combine_hash(combine_hash(array[0]->hash(), array[1]->hash()),
combine_hash(array[2]->hash(), init_value));
default: {
unsigned a, b, c;
a = b = 0x9e3779b9;
c = init_value;
while (size >= 3) {
size--;
a += array[size]->hash();
size--;
b += array[size]->hash();
size--;
c += array[size]->hash();
mix(a, b, c);
}
switch (size) {
case 2:
b += array[1]->hash();
Z3_fallthrough;
case 1:
c += array[0]->hash();
}
mix(a, b, c);
return c;
} }
}
unsigned get_node_hash(ast const * n) {
unsigned a, b, c;
switch (n->get_kind()) {
case AST_SORT:
if (to_sort(n)->get_info() == nullptr)
return to_sort(n)->get_name().hash();
else
return combine_hash(to_sort(n)->get_name().hash(), to_sort(n)->get_info()->hash());
case AST_FUNC_DECL: {
unsigned h = combine_hash(to_func_decl(n)->get_name().hash(), to_func_decl(n)->get_range()->hash());
return ast_array_hash(to_func_decl(n)->get_domain(), to_func_decl(n)->get_arity(),
combine_hash(h, to_func_decl(n)->get_info() == nullptr ? 0 : to_func_decl(n)->get_info()->hash()));
}
case AST_APP:
return ast_array_hash(to_app(n)->get_args(),
to_app(n)->get_num_args(),
to_app(n)->get_decl()->hash());
case AST_VAR:
return combine_hash(to_var(n)->get_idx(), to_var(n)->get_sort()->hash());
case AST_QUANTIFIER:
a = ast_array_hash(to_quantifier(n)->get_decl_sorts(),
to_quantifier(n)->get_num_decls(),
to_quantifier(n)->get_kind() == forall_k ? 31 : 19);
b = to_quantifier(n)->get_num_patterns();
c = to_quantifier(n)->get_expr()->hash();
mix(a,b,c);
return c;
default:
UNREACHABLE();
}
return 0;
}
void ast_table::push_erase(ast * n) {
// It uses two important properties:
// 1. n is known to be in the table.
// 2. operator== can be used instead of compare_nodes (big savings)
unsigned mask = m_slots - 1;
unsigned h = n->hash();
unsigned idx = h & mask;
cell * c = m_table + idx;
cell * prev = nullptr;
while (true) {
SASSERT(!c->is_free());
cell * next = c->m_next;
if (c->m_data == n) {
m_size--;
if (prev == nullptr) {
if (next == nullptr) {
m_used_slots--;
push_recycle_cell(c);
c->mark_free();
SASSERT(c->is_free());
}
else {
*c = *next;
next->m_data = n;
push_recycle_cell(next);
}
}
else {
prev->m_next = next;
push_recycle_cell(c);
}
return;
}
CHS_CODE(m_collisions++;);
prev = c;
c = next;
SASSERT(c);
}
}
ast* ast_table::pop_erase() {
cell* c = m_tofree_cell;
if (c == nullptr) {
return nullptr;
}
if (c->is_free()) {
// cell was marked free, should not be recycled.
c->unmark_free();
m_tofree_cell = c->m_next;
c->mark_free();
}
else {
// cell should be recycled with m_free_cell
m_tofree_cell = c->m_next;
recycle_cell(c);
}
return c->m_data;
}
// -----------------------------------
//
// decl_plugin
//
// -----------------------------------
/**
\brief Checks whether a log is being generated and, if necessary, adds the beginning of an "[attach-meaning]" line
to that log. The theory solver should add some description of the meaning of the term in terms of the theory's
internal reasoning to the end of the line and insert a line break.
\param a the term that should be described.
\return true if a log is being generated, false otherwise.
*/
bool decl_plugin::log_constant_meaning_prelude(app * a) {
if (m_manager->has_trace_stream()) {
m_manager->trace_stream() << "[attach-meaning] #" << a->get_id() << " " << m_manager->get_family_name(m_family_id).str() << " ";
return true;
}
return false;
}
func_decl * decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters,
unsigned num_args, expr * const * args, sort * range) {
ptr_buffer<sort> sorts;
for (unsigned i = 0; i < num_args; i++) {
sorts.push_back(args[i]->get_sort());
}
return mk_func_decl(k, num_parameters, parameters, num_args, sorts.data(), range);
}
// -----------------------------------
//
// basic_decl_plugin (i.e., builtin plugin)
//
// -----------------------------------
bool basic_decl_plugin::check_proof_sorts(basic_op_kind k, unsigned arity, sort * const * domain) const {
if (k == PR_UNDEF)
return arity == 0;
if (arity == 0)
return false;
else {
for (unsigned i = 0; i < arity - 1; i++)
if (domain[i] != m_proof_sort)
return false;
#define is_array(_x_) true
return domain[arity-1] == m_bool_sort || domain[arity-1] == m_proof_sort || is_array(domain[arity-1]);
}
}
bool basic_decl_plugin::check_proof_args(basic_op_kind k, unsigned num_args, expr * const * args) const {
if (k == PR_UNDEF)
return num_args == 0;
if (num_args == 0)
return false;
else {
for (unsigned i = 0; i < num_args - 1; i++)
if (args[i]->get_sort() != m_proof_sort)
return false;
return
args[num_args - 1]->get_sort() == m_bool_sort ||
args[num_args - 1]->get_sort() == m_proof_sort ||
is_lambda(args[num_args-1]);
}
}
func_decl * basic_decl_plugin::mk_bool_op_decl(char const * name, basic_op_kind k, unsigned num_args, bool assoc, bool comm, bool idempotent,
bool flat_associative, bool chainable) {
ptr_buffer<sort> domain;
for (unsigned i = 0; i < num_args; i++)
domain.push_back(m_bool_sort);
func_decl_info info(m_family_id, k);
info.set_associative(assoc);
info.set_flat_associative(flat_associative);
info.set_commutative(comm);
info.set_idempotent(idempotent);
info.set_chainable(chainable);
func_decl * d = m_manager->mk_func_decl(symbol(name), num_args, domain.data(), m_bool_sort, info);
m_manager->inc_ref(d);
return d;
}
func_decl * basic_decl_plugin::mk_implies_decl() {
sort * domain[2] = { m_bool_sort, m_bool_sort };
func_decl_info info(m_family_id, OP_IMPLIES);
info.set_right_associative();
func_decl * d = m_manager->mk_func_decl(symbol("=>"), 2, domain, m_bool_sort, info);
m_manager->inc_ref(d);
return d;
}
func_decl * basic_decl_plugin::mk_proof_decl(
char const * name, basic_op_kind k,
unsigned num_parameters, parameter const* params, unsigned num_parents) {
ptr_buffer<sort> domain;
for (unsigned i = 0; i < num_parents; i++)
domain.push_back(m_proof_sort);
domain.push_back(m_bool_sort);
func_decl_info info(m_family_id, k, num_parameters, params);
return m_manager->mk_func_decl(symbol(name), num_parents+1, domain.data(), m_proof_sort, info);
}
func_decl * basic_decl_plugin::mk_proof_decl(char const * name, basic_op_kind k, unsigned num_parents, bool inc_ref) {
ptr_buffer<sort> domain;
for (unsigned i = 0; i < num_parents; i++)
domain.push_back(m_proof_sort);
domain.push_back(m_bool_sort);
func_decl * d = m_manager->mk_func_decl(symbol(name), num_parents+1, domain.data(), m_proof_sort, func_decl_info(m_family_id, k));
if (inc_ref) m_manager->inc_ref(d);
return d;
}
func_decl * basic_decl_plugin::mk_compressed_proof_decl(char const * name, basic_op_kind k, unsigned num_parents) {
ptr_buffer<sort> domain;
for (unsigned i = 0; i < num_parents; i++)
domain.push_back(m_proof_sort);
func_decl * d = m_manager->mk_func_decl(symbol(name), num_parents, domain.data(), m_proof_sort, func_decl_info(m_family_id, k));
m_manager->inc_ref(d);
return d;
}
func_decl * basic_decl_plugin::mk_proof_decl(char const * name, basic_op_kind k, unsigned num_parents, ptr_vector<func_decl> & cache) {
if (num_parents >= cache.size()) {
cache.resize(num_parents+1, nullptr);
}
if (!cache[num_parents]) {
cache[num_parents] = mk_proof_decl(name, k, num_parents, true);
}
return cache[num_parents];
}
func_decl * basic_decl_plugin::mk_proof_decl(basic_op_kind k, unsigned num_parameters, parameter const* params, unsigned num_parents) {
switch(k) {
case PR_TH_LEMMA: {
return mk_proof_decl("th-lemma", k, num_parameters, params, num_parents);
}
case PR_QUANT_INST: {
SASSERT(num_parents == 0);
return mk_proof_decl("quant-inst", k, num_parameters, params, num_parents);
}
case PR_HYPER_RESOLVE: {
return mk_proof_decl("hyper-res", k, num_parameters, params, num_parents);
}
default:
UNREACHABLE();
return nullptr;
}
}
#define MK_DECL(_decl_,_mk_decl_) if (!_decl_) _decl_ = _mk_decl_; return _decl_;
func_decl * basic_decl_plugin::mk_proof_decl(char const* name, basic_op_kind k, unsigned num_parents, func_decl*& fn) {
if (!fn) {
fn = mk_proof_decl(name, k, num_parents, true);
}
return fn;
}
func_decl * basic_decl_plugin::mk_proof_decl(basic_op_kind k, unsigned num_parents) {
switch (static_cast<basic_op_kind>(k)) {
//
// A description of the semantics of the proof
// declarations is provided in z3_api.h
//
case PR_UNDEF: return m_undef_decl;
case PR_TRUE: return mk_proof_decl("true-axiom", k, 0, m_true_pr_decl);
case PR_ASSERTED: return mk_proof_decl("asserted", k, 0, m_asserted_decl);
case PR_GOAL: return mk_proof_decl("goal", k, 2, m_goal_decl);
case PR_MODUS_PONENS: return mk_proof_decl("mp", k, 2, m_modus_ponens_decl);
case PR_REFLEXIVITY: return mk_proof_decl("refl", k, 0, m_reflexivity_decl);
case PR_SYMMETRY: return mk_proof_decl("symm", k, 1, m_symmetry_decl);
case PR_TRANSITIVITY: return mk_proof_decl("trans", k, 2, m_transitivity_decl);
case PR_TRANSITIVITY_STAR: return mk_proof_decl("trans*", k, num_parents, m_transitivity_star_decls);
case PR_MONOTONICITY: return mk_proof_decl("monotonicity", k, num_parents, m_monotonicity_decls);
case PR_QUANT_INTRO: return mk_proof_decl("quant-intro", k, 1, m_quant_intro_decl);
case PR_BIND: UNREACHABLE();
case PR_DISTRIBUTIVITY: return mk_proof_decl("distributivity", k, num_parents, m_distributivity_decls);
case PR_AND_ELIM: return mk_proof_decl("and-elim", k, 1, m_and_elim_decl);
case PR_NOT_OR_ELIM: return mk_proof_decl("not-or-elim", k, 1, m_not_or_elim_decl);
case PR_REWRITE: return mk_proof_decl("rewrite", k, 0, m_rewrite_decl);
case PR_REWRITE_STAR: return mk_proof_decl("rewrite*", k, num_parents, m_rewrite_star_decls);
case PR_PULL_QUANT: return mk_proof_decl("pull-quant", k, 0, m_pull_quant_decl);
case PR_PUSH_QUANT: return mk_proof_decl("push-quant", k, 0, m_push_quant_decl);
case PR_ELIM_UNUSED_VARS: return mk_proof_decl("elim-unused", k, 0, m_elim_unused_vars_decl);
case PR_DER: return mk_proof_decl("der", k, 0, m_der_decl);
case PR_QUANT_INST: return mk_proof_decl("quant-inst", k, 0, m_quant_inst_decl);
case PR_HYPOTHESIS: return mk_proof_decl("hypothesis", k, 0, m_hypothesis_decl);
case PR_LEMMA: return mk_proof_decl("lemma", k, 1, m_lemma_decl);
case PR_UNIT_RESOLUTION: return mk_proof_decl("unit-resolution", k, num_parents, m_unit_resolution_decls);
case PR_IFF_TRUE: return mk_proof_decl("iff-true", k, 1, m_iff_true_decl);
case PR_IFF_FALSE: return mk_proof_decl("iff-false", k, 1, m_iff_false_decl);
case PR_COMMUTATIVITY: return mk_proof_decl("commutativity", k, 0, m_commutativity_decl);
case PR_DEF_AXIOM: return mk_proof_decl("def-axiom", k, 0, m_def_axiom_decl);
case PR_DEF_INTRO: return mk_proof_decl("intro-def", k, 0, m_def_intro_decl);
case PR_APPLY_DEF: return mk_proof_decl("apply-def", k, num_parents, m_apply_def_decls);
case PR_IFF_OEQ: return mk_proof_decl("iff~", k, 1, m_iff_oeq_decl);
case PR_NNF_POS: return mk_proof_decl("nnf-pos", k, num_parents, m_nnf_pos_decls);
case PR_NNF_NEG: return mk_proof_decl("nnf-neg", k, num_parents, m_nnf_neg_decls);
case PR_SKOLEMIZE: return mk_proof_decl("sk", k, 0, m_skolemize_decl);
case PR_MODUS_PONENS_OEQ: return mk_proof_decl("mp~", k, 2, m_mp_oeq_decl);
case PR_TH_LEMMA: return mk_proof_decl("th-lemma", k, num_parents, m_th_lemma_decls);
case PR_HYPER_RESOLVE: return mk_proof_decl("hyper-res", k, num_parents, m_hyper_res_decl0);
case PR_ASSUMPTION_ADD: return mk_proof_decl("assume", k, num_parents, m_assumption_add_decl);
case PR_LEMMA_ADD: return mk_proof_decl("infer", k, num_parents, m_lemma_add_decl);
case PR_TH_ASSUMPTION_ADD: return mk_proof_decl("th-assume", k, num_parents, m_th_assumption_add_decl);
case PR_TH_LEMMA_ADD: return mk_proof_decl("th-lemma", k, num_parents, m_th_lemma_add_decl);
case PR_REDUNDANT_DEL: return mk_proof_decl("del", k, num_parents, m_redundant_del_decl);
case PR_CLAUSE_TRAIL: return mk_proof_decl("proof-trail", k, num_parents, false);
default:
UNREACHABLE();
return nullptr;
}
}
void basic_decl_plugin::set_manager(ast_manager * m, family_id id) {
decl_plugin::set_manager(m, id);
m_bool_sort = m->mk_sort(symbol("Bool"), sort_info(id, BOOL_SORT, sort_size(2)));
m->inc_ref(m_bool_sort);
m_true_decl = mk_bool_op_decl("true", OP_TRUE);
m_false_decl = mk_bool_op_decl("false", OP_FALSE);
m_and_decl = mk_bool_op_decl("and", OP_AND, 2, true, true, true, true);
m_or_decl = mk_bool_op_decl("or", OP_OR, 2, true, true, true, true);
m_xor_decl = mk_bool_op_decl("xor", OP_XOR, 2, true, true);
m_not_decl = mk_bool_op_decl("not", OP_NOT, 1);
m_implies_decl = mk_implies_decl();
m_proof_sort = m->mk_sort(symbol("Proof"), sort_info(id, PROOF_SORT));
m->inc_ref(m_proof_sort);
m_undef_decl = mk_compressed_proof_decl("undef", PR_UNDEF, 0);
}
void basic_decl_plugin::get_sort_names(svector<builtin_name> & sort_names, symbol const & logic) {
if (logic == symbol::null) {
sort_names.push_back(builtin_name("bool", BOOL_SORT));
sort_names.push_back(builtin_name("Proof", PROOF_SORT)); // reserved name?
}
sort_names.push_back(builtin_name("Bool", BOOL_SORT));
}
void basic_decl_plugin::get_op_names(svector<builtin_name> & op_names, symbol const & logic) {
op_names.push_back(builtin_name("true", OP_TRUE));
op_names.push_back(builtin_name("false", OP_FALSE));
op_names.push_back(builtin_name("=", OP_EQ));
op_names.push_back(builtin_name("distinct", OP_DISTINCT));
op_names.push_back(builtin_name("ite", OP_ITE));
op_names.push_back(builtin_name("if", OP_ITE));
op_names.push_back(builtin_name("and", OP_AND));
op_names.push_back(builtin_name("or", OP_OR));
op_names.push_back(builtin_name("xor", OP_XOR));
op_names.push_back(builtin_name("not", OP_NOT));
op_names.push_back(builtin_name("=>", OP_IMPLIES));
if (logic == symbol::null) {
// user friendly aliases
op_names.push_back(builtin_name("implies", OP_IMPLIES));
op_names.push_back(builtin_name("iff", OP_EQ));
op_names.push_back(builtin_name("if_then_else", OP_ITE));
op_names.push_back(builtin_name("&&", OP_AND));
op_names.push_back(builtin_name("||", OP_OR));
op_names.push_back(builtin_name("equals", OP_EQ));
op_names.push_back(builtin_name("equiv", OP_EQ));
}
}
bool basic_decl_plugin::is_value(app* a) const {
return a->get_decl() == m_true_decl || a->get_decl() == m_false_decl;
}
bool basic_decl_plugin::is_unique_value(app* a) const {
return is_value(a);
}
void basic_decl_plugin::finalize() {
#define DEC_REF(FIELD) if (FIELD) { m_manager->dec_ref(FIELD); }
#define DEC_ARRAY_REF(FIELD) m_manager->dec_array_ref(FIELD.size(), FIELD.begin())
DEC_REF(m_bool_sort);
DEC_REF(m_true_decl);
DEC_REF(m_false_decl);
DEC_REF(m_and_decl);
DEC_REF(m_or_decl);
DEC_REF(m_not_decl);
DEC_REF(m_xor_decl);
DEC_REF(m_implies_decl);
DEC_ARRAY_REF(m_eq_decls);
DEC_ARRAY_REF(m_ite_decls);
DEC_ARRAY_REF(m_oeq_decls);
DEC_REF(m_proof_sort);
DEC_REF(m_undef_decl);
DEC_REF(m_true_pr_decl);
DEC_REF(m_asserted_decl);
DEC_REF(m_goal_decl);
DEC_REF(m_modus_ponens_decl);
DEC_REF(m_reflexivity_decl);
DEC_REF(m_symmetry_decl);
DEC_REF(m_transitivity_decl);
DEC_REF(m_quant_intro_decl);
DEC_REF(m_and_elim_decl);
DEC_REF(m_not_or_elim_decl);
DEC_REF(m_rewrite_decl);
DEC_REF(m_pull_quant_decl);
DEC_REF(m_push_quant_decl);
DEC_REF(m_elim_unused_vars_decl);
DEC_REF(m_der_decl);
DEC_REF(m_quant_inst_decl);
DEC_ARRAY_REF(m_monotonicity_decls);
DEC_ARRAY_REF(m_transitivity_star_decls);
DEC_ARRAY_REF(m_distributivity_decls);
DEC_ARRAY_REF(m_assoc_flat_decls);
DEC_ARRAY_REF(m_rewrite_star_decls);
DEC_REF(m_hypothesis_decl);
DEC_REF(m_iff_true_decl);
DEC_REF(m_iff_false_decl);
DEC_REF(m_commutativity_decl);
DEC_REF(m_def_axiom_decl);
DEC_REF(m_lemma_decl);
DEC_ARRAY_REF(m_unit_resolution_decls);
DEC_REF(m_def_intro_decl);
DEC_REF(m_iff_oeq_decl);
DEC_REF(m_skolemize_decl);
DEC_REF(m_mp_oeq_decl);
DEC_REF(m_assumption_add_decl);
DEC_REF(m_lemma_add_decl);
DEC_REF(m_th_assumption_add_decl);
DEC_REF(m_th_lemma_add_decl);
DEC_REF(m_redundant_del_decl);
DEC_ARRAY_REF(m_apply_def_decls);
DEC_ARRAY_REF(m_nnf_pos_decls);
DEC_ARRAY_REF(m_nnf_neg_decls);
DEC_ARRAY_REF(m_th_lemma_decls);
DEC_REF(m_hyper_res_decl0);
}
sort * basic_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters) {
if (k == BOOL_SORT)
return m_bool_sort;
SASSERT(k == PROOF_SORT);
return m_proof_sort;
}
func_decl * basic_decl_plugin::mk_eq_decl_core(char const * name, decl_kind k, sort * s, ptr_vector<func_decl> & cache) {
unsigned id = s->get_small_id();
force_ptr_array_size(cache, id + 1);
if (cache[id] == 0) {
sort * domain[2] = { s, s};
func_decl_info info(m_family_id, k);
info.set_commutative();
info.set_chainable();
func_decl * decl = m_manager->mk_func_decl(symbol(name), 2, domain, m_bool_sort, info);
SASSERT(decl->is_chainable());
cache[id] = decl;
m_manager->inc_ref(decl);
}
return cache[id];
}
func_decl * basic_decl_plugin::mk_ite_decl(sort * s) {
unsigned id = s->get_small_id();
force_ptr_array_size(m_ite_decls, id + 1);
if (m_ite_decls[id] == 0) {
sort * domain[3] = { m_bool_sort, s, s};
func_decl * decl = m_manager->mk_func_decl(symbol("if"), 3, domain, s, func_decl_info(m_family_id, OP_ITE));
m_ite_decls[id] = decl;
m_manager->inc_ref(decl);
}
return m_ite_decls[id];
}
sort* basic_decl_plugin::join(unsigned n, sort* const* srts) {
SASSERT(n > 0);
sort* s = srts[0];
while (n > 1) {
++srts;
--n;
s = join(s, *srts);
}
return s;
}
sort* basic_decl_plugin::join(unsigned n, expr* const* es) {
SASSERT(n > 0);