author | wenzelm |
Wed, 15 May 2013 20:34:42 +0200 | |
changeset 52009 | 3b18ef9df768 |
parent 42810 | 2425068fe13a |
child 53941 | 54874871aa06 |
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 |
42810
2425068fe13a
slightly more efficient claset operations, using Item_Net to maintain rules in canonical order;
wenzelm
parents:
36296
diff
changeset
|
13 |
val length: 'a T -> int |
11776 | 14 |
val retrieve: 'a T -> term -> 'a list |
36296
5cc547abd995
Item_Net/Named_Thms: export efficient member operation;
wenzelm
parents:
33721
diff
changeset
|
15 |
val member: 'a T -> 'a -> bool |
8298 | 16 |
val merge: 'a T * 'a T -> 'a T |
33372 | 17 |
val remove: 'a -> 'a T -> 'a T |
18 |
val update: 'a -> 'a T -> 'a T |
|
8298 | 19 |
end; |
20 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
21 |
structure Item_Net: ITEM_NET = |
8298 | 22 |
struct |
23 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
24 |
(* datatype *) |
8298 | 25 |
|
26 |
datatype 'a T = |
|
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
27 |
Items of { |
8298 | 28 |
eq: 'a * 'a -> bool, |
33372 | 29 |
index: 'a -> term list, |
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
30 |
content: 'a list, |
8298 | 31 |
next: int, |
12385 | 32 |
net: (int * 'a) Net.net}; |
8298 | 33 |
|
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
34 |
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
|
35 |
Items {eq = eq, index = index, content = content, next = next, net = net}; |
8298 | 36 |
|
33372 | 37 |
fun init eq index = mk_items eq index [] ~1 Net.empty; |
38 |
||
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
39 |
fun content (Items {content, ...}) = content; |
42810
2425068fe13a
slightly more efficient claset operations, using Item_Net to maintain rules in canonical order;
wenzelm
parents:
36296
diff
changeset
|
40 |
fun length items = List.length (content items); |
30559
e5987a7ac5df
turned structure NetRules into general Item_Net, which is loaded earlier;
wenzelm
parents:
29606
diff
changeset
|
41 |
fun retrieve (Items {net, ...}) = order_list o Net.unify_term net; |
8298 | 42 |
|
43 |
||
33372 | 44 |
(* standard operations *) |
8298 | 45 |
|
33721
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
46 |
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
|
47 |
(case index x of |
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
48 |
[] => Library.member eq content x |
f15c9ded9676
member/cons: slightly more correct treatment of multi-index, notably empty one;
wenzelm
parents:
33372
diff
changeset
|
49 |
| t :: _ => exists (fn (_, y) => eq (x, y)) (Net.unify_term net t)); |
8298 | 50 |
|
33372 | 51 |
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
|
52 |
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
|
53 |
(fold (fn t => Net.insert_term (K false) (t, (next, x))) (index x) net); |
33372 | 54 |
|
55 |
fun merge (items1, Items {content = content2, ...}) = |
|
56 |
fold_rev (fn y => if member items1 y then I else cons y) content2 items1; |
|
8298 | 57 |
|
33372 | 58 |
fun remove x (items as Items {eq, index, content, next, net}) = |
59 |
if member items x then |
|
60 |
mk_items eq index (Library.remove eq x content) next |
|
61 |
(fold (fn t => Net.delete_term_safe (eq o pairself #2) (t, (0, x))) (index x) net) |
|
62 |
else items; |
|
8298 | 63 |
|
33372 | 64 |
fun update x items = cons x (remove x items); |
8298 | 65 |
|
66 |
end; |