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

(* This file is free software, part of Zipperposition. See file "license" for more details. *)


(** {1 Fingerprint term indexing} *)

module T = Term
module I = Index
module S = Subst

let prof_traverse = Util.mk_profiler "fingerprint.traverse"

(* a feature.
   A = variable
   B = below variable
   NonFO = Builtin/nonFO (non-syntactically unifiable)
   N = invalid position
   S = symbol
*)
type feature = A | B | NonFO | N | S of ID.t

(* a fingerprint function, it computes several features of a term *)
type fingerprint_fun = T.t -> feature list

(* TODO: use a feature array, rather than a list *)

(* TODO: more efficient implem of traversal, only following branches that
   are useful instead of folding and filtering *)

(* compute a feature for a given position *)
let rec gfpf pos t = match pos, T.Classic.view t with
  | [], T.Classic.Var _ -> A
  | [], T.Classic.DB _ -> B
  | [], _
    when not (Unif.Ty.type_is_unifiable @@ T.ty t) ||
         Type.is_fun (T.ty t) -> NonFO
  | [], T.Classic.App (s, _) -> S s
  | i::pos', T.Classic.App (_, l) ->
    begin try gfpf pos' (List.nth l i)  (* recurse in subterm *)
      with Failure _ -> N  (* not a position in t *)
    end
  | _::_, T.Classic.DB _ -> N
  | _::_, T.Classic.Var _ -> B  (* under variable *)
  | _, T.Classic.AppBuiltin _
  | _, T.Classic.NonFO -> NonFO (* don't filter! *)

(* TODO more efficient way to compute a vector of features: if the fingerprint
   is in BFS, compute features during only one traversal of the term? *)

(** compute a feature vector for some positions *)
let fp positions =
  (* list of fingerprint feature functions *)
  let fpfs = List.map (fun pos -> gfpf pos) 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
  | NonFO -> 4

let cmp_feature f1 f2 = match f1, f2 with
  | A, A
  | B, B
  | N, N
  | NonFO, NonFO
    -> 0
  | S s1, S s2 -> ID.compare s1 s2
  | _ -> feat_to_int_ f1 - feat_to_int_ f2

(** check whether two features are compatible for unification. *)
let compatible_features_unif f1 f2 =
  match f1, f2 with
    | S s1, S s2 -> ID.equal s1 s2
    | NonFO, _ | _, NonFO
    | B, _ | _, B -> true
    | A, N | N, A -> false
    | A, _ | _, A -> true
    | N, S _ | S _, N -> false
    | N, N -> true

(** check whether two features are compatible for matching. *)
let compatible_features_match f1 f2 =
  match f1, f2 with
    | S s1, S s2 -> ID.equal s1 s2
    | NonFO, _ | _, NonFO
    | B, _ -> true
    | N, N -> true
    | N, _ -> false
    | _, N -> false
    | A, B -> false
    | A, _ -> true
    | S _, _ -> 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 =
    (* recursive insertion *)
    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 (* creation of new leaf *)
        | Empty, f::features' ->
          let subtrie = recurse Empty features' in
          let map = FeatureMap.add f subtrie FeatureMap.empty in
          Node map  (* index new subtrie by feature *)
        | Node map, f::features' ->
          let subtrie =
            try FeatureMap.find f map
            with Not_found -> Empty in
          (* insert in subtrie *)
          let subtrie = recurse subtrie features' in
          let map = FeatureMap.add f subtrie map in
          Node map  (* point to new subtrie *)
        | Leaf leaf, [] ->
          let leaf = Leaf.add leaf t data in
          Leaf leaf (* addition to set *)
        | Node _, [] | Leaf _, _::_ ->
          failwith "different feature length in fingerprint trie"
    in
    let features = idx.fp t in  (* features of term *)
    { 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 =
    (* recursive deletion *)
    let rec recurse trie features =
      match trie, features with
        | Empty, [] | Empty, _::_ ->
          Empty (* keep it empty *)
        | Node map, f::features' ->
          let map =
            (* delete from subtrie, if there is a subtrie *)
            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 the map is empty, use Empty *)
          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  (* features of term *)
    { 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;
    (* fold on the trie *)
    let rec recurse trie features =
      match trie, features with
        | Empty, _ -> ()
        | Leaf leaf, [] ->
          k leaf  (* give the leaf to [k] *)
        | Node map, f::features' ->
          (* fold on any subtrie that is compatible with current feature *)
          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 ?(subst=Unif_subst.empty) (idx,sc_idx) t k =
    let features = idx.fp (fst t) in
    let compatible = compatible_features_unif in
    traverse ~compatible idx features
      (fun leaf -> Leaf.fold_unify ~subst (leaf,sc_idx) t k)

  let retrieve_generalizations ?(subst=S.empty) (idx,sc_idx) t k =
    let features = idx.fp (fst t) in
    (* compatible t1 t2 if t2 can match t1 *)
    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