/**
   \brief Return true if m is a wellformed monomial.
*/
bool poly_simplifier_plugin::wf_monomial(expr * m) const {
    SASSERT(!is_add(m));
    if (is_mul(m)) {
        app * curr = to_app(m);
        expr * pp  = 0;
        if (is_numeral(curr->get_arg(0)))
            pp = curr->get_arg(1);
        else
            pp = curr;
        if (is_mul(pp)) {
            for (unsigned i = 0; i < to_app(pp)->get_num_args(); i++) {
                expr * arg = to_app(pp)->get_arg(i);
                CTRACE("wf_monomial_bug", is_mul(arg), 
                       tout << "m:  "  << mk_ismt2_pp(m, m_manager) << "\n";
                       tout << "pp: "  << mk_ismt2_pp(pp, m_manager) << "\n";
                       tout << "arg: " << mk_ismt2_pp(arg, m_manager) << "\n";
                       tout << "i:  " << i << "\n";
                       );
                SASSERT(!is_mul(arg));
                SASSERT(!is_numeral(arg));
            }
        }
    }
void arith_simplifier_plugin::div_monomial(expr_ref_vector& monomials, numeral const& g) {
    numeral n;
    for (unsigned  i = 0; i < monomials.size(); ++i) {
        expr* e = monomials[i].get();
        if (is_numeral(e, n)) {
            SASSERT((n/g).is_int());
            monomials[i] = mk_numeral(n/g);
        }
        else if (is_mul(e) && is_numeral(to_app(e)->get_arg(0), n)) {
            SASSERT((n/g).is_int());
            monomials[i] = mk_mul(n/g, to_app(e)->get_arg(1));
        }
        else {
            UNREACHABLE();
        }
    }
}
void arith_simplifier_plugin::get_monomial_gcd(expr_ref_vector& monomials, numeral& g) {
    g = numeral::zero();
    numeral n;
    for (unsigned  i = 0; !g.is_one() && i < monomials.size(); ++i) {
        expr* e = monomials[i].get();
        if (is_numeral(e, n)) {
            g = gcd(abs(n), g);
        }
        else if (is_mul(e) && is_numeral(to_app(e)->get_arg(0), n)) {
            g = gcd(abs(n), g);        
        }
        else {
            g = numeral::one();
            return;
        }
    }
    if (g.is_zero()) {
        g = numeral::one();
    }
}