src/Pure/item_net.ML
author wenzelm
Mon Feb 23 14:50:30 2015 +0100 (2015-02-23)
changeset 59564 fdc03c8daacc
parent 59058 a78612c67ec0
child 60946 46ec72073dc1
permissions -rw-r--r--
Goal.prove_multi is superseded by the fully general Goal.prove_common;
     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 length: 'a T -> int
    14   val retrieve: 'a T -> term -> 'a list
    15   val retrieve_matching: 'a T -> term -> 'a list
    16   val member: 'a T -> 'a -> bool
    17   val merge: 'a T * 'a T -> 'a T
    18   val remove: 'a -> 'a T -> 'a T
    19   val update: 'a -> 'a T -> 'a T
    20 end;
    21 
    22 structure Item_Net: ITEM_NET =
    23 struct
    24 
    25 (* datatype *)
    26 
    27 datatype 'a T =
    28   Items of {
    29     eq: 'a * 'a -> bool,
    30     index: 'a -> term list,
    31     content: 'a list,
    32     next: int,
    33     net: (int * 'a) Net.net};
    34 
    35 fun mk_items eq index content next net =
    36   Items {eq = eq, index = index, content = content, next = next, net = net};
    37 
    38 fun init eq index = mk_items eq index [] ~1 Net.empty;
    39 
    40 fun content (Items {content, ...}) = content;
    41 fun length items = List.length (content items);
    42 fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
    43 fun retrieve_matching (Items {net, ...}) = order_list o Net.match_term net;
    44 
    45 
    46 (* standard operations *)
    47 
    48 fun member (Items {eq, index, content, net, ...}) x =
    49   (case index x of
    50     [] => Library.member eq content x
    51   | t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t));
    52 
    53 fun cons x (Items {eq, index, content, next, net}) =
    54   mk_items eq index (x :: content) (next - 1)
    55     (fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net);
    56 
    57 fun merge
    58     (items1 as Items {net = net1, ...},
    59      items2 as Items {net = net2, content = content2, ...}) =
    60   if pointer_eq (net1, net2) then items1
    61   else if Net.is_empty net1 then items2
    62   else fold_rev (fn y => if member items1 y then I else cons y) content2 items1;
    63 
    64 fun remove x (items as Items {eq, index, content, next, net}) =
    65   if member items x then
    66     mk_items eq index (Library.remove eq x content) next
    67       (fold (fn t => Net.delete_term_safe (eq o apply2 #2) (t, (0, x))) (index x) net)
    68   else items;
    69 
    70 fun update x items = cons x (remove x items);
    71 
    72 end;