| author | wenzelm | 
| Tue, 11 Apr 2023 10:45:04 +0200 | |
| changeset 77817 | a1bf8f706bc1 | 
| parent 77731 | 48fbecc8fab1 | 
| child 79446 | ec7a1603129a | 
| permissions | -rw-r--r-- | 
| 6134 | 1 | (* Title: Pure/General/graph.ML | 
| 15759 | 2 | Author: Markus Wenzel and Stefan Berghofer, TU Muenchen | 
| 6134 | 3 | |
| 4 | Directed graphs. | |
| 5 | *) | |
| 6 | ||
| 7 | signature GRAPH = | |
| 8 | sig | |
| 77731 | 9 | structure Key: KEY | 
| 6134 | 10 | type key | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 11 | structure Keys: | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 12 | sig | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 13 | type T | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 14 | val is_empty: T -> bool | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 15 | val fold: (key -> 'a -> 'a) -> T -> 'a -> 'a | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 16 | val fold_rev: (key -> 'a -> 'a) -> T -> 'a -> 'a | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 17 | val dest: T -> key list | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 18 | end | 
| 6134 | 19 | type 'a T | 
| 9321 | 20 | exception DUP of key | 
| 19029 | 21 | exception SAME | 
| 22 | exception UNDEF of key | |
| 6134 | 23 | val empty: 'a T | 
| 28204 | 24 | val is_empty: 'a T -> bool | 
| 6659 | 25 | val keys: 'a T -> key list | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 26 |   val get_first: (key * ('a * (Keys.T * Keys.T)) -> 'b option) -> 'a T -> 'b option
 | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 27 |   val fold: (key * ('a * (Keys.T * Keys.T)) -> 'b -> 'b) -> 'a T -> 'b -> 'b
 | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 28 |   val get_entry: 'a T -> key -> key * ('a * (Keys.T * Keys.T))        (*exception UNDEF*)
 | 
| 15759 | 29 | val get_node: 'a T -> key -> 'a (*exception UNDEF*) | 
| 6142 | 30 |   val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
 | 
| 46611 | 31 | val map: (key -> 'a -> 'b) -> 'a T -> 'b T | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 32 | val imm_preds: 'a T -> key -> Keys.T | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 33 | val imm_succs: 'a T -> key -> Keys.T | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 34 | val immediate_preds: 'a T -> key -> key list | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 35 | val immediate_succs: 'a T -> key -> key list | 
| 6134 | 36 | val all_preds: 'a T -> key list -> key list | 
| 37 | val all_succs: 'a T -> key list -> key list | |
| 46613 | 38 | val strong_conn: 'a T -> key list list | 
| 55658 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 39 | val map_strong_conn: ((key * 'a) list -> 'b list) -> 'a T -> 'b T | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 40 | val minimals: 'a T -> key list | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 41 | val maximals: 'a T -> key list | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 42 | val is_minimal: 'a T -> key -> bool | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 43 | val is_maximal: 'a T -> key -> bool | 
| 66830 | 44 | val is_isolated: 'a T -> key -> bool | 
| 15759 | 45 | val new_node: key * 'a -> 'a T -> 'a T (*exception DUP*) | 
| 17179 | 46 | val default_node: key * 'a -> 'a T -> 'a T | 
| 28333 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 47 | val del_node: key -> 'a T -> 'a T (*exception UNDEF*) | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 48 | val is_edge: 'a T -> key * key -> bool | 
| 44202 | 49 | val add_edge: key * key -> 'a T -> 'a T (*exception UNDEF*) | 
| 50 | val del_edge: key * key -> 'a T -> 'a T (*exception UNDEF*) | |
| 46614 
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
 wenzelm parents: 
46613diff
changeset | 51 | val restrict: (key -> bool) -> 'a T -> 'a T | 
| 49560 | 52 | val dest: 'a T -> ((key * 'a) * key list) list | 
| 53 | val make: ((key * 'a) * key list) list -> 'a T (*exception DUP | UNDEF*) | |
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21935diff
changeset | 54 |   val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUP*)
 | 
| 19029 | 55 | val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) -> | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21935diff
changeset | 56 | 'a T * 'a T -> 'a T (*exception DUP*) | 
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 57 | val irreducible_paths: 'a T -> key * key -> key list list | 
| 6142 | 58 | exception CYCLES of key list list | 
| 44202 | 59 | val add_edge_acyclic: key * key -> 'a T -> 'a T (*exception UNDEF | CYCLES*) | 
| 60 | val add_deps_acyclic: key * key list -> 'a T -> 'a T (*exception UNDEF | CYCLES*) | |
| 15759 | 61 |   val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T         (*exception CYCLES*)
 | 
| 23964 | 62 | val topological_order: 'a T -> key list | 
| 44202 | 63 | val add_edge_trans_acyclic: key * key -> 'a T -> 'a T (*exception UNDEF | CYCLES*) | 
| 15759 | 64 |   val merge_trans_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T   (*exception CYCLES*)
 | 
| 44162 | 65 | exception DEP of key * key | 
| 66 | val schedule: ((key * 'b) list -> key * 'a -> 'b) -> 'a T -> 'b list (*exception DEP*) | |
| 49560 | 67 | val encode: key XML.Encode.T -> 'a XML.Encode.T -> 'a T XML.Encode.T | 
| 68 | val decode: key XML.Decode.T -> 'a XML.Decode.T -> 'a T XML.Decode.T | |
| 6134 | 69 | end; | 
| 70 | ||
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31616diff
changeset | 71 | functor Graph(Key: KEY): GRAPH = | 
| 6134 | 72 | struct | 
| 73 | ||
| 74 | (* keys *) | |
| 75 | ||
| 77731 | 76 | structure Key = Key; | 
| 6134 | 77 | type key = Key.key; | 
| 77731 | 78 | |
| 18970 | 79 | val eq_key = is_equal o Key.ord; | 
| 6134 | 80 | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 81 | structure Table = Table(Key); | 
| 77723 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 82 | structure Set = Set(Key); | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 83 | |
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 84 | structure Keys = | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 85 | struct | 
| 6152 | 86 | |
| 77723 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 87 | abstype T = Keys of Set.T | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 88 | with | 
| 6134 | 89 | |
| 77723 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 90 | val empty = Keys Set.empty; | 
| 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 91 | fun is_empty (Keys set) = Set.is_empty set; | 
| 6134 | 92 | |
| 77723 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 93 | fun member (Keys set) = Set.member set; | 
| 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 94 | fun insert x (Keys set) = Keys (Set.insert x set); | 
| 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 95 | fun remove x (Keys set) = Keys (Set.remove x set); | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 96 | |
| 77723 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 97 | fun fold f (Keys set) = Set.fold f set; | 
| 
b761c91c2447
performance tuning: prefer functor Set() over Table();
 wenzelm parents: 
70773diff
changeset | 98 | fun fold_rev f (Keys set) = Set.fold_rev f set; | 
| 6134 | 99 | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 100 | fun dest keys = fold_rev cons keys []; | 
| 6142 | 101 | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 102 | fun filter P keys = fold (fn x => P x ? insert x) keys empty; | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 103 | |
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 104 | end; | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 105 | end; | 
| 6134 | 106 | |
| 107 | ||
| 6142 | 108 | (* graphs *) | 
| 6134 | 109 | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 110 | datatype 'a T = Graph of ('a * (Keys.T * Keys.T)) Table.table;
 | 
| 6134 | 111 | |
| 9321 | 112 | exception DUP = Table.DUP; | 
| 19029 | 113 | exception UNDEF = Table.UNDEF; | 
| 114 | exception SAME = Table.SAME; | |
| 6134 | 115 | |
| 116 | val empty = Graph Table.empty; | |
| 28204 | 117 | fun is_empty (Graph tab) = Table.is_empty tab; | 
| 6659 | 118 | fun keys (Graph tab) = Table.keys tab; | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 119 | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
32710diff
changeset | 120 | fun get_first f (Graph tab) = Table.get_first f tab; | 
| 19615 | 121 | fun fold_graph f (Graph tab) = Table.fold f tab; | 
| 122 | ||
| 6142 | 123 | fun get_entry (Graph tab) x = | 
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39021diff
changeset | 124 | (case Table.lookup_key tab x of | 
| 15531 | 125 | SOME entry => entry | 
| 126 | | NONE => raise UNDEF x); | |
| 6134 | 127 | |
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39021diff
changeset | 128 | fun map_entry x f (G as Graph tab) = Graph (Table.update (x, f (#2 (get_entry G x))) tab); | 
| 19290 | 129 | |
| 6134 | 130 | |
| 6142 | 131 | (* nodes *) | 
| 132 | ||
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39021diff
changeset | 133 | fun get_node G = #1 o #2 o get_entry G; | 
| 18133 | 134 | |
| 6142 | 135 | fun map_node x f = map_entry x (fn (i, ps) => (f i, ps)); | 
| 19290 | 136 | |
| 46611 | 137 | fun map_nodes f (Graph tab) = Graph (Table.map (apfst o f) tab); | 
| 138 | ||
| 18133 | 139 | |
| 6142 | 140 | (* reachability *) | 
| 141 | ||
| 6659 | 142 | (*nodes reachable from xs -- topologically sorted for acyclic graphs*) | 
| 6142 | 143 | fun reachable next xs = | 
| 6134 | 144 | let | 
| 18006 
535de280c812
reachable - abandoned foldl_map in favor of fold_map
 haftmann parents: 
17912diff
changeset | 145 | fun reach x (rs, R) = | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 146 | if Keys.member R x then (rs, R) | 
| 48350 
09bf3b73e446
clarified topological ordering: preserve order of adjacency via reverse fold;
 wenzelm parents: 
46668diff
changeset | 147 | else Keys.fold_rev reach (next x) (rs, Keys.insert x R) |>> cons x; | 
| 32710 | 148 | fun reachs x (rss, R) = | 
| 149 | reach x ([], R) |>> (fn rs => rs :: rss); | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 150 | in fold reachs xs ([], Keys.empty) end; | 
| 6134 | 151 | |
| 6142 | 152 | (*immediate*) | 
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39021diff
changeset | 153 | fun imm_preds G = #1 o #2 o #2 o get_entry G; | 
| 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39021diff
changeset | 154 | fun imm_succs G = #2 o #2 o #2 o get_entry G; | 
| 6134 | 155 | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 156 | fun immediate_preds G = Keys.dest o imm_preds G; | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 157 | fun immediate_succs G = Keys.dest o imm_succs G; | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 158 | |
| 6142 | 159 | (*transitive*) | 
| 32710 | 160 | fun all_preds G = flat o #1 o reachable (imm_preds G); | 
| 161 | fun all_succs G = flat o #1 o reachable (imm_succs G); | |
| 14161 
73ad4884441f
Added function strong_conn for computing the strongly connected components
 berghofe parents: 
12451diff
changeset | 162 | |
| 46613 | 163 | (*strongly connected components; see: David King and John Launchbury, | 
| 164 | "Structuring Depth First Search Algorithms in Haskell"*) | |
| 165 | fun strong_conn G = | |
| 166 | rev (filter_out null (#1 (reachable (imm_preds G) (all_succs G (keys G))))); | |
| 167 | ||
| 55658 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 168 | fun map_strong_conn f G = | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 169 | let | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 170 | val xss = strong_conn G; | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 171 | fun map' xs = | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 172 | fold2 (curry Table.update) xs (f (AList.make (get_node G) xs)); | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 173 | val tab' = Table.empty | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 174 | |> fold map' xss; | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 175 | in map_nodes (fn x => fn _ => the (Table.lookup tab' x)) G end; | 
| 
d696adf157e6
simultaneous mapping of strongly connected components
 haftmann parents: 
55154diff
changeset | 176 | |
| 46613 | 177 | |
| 178 | (* minimal and maximal elements *) | |
| 179 | ||
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 180 | fun minimals G = fold_graph (fn (m, (_, (preds, _))) => Keys.is_empty preds ? cons m) G []; | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 181 | fun maximals G = fold_graph (fn (m, (_, (_, succs))) => Keys.is_empty succs ? cons m) G []; | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 182 | fun is_minimal G x = Keys.is_empty (imm_preds G x); | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 183 | fun is_maximal G x = Keys.is_empty (imm_succs G x); | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 184 | |
| 66830 | 185 | fun is_isolated G x = is_minimal G x andalso is_maximal G x; | 
| 186 | ||
| 18133 | 187 | |
| 46668 | 188 | (* node operations *) | 
| 6134 | 189 | |
| 6152 | 190 | fun new_node (x, info) (Graph tab) = | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 191 | Graph (Table.update_new (x, (info, (Keys.empty, Keys.empty))) tab); | 
| 6134 | 192 | |
| 17179 | 193 | fun default_node (x, info) (Graph tab) = | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 194 | Graph (Table.default (x, (info, (Keys.empty, Keys.empty))) tab); | 
| 17140 | 195 | |
| 28333 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 196 | fun del_node x (G as Graph tab) = | 
| 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 197 | let | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 198 | fun del_adjacent which y = | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 199 | Table.map_entry y (fn (i, ps) => (i, (which (Keys.remove x) ps))); | 
| 43792 
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
 wenzelm parents: 
39021diff
changeset | 200 | val (preds, succs) = #2 (#2 (get_entry G x)); | 
| 28333 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 201 | in | 
| 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 202 | Graph (tab | 
| 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 203 | |> Table.delete x | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 204 | |> Keys.fold (del_adjacent apsnd) preds | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 205 | |> Keys.fold (del_adjacent apfst) succs) | 
| 28333 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 206 | end; | 
| 
507b64f4cd2a
added del_node, which is more efficient for sparse graphs;
 wenzelm parents: 
28204diff
changeset | 207 | |
| 46614 
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
 wenzelm parents: 
46613diff
changeset | 208 | fun restrict pred G = | 
| 
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
 wenzelm parents: 
46613diff
changeset | 209 | fold_graph (fn (x, _) => not (pred x) ? del_node x) G G; | 
| 
165886a4fe64
clarified Graph.restrict (formerly Graph.subgraph) based on public graph operations;
 wenzelm parents: 
46613diff
changeset | 210 | |
| 6152 | 211 | |
| 46668 | 212 | (* edge operations *) | 
| 9321 | 213 | |
| 77724 
b4032c468d74
performanc tuning: avoid exception overhead, potentially relevant for Sorts.class_less;
 wenzelm parents: 
77723diff
changeset | 214 | fun is_edge (Graph tab) (x, y) = | 
| 
b4032c468d74
performanc tuning: avoid exception overhead, potentially relevant for Sorts.class_less;
 wenzelm parents: 
77723diff
changeset | 215 | (case Table.lookup tab x of | 
| 
b4032c468d74
performanc tuning: avoid exception overhead, potentially relevant for Sorts.class_less;
 wenzelm parents: 
77723diff
changeset | 216 | SOME (_, (_, succs)) => Keys.member succs y | 
| 
b4032c468d74
performanc tuning: avoid exception overhead, potentially relevant for Sorts.class_less;
 wenzelm parents: 
77723diff
changeset | 217 | | NONE => false); | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 218 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 219 | fun add_edge (x, y) G = | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 220 | if is_edge G (x, y) then G | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 221 | else | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 222 | G |> map_entry y (fn (i, (preds, succs)) => (i, (Keys.insert x preds, succs))) | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 223 | |> map_entry x (fn (i, (preds, succs)) => (i, (preds, Keys.insert y succs))); | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 224 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 225 | fun del_edge (x, y) G = | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 226 | if is_edge G (x, y) then | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 227 | G |> map_entry y (fn (i, (preds, succs)) => (i, (Keys.remove x preds, succs))) | 
| 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 228 | |> map_entry x (fn (i, (preds, succs)) => (i, (preds, Keys.remove y succs))) | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 229 | else G; | 
| 9321 | 230 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 231 | fun diff_edges G1 G2 = | 
| 49560 | 232 | fold_graph (fn (x, (_, (_, succs))) => | 
| 233 | Keys.fold (fn y => not (is_edge G2 (x, y)) ? cons (x, y)) succs) G1 []; | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 234 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 235 | fun edges G = diff_edges G empty; | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 236 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 237 | |
| 49560 | 238 | (* dest and make *) | 
| 239 | ||
| 240 | fun dest G = fold_graph (fn (x, (i, (_, succs))) => cons ((x, i), Keys.dest succs)) G []; | |
| 241 | ||
| 242 | fun make entries = | |
| 243 | empty | |
| 244 | |> fold (new_node o fst) entries | |
| 245 | |> fold (fn ((x, _), ys) => fold (fn y => add_edge (x, y)) ys) entries; | |
| 246 | ||
| 247 | ||
| 18126 | 248 | (* join and merge *) | 
| 249 | ||
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 250 | fun no_edges (i, _) = (i, (Keys.empty, Keys.empty)); | 
| 18133 | 251 | |
| 35974 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 252 | fun join f (G1 as Graph tab1, G2 as Graph tab2) = | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 253 | let fun join_node key ((i1, edges1), (i2, _)) = (f key (i1, i2), edges1) in | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 254 | if pointer_eq (G1, G2) then G1 | 
| 39020 | 255 | else fold add_edge (edges G2) (Graph (Table.join join_node (tab1, Table.map (K no_edges) tab2))) | 
| 35974 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 256 | end; | 
| 6152 | 257 | |
| 35974 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 258 | fun gen_merge add eq (G1 as Graph tab1, G2 as Graph tab2) = | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 259 | let fun eq_node ((i1, _), (i2, _)) = eq (i1, i2) in | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 260 | if pointer_eq (G1, G2) then G1 | 
| 39020 | 261 | else fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map (K no_edges) tab2))) | 
| 35974 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 262 | end; | 
| 6152 | 263 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 264 | fun merge eq GG = gen_merge add_edge eq GG; | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 265 | |
| 18133 | 266 | |
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 267 | (* irreducible paths -- Hasse diagram *) | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 268 | |
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 269 | fun irreducible_preds G X path z = | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 270 | let | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 271 | fun red x x' = is_edge G (x, x') andalso not (eq_key (x', z)); | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 272 | fun irreds [] xs' = xs' | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 273 | | irreds (x :: xs) xs' = | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 274 | if not (Keys.member X x) orelse eq_key (x, z) orelse member eq_key path x orelse | 
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 275 | exists (red x) xs orelse exists (red x) xs' | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 276 | then irreds xs xs' | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 277 | else irreds xs (x :: xs'); | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 278 | in irreds (immediate_preds G z) [] end; | 
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 279 | |
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 280 | fun irreducible_paths G (x, y) = | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 281 | let | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 282 | val (_, X) = reachable (imm_succs G) [x]; | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 283 | fun paths path z = | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 284 | if eq_key (x, z) then cons (z :: path) | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 285 | else fold (paths (z :: path)) (irreducible_preds G X path z); | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 286 | in if eq_key (x, y) andalso not (is_edge G (x, x)) then [[]] else paths [] y [] end; | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 287 | |
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 288 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 289 | (* maintain acyclic graphs *) | 
| 6142 | 290 | |
| 291 | exception CYCLES of key list list; | |
| 6134 | 292 | |
| 293 | fun add_edge_acyclic (x, y) G = | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 294 | if is_edge G (x, y) then G | 
| 9347 | 295 | else | 
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 296 | (case irreducible_paths G (y, x) of | 
| 9347 | 297 | [] => add_edge (x, y) G | 
| 298 | | cycles => raise CYCLES (map (cons x) cycles)); | |
| 6134 | 299 | |
| 15759 | 300 | fun add_deps_acyclic (y, xs) = fold (fn x => add_edge_acyclic (x, y)) xs; | 
| 9321 | 301 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 302 | fun merge_acyclic eq GG = gen_merge add_edge_acyclic eq GG; | 
| 9321 | 303 | |
| 23964 | 304 | fun topological_order G = minimals G |> all_succs G; | 
| 305 | ||
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 306 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 307 | (* maintain transitive acyclic graphs *) | 
| 9321 | 308 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 309 | fun add_edge_trans_acyclic (x, y) G = | 
| 19290 | 310 | add_edge_acyclic (x, y) G | 
| 25538 | 311 | |> fold_product (curry add_edge) (all_preds G [x]) (all_succs G [y]); | 
| 9321 | 312 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 313 | fun merge_trans_acyclic eq (G1, G2) = | 
| 35974 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 314 | if pointer_eq (G1, G2) then G1 | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 315 | else | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 316 | merge_acyclic eq (G1, G2) | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 317 | |> fold add_edge_trans_acyclic (diff_edges G1 G2) | 
| 
3a588b344749
low-level tuning for join/merge: ignore identical versions (SUBTLE CHANGE IN SEMANTICS);
 wenzelm parents: 
35405diff
changeset | 318 | |> fold add_edge_trans_acyclic (diff_edges G2 G1); | 
| 6134 | 319 | |
| 31540 | 320 | |
| 44162 | 321 | (* schedule acyclic graph *) | 
| 322 | ||
| 323 | exception DEP of key * key; | |
| 324 | ||
| 325 | fun schedule f G = | |
| 326 | let | |
| 327 | val xs = topological_order G; | |
| 328 | val results = (xs, Table.empty) |-> fold (fn x => fn tab => | |
| 329 | let | |
| 330 | val a = get_node G x; | |
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
44202diff
changeset | 331 | val deps = immediate_preds G x |> map (fn y => | 
| 44162 | 332 | (case Table.lookup tab y of | 
| 333 | SOME b => (y, b) | |
| 334 | | NONE => raise DEP (x, y))); | |
| 335 | in Table.update (x, f deps (x, a)) tab end); | |
| 336 | in map (the o Table.lookup results) xs end; | |
| 337 | ||
| 338 | ||
| 49560 | 339 | (* XML data representation *) | 
| 340 | ||
| 341 | fun encode key info G = | |
| 342 | dest G |> | |
| 343 | let open XML.Encode | |
| 344 | in list (pair (pair key info) (list key)) end; | |
| 345 | ||
| 346 | fun decode key info body = | |
| 347 | body |> | |
| 348 | let open XML.Decode | |
| 349 | in list (pair (pair key info) (list key)) end |> make; | |
| 350 | ||
| 351 | ||
| 19615 | 352 | (*final declarations of this structure!*) | 
| 39021 | 353 | val map = map_nodes; | 
| 19615 | 354 | val fold = fold_graph; | 
| 355 | ||
| 6134 | 356 | end; | 
| 357 | ||
| 77731 | 358 | structure Graph = Graph(Symtab.Key); | 
| 46667 | 359 | structure String_Graph = Graph(type key = string val ord = string_ord); | 
| 77731 | 360 | structure Int_Graph = Graph(Inttab.Key); |