z3-src 500.0.0

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
/*++
Copyright (c) 2012 Microsoft Corporation

Module Name:

    probe_arith.cpp

Abstract:

    Some probes for arithmetic problems.

Author:

    Leonardo de Moura (leonardo) 2012-03-01.

Revision History:

--*/
#include "tactic/probe.h"
#include "ast/expr2polynomial.h"
#include "ast/for_each_expr.h"
#include "ast/arith_decl_plugin.h"
#include "tactic/goal_util.h"

namespace {

class arith_degree_probe : public probe {
    struct proc {
        ast_manager &            m;
        unsynch_mpq_manager      m_qm;
        polynomial::manager      m_pm;
        default_expr2polynomial  m_expr2poly;
        arith_util               m_util;
        unsigned                 m_max_degree;
        unsigned long long       m_acc_degree;
        unsigned                 m_counter;

        proc(ast_manager & _m):m(_m), m_pm(m.limit(), m_qm), m_expr2poly(m, m_pm), m_util(m) {
            m_max_degree = 0;
            m_acc_degree = 0;
            m_counter    = 0;
        }

        void updt_degree(polynomial_ref const & p) {
            unsigned deg = m_pm.total_degree(p);
            if (deg > m_max_degree)
                m_max_degree = deg;
            m_acc_degree += deg;
            m_counter++;
        }
        
        void process(app * n) {
            expr * lhs = n->get_arg(0);
            expr * rhs = n->get_arg(1);
            polynomial_ref p1(m_pm);
            polynomial_ref p2(m_pm);
            scoped_mpz d1(m_qm);
            scoped_mpz d2(m_qm);
            m_expr2poly.to_polynomial(lhs, p1, d1);
            m_expr2poly.to_polynomial(rhs, p2, d2);
            updt_degree(p1);
            updt_degree(p2);
        }

        void operator()(var * n) {}
        void operator()(quantifier * n) {}
        void operator()(app * n) {
            if (m_util.is_le(n) || m_util.is_lt(n) || m_util.is_gt(n) || m_util.is_ge(n))
                process(n);
            if (m.is_eq(n) && m_util.is_int_real(n->get_arg(0)))
                process(n);
        }
    };

    bool m_avg;
public:
    arith_degree_probe(bool avg):m_avg(avg) {}

    result operator()(goal const & g) override {
        proc p(g.m());
        for_each_expr_at(p, g);
        if (m_avg)
            return p.m_counter == 0 ? 0.0 : static_cast<double>(p.m_acc_degree)/static_cast<double>(p.m_counter);
        else
            return p.m_max_degree;
    }
};

class arith_bw_probe : public probe {
    struct proc {
        ast_manager &            m;
        arith_util               m_util;
        unsigned                 m_max_bw;
        unsigned long long       m_acc_bw;
        unsigned                 m_counter;

        proc(ast_manager & _m):m(_m), m_util(m) {
            m_max_bw  = 0;
            m_acc_bw  = 0;
            m_counter = 0;
        }
        
        void operator()(var * n) {}
        void operator()(quantifier * n) {}
        void operator()(app * n) {
            rational val;
            if (m_util.is_numeral(n, val)) {
                unsigned bw = val.bitsize();
                if (bw > m_max_bw)
                    m_max_bw = bw;
                m_acc_bw += bw;
                m_counter++;
            }
        }

    };

    bool m_avg;
public:
    arith_bw_probe(bool avg):m_avg(avg) {}
        
    result operator()(goal const & g) override {
        proc p(g.m());
        for_each_expr_at(p, g);
        if (m_avg)
            return p.m_counter == 0 ? 0.0 : static_cast<double>(p.m_acc_bw)/static_cast<double>(p.m_counter);
        else
            return p.m_max_bw;
    }
};

struct has_nlmul {
    struct found : public std::exception {};
    ast_manager& m;
    arith_util   a;
    has_nlmul(ast_manager& m):m(m), a(m) {}
    
    void throw_found(expr* e) {
        TRACE(probe, tout << expr_ref(e, m) << ": " << sort_ref(e->get_sort(), m) << "\n";);
        throw found();
    }

    void operator()(var *) { }

    void operator()(quantifier *) { }

    void operator()(app * n) {
        family_id fid = n->get_family_id();
        if (fid == a.get_family_id()) {
            switch (n->get_decl_kind()) {
            case OP_MUL:
                if (n->get_num_args() != 2 || !a.is_numeral(n->get_arg(0)))
                    throw_found(n);
                break;
            case OP_IDIV: case OP_DIV: case OP_REM: case OP_MOD:
                if (!a.is_numeral(n->get_arg(1)))
                    throw_found(n);
                break;
            case OP_POWER:
                throw_found(n);
            default:
                break;
            }
        }
    }
};

}

probe * mk_arith_avg_degree_probe() {
    return alloc(arith_degree_probe, true);
}

probe * mk_arith_max_degree_probe() {
    return alloc(arith_degree_probe, false);
}

probe * mk_arith_avg_bw_probe() {
    return alloc(arith_bw_probe, true);
}

probe * mk_arith_max_bw_probe() {
    return alloc(arith_bw_probe, false);
}

namespace {
struct is_non_qflira_functor {
    struct found : public std::exception {};
    ast_manager & m;
    arith_util    u;
    bool          m_int;
    bool          m_real;

    is_non_qflira_functor(ast_manager & _m, bool _int, bool _real):m(_m), u(m), m_int(_int), m_real(_real) {}

    void operator()(var *) { throw found();  }
    
    void operator()(quantifier *) { throw found(); }
    
    bool compatible_sort(app * n) const {
        if (m.is_bool(n))
            return true;
        if (m_int && u.is_int(n))
            return true;
        if (m_real && u.is_real(n))
            return true;
        return false;
    }

    void operator()(app * n) {
        if (!compatible_sort(n))
            throw found();
        family_id fid = n->get_family_id();
        if (fid == m.get_basic_family_id())
            return; 
        if (fid == u.get_family_id()) {
            switch (n->get_decl_kind()) {
            case OP_LE:  case OP_GE: case OP_LT: case OP_GT:
            case OP_ADD: case OP_NUM:
                return;
            case OP_MUL:
                if (n->get_num_args() != 2)
                    throw found();
                if (!u.is_numeral(n->get_arg(0)))
                    throw found();
                return;
            case OP_TO_REAL:
                if (!m_real)
                    throw found();
                break;
            default:
                throw found();
            }
            return;
        }
        if (is_uninterp_const(n))
            return;
        throw found();
    }
};

struct is_non_qfauflira_functor {
    struct found : public std::exception {};
    ast_manager & m;
    arith_util    m_arith_util;
    array_util    m_array_util;
    bool          m_int;
    bool          m_real;

    is_non_qfauflira_functor(ast_manager & _m, bool _int, bool _real) : 
        m(_m), m_arith_util(_m), m_array_util(_m), m_int(_int), m_real(_real) {}

    void operator()(var *) { throw found(); }

    void operator()(quantifier *) { throw found(); }

    bool compatible_sort(app * n) const {
        if (m.is_bool(n))
            return true;
        if (m_int && m_arith_util.is_int(n))
            return true;
        if (m_real && m_arith_util.is_real(n))
            return true;
        if (m_array_util.is_array(n))
            return true;
        return false;
    }

    void operator()(app * n) {
        if (!compatible_sort(n))
            throw found();
        family_id fid = n->get_family_id();
        if (fid == m.get_basic_family_id())
            return;
        if (fid == m_arith_util.get_family_id()) {
            switch (n->get_decl_kind()) {
            case OP_LE:  case OP_GE: case OP_LT: case OP_GT:
            case OP_ADD: case OP_NUM:
                return;
            case OP_MUL:
                if (n->get_num_args() != 2)
                    throw found();
                if (!m_arith_util.is_numeral(n->get_arg(0)))
                    throw found();
                return;
            case OP_TO_REAL:
                if (!m_real)
                    throw found();
                break;
            default:
                throw found();
            }
            return;
        }
        if (is_uninterp(n))
            return;
        throw found();
    }
};

static bool is_qflia(goal const & g) {
    is_non_qflira_functor p(g.m(), true, false);
    return !test(g, p);
}

static bool is_qfauflia(goal const & g) {
    is_non_qfauflira_functor p(g.m(), true, false);
    return !test(g, p);
}

class is_qflia_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qflia(g);
    }
};

class is_qfauflia_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qfauflia(g);
    }
};

static bool is_qflra(goal const & g) {
    is_non_qflira_functor p(g.m(), false, true);
    return !test(g, p);
}

class is_qflra_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qflra(g);
    }
};

static bool is_qflira(goal const & g) {
    is_non_qflira_functor p(g.m(), true, true);
    return !test(g, p);
}

class is_qflira_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qflira(g);
    }
};

static bool is_lp(goal const & g) {
    ast_manager & m = g.m();
    arith_util u(m);
    unsigned sz = g.size();
    for (unsigned i = 0; i < sz; ++i) {
        expr * f  = g.form(i);
        bool sign = false;
        while (m.is_not(f, f))
            sign = !sign;
        if (m.is_eq(f) && !sign) {
            if (to_app(f)->get_arg(0)->get_sort()->get_family_id() != u.get_family_id())
                return false;
            continue;
        }
        if (u.is_le(f) || u.is_ge(f) || u.is_lt(f) || u.is_gt(f))
            continue;
        return false;
    }
    return true;
}

static bool is_ilp(goal const & g) {
    if (!is_qflia(g))
        return false;
    if (has_term_ite(g))
        return false;
    return is_lp(g);
}

static bool is_mip(goal const & g) {
    if (!is_qflira(g))
        return false;
    if (has_term_ite(g))
        return false;
    return is_lp(g);
}

class is_ilp_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_ilp(g);
    }
};

class is_mip_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_mip(g);
    }
};

}

probe * mk_is_qflia_probe() {
    return alloc(is_qflia_probe);
}

probe * mk_is_qfauflia_probe() {
    return alloc(is_qfauflia_probe);
}

probe * mk_is_qflra_probe() {
    return alloc(is_qflra_probe);
}

probe * mk_is_qflira_probe() {
    return alloc(is_qflira_probe);
}

probe * mk_is_ilp_probe() {
    return alloc(is_ilp_probe);
}

probe * mk_is_mip_probe() {
    return alloc(is_mip_probe);
}


namespace {

struct is_non_nira_functor {
    struct found : public std::exception {};
    ast_manager & m;
    arith_util    u;
    bool          m_int;
    bool          m_real;
    bool          m_quant;
    bool          m_linear;

    is_non_nira_functor(ast_manager & _m, bool _int, bool _real, bool _quant, bool linear):m(_m), u(m), m_int(_int), m_real(_real), m_quant(_quant), m_linear(linear) {}

    void throw_found(expr* e) {
        TRACE(probe, tout << expr_ref(e, m) << ": " << sort_ref(e->get_sort(), m) << "\n";);
        throw found();
    }

    void operator()(var * x) {
        if (!m_quant)
            throw_found(x);
        sort * s = x->get_sort();
        if (m_int && u.is_int(s))
            return;
        if (m_real && u.is_real(s))
            return;
        if (m.is_bool(s))
            return;
        throw_found(x);
    }
    
    void operator()(quantifier * q) { 
        if (!m_quant)
            throw_found(q); 
    }
    
    bool compatible_sort(app * n) const {
        if (m.is_bool(n))
            return true;
        if (m_int && u.is_int(n))
            return true;
        if (m_real && u.is_real(n))
            return true;
        return false;
    }

    void operator()(app * n) {
        if (!compatible_sort(n))
            throw_found(n);
        family_id fid = n->get_family_id();
        rational r;
        if (fid == m.get_basic_family_id())
            return; 
        if (fid == u.get_family_id()) {
            switch (n->get_decl_kind()) {
            case OP_LE:  case OP_GE: case OP_LT: case OP_GT:
            case OP_ADD: case OP_UMINUS: case OP_SUB: case OP_ABS: 
            case OP_NUM:
                return;
            case OP_MUL:
                if (m_linear) {
                    if (n->get_num_args() != 2)
                        throw_found(n);
                    if (!u.is_numeral(n->get_arg(0)) && !u.is_numeral(n->get_arg(1))) 
                        throw_found(n);
                }
                return;
            case OP_IDIV: case OP_DIV: case OP_REM: case OP_MOD:
                if (m_linear && !u.is_numeral(n->get_arg(1)))
                    throw_found(n); 
                if (m_linear && u.is_numeral(n->get_arg(1), r) && r.is_zero())
                    throw_found(n); 
                if (m_linear && u.is_numeral(n->get_arg(1), r) && !r.is_zero())
                    return;
                if (!is_ground(n->get_arg(0)) || !is_ground(n->get_arg(1))) 
                    throw_found(n);
                return;
            case OP_IS_INT:
                if (m_real)
                    throw_found(n);
                return;
            case OP_TO_INT:
            case OP_TO_REAL:
                return;
            case OP_POWER:
                if (m_linear)
                    throw_found(n);
                //if (!u.is_numeral(n->get_arg(0), r) || !r.is_unsigned() || r.is_zero())
                //    throw_found(n);
                return;
            case OP_IRRATIONAL_ALGEBRAIC_NUM:
                if (m_linear || !m_real)
                    throw_found(n);
                return;
            default:
                throw_found(n);
            }
            return;
        }

        if (is_uninterp_const(n))
            return;
        throw_found(n);
    }
};

static bool is_qfnia(goal const & g) {
    is_non_nira_functor p(g.m(), true, false, false, false);
    return !test(g, p) && test<has_nlmul>(g);
}

static bool is_qfnra(goal const & g) {
    is_non_nira_functor p(g.m(), false, true, false, false);
    return !test(g, p) && test<has_nlmul>(g);
}

static bool is_nia(goal const & g) {
    is_non_nira_functor p(g.m(), true, false, true, false);
    return !test(g, p) && test<has_nlmul>(g);
}

static bool is_nra(goal const & g) {
    is_non_nira_functor p(g.m(), false, true, true, false);
    return !test(g, p) && test<has_nlmul>(g);
}

static bool is_nira(goal const & g) {
    is_non_nira_functor p(g.m(), true, true, true, false);
    return !test(g, p) && test<has_nlmul>(g);
}

static bool is_lra(goal const & g) {
    is_non_nira_functor p(g.m(), false, true, true, true);
    return !test(g, p);
}

static bool is_lia(goal const & g) {
    is_non_nira_functor p(g.m(), true, false, true, true);
    return !test(g, p);
}

static bool is_lira(goal const & g) {
    is_non_nira_functor p(g.m(), true, true, true, true);
    return !test(g, p);
}


struct is_non_qfufnra_functor {
    struct found : public std::exception {};
    ast_manager & m;
    arith_util    u;
    bool          m_has_nonlinear;

    is_non_qfufnra_functor(ast_manager & _m): 
        m(_m), u(m), m_has_nonlinear(false) {}

    void throw_found() {
        throw found();
    }

    bool has_nonlinear() const {
        return m_has_nonlinear;
    }

    void operator()(var * x) {
        throw_found();
    }    
    void operator()(quantifier *) { 
        throw_found(); 
    }    
    void operator()(app * n) {
        family_id fid = n->get_family_id();
        if (fid == m.get_basic_family_id())
            return; 
        if (fid == u.get_family_id()) {
            switch (n->get_decl_kind()) {
            case OP_LE:  case OP_GE: case OP_LT: case OP_GT:
            case OP_ADD: case OP_UMINUS: case OP_SUB: case OP_ABS: 
            case OP_NUM: 
            case OP_IRRATIONAL_ALGEBRAIC_NUM:
                return;
            case OP_MUL:
                if (n->get_num_args() == 2 &&
                    u.is_real(n->get_arg(0)) && 
                    !u.is_numeral(n->get_arg(0)) &&
                    !u.is_numeral(n->get_arg(1))) {
                    m_has_nonlinear = true;
                }
                return;
            case OP_IDIV: case OP_DIV: case OP_REM: case OP_MOD:
                if (!u.is_numeral(n->get_arg(1))) {
                    TRACE(arith, tout << "non-linear " << expr_ref(n, m) << "\n";);
                    throw_found();
                }
                return;
            case OP_POWER: 
                if (!u.is_numeral(n->get_arg(1))) {
                    TRACE(arith, tout << "non-linear " << expr_ref(n, m) << "\n";);
                    throw_found();
                }
                m_has_nonlinear = true;
                return;
            case OP_IS_INT:
            case OP_TO_INT:
            case OP_TO_REAL:
                throw_found();
                return;
            default:
                TRACE(arith, tout << "non-linear " << expr_ref(n, m) << "\n";);
                throw_found();
            }
        } 
    }
};


class is_qfnia_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qfnia(g);
    }
};

class is_qfnra_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qfnra(g);
    }
};

class is_nia_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_nia(g);
    }
};

class is_nra_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_nra(g);
    }
};

class is_nira_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_nira(g);
    }
};

class is_lia_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_lia(g);
    }
};

class is_lra_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_lra(g);
    }
};

class is_lira_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_lira(g);
    }
};

static bool is_qfufnra(goal const& g) {
    is_non_qfufnra_functor p(g.m());
    return !g.proofs_enabled() && !g.unsat_core_enabled() && !test(g, p) && p.has_nonlinear();
}

class is_qfufnra_probe : public probe {
public:
    result operator()(goal const & g) override {
        return is_qfufnra(g);
    }
};

}

probe * mk_is_qfnia_probe() {
    return alloc(is_qfnia_probe);
}

probe * mk_is_qfnra_probe() {
    return alloc(is_qfnra_probe);
}

probe * mk_is_nia_probe() {
    return alloc(is_nia_probe);
}

probe * mk_is_nra_probe() {
    return alloc(is_nra_probe);
}

probe * mk_is_nira_probe() {
    return alloc(is_nira_probe);
}

probe * mk_is_lia_probe() {
    return alloc(is_lia_probe);
}

probe * mk_is_lra_probe() {
    return alloc(is_lra_probe);
}

probe * mk_is_lira_probe() {
    return alloc(is_lira_probe);
}

probe* mk_is_qfufnra_probe() {
    return alloc(is_qfufnra_probe);
}