Source file Fingerprint.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
(** {1 Fingerprint term indexing} *)
module T = Term
module I = Index
module S = Subst
let prof_traverse = Util.mk_profiler "fingerprint.traverse"
type feature = A | B | DB of int | N | S of ID.t | Ignore
type fingerprint_fun = T.t -> feature list
let expand_otf_ body =
let = Type.expected_args (Term.ty body) in
if CCList.is_empty extra_args then body else (
let n = List.length extra_args in
T.app (T.DB.shift n body)
(List.mapi (fun i ty -> T.bvar ~ty (n-1-i)) extra_args)
)
let rec gfpf ?(depth=0) pos t =
let t_ty = Term.ty t in
let exp_args_num = List.length (Type.expected_args t_ty) in
let _, body = T.open_fun t in
match pos with
| [] ->
let body = expand_otf_ body in
gfpf_root ~depth:(depth + exp_args_num) body
| i::is ->
let hd, args = T.as_app body in
let args = List.filter (fun x -> not @@ T.is_type x) args in
if T.is_var hd || (Type.is_var (snd (Type.open_fun (Term.ty body)))) then B
else (
let num_acutal_args = List.length args in
let ,_ = Type.open_fun (T.ty body) in
if num_acutal_args >= i then (
let arg = T.DB.shift (List.length extra_args) (List.nth args (i-1)) in
gfpf ~depth:(depth + exp_args_num) is arg
)
else if num_acutal_args + (List.length extra_args) >= i then (
let exp_arg_idx = i - num_acutal_args in
let db_ty = List.nth extra_args (exp_arg_idx-1) in
let arg = T.bvar (List.length extra_args - exp_arg_idx) ~ty:db_ty in
gfpf ~depth:(depth + exp_args_num) is arg)
else N
)
and gfpf_root ~depth t =
match T.view t with
| T.AppBuiltin(_, _) -> Ignore
| T.DB i -> if (i < depth) then DB i else Ignore
| T.Var _ -> A
| T.Const c -> S c
| T.App (hd,_) -> (match T.view hd with
T.Var _ -> A
| T.Const s -> S s
| T.DB i -> if (i < depth) then DB i else Ignore
| T.AppBuiltin(_,_) -> Ignore
| _ -> assert false)
| T.Fun (_, _) -> assert false
let pp_feature out = function
| A -> CCFormat.fprintf out "A"
| B -> CCFormat.fprintf out "B"
| DB i -> CCFormat.fprintf out "DB %d" i
| N -> CCFormat.fprintf out "N"
| S id -> CCFormat.fprintf out "S %a" ID.pp id
| Ignore -> CCFormat.fprintf out "I"
(** compute a feature vector for some positions *)
let fp positions =
let fpfs = List.map gfpf positions in
fun t ->
List.map (fun fpf -> fpf t) fpfs
(** {2 Fingerprint functions} *)
let fp3d = fp [[]; [1]; [1;1]]
let fp3w = fp [[]; [1]; [2]]
let fp4d = fp [[]; [1]; [1;1;]; [1;1;1]]
let fp4m = fp [[]; [1]; [2]; [1;1]]
let fp4w = fp [[]; [1]; [2]; [3]]
let fp5m = fp [[]; [1]; [2]; [3]; [1;1]]
let fp6m = fp [[]; [1]; [2]; [3]; [1;1]; [1;2]]
let fp7 = fp [[]; [1]; [2]; [1;1]; [1;2]; [2;1] ; [2;2]]
let fp7m = fp [[]; [1]; [2]; [3]; [1;1]; [4]; [1;2]]
let fp16 = fp [[]; [1]; [2]; [3]; [4]; [1;1]; [1;2]; [1;3]; [2;1];
[2;2]; [2;3]; [3;1]; [3;2]; [3;3]; [1;1;1]; [2;1;1]]
(** {2 Index construction} *)
let feat_to_int_ = function
| A -> 0
| B -> 1
| S _ -> 2
| N -> 3
| DB _ -> 4
| Ignore -> 5
let cmp_feature f1 f2 = match f1, f2 with
| A, A
| B, B
| N, N
| Ignore, Ignore
-> 0
| S s1, S s2 -> ID.compare s1 s2
| DB i, DB j -> compare i j
| _ -> feat_to_int_ f1 - feat_to_int_ f2
(** check whether two features are compatible for unification. *)
let compatible_features_unif f1 f2 =
match f1 with
| S s1 -> (match f2 with
| S s2 -> ID.equal s1 s2
| A | B | Ignore -> true
| N | DB _ -> false)
| Ignore -> true
| B -> true
| A -> (match f2 with
| N -> false
| DB _ | Ignore | S _ | A | B -> true)
| DB i -> (match f2 with
| DB j -> i = j
| B | A | Ignore -> true
| S _ | N -> false)
| N -> (match f2 with
| N | B | Ignore -> true
| A | DB _ | S _ -> false)
(** check whether two features are compatible for matching. *)
let compatible_features_match f1 f2 =
match f1 with
| S s1 -> (match f2 with
| S s2 -> ID.equal s1 s2
| Ignore -> true
| _ -> false)
| Ignore
| B -> true
| A -> (match f2 with
| A | DB _ | S _ | Ignore -> true
| _ -> false)
| DB i -> (match f2 with
| DB j -> i = j
| Ignore -> true
| _ -> false)
| N -> (match f2 with
| N | Ignore -> true
| _ -> false)
(** Map whose keys are features *)
module FeatureMap = Map.Make(struct
type t = feature
let compare = cmp_feature
end)
module Make(X : Set.OrderedType) = struct
type elt = X.t
module Leaf = Index.MakeLeaf(X)
type t = {
trie : trie;
fp : fingerprint_fun;
}
and trie =
| Empty
| Node of trie FeatureMap.t
| Leaf of Leaf.t
(** The index *)
let default_fp = fp7m
let empty () = {
trie = Empty;
fp = default_fp;
}
let empty_with fp = {
trie = Empty;
fp;
}
let get_fingerprint idx = idx.fp
let name = "fingerprint_idx"
let is_empty idx =
let rec is_empty trie =
match trie with
| Empty -> true
| Leaf l -> Leaf.is_empty l
| Node map -> FeatureMap.for_all (fun _ trie' -> is_empty trie') map
in is_empty idx.trie
(** add t -> data to the trie *)
let add idx t data =
let rec recurse trie features =
match trie, features with
| Empty, [] ->
let leaf = Leaf.empty in
let leaf = Leaf.add leaf t data in
Leaf leaf
| Empty, f::features' ->
let subtrie = recurse Empty features' in
let map = FeatureMap.add f subtrie FeatureMap.empty in
Node map
| Node map, f::features' ->
let subtrie =
try FeatureMap.find f map
with Not_found -> Empty in
let subtrie = recurse subtrie features' in
let map = FeatureMap.add f subtrie map in
Node map
| Leaf leaf, [] ->
let leaf = Leaf.add leaf t data in
Leaf leaf
| Node _, [] | Leaf _, _::_ ->
failwith "different feature length in fingerprint trie"
in
let features = idx.fp t in
{ idx with trie = recurse idx.trie features; }
let add_ trie = CCFun.uncurry (add trie)
let add_seq = Iter.fold add_
let add_list = List.fold_left add_
(** remove t -> data from the trie *)
let remove idx t data =
let rec recurse trie features =
match trie, features with
| Empty, [] | Empty, _::_ ->
Empty
| Node map, f::features' ->
let map =
try
let subtrie = FeatureMap.find f map in
let subtrie = recurse subtrie features' in
if subtrie = Empty
then FeatureMap.remove f map
else FeatureMap.add f subtrie map
with Not_found -> map
in
if FeatureMap.is_empty map
then Empty
else Node map
| Leaf leaf, [] ->
let leaf = Leaf.remove leaf t data in
if Leaf.is_empty leaf
then Empty
else Leaf leaf
| Node _, [] | Leaf _, _::_ ->
failwith "different feature length in fingerprint trie"
in
let features = idx.fp t in
{ idx with trie = recurse idx.trie features; }
let remove_ trie = CCFun.uncurry (remove trie)
let remove_seq dt seq = Iter.fold remove_ dt seq
let remove_list dt seq = List.fold_left remove_ dt seq
let iter idx f =
let rec iter trie f = match trie with
| Empty -> ()
| Node map -> FeatureMap.iter (fun _ subtrie -> iter subtrie f) map
| Leaf leaf -> Leaf.iter leaf f
in
iter idx.trie f
let fold idx f acc =
let rec fold trie f acc = match trie with
| Empty -> acc
| Node map -> FeatureMap.fold (fun _ subtrie acc -> fold subtrie f acc) map acc
| Leaf leaf -> Leaf.fold leaf acc f
in
fold idx.trie f acc
(** number of indexed terms *)
let size idx =
let n = ref 0 in
iter idx (fun _ _ -> incr n);
!n
(** fold on parts of the trie that are compatible with features *)
let traverse ~compatible idx features k =
Util.enter_prof prof_traverse;
let rec recurse trie features =
match trie, features with
| Empty, _ -> ()
| Leaf leaf, [] ->
k leaf
| Node map, f::features' ->
FeatureMap.iter
(fun f' subtrie ->
if compatible f f' then recurse subtrie features')
map
| Node _, [] | Leaf _, _::_ ->
failwith "different feature length in fingerprint trie"
in
try
recurse idx.trie features;
Util.exit_prof prof_traverse;
with e ->
Util.exit_prof prof_traverse;
raise e
let retrieve_unifiables_aux fold_unify (idx,sc_idx) t k =
let features = idx.fp (fst t) in
let compatible = compatible_features_unif in
traverse ~compatible idx features
(fun leaf -> fold_unify (leaf,sc_idx) t k)
let retrieve_unifiables = retrieve_unifiables_aux Leaf.fold_unify
let retrieve_unifiables_complete ?(unif_alg=JP_unif.unify_scoped) =
retrieve_unifiables_aux (Leaf.fold_unify_complete ~unif_alg)
let retrieve_generalizations ?(subst=S.empty) (idx,sc_idx) t k =
let features = idx.fp (fst t) in
let compatible f1 f2 = compatible_features_match f2 f1 in
traverse ~compatible idx features
(fun leaf -> Leaf.fold_match ~subst (leaf,sc_idx) t k)
let retrieve_specializations ?(subst=S.empty) (idx,sc_idx) t k =
let features = idx.fp (fst t) in
let compatible = compatible_features_match in
traverse ~compatible idx features
(fun leaf -> Leaf.fold_matched ~subst (leaf,sc_idx) t k)
let to_dot _ _ =
failwith "Fingerprint: to_dot not implemented"
end