src/Pure/item_net.ML
author huffman
Thu, 04 Jun 2009 17:28:31 -0700
changeset 31448 29090e3111bd
parent 30559 e5987a7ac5df
child 33372 f380fbd6e329
permissions -rw-r--r--
define netlimit in terms of eventually
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
     1
(*  Title:      Pure/item_net.ML
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
     3
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
     4
Efficient storage of items indexed by terms; preserves order and
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
     5
prefers later entries.
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
     6
*)
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
     7
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
     8
signature ITEM_NET =
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
     9
sig
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    10
  type 'a T
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    11
  val content: 'a T -> 'a list
11776
d4f9de0bde28 support weight;
wenzelm
parents: 11693
diff changeset
    12
  val retrieve: 'a T -> term -> 'a list
12385
389d11fb62c8 removed unused functionality (weight etc.);
wenzelm
parents: 12290
diff changeset
    13
  val init: ('a * 'a -> bool) -> ('a -> term) -> 'a T
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    14
  val merge: 'a T * 'a T -> 'a T
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    15
  val delete: 'a -> 'a T -> 'a T
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    16
  val insert: 'a -> 'a T -> 'a T
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    17
end;
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    18
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    19
structure Item_Net: ITEM_NET =
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    20
struct
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    21
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    22
(* datatype *)
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    23
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    24
datatype 'a T =
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    25
  Items of {
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    26
    eq: 'a * 'a -> bool,
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    27
    index: 'a -> term,
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    28
    content: 'a list,
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    29
    next: int,
12385
389d11fb62c8 removed unused functionality (weight etc.);
wenzelm
parents: 12290
diff changeset
    30
    net: (int * 'a) Net.net};
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    31
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    32
fun mk_items eq index content next net =
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    33
  Items {eq = eq, index = index, content = content, next = next, net = net};
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    34
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    35
fun content (Items {content, ...}) = content;
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    36
fun retrieve (Items {net, ...}) = order_list o Net.unify_term net;
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    37
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    38
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    39
(* build net *)
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    40
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    41
fun init eq index = mk_items eq index [] ~1 Net.empty;
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    42
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    43
fun add x (Items {eq, index, content, next, net}) =
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    44
  mk_items eq index (x :: content) (next - 1)
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    45
    (Net.insert_term (K false) (index x, (next, x)) net);
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    46
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    47
fun merge (Items {eq, index, content = content1, ...}, Items {content = content2, ...}) =
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    48
  let val content = Library.merge eq (content1, content2)
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    49
  in fold_rev add content (init eq index) end;
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    50
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    51
fun delete x (items as Items {eq, index, content, next, net}) =
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    52
  if not (member eq content x) then items
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    53
  else mk_items eq index (remove eq x content) next
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    54
    (Net.delete_term (eq o pairself #2) (index x, (0, x)) net);
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    55
30559
e5987a7ac5df turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents: 29606
diff changeset
    56
fun insert x items = add x (delete x items);   (*ensure that added entry gets precedence*)
8298
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    57
9b089bc07f69 added Isar/net_rules.ML;
wenzelm
parents:
diff changeset
    58
end;