src/Pure/General/graph.ML
author wenzelm
Sun, 12 Feb 2006 21:34:23 +0100
changeset 19029 8635700e2c9c
parent 18970 d055a29ddd23
child 19290 033c3ade1e84
permissions -rw-r--r--
share exception UNDEF with Table; simplified TableFun.join;
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 DUP of key
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
    13
  exception DUPS of key list
19029
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
    14
  exception SAME
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
    15
  exception UNDEF of key
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
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    18
  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
    19
  val minimals: 'a T -> key list
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
    20
  val maximals: 'a T -> key list
6142
wenzelm
parents: 6134
diff changeset
    21
  val map_nodes: ('a -> 'b) -> 'a T -> 'b T
17767
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
    22
  val fold_nodes: (key * 'b -> 'a -> 'a) -> 'b T -> 'a -> 'a
17580
78a286d696c1 added fold_map_graph
haftmann
parents: 17412
diff changeset
    23
  val fold_map_nodes: (key * 'b -> 'a -> 'c * 'a) -> 'b T -> 'a -> 'c T * 'a
15759
wenzelm
parents: 15570
diff changeset
    24
  val get_node: 'a T -> key -> 'a                                     (*exception UNDEF*)
6142
wenzelm
parents: 6134
diff changeset
    25
  val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
17767
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
    26
  val map_node_yield: key -> ('a -> 'b * 'a) -> 'a T -> 'b * 'a T
6142
wenzelm
parents: 6134
diff changeset
    27
  val imm_preds: 'a T -> key -> key list
wenzelm
parents: 6134
diff changeset
    28
  val imm_succs: 'a T -> key -> key list
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    29
  val all_preds: 'a T -> key list -> key list
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    30
  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
    31
  val strong_conn: 'a T -> key list list
17912
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
    32
  val subgraph: key list -> 'a T -> 'a T
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    33
  val find_paths: 'a T -> key * key -> key list list
15759
wenzelm
parents: 15570
diff changeset
    34
  val new_node: key * 'a -> 'a T -> 'a T                              (*exception DUP*)
17179
28802c8a9816 canonical interface for 'default'
haftmann
parents: 17140
diff changeset
    35
  val default_node: key * 'a -> 'a T -> 'a T
15759
wenzelm
parents: 15570
diff changeset
    36
  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
    37
  val is_edge: 'a T -> key * key -> bool
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    38
  val add_edge: key * key -> 'a T -> 'a T
6152
wenzelm
parents: 6142
diff changeset
    39
  val del_edge: key * key -> 'a T -> 'a T
15759
wenzelm
parents: 15570
diff changeset
    40
  val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUPS*)
19029
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
    41
  val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) ->
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
    42
    'a T * 'a T -> 'a T                                               (*exception DUPS*)
6142
wenzelm
parents: 6134
diff changeset
    43
  exception CYCLES of key list list
15759
wenzelm
parents: 15570
diff changeset
    44
  val add_edge_acyclic: key * key -> 'a T -> 'a T                     (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    45
  val add_deps_acyclic: key * key list -> 'a T -> 'a T                (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    46
  val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T         (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    47
  val add_edge_trans_acyclic: key * key -> 'a T -> 'a T               (*exception CYCLES*)
wenzelm
parents: 15570
diff changeset
    48
  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
    49
end;
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
functor GraphFun(Key: KEY): GRAPH =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    52
struct
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    53
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    54
(* keys *)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    55
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    56
type key = Key.key;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    57
18970
d055a29ddd23 Library.is_equal;
wenzelm
parents: 18921
diff changeset
    58
val eq_key = is_equal o Key.ord;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    59
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
    60
val member_key = member eq_key;
15759
wenzelm
parents: 15570
diff changeset
    61
val remove_key = remove eq_key;
6152
wenzelm
parents: 6142
diff changeset
    62
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    63
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    64
(* tables and sets of keys *)
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    65
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    66
structure Table = TableFun(Key);
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    67
type keys = unit Table.table;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    68
6142
wenzelm
parents: 6134
diff changeset
    69
val empty_keys = Table.empty: keys;
wenzelm
parents: 6134
diff changeset
    70
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
    71
fun member_keys tab = Table.defined (tab: keys);
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
    72
fun insert_keys x tab = Table.insert (K true) (x, ()) (tab: keys);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    73
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    74
6142
wenzelm
parents: 6134
diff changeset
    75
(* graphs *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    76
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    77
datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    78
9321
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;
19029
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
    81
exception UNDEF = Table.UNDEF;
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
    82
exception SAME = Table.SAME;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    83
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    84
val empty = Graph Table.empty;
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
    85
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
    86
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
    87
16445
bc90e58bb6ac Table.fold;
wenzelm
parents: 16003
diff changeset
    88
fun minimals (Graph tab) = Table.fold (fn (m, (_, ([], _))) => cons m | _ => I) tab [];
bc90e58bb6ac Table.fold;
wenzelm
parents: 16003
diff changeset
    89
fun maximals (Graph tab) = Table.fold (fn (m, (_, (_, []))) => cons m | _ => I) tab [];
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    90
6142
wenzelm
parents: 6134
diff changeset
    91
fun get_entry (Graph tab) x =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    92
  (case Table.lookup tab x of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    93
    SOME entry => entry
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    94
  | NONE => raise UNDEF x);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
    95
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    96
fun map_entry x f (G as Graph tab) = Graph (Table.update (x, f (get_entry G x)) tab);
17767
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
    97
fun map_entry_yield x f (G as Graph tab) =
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
    98
  let val (a, node') = f (get_entry G x)
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
    99
  in (a, Graph (Table.update (x, node') tab)) end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   100
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   101
6142
wenzelm
parents: 6134
diff changeset
   102
(* nodes *)
wenzelm
parents: 6134
diff changeset
   103
wenzelm
parents: 6134
diff changeset
   104
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
   105
17767
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
   106
fun fold_nodes f (Graph tab) s =
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
   107
  Table.fold (fn (k, (i, ps)) => f (k, i)) tab s
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
   108
17580
78a286d696c1 added fold_map_graph
haftmann
parents: 17412
diff changeset
   109
fun fold_map_nodes f (Graph tab) s =
78a286d696c1 added fold_map_graph
haftmann
parents: 17412
diff changeset
   110
  s
78a286d696c1 added fold_map_graph
haftmann
parents: 17412
diff changeset
   111
  |> Table.fold_map (fn (k, (i, ps)) => f (k, i) #> apfst (rpair ps)) tab
78a286d696c1 added fold_map_graph
haftmann
parents: 17412
diff changeset
   112
  |> apfst Graph;
78a286d696c1 added fold_map_graph
haftmann
parents: 17412
diff changeset
   113
6142
wenzelm
parents: 6134
diff changeset
   114
fun get_node G = #1 o get_entry G;
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   115
6142
wenzelm
parents: 6134
diff changeset
   116
fun map_node x f = map_entry x (fn (i, ps) => (f i, ps));
17767
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
   117
fun map_node_yield x f = map_entry_yield x (fn (i, ps) =>
504acb86c9f5 added fold_nodes, map_node_yield
haftmann
parents: 17580
diff changeset
   118
  let val (a, i') = f i in (a, (i', ps)) end);
6142
wenzelm
parents: 6134
diff changeset
   119
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   120
6142
wenzelm
parents: 6134
diff changeset
   121
(* reachability *)
wenzelm
parents: 6134
diff changeset
   122
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   123
(*nodes reachable from xs -- topologically sorted for acyclic graphs*)
6142
wenzelm
parents: 6134
diff changeset
   124
fun reachable next xs =
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   125
  let
18006
535de280c812 reachable - abandoned foldl_map in favor of fold_map
haftmann
parents: 17912
diff changeset
   126
    fun reach x (rs, R) =
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
   127
      if member_keys R x then (rs, R)
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
   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: 17912
diff changeset
   129
  in fold_map (fn x => reach x o pair []) xs empty_keys end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   130
6142
wenzelm
parents: 6134
diff changeset
   131
(*immediate*)
wenzelm
parents: 6134
diff changeset
   132
fun imm_preds G = #1 o #2 o get_entry G;
wenzelm
parents: 6134
diff changeset
   133
fun imm_succs G = #2 o #2 o get_entry G;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   134
6142
wenzelm
parents: 6134
diff changeset
   135
(*transitive*)
18006
535de280c812 reachable - abandoned foldl_map in favor of fold_map
haftmann
parents: 17912
diff changeset
   136
fun all_preds G = List.concat o fst o reachable (imm_preds G);
535de280c812 reachable - abandoned foldl_map in favor of fold_map
haftmann
parents: 17912
diff changeset
   137
fun all_succs G = List.concat o fst o reachable (imm_succs G);
14161
73ad4884441f Added function strong_conn for computing the strongly connected components
berghofe
parents: 12451
diff changeset
   138
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff 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: 14161
diff changeset
   140
  "Structuring Depth First Search Algorithms in Haskell"*)
18006
535de280c812 reachable - abandoned foldl_map in favor of fold_map
haftmann
parents: 17912
diff changeset
   141
fun strong_conn G = filter_out null (fst (reachable (imm_preds G)
535de280c812 reachable - abandoned foldl_map in favor of fold_map
haftmann
parents: 17912
diff changeset
   142
  (List.concat (rev (fst (reachable (imm_succs G) (keys G)))))));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   143
17912
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   144
(*subgraph induced by node subset*)
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   145
fun subgraph keys (Graph tab) =
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   146
  let
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   147
    val select = member eq_key keys;
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   148
    fun subg (k, (i, (preds, succs))) tab' =
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   149
      if select k
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   150
      then tab' |> Table.update (k, (i, (filter select preds, filter select succs)))
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   151
      else tab'
410ec3b7e771 added subgraph
haftmann
parents: 17767
diff changeset
   152
  in Table.empty |> Table.fold subg tab |> Graph end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   153
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   154
6142
wenzelm
parents: 6134
diff changeset
   155
(* paths *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   156
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   157
fun find_paths G (x, y) =
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   158
  let
18006
535de280c812 reachable - abandoned foldl_map in favor of fold_map
haftmann
parents: 17912
diff changeset
   159
    val (_, X) = reachable (imm_succs G) [x];
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   160
    fun paths ps p =
12451
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   161
      if not (null ps) andalso eq_key (p, x) then [p :: ps]
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
   162
      else if member_keys X p andalso not (member_key ps p)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   163
      then List.concat (map (paths (p :: ps)) (imm_preds G p))
12451
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   164
      else [];
0224f472be71 Fixed bug in function find_paths.
berghofe
parents: 9347
diff changeset
   165
  in paths [] y end;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   166
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   167
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   168
(* nodes *)
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   169
6152
wenzelm
parents: 6142
diff changeset
   170
fun new_node (x, info) (Graph tab) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   171
  Graph (Table.update_new (x, (info, ([], []))) tab);
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   172
17179
28802c8a9816 canonical interface for 'default'
haftmann
parents: 17140
diff changeset
   173
fun default_node (x, info) (Graph tab) =
28802c8a9816 canonical interface for 'default'
haftmann
parents: 17140
diff changeset
   174
  Graph (Table.default (x, (info, ([], []))) tab);
17140
5be3a21ec949 added 'default' function
haftmann
parents: 16894
diff changeset
   175
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   176
fun del_nodes xs (Graph tab) =
15759
wenzelm
parents: 15570
diff changeset
   177
  Graph (tab
wenzelm
parents: 15570
diff changeset
   178
    |> fold Table.delete xs
wenzelm
parents: 15570
diff changeset
   179
    |> Table.map (fn (i, (preds, succs)) =>
wenzelm
parents: 15570
diff changeset
   180
      (i, (fold remove_key xs preds, fold remove_key xs succs))));
6659
7a056250899d removed get_nodes;
wenzelm
parents: 6152
diff changeset
   181
6152
wenzelm
parents: 6142
diff changeset
   182
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   183
(* edges *)
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   184
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18133
diff changeset
   185
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: 14161
diff changeset
   186
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   187
fun add_edge (x, y) G =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   188
  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
   189
  else
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   190
    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
   191
      |> 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
   192
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   193
fun del_edge (x, y) G =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   194
  if is_edge G (x, y) then
15759
wenzelm
parents: 15570
diff changeset
   195
    G |> map_entry y (fn (i, (preds, succs)) => (i, (remove_key x preds, succs)))
wenzelm
parents: 15570
diff changeset
   196
      |> 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
   197
  else G;
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   198
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   199
fun diff_edges G1 G2 =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   200
  List.concat (dest G1 |> map (fn (x, ys) => ys |> List.mapPartial (fn y =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   201
    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
   202
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   203
fun edges G = diff_edges G empty;
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   204
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   205
18126
b74145e46e0d added join function
haftmann
parents: 18006
diff changeset
   206
(* join and merge *)
b74145e46e0d added join function
haftmann
parents: 18006
diff changeset
   207
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   208
fun no_edges (i, _) = (i, ([], []));
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   209
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   210
fun join f (Graph tab1, G2 as Graph tab2) =
19029
8635700e2c9c share exception UNDEF with Table;
wenzelm
parents: 18970
diff changeset
   211
  let fun join_node key ((i1, edges1), (i2, _)) = (f key (i1, i2), edges1)
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   212
  in fold add_edge (edges G2) (Graph (Table.join join_node (tab1, Table.map no_edges tab2))) end;
6152
wenzelm
parents: 6142
diff changeset
   213
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   214
fun gen_merge add eq (Graph tab1, G2 as Graph tab2) =
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   215
  let fun eq_node ((i1, _), (i2, _)) = eq (i1, i2)
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   216
  in fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map no_edges tab2))) end;
6152
wenzelm
parents: 6142
diff changeset
   217
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   218
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
   219
18133
1d403623dabc avoid code redundancy;
wenzelm
parents: 18126
diff changeset
   220
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   221
(* maintain acyclic graphs *)
6142
wenzelm
parents: 6134
diff changeset
   222
wenzelm
parents: 6134
diff changeset
   223
exception CYCLES of key list list;
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   224
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   225
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
   226
  if is_edge G (x, y) then G
9347
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   227
  else
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   228
    (case find_paths G (y, x) of
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   229
      [] => add_edge (x, y) G
1791a62b33e7 improved add_edges_cyclic;
wenzelm
parents: 9321
diff changeset
   230
    | cycles => raise CYCLES (map (cons x) cycles));
6134
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   231
15759
wenzelm
parents: 15570
diff changeset
   232
fun add_deps_acyclic (y, xs) = fold (fn x => add_edge_acyclic (x, y)) xs;
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   233
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   234
fun merge_acyclic eq GG = gen_merge add_edge_acyclic eq GG;
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   235
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   236
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   237
(* maintain transitive acyclic graphs *)
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   238
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   239
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
   240
  add_edge_acyclic (x, y) G |>
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   241
  fold add_edge (Library.product (all_preds G [x]) (all_succs G [y]));
9321
e0dda4bde88c tuned exceptions;
wenzelm
parents: 8806
diff changeset
   242
14793
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   243
fun merge_trans_acyclic eq (G1, G2) =
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   244
  merge_acyclic eq (G1, G2) |>
32d94d1e4842 added dest, minimals, maximals, is_edge, add_edge/merge_trans_acyclic;
wenzelm
parents: 14161
diff changeset
   245
  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
   246
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   247
end;
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   248
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   249
ec6092b0599d added General/graph.ML: generic direct graphs;
wenzelm
parents:
diff changeset
   250
(*graphs indexed by strings*)
16810
2406588f99cb Graph: fast_string_ord;
wenzelm
parents: 16445
diff changeset
   251
structure Graph = GraphFun(type key = string val ord = fast_string_ord);