src/Pure/General/graph.ML
author dixon
Thu, 05 May 2005 11:56:00 +0200
changeset 15927 db77bed00211
parent 15759 144c9f9a8ade
child 16003 48ae07a95c70
permissions -rw-r--r--
lucas - added update node function.
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$
15759
wenzelm
parents: 15570
diff changeset
     3
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     4
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     5
Directed graphs.
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     6
*)
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
signature GRAPH =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
     9
sig
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    10
  type key
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    11
  type 'a T
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    12
  exception UNDEF of key
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    13
  exception DUP of key
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    14
  exception DUPS of key list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    15
  val empty: 'a T
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    16
  val keys: 'a T -> key list
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    17
  val dest: 'a T -> (key * key list) list
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    18
  val minimals: 'a T -> key list
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    19
  val maximals: 'a T -> key list
6142
wenzelm
parents: 6134
diff changeset
    20
  val map_nodes: ('a -> 'b) -> 'a T -> 'b T
15759
wenzelm
parents: 15570
diff changeset
    21
  val get_node: 'a T -> key -> 'a                                     (*exception UNDEF*)
6142
wenzelm
parents: 6134
diff changeset
    22
  val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
wenzelm
parents: 6134
diff changeset
    23
  val imm_preds: 'a T -> key -> key list
wenzelm
parents: 6134
diff changeset
    24
  val imm_succs: 'a T -> key -> key list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    25
  val all_preds: 'a T -> key list -> key list
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    26
  val all_succs: 'a T -> key list -> key list
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
    27
  val strong_conn: 'a T -> key list list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    28
  val find_paths: 'a T -> key * key -> key list list
15759
wenzelm
parents: 15570
diff changeset
    29
  val new_node: key * 'a -> 'a T -> 'a T                              (*exception DUP*)
15927
db77bed00211 lucas - added update node function.
dixon
parents: 15759
diff changeset
    30
  val update_node : key * 'a -> 'a T -> 'a T
15759
wenzelm
parents: 15570
diff changeset
    31
  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: 14161
diff changeset
    32
  val is_edge: 'a T -> key * key -> bool
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    33
  val add_edge: key * key -> 'a T -> 'a T
6152
wenzelm
parents: 6142
diff changeset
    34
  val del_edge: key * key -> 'a T -> 'a T
15759
wenzelm
parents: 15570
diff changeset
    35
  val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUPS*)
6142
wenzelm
parents: 6134
diff changeset
    36
  exception CYCLES of key list list
15759
wenzelm
parents: 15570
diff changeset
    37
  val add_edge_acyclic: key * key -> 'a T -> 'a T                     (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    38
  val add_deps_acyclic: key * key list -> 'a T -> 'a T                (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    39
  val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T         (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    40
  val add_edge_trans_acyclic: key * key -> 'a T -> 'a T               (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    41
  val merge_trans_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T   (*exception CYCLES*)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    42
end;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    43
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    44
functor GraphFun(Key: KEY): GRAPH =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    45
struct
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
(* keys *)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    48
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    49
type key = Key.key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    50
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    51
val eq_key = equal EQUAL o Key.ord;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    52
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    53
infix mem_key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    54
val op mem_key = gen_mem eq_key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    55
15759
wenzelm
parents: 15570
diff changeset
    56
val remove_key = remove eq_key;
6152
wenzelm
parents: 6142
diff changeset
    57
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    58
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    59
(* tables and sets of keys *)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    60
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    61
structure Table = TableFun(Key);
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    62
type keys = unit Table.table;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    63
6142
wenzelm
parents: 6134
diff changeset
    64
val empty_keys = Table.empty: keys;
wenzelm
parents: 6134
diff changeset
    65
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    66
infix mem_keys;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    67
fun x mem_keys tab = isSome (Table.lookup (tab: keys, x));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    68
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    69
infix ins_keys;
15759
wenzelm
parents: 15570
diff changeset
    70
fun x ins_keys tab = Table.insert (K true) (x, ()) (tab: keys);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    71
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    72
6142
wenzelm
parents: 6134
diff changeset
    73
(* graphs *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    74
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    75
datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    76
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    77
exception UNDEF of key;
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    78
exception DUP = Table.DUP;
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    79
exception DUPS = Table.DUPS;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    80
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    81
val empty = Graph Table.empty;
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    82
fun keys (Graph tab) = Table.keys tab;
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    83
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: 14161
diff changeset
    84
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    85
fun minimals (Graph tab) =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    86
  Table.foldl (fn (ms, (m, (_, ([], _)))) => m :: ms | (ms, _) => ms) ([], tab);
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    87
fun maximals (Graph tab) =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    88
  Table.foldl (fn (ms, (m, (_, (_, [])))) => m :: ms | (ms, _) => ms) ([], tab);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    89
6142
wenzelm
parents: 6134
diff changeset
    90
fun get_entry (Graph tab) x =
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    91
  (case Table.lookup (tab, x) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    92
    SOME entry => entry
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    93
  | NONE => raise UNDEF x);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    94
6142
wenzelm
parents: 6134
diff changeset
    95
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
    96
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    97
6142
wenzelm
parents: 6134
diff changeset
    98
(* nodes *)
wenzelm
parents: 6134
diff changeset
    99
wenzelm
parents: 6134
diff changeset
   100
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
   101
6142
wenzelm
parents: 6134
diff changeset
   102
fun get_node G = #1 o get_entry G;
wenzelm
parents: 6134
diff changeset
   103
fun map_node x f = map_entry x (fn (i, ps) => (f i, ps));
wenzelm
parents: 6134
diff changeset
   104
wenzelm
parents: 6134
diff changeset
   105
wenzelm
parents: 6134
diff changeset
   106
(* reachability *)
wenzelm
parents: 6134
diff changeset
   107
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   108
(*nodes reachable from xs -- topologically sorted for acyclic graphs*)
6142
wenzelm
parents: 6134
diff changeset
   109
fun reachable next xs =
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   110
  let
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   111
    fun reach ((R, rs), x) =
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   112
      if x mem_keys R then (R, rs)
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   113
      else apsnd (cons x) (reachs ((x ins_keys R, rs), next x))
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   114
    and reachs R_xs = Library.foldl reach R_xs;
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   115
  in foldl_map (reach o apfst (rpair [])) (empty_keys, xs) end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   116
6142
wenzelm
parents: 6134
diff changeset
   117
(*immediate*)
wenzelm
parents: 6134
diff changeset
   118
fun imm_preds G = #1 o #2 o get_entry G;
wenzelm
parents: 6134
diff changeset
   119
fun imm_succs G = #2 o #2 o get_entry G;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   120
6142
wenzelm
parents: 6134
diff changeset
   121
(*transitive*)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   122
fun all_preds G = List.concat o snd o reachable (imm_preds G);
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   123
fun all_succs G = List.concat o snd o reachable (imm_succs G);
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   124
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   125
(*strongly connected components; see: David King and John Launchbury,
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   126
  "Structuring Depth First Search Algorithms in Haskell"*)
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   127
fun strong_conn G = filter_out null (snd (reachable (imm_preds G)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   128
  (List.concat (rev (snd (reachable (imm_succs G) (keys G)))))));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   129
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   130
6142
wenzelm
parents: 6134
diff changeset
   131
(* paths *)
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
fun find_paths G (x, y) =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   134
  let
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   135
    val (X, _) = reachable (imm_succs G) [x];
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   136
    fun paths ps p =
12451
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   137
      if not (null ps) andalso eq_key (p, x) then [p :: ps]
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   138
      else if p mem_keys X andalso not (p mem_key ps)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   139
      then List.concat (map (paths (p :: ps)) (imm_preds G p))
12451
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   140
      else [];
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   141
  in paths [] y end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   142
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   143
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   144
(* nodes *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   145
6152
wenzelm
parents: 6142
diff changeset
   146
fun new_node (x, info) (Graph tab) =
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   147
  Graph (Table.update_new ((x, (info, ([], []))), tab));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   148
15927
db77bed00211 lucas - added update node function.
dixon
parents: 15759
diff changeset
   149
fun update_node (x, info) (G as Graph tab) =
db77bed00211 lucas - added update node function.
dixon
parents: 15759
diff changeset
   150
    let val (_, (preds, succs)) = get_entry G x in
db77bed00211 lucas - added update node function.
dixon
parents: 15759
diff changeset
   151
      Graph (Table.update ((x, (info, (preds, succs))), tab))
db77bed00211 lucas - added update node function.
dixon
parents: 15759
diff changeset
   152
    end;
db77bed00211 lucas - added update node function.
dixon
parents: 15759
diff changeset
   153
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   154
fun del_nodes xs (Graph tab) =
15759
wenzelm
parents: 15570
diff changeset
   155
  Graph (tab
wenzelm
parents: 15570
diff changeset
   156
    |> fold Table.delete xs
wenzelm
parents: 15570
diff changeset
   157
    |> Table.map (fn (i, (preds, succs)) =>
wenzelm
parents: 15570
diff changeset
   158
      (i, (fold remove_key xs preds, fold remove_key xs succs))));
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   159
6152
wenzelm
parents: 6142
diff changeset
   160
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   161
(* edges *)
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   162
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   163
fun is_edge G (x, y) = y mem_key imm_succs G x handle UNDEF _ => false;
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   164
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   165
fun add_edge (x, y) G =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   166
  if is_edge G (x, y) then G
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   167
  else
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   168
    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: 14161
diff changeset
   169
      |> 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: 14161
diff changeset
   170
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   171
fun del_edge (x, y) G =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   172
  if is_edge G (x, y) then
15759
wenzelm
parents: 15570
diff changeset
   173
    G |> map_entry y (fn (i, (preds, succs)) => (i, (remove_key x preds, succs)))
wenzelm
parents: 15570
diff changeset
   174
      |> 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: 14161
diff changeset
   175
  else G;
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   176
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   177
fun diff_edges G1 G2 =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   178
  List.concat (dest G1 |> map (fn (x, ys) => ys |> List.mapPartial (fn y =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   179
    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: 14161
diff changeset
   180
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   181
fun edges G = diff_edges G empty;
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   182
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   183
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   184
(* merge *)
6152
wenzelm
parents: 6142
diff changeset
   185
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   186
fun gen_merge add eq (Graph tab1, G2 as Graph tab2) =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   187
  let
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   188
    fun eq_node ((i1, _), (i2, _)) = eq (i1, i2);
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   189
    fun no_edges (i, _) = (i, ([], []));
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   190
  in fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map no_edges tab2))) end;
6152
wenzelm
parents: 6142
diff changeset
   191
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   192
fun merge eq GG = gen_merge add_edge eq GG;
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   193
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   194
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   195
(* maintain acyclic graphs *)
6142
wenzelm
parents: 6134
diff changeset
   196
wenzelm
parents: 6134
diff changeset
   197
exception CYCLES of key list list;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   198
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   199
fun add_edge_acyclic (x, y) G =
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   200
  if is_edge G (x, y) then G
9347
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   201
  else
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   202
    (case find_paths G (y, x) of
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   203
      [] => add_edge (x, y) G
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   204
    | cycles => raise CYCLES (map (cons x) cycles));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   205
15759
wenzelm
parents: 15570
diff changeset
   206
fun add_deps_acyclic (y, xs) = fold (fn x => add_edge_acyclic (x, y)) xs;
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   207
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   208
fun merge_acyclic eq GG = gen_merge add_edge_acyclic eq GG;
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   209
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   210
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   211
(* maintain transitive acyclic graphs *)
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   212
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   213
fun add_edge_trans_acyclic (x, y) G =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   214
  add_edge_acyclic (x, y) G |>
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   215
  fold add_edge (Library.product (all_preds G [x]) (all_succs G [y]));
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   216
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   217
fun merge_trans_acyclic eq (G1, G2) =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   218
  merge_acyclic eq (G1, G2) |>
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   219
  fold add_edge_trans_acyclic (diff_edges G1 G2 @ diff_edges G2 G1);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   220
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   221
end;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   222
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   223
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   224
(*graphs indexed by strings*)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   225
structure Graph = GraphFun(type key = string val ord = string_ord);