public static Expression simplifyBooleanExpression(Expression expr, boolean negate) {
        if (expr instanceof PrefixExpression) {
            PrefixExpression pe = (PrefixExpression) expr;
            if (pe.getOperator() != PrefixExpression.NOT) return expr;
            return simplifyBooleanExpression((Expression) pe.getOperand(), !negate);
        }
        
        if (expr instanceof InfixExpression && expr.getTypeBinding() == Type.BOOLEAN) {
            InfixExpression in = (InfixExpression) expr;
            InfixExpression.Operator op = in.getOperator();
            if (negate) {
                op = op.getComplement();
                if (op != InfixExpression.Operator.CONDITIONAL_AND && op != InfixExpression.Operator.CONDITIONAL_OR)
                    negate = false;
            }
            InfixExpression out = new InfixExpression(op);
            out.widen(in);
            out.setOperands(simplifyBooleanExpression(in.getLeftOperand(), negate), simplifyBooleanExpression(in.getRightOperand(), negate));
            return out;
        }
        
        if (negate) {
            PrefixExpression pe = new PrefixExpression();
            pe.setOperator(PrefixExpression.NOT);
            pe.setOperand(expr);
            return pe;
        }
        
        return expr;
    }