6134
|
1 |
(* Title: Pure/General/graph.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Directed graphs.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature GRAPH =
|
|
9 |
sig
|
|
10 |
type key
|
|
11 |
type 'a T
|
|
12 |
exception UNDEFINED of key
|
|
13 |
val empty: 'a T
|
6142
|
14 |
val get_nodes: 'a T -> (key * 'a) list
|
|
15 |
val map_nodes: ('a -> 'b) -> 'a T -> 'b T
|
|
16 |
val get_node: 'a T -> key -> 'a
|
|
17 |
val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
|
|
18 |
val imm_preds: 'a T -> key -> key list
|
|
19 |
val imm_succs: 'a T -> key -> key list
|
6134
|
20 |
val all_preds: 'a T -> key list -> key list
|
|
21 |
val all_succs: 'a T -> key list -> key list
|
|
22 |
val find_paths: 'a T -> key * key -> key list list
|
6142
|
23 |
exception DUPLICATE of key
|
6152
|
24 |
val new_node: key * 'a -> 'a T -> 'a T
|
6134
|
25 |
val add_edge: key * key -> 'a T -> 'a T
|
6152
|
26 |
val del_edge: key * key -> 'a T -> 'a T
|
6142
|
27 |
exception CYCLES of key list list
|
6134
|
28 |
val add_edge_acyclic: key * key -> 'a T -> 'a T
|
|
29 |
end;
|
|
30 |
|
|
31 |
functor GraphFun(Key: KEY): GRAPH =
|
|
32 |
struct
|
|
33 |
|
|
34 |
|
|
35 |
(* keys *)
|
|
36 |
|
|
37 |
type key = Key.key;
|
|
38 |
|
|
39 |
val eq_key = equal EQUAL o Key.ord;
|
|
40 |
|
|
41 |
infix mem_key;
|
|
42 |
val op mem_key = gen_mem eq_key;
|
|
43 |
|
|
44 |
infix ins_key;
|
|
45 |
val op ins_key = gen_ins eq_key;
|
|
46 |
|
6152
|
47 |
infix del_key;
|
|
48 |
fun xs del_key x = if x mem_key xs then gen_rem eq_key (xs, x) else xs;
|
|
49 |
|
6134
|
50 |
|
|
51 |
(* tables and sets of keys *)
|
|
52 |
|
|
53 |
structure Table = TableFun(Key);
|
|
54 |
type keys = unit Table.table;
|
|
55 |
|
6142
|
56 |
val empty_keys = Table.empty: keys;
|
|
57 |
|
6134
|
58 |
infix mem_keys;
|
|
59 |
fun x mem_keys tab = is_some (Table.lookup (tab: keys, x));
|
|
60 |
|
|
61 |
infix ins_keys;
|
|
62 |
fun x ins_keys tab = if x mem_keys tab then tab else Table.update ((x, ()), tab);
|
|
63 |
|
|
64 |
fun dest_keys tab = rev (Table.foldl (fn (xs, (x, ())) => x :: xs) ([], tab: keys));
|
|
65 |
|
|
66 |
|
6142
|
67 |
(* graphs *)
|
6134
|
68 |
|
|
69 |
datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
|
|
70 |
|
|
71 |
exception UNDEFINED of key;
|
|
72 |
|
|
73 |
val empty = Graph Table.empty;
|
|
74 |
|
6142
|
75 |
fun get_entry (Graph tab) x =
|
6134
|
76 |
(case Table.lookup (tab, x) of
|
6142
|
77 |
Some entry => entry
|
6134
|
78 |
| None => raise UNDEFINED x);
|
|
79 |
|
6142
|
80 |
fun map_entry x f (G as Graph tab) = Graph (Table.update ((x, f (get_entry G x)), tab));
|
6134
|
81 |
|
|
82 |
|
6142
|
83 |
(* nodes *)
|
|
84 |
|
|
85 |
fun get_nodes (Graph tab) =
|
|
86 |
rev (Table.foldl (fn (nodes, (x, (i, _))) => (x, i) :: nodes) ([], tab));
|
|
87 |
|
|
88 |
fun map_nodes f (Graph tab) = Graph (Table.map (fn (i, ps) => (f i, ps)) tab);
|
6134
|
89 |
|
6142
|
90 |
fun get_node G = #1 o get_entry G;
|
|
91 |
fun map_node x f = map_entry x (fn (i, ps) => (f i, ps));
|
|
92 |
|
|
93 |
|
|
94 |
(* reachability *)
|
|
95 |
|
|
96 |
fun reachable next xs =
|
6134
|
97 |
let
|
|
98 |
fun reach (R, x) =
|
|
99 |
if x mem_keys R then R
|
|
100 |
else reachs (x ins_keys R, next x)
|
|
101 |
and reachs R_xs = foldl reach R_xs;
|
|
102 |
in reachs (empty_keys, xs) end;
|
|
103 |
|
6142
|
104 |
(*immediate*)
|
|
105 |
fun imm_preds G = #1 o #2 o get_entry G;
|
|
106 |
fun imm_succs G = #2 o #2 o get_entry G;
|
6134
|
107 |
|
6142
|
108 |
(*transitive*)
|
|
109 |
fun all_preds G = dest_keys o reachable (imm_preds G);
|
|
110 |
fun all_succs G = dest_keys o reachable (imm_succs G);
|
6134
|
111 |
|
|
112 |
|
6142
|
113 |
(* paths *)
|
6134
|
114 |
|
|
115 |
fun find_paths G (x, y) =
|
|
116 |
let
|
6142
|
117 |
val X = reachable (imm_succs G) [x];
|
6134
|
118 |
fun paths ps p =
|
|
119 |
if eq_key (p, x) then [p :: ps]
|
|
120 |
else flat (map (paths (p :: ps))
|
6142
|
121 |
(filter (fn pp => pp mem_keys X andalso not (pp mem_key ps)) (imm_preds G p)));
|
|
122 |
in get_entry G y; if y mem_keys X then (paths [] y) else [] end;
|
6134
|
123 |
|
|
124 |
|
|
125 |
(* build graphs *)
|
|
126 |
|
6142
|
127 |
exception DUPLICATE of key;
|
|
128 |
|
6152
|
129 |
fun new_node (x, info) (Graph tab) =
|
6134
|
130 |
Graph (Table.update_new ((x, (info, ([], []))), tab)
|
|
131 |
handle Table.DUP key => raise DUPLICATE key);
|
|
132 |
|
6152
|
133 |
|
|
134 |
fun add_edge (x, y) =
|
|
135 |
map_entry x (fn (i, (preds, succs)) => (i, (preds, y ins_key succs))) o
|
|
136 |
map_entry y (fn (i, (preds, succs)) => (i, (x ins_key preds, succs)));
|
|
137 |
|
|
138 |
fun del_edge (x, y) =
|
|
139 |
map_entry x (fn (i, (preds, succs)) => (i, (preds, succs del_key y))) o
|
|
140 |
map_entry y (fn (i, (preds, succs)) => (i, (preds del_key x, succs)));
|
|
141 |
|
6142
|
142 |
|
|
143 |
exception CYCLES of key list list;
|
6134
|
144 |
|
|
145 |
fun add_edge_acyclic (x, y) G =
|
|
146 |
(case find_paths G (y, x) of
|
|
147 |
[] => add_edge (x, y) G
|
|
148 |
| cycles => raise CYCLES (map (cons x) cycles));
|
|
149 |
|
|
150 |
|
|
151 |
end;
|
|
152 |
|
|
153 |
|
|
154 |
(*graphs indexed by strings*)
|
|
155 |
structure Graph = GraphFun(type key = string val ord = string_ord);
|