#NEWLINE# def replace( self , variable , expression , replace_bound = False , alpha_convert = True ) : #NEWLINE# #INDENT# 'Replace all instances of variable v with expression E in self,\n        where v is free in self.' #NEWLINE# if ( variable in self . refs ) : #NEWLINE# #INDENT# if ( not replace_bound ) : #NEWLINE# #INDENT# return self #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# i = self . refs . index ( variable ) #NEWLINE# if self . consequent : #NEWLINE# #INDENT# consequent = self . consequent . replace ( variable , expression , True , alpha_convert ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# consequent = None #NEWLINE# #UNINDENT# return DRS ( ( ( self . refs [ : i ] + [ expression . variable ] ) + self . refs [ ( i + 1 ) : ] ) , [ cond . replace ( variable , expression , True , alpha_convert ) for cond in self . conds ] , consequent ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if alpha_convert : #NEWLINE# #INDENT# for ref in ( set ( self . refs ) & expression . free ( ) ) : #NEWLINE# #INDENT# newvar = unique_variable ( ref ) #NEWLINE# newvarex = DrtVariableExpression ( newvar ) #NEWLINE# i = self . refs . index ( ref ) #NEWLINE# if self . consequent : #NEWLINE# #INDENT# consequent = self . consequent . replace ( ref , newvarex , True , alpha_convert ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# consequent = None #NEWLINE# #UNINDENT# self = DRS ( ( ( self . refs [ : i ] + [ newvar ] ) + self . refs [ ( i + 1 ) : ] ) , [ cond . replace ( ref , newvarex , True , alpha_convert ) for cond in self . conds ] , consequent ) #NEWLINE# #UNINDENT# #UNINDENT# if self . consequent : #NEWLINE# #INDENT# consequent = self . consequent . replace ( variable , expression , replace_bound , alpha_convert ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# consequent = None #NEWLINE# #UNINDENT# return DRS ( self . refs , [ cond . replace ( variable , expression , replace_bound , alpha_convert ) for cond in self . conds ] , consequent )