src/Pure/item_net.ML
author wenzelm
Thu Aug 15 16:02:47 2019 +0200 (9 months ago)
changeset 70533 031620901fcd
parent 68126 5da8b97d9183
permissions -rw-r--r--
support for (fully reconstructed) proof terms in Scala;
proper cache_typs;
wenzelm@30559
     1
(*  Title:      Pure/item_net.ML
wenzelm@8298
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8298
     3
wenzelm@30559
     4
Efficient storage of items indexed by terms; preserves order and
wenzelm@30559
     5
prefers later entries.
wenzelm@8298
     6
*)
wenzelm@8298
     7
wenzelm@30559
     8
signature ITEM_NET =
wenzelm@8298
     9
sig
wenzelm@8298
    10
  type 'a T
wenzelm@33372
    11
  val init: ('a * 'a -> bool) -> ('a -> term list) -> 'a T
wenzelm@68126
    12
  val is_empty: 'a T -> bool
wenzelm@30559
    13
  val content: 'a T -> 'a list
wenzelm@42810
    14
  val length: 'a T -> int
wenzelm@11776
    15
  val retrieve: 'a T -> term -> 'a list
lammich@53941
    16
  val retrieve_matching: 'a T -> term -> 'a list
wenzelm@36296
    17
  val member: 'a T -> 'a -> bool
wenzelm@60946
    18
  val lookup: 'a T -> 'a -> 'a list
wenzelm@8298
    19
  val merge: 'a T * 'a T -> 'a T
wenzelm@33372
    20
  val remove: 'a -> 'a T -> 'a T
wenzelm@33372
    21
  val update: 'a -> 'a T -> 'a T
wenzelm@62165
    22
  val filter: ('a -> bool) -> 'a T -> 'a T
wenzelm@8298
    23
end;
wenzelm@8298
    24
wenzelm@30559
    25
structure Item_Net: ITEM_NET =
wenzelm@8298
    26
struct
wenzelm@8298
    27
wenzelm@30559
    28
(* datatype *)
wenzelm@8298
    29
wenzelm@8298
    30
datatype 'a T =
wenzelm@30559
    31
  Items of {
wenzelm@8298
    32
    eq: 'a * 'a -> bool,
wenzelm@33372
    33
    index: 'a -> term list,
wenzelm@30559
    34
    content: 'a list,
wenzelm@8298
    35
    next: int,
wenzelm@12385
    36
    net: (int * 'a) Net.net};
wenzelm@8298
    37
wenzelm@30559
    38
fun mk_items eq index content next net =
wenzelm@30559
    39
  Items {eq = eq, index = index, content = content, next = next, net = net};
wenzelm@8298
    40
wenzelm@33372
    41
fun init eq index = mk_items eq index [] ~1 Net.empty;
wenzelm@68126
    42
fun is_empty (Items {content, ...}) = null content;
wenzelm@33372
    43
wenzelm@30559
    44
fun content (Items {content, ...}) = content;
wenzelm@42810
    45
fun length items = List.length (content items);
wenzelm@30559
    46
fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
lammich@53941
    47
fun retrieve_matching (Items {net, ...}) = order_list o Net.match_term net;
wenzelm@8298
    48
wenzelm@8298
    49
wenzelm@33372
    50
(* standard operations *)
wenzelm@8298
    51
wenzelm@33721
    52
fun member (Items {eq, index, content, net, ...}) x =
wenzelm@33721
    53
  (case index x of
wenzelm@33721
    54
    [] => Library.member eq content x
wenzelm@33721
    55
  | t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t));
wenzelm@8298
    56
wenzelm@60946
    57
fun lookup (Items {eq, index, content, net, ...}) x =
wenzelm@60946
    58
  (case index x of
wenzelm@60946
    59
    [] => content
wenzelm@60946
    60
  | t :: _ => map #2 (Net.unify_term net t))
wenzelm@60946
    61
  |> filter (fn y => eq (x, y));
wenzelm@60946
    62
wenzelm@33372
    63
fun cons x (Items {eq, index, content, next, net}) =
wenzelm@30559
    64
  mk_items eq index (x :: content) (next - 1)
wenzelm@33721
    65
    (fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net);
wenzelm@33372
    66
wenzelm@68126
    67
fun merge (items1, items2) =
wenzelm@68126
    68
  if pointer_eq (items1, items2) then items1
wenzelm@68126
    69
  else if is_empty items1 then items2
wenzelm@68126
    70
  else fold_rev (fn y => if member items1 y then I else cons y) (content items2) items1;
wenzelm@8298
    71
wenzelm@33372
    72
fun remove x (items as Items {eq, index, content, next, net}) =
wenzelm@33372
    73
  if member items x then
wenzelm@33372
    74
    mk_items eq index (Library.remove eq x content) next
wenzelm@59058
    75
      (fold (fn t => Net.delete_term_safe (eq o apply2 #2) (t, (0, x))) (index x) net)
wenzelm@33372
    76
  else items;
wenzelm@8298
    77
wenzelm@33372
    78
fun update x items = cons x (remove x items);
wenzelm@8298
    79
wenzelm@62165
    80
fun filter pred items =
wenzelm@62165
    81
  fold (fn x => not (pred x) ? remove x) (content items) items;
wenzelm@62165
    82
wenzelm@8298
    83
end;