app_ref mk_loop_counter::add_arg(rule_set const& src, rule_set& dst, app* fn, unsigned idx) {
     expr_ref_vector args(m);
     func_decl* new_fn, *old_fn = fn->get_decl();
     args.append(fn->get_num_args(), fn->get_args());
     args.push_back(m.mk_var(idx, a.mk_int()));
     
     if (!m_old2new.find(old_fn, new_fn)) {
         ptr_vector<sort> domain;
         domain.append(fn->get_num_args(), old_fn->get_domain());
         domain.push_back(a.mk_int());
         new_fn = m.mk_func_decl(old_fn->get_name(), domain.size(), domain.c_ptr(), old_fn->get_range());
         m_old2new.insert(old_fn, new_fn);
         m_new2old.insert(new_fn, old_fn);
         m_refs.push_back(new_fn);
         m_ctx.register_predicate(new_fn, false);
         if (src.is_output_predicate(old_fn)) {
             dst.set_output_predicate(new_fn);
         }
     }
     return app_ref(m.mk_app(new_fn, args.size(), args.c_ptr()), m);
 }
    bool mk_subsumption_checker::transform_rules(const rule_set & orig, rule_set & tgt) {

        bool modified = false;

        func_decl_set total_relations_with_included_rules;

        rule_subsumption_index subs_index(m_context);

        rule_ref_vector orig_rules(m_context.get_rule_manager());
        orig_rules.append(orig.get_num_rules(), orig.begin());

        rule * * rbegin = orig_rules.c_ptr();
        rule * * rend = rbegin + orig_rules.size();

        //before traversing we sort rules so that the shortest are in the beginning.
        //this will help make subsumption checks more efficient
        std::sort(rbegin, rend, rule_size_comparator);

        for(rule_set::iterator rit = rbegin; rit!=rend; ++rit) {

            rule * r = *rit;
            func_decl * head_pred = r->get_decl();

            if(m_total_relations.contains(head_pred)) {
                if(!orig.is_output_predicate(head_pred) ||
                        total_relations_with_included_rules.contains(head_pred)) {
                    //We just skip definitions of total non-output relations as 
                    //we'll eliminate them from the problem.
                    //We also skip rules of total output relations for which we have 
                    //already output the rule which implies their totality.
                    modified = true;
                    continue;
                }
                rule * defining_rule;
                VERIFY(m_total_relation_defining_rules.find(head_pred, defining_rule));
                if (defining_rule) {
                    rule_ref totality_rule(m_context.get_rule_manager());
                    VERIFY(transform_rule(defining_rule, subs_index, totality_rule));
                    if(defining_rule!=totality_rule) {
                        modified = true;
                    }
                    tgt.add_rule(totality_rule);
                    SASSERT(totality_rule->get_decl()==head_pred);
                }
                else {
                    modified = true;
                }
                total_relations_with_included_rules.insert(head_pred);
                continue;
            }

            rule_ref new_rule(m_context.get_rule_manager());
            if(!transform_rule(r, subs_index, new_rule)) {
                modified = true;
                continue;
            }
            if(m_new_total_relation_discovery_during_transformation && is_total_rule(new_rule)) {
                on_discovered_total_relation(head_pred, new_rule.get());
            }
            if(subs_index.is_subsumed(new_rule)) {
                modified = true;
                continue;
            }
            if(new_rule.get()!=r) {
                modified = true;
            }
            tgt.add_rule(new_rule);
            subs_index.add(new_rule);
        }
        tgt.inherit_predicates(orig);
        TRACE("dl",
            tout << "original set size: "<<orig.get_num_rules()<<"\n"
                 << "reduced set size: "<<tgt.get_num_rules()<<"\n"; );