Esempio n. 1
0
/**
   \brief Assert expressions from ctx into t.
*/
void assert_exprs_from(cmd_context const & ctx, assertion_set & t) {
    ptr_vector<expr>::const_iterator it  = ctx.begin_assertions();
    ptr_vector<expr>::const_iterator end = ctx.end_assertions();
    for (; it != end; ++it) {
        t.assert_expr(*it);
    }
}
Esempio n. 2
0
void install_dl_cmds(cmd_context & ctx) {
    dl_context * dl_ctx = alloc(dl_context, ctx);
    ctx.insert(alloc(dl_rule_cmd, dl_ctx));
    ctx.insert(alloc(dl_query_cmd, dl_ctx));
    ctx.insert(alloc(dl_declare_rel_cmd, dl_ctx));
    ctx.insert(alloc(dl_declare_var_cmd, dl_ctx));
    PRIVATE_PARAMS(ctx.insert(alloc(dl_push_cmd, dl_ctx));); // not exposed to keep command-extensions simple.
Esempio n. 3
0
 virtual void execute(cmd_context & ctx) {
     ctx.regular_stream() << "\"";
     if (m_cmds.empty()) {
         vector<named_cmd> cmds;
         cmd_context::cmd_iterator it  = ctx.begin_cmds();
         cmd_context::cmd_iterator end = ctx.end_cmds();
         for (; it != end; ++it) {
             cmds.push_back(named_cmd((*it).m_key, (*it).m_value));
         }
         // named_cmd_lt is not a total order for commands, but this is irrelevant for Linux x Windows behavior
         std::sort(cmds.begin(), cmds.end(), named_cmd_lt());
         vector<named_cmd>::const_iterator it2  = cmds.begin();
         vector<named_cmd>::const_iterator end2 = cmds.end();
         for (; it2 != end2; ++it2) {
             display_cmd(ctx, it2->first, it2->second);
         }
     }
     else {
         svector<symbol>::const_iterator it  = m_cmds.begin();
         svector<symbol>::const_iterator end = m_cmds.end();
         for (; it != end; ++it) {
             cmd * c = ctx.find_cmd(*it);
             SASSERT(c);
             display_cmd(ctx, *it, c);
         }
     }
     ctx.regular_stream() << "\"\n";
 }
Esempio n. 4
0
 void print_certificate(cmd_context& ctx) {
     if (m_params.get_bool(":print-certificate", false)) {
         datalog::context& dlctx = m_dl_ctx->get_dl_context();
         if (!dlctx.display_certificate(ctx.regular_stream())) {
             throw cmd_exception("certificates are not supported for selected DL_ENGINE");
         }
         ctx.regular_stream() << "\n";
     }
 }
Esempio n. 5
0
 void set_background(cmd_context& ctx) {
     datalog::context& dlctx = m_dl_ctx->get_dl_context();
     ast_manager& m = ctx.m();                
     ptr_vector<expr>::const_iterator it  = ctx.begin_assertions();
     ptr_vector<expr>::const_iterator end = ctx.end_assertions();
     for (; it != end; ++it) {
         dlctx.assert_expr(*it);
     }
 }
Esempio n. 6
0
 void display_cmd(cmd_context & ctx, symbol const & s, cmd * c) {
     char const * usage = c->get_usage();
     char const * descr = c->get_descr(ctx);
     ctx.regular_stream() << " (" << s;
     if (usage)
         ctx.regular_stream() << " " << escaped(usage, true) << ")\n";
     else 
         ctx.regular_stream() << ")\n";
     if (descr) {
         ctx.regular_stream() << "    " << escaped(descr, true, 4) << "\n";
     }
 }
Esempio n. 7
0
 void print_statistics(cmd_context& ctx) {
     if (m_params.get_bool(":print-statistics", false)) {
         statistics st;
         datalog::context& dlctx = m_dl_ctx->get_dl_context();
         unsigned long long max_mem = memory::get_max_used_memory();
         unsigned long long mem = memory::get_allocation_size();
         dlctx.collect_statistics(st);
         st.update("time", ctx.get_seconds());            
         st.update("memory", static_cast<double>(mem)/static_cast<double>(1024*1024));
         st.update("max-memory", static_cast<double>(max_mem)/static_cast<double>(1024*1024));
         st.display_smt2(ctx.regular_stream());            
     }
 }
Esempio n. 8
0
 void print_answer(cmd_context& ctx) {
     if (m_params.get_bool(":print-answer", false)) {
         datalog::context& dlctx = m_dl_ctx->get_dl_context();
         ast_manager& m = ctx.m();
         expr_ref query_result(dlctx.get_answer_as_formula(), m);
         sbuffer<symbol> var_names;
         unsigned num_decls = 0;
         if (is_quantifier(m_target)) {
             num_decls = to_quantifier(m_target)->get_num_decls();
         }
         ctx.display(ctx.regular_stream(), query_result, 0, num_decls, "X", var_names);
         ctx.regular_stream() << std::endl;
     }
 }
Esempio n. 9
0
    virtual void execute(cmd_context & ctx) {
        if(m_arg_idx<2) {
            throw cmd_exception("at least 2 arguments expected");
        }
        ensure_domain(ctx);
        ast_manager& m = ctx.m();

        func_decl_ref pred(
            m.mk_func_decl(m_rel_name, m_domain->size(), m_domain->c_ptr(), m.mk_bool_sort()), m);
        ctx.insert(pred);
        datalog::context& dctx = m_dl_ctx->get_dl_context();
        dctx.register_predicate(pred, false);
        if(!m_kinds.empty()) {
            dctx.set_predicate_representation(pred, m_kinds.size(), m_kinds.c_ptr());
        }
        m_domain = 0;
    }
Esempio n. 10
0
 virtual void set_next_arg(cmd_context & ctx, expr * t) {
     SASSERT(m_idx == 0);
     if (!ctx.m().is_bool(t)) {
         throw cmd_exception("Invalid type for expression. Expected Boolean type.");
     }
     m_formula = t;
     ++m_idx;
 }
Esempio n. 11
0
void assertion_set::display(cmd_context & ctx, std::ostream & out) const {
    out << "(assertion-set";
    unsigned sz = size();
    for (unsigned i = 0; i < sz; i++) {
        out << "\n  ";
        ctx.display(out, form(i), 2);
    }
    out << ")" << std::endl;
}
Esempio n. 12
0
 virtual void set_next_arg(cmd_context & ctx, symbol const & s) {
     cmd * c = ctx.find_cmd(s);
     if (c == 0) {
         std::string err_msg("unknown command '");
         err_msg = err_msg + s.bare_str() + "'";
         throw cmd_exception(err_msg);
     }
     m_cmds.push_back(s);
 }
Esempio n. 13
0
 void set_next_arg(cmd_context & ctx, symbol const & s) override {
     cmd * c = ctx.find_cmd(s);
     if (c == nullptr) {
         std::string err_msg("unknown command '");
         err_msg = err_msg + s.bare_str() + "'";
         throw cmd_exception(std::move(err_msg));
     }
     m_cmds.push_back(s);
 }
Esempio n. 14
0
    virtual void execute(cmd_context& ctx) {
        if (m_target == 0) {
            throw cmd_exception("invalid query command, argument expected");
        }
        datalog::context& dlctx = m_dl_ctx->get_dl_context();
        set_background(ctx);        
        dlctx.updt_params(m_params);
        unsigned timeout   = m_params.get_uint(":timeout", UINT_MAX);
        cancel_eh<datalog::context> eh(dlctx);
        lbool status = l_undef;
        {
            scoped_ctrl_c ctrlc(eh);
            scoped_timer timer(timeout, &eh);
            cmd_context::scoped_watch sw(ctx);
            try {
                status = dlctx.query(m_target);
            }
            catch (z3_error & ex) {
                throw ex;
            }
            catch (z3_exception& ex) {
                ctx.regular_stream() << "(error \"query failed: " << ex.msg() << "\")" << std::endl;
            }
            dlctx.cleanup();
        }
        switch (status) {
        case l_false:
            ctx.regular_stream() << "unsat\n";
            print_certificate(ctx);
            break;
        case l_true: 
            ctx.regular_stream() << "sat\n";
            print_answer(ctx);
            print_certificate(ctx);
            break;
        case l_undef: 
            ctx.regular_stream() << "unknown\n";
            switch(dlctx.get_status()) {
            case datalog::INPUT_ERROR:
                break;
                
            case datalog::MEMOUT:
                ctx.regular_stream() << "memory bounds exceeded\n";
                break;

            case datalog::TIMEOUT:
                ctx.regular_stream() << "timeout\n";
                break;
                
            case datalog::OK: 
                break;
            default:
                UNREACHABLE();
            }
            break;
        }
        print_statistics(ctx);
        m_target = 0;
    }
Esempio n. 15
0
 void execute(cmd_context & ctx) override {
     ctx.regular_stream() << "\"";
     if (m_cmds.empty()) {
         vector<named_cmd> cmds;
         cmd_context::cmd_iterator it  = ctx.begin_cmds();
         cmd_context::cmd_iterator end = ctx.end_cmds();
         for (; it != end; ++it) {
             cmds.push_back(named_cmd((*it).m_key, (*it).m_value));
         }
         // named_cmd_lt is not a total order for commands, but this is irrelevant for Linux x Windows behavior
         std::sort(cmds.begin(), cmds.end(), named_cmd_lt());
         for (named_cmd const& nc : cmds) {
             display_cmd(ctx, nc.first, nc.second);
         }
     }
     else {
         for (symbol const& s : m_cmds) {
             cmd * c = ctx.find_cmd(s);
             SASSERT(c);
             display_cmd(ctx, s, c);
         }
     }
     ctx.regular_stream() << "\"\n";
 }
Esempio n. 16
0
static void compute_interpolant_and_maybe_check(cmd_context & ctx, expr * t, params_ref &m_params, bool check){
    
    // create a fresh solver suitable for interpolation
    bool proofs_enabled, models_enabled, unsat_core_enabled;
    params_ref p;
    ast_manager &_m = ctx.m();
    // TODO: the following is a HACK to enable proofs in the old smt solver
    // When we stop using that solver, this hack can be removed
    scoped_proof_mode spm(_m,PGM_FINE);
    ctx.params().get_solver_params(_m, p, proofs_enabled, models_enabled, unsat_core_enabled);
    p.set_bool("proof", true);
    scoped_ptr<solver> sp = (ctx.get_interpolating_solver_factory())(_m, p, true, models_enabled, false, ctx.get_logic());

    ptr_vector<ast> cnsts;
    ptr_vector<ast> interps;
    model_ref m;
  
    // compute an interpolant
  
    lbool res;
    try {
        res = iz3interpolate(_m, *sp.get(), t, cnsts, interps, m, 0);
    }
    catch (iz3_incompleteness &) {
        throw cmd_exception("incompleteness in interpolator");
    }

    switch(res){
    case l_false:
        ctx.regular_stream() << "unsat\n";
        show_interpolant_and_maybe_check(ctx, cnsts, t, interps, m_params, check);
        break;

    case l_true:
        ctx.regular_stream() << "sat\n";
        // TODO: how to return the model to the context, if it exists?
        break;

    case l_undef:
        ctx.regular_stream() << "unknown\n";
        // TODO: how to return the model to the context, if it exists?
        break;
    }    

    for(unsigned i = 0; i < cnsts.size(); i++)
        ctx.m().dec_ref(cnsts[i]);

}
Esempio n. 17
0
    void execute(cmd_context & ctx) override {
        proof_ref pr(ctx.m());
        qe::simplify_rewriter_star qe(ctx.m());
        expr_ref result(ctx.m());

        qe(m_target, result, pr);            

        if (m_params.get_bool("print", true)) {
            ctx.display(ctx.regular_stream(), result);
            ctx.regular_stream() << std::endl; 
        }
        if (m_params.get_bool("print_statistics", false)) {
            statistics st;
            qe.collect_statistics(st);
            st.display(ctx.regular_stream());
        }
    }
Esempio n. 18
0
static void get_interpolant_and_maybe_check(cmd_context & ctx, expr * t, params_ref &m_params, bool check) {

    check_can_interpolate(ctx);

    // get the proof, if there is one

    if (!ctx.has_manager() ||
        ctx.cs_state() != cmd_context::css_unsat)
        throw cmd_exception("proof is not available");
    expr_ref pr(ctx.m());
    pr = ctx.get_check_sat_result()->get_proof();
    if (pr == 0)
        throw cmd_exception("proof is not available");

    // get the assertions from the context

    ptr_vector<expr>::const_iterator it  = ctx.begin_assertions();
    ptr_vector<expr>::const_iterator end = ctx.end_assertions();
    ptr_vector<ast> cnsts((unsigned)(end - it));
    for (int i = 0; it != end; ++it, ++i)
        cnsts[i] = *it;
    
    // compute an interpolant
  
    ptr_vector<ast> interps;
 
    try {
        iz3interpolate(ctx.m(),pr.get(),cnsts,t,interps,0);
    }
    catch (iz3_bad_tree &) {
        throw cmd_exception("interpolation pattern contains non-asserted formula");
    }
    catch (iz3_incompleteness &) {
        throw cmd_exception("incompleteness in interpolator");
    }

    show_interpolant_and_maybe_check(ctx, cnsts, t, interps, m_params, check);
}
Esempio n. 19
0
static void to_subpaving(cmd_context & ctx, expr * t) {
    ast_manager & m = ctx.m();
    unsynch_mpq_manager qm;
    scoped_ptr<subpaving::context> s;
    s = subpaving::mk_mpq_context(ctx.m().limit(), qm);
    expr2var e2v(m);
    expr2subpaving e2s(m, *s, &e2v);
    params_ref p;
    p.set_bool("mul_to_power", true);
    th_rewriter simp(m, p);
    expr_ref t_s(m);
    simp(t, t_s);
    scoped_mpz n(qm), d(qm);
    ctx.regular_stream() << mk_ismt2_pp(t_s, m) << "\n=======>" << std::endl;
    subpaving::var x = e2s.internalize_term(t_s, n, d);
    expr2var::iterator it  = e2v.begin();
    expr2var::iterator end = e2v.end();
    for (; it != end; ++it) {
        ctx.regular_stream() << "x" << it->m_value << " := " << mk_ismt2_pp(it->m_key, m) << "\n";
    }
    s->display_constraints(ctx.regular_stream());
    ctx.regular_stream() << n << "/" << d << " x" << x << "\n";
}
Esempio n. 20
0
 void ensure_domain(cmd_context& ctx) {
     if (!m_domain) m_domain = alloc(sort_ref_vector, ctx.m());
 }
Esempio n. 21
0
 virtual void execute(cmd_context & ctx) {
     ast_manager& m = ctx.m();
     func_decl_ref var(m.mk_func_decl(m_var_name, 0, static_cast<sort*const*>(0), m_var_sort), m);
     ctx.insert(var);
     m_dl_ctx->get_dl_context().register_variable(var);
 }
Esempio n. 22
0
void store_expr_ref(cmd_context & ctx, symbol const & v, expr * t) {
    ctx.insert(v, alloc(ast_object_ref, ctx, t));
}
Esempio n. 23
0
static void check_can_interpolate(cmd_context & ctx){
    if (!ctx.produce_interpolants())
        throw cmd_exception("interpolation is not enabled, use command (set-option :produce-interpolants true)");
}
Esempio n. 24
0
void install_interpolant_cmds(cmd_context & ctx) {
    ctx.insert(alloc(get_interpolant_cmd));
    ctx.insert(alloc(compute_interpolant_cmd));
    //    ctx.insert(alloc(get_and_check_interpolant_cmd));
}
Esempio n. 25
0
static void show_interpolant_and_maybe_check(cmd_context & ctx,
                                             ptr_vector<ast> &cnsts,
                                             expr *t, 
                                             ptr_vector<ast> &interps,
                                             params_ref &m_params,
                                             bool check)
{
  
    if (m_params.get_bool("som", false))
        m_params.set_bool("flat", true);
    th_rewriter s(ctx.m(), m_params);
  
    for(unsigned i = 0; i < interps.size(); i++){

        expr_ref r(ctx.m());
        proof_ref pr(ctx.m());
        s(to_expr(interps[i]),r,pr);

        ctx.regular_stream()  << mk_pp(r.get(), ctx.m()) << std::endl;
#if 0
        ast_smt_pp pp(ctx.m());
        pp.set_logic(ctx.get_logic().str().c_str());
        pp.display_smt2(ctx.regular_stream(), to_expr(interps[i]));
        ctx.regular_stream() << std::endl;
#endif
    }

    s.cleanup();

    // verify, for the paranoid...
    if(check || interp_params(m_params).check()){
        std::ostringstream err;
        ast_manager &_m = ctx.m();

        // need a solver -- make one here FIXME is this right?
        bool proofs_enabled, models_enabled, unsat_core_enabled;
        params_ref p;
        ctx.params().get_solver_params(_m, p, proofs_enabled, models_enabled, unsat_core_enabled);
        scoped_ptr<solver> sp = (ctx.get_solver_factory())(_m, p, false, true, false, ctx.get_logic());

        if(iz3check(_m,sp.get(),err,cnsts,t,interps))
            ctx.regular_stream() << "correct\n";
        else 
            ctx.regular_stream() << "incorrect: " << err.str().c_str() << "\n";
    }

    for(unsigned i = 0; i < interps.size(); i++){
        ctx.m().dec_ref(interps[i]);
    }

    interp_params itp_params(m_params);
    if(itp_params.profile())
        profiling::print(ctx.regular_stream());

}
Esempio n. 26
0
static void compute_interpolant(cmd_context & ctx, const ptr_vector<expr> &exprs, params_ref &m_params) {
    expr_ref foo(make_tree(ctx, exprs),ctx.m());
    compute_interpolant_and_maybe_check(ctx,foo.get(),m_params,false);
}
Esempio n. 27
0
static void get_interpolant(cmd_context & ctx, const ptr_vector<expr> &exprs, params_ref &m_params) {
    expr_ref foo(make_tree(ctx, exprs),ctx.m());
    get_interpolant(ctx,foo.get(),m_params);
}
Esempio n. 28
0
void install_qe_cmd(cmd_context & ctx, char const * cmd_name) {
    ctx.insert(alloc(qe_cmd, cmd_name));
}
Esempio n. 29
0
 virtual void execute(cmd_context & ctx) {
     if (m_target == 0)
         throw cmd_exception("invalid simplify command, argument expected");
     expr_ref r(ctx.m());
     proof_ref pr(ctx.m());
     if (m_params.get_bool("som", false))
         m_params.set_bool("flat", true);
     th_rewriter s(ctx.m(), m_params);
     unsigned cache_sz;
     unsigned num_steps = 0;
     unsigned timeout   = m_params.get_uint("timeout", UINT_MAX);
     bool failed = false;
     cancel_eh<th_rewriter> eh(s);
     { 
         scoped_ctrl_c ctrlc(eh);
         scoped_timer timer(timeout, &eh);
         cmd_context::scoped_watch sw(ctx);
         try {
             s(m_target, r, pr);
         }
         catch (z3_error & ex) {
             throw ex;
         }
         catch (z3_exception & ex) {
             ctx.regular_stream() << "(error \"simplifier failed: " << ex.msg() << "\")" << std::endl;
             failed = true;
             r = m_target;
         }
         cache_sz  = s.get_cache_size();
         num_steps = s.get_num_steps();
         s.cleanup();
     }
     if (m_params.get_bool("print", true)) {
         ctx.display(ctx.regular_stream(), r);
         ctx.regular_stream() << std::endl; 
     }
     if (!failed && m_params.get_bool("print_proofs", false)) {
         ast_smt_pp pp(ctx.m());
         pp.set_logic(ctx.get_logic().str().c_str());
         pp.display_expr_smt2(ctx.regular_stream(), pr.get());
         ctx.regular_stream() << std::endl;
     }
     if (m_params.get_bool("print_statistics", false)) {
         shared_occs s1(ctx.m());
         if (!failed)
             s1(r);
         unsigned long long max_mem = memory::get_max_used_memory();
         unsigned long long mem = memory::get_allocation_size();
         ctx.regular_stream() << "(:time " << std::fixed << std::setprecision(2) << ctx.get_seconds() << " :num-steps " << num_steps
                              << " :memory " << std::fixed << std::setprecision(2) << static_cast<double>(mem)/static_cast<double>(1024*1024)
                              << " :max-memory " << std::fixed << std::setprecision(2) << static_cast<double>(max_mem)/static_cast<double>(1024*1024)
                              << " :cache-size: " << cache_sz
                              << " :num-nodes-before " << get_num_exprs(m_target);
         if (!failed)
             ctx.regular_stream() << " :num-shared " << s1.num_shared() << " :num-nodes " << get_num_exprs(r);
         ctx.regular_stream() << ")" << std::endl;
     }
 }
Esempio n. 30
0
void install_simplify_cmd(cmd_context & ctx, char const * cmd_name) {
    ctx.insert(alloc(simplify_cmd, cmd_name));
}