src/Pure/item_net.ML
author wenzelm
Mon Nov 16 21:56:32 2009 +0100 (2009-11-16 ago)
changeset 33721 f15c9ded9676
parent 33372 f380fbd6e329
child 36296 5cc547abd995
permissions -rw-r--r--
member/cons: slightly more correct treatment of multi-index, notably empty one;
     1 (*  Title:      Pure/item_net.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Efficient storage of items indexed by terms; preserves order and
     5 prefers later entries.
     6 *)
     7 
     8 signature ITEM_NET =
     9 sig
    10   type 'a T
    11   val init: ('a * 'a -> bool) -> ('a -> term list) -> 'a T
    12   val content: 'a T -> 'a list
    13   val retrieve: 'a T -> term -> 'a list
    14   val merge: 'a T * 'a T -> 'a T
    15   val remove: 'a -> 'a T -> 'a T
    16   val update: 'a -> 'a T -> 'a T
    17 end;
    18 
    19 structure Item_Net: ITEM_NET =
    20 struct
    21 
    22 (* datatype *)
    23 
    24 datatype 'a T =
    25   Items of {
    26     eq: 'a * 'a -> bool,
    27     index: 'a -> term list,
    28     content: 'a list,
    29     next: int,
    30     net: (int * 'a) Net.net};
    31 
    32 fun mk_items eq index content next net =
    33   Items {eq = eq, index = index, content = content, next = next, net = net};
    34 
    35 fun init eq index = mk_items eq index [] ~1 Net.empty;
    36 
    37 fun content (Items {content, ...}) = content;
    38 fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
    39 
    40 
    41 (* standard operations *)
    42 
    43 fun member (Items {eq, index, content, net, ...}) x =
    44   (case index x of
    45     [] => Library.member eq content x
    46   | t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t));
    47 
    48 fun cons x (Items {eq, index, content, next, net}) =
    49   mk_items eq index (x :: content) (next - 1)
    50     (fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net);
    51 
    52 
    53 fun merge (items1, Items {content = content2, ...}) =
    54   fold_rev (fn y => if member items1 y then I else cons y) content2 items1;
    55 
    56 fun remove x (items as Items {eq, index, content, next, net}) =
    57   if member items x then
    58     mk_items eq index (Library.remove eq x content) next
    59       (fold (fn t => Net.delete_term_safe (eq o pairself #2) (t, (0, x))) (index x) net)
    60   else items;
    61 
    62 fun update x items = cons x (remove x items);
    63 
    64 end;