z3 / src / sat / tactic / goal2sat.cpp

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

Module Name:

    goal2sat.cpp

Abstract:

    "Compile" a goal into the SAT engine.
    Atoms are "abstracted" into boolean variables.
    The mapping between boolean variables and atoms
    can be used to convert back the state of the 
    SAT engine into a goal.

    The idea is to support scenarios such as:
    1) simplify, blast, convert into SAT, and solve
    2) convert into SAT, apply SAT for a while, learn new units, and translate back into a goal.
    3) convert into SAT, apply SAT preprocessor (failed literal propagation, resolution, etc) and translate back into a goal.
    4) Convert boolean structure into SAT, convert atoms into another engine, combine engines using lazy combination, solve.

Author:

    Leonardo (leonardo) 2011-10-26

Notes:

--*/
#include"goal2sat.h"
#include"ast_smt2_pp.h"
#include"ref_util.h"
#include"cooperate.h"
#include"filter_model_converter.h"
#include"model_evaluator.h"
#include"for_each_expr.h"
#include"model_v2_pp.h"
#include"tactic.h"

struct goal2sat::imp {
    struct frame {
        app *    m_t;
        unsigned m_root:1;
        unsigned m_sign:1;
        unsigned m_idx;
        frame(app * t, bool r, bool s, unsigned idx):
            m_t(t), m_root(r), m_sign(s), m_idx(idx) {}
    };
    ast_manager &               m;
    svector<frame>              m_frame_stack;
    svector<sat::literal>       m_result_stack;
    obj_map<app, sat::literal>  m_cache;
    obj_hashtable<expr>         m_interface_vars;
    sat::solver &               m_solver;
    atom2bool_var &             m_map;
    sat::bool_var               m_true;
    bool                        m_ite_extra;
    unsigned long long          m_max_memory;
    volatile bool               m_cancel;
    
    imp(ast_manager & _m, params_ref const & p, sat::solver & s, atom2bool_var & map):
        m(_m),
        m_solver(s),
        m_map(map) {
        updt_params(p);
        m_cancel = false;
        m_true = sat::null_bool_var;
    }
        
    void updt_params(params_ref const & p) {
        m_ite_extra       = p.get_bool(":ite-extra", true);
        m_max_memory      = megabytes_to_bytes(p.get_uint(":max-memory", UINT_MAX));
    }

    void throw_op_not_handled() {
        throw tactic_exception("operator not supported, apply simplifier before invoking translator");
    }
    
    void mk_clause(sat::literal l) {
        TRACE("goal2sat", tout << "mk_clause: " << l << "\n";);
        m_solver.mk_clause(1, &l);
    }

    void mk_clause(sat::literal l1, sat::literal l2) {
        TRACE("goal2sat", tout << "mk_clause: " << l1 << " " << l2 << "\n";);
        m_solver.mk_clause(l1, l2);
    }

    void mk_clause(sat::literal l1, sat::literal l2, sat::literal l3) {
        TRACE("goal2sat", tout << "mk_clause: " << l1 << " " << l2 << " " << l3 << "\n";);
        m_solver.mk_clause(l1, l2, l3);
    }

    void mk_clause(unsigned num, sat::literal * lits) {
        TRACE("goal2sat", tout << "mk_clause: "; for (unsigned i = 0; i < num; i++) tout << lits[i] << " "; tout << "\n";);
        m_solver.mk_clause(num, lits);
    }

    sat::bool_var mk_true() {
        // create fake variable to represent true;
        if (m_true == sat::null_bool_var) {
            m_true = m_solver.mk_var();
            mk_clause(sat::literal(m_true, false)); // v is true
        }
        return m_true;
    }
    
    void convert_atom(expr * t, bool root, bool sign) {
        SASSERT(m.is_bool(t));
        sat::literal  l;
        sat::bool_var v = m_map.to_bool_var(t);
        if (v == sat::null_bool_var) {
            if (m.is_true(t)) {
                l = sat::literal(mk_true(), sign);
            }
            else if (m.is_false(t)) {
                l = sat::literal(mk_true(), !sign);
            }
            else {
                bool ext = !is_uninterp_const(t) || m_interface_vars.contains(t);
                sat::bool_var v = m_solver.mk_var(ext);
                m_map.insert(t, v);
                l = sat::literal(v, sign);
                TRACE("goal2sat", tout << "new_var: " << v << "\n" << mk_ismt2_pp(t, m) << "\n";);
            }
        }
        else {
            SASSERT(v != sat::null_bool_var);
            l = sat::literal(v, sign);
        }
        SASSERT(l != sat::null_literal);
        if (root)
            mk_clause(l);
        else
            m_result_stack.push_back(l);
    }

    bool process_cached(app * t, bool root, bool sign) {
        sat::literal l;
        if (m_cache.find(t, l)) {
            if (sign)
                l.neg();
            if (root)
                mk_clause(l);
            else
                m_result_stack.push_back(l);
            return true;
        }
        return false;
    }

    bool visit(expr * t, bool root, bool sign) {
        if (!is_app(t)) {
            convert_atom(t, root, sign);
            return true;
        }
        if (process_cached(to_app(t), root, sign))
            return true;
        if (to_app(t)->get_family_id() != m.get_basic_family_id()) {
            convert_atom(t, root, sign);
            return true;
        }
        switch (to_app(t)->get_decl_kind()) {
        case OP_NOT:
        case OP_OR:
        case OP_IFF:
            m_frame_stack.push_back(frame(to_app(t), root, sign, 0));
            return false;
        case OP_ITE:
        case OP_EQ:
            if (m.is_bool(to_app(t)->get_arg(1))) {
                m_frame_stack.push_back(frame(to_app(t), root, sign, 0));
                return false;
            }
            convert_atom(t, root, sign);
            return true;
        case OP_AND:
        case OP_XOR:
        case OP_IMPLIES:
        case OP_DISTINCT:
            TRACE("goal2sat_not_handled", tout << mk_ismt2_pp(t, m) << "\n";);
            throw_op_not_handled();
        default:
            convert_atom(t, root, sign);
            return true;
        }
    }

    void convert_or(app * t, bool root, bool sign) {
        TRACE("goal2sat", tout << "convert_or:\n" << mk_ismt2_pp(t, m) << "\n";);
        unsigned num = t->get_num_args();
        if (root) {
            SASSERT(num == m_result_stack.size());
            if (sign) {
                // this case should not really happen.
                for (unsigned i = 0; i < num; i++) {
                    sat::literal l = m_result_stack[i];
                    l.neg();
                    mk_clause(l);
                }
            }
            else {
                mk_clause(m_result_stack.size(), m_result_stack.c_ptr());
                m_result_stack.reset();
            }
        }
        else {
            SASSERT(num <= m_result_stack.size());
            sat::bool_var k = m_solver.mk_var();
            sat::literal  l(k, false);
            m_cache.insert(t, l);
            sat::literal * lits = m_result_stack.end() - num;
            for (unsigned i = 0; i < num; i++) {
                mk_clause(~lits[i], l);
            }
            m_result_stack.push_back(~l);
            lits = m_result_stack.end() - num - 1;
            // remark: mk_clause may perform destructive updated to lits.
            // I have to execute it after the binary mk_clause above.
            mk_clause(num+1, lits);
            unsigned old_sz = m_result_stack.size() - num - 1;
            m_result_stack.shrink(old_sz);
            if (sign)
                l.neg();
            m_result_stack.push_back(l);
        }
    }

    void convert_ite(app * n, bool root, bool sign) {
        unsigned sz = m_result_stack.size();
        SASSERT(sz >= 3);
        sat::literal  c = m_result_stack[sz-3];
        sat::literal  t = m_result_stack[sz-2];
        sat::literal  e = m_result_stack[sz-1];
        if (root) {
            SASSERT(sz == 3);
            if (sign) {
                mk_clause(~c, ~t);
                mk_clause(c,  ~e);
            }
            else {
                mk_clause(~c, t);
                mk_clause(c, e);
            }
            m_result_stack.reset();
        }
        else {
            sat::bool_var k = m_solver.mk_var();
            sat::literal  l(k, false);
            m_cache.insert(n, l);
            mk_clause(~l, ~c, t);
            mk_clause(~l,  c, e);
            mk_clause(l,  ~c, ~t);
            mk_clause(l,   c, ~e);
            if (m_ite_extra) {
                mk_clause(~t, ~e, l);
                mk_clause(t,  e, ~l);
            }
            m_result_stack.shrink(sz-3);
            if (sign)
                l.neg();
            m_result_stack.push_back(l);
        }
    }

    void convert_iff(app * t, bool root, bool sign) {
        TRACE("goal2sat", tout << "convert_iff " << root << " " << sign << "\n" << mk_ismt2_pp(t, m) << "\n";);
        unsigned sz = m_result_stack.size();
        SASSERT(sz >= 2);
        sat::literal  l1 = m_result_stack[sz-1];
        sat::literal  l2 = m_result_stack[sz-2];
        if (root) {
            SASSERT(sz == 2);
            if (sign) {
                mk_clause(l1, l2);
                mk_clause(~l1, ~l2);
            }
            else {
                mk_clause(l1, ~l2);
                mk_clause(~l1, l2);
            }
            m_result_stack.reset();
        }
        else {
            sat::bool_var k = m_solver.mk_var();
            sat::literal  l(k, false);
            m_cache.insert(t, l);
            mk_clause(~l, l1, ~l2);
            mk_clause(~l, ~l1, l2);
            mk_clause(l,  l1, l2);
            mk_clause(l, ~l1, ~l2);
            m_result_stack.shrink(sz-2);
            if (sign)
                l.neg();
            m_result_stack.push_back(l);
        }
    }

    void convert(app * t, bool root, bool sign) {
        SASSERT(t->get_family_id() == m.get_basic_family_id());
        switch (to_app(t)->get_decl_kind()) {
        case OP_OR:
            convert_or(t, root, sign);
            break;
        case OP_ITE:
            convert_ite(t, root, sign);
            break;
        case OP_IFF:
        case OP_EQ:
            convert_iff(t, root, sign);
            break;
        default:
            UNREACHABLE();
        }
    }
    
    void process(expr * n) {
        TRACE("goal2sat", tout << "converting: " << mk_ismt2_pp(n, m) << "\n";);
        if (visit(n, true, false)) {
            SASSERT(m_result_stack.empty());
            return;
        }
        while (!m_frame_stack.empty()) {
        loop:
            cooperate("goal2sat");
            if (m_cancel)
                throw tactic_exception(TACTIC_CANCELED_MSG);
            if (memory::get_allocation_size() > m_max_memory)
                throw tactic_exception(TACTIC_MAX_MEMORY_MSG);
            frame & fr = m_frame_stack.back();
            app * t    = fr.m_t;
            bool root  = fr.m_root;
            bool sign  = fr.m_sign;
            TRACE("goal2sat_bug", tout << "result stack\n";
                  tout << mk_ismt2_pp(t, m) << " root: " << root << " sign: " << sign << "\n";
                  for (unsigned i = 0; i < m_result_stack.size(); i++) tout << m_result_stack[i] << " ";
                  tout << "\n";);
            if (fr.m_idx == 0 && process_cached(t, root, sign)) {
                m_frame_stack.pop_back();
                continue;
            }
            if (m.is_not(t)) {
                m_frame_stack.pop_back();
                visit(t->get_arg(0), root, !sign);
                continue;
            }
            unsigned num = t->get_num_args();
            while (fr.m_idx < num) {
                expr * arg = t->get_arg(fr.m_idx);
                fr.m_idx++;
                if (!visit(arg, false, false))
                    goto loop;
            }
            TRACE("goal2sat_bug", tout << "converting\n";
                  tout << mk_ismt2_pp(t, m) << " root: " << root << " sign: " << sign << "\n";
                  for (unsigned i = 0; i < m_result_stack.size(); i++) tout << m_result_stack[i] << " ";
                  tout << "\n";);
            convert(t, root, sign);
            m_frame_stack.pop_back();
        }
        SASSERT(m_result_stack.empty());
    }


    void operator()(goal const & g) {
        m_interface_vars.reset();
        collect_boolean_interface(g, m_interface_vars);
        
        unsigned size = g.size();
        for (unsigned idx = 0; idx < size; idx++) {
            expr * f = g.form(idx);
            process(f);
        }
    }

    void operator()(unsigned sz, expr * const * fs) {
        m_interface_vars.reset();
        collect_boolean_interface(m, sz, fs, m_interface_vars);
        
        for (unsigned i = 0; i < sz; i++)
            process(fs[i]);
    }

    void set_cancel(bool f) { m_cancel = f; }
};

struct unsupported_bool_proc {
    struct found {};
    ast_manager & m;
    unsupported_bool_proc(ast_manager & _m):m(_m) {}
    void operator()(var *) {}
    void operator()(quantifier *) {}
    void operator()(app * n) { 
        if (n->get_family_id() == m.get_basic_family_id()) {
            switch (n->get_decl_kind()) {
            case OP_AND:
            case OP_XOR:
            case OP_IMPLIES:
            case OP_DISTINCT:
                throw found();
            default:
                break;
            }
        }
    }
};

/**
   \brief Return true if s contains an unsupported Boolean operator.
   goal_rewriter (with the following configuration) can be used to
   eliminate unsupported operators.
      :elim-and true
      :blast-distinct true
*/
bool goal2sat::has_unsupported_bool(goal const & g) {
    return test<unsupported_bool_proc>(g);
}

goal2sat::goal2sat():m_imp(0) {
}

void goal2sat::collect_param_descrs(param_descrs & r) {
    insert_max_memory(r);
    r.insert(":ite-extra", CPK_BOOL, "(default: true) add redundant clauses (that improve unit propagation) when encoding if-then-else formulas");
}

struct goal2sat::scoped_set_imp {
    goal2sat * m_owner; 
    scoped_set_imp(goal2sat * o, goal2sat::imp * i):m_owner(o) {
        #pragma omp critical (goal2sat)
        {
            m_owner->m_imp = i;
        }
    }
    ~scoped_set_imp() {
        #pragma omp critical (goal2sat)
        {
            m_owner->m_imp = 0;
        }
    }
};

void goal2sat::operator()(goal const & g, params_ref const & p, sat::solver & t, atom2bool_var & m) {
    imp proc(g.m(), p, t, m);
    scoped_set_imp set(this, &proc);
    proc(g);
}

void goal2sat::set_cancel(bool f) {
    #pragma omp critical (goal2sat)
    {
        if (m_imp)
            m_imp->set_cancel(f);
    }
}

struct sat2goal::imp {

    // Wrapper for sat::model_converter: converts it into an "AST level" model_converter.
    class sat_model_converter : public model_converter {
        sat::model_converter        m_mc;
        // TODO: the following mapping is storing a lot of useless information, and may be a performance bottleneck.
        // We need to save only the expressions associated with variables that occur in m_mc.
        // This information may be stored as a vector of pairs.
        // The mapping is only created during the model conversion.
        expr_ref_vector             m_var2expr;
        ref<filter_model_converter> m_fmc; // filter for eliminating fresh variables introduced in the assertion-set --> sat conversion
        
        sat_model_converter(ast_manager & m):
            m_var2expr(m) {
        }
        
    public:
        sat_model_converter(ast_manager & m, sat::solver const & s):m_var2expr(m) {
            m_mc.copy(s.get_model_converter());
            m_fmc = alloc(filter_model_converter, m);
        }
        
        ast_manager & m() { return m_var2expr.get_manager(); }
        
        void insert(expr * atom, bool aux) {
            m_var2expr.push_back(atom);
            if (aux) {
                SASSERT(is_uninterp_const(atom));
                SASSERT(m().is_bool(atom));
                m_fmc->insert(to_app(atom)->get_decl());
            }
        }
        
        virtual void operator()(model_ref & md, unsigned goal_idx) {
            SASSERT(goal_idx == 0);
            TRACE("sat_mc", tout << "before sat_mc\n"; model_v2_pp(tout, *md); display(tout););
            // REMARK: potential problem
            // model_evaluator can't evaluate quantifiers. Then,
            // an eliminated variable that depends on a quantified expression can't be recovered.
            // A similar problem also affects any model_converter that uses elim_var_model_converter.
            //
            // Possible solution:
            //   model_converters reject any variable elimination that depends on a quantified expression.
            
            model_evaluator ev(*md);
            ev.set_model_completion(false);
            
            // create a SAT model using md
            sat::model sat_md;
            unsigned sz = m_var2expr.size();
            expr_ref val(m());
            for (sat::bool_var v = 0; v < sz; v++) {
                expr * atom = m_var2expr.get(v);
                ev(atom, val);
                if (m().is_true(val)) 
                    sat_md.push_back(l_true);
                else if (m().is_false(val))
                    sat_md.push_back(l_false);
                else 
                    sat_md.push_back(l_undef);
            }
            
            // apply SAT model converter
            m_mc(sat_md);
            
            // register value of non-auxiliary boolean variables back into md
            sz = m_var2expr.size();
            for (sat::bool_var v = 0; v < sz; v++) {
                expr * atom = m_var2expr.get(v);
                if (is_uninterp_const(atom)) {
                    func_decl * d = to_app(atom)->get_decl();
                    lbool new_val = sat_md[v];
                    if (new_val == l_true)
                        md->register_decl(d, m().mk_true());
                    else if (new_val == l_false)
                        md->register_decl(d, m().mk_false());
                }
            }
            
            // apply filter model converter
            (*m_fmc)(md);
            TRACE("sat_mc", tout << "after sat_mc\n"; model_v2_pp(tout, *md););
        }
        
        virtual model_converter * translate(ast_translation & translator) {
            sat_model_converter * res = alloc(sat_model_converter, translator.to());
            res->m_fmc = static_cast<filter_model_converter*>(m_fmc->translate(translator));
            unsigned sz = m_var2expr.size();
            for (unsigned i = 0; i < sz; i++) 
                res->m_var2expr.push_back(translator(m_var2expr.get(i)));
            return res;
        }
        
        void display(std::ostream & out) {
            out << "(sat-model-converter\n";
            m_mc.display(out);
            sat::bool_var_set vars;
            m_mc.collect_vars(vars);
            out << "(atoms";
            unsigned sz = m_var2expr.size();
            for (unsigned i = 0; i < sz; i++) {
                if (vars.contains(i)) {
                    out << "\n (" << i << "\n  " << mk_ismt2_pp(m_var2expr.get(i), m(), 2) << ")";
                }
            }
            out << ")\n";
            m_fmc->display(out);
            out << ")\n";
        }
    };

    ast_manager &           m;
    expr_ref_vector         m_lit2expr;
    unsigned long long      m_max_memory;
    bool                    m_learned;
    volatile bool           m_cancel;
    
    imp(ast_manager & _m, params_ref const & p):m(_m), m_lit2expr(m), m_cancel(false) {
        updt_params(p);
    }

    void updt_params(params_ref const & p) {
        m_learned        = p.get_bool(":learned", false);
        m_max_memory     = megabytes_to_bytes(p.get_uint(":max-memory", UINT_MAX));
    }

    void checkpoint() {
        if (m_cancel)
            throw tactic_exception(TACTIC_CANCELED_MSG);
        if (memory::get_allocation_size() > m_max_memory)
            throw tactic_exception(TACTIC_MAX_MEMORY_MSG);
    }

    void init_lit2expr(sat::solver const & s, atom2bool_var const & map, model_converter_ref & mc, bool produce_models) {
        ref<sat_model_converter> _mc;
        if (produce_models)
            _mc = alloc(sat_model_converter, m, s);
        unsigned num_vars = s.num_vars();
        m_lit2expr.resize(num_vars * 2);
        map.mk_inv(m_lit2expr);
        sort * b = m.mk_bool_sort();
        for (sat::bool_var v = 0; v < num_vars; v++) {
            checkpoint();
            sat::literal l(v, false);
            if (m_lit2expr.get(l.index()) == 0) {
                SASSERT(m_lit2expr.get((~l).index()) == 0);
                app * aux = m.mk_fresh_const(0, b);
                if (_mc)
                    _mc->insert(aux, true);
                m_lit2expr.set(l.index(), aux);
                m_lit2expr.set((~l).index(), m.mk_not(aux));
            }
            else {
                if (_mc)
                    _mc->insert(m_lit2expr.get(l.index()), false);
                SASSERT(m_lit2expr.get((~l).index()) != 0);
            }
        }
        mc = _mc.get();
    }

    expr * lit2expr(sat::literal l) {
        return m_lit2expr.get(l.index());
    }

    void assert_clauses(sat::clause * const * begin, sat::clause * const * end, goal & r) {
        ptr_buffer<expr> lits;
        for (sat::clause * const * it = begin; it != end; it++) {
            checkpoint();
            lits.reset();
            sat::clause const & c = *(*it);
            unsigned sz = c.size();
            for (unsigned i = 0; i < sz; i++) {
                lits.push_back(lit2expr(c[i]));
            }
            r.assert_expr(m.mk_or(lits.size(), lits.c_ptr()));
        }
    }

    void operator()(sat::solver const & s, atom2bool_var const & map, goal & r, model_converter_ref & mc) {
        if (s.inconsistent()) {
            r.assert_expr(m.mk_false());
            return;
        }
        init_lit2expr(s, map, mc, r.models_enabled());
        // collect units
        unsigned num_vars = s.num_vars();
        for (sat::bool_var v = 0; v < num_vars; v++) {
            checkpoint();
            switch (s.value(v)) {
            case l_true:
                r.assert_expr(lit2expr(sat::literal(v, false)));
                break;
            case l_false:
                r.assert_expr(lit2expr(sat::literal(v, true)));
                break;
            case l_undef:
                break;
            }
        }
        // collect binary clauses
        svector<sat::solver::bin_clause> bin_clauses;
        s.collect_bin_clauses(bin_clauses, m_learned);
        svector<sat::solver::bin_clause>::iterator it  = bin_clauses.begin();
        svector<sat::solver::bin_clause>::iterator end = bin_clauses.end();
        for (; it != end; ++it) {
            checkpoint();
            r.assert_expr(m.mk_or(lit2expr(it->first), lit2expr(it->second)));
        }
        // collect clauses
        assert_clauses(s.begin_clauses(), s.end_clauses(), r);
        if (m_learned)
            assert_clauses(s.begin_learned(), s.end_learned(), r);
    }

    void set_cancel(bool f) { m_cancel = f; }
};

sat2goal::sat2goal():m_imp(0) {
}

void sat2goal::collect_param_descrs(param_descrs & r) {
    insert_max_memory(r);
    r.insert(":learned", CPK_BOOL, "(default: false) collect also learned clauses.");
}

struct sat2goal::scoped_set_imp {
    sat2goal * m_owner; 
    scoped_set_imp(sat2goal * o, sat2goal::imp * i):m_owner(o) {
        #pragma omp critical (sat2goal)
        {
            m_owner->m_imp = i;
        }
    }
    ~scoped_set_imp() {
        #pragma omp critical (sat2goal)
        {
            m_owner->m_imp = 0;
        }
    }
};

void sat2goal::operator()(sat::solver const & t, atom2bool_var const & m, params_ref const & p, 
                          goal & g, model_converter_ref & mc) {
    imp proc(g.m(), p);
    scoped_set_imp set(this, &proc);
    proc(t, m, g, mc);
}

void sat2goal::set_cancel(bool f) {
    #pragma omp critical (sat2goal)
    {
        if (m_imp)
            m_imp->set_cancel(f);
    }
}
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.