author | wenzelm |
Thu, 14 Jul 2005 19:28:25 +0200 | |
changeset 16843 | 8ff9a80f3c93 |
parent 16842 | 5979c46853d1 |
child 16938 | 04bdd18e0ad1 |
permissions | -rw-r--r-- |
12319 | 1 |
(* Title: Pure/net.ML |
0 | 2 |
ID: $Id$ |
12319 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
0 | 4 |
Copyright 1993 University of Cambridge |
5 |
||
6 |
Discrimination nets: a data structure for indexing items |
|
7 |
||
12319 | 8 |
From the book |
9 |
E. Charniak, C. K. Riesbeck, D. V. McDermott. |
|
0 | 10 |
Artificial Intelligence Programming. |
11 |
(Lawrence Erlbaum Associates, 1980). [Chapter 14] |
|
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
12 |
|
12319 | 13 |
match_term no longer treats abstractions as wildcards; instead they match |
228 | 14 |
only wildcards in patterns. Requires operands to be beta-eta-normal. |
0 | 15 |
*) |
16 |
||
12319 | 17 |
signature NET = |
16808 | 18 |
sig |
0 | 19 |
type key |
16808 | 20 |
val key_of_term: term -> key list |
0 | 21 |
type 'a net |
22 |
val empty: 'a net |
|
16808 | 23 |
exception INSERT |
24 |
val insert: ('a * 'a -> bool) -> key list * 'a -> 'a net -> 'a net |
|
25 |
val insert_term: ('a * 'a -> bool) -> term * 'a -> 'a net -> 'a net |
|
26 |
exception DELETE |
|
27 |
val delete: ('b * 'a -> bool) -> key list * 'b -> 'a net -> 'a net |
|
28 |
val delete_term: ('b * 'a -> bool) -> term * 'b -> 'a net -> 'a net |
|
29 |
val lookup: 'a net -> key list -> 'a list |
|
0 | 30 |
val match_term: 'a net -> term -> 'a list |
31 |
val unify_term: 'a net -> term -> 'a list |
|
16808 | 32 |
val entries: 'a net -> 'a list |
33 |
val subtract: ('b * 'a -> bool) -> 'a net -> 'b net -> 'b list |
|
34 |
val merge: ('a * 'a -> bool) -> 'a net * 'a net -> 'a net |
|
35 |
end; |
|
0 | 36 |
|
16808 | 37 |
structure Net: NET = |
0 | 38 |
struct |
39 |
||
40 |
datatype key = CombK | VarK | AtomK of string; |
|
41 |
||
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
42 |
(*Bound variables*) |
7943
e31a3c0c2c1e
now more than 256 generated bound variables possible
oheimb
parents:
6539
diff
changeset
|
43 |
fun string_of_bound i = "*B*" ^ chr (i div 256) ^ chr (i mod 256); |
0 | 44 |
|
228 | 45 |
(*Keys are preorder lists of symbols -- Combinations, Vars, Atoms. |
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
46 |
Any term whose head is a Var is regarded entirely as a Var. |
228 | 47 |
Abstractions are also regarded as Vars; this covers eta-conversion |
48 |
and "near" eta-conversions such as %x.?P(?f(x)). |
|
0 | 49 |
*) |
12319 | 50 |
fun add_key_of_terms (t, cs) = |
0 | 51 |
let fun rands (f$t, cs) = CombK :: rands (f, add_key_of_terms(t, cs)) |
12319 | 52 |
| rands (Const(c,_), cs) = AtomK c :: cs |
53 |
| rands (Free(c,_), cs) = AtomK c :: cs |
|
54 |
| rands (Bound i, cs) = AtomK (string_of_bound i) :: cs |
|
0 | 55 |
in case (head_of t) of |
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
56 |
Var _ => VarK :: cs |
228 | 57 |
| Abs _ => VarK :: cs |
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
58 |
| _ => rands(t,cs) |
0 | 59 |
end; |
60 |
||
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
61 |
(*convert a term to a list of keys*) |
0 | 62 |
fun key_of_term t = add_key_of_terms (t, []); |
63 |
||
64 |
||
65 |
(*Trees indexed by key lists: each arc is labelled by a key. |
|
66 |
Each node contains a list of items, and arcs to children. |
|
67 |
The empty key addresses the entire net. |
|
68 |
Lookup functions preserve order in items stored at same level. |
|
69 |
*) |
|
70 |
datatype 'a net = Leaf of 'a list |
|
12319 | 71 |
| Net of {comb: 'a net, |
72 |
var: 'a net, |
|
16708 | 73 |
atoms: 'a net Symtab.table}; |
0 | 74 |
|
75 |
val empty = Leaf[]; |
|
16708 | 76 |
fun is_empty (Leaf []) = true | is_empty _ = false; |
77 |
val emptynet = Net{comb=empty, var=empty, atoms=Symtab.empty}; |
|
0 | 78 |
|
79 |
||
80 |
(*** Insertion into a discrimination net ***) |
|
81 |
||
12319 | 82 |
exception INSERT; (*duplicate item in the net*) |
0 | 83 |
|
84 |
||
85 |
(*Adds item x to the list at the node addressed by the keys. |
|
86 |
Creates node if not already present. |
|
12319 | 87 |
eq is the equality test for items. |
0 | 88 |
The empty list of keys generates a Leaf node, others a Net node. |
89 |
*) |
|
16808 | 90 |
fun insert eq (keys,x) net = |
12319 | 91 |
let fun ins1 ([], Leaf xs) = |
16686 | 92 |
if member eq xs x then raise INSERT else Leaf(x::xs) |
0 | 93 |
| ins1 (keys, Leaf[]) = ins1 (keys, emptynet) (*expand empty...*) |
16708 | 94 |
| ins1 (CombK :: keys, Net{comb,var,atoms}) = |
95 |
Net{comb=ins1(keys,comb), var=var, atoms=atoms} |
|
96 |
| ins1 (VarK :: keys, Net{comb,var,atoms}) = |
|
97 |
Net{comb=comb, var=ins1(keys,var), atoms=atoms} |
|
98 |
| ins1 (AtomK a :: keys, Net{comb,var,atoms}) = |
|
99 |
let |
|
100 |
val net' = if_none (Symtab.lookup (atoms, a)) empty; |
|
101 |
val atoms' = Symtab.update ((a, ins1(keys,net')), atoms); |
|
102 |
in Net{comb=comb, var=var, atoms=atoms'} end |
|
0 | 103 |
in ins1 (keys,net) end; |
104 |
||
16808 | 105 |
fun insert_safe eq entry net = insert eq entry net handle INSERT => net; |
106 |
fun insert_term eq (t, x) = insert eq (key_of_term t, x); |
|
107 |
||
0 | 108 |
|
109 |
(*** Deletion from a discrimination net ***) |
|
110 |
||
12319 | 111 |
exception DELETE; (*missing item in the net*) |
0 | 112 |
|
113 |
(*Create a new Net node if it would be nonempty*) |
|
16708 | 114 |
fun newnet (args as {comb,var,atoms}) = |
115 |
if is_empty comb andalso is_empty var andalso Symtab.is_empty atoms |
|
116 |
then empty else Net args; |
|
0 | 117 |
|
118 |
(*Deletes item x from the list at the node addressed by the keys. |
|
119 |
Raises DELETE if absent. Collapses the net if possible. |
|
120 |
eq is the equality test for items. *) |
|
16808 | 121 |
fun delete eq (keys, x) net = |
0 | 122 |
let fun del1 ([], Leaf xs) = |
16686 | 123 |
if member eq xs x then Leaf (remove eq x xs) |
0 | 124 |
else raise DELETE |
12319 | 125 |
| del1 (keys, Leaf[]) = raise DELETE |
16708 | 126 |
| del1 (CombK :: keys, Net{comb,var,atoms}) = |
127 |
newnet{comb=del1(keys,comb), var=var, atoms=atoms} |
|
128 |
| del1 (VarK :: keys, Net{comb,var,atoms}) = |
|
129 |
newnet{comb=comb, var=del1(keys,var), atoms=atoms} |
|
130 |
| del1 (AtomK a :: keys, Net{comb,var,atoms}) = |
|
131 |
let val atoms' = |
|
132 |
(case Symtab.lookup (atoms, a) of |
|
133 |
NONE => raise DELETE |
|
134 |
| SOME net' => |
|
135 |
(case del1 (keys, net') of |
|
136 |
Leaf [] => Symtab.delete a atoms |
|
137 |
| net'' => Symtab.update ((a, net''), atoms))) |
|
138 |
in newnet{comb=comb, var=var, atoms=atoms'} end |
|
0 | 139 |
in del1 (keys,net) end; |
140 |
||
16808 | 141 |
fun delete_term eq (t, x) = delete eq (key_of_term t, x); |
0 | 142 |
|
16677 | 143 |
|
0 | 144 |
(*** Retrieval functions for discrimination nets ***) |
145 |
||
16708 | 146 |
exception ABSENT; |
0 | 147 |
|
16708 | 148 |
fun the_atom atoms a = |
149 |
(case Symtab.lookup (atoms, a) of |
|
150 |
NONE => raise ABSENT |
|
151 |
| SOME net => net); |
|
0 | 152 |
|
153 |
(*Return the list of items at the given node, [] if no such node*) |
|
16808 | 154 |
fun lookup (Leaf xs) [] = xs |
155 |
| lookup (Leaf _) (_ :: _) = [] (*non-empty keys and empty net*) |
|
156 |
| lookup (Net {comb, var, atoms}) (CombK :: keys) = lookup comb keys |
|
157 |
| lookup (Net {comb, var, atoms}) (VarK :: keys) = lookup var keys |
|
158 |
| lookup (Net {comb, var, atoms}) (AtomK a :: keys) = |
|
159 |
lookup (the_atom atoms a) keys handle ABSENT => []; |
|
0 | 160 |
|
161 |
||
162 |
(*Skipping a term in a net. Recursively skip 2 levels if a combination*) |
|
163 |
fun net_skip (Leaf _, nets) = nets |
|
16708 | 164 |
| net_skip (Net{comb,var,atoms}, nets) = |
165 |
foldr net_skip (Symtab.fold (cons o #2) atoms (var::nets)) (net_skip (comb,[])); |
|
0 | 166 |
|
16808 | 167 |
|
168 |
(** Matching and Unification **) |
|
0 | 169 |
|
170 |
(*conses the linked net, if present, to nets*) |
|
16708 | 171 |
fun look1 (atoms, a) nets = |
172 |
the_atom atoms a :: nets handle ABSENT => nets; |
|
0 | 173 |
|
12319 | 174 |
(*Return the nodes accessible from the term (cons them before nets) |
0 | 175 |
"unif" signifies retrieval for unification rather than matching. |
176 |
Var in net matches any term. |
|
12319 | 177 |
Abs or Var in object: if "unif", regarded as wildcard, |
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
178 |
else matches only a variable in net. |
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
179 |
*) |
0 | 180 |
fun matching unif t (net,nets) = |
181 |
let fun rands _ (Leaf _, nets) = nets |
|
16708 | 182 |
| rands t (Net{comb,atoms,...}, nets) = |
12319 | 183 |
case t of |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
184 |
f$t => foldr (matching unif t) nets (rands f (comb,[])) |
16708 | 185 |
| Const(c,_) => look1 (atoms, c) nets |
186 |
| Free(c,_) => look1 (atoms, c) nets |
|
187 |
| Bound i => look1 (atoms, string_of_bound i) nets |
|
12319 | 188 |
| _ => nets |
189 |
in |
|
0 | 190 |
case net of |
12319 | 191 |
Leaf _ => nets |
0 | 192 |
| Net{var,...} => |
12319 | 193 |
case head_of t of |
194 |
Var _ => if unif then net_skip (net,nets) |
|
195 |
else var::nets (*only matches Var in net*) |
|
2836 | 196 |
(*If "unif" then a var instantiation in the abstraction could allow |
197 |
an eta-reduction, so regard the abstraction as a wildcard.*) |
|
12319 | 198 |
| Abs _ => if unif then net_skip (net,nets) |
199 |
else var::nets (*only a Var can match*) |
|
200 |
| _ => rands t (net, var::nets) (*var could match also*) |
|
0 | 201 |
end; |
202 |
||
2672
85d7e800d754
Replaced "flat" by the Basis Library function List.concat
paulson
parents:
2226
diff
changeset
|
203 |
fun extract_leaves l = List.concat (map (fn Leaf(xs) => xs) l); |
0 | 204 |
|
225
76f60e6400e8
optimized net for matching of abstractions to speed up simplifier
nipkow
parents:
0
diff
changeset
|
205 |
(*return items whose key could match t, WHICH MUST BE BETA-ETA NORMAL*) |
12319 | 206 |
fun match_term net t = |
0 | 207 |
extract_leaves (matching false t (net,[])); |
208 |
||
209 |
(*return items whose key could unify with t*) |
|
12319 | 210 |
fun unify_term net t = |
0 | 211 |
extract_leaves (matching true t (net,[])); |
212 |
||
3548 | 213 |
|
16808 | 214 |
(** operations on nets **) |
215 |
||
216 |
(*subtraction: collect entries of second net that are NOT present in first net*) |
|
217 |
fun subtract eq net1 net2 = |
|
218 |
let |
|
219 |
fun subtr (Net _) (Leaf ys) = append ys |
|
220 |
| subtr (Leaf xs) (Leaf ys) = |
|
221 |
fold_rev (fn y => if member eq xs y then I else cons y) ys |
|
222 |
| subtr (Leaf _) (net as Net _) = subtr emptynet net |
|
223 |
| subtr (Net {comb = comb1, var = var1, atoms = atoms1}) |
|
224 |
(Net {comb = comb2, var = var2, atoms = atoms2}) = |
|
16842 | 225 |
subtr comb1 comb2 |
226 |
#> subtr var1 var2 |
|
227 |
#> Symtab.fold (fn (a, net) => |
|
228 |
subtr (if_none (Symtab.lookup (atoms1, a)) emptynet) net) atoms2 |
|
16808 | 229 |
in subtr net1 net2 [] end; |
230 |
||
231 |
fun entries net = subtract (K false) empty net; |
|
232 |
||
233 |
||
234 |
(* merge *) |
|
3548 | 235 |
|
236 |
fun cons_fst x (xs, y) = (x :: xs, y); |
|
237 |
||
238 |
fun dest (Leaf xs) = map (pair []) xs |
|
16708 | 239 |
| dest (Net {comb, var, atoms}) = |
3560 | 240 |
map (cons_fst CombK) (dest comb) @ |
241 |
map (cons_fst VarK) (dest var) @ |
|
16808 | 242 |
List.concat (map (fn (a, net) => map (cons_fst (AtomK a)) (dest net)) (Symtab.dest atoms)); |
3548 | 243 |
|
16808 | 244 |
fun merge eq (net1, net2) = fold (insert_safe eq) (dest net2) net1; |
3548 | 245 |
|
0 | 246 |
end; |