#NEWLINE# def replace_leaf( arg , leaves , new_leaves , op , neg ) : #NEWLINE# #INDENT# '\n    Attempt to replace a leaf of a multiplication tree.\n\n    We search for a leaf in `leaves` whose argument is `arg`, and if we find\n    one, we remove it from `leaves` and add to `new_leaves` a leaf with\n    argument `arg` and variable `op(arg)`.\n\n    Parameters\n    ----------\n    arg\n        The argument of the leaf we are looking for.\n    leaves\n        List of leaves to look into. Each leaf should be a pair\n        (x, l) with `x` the argument of the Op found in the leaf, and `l` the\n        actual leaf as found in a multiplication tree output by `parse_mul_tree`\n        (i.e. a pair [boolean, variable]).\n    new_leaves\n        If a replacement occurred, then the leaf is removed from `leaves`\n        and added to the list `new_leaves` (after being modified by `op`).\n    op\n        A function that, when applied to `arg`, returns the Variable\n        we want to replace the original leaf variable with.\n    neg : bool\n        If True, then the boolean value associated to the leaf should\n        be swapped. If False, then this value should remain unchanged.\n\n    Returns\n    -------\n    bool\n        True if a replacement occurred, or False otherwise.\n\n    ' #NEWLINE# for ( idx , x ) in enumerate ( leaves ) : #NEWLINE# #INDENT# if ( x [ 0 ] == arg ) : #NEWLINE# #INDENT# x [ 1 ] [ 0 ] ^= neg #NEWLINE# x [ 1 ] [ 1 ] = op ( arg ) #NEWLINE# leaves . pop ( idx ) #NEWLINE# new_leaves . append ( x ) #NEWLINE# return True #NEWLINE# #UNINDENT# #UNINDENT# return False