'Replace all instances of variable v with expression E in self,
where v is free in self.'

def replace(self, variable, expression, replace_bound=False, alpha_convert=True):

    first = self.first
    second = self.second
    consequent = self.consequent
    if (variable in self.get_refs()):
        if replace_bound:
            first = first.replace(variable, expression, replace_bound, alpha_convert)
            second = second.replace(variable, expression, replace_bound, alpha_convert)
            if consequent:
                consequent = consequent.replace(variable, expression, replace_bound, alpha_convert)
    else:
        if alpha_convert:
            for ref in (set(self.get_refs(True)) & expression.free()):
                v = DrtVariableExpression(unique_variable(ref))
                first = first.replace(ref, v, True, alpha_convert)
                second = second.replace(ref, v, True, alpha_convert)
                if consequent:
                    consequent = consequent.replace(ref, v, True, alpha_convert)
        first = first.replace(variable, expression, replace_bound, alpha_convert)
        second = second.replace(variable, expression, replace_bound, alpha_convert)
        if consequent:
            consequent = consequent.replace(variable, expression, replace_bound, alpha_convert)
    return self.__class__(first, second, consequent)
