z3 / src / smt / theory_arith_pp.h

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

Module Name:

    theory_arith_pp.h

Abstract:

    <abstract>

Author:

    Leonardo de Moura (leonardo) 2008-05-05.

Revision History:

--*/
#ifndef _THEORY_ARITH_PP_H_
#define _THEORY_ARITH_PP_H_

#include"theory_arith.h"
#include"ast_smt_pp.h"
#include"stats.h"

namespace smt {
    template<typename Ext>
    void theory_arith<Ext>::collect_statistics(::statistics & st) const {
        st.update("arith conflicts", m_stats.m_conflicts);
        st.update("add rows", m_stats.m_add_rows);
        st.update("pivots", m_stats.m_pivots);
        st.update("assert lower", m_stats.m_assert_lower);
        st.update("assert upper", m_stats.m_assert_upper);
        st.update("assert diseq", m_stats.m_assert_diseq);
        st.update("bound prop", m_stats.m_bound_props);
        st.update("fixed eqs", m_stats.m_fixed_eqs);
        st.update("offset eqs", m_stats.m_offset_eqs);
        st.update("gcd tests", m_stats.m_gcd_tests);
        st.update("ineq splits", m_stats.m_branches);
        st.update("gomory cuts", m_stats.m_gomory_cuts);
        st.update("max-min", m_stats.m_max_min);
        st.update("grobner", m_stats.m_gb_compute_basis);
        st.update("pseudo nonlinear", m_stats.m_nl_linear);
        st.update("nonlinear bounds", m_stats.m_nl_bounds);
        st.update("nonlinear horner", m_stats.m_nl_cross_nested);
        m_arith_eq_adapter.collect_statistics(st);
    }

    template<typename Ext>
    void theory_arith<Ext>::display(std::ostream & out) const {
        out << "Theory arithmetic:\n";
        display_vars(out);
        display_nl_monomials(out);
        display_rows(out, true);
        display_rows(out, false);
        display_atoms(out);
        display_asserted_atoms(out);
    }

    template<typename Ext>
    void theory_arith<Ext>::display_nl_monomials(std::ostream & out) const {
        if (m_nl_monomials.empty())
            return;
        out << "non linear monomials:\n";
        svector<theory_var>::const_iterator it  = m_nl_monomials.begin();
        svector<theory_var>::const_iterator end = m_nl_monomials.end();
        for (; it != end; ++it)
            display_var(out, *it);
    }

    template<typename Ext>
    void theory_arith<Ext>::display_row(std::ostream & out, unsigned r_id, bool compact) const {
        out << r_id << " ";
        display_row(out, m_rows[r_id], compact);
    }

    template<typename Ext>
    void theory_arith<Ext>::display_row(std::ostream & out, row const & r, bool compact) const {
        typename vector<row_entry>::const_iterator it  = r.begin_entries();
        typename vector<row_entry>::const_iterator end = r.end_entries();
        out << "(v" << r.get_base_var() << ") : ";
        bool first = true;
        for (; it != end; ++it) {
            if (!it->is_dead()) {
                if (first)
                    first = false;
                else
                    out << " + ";
                theory_var s      = it->m_var;
                numeral const & c = it->m_coeff;
                if (!c.is_one())
                    out << c << "*";
                if (compact) {
                    out << "v" << s;
                    if (is_fixed(s)) {
                        out << ":" << lower(s)->get_value();
                    }
                }
                else
                    display_var_flat_def(out, s);
            }
        }
        out << "\n";
    }


    template<typename Ext>
    void theory_arith<Ext>::display_rows(std::ostream & out, bool compact) const {
        if (compact)
            out << "rows (compact view):\n";
        else
            out << "rows (expanded view):\n";
        unsigned num = m_rows.size();
        for (unsigned r_id = 0; r_id < num; r_id++) {
            if (m_rows[r_id].m_base_var != null_theory_var) {
                display_row(out, r_id, compact);
            }
        }
    }

    template<typename Ext>
    void theory_arith<Ext>::display_row_shape(std::ostream & out, row const & r) const {
        typename vector<row_entry>::const_iterator it  = r.begin_entries();
        typename vector<row_entry>::const_iterator end = r.end_entries();
        for (; it != end; ++it) {
            if (!it->is_dead()) {
                numeral const & c = it->m_coeff;
                if (c.is_one())
                    out << "1";
                else if (c.is_minus_one())
                    out << "-";
                else if (c.is_int() && c.to_rational().is_small())
                    out << "i";
                else if (c.is_int() && !c.to_rational().is_small())
                    out << "I";
                else if (c.to_rational().is_small())
                    out << "r";
                else
                    out << "R";
            }
        }
        out << "\n";
    }

    template<typename Ext>
    bool theory_arith<Ext>::is_one_minus_one_row(row const & r) const {
        typename vector<row_entry>::const_iterator it  = r.begin_entries();
        typename vector<row_entry>::const_iterator end = r.end_entries();
        for (; it != end; ++it) {
            if (!it->is_dead()) {
                numeral const & c = it->m_coeff;
                if (!c.is_one() && !c.is_minus_one())
                    return false;
            }
        }
        return true;
    }

    template<typename Ext>
    void theory_arith<Ext>::display_rows_shape(std::ostream & out) const {
        unsigned num = m_rows.size();
        unsigned num_trivial = 0;
        for (unsigned r_id = 0; r_id < num; r_id++) {
            row const & r = m_rows[r_id];
            if (r.m_base_var != null_theory_var) {
                if (is_one_minus_one_row(r)) 
                    num_trivial++;
                else
                    display_row_shape(out, r);
            }
        }
        out << "num. trivial: " << num_trivial << "\n";
    }

    template<typename Ext>
    void theory_arith<Ext>::display_rows_bignums(std::ostream & out) const {
        unsigned num = m_rows.size();
        for (unsigned r_id = 0; r_id < num; r_id++) {
            row const & r = m_rows[r_id];
            if (r.m_base_var != null_theory_var) {
                typename vector<row_entry>::const_iterator it  = r.begin_entries();
                typename vector<row_entry>::const_iterator end = r.end_entries();
                for (; it != end; ++it) {
                    if (!it->is_dead()) {
                        numeral const & c = it->m_coeff;
                        if (c.to_rational().is_big()) {
                            std::string str = c.to_rational().to_string();
                            if (str.length() > 48) 
                                out << str << "\n";
                        }
                    }
                }
            }
        }
    }

    template<typename Ext>
    void theory_arith<Ext>::display_rows_stats(std::ostream & out) const {
        unsigned num_vars = get_num_vars();
        unsigned num_rows = 0;
        unsigned num_non_zeros  = 0;
        unsigned num_ones       = 0;
        unsigned num_minus_ones = 0;
        unsigned num_small_ints = 0;
        unsigned num_big_ints   = 0;
        unsigned num_small_rats = 0;
        unsigned num_big_rats   = 0;
        for (unsigned r_id = 0; r_id < m_rows.size(); r_id++) {
            row const & r = m_rows[r_id];
            if (r.m_base_var != null_theory_var) {
                num_rows++;
                typename vector<row_entry>::const_iterator it  = r.begin_entries();
                typename vector<row_entry>::const_iterator end = r.end_entries();
                for (; it != end; ++it) {
                    if (!it->is_dead()) {
                        numeral const & c = it->m_coeff;
                        num_non_zeros++;
                        if (c.is_one())
                            num_ones++;
                        else if (c.is_minus_one())
                            num_minus_ones++;
                        else if (c.is_int() && c.to_rational().is_small())
                            num_small_ints++;
                        else if (c.is_int() && !c.to_rational().is_small())
                            num_big_ints++;
                        else if (c.to_rational().is_small())
                            num_small_rats++;
                        else
                            num_big_rats++;
                    }
                }
            }
        }
        out << "A:        " << num_rows << " X " << num_vars << "\n";
        out << "avg. row: " << num_non_zeros / num_rows << ", num. non zeros: " << num_non_zeros << "\n";
        unsigned spc = 6;
        out.width(spc);
        out << 1 << "|";
        out.width(spc);
        out << -1 << "|";
        out.width(spc);
        out << "i";
        out << "|";
        out.width(spc);
        out << "I";
        out << "|";
        out.width(spc);
        out << "r";
        out << "|";
        out.width(spc);
        out << "R";
        out << "\n";

        out.width(spc);
        out << num_ones << "|";
        out.width(spc);
        out << num_minus_ones << "|";
        out.width(spc);
        out << num_small_ints;
        out << "|";
        out.width(spc);
        out << num_big_ints;
        out << "|";
        out.width(spc);
        out << num_small_rats;
        out << "|";
        out.width(spc);
        out << num_big_rats;
        out << "\n";
    }

    template<typename Ext>
    void theory_arith<Ext>::display_row_info(std::ostream & out, unsigned r_id) const {
        out << r_id << " ";
        display_row_info(out, m_rows[r_id]);
    }

    template<typename Ext>
    void theory_arith<Ext>::display_row_info(std::ostream & out, row const & r) const {
        display_row(out, r, true);
        typename vector<row_entry>::const_iterator it  = r.begin_entries();
        typename vector<row_entry>::const_iterator end = r.end_entries();
        for (; it != end; ++it) 
            if (!it->is_dead())
                display_var(out, it->m_var);
    }

    /**
       \brief Display row after substituting fixed variables.
    */
    template<typename Ext>
    void theory_arith<Ext>::display_simplified_row(std::ostream & out, row const & r) const {
        bool has_rat_coeff = false;
        numeral k;
        typename vector<row_entry>::const_iterator it  = r.begin_entries();
        typename vector<row_entry>::const_iterator end = r.end_entries();
        out << "(v" << r.get_base_var() << ") : ";
        bool first = true;
        for (; it != end; ++it) {
            if (it->is_dead())
                continue;
            theory_var v      = it->m_var;
            numeral const & c = it->m_coeff;
            if (is_fixed(v)) {
                k += c * lower_bound(v).get_rational(); 
                continue;
            }
            if (!c.is_int())
                has_rat_coeff = true;
            if (first)
                first = false;
            else
                out << " + ";
            if (!c.is_one())
                out << c << "*";
            out << "v" << v;
        }
        if (!k.is_zero()) {
            if (!first)
                out << " + ";
            out << k;
        }
        out << "\n";
        if (has_rat_coeff) {
            typename vector<row_entry>::const_iterator it  = r.begin_entries();
            typename vector<row_entry>::const_iterator end = r.end_entries();
            for (; it != end; ++it) 
                if (!it->is_dead() && (is_base(it->m_var) || (!is_fixed(it->m_var) && (lower(it->m_var) || upper(it->m_var)))))
                    display_var(out, it->m_var);
        }
    }

    template<typename Ext>
    void theory_arith<Ext>::display_var(std::ostream & out, theory_var v) const {
        out << "v";
        out.width(4);
        out << std::left << v;
        out << " #";
        out.width(4);
        out << get_enode(v)->get_owner_id();
        out << std::right;
        out << " lo:";
        out.width(10);
        if (lower(v)) {
            out << lower(v)->get_value();
        }
        else {
            out << "-oo";
        }
        out << ", up:";
        out.width(10);
        if (upper(v)) {
            out << upper(v)->get_value();
        }
        else {
            out << "oo";
        }
        out << ", value: ";
        out.width(10);
        out << get_value(v);
        out << ", occs: ";
        out.width(4);
        out << m_columns[v].size();
        out << ", atoms: ";
        out.width(4);
        out << m_var_occs[v].size();
        out << (is_int(v) ? ", int " : ", real");
        switch (get_var_kind(v)) {
        case NON_BASE:
            out << ", non-base  ";
            break;
        case QUASI_BASE:
            out << ", quasi-base";
            break;
        case BASE:
            out << ", base      ";
            break;
        }
        out << ", shared: " << get_context().is_shared(get_enode(v));
        out << ", unassigned: " << m_unassigned_atoms[v];
        out << ", rel: " << get_context().is_relevant(get_enode(v));
        out << ", def: ";
        display_var_flat_def(out, v);
        out << "\n";
    }

    template<typename Ext>
    void theory_arith<Ext>::display_vars(std::ostream & out) const {
        out << "vars:\n";
        int n = get_num_vars();
        for (theory_var v = 0; v < n; v++)
            display_var(out, v);
    }

    template<typename Ext>
    void theory_arith<Ext>::display_bound(std::ostream & out, bound * b, unsigned indent) const {
        for (unsigned i = 0; i < indent; i++) out << "  ";
        theory_var v = b->get_var();
        enode * e    = get_enode(v);
        out << "v" << v << " #" << e->get_owner_id() << " " << (b->get_bound_kind() == B_LOWER ? ">=" : "<=") << " " << b->get_value() << "\n";
    }

    template<typename Ext>
    void theory_arith<Ext>::display_atoms(std::ostream & out) const {
        out << "atoms:\n";
        for (unsigned i = 0; i < m_atoms.size(); i++)
            display_atom(out, m_atoms[i], false);
    }

    template<typename Ext>
    void theory_arith<Ext>::display_asserted_atoms(std::ostream & out) const {
        out << "asserted atoms:\n";
        for (unsigned i = 0; i < m_asserted_qhead; i++) {
            bound * b = m_asserted_bounds[i];
            if (b->is_atom()) 
                display_atom(out, static_cast<atom*>(b), true);
        }
        if (m_asserted_qhead < m_asserted_bounds.size()) {
            out << "delayed atoms:\n";
            for (unsigned i = m_asserted_qhead; i < m_asserted_bounds.size(); i++) {
                bound * b = m_asserted_bounds[i];
                if (b->is_atom()) 
                    display_atom(out, static_cast<atom*>(b), true);
            }
        }
    }

    template<typename Ext>
    void theory_arith<Ext>::display_atom(std::ostream & out, atom * a, bool show_sign) const {
        theory_var      v = a->get_var();
        numeral const & k = a->get_k();
        enode *         e = get_enode(v);
        if (show_sign) {
            if (!a->is_true()) 
                out << "not ";
            else 
                out << "    ";
        }
        out << "v";
        out.width(3);
        out << std::left << v << " #";
        out.width(3);
        out << e->get_owner_id();
        out << std::right;
        out << " ";
        if (a->get_atom_kind() == A_LOWER)
            out << ">=";
        else
            out << "<=";
        out << " ";
        out.width(6);
        out << k << "    ";
        display_var_flat_def(out, v);
        out << "\n";
    }

    template<typename Ext>
    void theory_arith<Ext>::display_bounds_in_smtlib(std::ostream & out) const {
        ast_manager & m = get_manager();
        ast_smt_pp pp(m);
        pp.set_benchmark_name("lemma");
        int n = get_num_vars();
        for (theory_var v = 0; v < n; v++) {
            expr * n = get_enode(v)->get_owner();
            if (is_fixed(v)) {
                inf_numeral k_inf = lower_bound(v);
                rational k = k_inf.get_rational().to_rational();
                expr_ref eq(m);
                eq = m.mk_eq(n, m_util.mk_numeral(k, is_int(v)));
                pp.add_assumption(eq);
            }
            else {
                if (lower(v) != 0) {
                    inf_numeral k_inf = lower_bound(v);
                    rational k = k_inf.get_rational().to_rational();
                    expr_ref ineq(m);
                    if (k_inf.get_infinitesimal().is_zero())
                        ineq = m_util.mk_le(m_util.mk_numeral(k, is_int(v)), n);
                    else
                        ineq = m_util.mk_lt(m_util.mk_numeral(k, is_int(v)), n);
                    pp.add_assumption(ineq);
                }
                if (upper(v) != 0) {
                    inf_numeral k_inf = upper_bound(v);
                    rational k = k_inf.get_rational().to_rational();
                    expr_ref ineq(m);
                    if (k_inf.get_infinitesimal().is_zero())
                        ineq = m_util.mk_le(n, m_util.mk_numeral(k, is_int(v)));
                    else
                        ineq = m_util.mk_lt(n, m_util.mk_numeral(k, is_int(v)));
                    pp.add_assumption(ineq);
                }
            }
        }
        pp.display(out, m.mk_true());
    }

    template<typename Ext>
    void theory_arith<Ext>::display_bounds_in_smtlib() const {
        char buffer[128];
        static int id = 0;
#ifdef _WINDOWS
        sprintf_s(buffer, ARRAYSIZE(buffer), "arith_%d.smt", id);
#else
        sprintf(buffer, "arith_%d.smt", id);
#endif
        std::ofstream out(buffer);
        display_bounds_in_smtlib(out);
        out.close();
        id++;
    }

};

#endif /* _THEORY_ARITH_PP_H_ */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.