/*++ Copyright (c) 2006 Microsoft Corporation Module Name: theory_recfun.h Abstract: Theory responsible for unrolling recursive functions Author: Leonardo de Moura (leonardo) 2008-10-31. Revision History: --*/ #ifndef THEORY_RECFUN_H_ #define THEORY_RECFUN_H_ #include "smt/smt_theory.h" #include "smt/smt_context.h" #include "ast/ast_pp.h" #include "ast/recfun_decl_plugin.h" namespace smt { class theory_recfun : public theory { struct stats { unsigned m_case_expansions, m_body_expansions, m_macro_expansions; void reset() { memset(this, 0, sizeof(stats)); } stats() { reset(); } }; // one case-expansion of `f(t1…tn)` struct case_expansion { expr * m_lhs; // the term to expand recfun_def * m_def; ptr_vector m_args; case_expansion(recfun_util& u, app * n) : m_lhs(n), m_def(0), m_args() { SASSERT(u.is_defined(n)); func_decl * d = n->get_decl(); const symbol& name = d->get_name(); m_def = &u.get_def(name); m_args.append(n->get_num_args(), n->get_args()); } case_expansion(case_expansion const & from) : m_lhs(from.m_lhs), m_def(from.m_def), m_args(from.m_args) {} case_expansion(case_expansion && from) : m_lhs(from.m_lhs), m_def(from.m_def), m_args(std::move(from.m_args)) {} }; struct pp_case_expansion { case_expansion & e; ast_manager & m; pp_case_expansion(case_expansion & e, ast_manager & m) : e(e), m(m) {} }; friend std::ostream& operator<<(std::ostream&, pp_case_expansion const &); // one body-expansion of `f(t1…tn)` using a `C_f_i(t1…tn)` struct body_expansion { recfun_case_def const * m_cdef; ptr_vector m_args; body_expansion(recfun_util& u, app * n) : m_cdef(0), m_args() { SASSERT(u.is_case_pred(n)); func_decl * d = n->get_decl(); const symbol& name = d->get_name(); m_cdef = &u.get_case_def(name); for (unsigned i = 0; i < n->get_num_args(); ++i) m_args.push_back(n->get_arg(i)); } body_expansion(recfun_case_def const & d, ptr_vector & args) : m_cdef(&d), m_args(args) {} body_expansion(body_expansion const & from): m_cdef(from.m_cdef), m_args(from.m_args) {} body_expansion(body_expansion && from) : m_cdef(from.m_cdef), m_args(std::move(from.m_args)) {} }; struct pp_body_expansion { body_expansion & e; ast_manager & m; pp_body_expansion(body_expansion & e, ast_manager & m) : e(e), m(m) {} }; friend std::ostream& operator<<(std::ostream&, pp_body_expansion const &); struct empty{}; typedef trail_stack th_trail_stack; typedef obj_map guard_set; recfun_decl_plugin& m_plugin; recfun_util& m_util; stats m_stats; th_trail_stack m_trail; guard_set m_guards; // true case-preds unsigned m_max_depth; // for fairness and termination vector m_q_case_expand; vector m_q_body_expand; vector m_q_clauses; recfun_util & u() const { return m_util; } ast_manager & m() { return get_manager(); } bool is_defined(app * f) const { return u().is_defined(f); } bool is_case_pred(app * f) const { return u().is_case_pred(f); } bool is_defined(enode * e) const { return is_defined(e->get_owner()); } bool is_case_pred(enode * e) const { return is_case_pred(e->get_owner()); } void reset_queues(); expr_ref apply_args(recfun::vars const & vars, ptr_vector const & args, expr * e); //!< substitute variables by args app_ref apply_pred(recfun::case_pred const & p, ptr_vector const & args); //0); m_max_depth = n; } }; } #endif