src/Pure/General/graph.ML
changeset 19029 8635700e2c9c
parent 18970 d055a29ddd23
child 19290 033c3ade1e84
equal deleted inserted replaced
19028:6c238953f66c 19029:8635700e2c9c
     7 
     7 
     8 signature GRAPH =
     8 signature GRAPH =
     9 sig
     9 sig
    10   type key
    10   type key
    11   type 'a T
    11   type 'a T
    12   exception UNDEF of key
       
    13   exception DUP of key
    12   exception DUP of key
    14   exception DUPS of key list
    13   exception DUPS of key list
       
    14   exception SAME
       
    15   exception UNDEF of key
    15   val empty: 'a T
    16   val empty: 'a T
    16   val keys: 'a T -> key list
    17   val keys: 'a T -> key list
    17   val dest: 'a T -> (key * key list) list
    18   val dest: 'a T -> (key * key list) list
    18   val minimals: 'a T -> key list
    19   val minimals: 'a T -> key list
    19   val maximals: 'a T -> key list
    20   val maximals: 'a T -> key list
    35   val del_nodes: key list -> 'a T -> 'a T                             (*exception UNDEF*)
    36   val del_nodes: key list -> 'a T -> 'a T                             (*exception UNDEF*)
    36   val is_edge: 'a T -> key * key -> bool
    37   val is_edge: 'a T -> key * key -> bool
    37   val add_edge: key * key -> 'a T -> 'a T
    38   val add_edge: key * key -> 'a T -> 'a T
    38   val del_edge: key * key -> 'a T -> 'a T
    39   val del_edge: key * key -> 'a T -> 'a T
    39   val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUPS*)
    40   val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUPS*)
    40   val join: (key -> 'a * 'a -> 'a option) -> 'a T * 'a T -> 'a T      (*exception DUPS*)
    41   val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) ->
       
    42     'a T * 'a T -> 'a T                                               (*exception DUPS*)
    41   exception CYCLES of key list list
    43   exception CYCLES of key list list
    42   val add_edge_acyclic: key * key -> 'a T -> 'a T                     (*exception CYCLES*)
    44   val add_edge_acyclic: key * key -> 'a T -> 'a T                     (*exception CYCLES*)
    43   val add_deps_acyclic: key * key list -> 'a T -> 'a T                (*exception CYCLES*)
    45   val add_deps_acyclic: key * key list -> 'a T -> 'a T                (*exception CYCLES*)
    44   val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T         (*exception CYCLES*)
    46   val merge_acyclic: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T         (*exception CYCLES*)
    45   val add_edge_trans_acyclic: key * key -> 'a T -> 'a T               (*exception CYCLES*)
    47   val add_edge_trans_acyclic: key * key -> 'a T -> 'a T               (*exception CYCLES*)
    72 
    74 
    73 (* graphs *)
    75 (* graphs *)
    74 
    76 
    75 datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
    77 datatype 'a T = Graph of ('a * (key list * key list)) Table.table;
    76 
    78 
    77 exception UNDEF of key;
       
    78 exception DUP = Table.DUP;
    79 exception DUP = Table.DUP;
    79 exception DUPS = Table.DUPS;
    80 exception DUPS = Table.DUPS;
       
    81 exception UNDEF = Table.UNDEF;
       
    82 exception SAME = Table.SAME;
    80 
    83 
    81 val empty = Graph Table.empty;
    84 val empty = Graph Table.empty;
    82 fun keys (Graph tab) = Table.keys tab;
    85 fun keys (Graph tab) = Table.keys tab;
    83 fun dest (Graph tab) = map (fn (x, (_, (_, succs))) => (x, succs)) (Table.dest tab);
    86 fun dest (Graph tab) = map (fn (x, (_, (_, succs))) => (x, succs)) (Table.dest tab);
    84 
    87 
   203 (* join and merge *)
   206 (* join and merge *)
   204 
   207 
   205 fun no_edges (i, _) = (i, ([], []));
   208 fun no_edges (i, _) = (i, ([], []));
   206 
   209 
   207 fun join f (Graph tab1, G2 as Graph tab2) =
   210 fun join f (Graph tab1, G2 as Graph tab2) =
   208   let
   211   let fun join_node key ((i1, edges1), (i2, _)) = (f key (i1, i2), edges1)
   209     fun join_node key ((i1, edges1), (i2, _)) =
       
   210       (Option.map (rpair edges1) o f key) (i1, i2);
       
   211   in fold add_edge (edges G2) (Graph (Table.join join_node (tab1, Table.map no_edges tab2))) end;
   212   in fold add_edge (edges G2) (Graph (Table.join join_node (tab1, Table.map no_edges tab2))) end;
   212 
   213 
   213 fun gen_merge add eq (Graph tab1, G2 as Graph tab2) =
   214 fun gen_merge add eq (Graph tab1, G2 as Graph tab2) =
   214   let fun eq_node ((i1, _), (i2, _)) = eq (i1, i2)
   215   let fun eq_node ((i1, _), (i2, _)) = eq (i1, i2)
   215   in fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map no_edges tab2))) end;
   216   in fold add (edges G2) (Graph (Table.merge eq_node (tab1, Table.map no_edges tab2))) end;