author | huffman |
Thu, 04 Jun 2009 17:28:31 -0700 | |
changeset 31448 | 29090e3111bd |
parent 30559 | e5987a7ac5df |
child 33372 | f380fbd6e329 |
permissions | -rw-r--r-- |
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 | 2 |
Author: Markus Wenzel, TU Muenchen |
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 | 6 |
*) |
7 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
8 |
signature ITEM_NET = |
8298 | 9 |
sig |
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 | 12 |
val retrieve: 'a T -> term -> 'a list |
12385 | 13 |
val init: ('a * 'a -> bool) -> ('a -> term) -> 'a T |
8298 | 14 |
val merge: 'a T * 'a T -> 'a T |
15 |
val delete: 'a -> 'a T -> 'a T |
|
16 |
val insert: 'a -> 'a T -> 'a T |
|
17 |
end; |
|
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 | 20 |
struct |
21 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
22 |
(* datatype *) |
8298 | 23 |
|
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 | 26 |
eq: 'a * 'a -> bool, |
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 | 29 |
next: int, |
12385 | 30 |
net: (int * 'a) Net.net}; |
8298 | 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 | 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 | 37 |
|
38 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
39 |
(* build net *) |
8298 | 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 | 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 | 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 | 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 | 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 | 57 |
|
58 |
end; |