def first_power_of_x_r(tree, entire): """"Performs x = x ^ 1 Give the x as cursor """ one = Tree("suc", [Token("NUMBER", "0")]) power = Tree("pow", []) if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e power.children = [tree, one] tree = power grandparent.children[treeindex] = tree return grandparent, entire else: power.children = [tree, one] entire = power return tree, entire
def plus_zero_r(tree, entire): """" Performs x = x + 0 """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): if child is tree: treeindex = e add = Tree("add", []) zerotoken = Token("NUMBER", "0") add.children = [tree, zerotoken] grandparent.children[treeindex] = add return tree, entire else: add = Tree("add", []) zerotoken = Token("NUMBER", "0") add.children = [tree, zerotoken] entire = add return tree, entire
def times_identity_r(tree, entire): """"Performs x = x * 1 Give the x as cursor """ mul = Tree("mul", []) one = Tree("suc", [Token("NUMBER", "0")]) if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e # Check this mul.children = [tree, one] tree = mul grandparent.children[treeindex] = tree return grandparent, entire else: mul.children = [entire, one] entire = mul return tree, entire
def distributivity_power_times_r(tree, entire): """" Performs x ^ z * x ^ z = (x * y) ^ z """ # TODO Check this with the axioms if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e x = tree.children[0].children[0] z = tree.children[0].children[1] y = tree.children[1].children[0] z2 = tree.children[1].children[1] assert z == z2 op1 = Tree("pow", []) op2 = Tree("mul", []) op2.children = [x, y] op1.children = [op2, z] tree = op1 grandparent.children[treeindex] = tree return tree, entire else: x = entire.children[0].children[0] z = entire.children[0].children[1] y = entire.children[1].children[0] z2 = entire.children[1].children[1] assert z == z2 op1 = Tree("pow", []) op2 = Tree("mul", []) op2.children = [x, y] op1.children = [op2, z] entire = op1 return tree, entire
def distributivity_power_plus_r(tree, entire): """" Performs x ^ y * x ^ z = x ^ (y + z) """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e x = tree.children[0].children[0] x2 = tree.children[1].children[0] assert x == x2 y = tree.children[0].children[1] z = tree.children[1].children[1] op1 = Tree("pow", []) op2 = Tree("add", []) op2.children = [y, z] op1.children = [x, op2] tree = op1 grandparent.children[treeindex] = tree return tree, entire else: x = entire.children[0].children[0] x2 = entire.children[1].children[0] assert x == x2 y = entire.children[0].children[1] z = entire.children[1].children[1] op1 = Tree("pow", []) op2 = Tree("add", []) op2.children = [y, z] op1.children = [x, op2] entire = op1 return tree, entire
def recursive_times_l(tree, entire): """" Performs x * s(y) = x * y + x Supply the cursor at * """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e # Make the new add node add = Tree("add", []) # This cop seems unnecessary too # Leaving it for reference # cop = tree grandparent.children[treeindex] = add # Copy the x term that will be in both multiply and add op x = copy.deepcopy(tree.children[0]) # Move the child over the S() to the y tree.children[1] = tree.children[1].children[0] # Give the add node its children add.children = [tree, x] return grandparent, entire else: # Need a new op, the add add = Tree("add", []) # Cop is unnecessary # cop = entire x = copy.deepcopy(entire.children[0]) entire.children[1] = entire.children[1].children[0] add.children = [entire, x] entire = add return tree, entire
def distributivity_power_power_l(tree, entire): """" Performs (x ^ y) ^ z= x ^ (y * z) """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e x = tree.children[0].children[0] y = tree.children[0].children[1] z = tree.children[1] op1 = Tree("mul", []) op2 = Tree("pow", []) op1.children = [y, z] op2.children = [x, op1] tree = op2 grandparent.children[treeindex] = tree return tree, entire else: x = entire.children[0].children[0] y = entire.children[0].children[1] z = entire.children[1] op1 = Tree("mul", []) op2 = Tree("pow", []) op1.children = [y, z] op2.children = [x, op1] entire = op2 return tree, entire
def insert_cursor_node(tree, entire): if hasattr(tree, "parent") and tree.parent is not None: gp = tree.parent # copy = tree cursor_tag = Tree("cursor", []) cursor_tag.children = [tree] for e, child in enumerate(gp.children): # print(child, tree, e) if child is tree: gp.children[e] = cursor_tag return entire else: cursor_tag = Tree("cursor", []) cursor_tag.children = [tree] return cursor_tag
def recursive_power_l(tree, entire): """" Performs x ^ s(y) = x ^ y * x Supply the cursor at * """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e # Make the new mul node mul = Tree("mul", []) grandparent.children[treeindex] = mul # Copy the x term that will be in both power and mul op x = copy.deepcopy(tree.children[0]) # Move the child over the S() to the y tree.children[1] = tree.children[1].children[0] # Give the mul node its children mul.children = [tree, x] return grandparent, entire else: # Need a new op, the mul mul = Tree("mul", []) x = copy.deepcopy(entire.children[0]) entire.children[1] = entire.children[1].children[0] mul.children = [entire, x] entire = mul return tree, entire
def recursive_times_r(tree, entire): """" Performs x * y + x -> x * s(y) """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e new_suc = Tree("suc", []) # cop = tree new_root = tree.children[0] new_suc.children = [new_root.children[1]] new_root.children[1] = new_suc grandparent.children[treeindex] = new_root return tree, entire else: new_suc = Tree("suc", []) # cop = entire new_root = entire.children[0] y = new_root.children[1] new_suc.children = [y] new_root.children[1] = new_suc entire = new_root return tree, entire
def distributivity_power_times_l(tree, entire): """" Performs (x * y) ^ z = x ^ z * x ^ z """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e z = tree.children[1] x = tree.children[0].children[0] y = tree.children[0].children[1] z2 = copy.deepcopy(z) op1 = Tree("pow", []) op2 = Tree("pow", []) op3 = Tree("mul", []) op1.children = [x, z] op2.children = [y, z2] op3.children = [op1, op2] tree = op3 grandparent.children[treeindex] = tree return tree, entire else: z = entire.children[1] x = entire.children[0].children[0] y = entire.children[0].children[1] z2 = copy.deepcopy(z) op1 = Tree("pow", []) op2 = Tree("pow", []) op3 = Tree("mul", []) op1.children = [x, z] op2.children = [y, z2] op3.children = [op1, op2] entire = op3 return tree, entire
def distributivity_power_plus_l(tree, entire): """" Performs x ^ (y + z) = x ^ y * x ^ z """ if hasattr(tree, "parent") and tree.parent is not None: grandparent = tree.parent for e, child in enumerate(grandparent.children): # print(child, tree, e) if child is tree: treeindex = e x = tree.children[0] y = tree.children[1].children[0] z = tree.children[1].children[1] x2 = copy.deepcopy(x) op1 = Tree("pow", []) op2 = Tree("pow", []) op3 = Tree("mul", []) op1.children = [x, y] op2.children = [x2, z] op3.children = [op1, op2] tree = op3 grandparent.children[treeindex] = tree return tree, entire else: x = entire.children[0] y = entire.children[1].children[0] z = entire.children[1].children[1] x2 = copy.deepcopy(x) op1 = Tree("pow", []) op2 = Tree("pow", []) op3 = Tree("mul", []) op1.children = [x, y] op2.children = [x2, z] op3.children = [op1, op2] entire = op3 return tree, entire
def _add_new_token(tree: Tree, new_token: Token) -> Tree: tree.children = [new_token] return tree