| author | wenzelm | 
| Sat, 07 Oct 2006 01:31:01 +0200 | |
| changeset 20873 | 4066ee15b278 | 
| parent 20736 | 934358468a1b | 
| child 21565 | bd28361f4c5b | 
| permissions | -rw-r--r-- | 
| 6134 | 1 | (* Title: Pure/General/graph.ML | 
| 2 | ID: $Id$ | |
| 15759 | 3 | Author: Markus Wenzel and Stefan Berghofer, TU Muenchen | 
| 6134 | 4 | |
| 5 | Directed graphs. | |
| 6 | *) | |
| 7 | ||
| 8 | signature GRAPH = | |
| 9 | sig | |
| 10 | type key | |
| 11 | type 'a T | |
| 9321 | 12 | exception DUP of key | 
| 13 | exception DUPS of key list | |
| 19029 | 14 | exception SAME | 
| 15 | exception UNDEF of key | |
| 6134 | 16 | val empty: 'a T | 
| 6659 | 17 | val keys: 'a T -> key list | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 18 | val dest: 'a T -> (key * key list) list | 
| 19615 | 19 |   val fold: (key * ('a * (key list * key list)) -> 'b -> 'b) -> 'a T -> 'b -> 'b
 | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 20 | val minimals: 'a T -> key list | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 21 | val maximals: 'a T -> key list | 
| 6142 | 22 |   val map_nodes: ('a -> 'b) -> 'a T -> 'b T
 | 
| 19615 | 23 | val fold_map_nodes: (key * 'a -> 'b -> 'c * 'b) -> 'a T -> 'b -> 'c T * 'b | 
| 15759 | 24 | val get_node: 'a T -> key -> 'a (*exception UNDEF*) | 
| 6142 | 25 |   val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
 | 
| 17767 | 26 |   val map_node_yield: key -> ('a -> 'b * 'a) -> 'a T -> 'b * 'a T
 | 
| 6142 | 27 | val imm_preds: 'a T -> key -> key list | 
| 28 | val imm_succs: 'a T -> key -> key list | |
| 6134 | 29 | val all_preds: 'a T -> key list -> key list | 
| 30 | val all_succs: 'a T -> key list -> key list | |
| 14161 
73ad4884441f
Added function strong_conn for computing the strongly connected components
 berghofe parents: 
12451diff
changeset | 31 | val strong_conn: 'a T -> key list list | 
| 19950 | 32 | val project: (key -> bool) -> 'a T -> 'a T | 
| 15759 | 33 | val new_node: key * 'a -> 'a T -> 'a T (*exception DUP*) | 
| 17179 | 34 | val default_node: key * 'a -> 'a T -> 'a T | 
| 15759 | 35 | val del_nodes: key list -> 'a T -> 'a T (*exception UNDEF*) | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 36 | val is_edge: 'a T -> key * key -> bool | 
| 6134 | 37 | val add_edge: key * key -> 'a T -> 'a T | 
| 6152 | 38 | val del_edge: key * key -> 'a T -> 'a T | 
| 15759 | 39 |   val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUPS*)
 | 
| 19029 | 40 | val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) -> | 
| 41 | 'a T * 'a T -> 'a T (*exception DUPS*) | |
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 42 | val irreducible_paths: 'a T -> key * key -> key list list | 
| 20679 | 43 | val all_paths: 'a T -> key * key -> key list list | 
| 6142 | 44 | exception CYCLES of key list list | 
| 15759 | 45 | val add_edge_acyclic: key * key -> 'a T -> 'a T (*exception CYCLES*) | 
| 46 | val add_deps_acyclic: key * key list -> 'a T -> 'a T (*exception CYCLES*) | |
| 47 |   val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T         (*exception CYCLES*)
 | |
| 48 | val add_edge_trans_acyclic: key * key -> 'a T -> 'a T (*exception CYCLES*) | |
| 49 |   val merge_trans_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T   (*exception CYCLES*)
 | |
| 6134 | 50 | end; | 
| 51 | ||
| 52 | functor GraphFun(Key: KEY): GRAPH = | |
| 53 | struct | |
| 54 | ||
| 55 | (* keys *) | |
| 56 | ||
| 57 | type key = Key.key; | |
| 58 | ||
| 18970 | 59 | val eq_key = is_equal o Key.ord; | 
| 6134 | 60 | |
| 18921 | 61 | val member_key = member eq_key; | 
| 15759 | 62 | val remove_key = remove eq_key; | 
| 6152 | 63 | |
| 6134 | 64 | |
| 65 | (* tables and sets of keys *) | |
| 66 | ||
| 67 | structure Table = TableFun(Key); | |
| 68 | type keys = unit Table.table; | |
| 69 | ||
| 6142 | 70 | val empty_keys = Table.empty: keys; | 
| 71 | ||
| 18921 | 72 | fun member_keys tab = Table.defined (tab: keys); | 
| 73 | fun insert_keys x tab = Table.insert (K true) (x, ()) (tab: keys); | |
| 6134 | 74 | |
| 75 | ||
| 6142 | 76 | (* graphs *) | 
| 6134 | 77 | |
| 78 | datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
 | |
| 79 | ||
| 9321 | 80 | exception DUP = Table.DUP; | 
| 81 | exception DUPS = Table.DUPS; | |
| 19029 | 82 | exception UNDEF = Table.UNDEF; | 
| 83 | exception SAME = Table.SAME; | |
| 6134 | 84 | |
| 85 | val empty = Graph Table.empty; | |
| 6659 | 86 | fun keys (Graph tab) = Table.keys tab; | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 87 | fun dest (Graph tab) = map (fn (x, (_, (_, succs))) => (x, succs)) (Table.dest tab); | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 88 | |
| 19615 | 89 | fun fold_graph f (Graph tab) = Table.fold f tab; | 
| 90 | ||
| 91 | fun minimals G = fold_graph (fn (m, (_, ([], _))) => cons m | _ => I) G []; | |
| 92 | fun maximals G = fold_graph (fn (m, (_, (_, []))) => cons m | _ => I) G []; | |
| 6134 | 93 | |
| 6142 | 94 | fun get_entry (Graph tab) x = | 
| 17412 | 95 | (case Table.lookup tab x of | 
| 15531 | 96 | SOME entry => entry | 
| 97 | | NONE => raise UNDEF x); | |
| 6134 | 98 | |
| 17412 | 99 | fun map_entry x f (G as Graph tab) = Graph (Table.update (x, f (get_entry G x)) tab); | 
| 19290 | 100 | |
| 17767 | 101 | fun map_entry_yield x f (G as Graph tab) = | 
| 102 | let val (a, node') = f (get_entry G x) | |
| 103 | in (a, Graph (Table.update (x, node') tab)) end; | |
| 6134 | 104 | |
| 105 | ||
| 6142 | 106 | (* nodes *) | 
| 107 | ||
| 108 | fun map_nodes f (Graph tab) = Graph (Table.map (fn (i, ps) => (f i, ps)) tab); | |
| 6134 | 109 | |
| 19290 | 110 | fun fold_map_nodes f (Graph tab) = | 
| 111 | apfst Graph o Table.fold_map (fn (k, (i, ps)) => f (k, i) #> apfst (rpair ps)) tab; | |
| 17580 | 112 | |
| 6142 | 113 | fun get_node G = #1 o get_entry G; | 
| 18133 | 114 | |
| 6142 | 115 | fun map_node x f = map_entry x (fn (i, ps) => (f i, ps)); | 
| 19290 | 116 | |
| 17767 | 117 | fun map_node_yield x f = map_entry_yield x (fn (i, ps) => | 
| 118 | let val (a, i') = f i in (a, (i', ps)) end); | |
| 6142 | 119 | |
| 18133 | 120 | |
| 6142 | 121 | (* reachability *) | 
| 122 | ||
| 6659 | 123 | (*nodes reachable from xs -- topologically sorted for acyclic graphs*) | 
| 6142 | 124 | fun reachable next xs = | 
| 6134 | 125 | let | 
| 18006 
535de280c812
reachable - abandoned foldl_map in favor of fold_map
 haftmann parents: 
17912diff
changeset | 126 | fun reach x (rs, R) = | 
| 18921 | 127 | if member_keys R x then (rs, R) | 
| 128 | else apfst (cons x) (fold reach (next x) (rs, insert_keys x R)) | |
| 18006 
535de280c812
reachable - abandoned foldl_map in favor of fold_map
 haftmann parents: 
17912diff
changeset | 129 | in fold_map (fn x => reach x o pair []) xs empty_keys end; | 
| 6134 | 130 | |
| 6142 | 131 | (*immediate*) | 
| 132 | fun imm_preds G = #1 o #2 o get_entry G; | |
| 133 | fun imm_succs G = #2 o #2 o get_entry G; | |
| 6134 | 134 | |
| 6142 | 135 | (*transitive*) | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19409diff
changeset | 136 | fun all_preds G = flat o fst o reachable (imm_preds G); | 
| 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19409diff
changeset | 137 | fun all_succs G = flat o fst o reachable (imm_succs G); | 
| 14161 
73ad4884441f
Added function strong_conn for computing the strongly connected components
 berghofe parents: 
12451diff
changeset | 138 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 139 | (*strongly connected components; see: David King and John Launchbury, | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 140 | "Structuring Depth First Search Algorithms in Haskell"*) | 
| 18006 
535de280c812
reachable - abandoned foldl_map in favor of fold_map
 haftmann parents: 
17912diff
changeset | 141 | fun strong_conn G = filter_out null (fst (reachable (imm_preds G) | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19409diff
changeset | 142 | (flat (rev (fst (reachable (imm_succs G) (keys G))))))); | 
| 6134 | 143 | |
| 17912 | 144 | (*subgraph induced by node subset*) | 
| 19950 | 145 | fun project proj G = | 
| 17912 | 146 | let | 
| 19409 | 147 | fun subg (k, (i, (preds, succs))) = | 
| 19950 | 148 | K (proj k) ? Table.update (k, (i, (filter proj preds, filter proj succs))); | 
| 19615 | 149 | in Graph (fold_graph subg G Table.empty) end; | 
| 6134 | 150 | |
| 18133 | 151 | |
| 9321 | 152 | (* nodes *) | 
| 6134 | 153 | |
| 6152 | 154 | fun new_node (x, info) (Graph tab) = | 
| 17412 | 155 | Graph (Table.update_new (x, (info, ([], []))) tab); | 
| 6134 | 156 | |
| 17179 | 157 | fun default_node (x, info) (Graph tab) = | 
| 158 | Graph (Table.default (x, (info, ([], []))) tab); | |
| 17140 | 159 | |
| 6659 | 160 | fun del_nodes xs (Graph tab) = | 
| 15759 | 161 | Graph (tab | 
| 162 | |> fold Table.delete xs | |
| 163 | |> Table.map (fn (i, (preds, succs)) => | |
| 164 | (i, (fold remove_key xs preds, fold remove_key xs succs)))); | |
| 6659 | 165 | |
| 6152 | 166 | |
| 9321 | 167 | (* edges *) | 
| 168 | ||
| 18921 | 169 | fun is_edge G (x, y) = member_key (imm_succs G x) y handle UNDEF _ => false; | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 170 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 171 | fun add_edge (x, y) G = | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 172 | if is_edge G (x, y) then G | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 173 | else | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 174 | G |> map_entry y (fn (i, (preds, succs)) => (i, (x :: preds, succs))) | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 175 | |> map_entry x (fn (i, (preds, succs)) => (i, (preds, y :: succs))); | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 176 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 177 | fun del_edge (x, y) G = | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 178 | if is_edge G (x, y) then | 
| 15759 | 179 | G |> map_entry y (fn (i, (preds, succs)) => (i, (remove_key x preds, succs))) | 
| 180 | |> map_entry x (fn (i, (preds, succs)) => (i, (preds, remove_key y succs))) | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 181 | else G; | 
| 9321 | 182 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 183 | fun diff_edges G1 G2 = | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19409diff
changeset | 184 | flat (dest G1 |> map (fn (x, ys) => ys |> map_filter (fn y => | 
| 15531 | 185 | if is_edge G2 (x, y) then NONE else SOME (x, y)))); | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 186 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 187 | fun edges G = diff_edges G empty; | 
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 188 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 189 | |
| 18126 | 190 | (* join and merge *) | 
| 191 | ||
| 18133 | 192 | fun no_edges (i, _) = (i, ([], [])); | 
| 193 | ||
| 194 | fun join f (Graph tab1, G2 as Graph tab2) = | |
| 19029 | 195 | let fun join_node key ((i1, edges1), (i2, _)) = (f key (i1, i2), edges1) | 
| 18133 | 196 | in fold add_edge (edges G2) (Graph (Table.join join_node (tab1, Table.map no_edges tab2))) end; | 
| 6152 | 197 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 198 | fun gen_merge add eq (Graph tab1, G2 as Graph tab2) = | 
| 18133 | 199 | let fun eq_node ((i1, _), (i2, _)) = eq (i1, i2) | 
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 200 | in fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map no_edges tab2))) end; | 
| 6152 | 201 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 202 | 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 | 203 | |
| 18133 | 204 | |
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 205 | (* irreducible paths -- Hasse diagram *) | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 206 | |
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 207 | 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 | 208 | let | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 209 | 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 | 210 | fun irreds [] xs' = xs' | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 211 | | irreds (x :: xs) xs' = | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 212 | if not (member_keys X x) orelse eq_key (x, z) orelse member_key path x orelse | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 213 | 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 | 214 | then irreds xs xs' | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 215 | else irreds xs (x :: xs'); | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 216 | in irreds (imm_preds G z) [] end; | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 217 | |
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 218 | 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 | 219 | let | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 220 | 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 | 221 | fun paths path z = | 
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 222 | 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 | 223 | 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 | 224 | 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 | 225 | |
| 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 226 | |
| 20736 | 227 | (* all paths *) | 
| 20679 | 228 | |
| 229 | fun all_paths G (x, y) = | |
| 230 | let | |
| 231 | val (_, X) = reachable (imm_succs G) [x]; | |
| 20736 | 232 | fun paths path z = | 
| 233 | if not (null path) andalso eq_key (x, z) then [z :: path] | |
| 234 | else if member_keys X z andalso not (member_key path z) | |
| 235 | then maps (paths (z :: path)) (imm_preds G z) | |
| 20679 | 236 | else []; | 
| 237 | in paths [] y end; | |
| 238 | ||
| 239 | ||
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 240 | (* maintain acyclic graphs *) | 
| 6142 | 241 | |
| 242 | exception CYCLES of key list list; | |
| 6134 | 243 | |
| 244 | fun add_edge_acyclic (x, y) G = | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 245 | if is_edge G (x, y) then G | 
| 9347 | 246 | else | 
| 19580 
c878a09fb849
replaced find_paths by irreducible_paths, i.e. produce paths within a Hasse diagram;
 wenzelm parents: 
19482diff
changeset | 247 | (case irreducible_paths G (y, x) of | 
| 9347 | 248 | [] => add_edge (x, y) G | 
| 249 | | cycles => raise CYCLES (map (cons x) cycles)); | |
| 6134 | 250 | |
| 15759 | 251 | fun add_deps_acyclic (y, xs) = fold (fn x => add_edge_acyclic (x, y)) xs; | 
| 9321 | 252 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 253 | fun merge_acyclic eq GG = gen_merge add_edge_acyclic eq GG; | 
| 9321 | 254 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 255 | |
| 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 256 | (* maintain transitive acyclic graphs *) | 
| 9321 | 257 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 258 | fun add_edge_trans_acyclic (x, y) G = | 
| 19290 | 259 | add_edge_acyclic (x, y) G | 
| 260 | |> fold add_edge (Library.product (all_preds G [x]) (all_succs G [y])); | |
| 9321 | 261 | |
| 14793 
32d94d1e4842
added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
 wenzelm parents: 
14161diff
changeset | 262 | fun merge_trans_acyclic eq (G1, G2) = | 
| 19290 | 263 | merge_acyclic eq (G1, G2) | 
| 264 | |> fold add_edge_trans_acyclic (diff_edges G1 G2) | |
| 265 | |> fold add_edge_trans_acyclic (diff_edges G2 G1); | |
| 6134 | 266 | |
| 19615 | 267 | |
| 268 | (*final declarations of this structure!*) | |
| 269 | val fold = fold_graph; | |
| 270 | ||
| 6134 | 271 | end; | 
| 272 | ||
| 16810 | 273 | structure Graph = GraphFun(type key = string val ord = fast_string_ord); | 
| 19615 | 274 | structure IntGraph = GraphFun(type key = int val ord = int_ord); |