src/Pure/General/graph.ML
author berghofe
Mon, 10 Dec 2001 15:37:03 +0100
changeset 12451 0224f472be71
parent 9347 1791a62b33e7
child 14161 73ad4884441f
permissions -rw-r--r--
Fixed bug in function find_paths.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/graph.ML
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
8806
wenzelm
parents: 6659
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     5
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     6
Directed graphs.
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     7
*)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     8
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     9
signature GRAPH =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    10
sig
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    11
  type key
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    12
  type 'a T
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    13
  exception UNDEF of key
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    14
  exception DUP of key
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    15
  exception DUPS of key list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    16
  val empty: 'a T
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    17
  val keys: 'a T -> key list
6142
wenzelm
parents: 6134
diff changeset
    18
  val map_nodes: ('a -> 'b) -> 'a T -> 'b T
wenzelm
parents: 6134
diff changeset
    19
  val get_node: 'a T -> key -> 'a
wenzelm
parents: 6134
diff changeset
    20
  val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
wenzelm
parents: 6134
diff changeset
    21
  val imm_preds: 'a T -> key -> key list
wenzelm
parents: 6134
diff changeset
    22
  val imm_succs: 'a T -> key -> key list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    23
  val all_preds: 'a T -> key list -> key list
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    24
  val all_succs: 'a T -> key list -> key list
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    25
  val find_paths: 'a T -> key * key -> key list list
6152
wenzelm
parents: 6142
diff changeset
    26
  val new_node: key * 'a -> 'a T -> 'a T
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    27
  val del_nodes: key list -> 'a T -> 'a T
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    28
  val edges: 'a T -> (key * key) list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    29
  val add_edge: key * key -> 'a T -> 'a T
6152
wenzelm
parents: 6142
diff changeset
    30
  val del_edge: key * key -> 'a T -> 'a T
6142
wenzelm
parents: 6134
diff changeset
    31
  exception CYCLES of key list list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    32
  val add_edge_acyclic: key * key -> 'a T -> 'a T
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    33
  val add_deps_acyclic: key * key list -> 'a T -> 'a T
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    34
  val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    35
end;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    36
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    37
functor GraphFun(Key: KEY): GRAPH =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    38
struct
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    39
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    40
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    41
(* keys *)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    42
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    43
type key = Key.key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    44
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    45
val eq_key = equal EQUAL o Key.ord;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    46
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    47
infix mem_key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    48
val op mem_key = gen_mem eq_key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    49
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    50
infix ins_key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    51
val op ins_key = gen_ins eq_key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    52
6152
wenzelm
parents: 6142
diff changeset
    53
infix del_key;
wenzelm
parents: 6142
diff changeset
    54
fun xs del_key x = if x mem_key xs then gen_rem eq_key (xs, x) else xs;
wenzelm
parents: 6142
diff changeset
    55
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    56
infix del_keys;
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    57
val op del_keys = foldl (op del_key);
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    58
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    59
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    60
(* tables and sets of keys *)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    61
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    62
structure Table = TableFun(Key);
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    63
type keys = unit Table.table;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    64
6142
wenzelm
parents: 6134
diff changeset
    65
val empty_keys = Table.empty: keys;
wenzelm
parents: 6134
diff changeset
    66
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    67
infix mem_keys;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    68
fun x mem_keys tab = is_some (Table.lookup (tab: keys, x));
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    69
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    70
infix ins_keys;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    71
fun x ins_keys tab = if x mem_keys tab then tab else Table.update ((x, ()), tab);
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    72
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    73
6142
wenzelm
parents: 6134
diff changeset
    74
(* graphs *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    75
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    76
datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    77
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    78
exception UNDEF of key;
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    79
exception DUP = Table.DUP;
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    80
exception DUPS = Table.DUPS;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    81
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    82
val empty = Graph Table.empty;
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    83
fun keys (Graph tab) = Table.keys tab;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    84
6142
wenzelm
parents: 6134
diff changeset
    85
fun get_entry (Graph tab) x =
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    86
  (case Table.lookup (tab, x) of
6142
wenzelm
parents: 6134
diff changeset
    87
    Some entry => entry
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    88
  | None => raise UNDEF x);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    89
6142
wenzelm
parents: 6134
diff changeset
    90
fun map_entry x f (G as Graph tab) = Graph (Table.update ((x, f (get_entry G x)), tab));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    91
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    92
6142
wenzelm
parents: 6134
diff changeset
    93
(* nodes *)
wenzelm
parents: 6134
diff changeset
    94
wenzelm
parents: 6134
diff changeset
    95
fun map_nodes f (Graph tab) = Graph (Table.map (fn (i, ps) => (f i, ps)) tab);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    96
6142
wenzelm
parents: 6134
diff changeset
    97
fun get_node G = #1 o get_entry G;
wenzelm
parents: 6134
diff changeset
    98
fun map_node x f = map_entry x (fn (i, ps) => (f i, ps));
wenzelm
parents: 6134
diff changeset
    99
wenzelm
parents: 6134
diff changeset
   100
wenzelm
parents: 6134
diff changeset
   101
(* reachability *)
wenzelm
parents: 6134
diff changeset
   102
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   103
(*nodes reachable from xs -- topologically sorted for acyclic graphs*)
6142
wenzelm
parents: 6134
diff changeset
   104
fun reachable next xs =
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   105
  let
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   106
    fun reach ((rs, R), x) =
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   107
      if x mem_keys R then (rs, R)
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   108
      else apfst (cons x) (reachs ((rs, x ins_keys R), next x))
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   109
    and reachs R_xs = foldl reach R_xs;
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   110
  in reachs (([], empty_keys), xs) end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   111
6142
wenzelm
parents: 6134
diff changeset
   112
(*immediate*)
wenzelm
parents: 6134
diff changeset
   113
fun imm_preds G = #1 o #2 o get_entry G;
wenzelm
parents: 6134
diff changeset
   114
fun imm_succs G = #2 o #2 o get_entry G;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   115
6142
wenzelm
parents: 6134
diff changeset
   116
(*transitive*)
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   117
fun all_preds G = #1 o reachable (imm_preds G);
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   118
fun all_succs G = #1 o reachable (imm_succs G);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   119
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   120
6142
wenzelm
parents: 6134
diff changeset
   121
(* paths *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   122
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   123
fun find_paths G (x, y) =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   124
  let
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   125
    val (_, X) = reachable (imm_succs G) [x];
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   126
    fun paths ps p =
12451
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   127
      if not (null ps) andalso eq_key (p, x) then [p :: ps]
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   128
      else if p mem_keys X andalso not (p mem_key ps)
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   129
      then flat (map (paths (p :: ps)) (imm_preds G p))
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   130
      else [];
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   131
  in paths [] y end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   132
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   133
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   134
(* nodes *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   135
6142
wenzelm
parents: 6134
diff changeset
   136
exception DUPLICATE of key;
wenzelm
parents: 6134
diff changeset
   137
6152
wenzelm
parents: 6142
diff changeset
   138
fun new_node (x, info) (Graph tab) =
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   139
  Graph (Table.update_new ((x, (info, ([], []))), tab));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   140
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   141
fun del_nodes xs (Graph tab) =
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   142
  let
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   143
    fun del (x, (i, (preds, succs))) =
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   144
      if x mem_key xs then None
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   145
      else Some (x, (i, (preds del_keys xs, succs del_keys xs)));
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   146
  in Graph (Table.make (mapfilter del (Table.dest tab))) end;
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   147
6152
wenzelm
parents: 6142
diff changeset
   148
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   149
(* edges *)
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   150
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   151
fun edges (Graph tab) =
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   152
  flat (map (fn (x, (_, (_, succs))) => (map (pair x) succs)) (Table.dest tab));
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   153
6152
wenzelm
parents: 6142
diff changeset
   154
fun add_edge (x, y) =
wenzelm
parents: 6142
diff changeset
   155
  map_entry x (fn (i, (preds, succs)) => (i, (preds, y ins_key succs))) o
wenzelm
parents: 6142
diff changeset
   156
   map_entry y (fn (i, (preds, succs)) => (i, (x ins_key preds, succs)));
wenzelm
parents: 6142
diff changeset
   157
wenzelm
parents: 6142
diff changeset
   158
fun del_edge (x, y) =
wenzelm
parents: 6142
diff changeset
   159
  map_entry x (fn (i, (preds, succs)) => (i, (preds, succs del_key y))) o
wenzelm
parents: 6142
diff changeset
   160
   map_entry y (fn (i, (preds, succs)) => (i, (preds del_key x, succs)));
wenzelm
parents: 6142
diff changeset
   161
6142
wenzelm
parents: 6134
diff changeset
   162
wenzelm
parents: 6134
diff changeset
   163
exception CYCLES of key list list;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   164
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   165
fun add_edge_acyclic (x, y) G =
9347
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   166
  if y mem_key imm_succs G x then G
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   167
  else
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   168
    (case find_paths G (y, x) of
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   169
      [] => add_edge (x, y) G
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   170
    | cycles => raise CYCLES (map (cons x) cycles));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   171
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   172
fun add_deps_acyclic (y, xs) G =
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   173
  foldl (fn (H, x) => add_edge_acyclic (x, y) H) (G, xs);
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   174
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   175
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   176
(* merge_acyclic *)
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   177
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   178
fun merge_acyclic eq (Graph tab1, G2 as Graph tab2) =
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   179
  foldl (fn (G, xy) => add_edge_acyclic xy G)
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   180
    (Graph (Table.merge (fn ((i1, _), (i2, _)) => eq (i1, i2)) (tab1, tab2)), edges G2);
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   181
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   182
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   183
end;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   184
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   185
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   186
(*graphs indexed by strings*)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   187
structure Graph = GraphFun(type key = string val ord = string_ord);