z3-src 503.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
/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    ast_util.cpp

Abstract:

    Helper functions

Author:

    Leonardo de Moura (leonardo) 2007-06-08.

Revision History:

--*/
#include "ast/ast_util.h"
#include "ast/ast_pp.h"
#include "ast/for_each_expr.h"
#include "ast/arith_decl_plugin.h"

app * mk_list_assoc_app(ast_manager & m, func_decl * f, unsigned num_args, expr * const * args) {
    SASSERT(f->is_associative());
    SASSERT(num_args >= 2);
    if (num_args > 2) {
        unsigned j = num_args - 1;
        app * r = m.mk_app(f, args[j-1], args[j]);
        -- j;
        while (j > 0) {
            --j;
            r = m.mk_app(f, args[j], r);
        }
        return r;
    }
    else {
        SASSERT(num_args == 2);
        return m.mk_app(f, args[0], args[1]);
    }
}

app * mk_list_assoc_app(ast_manager & m, family_id fid, decl_kind k, unsigned num_args, expr * const * args) {
    func_decl * decl = m.mk_func_decl(fid, k, 0, nullptr, num_args, args, nullptr);
    SASSERT(decl != 0);
    SASSERT(decl->is_associative());
    return mk_list_assoc_app(m, decl, num_args, args);
}

bool is_well_formed_vars(ptr_vector<sort>& bound, expr * top) {
    ptr_vector<expr> todo;
    ast_mark mark;
    todo.push_back(top);
    while (!todo.empty()) {
        expr * e = todo.back();
        todo.pop_back();
        if (mark.is_marked(e)) {
            continue;
        }
        mark.mark(e, true);
        if (is_quantifier(e)) {
            quantifier* q = to_quantifier(e);
            unsigned depth = q->get_num_decls();
            bound.append(depth, q->get_decl_sorts());
            if (!is_well_formed_vars(bound, q->get_expr())) {
                return false;
            }
            bound.resize(bound.size()-depth);
        }
        else if (is_app(e)) {
            app* a = to_app(e);
            for (unsigned i = 0; i < a->get_num_args(); ++i) {
                todo.push_back(a->get_arg(i));
            }
        }
        else if (is_var(e)) {
            var* v = to_var(e);
            unsigned index = v->get_idx();
            sort* s = v->get_sort();
            SASSERT(index < bound.size());
            index = bound.size()-1-index;
            if (!bound[index]) {
                bound[index] = s;
            }
            if (bound[index] != s) {
                return false;
            }
        }
        else {
            UNREACHABLE();
        }
    }
    return true;
}


bool is_atom(ast_manager & m, expr * n) {
    if (is_quantifier(n) || !m.is_bool(n))
        return false;
    if (is_var(n))
        return true;
    SASSERT(is_app(n));
    if (to_app(n)->get_family_id() != m.get_basic_family_id()) {
        return true;
    }
    // the other operators of the basic family are not considered atomic: distinct, ite, and, or, iff, xor, not, implies.
    return (m.is_eq(n) && !m.is_bool(to_app(n)->get_arg(0))) || m.is_true(n) || m.is_false(n);
}


bool is_literal(ast_manager & m, expr * n) {
    return
        is_atom(m, n) ||
        (m.is_not(n) && is_atom(m, to_app(n)->get_arg(0)));
}

void get_literal_atom_sign(ast_manager & m, expr * n, expr * & atom, bool & sign) {
    SASSERT(is_literal(m, n));
    if (is_atom(m, n)) {
        atom = n;
        sign = false;
    }
    else {
        SASSERT(m.is_not(n));
        atom = to_app(n)->get_arg(0);
        sign = true;
    }
}

bool is_clause(ast_manager & m, expr * n) {
    if (is_literal(m, n))
        return true;
    if (m.is_or(n)) {
        for (expr* arg : *to_app(n))
            if (!is_literal(m, arg))
                return false;        
        return true;
    }
    return false;
}

unsigned get_clause_num_literals(ast_manager & m, expr * cls) {
    SASSERT(is_clause(m, cls));
    if (is_literal(m, cls))
        return 1;
    SASSERT(m.is_or(cls));
    return to_app(cls)->get_num_args();
}

expr * get_clause_literal(ast_manager & m, expr * cls, unsigned idx) {
    SASSERT(is_clause(m, cls));
    SASSERT(idx < get_clause_num_literals(m, cls));
    if (is_literal(m, cls)) {
        SASSERT(idx == 0);
        return cls;
    }
    SASSERT(m.is_or(cls));
    return to_app(cls)->get_arg(idx);
}

expr * mk_and(ast_manager & m, unsigned num_args, expr * const * args) {
    if (num_args == 0)
        return m.mk_true();
    else if (num_args == 1)
        return args[0];
    else
        return m.mk_and(std::span<expr* const>(args, num_args));
}

app* mk_and(ast_manager & m, unsigned num_args, app * const * args) {
    return to_app(mk_and(m, num_args, reinterpret_cast<expr* const*>(args)));
}

expr * mk_or(ast_manager & m, unsigned num_args, expr * const * args) {
    if (num_args == 0)
        return m.mk_false();
    else if (num_args == 1)
        return args[0];
    else
        return m.mk_or(num_args, args);
}

app* mk_or(ast_manager & m, unsigned num_args, app * const * args) {
    return to_app(mk_or(m, num_args, (expr* const*) args));
}

expr * mk_not(ast_manager & m, expr * arg) {
    expr * atom;
    if (m.is_not(arg, atom))
        return atom;
    else if (m.is_true(arg))
        return m.mk_false();
    else if (m.is_false(arg))
        return m.mk_true();
    else
        return m.mk_not(arg);
}

expr_ref mk_not(const expr_ref& e) {
    return expr_ref(mk_not(e.m(), e), e.m());
}


expr_ref push_not(const expr_ref& e, unsigned limit) {
    ast_manager& m = e.get_manager();
    if (!is_app(e) || limit == 0) {
        return mk_not(e);
    }
    app* a = to_app(e);
    if (m.is_and(a)) {
        if (a->get_num_args() == 0) {
            return expr_ref(m.mk_false(), m);
        }
        expr_ref_vector args(m);
        for (expr* arg : *a) {
            args.push_back(push_not(expr_ref(arg, m), limit-1));
        }
        return mk_or(args);
    }
    if (m.is_or(a)) {
        if (a->get_num_args() == 0) {
            return expr_ref(m.mk_true(), m);
        }
        expr_ref_vector args(m);
        for (expr* arg : *a) {
            args.push_back(push_not(expr_ref(arg, m), limit-1));
        }
        return mk_and(args);
    }
    return mk_not(e);
}

expr * expand_distinct(ast_manager & m, unsigned num_args, expr * const * args) {
    expr_ref_buffer new_diseqs(m);
    for (unsigned i = 0; i < num_args; ++i) {
        for (unsigned j = i + 1; j < num_args; ++j)
            new_diseqs.push_back(m.mk_not(m.mk_eq(args[i], args[j])));
    }
    return mk_and(m, new_diseqs.size(), new_diseqs.data());
}

expr* mk_distinct(ast_manager& m, unsigned num_args, expr * const * args) {
    switch (num_args) {
    case 0:
    case 1:
        return m.mk_true();
    case 2:
        return m.mk_not(m.mk_eq(args[0], args[1]));
    default:
        return m.mk_distinct(num_args, args);
    }
}

expr_ref mk_distinct(expr_ref_vector const& args) {
    ast_manager& m = args.get_manager();
    return expr_ref(mk_distinct(m, args.size(), args.data()), m);
}


void flatten_and(expr_ref_vector& result) {
    ast_manager& m = result.get_manager();
    expr* e1, *e2, *e3;
    expr_ref_vector pin(m);
    expr_fast_mark1 seen;
    for (unsigned i = 0; i < result.size(); ++i) {
        expr* e = result.get(i);
        if (seen.is_marked(e)) {
            result[i] = result.back();
            result.pop_back();
            --i;
            continue;
        }
        seen.mark(e);
        pin.push_back(e);
        if (m.is_and(e)) {
            app* a = to_app(e);
            for (expr* arg : *a)
                result.push_back(arg);
            result[i] = result.back();
            result.pop_back();
            --i;
        }
        else if (m.is_not(e, e1) && m.is_not(e1, e2)) {
            result[i] = e2;
            --i;
        }
        else if (m.is_not(e, e1) && m.is_or(e1)) {
            app* a = to_app(e1);
            for (expr* arg : *a)
                result.push_back(mk_not(m, arg));
            result[i] = result.back();
            result.pop_back();
            --i;
        }
        else if (m.is_not(e, e1) && m.is_implies(e1, e2, e3)) {
            result.push_back(e2);
            result[i] = mk_not(m, e3);
            --i;
        }
        else if (m.is_true(e) ||
                 (m.is_not(e, e1) &&
                  m.is_false(e1))) {
            result[i] = result.back();
            result.pop_back();
            --i;
        }
        else if (m.is_false(e) ||
                 (m.is_not(e, e1) &&
                  m.is_true(e1))) {
            result.reset();
            result.push_back(m.mk_false());
            return;
        }
    }
}

void flatten_and(expr* fml, expr_ref_vector& result) {
    SASSERT(result.get_manager().is_bool(fml));
    result.push_back(fml);
    flatten_and(result);
}

void flatten_and(expr_ref& fml) {
    expr_ref_vector fmls(fml.get_manager());
    fmls.push_back(fml);
    flatten_and(fmls);
    fml = mk_and(fmls);
}

void flatten_or(expr_ref_vector& result) {
    ast_manager& m = result.get_manager();
    expr* e1, *e2, *e3;
    expr_ref_vector pin(m);
    expr_fast_mark1 seen;
    for (unsigned i = 0; i < result.size(); ++i) {
        expr* e = result.get(i);
        if (seen.is_marked(e)) {
            result[i] = result.back();
            result.pop_back();
            --i;
            continue;
        }
        seen.mark(e);
        pin.push_back(e);
        if (m.is_or(e)) {
            app* a = to_app(e);
            for (expr* arg : *a)
                result.push_back(arg);
            result[i] = result.back();
            result.pop_back();
            --i;
        }
        else if (m.is_not(e, e1) && m.is_not(e1, e2)) {
            result[i] = e2;
            --i;
        }
        else if (m.is_not(e, e1) && m.is_and(e1)) {
            app* a = to_app(e1);
            for (expr* arg : *a)
                result.push_back(mk_not(m, arg));
            result[i] = result.back();
            result.pop_back();
            --i;
        }
        else if (m.is_implies(e,e2,e3)) {
            result.push_back(e3);
            result[i] = mk_not(m, e2);
            --i;
        }
        else if (m.is_false(e) ||
                 (m.is_not(e, e1) &&
                  m.is_true(e1))) {
            result[i] = result.back();
            result.pop_back();
            --i;
        }
        else if (m.is_true(e) ||
                 (m.is_not(e, e1) &&
                  m.is_false(e1))) {
            result.reset();
            result.push_back(m.mk_true());
            return;
        }
    }
}


void flatten_or(expr* fml, expr_ref_vector& result) {
    SASSERT(result.get_manager().is_bool(fml));
    result.push_back(fml);
    flatten_or(result);
}

static app_ref plus(ast_manager& m, expr* a, expr* b) {
    arith_util arith(m);
    return app_ref(arith.mk_add(a, b), m);
}


app_ref operator+(expr_ref& a, expr_ref& b) { 
    return plus(a.m(), a.get(), b.get()); 
}

bool has_uninterpreted(ast_manager& m, expr* _e) {
    expr_ref e(_e, m);
    arith_util au(m);
    func_decl_ref f_out(m);
    for (expr* arg : subterms::all(e)) {
        if (!is_app(arg)) 
            continue;
        app* a = to_app(arg);
        func_decl* f = a->get_decl();
        if (a->get_num_args() == 0)
            continue;
        if (m.is_considered_uninterpreted(f))
            return true;
        if (au.is_considered_uninterpreted(f, a->get_num_args(), a->get_args(), f_out))
            return true;
    }
    return false;
}