Module GrbTrRemoveMuxStrictCycles.Tree


module Tree: sig .. end
Module that creates a tree and contains functions that work on a tree


type treeNode = {
   number : GrbTrRemoveMuxStrictCycles.NodeNumber.num; (*Number the node has in depth first search*)
   allDone : bool; (*true if we have finished search in this node - all children have been found and passed*)
   node : GrbGraphs.nodetype; (*DG.t graph node*)
   children : GrbGraphs.NewName.idtype list; (*list of all children in order of which they are passed*)
}
Tree node, contains info about corresponding graph node, the number the node has in depth first search and if all children of this node have been found

type treeEdgeType =
| Normal (*Actual tree edge*)
| Cross (*Edge going to a part of graph we have already searched*)
| Forward (*Edge going to a branch starting from source node*)
| Backward (*Edge going to an ancestor in the tree*)
| Unknown (*Edge that has not been passed in depth first search*)
Possible types the graph edges can have in a tree

type treeEdge = {
   edge : GrbGraphs.connectiontype * GrbGraphs.NewName.idtype * GrbGraphs.portname; (*Tree edge in DG.t*)
   edgeway : treeEdgeType; (*Type the edge has in the tree*)
   used : bool; (*true if this edges has already been passed*)
}
Tree edge

type treetype = {
   treeEdges : treeEdge GrbGraphs.IdtMap.t; (*Map for tree edges, key is the graph edge id (connectiontype.nameforudg)*)
   treeNodes : treeNode GrbGraphs.IdtMap.t; (*Map for tree nodes, key is the graph node id (nodetype.id)*)
   starting_places : GrbGraphs.NewName.idtype list; (*List of beginning nodes so that they are always taken in same order*)
}
Tree
val emptyTree : treetype
A tree with no nodes and edges
val report2 : string -> int
Logging and debugging method, writes information about transformation to special file
Returns int 0
Parameters:
logtext : string
- text to be appended to the log file
val hasnode : GrbGraphs.IdtMap.key -> treetype -> bool
Boolean function to check if a tree has a node with certain id
Returns boolean - true if given tree has node with nid
Parameters:
nid : GrbGraphs.IdtMap.key
- node id
tree : treetype
- tree where the node could be
val hasedge : GrbGraphs.IdtMap.key -> treetype -> bool
Boolean function to check if a tree has an edge with certain id
Returns boolean - true if given tree has edge with eid
Parameters:
eid : GrbGraphs.IdtMap.key
- edge id
tree : treetype
- tree where the edge could be
val findnode : GrbGraphs.IdtMap.key ->
treetype ->
treeNode
Function to find a node with given id
Raises Not_found - is NodeMap does not have that element
Returns treeNode - node with given id,
Parameters:
nid : GrbGraphs.IdtMap.key
- node id
tree : treetype
- treetype
val findedge : GrbGraphs.IdtMap.key ->
treetype ->
treeEdge
Function to find an edge with given id
Raises Not_found - is EdgeMap does not have that element
Returns edge with given id
Parameters:
eid : GrbGraphs.IdtMap.key
- edge id
tree : treetype
- tree of treetype
val find_edge_id : GrbGraphs.NewName.idtype ->
GrbGraphs.NewName.idtype ->
treetype -> GrbGraphs.NewName.idtype
Function to find an edge between given nodes, works only if an edge is present
Raises Failure - if there is no such edge
Returns edge id for edge from source to target
Parameters:
source_node : GrbGraphs.NewName.idtype
- source node id
end_node : GrbGraphs.NewName.idtype
- target node id
tree : treetype
- tree, where given edge should exist
val has_edge_id : GrbGraphs.NewName.idtype ->
GrbGraphs.NewName.idtype -> treetype -> bool
Function to to check if an edge between given nodes exists,
Returns boolean, true if the edge between given nodes is present
Parameters:
source_node : GrbGraphs.NewName.idtype
- source node id
end_node : GrbGraphs.NewName.idtype
- target node id
tree : treetype
- tree, where given edge should exist
val graph_from_tree : treetype -> GrbGraphs.DG.t
Function that makes a DG.t type graph from tree
Returns graph of DG.t type
Parameters:
tree : treetype
- tree
val edgefilter : treetype ->
(GrbGraphs.IdtMap.key -> treeEdge -> bool) ->
treeEdge list
Function that maps all edges in a tree, that the filter function allows, to a list
Returns List of edges that gave result true in filter_fun, treeEdge list
Parameters:
tree : treetype
- tree whose edges are listed
filter_fun : GrbGraphs.IdtMap.key -> treeEdge -> bool
- two parameter boolean function, first is edge key in a map, second is treeEdge
val addNewNode : treetype ->
GrbGraphs.nodetype ->
GrbGraphs.NewName.idtype list -> treetype
Add new node with no extra information, number will be invalid
Returns tree with given node, node id remains the same
Parameters:
tree : treetype
- tree where the node is added
nodet : GrbGraphs.nodetype
- graph node
outputs : GrbGraphs.NewName.idtype list
- list of all children
val addNewEdge : treetype ->
GrbGraphs.connectiontype * GrbGraphs.NewName.idtype * GrbGraphs.portname ->
treetype
Add edge with no edgeway
Returns tree with given edge, edgeway is Unknown and used is false
Parameters:
tree : treetype
- treewhere the edge is added
(conn,node,port) : GrbGraphs.connectiontype * GrbGraphs.NewName.idtype * GrbGraphs.portname
port : - portname for the edge
node : - target node for graph edge
conn : - connection part of graph edge
val nodelist : GrbGraphs.DG.t -> GrbGraphs.nodetype list
Parameters:
gr : GrbGraphs.DG.t
val edgelist_sub : GrbGraphs.DG.t ->
(GrbGraphs.connectiontype * GrbGraphs.nodetype * GrbGraphs.portname) list
Get a (connectiontype,nodetype,portname) list of all edges in graph
Returns edge list
Parameters:
gr : GrbGraphs.DG.t
- graph
val edgelist : GrbGraphs.DG.t ->
(GrbGraphs.connectiontype * GrbGraphs.NewName.idtype * GrbGraphs.portname)
list
Get a (connectiontype,NewName.idtype,portname) list of all edges in graph
Returns edge list where target nodes are marked by their identificators
Parameters:
gr : GrbGraphs.DG.t
- graph
val mapToTreeNodes : GrbGraphs.nodetype list ->
treetype ->
GrbGraphs.DG.t -> treetype
Get tree with graph nodes without extra information
Returns tree with all nodes in graphnodelist (for all added nodes: number is invalid, allDone is set false and children list contains all childnodes)
Parameters:
graphnodelist : GrbGraphs.nodetype list
- list of nodes that are added to tree
tree : treetype
- tree where the node should be added
graph : GrbGraphs.DG.t
- graph where the node is in
val mapToTreeEdges : (GrbGraphs.connectiontype * GrbGraphs.NewName.idtype * GrbGraphs.portname)
list ->
treetype ->
treetype
Get tree with added edges
Returns tree with added edges (all edges have Unknown edgeway and are marked unused)
Parameters:
graphedgelist : (GrbGraphs.connectiontype * GrbGraphs.NewName.idtype * GrbGraphs.portname)
list

- list of edges to be added
tree : treetype
val isInputNode : GrbGraphs.nodetype -> bool
Boolean function to check if a node has no incoming edges
Returns boolean, true if node has no incoming edges
Parameters:
node : GrbGraphs.nodetype
- graph node to be checked
val findBeginnings : GrbGraphs.nodetype list -> GrbGraphs.NewName.idtype list
Finds nodetype list of all input nodes, input nodes are the ones that hav no incoming edges
Returns list of all input nodes in nodelist
Parameters:
nodelist : GrbGraphs.nodetype list
- list of all possible nodes
val createTree : GrbGraphs.DG.t -> treetype
Create a tree with just information about the graph
Returns tree where all edgeways are Unknown and all node numbers are invalid
Parameters:
gr : GrbGraphs.DG.t
- graph
val markedNode : treetype ->
treeNode ->
treetype
Give node the next available number
Returns tree with number for nodet
Parameters:
tree : treetype
- tree where the node is in
nodet : treeNode
- treeNode for numbering
val usedNode : treetype ->
treeNode ->
treetype
Fix that we have seen all edges going out from this node
Returns tree where allDone for nodet is true
Parameters:
tree : treetype
- three where the node is in
nodet : treeNode
- treeNode that has been passed
val usedEdge : treetype ->
treeEdge ->
treeEdgeType ->
treetype
Fix edgeway and that an edge has been used
Returns tree where edgeway for edget is etype and used is true
Parameters:
tree : treetype
- tree where the edge id in
edget : treeEdge
- edge without edgeway
etype : treeEdgeType
- edgeway for edget
val work : treetype ->
GrbGraphs.IdtMap.key list ->
treetype * GrbGraphs.IdtMap.key list
Depth first search method for determining what to do with given node - see another out edge or fix that all have been seen
Returns pair, first is the tree with additional depth first search info, second is a list for nodes to see in future
Parameters:
tree : treetype
- tree where the search is performed
() : GrbGraphs.IdtMap.key list
val depth : treetype ->
GrbGraphs.IdtMap.key list -> treetype
Depth first search for fixing node numbers and edeways
Returns tree with extra information
Parameters:
tree : treetype
- tree where edeways and numbers need to be fixes
worklist : GrbGraphs.IdtMap.key list
- list of nodes to see, should contain all input nodes at first (tree.starting_places)
val rearrange : treetype ->
GrbGraphs.IdtMap.key ->
GrbGraphs.IdtMap.key list -> treetype
Rearrange tree so that nodes in chosen cycle are all in the leftmost branch of the subtree
Returns treetype - tree with fixed subtree
Parameters:
tree : treetype
- treetype tree where the nodes are in
start : GrbGraphs.IdtMap.key
- the node where the subtree starts, must be present in the cycle
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
val clean : treetype ->
treetype
Function that cleans tree from all extra information before starting to build the tree again, also it corrects all info about nodes children and input nodes
Returns tree with no depth first search information
Parameters:
tree : treetype
- tree to be cleaned
val new_tree : treetype ->
treetype
Function that cleans and builds new depth first search tree based on given tree
Returns treetype - new tree as similar to the first as possible
Parameters:
tree : treetype
- tree where depth first search is performed again
val depth_tree : treetype ->
treetype
Function that creates first tree
Returns treetype - tree with depth first search information
Parameters:
tree : treetype
- tree without information
val is_child : GrbGraphs.IdtMap.key ->
GrbGraphs.NewName.idtype -> treetype -> bool
Boolean function to check if there is a path of Normal edges from parent to child, checks if parent node id indeed a parent of child node
Returns boolean - true if asked parent is a parent of child node
Parameters:
child : GrbGraphs.IdtMap.key
- id for the child node
parent : GrbGraphs.NewName.idtype
- id for the parent node
tree : treetype
- tree that contains parent and child
val is_MUXor : GrbGraphs.IdtMap.key -> treetype -> bool
Boolean function to check if given node is unstrict
Returns boolean - true if node fith treenodeid is unstrict
Parameters:
treenodeid : GrbGraphs.IdtMap.key
- id of node
tree : treetype
- tree where the node is
val is_MUX : GrbGraphs.IdtMap.key -> treetype -> bool
Boolean function to check if given node is MUX (ShortMux)
Returns boolean - true if node with treenodeid is MUX
Parameters:
treenodeid : GrbGraphs.IdtMap.key
- id of the node
tree : treetype
- tree where the node is
val is_OR : GrbGraphs.IdtMap.key -> treetype -> bool
Boolean function to check if given node is Or
Returns boolean - true if node with treenodeid is Or
Parameters:
treenodeid : GrbGraphs.IdtMap.key
- id of the node
tree : treetype
- tree where the node is
val has_MUXOr : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has unctrict nodes
Returns boolean - true if cycle contains at least one unstrict node
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val has_or : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has Or nodes
Returns boolean - true if cycle contains at least one Or node
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val has_MUX : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has MUX nodes
Returns boolean - true if cycle contains at least one MUX node
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val has_many_MUXOr : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has multiple unstrict nodes
Returns boolean - true if cycle contains at least two unstrict nodes
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val has_one_or : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has exactly one Or node
Returns boolean - true if cycle contains at exactly one Or node
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val has_one_MUX : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has exactly one MUX node
Returns boolean - true if cycle contains at exactly one MUX node
Parameters:
cycle : GrbGraphs.IdtMap.key list
-list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val has_neighbour_MUXOrs : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if cycle has edge whose target and source are both unstrict nodes
Returns boolean - true if cycle contains edge with unstrict target and source
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val find_two_mux_edge : GrbGraphs.IdtMap.key list ->
treetype -> GrbGraphs.NewName.idtype
Function to find an edge whose target and source are both unstrict nodes
Returns id of edge with unstrict target and source in the cycle
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val upper_is_or : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if the upper node (exept the node where backward input goes to) is Or
Returns boolean - true if starting from the end of cycle list (except last node) first unstrict node id Or
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val upper_mux_edge : GrbGraphs.IdtMap.key list ->
treetype -> GrbGraphs.NewName.idtype
Function to find an upper edge in cycle that starts from MUX
Returns NewName.idtype - id of edge
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val upper_or_edge : GrbGraphs.IdtMap.key list ->
treetype -> GrbGraphs.NewName.idtype
Function to find an upper edge in cycle that starts from Or
Returns id of edge
Parameters:
cycle : GrbGraphs.IdtMap.key list
- where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val backward_end_MUX : GrbGraphs.IdtMap.key list -> treetype -> bool
Boolean function to check if the last element in cycle list is unstrict
Returns boolean - true if last element (where backward edge ends) is unstrict
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree where the cycle exists
val backward_edges : treetype ->
treeEdge list
Function that lists all Backward edges in a tree
Returns treeEdges list - list of all Backward edges in a tree
Parameters:
tree : treetype
- tree where to look for backward edges
val normal_edges_until : GrbGraphs.NewName.idtype ->
GrbGraphs.NewName.idtype ->
treetype -> GrbGraphs.NewName.idtype list
Find a list of nodes that have Normal edges between them and normal path connects edgeend_id and findal_id, path is searched starting from below
Returns cycle: list where every node has a Normal edge to the one before it in the list
Parameters:
edgeend_id : GrbGraphs.NewName.idtype
- target node for last Normal edge in path
final_id : GrbGraphs.NewName.idtype
- source of the path
tree : treetype
val find_normal_cycles : treetype ->
treeEdge list ->
GrbGraphs.NewName.idtype list list
Find all cycles that contain one Backward edge and Normal edges
Returns list of cycles (cycle = NewName.idtype list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward)
Parameters:
tree : treetype
- tree
backwardedges : treeEdge list
- list of backward edges whose cycles are to be found
val find_cycles : treetype ->
GrbGraphs.NewName.idtype list list
Finds all interesting cycles in a tree
Returns list of cycles (cycle = NewName.idtype list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward)
Parameters:
tree : treetype
- tree
val find_next_cycle : GrbGraphs.IdtMap.key list list ->
treetype -> GrbGraphs.IdtMap.key list
Finds next cycle for transformation. At first it finds cycles with no backward inputs in other nodes than the one, where cycles own backward edge goes to. Second part could be changed for different results.
Returns cycle - list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
Parameters:
cycles : GrbGraphs.IdtMap.key list list
- list of cycles (cycle = NewName.idtype list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward)
tree : treetype
- tree
val rearrange_treeCut : treetype ->
(GrbGraphs.DG.t, 'a) GrbCommons.either ->
GrbGraphs.IdtMap.key ->
'b -> treetype * 'c list
Function to rearrange tree after GrbTrCutStrictCycle transformation. Works for backward edges only.
Returns pair of new tree and an empty list
Parameters:
tree : treetype
- tree before the transformation
gr : (GrbGraphs.DG.t, 'a) GrbCommons.either
- result of the transformation
edge_id : GrbGraphs.IdtMap.key
- edge that was chosen for transformation
cycle : 'b
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward, not used
val rearrange_treeFuse : treetype ->
(GrbGraphs.DG.t, 'a) GrbCommons.either ->
GrbGraphs.IdtMap.key ->
GrbGraphs.NewName.idtype list ->
treetype * GrbGraphs.NewName.idtype list
Function to rearrange tree and find new cycle after GrbTrFuseTwoAndOr (and MoveOverOr for Or case) transformation.
Returns pair of new tree and new cycle
Parameters:
tree : treetype
- tree before the transformation
gr : (GrbGraphs.DG.t, 'a) GrbCommons.either
- result of the transformation
edge_id : GrbGraphs.IdtMap.key
- edge that was chosen for transformation
cycle : GrbGraphs.NewName.idtype list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
val rearrange_treeMUX : treetype ->
(GrbGraphs.DG.t, 'a) GrbCommons.either ->
GrbGraphs.IdtMap.key ->
GrbGraphs.IdtMap.key list ->
treetype * GrbGraphs.IdtMap.key list
Function to rearrange tree and find new cycle after GrbTrMoveOverMux, GrbTrMoveOverDtoCMUX and MoveOverOr for And and LongOr cases transformations.
Returns pair of new tree and new cycle
Parameters:
tree : treetype
- tree before the transformation
gr : (GrbGraphs.DG.t, 'a) GrbCommons.either
- result of the transformation
edge_id : GrbGraphs.IdtMap.key
- edge that was chosen for transformation
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
val rearrange_treeId : treetype ->
(GrbGraphs.DG.t, string) GrbCommons.either ->
GrbGraphs.IdtMap.key ->
GrbGraphs.IdtMap.key list ->
GrbGraphs.IdtMap.key ->
treetype * GrbGraphs.IdtMap.key list
Function to rearrange tree and find new cycle after GrbTrPassOverId transformation. Also takes care of possible error cases where transformation is used on edge with target node MUX.
Returns pair of new tree and new cycle
Parameters:
tree : treetype
- tree before the transformation
gr : (GrbGraphs.DG.t, string) GrbCommons.either
- result of the transformation
edge_id : GrbGraphs.IdtMap.key
- edge that was chosen for transformation
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
or_id : GrbGraphs.IdtMap.key
- id of the Or node before Id node whose transformation created this Id node
val rearrange_treeOr : treetype ->
(GrbGraphs.DG.t, 'a) GrbCommons.either ->
GrbGraphs.IdtMap.key ->
GrbGraphs.IdtMap.key list ->
GrbGraphs.nodetype ->
treetype * GrbGraphs.IdtMap.key list
Function to rearrange tree and find new cycle after MoveOverOr transformation.
Returns pair of new tree and new cycle
Parameters:
tree : treetype
- tree before the transformation
gr : (GrbGraphs.DG.t, 'a) GrbCommons.either
- result of the transformation
edge_id : GrbGraphs.IdtMap.key
- edge that was chosen for transformation
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
node : GrbGraphs.nodetype
val choose_move : GrbGraphs.IdtMap.key list ->
treetype ->
treetype * GrbGraphs.IdtMap.key list
Function that chooses and performs next step in cycle
Returns pair of new tree and new cycle
Parameters:
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
tree : treetype
- tree
val node_to_string : treeNode -> string
Debugging method, returns string representation of node
Returns string representation of a node
Parameters:
tnode : treeNode
- treeNode
val node_to_string_id : treetype -> GrbGraphs.IdtMap.key -> string
Parameters:
tree : treetype
id : GrbGraphs.IdtMap.key
val edge_to_string : treeEdge -> string
Debugging method, returns string representation of edge
Returns string
Parameters:
tedge : treeEdge
- treeEdge
val remove_cycles2 : treetype ->
GrbGraphs.IdtMap.key list -> int -> (GrbGraphs.DG.t, 'a) GrbCommons.either
Main cycle of the transformation. Goes recursively over all cycles.
Returns Left graph, final graph
Parameters:
tree : treetype
- current tree
cycle : GrbGraphs.IdtMap.key list
- list where every node has a Normal edge to the one before it in the list and edge from first element to last is Backward
n : int
- counter for recursion depth
val remove_cycles : treetype ->
(GrbGraphs.DG.t, string) GrbCommons.either
First cycle removing method that checks id there are any cycles
Returns transformation result
Parameters:
tree : treetype
- tree with all edgeways set