z3-src 415.4.3

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

Module Name:

    check_logic.cpp

Abstract:

    Check whether a given assertion is in the correct logic or not

Author:

    Leonardo de Moura (leonardo) 2011-08-11.

Revision History:

--*/
#include "solver/check_logic.h"
#include "solver/smt_logics.h"
#include "ast/arith_decl_plugin.h"
#include "ast/array_decl_plugin.h"
#include "ast/bv_decl_plugin.h"
#include "ast/seq_decl_plugin.h"
#include "ast/pb_decl_plugin.h"
#include "ast/datatype_decl_plugin.h"
#include "ast/ast_pp.h"
#include "ast/for_each_expr.h"
#include<sstream>

struct check_logic::imp {
    ast_manager & m;
    symbol        m_logic;
    arith_util    m_a_util;
    bv_util       m_bv_util;
    array_util    m_ar_util;
    seq_util      m_seq_util;
    datatype_util m_dt_util;
    pb_util       m_pb_util;
    bool          m_uf;        // true if the logic supports uninterpreted functions
    bool          m_dt;        // true if the logic supports dattypes
    bool          m_arrays;    // true if the logic supports arbitrary arrays
    bool          m_bv_arrays; // true if the logic supports only bv arrays
    bool          m_reals;     // true if the logic supports reals
    bool          m_ints;      // true if the logic supports integers
    bool          m_diff;      // true if the logic supports difference logic only
    bool          m_nonlinear; // true if the logic supports nonlinear arithmetic
    bool          m_bvs;       // true if the logic supports bit-vectors
    bool          m_quantifiers; // true if the logic supports quantifiers
    bool          m_unknown_logic;

    imp(ast_manager & _m):m(_m), m_a_util(m), m_bv_util(m), m_ar_util(m), m_seq_util(m), m_dt_util(m), m_pb_util(m) {
        reset();
    }

    void reset() {
        m_uf          = false;
        m_dt          = false;
        m_arrays      = false;
        m_bv_arrays   = false;
        m_reals       = false;
        m_ints        = false;
        m_diff        = false;
        m_nonlinear   = false;
        m_bvs         = false;
        m_quantifiers = false;
        m_unknown_logic = true;
    }

    void set_logic(symbol const & logic) {
        reset();
        m_unknown_logic = false;
        if (logic == "AUFLIA") {
            m_uf     = true;
            m_arrays = true;
            m_ints   = true;
            m_quantifiers = true;
        }
        else if (logic == "AUFLIRA") {
            m_uf     = true;
            m_arrays = true;
            m_reals  = true;
            m_ints   = true;
            m_quantifiers = true;
        }
        else if (logic == "AUFNIRA") {
            m_uf     = true;
            m_arrays = true;
            m_reals  = true;
            m_ints   = true;
            m_nonlinear   = true;
            m_quantifiers = true;
        }
        else if (logic == "LRA") {
            m_reals       = true;
            m_quantifiers = true;
        }
        else if (logic == "QF_ABV") {
            m_bv_arrays = true;
            m_bvs       = true;
        }
        else if (logic == "QF_AUFBV") {
            m_uf        = true;
            m_bv_arrays = true;
            m_bvs       = true;
        }
        else if (logic == "QF_UFBV") {
            m_uf        = true;
            m_bvs       = true;
        }
        else if (logic == "QF_UFDT") {
            m_uf        = true;            
            m_dt        = true;
        }
        else if (logic == "QF_DT") {
            m_dt        = true;
        }
        else if (logic == "QF_AUFLIA") {
            m_uf     = true;
            m_arrays = true;
            m_ints   = true;
        }
        else if (logic == "QF_AX") {
            m_arrays = true;
        }
        else if (logic == "QF_BV") {
            m_bvs    = true;
        }
        else if (logic == "QF_IDL") {
            m_ints   = true;
            m_diff   = true;
        }
        else if (logic == "QF_RDL") {
            m_reals  = true;
            m_diff   = true;
        }
        else if (logic == "QF_LIA") {
            m_ints   = true;
        }
        else if (logic == "QF_LRA") {
            m_reals   = true;
        }
        else if (logic == "QF_NIA") {
            m_ints   = true;
            m_nonlinear = true;
        }
        else if (logic == "QF_NRA") {
            m_reals     = true;
            m_nonlinear = true;
        }
        else if (logic == "QF_UF") {
            m_uf = true;
        }
        else if (logic == "QF_UFIDL") {
            m_uf     = true;
            m_ints   = true;
            m_diff   = true;
        }
        else if (logic == "QF_UFLIA") {
            m_uf     = true;
            m_ints   = true;
        }
        else if (logic == "QF_UFLRA") {
            m_uf     = true;
            m_reals  = true;
        }
        else if (logic == "QF_UFNRA") {
            m_uf        = true;
            m_reals     = true;
            m_nonlinear = true;
        }
        else if (logic == "UFLRA") {
            m_uf          = true;
            m_reals       = true;
            m_quantifiers = true;
        }
        else if (logic == "UFNIA") {
            m_uf          = true;
            m_ints        = true;
            m_quantifiers = true;
            m_nonlinear   = true;
        }
        else if (logic == "UFBV") {
            m_uf          = true;
            m_bvs         = true;
            m_quantifiers = true;
        }
        else if (logic == "QF_S" || logic == "QF_SLIA") {
            m_uf          = true;
            m_bvs         = true;
            m_ints        = true;
            m_arrays      = true;
            m_reals       = true;
            m_quantifiers = true; // some QF_SLIA benchmarks are miss-classified
        }
        else if (logic == "QF_FD") {
            m_bvs         = true;
            m_uf          = true;
            m_ints        = true;
            m_dt          = true;
            m_nonlinear   = true; // non-linear 0-1 variables may get eliminated
        }
        else if (logic == "SMTFD") {
            m_bvs         = true;
            m_uf          = true;
            m_arrays      = true;
            m_ints        = false;
            m_dt          = false;
            m_nonlinear   = false; 
        }
        else {
            m_unknown_logic = true;
        }

        m_logic = logic;
    }

    struct failed : public std::exception {};
    std::string m_last_error;

    void fail(std::string &&msg) {
        m_last_error = std::move(msg);
        throw failed();
    }

    void fail(char const * msg) {
        m_last_error = msg;
        throw failed();
    }

    void check_sort(sort * s) {
        if (s->get_family_id() == null_family_id) {
            if (!m_uf)
                fail("logic does not support uninterpreted sorts");
        }
        else if (m.is_bool(s)) {
            return;
        }
        else if (m_a_util.is_int(s)) {
            if (!m_ints)
                fail("logic does not support integers");
        }
        else if (m_a_util.is_real(s)) {
            if (!m_reals)
                fail("logic does not support reals");
        }
        else if (m_bv_util.is_bv_sort(s)) {
            if (!m_bvs)
                fail("logic does not support bitvectors");
        }
        else if (m_dt_util.is_datatype(s)) {
            if (!m_dt) 
                fail("logic does not support algebraic datatypes");
        }
        else if (m_ar_util.is_array(s)) {
            if (m_arrays) {
                return;
            }
            else if (m_bv_arrays) {
                unsigned sz = get_array_arity(s);
                for (unsigned i = 0; i < sz; ++i) {
                    if (!m_bv_util.is_bv_sort(get_array_domain(s, i)))
                        fail("logic supports only arrays from bitvectors to bitvectors");
                }
                check_sort(get_array_range(s));
            }
            else {
                fail("logic does not support arrays");
            }
        }
    }

    void operator()(var * n) {
        if (!m_quantifiers)
            fail("logic does not support quantifiers");
        check_sort(n->get_sort());
    }

    bool is_int(expr * t) {
        if (m_a_util.is_uminus(t))
            t = to_app(t)->get_arg(0);
        // Take care of coercions automatically added by Z3
        if (m_a_util.is_to_real(t))
            t = to_app(t)->get_arg(0);
        return m_a_util.is_numeral(t);
    }

    bool is_numeral(expr * t) {
        if (m_a_util.is_uminus(t))
            t = to_app(t)->get_arg(0);
        // c
        if (is_int(t))
            return true;
        // c1/c2
        if (m_a_util.is_div(t) && is_int(to_app(t)->get_arg(0)) && is_int(to_app(t)->get_arg(1)))
            return true;
        return false;
    }

    // check if n has at most one argument that is not numeral.
    void check_mul(app * n) {
        if (m_nonlinear)
            return; // nothing to check
        unsigned num_args = n->get_num_args();
        bool found_non_numeral = false;
        for (unsigned i = 0; i < num_args; ++i) {
            if (!is_numeral(n->get_arg(i))) {
                if (found_non_numeral)
                    fail("logic does not support nonlinear arithmetic");
                else
                    found_non_numeral = true;
            }
        }
    }

    // check if the divisor is a numeral
    void check_div(app * n) {        
        if (n->get_num_args() != 2 || (!m_nonlinear && !is_numeral(n->get_arg(1))))
            fail("logic does not support nonlinear arithmetic");
    }

    bool is_diff_var(expr * t) const {
        if (is_app(t) && to_app(t)->get_decl()->get_family_id() == null_family_id)
            return true;
        if (m.is_ite(t))
            return true;
        return false;
    }

    void fail_non_diff(expr * t) {
        TRACE(check_logic, tout << mk_pp(t, m) << "\n";);
        fail("logic only supports difference arithmetic");
    }

    bool same_args(app * t) {
        unsigned num_args = t->get_num_args();
        if (num_args == 0)
            return false;
        expr * arg = t->get_arg(0);
        for (unsigned i = 1; i < num_args; ++i) {
            if (t->get_arg(i) != arg)
                return false;
        }
        return true;
    }

    bool is_arith(expr * t) const {
        return t->get_sort()->get_family_id() == m_a_util.get_family_id();
    }

    bool is_offset(app * t) {
        while (true) {
            expr * non_numeral = nullptr;
            unsigned num_args = t->get_num_args();
            for (unsigned i = 0; i < num_args; ++i) {
                expr * arg = t->get_arg(i);
                if (is_numeral(arg))
                    continue;
                if (non_numeral != nullptr)
                    return false;
                non_numeral = arg;
            }
            if (non_numeral == nullptr)
                return true;
            if (is_diff_var(non_numeral))
                return true;
            if (!m_a_util.is_add(non_numeral) && !m_a_util.is_sub(non_numeral))
                return false;
            t = to_app(non_numeral);
        }
        return true;
    }

    bool is_diff_arg(expr * t) {
        if (is_diff_var(t))
            return true;
        if (is_numeral(t))
            return true;
        if (m_a_util.is_add(t) || m_a_util.is_sub(t))
            return is_offset(to_app(t));
        return false;
    }

    // Check if n is a diff logic predicate
    void check_diff_predicate(app * n) {
        expr * lhs = n->get_arg(0);
        expr * rhs = n->get_arg(1);
        if (!is_arith(lhs))
            return; // formula is not in arithmetic
        if (is_diff_arg(lhs) && is_diff_arg(rhs))
            return;
        if (is_numeral(lhs))
            std::swap(lhs, rhs);
        if (!is_numeral(rhs))
            fail_non_diff(n);
        if (!m_a_util.is_sub(lhs) || to_app(lhs)->get_num_args() != 2)
            fail_non_diff(n);
        expr * t1 = to_app(lhs)->get_arg(0);
        expr * t2 = to_app(lhs)->get_arg(1);
        if (is_diff_var(t1) && is_diff_var(t2))
            return;
        if (m_a_util.is_add(t1) && m_a_util.is_add(t2)) {
            // QF_RDL supports (<= (- (+ x ... x) (+ y ... y)) c)
            if (to_app(t1)->get_num_args() != to_app(t2)->get_num_args())
                fail_non_diff(n);
            if (!same_args(to_app(t1)) || !same_args(to_app(t2)))
                fail_non_diff(n);
            return;
        }
        fail_non_diff(n);
    }

    void check_diff_arg(expr * t) {
        if (!is_diff_arg(t))
            fail_non_diff(t);
    }

    // Check if the arith args of n are of the form (t + k) where k is a numeral.
    void check_diff_args(app * n) {
        unsigned num_args = n->get_num_args();
        for (unsigned i = 0; i < num_args; ++i) {
            if (is_arith(n))
                check_diff_arg(n);
        }
    }

    void operator()(app * n) {
        sort * s = n->get_sort();
        check_sort(s);
        func_decl * f = n->get_decl();
        family_id fid = f->get_family_id();
        if (fid == null_family_id) {
            if (!m_uf && f->get_arity() > 0)
                fail("logic does not support uninterpreted functions");
            if (m_diff)
                check_diff_args(n);
        }
        else if (fid == m_a_util.get_family_id()) {
            if (m_a_util.is_mul(n))
                check_mul(n);
            else if (m_a_util.is_div(n) || m_a_util.is_idiv(n) || m_a_util.is_rem(n) || m_a_util.is_mod(n))
                check_div(n);
            if (m_diff) {
                if (m_a_util.is_le(n) || m_a_util.is_lt(n) || m_a_util.is_ge(n) || m_a_util.is_gt(n))
                    check_diff_predicate(n);
            }
            if (!m_ints || !m_reals) {
                if (m_a_util.is_to_real(n) || m_a_util.is_to_int(n))
                    fail("logic does not support casting operators");
            }
        }
        else if (fid == m_bv_util.get_family_id()) {
            // nothing to check...
        }
        else if (fid == m_ar_util.get_family_id()) {
            // nothing to check...
            if (m_diff)
                check_diff_args(n);
        }
        else if (fid == m.get_basic_family_id()) {
            // nothing to check...
            if (m_diff) {
                if (m.is_eq(n))
                    check_diff_predicate(n);
                else if (m.is_distinct(n) || m.is_ite(n))
                    check_diff_args(n);
            }
        }
        else if (m.is_builtin_family_id(fid)) {
            // nothing to check
        }
        else if (fid == m_seq_util.get_family_id()) {
            // nothing to check
        }
        else if (fid == m_dt_util.get_family_id() && m_dt) {
            // nothing to check
        }
        else if (fid == m_pb_util.get_family_id() && smt_logics::logic_has_pb(m_logic)) {
            // nothing to check
        }
        else {
            std::stringstream strm;
            strm << "logic does not support theory " << m.get_family_name(fid);
            fail(strm.str());
        }
    }

    void operator()(quantifier * n) {
        if (!m_quantifiers)
            fail("logic does not support quantifiers");
    }

    bool operator()(expr * n) {
        if (m_unknown_logic)
            return true;
        try {
            quick_for_each_expr(*this, n);
            return true;
        }
        catch (const failed &) {
            return false;
        }
    }

    bool operator()(func_decl * f) {
        if (m_unknown_logic)
            return true;
        try {
            unsigned arity = f->get_arity();
            if (arity > 0) {
                if (!m_uf && f->get_family_id() == null_family_id)
                    fail("logic does not support uninterpreted functions");
                for (unsigned i = 0; i < arity; ++i)
                    check_sort(f->get_domain(i));
            }
            check_sort(f->get_range());
            return true;
        }
        catch (const failed &) {
            return false;
        }
    }
};

check_logic::check_logic() {
    m_imp = nullptr;
}

check_logic::~check_logic() {
    if (m_imp)
        dealloc(m_imp);
}

void check_logic::reset() {
    if (m_imp)
        dealloc(m_imp);
    m_imp = nullptr;
}

void check_logic::set_logic(ast_manager & m, symbol const & logic) {
    reset();
    m_imp = alloc(imp, m);
    m_imp->set_logic(logic);
}

bool check_logic::operator()(expr * n) {
    if (m_imp)
        return m_imp->operator()(n);
    return true;
}

bool check_logic::operator()(func_decl * f) {
    if (m_imp)
        return m_imp->operator()(f);
    return true;
}

char const * check_logic::get_last_error() const {
    if (m_imp)
        return m_imp->m_last_error.c_str();
    return "";
}