z3-src 501.0.1

Source distribution of the Z3 SMT solver, for use as a build dependency
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    smt_context.h

Abstract:

    Logical context

Author:

    Leonardo de Moura (leonardo) 2008-02-18.

Revision History:

--*/
#pragma once

#include <atomic>
#include "ast/quantifier_stat.h"
#include "ast/simplifiers/dependent_expr_state.h"
#include "smt/smt_clause.h"
#include "smt/smt_setup.h"
#include "smt/smt_enode.h"
#include "smt/smt_cg_table.h"
#include "smt/smt_b_justification.h"
#include "smt/smt_eq_justification.h"
#include "smt/smt_justification.h"
#include "smt/smt_bool_var_data.h"
#include "smt/smt_clause_proof.h"
#include "smt/smt_theory.h"
#include "smt/smt_quantifier.h"
#include "smt/smt_statistics.h"
#include "smt/smt_conflict_resolution.h"
#include "smt/smt_relevancy.h"
#include "smt/smt_case_split_queue.h"
#include "smt/smt_almost_cg_table.h"
#include "smt/smt_failure.h"
#include "smt/smt_types.h"
#include "smt/dyn_ack.h"
#include "ast/ast_smt_pp.h"
#include "smt/watch_list.h"
#include "util/trail.h"
#include "util/ref.h"
#include "util/timer.h"
#include "util/statistics.h"
#include "util/map.h"
#include "smt/fingerprints.h"
#include "smt/proto_model/proto_model.h"
#include "smt/theory_user_propagator.h"
#include "model/model.h"
#include "solver/progress_callback.h"
#include "solver/assertions/asserted_formulas.h"
#include <tuple>

// there is a significant space overhead with allocating 1000+ contexts in
// the case that each context only references a few expressions.
// Using a map instead of a vector for the literals can compress space
// consumption.
#define USE_BOOL_VAR_VECTOR 1

namespace smt {

    class model_generator;
    class context;
    class kernel;

    // Hash table for storing enode -> generation mappings
    typedef ptr_addr_map<enode, unsigned> enode_generation_table;

    struct oom_exception : public z3_error {
        oom_exception() : z3_error(ERR_MEMOUT) {}
    };

    struct enode_pp {
        context const& ctx;
        enode*   n;
        enode_pp(enode* n, context const& ctx): ctx(ctx), n(n) {}
    };

    struct replay_unit {
        expr_ref m_unit;
        bool     m_sign;
        bool     m_relevant;
    };

    class context {
        friend class model_generator;
        friend class lookahead;
        friend class parallel;
        friend class kernel;
    public:
        statistics                  m_stats;

        std::ostream& display_last_failure(std::ostream& out) const;
        std::string last_failure_as_string() const;
        void set_reason_unknown(char const* msg) { m_unknown = msg; }
        void set_progress_callback(progress_callback *callback);


    protected:
        ast_manager &               m;
        smt_params &                m_fparams;
        params_ref                  m_params;
        ::statistics                m_aux_stats;
        setup                       m_setup;
        unsigned                    m_relevancy_lvl;
        timer                       m_timer;
        region                      m_region;
        asserted_formulas           m_asserted_formulas;
        th_rewriter                 m_rewriter;
        scoped_ptr<quantifier_manager>   m_qmanager;
        scoped_ptr<model_generator>      m_model_generator;
        scoped_ptr<relevancy_propagator> m_relevancy_propagator;
        theory_user_propagator*          m_user_propagator;
        random_gen                  m_random;
        bool                        m_flushing; // (debug support) true when flushing
        mutable unsigned            m_lemma_id;
        progress_callback *         m_progress_callback;
        unsigned                    m_next_progress_sample;
        clause_proof                m_clause_proof;
        fingerprint_set             m_fingerprints;

        expr_ref_vector             m_b_internalized_stack; // stack of the boolean expressions already internalized.
        // Remark: boolean expressions can also be internalized as
        // enodes. Examples: boolean expression nested in an
        // uninterpreted function.
        expr_ref_vector             m_e_internalized_stack; // stack of the expressions already internalized as enodes.

        ptr_vector<justification>   m_justifications;

        unsigned                    m_final_check_idx = 0; // circular counter used for implementing fairness

        bool                        m_is_auxiliary = false; // used to prevent unwanted information from being logged.
        class parallel*             m_par = nullptr;
        unsigned                    m_par_index = 0;
        bool                        m_internalizing_assertions = false;
        lbool                       m_internal_completed = l_undef;

        scoped_ptr<dependent_expr_simplifier> m_simplifier;
        scoped_ptr<base_dependent_expr_state> m_fmls;

        svector<double> m_lit_scores[2];
        svector<unsigned> m_birthdate;


        // -----------------------------------
        //
        // Equality & Uninterpreted functions
        //
        // -----------------------------------
        enode *                     m_true_enode;
        enode *                     m_false_enode;
        app2enode_t                 m_app2enode;    // app -> enode
        ptr_vector<enode>           m_enodes;
        plugin_manager<theory>      m_theories;     // mapping from theory_id -> theory
        ptr_vector<theory>          m_theory_set;   // set of theories for fast traversal
        vector<enode_vector>        m_decl2enodes;  // decl -> enode (for decls with arity > 0)
        enode_vector                m_empty_vector;
        cg_table                    m_cg_table;
        enode_generation_table      m_sticky_generation_updates;
        vector<std::pair<enode*, unsigned>>      m_r1_parent_generations; // temporary field used to cache generations between remove_parents_from_cg_table and reinsert_parents_into_cg_table
        struct new_eq {
            enode *                 m_lhs;
            enode *                 m_rhs;
            eq_justification        m_justification;
            new_eq(enode * lhs, enode * rhs, eq_justification const & js):
                m_lhs(lhs), m_rhs(rhs), m_justification(js) {}
        };
        svector<new_eq>             m_eq_propagation_queue;
        struct new_th_eq {
            theory_id  m_th_id;
            theory_var m_lhs;
            theory_var m_rhs;
            new_th_eq(theory_id id, theory_var l, theory_var r):m_th_id(id), m_lhs(l), m_rhs(r) {}
        };
        svector<new_th_eq>          m_th_eq_propagation_queue;
        svector<new_th_eq>          m_th_diseq_propagation_queue;
#ifdef Z3DEBUG
        svector<new_th_eq>          m_propagated_th_eqs;
        svector<new_th_eq>          m_propagated_th_diseqs;
        svector<enode_pair>         m_diseq_vector;
#endif
        enode *                     m_is_diseq_tmp { nullptr }; // auxiliary enode used to find congruent equality atoms.

        tmp_enode                   m_tmp_enode;
        ptr_vector<almost_cg_table> m_almost_cg_tables; // temporary field for is_ext_diseq

        // -----------------------------------
        //
        // Boolean engine
        //
        // -----------------------------------
#if USE_BOOL_VAR_VECTOR
        svector<bool_var>           m_expr2bool_var;         // expr id -> bool_var
#else
        u_map<bool_var>             m_expr2bool_var;
#endif
        ptr_vector<expr>            m_bool_var2expr;         // bool_var -> expr
        signed_char_vector          m_assignment;  //!< mapping literal id -> assignment lbool
        vector<watch_list>          m_watches;     //!< per literal
        unsigned_vector             m_lit_occs;    //!< occurrence count of literals
        svector<bool_var_data>      m_bdata;       //!< mapping bool_var -> data
        svector<double>             m_activity;
        clause_vector               m_aux_clauses;
        clause_vector               m_lemmas;
        vector<clause_vector>       m_clauses_to_reinit;
        vector<replay_unit>         m_units_to_reassert;
        literal_vector              m_assigned_literals;
        typedef std::pair<clause*, literal_vector> tmp_clause;
        vector<tmp_clause>          m_tmp_clauses;
        unsigned                    m_qhead { 0 };
        unsigned                    m_simp_qhead { 0 };
        int                         m_simp_counter { 0 }; //!< can become negative
        scoped_ptr<case_split_queue> m_case_split_queue;
        double                      m_bvar_inc { 1.0 };
        bool                        m_phase_cache_on { true };
        unsigned                    m_phase_counter { 0 }; //!< auxiliary variable used to decide when to turn on/off phase caching
        bool                        m_phase_default { false }; //!< default phase when using phase caching

        // A conflict is usually a single justification. That is, a justification
        // for false. If m_not_l is not null_literal, then m_conflict is a
        // justification for l, and the conflict is union of m_not_l and m_conflict;
        // m_empty_clause is set to ensure that an empty clause generated in deep scope 
        // levels survives to the base level.
        b_justification             m_conflict;
        literal                     m_not_l;
        scoped_ptr<conflict_resolution> m_conflict_resolution;
        proof_ref                   m_unsat_proof;


        literal_vector              m_atom_propagation_queue;

        obj_map<expr, unsigned>     m_cached_generation;
        obj_hashtable<expr>         m_cache_generation_visited;
        dyn_ack_manager             m_dyn_ack_manager;

        // -----------------------------------
        //
        // Model generation
        //
        // -----------------------------------
        proto_model_ref            m_proto_model;
        model_ref                  m_model;
        const char *               m_unknown;
        void                       mk_proto_model();
        void                       reset_model() { m_model = nullptr; m_proto_model = nullptr; }

        // -----------------------------------
        //
        // Unsat core extraction
        //
        // -----------------------------------
        typedef u_map<expr *>      literal2assumption;
        literal_vector             m_assumptions;
        literal2assumption         m_literal2assumption; // maps an expression associated with a literal to the original assumption
        expr_ref_vector            m_unsat_core;

        unsigned                   m_last_position_log { 0 };
        svector<size_t>            m_last_positions;

        // -----------------------------------
        //
        // Theory case split
        //
        // -----------------------------------
        uint_set m_all_th_case_split_literals;
        vector<literal_vector> m_th_case_split_sets;
        u_map< vector<literal_vector> > m_literal2casesplitsets; // returns the case split literal sets that a literal participates in


        // ----------------------------------
        //
        // Value initialization
        //
        // ----------------------------------
        vector<std::pair<expr_ref, expr_ref>> m_values;
        void initialize_value(expr* var, expr* value);
        void initialize_values();


        // -----------------------------------
        //
        // Accessors
        //
        // -----------------------------------
    public:
        ast_manager & get_manager() const {
            return m;
        }

        th_rewriter & get_rewriter() {
            return m_rewriter;
        }

        smt_params & get_fparams() {
            return m_fparams;
        }

        smt_params const& get_fparams() const {
            return m_fparams;
        }

        params_ref const & get_params() {
            return m_params;
        }

        void updt_params(params_ref const& p);

        bool get_cancel_flag();

        void set_internal_completed() {
            if (m_internal_completed == l_undef)
                m_internal_completed = l_true;
        }

        region & get_region() {
            return m_region;
        }

        bool relevancy() const {
            return relevancy_lvl() > 0;
        }

        unsigned relevancy_lvl() const;

        enode * get_enode(expr const * n) const {
            SASSERT(e_internalized(n));
            return m_app2enode[n->get_id()];
        }

        void get_specrels(func_decl_set& rels) const;


        /**
           \brief Similar to get_enode, but returns 0 if n is to e_internalized.
        */
        enode * find_enode(expr const * n) const {
            return m_app2enode.get(n->get_id(), 0);
        }

        void reset_bool_vars() {
            m_expr2bool_var.reset();
        }

        bool_var get_bool_var(expr const * n) const {
            return m_expr2bool_var[n->get_id()];
        }

        bool_var get_bool_var(enode const * n) const {
            return get_bool_var(n->get_expr());
        }

        bool_var get_bool_var_of_id(unsigned id) const {
            return m_expr2bool_var[id];
        }

        bool_var get_bool_var_of_id_option(unsigned id) const {
            return m_expr2bool_var.get(id, null_bool_var);
        }

#if USE_BOOL_VAR_VECTOR

        void set_bool_var(unsigned id, bool_var v) {
            m_expr2bool_var.setx(id, v, null_bool_var);
        }
#else

        void set_bool_var(unsigned id, bool_var v) {
            if (v == null_bool_var) {
                m_expr2bool_var.erase(id);
            }
            else {
                m_expr2bool_var.insert(id, v);
            }
        }
#endif

        clause_vector const& get_lemmas() const { return m_lemmas; }

        literal get_literal(expr * n) const;

        bool has_enode(bool_var v) const {
            return m_bdata[v].is_enode();
        }

        enode * bool_var2enode(bool_var v) const {
            SASSERT(m_bdata[v].is_enode());
            return m_app2enode[m_bool_var2expr[v]->get_id()];
        }

        bool_var enode2bool_var(enode const * n) const {
            SASSERT(n->is_bool());
            SASSERT(n != m_false_enode);
            return get_bool_var_of_id(n->get_owner_id());
        }

        literal enode2literal(enode const * n) const {
            SASSERT(n->is_bool());
            return n == m_false_enode ? false_literal : literal(enode2bool_var(n));
        }

        unsigned get_num_bool_vars() const {
            return m_b_internalized_stack.size();
        }

        bool_var_data & get_bdata(bool_var v) {
            return m_bdata[v];
        }

        bool_var_data const & get_bdata(bool_var v) const {
            return m_bdata[v];
        }

        lbool get_lit_assignment(unsigned lit_idx) const {
            return static_cast<lbool>(m_assignment[lit_idx]);
        }

        lbool get_assignment(literal l) const {
            return get_lit_assignment(l.index());
        }

        lbool get_assignment(bool_var v) const {
            return get_assignment(literal(v));
        }

        literal_vector const & assigned_literals() const {
            return m_assigned_literals;
        }

        watch_list const& get_watch(literal l) const {
            return m_watches[l.index()];
        }

        lbool get_assignment(expr * n) const;

        // Similar to get_assignment, but returns l_undef if n is not internalized.
        lbool find_assignment(expr * n) const;

        lbool get_assignment(enode * n) const;

        void get_assignments(expr_ref_vector& assignments);

        b_justification get_justification(bool_var v) const {
            return get_bdata(v).justification();
        }

        void set_justification(bool_var v, bool_var_data& d, b_justification const& j);

        bool has_th_justification(bool_var v, theory_id th_id) const {
            b_justification js = get_justification(v);
            return js.get_kind() == b_justification::JUSTIFICATION && js.get_justification()->get_from_theory() == th_id;
        }

        void set_random_seed(unsigned s) { m_random.set_seed(s); }

        int get_random_value() { return m_random(); }

        bool is_searching() const { return m_searching; }

        svector<double> const & get_activity_vector() const { return m_activity; }

        double get_activity(bool_var v) const { return m_activity[v]; }
        unsigned get_num_assignments() const { return m_stats.m_num_assignments; }
        unsigned get_birthdate(bool_var v) const { return m_birthdate[v]; }

        void set_activity(bool_var v, double act) { m_activity[v] = act; }

        void activity_changed(bool_var v, bool increased) {
            if (increased) {
                m_case_split_queue->activity_increased_eh(v);
            }
            else {
                m_case_split_queue->activity_decreased_eh(v);
            }
        }

        bool is_assumption(bool_var v) const {
            return get_bdata(v).m_assumption;
        }

        bool is_assumption(literal l) const {
            return is_assumption(l.var());
        }

        bool is_marked(bool_var v) const {
            return get_bdata(v).m_mark;
        }

        void set_mark(bool_var v) {
            SASSERT(!is_marked(v));
            get_bdata(v).m_mark = true;
        }

        void unset_mark(bool_var v) {
            SASSERT(is_marked(v));
            get_bdata(v).m_mark = false;
        }

        /**
           \brief Return the scope level when v was assigned.
        */
        unsigned get_assign_level(bool_var v) const {
            return get_bdata(v).m_scope_lvl;
        }

        unsigned get_assign_level(literal l) const {
            return get_assign_level(l.var());
        }

        /**
           \brief Return the scope level when v was internalized.
        */
        unsigned get_intern_level(bool_var v) const {
            return get_bdata(v).get_intern_level();
        }

        theory * get_theory(theory_id th_id) const {
            return m_theories.get_plugin(th_id);
        }
        
        ptr_vector<theory> const& theories() const { return m_theories.plugins(); }

        ptr_vector<theory>::const_iterator begin_theories() const {
            return m_theories.begin();
        }

        ptr_vector<theory>::const_iterator end_theories() const {
            return m_theories.end();
        }

        unsigned get_scope_level() const {
            return m_scope_lvl;
        }

        unsigned get_base_level() const {
            return m_base_lvl;
        }

        bool at_base_level() const {
            return m_scope_lvl == m_base_lvl;
        }

        unsigned get_search_level() const {
            return m_search_lvl;
        }

        bool at_search_level() const {
            return m_scope_lvl == m_search_lvl;
        }

        void pop_to_search_level() { pop_to_search_lvl(); }

        bool tracking_assumptions() const {
            return !m_assumptions.empty() && m_search_lvl > m_base_lvl;
        }

        expr * bool_var2expr(bool_var v) const {
            return m_bool_var2expr[v];
        }

        void literal2expr(literal l, expr_ref & result) const {
            if (l == true_literal)
                result = m.mk_true();
            else if (l == false_literal)
                result = m.mk_false();
            else if (l.sign())
                result = m.mk_not(bool_var2expr(l.var()));
            else
                result = bool_var2expr(l.var());
        }

        expr_ref literal2expr(literal l) const {
            expr_ref result(m);
            literal2expr(l, result);
            return result;
        }

        bool is_true(enode const * n) const {
            return n == m_true_enode;
        }

        bool is_false(enode const * n) const {
            return n == m_false_enode;
        }

        unsigned get_num_enodes_of(func_decl const * decl) const {
            unsigned id = decl->get_small_id();
            return id < m_decl2enodes.size() ? m_decl2enodes[id].size() : 0;
        }

        enode_vector const& enodes_of(func_decl const * d) const {
            unsigned id = d->get_small_id();
            return id < m_decl2enodes.size() ? m_decl2enodes[id] : m_empty_vector;
        }

        enode_vector::const_iterator begin_enodes_of(func_decl const * decl) const {
            unsigned id = decl->get_small_id();
            return id < m_decl2enodes.size() ? m_decl2enodes[id].begin() : nullptr;
        }

        enode_vector::const_iterator end_enodes_of(func_decl const * decl) const {
            unsigned id = decl->get_small_id();
            return id < m_decl2enodes.size() ? m_decl2enodes[id].end() : nullptr;
        }

        ptr_vector<enode> const& enodes() const { return m_enodes; }

        ptr_vector<enode>::const_iterator begin_enodes() const {
            return m_enodes.begin();
        }

        ptr_vector<enode>::const_iterator end_enodes() const {
            return m_enodes.end();
        }

        unsigned get_generation(quantifier * q) const {
            return m_qmanager->get_generation(q);
        }

        unsigned get_generation(enode * e) const  {
            // We don't support patterns with equality so there is no need to track generations for them.
            if (e->is_eq())
                return 0;

            return get_cg_root(e)->m_generation;
        }

        unsigned get_max_generation(unsigned num_enodes, enode * const * enodes) {
            unsigned max = 0;
            for (unsigned i = 0; i < num_enodes; ++i) {
                unsigned curr = get_generation(enodes[i]);
                if (curr > max)
                    max = curr;
            }
            return max;
        }

        void set_generation(enode * e, unsigned generation);

        /**
           \brief Return true if the logical context internalized universal quantifiers.
        */
        bool internalized_quantifiers() const {
            return !m_qmanager->empty();
        }

        /**
           \brief Return true if the logical context internalized or will internalize universal quantifiers.
        */
        bool has_quantifiers() const {
            return m_asserted_formulas.has_quantifiers();
        }

        fingerprint * add_fingerprint(void * data, unsigned data_hash, unsigned num_args, enode * const * args) {
            return m_fingerprints.insert(data, data_hash, num_args, args);
        }

        theory_id get_var_theory(bool_var v) const {
            return get_bdata(v).get_theory();
        }

        /** 
         * flag to toggle quantifier tracing.
         */
        bool m_coming_from_quant { false };


        friend class set_var_theory_trail;
        void set_var_theory(bool_var v, theory_id tid);


        bool has_sls_model();

        // -----------------------------------
        //
        // Backtracking support
        //
        // -----------------------------------
    protected:
        trail_stack                           m_trail_stack;
#ifdef Z3DEBUG
        bool                                  m_trail_enabled { true };
#endif

    public:
        template<typename TrailObject>
        void push_trail(const TrailObject & obj) {
            SASSERT(m_trail_enabled);
            m_trail_stack.push(obj);
        }

        void push_trail_ptr(trail * ptr) {
            m_trail_stack.push_ptr(ptr);
        }

        trail_stack& get_trail_stack() {
            return m_trail_stack;
        }

    protected:

        unsigned                    m_scope_lvl { 0 };
        unsigned                    m_base_lvl { 0 };
        unsigned                    m_search_lvl { 0 }; // It is greater than m_base_lvl when assumptions are used.  Otherwise, it is equals to m_base_lvl
        struct scope {
            unsigned                m_assigned_literals_lim;
            unsigned                m_aux_clauses_lim;
            unsigned                m_justifications_lim;
            unsigned                m_units_to_reassert_lim;
        };
        struct base_scope {
            unsigned                m_lemmas_lim;
            unsigned                m_simp_qhead_lim;
            unsigned                m_inconsistent;
        };

        svector<scope>              m_scopes;
        svector<base_scope>         m_base_scopes;

        void push_scope();

        unsigned pop_scope_core(unsigned num_scopes);

        void pop_scope(unsigned num_scopes);

        void unassign_vars(unsigned old_lim);

        void remove_watch_literal(clause * cls, unsigned idx);

        void remove_cls_occs(clause * cls);

        void del_clause(bool log, clause * cls);

        void del_clauses(clause_vector & v, unsigned old_size);

        void del_justifications(ptr_vector<justification> & justifications, unsigned old_lim);

        bool is_unit_clause(clause const * c) const;

        bool is_empty_clause(clause const * c) const;

        void cache_generation(unsigned new_scope_lvl);

        void cache_generation(clause const * cls, unsigned new_scope_lvl);

        void cache_generation(unsigned num_lits, literal const * lits, unsigned new_scope_lvl);

        void cache_generation(expr * n, unsigned new_scope_lvl);

        void reset_cache_generation();

        void reinit_clauses(unsigned num_scopes, unsigned num_bool_vars);

        void reassert_units(unsigned units_to_reassert_lim);

    public:
        // \brief exposed for PB solver to participate in GC

        void remove_watch(bool_var v);

        // -----------------------------------
        //
        // Internalization
        //
        // -----------------------------------
    public:
        bool b_internalized(expr const * n) const {
            return get_bool_var_of_id_option(n->get_id()) != null_bool_var;
        }

        bool lit_internalized(expr const * n) const {
            return m.is_false(n) || (m.is_not(n) ? b_internalized(to_app(n)->get_arg(0)) : b_internalized(n));
        }

        bool e_internalized(expr const * n) const {
            return m_app2enode.get(n->get_id(), 0) != 0;
        }

        unsigned get_num_b_internalized() const {
            return m_b_internalized_stack.size();
        }

        expr * get_b_internalized(unsigned idx) const {
            return  m_b_internalized_stack.get(idx);
        }

        unsigned get_num_e_internalized() const {
            return m_e_internalized_stack.size();
        }

        expr * get_e_internalized(unsigned idx) const {
            return  m_e_internalized_stack.get(idx);
        }

        /**
           \brief Return the position (in the assignment stack) of the decision literal at the given scope level.
        */
        unsigned get_decision_literal_pos(unsigned scope_lvl) const {
            SASSERT(scope_lvl > m_base_lvl);
            return m_scopes[scope_lvl - 1].m_assigned_literals_lim;
        }

    protected:
        unsigned m_generation { 0 }; //!< temporary variable used during internalization

    public:
        bool binary_clause_opt_enabled() const {
            return !m.proofs_enabled() && m_fparams.m_binary_clause_opt;
        }
    protected:
        bool_var_data & get_bdata(expr const * n) {
            return get_bdata(get_bool_var(n));
        }

        bool_var_data const & get_bdata(expr const * n) const {
            return get_bdata(get_bool_var(n));
        }

        void update_generation(enode * n);

        void update_generation(expr * e) {
            if (is_app(e) && e_internalized(e))
                update_generation(get_enode(to_app(e)));
        }

        typedef std::pair<expr *, bool> expr_bool_pair;

        void ts_visit_child(expr * n, bool gate_ctx, svector<expr_bool_pair> & todo, bool & visited);

        bool ts_visit_children(expr * n, bool gate_ctx, svector<expr_bool_pair> & todo);

        svector<expr_bool_pair> ts_todo;
        char_vector       tcolors;
        char_vector       fcolors;

        bool should_internalize_rec(expr* e) const;

        void top_sort_expr(expr* const* exprs, unsigned num_exprs, svector<expr_bool_pair> & sorted_exprs);

        void internalize_rec(expr * n, bool gate_ctx);

        void internalize_deep(expr * n);
        void internalize_deep(expr* const* n, unsigned num_exprs);

        void assert_default(expr * n, proof * pr);

        void assert_distinct(app * n, proof * pr);

        void internalize_formula(expr * n, bool gate_ctx);

        void internalize_eq(app * n, bool gate_ctx);

        void internalize_distinct(app * n, bool gate_ctx);

        bool internalize_theory_atom(app * n, bool gate_ctx);

        void internalize_quantifier(quantifier * q, bool gate_ctx);

        obj_map<enode, quantifier*> m_lambdas;

        bool has_lambda();

        void internalize_lambda(quantifier * q);

        void internalize_formula_core(app * n, bool gate_ctx);

        void set_merge_tf(enode * n, bool_var v, bool is_new_var);

        friend class set_enode_flag_trail;

    public:
        
        void set_enode_flag(bool_var v, bool is_new_var);

    protected:
        void internalize_term(app * n);

        void internalize_ite_term(app * n);

        bool internalize_theory_term(app * n);

        void internalize_uninterpreted(app * n);

        friend class mk_bool_var_trail;
        class mk_bool_var_trail : public trail {
            context& ctx;
        public:
            mk_bool_var_trail(context& ctx) :ctx(ctx) {}
            void undo() override { ctx.undo_mk_bool_var(); }
        };
        mk_bool_var_trail   m_mk_bool_var_trail;
        void undo_mk_bool_var();

        friend class mk_enode_trail;
        class mk_enode_trail : public trail {
            context& ctx;
        public:
            mk_enode_trail(context& ctx) :ctx(ctx) {}
            void undo() override { ctx.undo_mk_enode(); }
        };
        mk_enode_trail   m_mk_enode_trail;
        void undo_mk_enode();


        void apply_sort_cnstr(expr * term, enode * e);

        bool simplify_aux_clause_literals(unsigned & num_lits, literal * lits, literal_buffer & simp_lits);

        bool simplify_aux_lemma_literals(unsigned & num_lits, literal * lits);

        void mark_for_reinit(clause * cls, unsigned scope_lvl, bool reinternalize_atoms);

        unsigned get_max_iscope_lvl(unsigned num_lits, literal const * lits) const;

        bool use_binary_clause_opt(literal l1, literal l2, bool lemma) const;

        int select_learned_watch_lit(clause const * cls) const;

        int select_watch_lit(clause const * cls, int starting_at) const;

        void add_watch_literal(clause * cls, unsigned idx);

        proof * mk_clause_def_axiom(unsigned num_lits, literal * lits, expr * root_gate);

    public:
        void mk_gate_clause(unsigned num_lits, literal * lits);

        void mk_gate_clause(literal l1, literal l2);

        void mk_gate_clause(literal l1, literal l2, literal l3);

        void mk_gate_clause(literal l1, literal l2, literal l3, literal l4);

    protected:
        void mk_root_clause(unsigned num_lits, literal * lits, proof * pr);

        void mk_root_clause(literal l1, literal l2, proof * pr);

        void mk_root_clause(literal l1, literal l2, literal l3, proof * pr);

        void add_and_rel_watches(app * n);

        void add_or_rel_watches(app * n);

        void add_implies_rel_watches(app* n);

        void add_ite_rel_watches(app * n);

        void mk_not_cnstr(app * n);

        void mk_and_cnstr(app * n);

        void mk_or_cnstr(app * n);

        void mk_implies_cnstr(app* n);

        void mk_iff_cnstr(app * n, bool sign);

        void mk_ite_cnstr(app * n);

        bool track_occs() const { return m_fparams.m_phase_selection == PS_OCCURRENCE; }
        
        void dec_ref(literal l);

        void inc_ref(literal l);

        void remove_lit_occs(clause const& cls, unsigned num_bool_vars);

        void add_lit_occs(clause const& cls);

        ast_pp_util m_lemma_visitor;
        void dump_lemma(unsigned n, literal const* lits);
        void dump_axiom(unsigned n, literal const* lits);
    public:        

        void ensure_internalized(expr* e);

        void internalize(expr * n, bool gate_ctx);
        void internalize(expr* const* exprs, unsigned num_exprs, bool gate_ctx);

        void internalize(expr * n, bool gate_ctx, unsigned generation);

        enode *non_ground_internalize(expr *e);

        clause * mk_clause(unsigned num_lits, literal * lits, justification * j, clause_kind k = CLS_AUX, clause_del_eh * del_eh = nullptr);

        void mk_clause(literal l1, literal l2, justification * j);

        void mk_clause(literal l1, literal l2, literal l3, justification * j);

        void mk_th_clause(theory_id tid, unsigned num_lits, literal * lits, unsigned num_params, parameter * params, clause_kind k);

        void mk_th_axiom(theory_id tid, unsigned num_lits, literal * lits, unsigned num_params = 0, parameter * params = nullptr) {
            mk_th_clause(tid, num_lits, lits, num_params, params, CLS_TH_AXIOM);
        }

        void mk_th_axiom(theory_id tid, literal l1, unsigned num_params = 0, parameter * params = nullptr);

        void mk_th_axiom(theory_id tid, literal l1, literal l2, unsigned num_params = 0, parameter * params = nullptr);

        void mk_th_axiom(theory_id tid, literal l1, literal l2, literal l3, unsigned num_params = 0, parameter * params = nullptr);

        void mk_th_axiom(theory_id tid, literal_vector const& ls, unsigned num_params = 0, parameter * params = nullptr) {
            mk_th_axiom(tid, ls.size(), ls.data(), num_params, params);
        }

        void mk_th_lemma(theory_id tid, literal l1, literal l2, unsigned num_params = 0, parameter * params = nullptr) {
            literal ls[2] = { l1, l2 };
            mk_th_lemma(tid, 2, ls, num_params, params);
        }

        void mk_th_lemma(theory_id tid, literal l1, literal l2, literal l3, unsigned num_params = 0, parameter * params = nullptr) {
            literal ls[3] = { l1, l2, l3 };
            mk_th_lemma(tid, 3, ls, num_params, params);
        }

        void mk_th_lemma(theory_id tid, unsigned num_lits, literal * lits, unsigned num_params = 0, parameter * params = nullptr) {
            mk_th_clause(tid, num_lits, lits, num_params, params, CLS_TH_LEMMA);
        }

        void mk_th_lemma(theory_id tid, literal_vector const& ls, unsigned num_params = 0, parameter * params = nullptr) {
            mk_th_lemma(tid, ls.size(), ls.data(), num_params, params);
        }

        /*
         * Provide a hint to the core solver that the specified literals form a "theory case split".
         * The core solver will enforce the condition that exactly one of these literals can be
         * assigned 'true' at any time.
         * We assume that the theory solver has already asserted the disjunction of these literals
         * or some other axiom that means at least one of them must be assigned 'true'.
         */
        void mk_th_case_split(unsigned num_lits, literal * lits);


        /*
         * Provide a hint to the branching heuristic about the priority of a "theory-aware literal".
         * Literals marked in this way will always be branched on before unmarked literals,
         * starting with the literal having the highest priority.
         */
        void add_theory_aware_branching_info(bool_var v, double priority, lbool phase);

    public:

        // helper function for trail
        void undo_th_case_split(literal l);

        bool propagate_th_case_split(unsigned qhead);

        bool_var mk_bool_var(expr * n);

        enode * mk_enode(expr * n, bool suppress_args, bool merge_tf, bool cgc_enabled);

        void attach_th_var(enode * n, theory * th, theory_var v);

        template<typename Justification>
        justification * mk_justification(Justification const & j) {
            justification * js = new (get_region()) Justification(j);
            SASSERT(js->in_region());
            if (js->has_del_eh())
                m_justifications.push_back(js);
            return js;
        }

        // -----------------------------------
        //
        // Engine
        //
        // -----------------------------------
    protected:
        lbool              m_last_search_result { l_undef };
        failure            m_last_search_failure { UNKNOWN };
        ptr_vector<theory> m_incomplete_theories; //!< theories that failed to produce a model
        bool               m_searching { false };
        unsigned           m_num_conflicts;
        unsigned           m_num_conflicts_since_restart;
        unsigned           m_num_conflicts_since_lemma_gc;
        unsigned           m_num_restarts;
        unsigned           m_num_simplifications;
        unsigned           m_restart_threshold;
        unsigned           m_restart_outer_threshold;
        unsigned           m_luby_idx;
        double             m_agility;
        unsigned           m_lemma_gc_threshold;

        void assign_core(literal l, b_justification j, bool decision = false);
        void trace_assign(literal l, b_justification j, bool decision) const;

    public:
        void assign(literal l, const b_justification & j, bool decision = false) {
            SASSERT(l != false_literal);
            SASSERT(l != null_literal);
            switch (get_assignment(l)) {
            case l_false:
                set_conflict(j, ~l);
                break;
            case l_undef:
                assign_core(l, j, decision);
                break;
            case l_true:
                return;
            }
        }

        void assign(literal l, justification * j, bool decision = false) {
            assign(l, j ? b_justification(j) : b_justification::mk_axiom(), decision);
        }

        friend class set_true_first_trail;
        void set_true_first_flag(bool_var v);

        bool try_true_first(bool_var v) const { return get_bdata(v).try_true_first(); }

        bool assume_eq(enode * lhs, enode * rhs);

        bool is_shared(enode * n) const;

        bool is_beta_redex(enode* p, enode* n) const;

        void assign_eq(enode * lhs, enode * rhs, eq_justification const & js) {
            push_eq(lhs, rhs, js);
        }

        /**
           \brief Force the given phase next time we case split v.
           This method has no effect if phase caching is disabled.
        */
        void force_phase(bool_var v, bool phase) {
            bool_var_data & d   = get_bdata(v);
            d.m_phase_available = true;
            d.m_phase           = phase;
        }

        void force_phase(literal l) {
            force_phase(l.var(), !l.sign());
        }

        bool contains_instance(quantifier * q, unsigned num_bindings, enode * const * bindings);

        bool add_instance(quantifier * q, app * pat, unsigned num_bindings, enode * const * bindings, 
            unsigned max_generation, unsigned min_top_generation, unsigned max_top_generation, vector<std::tuple<enode *, enode*>> & used_enodes /*gives the equalities used for the pattern match, see mam.cpp for more info*/);

        void set_global_generation(unsigned generation) { m_generation = generation; }

#ifdef Z3DEBUG
        bool slow_contains_instance(quantifier const * q, unsigned num_bindings, enode * const * bindings) const {
            return m_fingerprints.slow_contains(q, q->get_id(), num_bindings, bindings);
        }
#endif

        void add_eq(enode * n1, enode * n2, eq_justification js);

    protected:
        void push_new_th_eq(theory_id th, theory_var lhs, theory_var rhs);

        void push_new_th_diseq(theory_id th, theory_var lhs, theory_var rhs);

        friend class add_eq_trail;
        friend class merge_cgc_generations_trail;

        void remove_parents_from_cg_table(enode * r1);

        void reinsert_parents_into_cg_table(enode * r1, enode * r2, enode * n1, enode * n2, eq_justification js);

        void merge_cgc_generations(enode * e1, unsigned e1_generation, enode * e2);

        void set_generation_sticky(enode * e, unsigned generation);

        void invert_trans(enode * n);

        theory_var get_closest_var(enode * n, theory_id th_id);

        void merge_theory_vars(enode * r2, enode * r1, eq_justification js);

        void propagate_bool_enode_assignment(enode * r1, enode * r2, enode * n1, enode * n2);

        void propagate_bool_enode_assignment_core(enode * source, enode * target);

        void undo_merge_cgc_generations(enode * e1, unsigned e1_generation, enode * e2, unsigned e2_generation);

        void apply_sticky_updates(enode * e1, unsigned e1_generation, enode * e2, unsigned e2_generation);

        void undo_add_eq(enode * r1, enode * n1, unsigned r2_num_parents);

        void restore_theory_vars(enode * r2, enode * r1);

        void push_eq(enode * lhs, enode * rhs, eq_justification const & js) {
            if (lhs->get_root() != rhs->get_root()) {
                SASSERT(lhs->get_sort() == rhs->get_sort());
                m_eq_propagation_queue.push_back(new_eq(lhs, rhs, js));
            }
        }

        void push_new_congruence(enode * n1, enode * n2, bool used_commutativity) {
            SASSERT(n1->m_cg == n2);
            // if (is_relevant(n1)) mark_as_relevant(n2);
            push_eq(n1, n2, eq_justification::mk_cg(used_commutativity));
        }

        bool add_diseq(enode * n1, enode * n2);

        void assign_quantifier(quantifier * q);

        void set_conflict(const b_justification & js, literal not_l);

        void set_conflict(const b_justification & js) {
            set_conflict(js, null_literal);
        }

    public:
        void set_conflict(justification * js) {
            SASSERT(js);
            set_conflict(b_justification(js));
        }

        bool inconsistent() const {
            return m_conflict != null_b_justification ||
                m_asserted_formulas.inconsistent();
        }

        bool has_case_splits();

        unsigned get_num_conflicts() const {
            return m_num_conflicts;
        }

        static bool is_eq(enode const * n1, enode const * n2) { return n1->get_root() == n2->get_root(); }

        enode * get_cg_root(enode * n) const {
            if (!n->uses_cg_table())
                return n;
            // Fast path: if e is already the congruence root, avoid table lookup.
            // This is important for performance, since get_cg_root is called on every generation lookup.
            if (n->is_cgr())
                return n;
            auto r = m_cg_table.find(n);
            SASSERT(r != nullptr);
            return r;
        }

        bool is_diseq(enode * n1, enode * n2) const;

        bool is_diseq_slow(enode * n1, enode * n2) const;

        bool is_ext_diseq(enode * n1, enode * n2, unsigned depth);

        enode * get_enode_eq_to(func_decl * f, unsigned num_args, enode * const * args);

        bool guess(bool_var var, lbool phase);

    protected:
        bool m_has_case_split = true;
        bool decide();

        void update_phase_cache_counter();

#define ACTIVITY_LIMIT 1e100
#define INV_ACTIVITY_LIMIT 1e-100

        void rescale_bool_var_activity();

    public:
        void inc_bvar_activity(bool_var v, double inc = 1.0) {
            double & act = m_activity[v];
            act += m_bvar_inc * inc;
            if (act > ACTIVITY_LIMIT)
                rescale_bool_var_activity();
            m_case_split_queue->activity_increased_eh(v);
            TRACE(case_split, tout << "v" << v << " " << m_bvar_inc << " -> " << act << "\n";);
        }

    protected:

        void decay_bvar_activity() {
            m_bvar_inc *= m_fparams.m_inv_decay;
        }

        bool simplify_clause(clause& cls);

        unsigned simplify_clauses(clause_vector & clauses, unsigned starting_at);

        void simplify_clauses();

        /**
           \brief Return true if the give clause is justifying some literal.
        */
        bool is_justifying(clause * cls) const {
            for (unsigned i = 0; i < 2; ++i) {
                b_justification js;
                js = get_justification((*cls)[i].var());
                if (js.get_kind() == b_justification::CLAUSE && js.get_clause() == cls)
                    return true;
            }
            return false;
        }

        bool can_delete(clause * cls) const {
            if (cls->in_reinit_stack())
                return false;
            return !is_justifying(cls);
        }

        void del_inactive_lemmas();

        void del_inactive_lemmas1();

        void del_inactive_lemmas2();

        bool more_than_k_unassigned_literals(clause * cls, unsigned k);


        void asserted_inconsistent();

        bool validate_assumptions(expr_ref_vector const& asms);

        void init_assumptions(expr_ref_vector const& asms);

        void init_clause(expr_ref_vector const& clause);

        lbool decide_clause();

        void reset_tmp_clauses();

        void reset_assumptions();

        void add_theory_assumptions(expr_ref_vector & theory_assumptions);

        lbool mk_unsat_core(lbool result);
        
        bool should_research(lbool result);

        void validate_unsat_core();

        void init_search();

        void end_search();

        lbool search();

        void inc_limits();

        bool restart(lbool& status, unsigned curr_lvl);

        void tick(unsigned & counter) const;

        lbool bounded_search();

        final_check_status final_check();

        void check_proof(proof * pr);

        void forget_phase_of_vars_in_current_level();

        virtual bool resolve_conflict();

        void add_scores(unsigned n, literal const *lits);


        // -----------------------------------
        //
        // Propagation
        //
        // -----------------------------------
    protected:
        bool bcp();

        bool propagate_eqs();

        bool propagate_atoms();

        void push_new_th_diseqs(enode * r, theory_var v, theory * th);

        void propagate_bool_var_enode(bool_var v);

        bool is_relevant_core(expr * n) const { return m_relevancy_propagator->is_relevant(n); }

        bool_vector  m_relevant_conflict_literals;
        void record_relevancy(unsigned n, literal const* lits);
        void restore_relevancy(unsigned n, literal const* lits);

    public:
        // event handler for relevancy_propagator class
        void relevant_eh(expr * n);

        bool is_relevant(expr * n) const {
            return !relevancy() || is_relevant_core(n);
        }

        bool is_relevant(enode * n) const {
            return is_relevant(n->get_expr());
        }

        bool is_relevant(bool_var v) const {
            return is_relevant(bool_var2expr(v));
        }

        bool is_relevant(literal l) const {
            SASSERT(l != true_literal && l != false_literal);
            return is_relevant(l.var());
        }

        bool is_relevant_core(literal l) const {
            return is_relevant_core(bool_var2expr(l.var()));
        }

        void mark_as_relevant(expr * n) { m_relevancy_propagator->mark_as_relevant(n); m_relevancy_propagator->propagate(); }

        void mark_as_relevant(enode * n) { mark_as_relevant(n->get_expr()); }

        void mark_as_relevant(bool_var v) { mark_as_relevant(bool_var2expr(v)); }

        void mark_as_relevant(literal l) { mark_as_relevant(l.var()); }

        template<typename Eh>
        relevancy_eh * mk_relevancy_eh(Eh const & eh) {
            return m_relevancy_propagator->mk_relevancy_eh(eh);
        }

        void add_relevancy_eh(expr * source, relevancy_eh * eh) { m_relevancy_propagator->add_handler(source, eh); }
        void add_relevancy_dependency(expr * source, expr * target) { m_relevancy_propagator->add_dependency(source, target); }
        void add_rel_watch(literal l, relevancy_eh * eh) { m_relevancy_propagator->add_watch(bool_var2expr(l.var()), !l.sign(), eh); }
        void add_rel_watch(literal l, expr * n) { m_relevancy_propagator->add_watch(bool_var2expr(l.var()), !l.sign(), n); }

    protected:
        lbool get_assignment_core(expr * n) const;

        void propagate_relevancy(unsigned qhead);

        bool propagate_theories();

        void propagate_th_eqs();

        void propagate_th_diseqs();

        bool can_theories_propagate() const;

        bool propagate();

        void add_rec_funs_to_model();

    public:
        bool can_propagate() const;


        // -----------------------------------
        //
        // Value extraction and solving
        //
        // -----------------------------------
    public:
        bool get_value(enode * n, expr_ref & value);

        void solve_for(vector<solution>& sol);

        // -----------------------------------
        //
        // Pretty Printing
        //
        // -----------------------------------
    protected:
        ast_mark m_pp_visited;

        ast_mark & get_pp_visited() const {  return const_cast<ast_mark&>(m_pp_visited); }

    public:
        void display_enode_defs(std::ostream & out) const;

        void display_bool_var_defs(std::ostream & out) const;

        void display_asserted_formulas(std::ostream & out) const;

        enode_pp pp(enode* n) { return enode_pp(n, *this); }

        std::ostream& display_literal(std::ostream & out, literal l) const;

        std::ostream& display_detailed_literal(std::ostream & out, literal l) const { return smt::display(out, l, m, m_bool_var2expr.data()); }

        void display_literal_info(std::ostream & out, literal l) const;

        std::ostream& display_literals(std::ostream & out, unsigned num_lits, literal const * lits) const;

        std::ostream& display_literals(std::ostream & out, literal_vector const& lits) const {
            return display_literals(out, lits.size(), lits.data());
        }

        std::ostream& display_literal_smt2(std::ostream& out, literal lit) const;

        std::ostream& display_literals_smt2(std::ostream& out, literal l1, literal l2) const { literal ls[2] = { l1, l2 }; return display_literals_smt2(out, 2, ls); }

        std::ostream& display_literals_smt2(std::ostream& out, unsigned num_lits, literal const* lits) const;

        std::ostream& display_literals_smt2(std::ostream& out, literal_vector const& ls) const { return display_literals_smt2(out, ls.size(), ls.data()); }

        std::ostream& display_literal_verbose(std::ostream & out, literal lit) const;

        std::ostream& display_literals_verbose(std::ostream & out, unsigned num_lits, literal const * lits) const;
        
        std::ostream& display_literals_verbose(std::ostream & out, literal_vector const& lits) const {
            return display_literals_verbose(out, lits.size(), lits.data());
        }

        void display_watch_list(std::ostream & out, literal l) const;

        void display_watch_lists(std::ostream & out) const;

        std::ostream& display_clause_detail(std::ostream & out, clause const * cls) const;

        std::ostream& display_clause(std::ostream & out, clause const * cls) const;

        std::ostream& display_clause_smt2(std::ostream & out, clause const& cls) const;

        std::ostream& display_clauses(std::ostream & out, ptr_vector<clause> const & v) const;

        std::ostream& display_binary_clauses(std::ostream & out) const;

        void display_assignment(std::ostream & out) const;

        void display_eqc(std::ostream & out) const;

        void display_app_enode_map(std::ostream & out) const;

        void display_expr_bool_var_map(std::ostream & out) const;

        void display_relevant_exprs(std::ostream & out) const;

        void display_theories(std::ostream & out) const;

        void display_eq_detail(std::ostream & out, enode * n) const;

        void display_parent_eqs(std::ostream & out, enode * n) const;

        void display_hot_bool_vars(std::ostream & out) const;

        void display_lemma_as_smt_problem(std::ostream & out, unsigned num_antecedents, literal const * antecedents, literal consequent = false_literal, symbol const& logic = symbol::null) const;

        unsigned display_lemma_as_smt_problem(unsigned num_antecedents, literal const * antecedents, literal consequent = false_literal, symbol const& logic = symbol::null) const;
        void display_lemma_as_smt_problem(std::ostream & out, unsigned num_antecedents, literal const * antecedents,
                                          unsigned num_antecedent_eqs, enode_pair const * antecedent_eqs,
                                          literal consequent = false_literal, symbol const& logic = symbol::null, enode* x = nullptr, enode* y = nullptr) const;

        unsigned display_lemma_as_smt_problem(unsigned num_antecedents, literal const * antecedents,
                                          unsigned num_antecedent_eqs, enode_pair const * antecedent_eqs,
                                          literal consequent = false_literal, symbol const& logic = symbol::null) const;

        std::string mk_lemma_name() const;

        void display_assignment_as_smtlib2(std::ostream& out, symbol const& logic = symbol::null) const;

        void display_normalized_enodes(std::ostream & out) const;

        void display_enodes_lbls(std::ostream & out) const;

        void display_decl2enodes(std::ostream & out) const;

        void display_subexprs_info(std::ostream & out, expr * n) const;

        void display_var_occs_histogram(std::ostream & out) const;

        void display_num_min_occs(std::ostream & out) const;

        void display_profile_res_sub(std::ostream & out) const;

        void display_profile(std::ostream & out) const;

        std::ostream& display(std::ostream& out, b_justification j) const;

        std::ostream& display_compact_j(std::ostream& out, b_justification j) const;

        // -----------------------------------
        //
        // Debugging support
        //
        // -----------------------------------
    protected:
#ifdef Z3DEBUG
        bool is_watching_clause(literal l, clause const * cls) const;

        bool check_clause(clause const * cls) const;

        bool check_clauses(clause_vector const & v) const;

        bool check_watch_list(literal l) const;

        bool check_watch_list(unsigned l_idx) const;

        bool check_bin_watch_lists() const;

        bool check_enode(enode * n) const;

        bool check_enodes() const;

        bool check_invariant() const;

        bool check_eqc_bool_assignment() const;

        bool check_missing_clause_propagation(clause_vector const & v) const;

        bool check_missing_bin_clause_propagation() const;

        bool check_missing_eq_propagation() const;

        bool check_missing_congruence() const;

        bool check_missing_bool_enode_propagation() const;

        bool check_missing_propagation() const;

        bool check_relevancy(expr_ref_vector const & v) const;

        bool check_relevancy() const;

        bool check_bool_var_vector_sizes() const;

        bool check_th_diseq_propagation() const;

        bool check_missing_diseq_conflict() const;

#endif
        // -----------------------------------
        //
        // Introspection
        //
        // -----------------------------------
        unsigned get_lemma_avg_activity() const;
        void display_literal_num_occs(std::ostream & out) const;
        void display_num_assigned_literals_per_lvl(std::ostream & out) const;

        // -----------------------------------
        //
        // Auxiliary
        //
        // -----------------------------------
        void init();
        void flush();
        config_mode get_config_mode(bool use_static_features) const;
        virtual void setup_context(bool use_static_features);
        void setup_components();
        void pop_to_base_lvl();
        void pop_to_search_lvl();
#ifdef Z3DEBUG
        bool already_internalized_theory(theory * th) const;
        bool already_internalized_theory_core(theory * th, expr_ref_vector const & s) const;
#endif
        bool check_preamble(bool reset_cancel);

        struct search_completion {
            context& ctx;
            search_completion(context& ctx) : ctx(ctx) { ctx.m_search_finalized = false; }
            ~search_completion() { if (!ctx.m_search_finalized) ctx.m_last_search_failure = CANCELED; }
        };
        bool m_search_finalized = true;
        lbool check_finalize(lbool r);

        // -----------------------------------
        //
        // API
        //
        // -----------------------------------
        void assert_expr_core(expr * e, proof * pr);

        // copy plugins into a fresh context.
        void copy_plugins(context& src, context& dst);

        /*
          \brief Utilities for consequence finding.
        */
        typedef hashtable<unsigned, u_hash, u_eq> index_set;
        //typedef uint_set index_set;
        u_map<index_set> m_antecedents;
        obj_map<expr, expr*> m_var2orig;
        u_map<expr*> m_assumption2orig;
        obj_map<expr, expr*> m_var2val;
        void extract_fixed_consequences(literal lit, index_set const& assumptions, expr_ref_vector& conseq);
        void extract_fixed_consequences(unsigned& idx, index_set const& assumptions, expr_ref_vector& conseq);

        void display_consequence_progress(std::ostream& out, unsigned it, unsigned nv, unsigned fixed, unsigned unfixed, unsigned eq);

        unsigned delete_unfixed(expr_ref_vector& unfixed);

        unsigned extract_fixed_eqs(expr_ref_vector& conseq);

        expr_ref antecedent2fml(index_set const& ante);


        literal mk_diseq(expr* v, expr* val);

        void validate_consequences(expr_ref_vector const& assumptions, expr_ref_vector const& vars,
                                   expr_ref_vector const& conseq, expr_ref_vector const& unfixed);

        bool validate_justification(bool_var v, bool_var_data const& d, b_justification const& j);

        void justify(literal lit, index_set& s);

        void extract_cores(expr_ref_vector const& asms, vector<expr_ref_vector>& cores, unsigned& min_core_size);

        void preferred_sat(literal_vector& literals);

        void display_partial_assignment(std::ostream& out, expr_ref_vector const& asms, unsigned min_core_size);

        void log_stats();

        void copy_user_propagator(context& src, bool copy_registered);

    public:
        context(ast_manager & m, smt_params & fp, params_ref const & p = params_ref());


        virtual ~context();

        /**
           \brief Return a new context containing the same theories and simplifier plugins, but with an empty
           set of assertions.

           If l == 0, then the logic of this context is used in the new context.
           If p == 0, then this->m_params is used
        */
        context * mk_fresh(symbol const * l = nullptr,  smt_params * smtp = nullptr, params_ref const & p = params_ref());

        static void copy(context& src, context& dst, bool override_base = false);

        /**
           \brief Translate context to use new manager m.
         */

        app * mk_eq_atom(expr * lhs, expr * rhs);

        bool set_logic(symbol const& logic) { return m_setup.set_logic(logic); }

        void register_plugin(theory * th);

        void add_asserted(expr* e);

        void assert_expr(expr * e);

        void assert_expr(expr * e, proof * pr);

        void internalize_assertions();

        void push();

        void pop(unsigned num_scopes);

        lbool check(unsigned num_assumptions = 0, expr * const * assumptions = nullptr, bool reset_cancel = true);

        lbool check(expr_ref_vector const& cube, vector<expr_ref_vector> const& clauses);

        lbool get_consequences(expr_ref_vector const& assumptions, expr_ref_vector const& vars, expr_ref_vector& conseq, expr_ref_vector& unfixed);

        lbool find_mutexes(expr_ref_vector const& vars, vector<expr_ref_vector>& mutexes);

        lbool preferred_sat(expr_ref_vector const& asms, vector<expr_ref_vector>& cores);

        lbool setup_and_check(bool reset_cancel = true);

        void setup_for_parallel();

        void reduce_assertions();

        bool resource_limits_exceeded();

        failure get_last_search_failure() const;

        proof * get_proof();

        conflict_resolution& get_cr() { return *m_conflict_resolution.get(); }

        void get_relevant_labels(expr* cnstr, buffer<symbol> & result);

        void get_relevant_labeled_literals(bool at_lbls, expr_ref_vector & result);

        void get_relevant_literals(expr_ref_vector & result);

        void get_guessed_literals(expr_ref_vector & result);

        void internalize_assertion(expr * n, proof * pr, unsigned generation);

        void internalize_proxies(expr_ref_vector const& asms, vector<std::pair<expr*,expr_ref>>& asm2proxy);

        void internalize_instance(expr * body, proof * pr, unsigned generation) {
            if (inconsistent())
                return;
            internalize_assertion(body, pr, generation);
            if (relevancy()) {
                // if the instantiation creates a conflict, we backtrack immediately.
                // to retain the conflict clause being relevant we mark it here.
                // if the instantiation does not create a conflict, default relevancy propagation applies.
                //if (inconsistent() && is_app(body)) 
                //    for (auto arg: *to_app(body)) mark_as_relevant(arg);
                
                m_case_split_queue->internalize_instance_eh(body, generation);
            }
        }

        unsigned get_unsat_core_size() const {
            return m_unsat_core.size();
        }

        expr * get_unsat_core_expr(unsigned idx) const {
            return m_unsat_core.get(idx);
        }

        expr_ref_vector const& unsat_core() const { return m_unsat_core; }

        void get_levels(ptr_vector<expr> const& vars, unsigned_vector& depth);

        expr_ref_vector get_trail(unsigned max_level);

        void get_model(model_ref & m);

        void set_model(model* m) { m_model = m; }

        bool update_model(bool refinalize);

        bool validate_model();

        unsigned get_num_asserted_formulas() const { return m_asserted_formulas.get_num_formulas(); }

        unsigned get_asserted_formulas_last_level() const { return m_asserted_formulas.get_formulas_last_level(); }

        expr * get_asserted_formula(unsigned idx) const { return m_asserted_formulas.get_formula(idx); }

        proof * get_asserted_formula_proof(unsigned idx) const { return m_asserted_formulas.get_formula_proof(idx); }

        void get_asserted_formulas(ptr_vector<expr>& r) const { m_asserted_formulas.get_assertions(r); }

        //proof * const * get_asserted_formula_proofs() const { return m_asserted_formulas.get_formula_proofs(); }

        void get_assertions(ptr_vector<expr> & result) { m_asserted_formulas.get_assertions(result); }

        void get_units(expr_ref_vector& result);

        bool clause_proof_active() const { return m_clause_proof.is_enabled(); }

        clause_proof& get_clause_proof() { return m_clause_proof; }

        void register_on_clause(void* ctx, user_propagator::on_clause_eh_t& on_clause) {
            m_clause_proof.register_on_clause(ctx, on_clause);
        }

        /*
         * user-propagator
         */
        void user_propagate_init(
            void*                 ctx, 
            user_propagator::push_eh_t&    push_eh,
            user_propagator::pop_eh_t&     pop_eh,
            user_propagator::fresh_eh_t&   fresh_eh);

        void user_propagate_register_final(user_propagator::final_eh_t& final_eh) {
            if (!m_user_propagator) 
                throw default_exception("user propagator must be initialized");
            m_user_propagator->register_final(final_eh);
        }

        void user_propagate_register_fixed(user_propagator::fixed_eh_t& fixed_eh) {
            if (!m_user_propagator) 
                throw default_exception("user propagator must be initialized");
            m_user_propagator->register_fixed(fixed_eh);
        }
        
        void user_propagate_register_eq(user_propagator::eq_eh_t& eq_eh) {
            if (!m_user_propagator) 
                throw default_exception("user propagator must be initialized");
            m_user_propagator->register_eq(eq_eh);
        }
        
        void user_propagate_register_diseq(user_propagator::eq_eh_t& diseq_eh) {
            if (!m_user_propagator) 
                throw default_exception("user propagator must be initialized");
            m_user_propagator->register_diseq(diseq_eh);
        }

        void user_propagate_register_expr(expr* e) {
            if (!m_user_propagator) 
                throw default_exception("user propagator must be initialized");
            m_user_propagator->add_expr(e, true);
        }

        void user_propagate_register_created(user_propagator::created_eh_t& r) {
            if (!m_user_propagator)
                throw default_exception("user propagator must be initialized");
            m_user_propagator->register_created(r);
        }

        void user_propagate_register_decide(user_propagator::decide_eh_t& r) {
            if (!m_user_propagator)
                throw default_exception("user propagator must be initialized");
            m_user_propagator->register_decide(r);
        }

        void user_propagate_register_on_binding(user_propagator::binding_eh_t& t) {
            m_user_propagator->register_on_binding(t);
        }

        void register_on_binding(std::function<bool(quantifier* q, expr* inst)>& f) {
            m_qmanager->register_on_binding(f);
        }

        void user_propagate_initialize_value(expr* var, expr* value);

        bool watches_fixed(enode* n) const;

        bool has_split_candidate(bool_var& var, bool& is_pos);
        
        bool decide_user_interference(bool_var& var, bool& is_pos);

        void assign_fixed(enode* n, expr* val, unsigned sz, literal const* explain);

        void assign_fixed(enode* n, expr* val, literal_vector const& explain) {
            assign_fixed(n, val, explain.size(), explain.data());
        }

        void assign_fixed(enode* n, expr* val, literal explain) {
            assign_fixed(n, val, 1, &explain);
        }

        bool is_fixed(enode* n, expr_ref& val, literal_vector& explain);

        void display(std::ostream & out) const;

        void display_unsat_core(std::ostream & out) const;

        void collect_statistics(::statistics & st) const;

        void display_statistics(std::ostream & out) const;
        void display_istatistics(std::ostream & out) const;

        // -----------------------------------
        //
        // Macros
        //
        // -----------------------------------
    public:
        unsigned get_num_macros() const { return m_asserted_formulas.get_num_macros(); }
        unsigned get_first_macro_last_level() const { return m_asserted_formulas.get_first_macro_last_level(); }
        func_decl * get_macro_func_decl(unsigned i) const { return m_asserted_formulas.get_macro_func_decl(i); }
        func_decl * get_macro_interpretation(unsigned i, expr_ref & interp) const { return m_asserted_formulas.get_macro_interpretation(i, interp); }
        quantifier * get_macro_quantifier(func_decl * f) const { return m_asserted_formulas.get_macro_quantifier(f); }
        void insert_macro(func_decl * f, quantifier * m, proof * pr, expr_dependency * dep) { m_asserted_formulas.insert_macro(f, m, pr, dep); }
    };

    struct pp_lit {
        context & ctx;
        literal lit;
        pp_lit(context & ctx, literal lit) : ctx(ctx), lit(lit) {}
    };

    inline std::ostream & operator<<(std::ostream & out, pp_lit const & pp) {
        return pp.ctx.display_detailed_literal(out, pp.lit);
    }

    struct pp_lits {
        context & ctx;
        literal const *lits;
        unsigned len;
        pp_lits(context & ctx, unsigned len, literal const *lits) : ctx(ctx), lits(lits), len(len) {}
        pp_lits(context & ctx, literal_vector const& ls) : ctx(ctx), lits(ls.data()), len(ls.size()) {}
    };

    inline std::ostream & operator<<(std::ostream & out, pp_lits const & pp) {
        out << "{";
        bool first = true;
        for (unsigned i = 0; i < pp.len; ++i) {
            if (first) { first = false; } else { out << " or\n"; }
            pp.ctx.display_detailed_literal(out, pp.lits[i]);
        }
        return out << "}";

    }
    struct enode_eq_pp {
        context const&          ctx;
        enode_pair const& p;
        enode_eq_pp(enode_pair const& p, context const& ctx): ctx(ctx), p(p) {}        
    };

    std::ostream& operator<<(std::ostream& out, enode_eq_pp const& p);


    std::ostream& operator<<(std::ostream& out, enode_pp const& p);

}