1 (* Copyright (C) 2004, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 let critical_value = 7
31 module StringSet = Set.Make (String)
32 module SetSet = Set.Make (StringSet)
34 type term_signature = (string * string list) option * StringSet.t
36 type cardinality_condition =
41 type rating_criterion =
42 [ `Hits (** order by number of hits, most used objects first *)
45 let tbln n = "table" ^ string_of_int n
48 let add_depth_constr depth_opt cur_tbl where =
51 | Some depth -> (sprintf "%s.h_depth = %d" cur_tbl depth) :: where
54 let mk_positions positions cur_tbl =
59 let pos_str = MetadataPp.pp_position_tag pos in
64 | `MainConclusion None
65 | `MainHypothesis None ->
66 sprintf "%s.h_position = \"%s\"" cur_tbl pos_str
67 | `MainConclusion (Some d)
68 | `MainHypothesis (Some d) ->
69 sprintf "(%s.h_position = \"%s\" and %s.h_depth = %d)"
70 cur_tbl pos_str cur_tbl d)
71 (positions :> MetadataTypes.position list)) ^
74 let explode_card_constr = function
75 | Eq card -> "=", card
76 | Gt card -> ">", card
77 | Lt card -> "<", card
79 let add_card_constr tbl col where = function
82 let op, card = explode_card_constr constr in
83 (* count(_utente).hypothesis = 3 *)
84 (sprintf "%s.%s %s %d" tbl col op card :: where)
86 let add_diff_constr tbl where = function
89 let op, card = explode_card_constr constr in
90 (sprintf "%s.hypothesis - %s.conclusion %s %d" tbl tbl op card :: where)
92 let add_all_constr tbl (n,from,where) concl full diff =
93 match (concl, full, diff) with
94 | None, None, None -> (n,from,where)
96 let where = add_card_constr tbl "conclusion" where concl in
97 let where = add_card_constr tbl "statement" where full in
98 let where = add_diff_constr tbl where diff in
101 sprintf "table0.source = %s.source" tbl :: where
106 let add_constraint tables (n,from,where) metadata =
107 let obj_tbl,rel_tbl,sort_tbl,count_tbl = tables
109 let cur_tbl = tbln n in
111 | `Obj (uri, positions) ->
112 let from = (sprintf "%s as %s" obj_tbl cur_tbl) :: from in
114 (sprintf "(%s.h_occurrence = \"%s\")" cur_tbl uri) ::
115 mk_positions positions cur_tbl ::
117 else [sprintf "table0.source = %s.source" cur_tbl]) @
122 let from = (sprintf "%s as %s" rel_tbl cur_tbl) :: from in
124 mk_positions positions cur_tbl ::
126 else [sprintf "table0.source = %s.source" cur_tbl]) @
130 | `Sort (sort, positions) ->
131 let sort_str = CicPp.ppsort sort in
132 let from = (sprintf "%s as %s" sort_tbl cur_tbl) :: from in
134 (sprintf "%s.h_sort = \"%s\"" cur_tbl sort_str ) ::
135 mk_positions positions cur_tbl ::
139 [sprintf "table0.source = %s.source" cur_tbl ]) @ where
144 let at_least ~(dbd:Mysql.dbd) ?concl_card ?full_card ?diff ?rating tables
145 (metadata: MetadataTypes.constr list)
147 let obj_tbl,rel_tbl,sort_tbl, count_tbl = tables
149 if (metadata = []) && concl_card = None && full_card = None then
150 failwith "MetadataQuery.at_least: no constraints given";
152 List.fold_left (add_constraint tables) (0,[],[]) metadata
155 add_all_constr count_tbl (n,from,where) concl_card full_card diff
157 let from = String.concat ", " from in
158 let where = String.concat " and " where in
161 | None -> sprintf "select table0.source from %s where %s" from where
164 ("select table0.source from %s, hits where %s"
165 ^^ " and hits.source = table0.source order by hits.no desc")
169 let result = Mysql.exec dbd query in
171 (fun row -> match row.(0) with Some s -> s | _ -> assert false)
175 ~(dbd:Mysql.dbd) ?concl_card ?full_card ?diff ?rating
176 (metadata: MetadataTypes.constr list)
178 let module MT = MetadataTypes in
179 if MT.are_tables_ownerized () then
180 (at_least ~dbd ?concl_card ?full_card ?diff ?rating
181 (MT.obj_tbl (),MT.rel_tbl (),MT.sort_tbl (), MT.count_tbl ())
184 (at_least ~dbd ?concl_card ?full_card ?diff ?rating
185 (MT.library_obj_tbl,MT.library_rel_tbl,MT.library_sort_tbl,
186 MT.library_count_tbl)
189 at_least ~dbd ?concl_card ?full_card ?diff ?rating
190 (MT.library_obj_tbl,MT.library_rel_tbl,MT.library_sort_tbl,
191 MT.library_count_tbl)
195 (** Prefix handling *)
197 let filter_by_card n =
198 SetSet.filter (fun t -> (StringSet.cardinal t) <= n)
201 let init = SetSet.union a b in
202 let merge_single_set s1 b =
204 (fun s2 res -> SetSet.add (StringSet.union s1 s2) res)
207 SetSet.fold (fun s1 res -> SetSet.union (merge_single_set s1 b) res) a init
211 let rec inspect_children n childs =
213 (fun res term -> merge n (inspect_conclusion n term) res)
216 and add_root n root childs =
217 let childunion = inspect_children n childs in
218 let addroot = StringSet.add root in
220 (fun child newsets -> SetSet.add (addroot child) newsets)
222 (SetSet.singleton (StringSet.singleton root))
224 and inspect_conclusion n t =
225 if n = 0 then SetSet.empty
230 | Cic.Implicit _ -> SetSet.empty
231 | Cic.Var (u,exp_named_subst) -> SetSet.empty
232 | Cic.Const (u,exp_named_subst) ->
233 SetSet.singleton (StringSet.singleton (UriManager.string_of_uri u))
234 | Cic.MutInd (u, t, exp_named_subst) ->
235 SetSet.singleton (StringSet.singleton
236 (UriManager.string_of_uriref (u, [t])))
237 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
238 SetSet.singleton (StringSet.singleton
239 (UriManager.string_of_uriref (u, [t; c])))
240 | Cic.Cast (t, _) -> inspect_conclusion n t
241 | Cic.Prod (_, s, t) ->
242 merge n (inspect_conclusion n s) (inspect_conclusion n t)
243 | Cic.Lambda (_, s, t) ->
244 merge n (inspect_conclusion n s) (inspect_conclusion n t)
245 | Cic.LetIn (_, s, t) ->
246 merge n (inspect_conclusion n s) (inspect_conclusion n t)
247 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
248 let suri = UriManager.string_of_uri u in
249 add_root (n-1) suri l
250 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
251 let suri = UriManager.string_of_uriref (u, [t]) in
252 add_root (n-1) suri l
253 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
254 let suri = UriManager.string_of_uriref (u, [t; c]) in
255 add_root (n-1) suri l
258 | Cic.MutCase (u, t, tt, uu, m) ->
262 | Cic.CoFix (_, m) ->
265 let rec inspect_term n t =
273 | Cic.Implicit _ -> None, SetSet.empty
274 | Cic.Var (u,exp_named_subst) -> None, SetSet.empty
275 | Cic.Const (u,exp_named_subst) ->
276 Some (UriManager.string_of_uri u), SetSet.empty
277 | Cic.MutInd (u, t, exp_named_subst) ->
278 let uri = UriManager.string_of_uriref (u, [t]) in
279 Some uri, SetSet.empty
280 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
281 let uri = UriManager.string_of_uriref (u, [t; c]) in
282 Some uri, SetSet.empty
283 | Cic.Cast (t, _) -> inspect_term n t
284 | Cic.Prod (_, _, t) -> inspect_term n t
285 | Cic.LetIn (_, _, t) -> inspect_term n t
286 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
287 let suri = UriManager.string_of_uri u in
288 let childunion = inspect_children (n-1) l in
289 Some suri, childunion
290 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
291 let suri = UriManager.string_of_uriref (u, [t]) in
292 if u = HelmLibraryObjects.Logic.eq_URI && n>1 then
293 (* equality is handled in a special way: in particular,
294 the type, if defined, is always added to the prefix,
295 and n is not decremented - it should have been n-2 *)
297 Cic.Const (u1,exp_named_subst1)::l1 ->
298 let suri1 = UriManager.string_of_uri u1 in
299 let inconcl = add_root (n-1) suri1 l1 in
301 | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
302 let suri1 = UriManager.string_of_uriref (u1, [t1]) in
303 let inconcl = add_root (n-1) suri1 l1 in
305 | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
306 let suri1 = UriManager.string_of_uriref (u1, [t1; c1]) in
307 let inconcl = add_root (n-1) suri1 l1 in
309 | _ :: _ -> Some suri, SetSet.empty
310 | _ -> assert false (* args number must be > 0 *)
312 let childunion = inspect_children (n-1) l in
313 Some suri, childunion
314 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
315 let suri = UriManager.string_of_uriref (u, [t; c]) in
316 let childunion = inspect_children (n-1) l in
317 Some suri, childunion
318 | _ -> None, SetSet.empty
320 let add_cardinality s =
321 let l = SetSet.elements s in
325 let el = StringSet.elements set in
326 (List.length el, el)) l in
327 (* ordered by descending cardinality *)
328 List.sort (fun (n,_) (m,_) -> m - n) ((0,[])::res)
331 match inspect_term n t with
332 Some a, set -> Some a, add_cardinality set
333 | None, set when (SetSet.is_empty set) -> None, []
334 | _, _ -> assert false
337 let rec add children =
339 (fun acc t -> StringSet.union (signature_concl t) acc)
340 (StringSet.empty) children
342 (* this function creates the set of all different constants appearing in
343 the conclusion of the term *)
344 and signature_concl =
349 | Cic.Implicit _ -> StringSet.empty
350 | Cic.Var (u,exp_named_subst) -> StringSet.empty
351 | Cic.Const (u,exp_named_subst) ->
352 StringSet.singleton (UriManager.string_of_uri u)
353 | Cic.MutInd (u, t, exp_named_subst) ->
354 let uri = UriManager.string_of_uriref (u, [t]) in
355 StringSet.singleton uri
356 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
357 let uri = UriManager.string_of_uriref (u, [t;c]) in
358 StringSet.singleton uri
359 | Cic.Cast (t, _) -> signature_concl t
360 | Cic.Prod (_, s, t) ->
361 StringSet.union (signature_concl s) (signature_concl t)
362 | Cic.Lambda (_, s, t) ->
363 StringSet.union (signature_concl s) (signature_concl t)
364 | Cic.LetIn (_, s, t) ->
365 StringSet.union (signature_concl s) (signature_concl t)
366 | Cic.Appl l -> add l
372 let rec signature_of = function
373 | Cic.Cast (t, _) -> signature_of t
374 | Cic.Prod (_, _, t) -> signature_of t
375 | Cic.LetIn (_, _, t) -> signature_of t
376 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
377 let suri = UriManager.string_of_uri u in
378 Some (suri, []), add l
379 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
380 let suri = UriManager.string_of_uriref (u, [t]) in
381 if u = HelmLibraryObjects.Logic.eq_URI then
382 (* equality is handled in a special way: in particular,
383 the type, if defined, is always added to the prefix,
384 and n is not decremented - it should have been n-2 *)
386 Cic.Const (u1,exp_named_subst1)::l1 ->
387 let suri1 = UriManager.string_of_uri u1 in
388 let inconcl = StringSet.remove suri1 (add l1) in
389 Some (suri, [suri1]), inconcl
390 | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
391 let suri1 = UriManager.string_of_uriref (u1, [t1]) in
392 let inconcl = StringSet.remove suri1 (add l1) in
393 Some (suri, [suri1]), inconcl
394 | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
395 let suri1 = UriManager.string_of_uriref (u1, [t1;c1]) in
396 let inconcl = StringSet.remove suri1 (add l1) in
397 Some (suri, [suri1]), inconcl
398 | _ :: _ -> Some (suri, []), StringSet.empty
399 | _ -> assert false (* args number must be > 0 *)
401 Some (suri, []), add l
402 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
403 let suri = UriManager.string_of_uriref (u, [t;c]) in
404 Some (suri, []), add l
405 | t -> None, signature_concl t
407 (* takes a list of lists and returns the list of all elements
408 without repetitions *)
410 let rec drop_repetitions = function
413 | u1::u2::l when u1 = u2 -> drop_repetitions (u2::l)
414 | u::l -> u::(drop_repetitions l) in
415 drop_repetitions (List.sort Pervasives.compare (List.concat l))
417 let must_of_prefix ?(where = `Conclusion) m s =
420 | `Conclusion -> [`InConclusion]
421 | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
423 let s' = List.map (fun u -> `Obj (u, positions)) s in
424 `Obj (m, [`MainConclusion None]) :: s'
426 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
428 let get_constants (dbd:Mysql.dbd) ~where uri =
429 let uri = escape uri in
432 | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
434 [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
435 MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
440 (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
442 sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION "^^
443 "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)")
444 (MetadataTypes.obj_tbl ()) uri pos_predicate
445 MetadataTypes.library_obj_tbl uri pos_predicate
448 let result = Mysql.exec dbd query in
449 let set = ref StringSet.empty in
453 | Some uri -> set := StringSet.add uri !set
454 | _ -> assert false);
457 let at_most ~(dbd:Mysql.dbd) ?(where = `Conclusion) only u =
458 let inconcl = get_constants dbd ~where u in
459 StringSet.subset inconcl only
461 (* Special handling of equality. The problem is filtering out theorems just
462 * containing variables (e.g. all the theorems in cic:/Coq/Ring/). Really
463 * ad-hoc, no better solution found at the moment *)
464 let myspeciallist_of_facts =
465 [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
467 [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
468 (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
469 0,"cic:/Coq/Init/Logic/trans_eq.con";
470 0,"cic:/Coq/Init/Logic/f_equal.con";
471 0,"cic:/Coq/Init/Logic/f_equal2.con";
472 0,"cic:/Coq/Init/Logic/f_equal3.con"]
475 let compute_exactly ~(dbd:Mysql.dbd) ?(facts=false) ~where main prefixes =
479 if ((m = 0) && (main = HelmLibraryObjects.Logic.eq_XURI)) then
480 (if facts then myspeciallist_of_facts
484 let must = must_of_prefix ~where main s in
486 | `Conclusion -> at_least ~dbd ~concl_card:(Eq (m+1)) must
487 | `Statement -> at_least ~dbd ~full_card:(Eq (m+1)) must
489 List.map (fun uri -> (m, uri)) res)
492 (* critical value reached, fallback to "only" constraints *)
494 let compute_with_only ~(dbd:Mysql.dbd) ?(facts=false) ?(where = `Conclusion)
495 main prefixes constants
497 let max_prefix_length =
500 | (max,_)::_ -> max in
501 let maximal_prefixes =
502 let rec filter res = function
504 | (n,s)::l when n = max_prefix_length -> filter ((n,s)::res) l
506 filter [] prefixes in
512 let must = must_of_prefix ~where main s in
515 | `Conclusion -> at_least ~dbd ~concl_card:(Gt (m+1)) must
516 | `Statement -> at_least ~dbd ~full_card:(Gt (m+1)) must
518 (* we tag the uri with m+1, for sorting purposes *)
519 List.map (fun uri -> (m+1, uri)) res))
522 List.filter (function (_,uri) -> at_most ~dbd ~where constants uri) all in
523 let equal_to = compute_exactly ~dbd ~facts ~where main prefixes in
524 greater_than @ equal_to
526 (* real match query implementation *)
528 let cmatch ~(dbd:Mysql.dbd) ?(facts=false) t =
529 let (main, constants) = signature_of t in
532 | Some (main, types) ->
533 (* the type of eq is not counted in constants_no *)
534 let types_no = List.length types in
535 let constants_no = StringSet.cardinal constants in
536 if (constants_no > critical_value) then
537 let prefixes = prefixes just_factor t in
539 | Some main, all_concl ->
541 List.fold_right StringSet.add types (StringSet.add main constants)
543 compute_with_only ~dbd ~facts main all_concl all_constants
546 (* in this case we compute all prefixes, and we do not need
547 to apply the only constraints *)
549 if constants_no = 0 then
550 (if types_no = 0 then
553 Some main, [0, []; types_no, types])
555 prefixes (constants_no+types_no+1) t
558 Some main, all_concl ->
559 compute_exactly ~dbd ~facts ~where:`Conclusion main all_concl
564 let must = must_of_prefix ~where:`Conclusion main s in
565 let res = at_least ~dbd ~concl_card:(Eq (m+1)) must in
566 List.map (fun uri -> (m, uri)) res)
570 let power_upto upto consts =
571 let l = StringSet.elements consts in
572 List.sort (fun (n,_) (m,_) -> m - n)
575 List.filter (function (n,l) -> n <= upto)
576 res@(List.map (function (n,l) -> (n+1,a::l)) res))
580 let l = StringSet.elements consts in
581 List.sort (fun (n,_) (m,_) -> m - n)
583 (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res))
586 type where = [ `Conclusion | `Statement ]
588 let sigmatch ~(dbd:Mysql.dbd)
589 ?(facts=false) ?(where = `Conclusion) (main, constants) =
592 | Some (main, types) ->
593 let constants_no = StringSet.cardinal constants in
594 if (constants_no > critical_value) then
596 let subsets = power_upto just_factor constants in
597 let types_no = List.length types in
598 List.map (function (n,l) -> (n+types_no,types@l)) subsets
601 List.fold_right StringSet.add types (StringSet.add main constants)
603 compute_with_only ~dbd ~where main subsets all_constants
606 let subsets = power constants in
607 let types_no = List.length types in
609 (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
612 compute_exactly ~dbd ~facts ~where main subsets
614 (* match query wrappers *)
618 let cmatch ~dbd ?(facts=false) term =
621 (fun x y -> Pervasives.compare (fst y) (fst x))
622 (cmatch' ~dbd ~facts term))
624 let constants_of = signature_concl