def rename_and_pull(f, quant, old_var1, old_var2, arg1, arg2):
     """ Helper function that renames given variable in a formula. """
     # when both variables are to be renamed, re-use the variable name
     # eg. (forall x: P(x) & forall y: Q(y)) <-> (forall x: P(x) & Q(x))
     new_var = None
     if old_var1: # rename left?
         new_var = variant(old_var1, fvars(f))
         a1 = subst({old_var1: new_var}, arg1)
     else:
         a1 = arg1
     if old_var2: # rename right?
         if not new_var:
             new_var = variant(old_var2, fvars(f))
         a2 = subst({old_var2: new_var}, arg2)
     else:
         a2 = arg2
     return Quantifier(quant, new_var, pullquants(Expr(f.op, a1 , a2)))
    def pushquants(f):
    #    print('pushquant({0})'.format(f))
        if is_quantified(f):
            arg = f.args[0]
            if arg.op == OP_NOT:
                return ~pushquants(Quantifier(opposite(f.op), f.vars, arg.args[0]))
            elif is_quantified(arg):
                arg1 = pushquants(arg)
                # did pushquants have any effect?
                if arg1 == arg:
#                    # if no, see if changing the order of the quants has an effect
#                    arg2 = Quantifier(f.op, f.vars, arg.args[0])
#                    arg2_ = pushquants(arg2)
#                    if arg2 == arg2_:
#                        return Quantifier(f.op, f.vars, arg1)
#                    else:
#                        return Quantifier(arg.op, arg.vars, arg2_)
                    return Quantifier(f.op, f.vars, arg1)
                else:
                    return pushquants(Quantifier(f.op, f.vars, arg1))
            elif arg.op in [OP_AND, OP_OR]:
                arg1 = arg.args[0]
                arg2 = arg.args[1]
                variable = f.vars[0]
                if variable in fvars(arg1) and variable in fvars(arg2):
                    if ((arg.op == OP_AND and f.op == OP_FORALL) or
                        (arg.op == OP_OR and f.op == OP_EXISTS)):
                        return pushquants(Expr(arg.op,
                                          Quantifier(f.op, f.vars, arg1),
                                          Quantifier(f.op, f.vars, arg2)))
                    else:
                        return Quantifier(f.op, f.vars, pushquants(arg))
                if variable in fvars(arg1):
                    return pushquants(Expr(arg.op,
                                      Quantifier(f.op, f.vars, arg1), arg2))
                if variable in fvars(arg2):
                    return pushquants(Expr(arg.op, arg1,
                                           Quantifier(f.op, f.vars, arg2)))
                else:
                    get_log().warning('Dropped quantifier "{0} : {1}" from "{2}"'
                                      .format(f.op, f.vars, f))
                    return arg
            elif arg.op == OP_IMPLIES:
                arg1 = arg.args[0]
                arg2 = arg.args[1]
                variable = f.vars[0]
                if variable in fvars(arg1) and variable in fvars(arg2):
                    return Quantifier(f.op, f.vars, pushquants(arg))
                elif variable in fvars(arg1):
                    quant = opposite(f.op)
                    return (pushquants(Quantifier(quant, variable, arg1)) >>
                            pushquants(arg2))
                elif variable in fvars(arg2):
                    return (pushquants(arg1) >>
                            pushquants(Quantifier(f.op, variable, arg2)))
                else:
                    get_log().warning('Dropped quantifier "{0} : {1}" from "{2}"'
                                      .format(f.op, f.vars, f))
                    return (pushquants(arg1) >> pushquants(arg2))
            elif arg.op == OP_IMPLIED_BY:
                arg1 = arg.args[0]
                arg2 = arg.args[1]
                variable = f.vars[0]
                if variable in fvars(arg1) and variable in fvars(arg2):
                    return Quantifier(f.op, f.vars, pushquants(arg))
                elif variable in fvars(arg1):
                    return (pushquants(Quantifier(f.op, variable, arg1)) <<
                            pushquants(arg2))
                elif variable in fvars(arg2):
                    quant = opposite(f.op)
                    return (pushquants(arg1) <<
                            pushquants(Quantifier(quant, variable, arg2)))
                else:
                    get_log().warning('Dropped quantifier "{0} : {1}" from "{2}"'
                                      .format(f.op, f.vars, f))
                    return (pushquants(arg1) << pushquants(arg2))
            else:
                return Quantifier(f.op, f.vars[0], pushquants(f.args[0]))
        elif f.op == OP_NOT:
            return ~pushquants(f.args[0])
        elif f.op in BINARY_LOGIC_OPS:
            return Expr(f.op, *[pushquants(x) for x in f.args])
        else:
            return f
def kleene(f):
    """ Take a FOL formula and try to simplify it. """
#    print('\nSimplifying op "{0}" args "{1}"'.format(f.op, f.args))
    if is_quantified(f): # remove variable that are not in f
        vars = set(f.vars)
        fv = fvars(f.args[0])
        needed = vars & fv
        if needed == set():
            return kleene(f.args[0])
        else:
            arg = kleene(f.args[0])
            variables = [v for v in f.vars if v in needed] # keep the same order
            if (arg != f.args[0]) or (len(needed) < len(f.vars)):
                return kleene(Quantifier(f.op, variables, arg))
            else:
                return f
    elif f.op == OP_NOT:
        arg = f.args[0]
        if arg.op == OP_NOT: # double neg
            return kleene(arg.args[0])
        elif is_true(arg): # -TRUE --> FALSE
            return Expr(OP_FALSE)
        elif is_false(arg): # -FALSE --> TRUE
            return Expr(OP_TRUE)
        elif arg.op == OP_EQUALS:
            return Expr(OP_NOTEQUALS,
                        kleene(arg.args[0]),
                        kleene(arg.args[1]))
        elif arg.op == OP_NOTEQUALS:
            return Expr(OP_EQUALS,
                        kleene(arg.args[0]),
                        kleene(arg.args[1]))
        else:
            arg2 = kleene(arg)
            if arg2 != arg:
                return kleene(Expr(OP_NOT, arg2))
            return f
    elif f.op == OP_AND:
        if any(map(is_false, f.args)): # if one conjuct is FALSE, expr is FALSE
            return Expr(OP_FALSE)
        elif len(f.args) == 1:
            return kleene(f.args[0])
        else: # remove conjuncts that are TRUE and simplify args
            args = list(map(kleene, filter(lambda x: not is_true(x),f.args)))
            used = set()
            unique = []
            for arg in args:
                if arg not in used:
                    used.add(arg)
                    unique.append(arg)
            if args != unique or f.args != unique:
                return kleene(Expr(OP_AND, *unique))
            else:
                return f
    elif f.op == OP_OR:
        if any(map(is_true, f.args)): # if one conjuct is TRUE, expr is TRUE
            return Expr(OP_TRUE)
        elif len(f.args) == 1:
            return kleene(f.args[0])
        else: # remove conjuncts that are FALSE and simplify args
            args = list(map(kleene, filter(lambda x: not is_false(x),f.args)))
            used = set()
            unique = []
            for arg in args:
                if arg not in used:
                    used.add(arg)
                    unique.append(arg)
            if args != unique or f.args != unique:
                return kleene(Expr(OP_OR, *unique))
            else:
                return f
    elif f.op == OP_IMPLIES:
        if is_false(f.args[0]) or is_true(f.args[1]):
            return Expr(OP_TRUE)
        elif is_true(f.args[0]):
            return kleene(f.args[1])
        elif is_false(f.args[1]):
            return kleene(Expr(OP_NOT, kleene(f.args[0])))
        else:
            arg1 = kleene(f.args[0])
            arg2 = kleene(f.args[1])
            if arg1 != f.args[0] or arg2 != f.args[1]:
                return kleene(Expr(OP_IMPLIES, arg1, arg2))
            else:
                return f
    elif f.op == OP_IMPLIED_BY:
        if is_false(f.args[1]) or is_true(f.args[0]):
            return Expr(OP_TRUE)
        elif is_true(f.args[1]):
            return kleene(f.args[0])
        elif is_false(f.args[0]):
            return kleene(Expr(OP_NOT, kleene(f.args[1])))
        else:
            arg1 = kleene(f.args[0])
            arg2 = kleene(f.args[1])
            if arg1 != f.args[0] or arg2 != f.args[1]:
                return kleene(Expr(OP_IMPLIES, arg1, arg2))
            else:
                return f
    elif f.op == OP_EQUIVALENT:
        if is_true(f.args[0]):
            return kleene(f.args[1])
        elif is_true(f.args[1]):
            return kleene(f.args[0])
        elif is_false(f.args[0]):
            return kleene(Expr(OP_NOT, kleene(f.args[1])))
        elif is_false(f.args[1]):
            return kleene(Expr(OP_NOT, kleene(f.args[0])))
        else:
            arg1 = kleene(f.args[0])
            arg2 = kleene(f.args[1])
            if arg1 != f.args[0] or arg2 != f.args[1]:
                return kleene(Expr(OP_EQUIVALENT, arg1, arg2))
            else:
                return f
    else:
        return f