CNode* VariableEliminator::expand_neq_constraints(Clause& cl, set<Leaf*>& neqs)
{
	set<Leaf*>::iterator it = neqs.begin();
	for(; it!= neqs.end(); it++)
	{
		Leaf* l = *it;
		if(l->get_type() == ILP)
			cl.neg_ilp.erase((ILPLeaf*)l);
		else
			cl.neg_eq.erase((EqLeaf*)l);
	}

	CNode* node = cl.to_cnode();
	set<CNode*> to_and;
	to_and.insert(node);
	for(it=neqs.begin(); it!=neqs.end(); it++)
	{
		Leaf* l = *it;
		CNode* lt  = NULL;
		CNode* gt = NULL;
		if(l->get_type() == ILP) {
			ILPLeaf* ilp = (ILPLeaf*) l;
			lt = ILPLeaf::make(ILP_LT, ilp->get_elems(), ilp->get_constant());
			lt = lt->fold_negated_ilps();
			gt = ILPLeaf::make(ILP_GT, ilp->get_elems(), ilp->get_constant());
			gt = gt->fold_negated_ilps();
		}
		else {
			EqLeaf* eq = (EqLeaf*) l;
			map<Term*, long int> elems;
			elems[eq->get_lhs()] = 1;
			elems[eq->get_rhs()] = -1;
			lt = ILPLeaf::make(ILP_LT, elems, 0);
			lt = lt->fold_negated_ilps();
			gt = ILPLeaf::make(ILP_GT, elems, 0);
			gt = gt->fold_negated_ilps();
		}
		CNode* disjunct = Connective::make(OR, lt, gt);
		to_and.insert(disjunct);
	}
	return Connective::make_and(to_and);


}
bool VariableEliminator::expand_neq_constraints_with_bound(Clause& cl,
		set<Leaf*>& neqs, set<ILPLeaf*>& result, Term* evar, bool lt)
{
	set<Leaf*>::iterator it = neqs.begin();
	for(; it!= neqs.end(); it++)
	{
		Leaf* l = *it;
		if(l->get_type() == ILP){
			cl.neg_ilp.erase((ILPLeaf*)l);
			long int coef = ((ILPLeaf*)l)->get_coefficient(evar);
			if(coef < 0){
				CNode* n =((ILPLeaf*)l)->multiply(-1);
				n = n->fold_negated_ilps();
				assert(n->is_leaf());
				l = (Leaf*) n;
			}
		}
		else cl.neg_eq.erase((EqLeaf*)l);


		ILPLeaf* to_insert = NULL;
		if(lt) {
			CNode* _ilp_lt = NULL;
			if(l->get_type() == ILP) {
				ILPLeaf* ilp = (ILPLeaf*) l;
				_ilp_lt = ILPLeaf::make(ILP_LT, ilp->get_elems(),
						ilp->get_constant());
				_ilp_lt = _ilp_lt->fold_negated_ilps();

			}
			else {
				EqLeaf* eq = (EqLeaf*) l;
				map<Term*, long int> elems;
				elems[eq->get_lhs()] = 1;
				elems[eq->get_rhs()] = -1;
				_ilp_lt = ILPLeaf::make(ILP_LT, elems, 0);
				_ilp_lt = _ilp_lt->fold_negated_ilps();
			}
			cnode_type ct = _ilp_lt->get_type();
			if(ct == FALSE_NODE) return false;
			if(ct == TRUE_NODE) continue;
			assert(ct == ILP);
			to_insert = (ILPLeaf*) _ilp_lt;

		}
		else {
			CNode* _ilp_gt = NULL;
			if(l->get_type() == ILP) {
				ILPLeaf* ilp = (ILPLeaf*) l;
				_ilp_gt = ILPLeaf::make(ILP_GT, ilp->get_elems(),
						ilp->get_constant());
				_ilp_gt = _ilp_gt->fold_negated_ilps();
			}
			else {
				EqLeaf* eq = (EqLeaf*) l;
				map<Term*, long int> elems;
				elems[eq->get_lhs()] = 1;
				elems[eq->get_rhs()] = -1;
				_ilp_gt = ILPLeaf::make(ILP_GT, elems, 0);
				_ilp_gt = _ilp_gt->fold_negated_ilps();
			}
			cnode_type ct = _ilp_gt->get_type();
			if(ct == FALSE_NODE) return false;
			if(ct == TRUE_NODE) continue;
			assert(ct == ILP);
			to_insert = (ILPLeaf*) _ilp_gt;
		}

		cl.pos_ilp.insert(to_insert);
		result.insert(to_insert);


	}
	return true;
}