@ -32,6 +32,37 @@ A function will have to return the number of changes performed on the tree.
importoptimizer
importoptimizer
defreplace_node(replace,replacement):
'''This function replaces "replace" node with the node "with",
thefatherofthenodewillnowpointtothewithnode'''
replace.name=replacement.name
replace.kind=replacement.kind
ifreplace.kind==optimizer.UNARY:
replace.child=replacement.child
replace.prop=replacement.prop
elifreplace.kind==optimizer.BINARY:
replace.right=replacement.right
replace.left=replacement.left
defrecoursive_scan(function,node,rels=None):
changes=0
#recoursive scan
ifnode.kind==optimizer.UNARY:
ifrels!=None:
changes+=function(node.child,rels)
else:
changes+=function(node.child)
elifn.kind==optimizer.BINARY:
ifrels!=None:
changes+=function(node.right,rels)
changes+=function(node.left,rels)
else:
changes+=function(node.right)
changes+=function(node.left)
returnchanges
defduplicated_select(n):
defduplicated_select(n):
changes=0
changes=0
'''This function locates and deletes things like
'''This function locates and deletes things like
@ -53,21 +84,29 @@ def duplicated_select(n):
returnchanges
returnchanges
deffutile_union_intersection_subtraction(n):
deffutile_union_intersection_subtraction(n):
'''This function locates things like r ᑌ r, and replaces them with r'''
'''This function locates things like r ᑌ r, and replaces them with r.
σk(r)ᑌrwithr
σk(r)ᑎrwithσk(r)
'''
#TODO document into the wiki
#TODO document into the wiki
changes=0
changes=0
ifn.namein('ᑌ','ᑎ')andn.left==n.right:
ifn.namein('ᑌ','ᑎ')andn.left==n.right:
changes=1
changes=1
n.name=n.left.name
replace_node(n,n.left)
n.kind=n.left.kind
elif(n.name=='ᑌ'and((n.left.name=='σ'andn.left.child==n.right)or(n.right.name=='σ'andn.right.child==n.left))):#Union of two equal things, but one has a selection
ifn.kind==optimizer.UNARY:
changes=1
n.child=n.left.child
ifn.left!='σ':#Selection on left. replacing self with right.
n.prop=n.left.prop
replace_node(n,n.right)
elifn.kind==optimizer.BINARY:
else:#Selection on left. replacing self with right.
n.right=n.left.right
replace_node(n,n.left)
n.left=n.left.left
elif(n.name=='ᑎ'and((n.left.name=='σ'andn.left.child==n.right)or(n.right.name=='σ'andn.right.child==n.left))):#Intersection of two equal things, but one has a selection
pass
changes=1
ifn.left!='σ':#Swapping with the selection
replace_node(n,n.left)
else:
replace_node(n,n.right)
#elif (n.name == '-' and ((n.left.name=='σ' and n.left.child==n.right) or (n.right.name=='σ' and n.right.child==n.left))): #Intersection of two equal things, but one has a selection
elifn.name=='-'andn.left==n.right:#Empty relation
elifn.name=='-'andn.left==n.right:#Empty relation
changes=1
changes=1
n.kind=optimizer.UNARY
n.kind=optimizer.UNARY
@ -236,15 +275,7 @@ def futile_renames(n):
n.prop=n.prop[:-1]#Removing ending comma
n.prop=n.prop[:-1]#Removing ending comma
iflen(n.prop)==0:#Nothing to rename, removing the rename op
iflen(n.prop)==0:#Nothing to rename, removing the rename op
n.name=n.child.name
replace_node(n,n.child)
n.kind=n.child.kind
ifn.kind==optimizer.UNARY:
n.prop=n.child.prop
n.child=n.child.child
elifn.kind==optimizer.BINARY:
n.left=n.child.left
n.right=n.child.right
#recoursive scan
#recoursive scan
ifn.kind==optimizer.UNARY:
ifn.kind==optimizer.UNARY:
changes+=futile_renames(n.child)
changes+=futile_renames(n.child)
@ -297,14 +328,7 @@ def subsequent_renames(n):
n.prop=n.prop[:-1]#Removing ending comma
n.prop=n.prop[:-1]#Removing ending comma
iflen(n.prop)==0:#Nothing to rename, removing the rename op
iflen(n.prop)==0:#Nothing to rename, removing the rename op