author | paulson |
Mon, 21 Dec 2009 16:50:28 +0000 | |
changeset 34156 | 3a7937841585 |
parent 33721 | f15c9ded9676 |
child 36296 | 5cc547abd995 |
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 |
|
33372 | 11 |
val init: ('a * 'a -> bool) -> ('a -> term list) -> 'a T |
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
12 |
val content: 'a T -> 'a list |
11776 | 13 |
val retrieve: 'a T -> term -> 'a list |
8298 | 14 |
val merge: 'a T * 'a T -> 'a T |
33372 | 15 |
val remove: 'a -> 'a T -> 'a T |
16 |
val update: 'a -> 'a T -> 'a T |
|
8298 | 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, |
33372 | 27 |
index: 'a -> term list, |
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 |
|
33372 | 35 |
fun init eq index = mk_items eq index [] ~1 Net.empty; |
36 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
37 |
fun content (Items {content, ...}) = content; |
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
38 |
fun retrieve (Items {net, ...}) = order_list o Net.unify_term net; |
8298 | 39 |
|
40 |
||
33372 | 41 |
(* standard operations *) |
8298 | 42 |
|
33721
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
43 |
fun member (Items {eq, index, content, net, ...}) x = |
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
44 |
(case index x of |
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
45 |
[] => Library.member eq content x |
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
46 |
| t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t)); |
8298 | 47 |
|
33372 | 48 |
fun cons x (Items {eq, index, content, next, net}) = |
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
49 |
mk_items eq index (x :: content) (next - 1) |
33721
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
50 |
(fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net); |
33372 | 51 |
|
8298 | 52 |
|
33372 | 53 |
fun merge (items1, Items {content = content2, ...}) = |
54 |
fold_rev (fn y => if member items1 y then I else cons y) content2 items1; |
|
8298 | 55 |
|
33372 | 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; |
|
8298 | 61 |
|
33372 | 62 |
fun update x items = cons x (remove x items); |
8298 | 63 |
|
64 |
end; |