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
154 let selected = if metadata = [] then count_tbl else "table0" in
156 add_all_constr count_tbl (n,from,where) concl_card full_card diff
158 let from = String.concat ", " from in
159 let where = String.concat " and " where in
162 | None -> sprintf "select %s.source from %s where %s" selected from where
165 ("select %s.source from %s, hits where %s"
166 ^^ " and hits.source = %s.source order by hits.no desc")
167 selected from where selected
170 let result = Mysql.exec dbd query in
172 (fun row -> match row.(0) with Some s -> s | _ -> assert false)
176 ~(dbd:Mysql.dbd) ?concl_card ?full_card ?diff ?rating
177 (metadata: MetadataTypes.constr list)
179 let module MT = MetadataTypes in
180 if MT.are_tables_ownerized () then
181 (at_least ~dbd ?concl_card ?full_card ?diff ?rating
182 (MT.obj_tbl (),MT.rel_tbl (),MT.sort_tbl (), MT.count_tbl ())
185 (at_least ~dbd ?concl_card ?full_card ?diff ?rating
186 (MT.library_obj_tbl,MT.library_rel_tbl,MT.library_sort_tbl,
187 MT.library_count_tbl)
190 at_least ~dbd ?concl_card ?full_card ?diff ?rating
191 (MT.library_obj_tbl,MT.library_rel_tbl,MT.library_sort_tbl,
192 MT.library_count_tbl)
196 (** Prefix handling *)
198 let filter_by_card n =
199 SetSet.filter (fun t -> (StringSet.cardinal t) <= n)
202 let init = SetSet.union a b in
203 let merge_single_set s1 b =
205 (fun s2 res -> SetSet.add (StringSet.union s1 s2) res)
208 SetSet.fold (fun s1 res -> SetSet.union (merge_single_set s1 b) res) a init
212 let rec inspect_children n childs =
214 (fun res term -> merge n (inspect_conclusion n term) res)
217 and add_root n root childs =
218 let childunion = inspect_children n childs in
219 let addroot = StringSet.add root in
221 (fun child newsets -> SetSet.add (addroot child) newsets)
223 (SetSet.singleton (StringSet.singleton root))
225 and inspect_conclusion n t =
226 if n = 0 then SetSet.empty
231 | Cic.Implicit _ -> SetSet.empty
232 | Cic.Var (u,exp_named_subst) -> SetSet.empty
233 | Cic.Const (u,exp_named_subst) ->
234 SetSet.singleton (StringSet.singleton (UriManager.string_of_uri u))
235 | Cic.MutInd (u, t, exp_named_subst) ->
236 SetSet.singleton (StringSet.singleton
237 (UriManager.string_of_uriref (u, [t])))
238 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
239 SetSet.singleton (StringSet.singleton
240 (UriManager.string_of_uriref (u, [t; c])))
241 | Cic.Cast (t, _) -> inspect_conclusion n t
242 | Cic.Prod (_, s, t) ->
243 merge n (inspect_conclusion n s) (inspect_conclusion n t)
244 | Cic.Lambda (_, s, t) ->
245 merge n (inspect_conclusion n s) (inspect_conclusion n t)
246 | Cic.LetIn (_, s, t) ->
247 merge n (inspect_conclusion n s) (inspect_conclusion n t)
248 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
249 let suri = UriManager.string_of_uri u in
250 add_root (n-1) suri l
251 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
252 let suri = UriManager.string_of_uriref (u, [t]) in
253 add_root (n-1) suri l
254 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
255 let suri = UriManager.string_of_uriref (u, [t; c]) in
256 add_root (n-1) suri l
259 | Cic.MutCase (u, t, tt, uu, m) ->
263 | Cic.CoFix (_, m) ->
266 let rec inspect_term n t =
274 | Cic.Implicit _ -> None, SetSet.empty
275 | Cic.Var (u,exp_named_subst) -> None, SetSet.empty
276 | Cic.Const (u,exp_named_subst) ->
277 Some (UriManager.string_of_uri u), SetSet.empty
278 | Cic.MutInd (u, t, exp_named_subst) ->
279 let uri = UriManager.string_of_uriref (u, [t]) in
280 Some uri, SetSet.empty
281 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
282 let uri = UriManager.string_of_uriref (u, [t; c]) in
283 Some uri, SetSet.empty
284 | Cic.Cast (t, _) -> inspect_term n t
285 | Cic.Prod (_, _, t) -> inspect_term n t
286 | Cic.LetIn (_, _, t) -> inspect_term n t
287 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
288 let suri = UriManager.string_of_uri u in
289 let childunion = inspect_children (n-1) l in
290 Some suri, childunion
291 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
292 let suri = UriManager.string_of_uriref (u, [t]) in
293 if u = HelmLibraryObjects.Logic.eq_URI && n>1 then
294 (* equality is handled in a special way: in particular,
295 the type, if defined, is always added to the prefix,
296 and n is not decremented - it should have been n-2 *)
298 Cic.Const (u1,exp_named_subst1)::l1 ->
299 let suri1 = UriManager.string_of_uri u1 in
300 let inconcl = add_root (n-1) suri1 l1 in
302 | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
303 let suri1 = UriManager.string_of_uriref (u1, [t1]) in
304 let inconcl = add_root (n-1) suri1 l1 in
306 | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
307 let suri1 = UriManager.string_of_uriref (u1, [t1; c1]) in
308 let inconcl = add_root (n-1) suri1 l1 in
310 | _ :: _ -> Some suri, SetSet.empty
311 | _ -> assert false (* args number must be > 0 *)
313 let childunion = inspect_children (n-1) l in
314 Some suri, childunion
315 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
316 let suri = UriManager.string_of_uriref (u, [t; c]) in
317 let childunion = inspect_children (n-1) l in
318 Some suri, childunion
319 | _ -> None, SetSet.empty
321 let add_cardinality s =
322 let l = SetSet.elements s in
326 let el = StringSet.elements set in
327 (List.length el, el)) l in
328 (* ordered by descending cardinality *)
329 List.sort (fun (n,_) (m,_) -> m - n) ((0,[])::res)
332 match inspect_term n t with
333 Some a, set -> Some a, add_cardinality set
334 | None, set when (SetSet.is_empty set) -> None, []
335 | _, _ -> assert false
338 let rec add children =
340 (fun acc t -> StringSet.union (signature_concl t) acc)
341 (StringSet.empty) children
343 (* this function creates the set of all different constants appearing in
344 the conclusion of the term *)
345 and signature_concl =
350 | Cic.Implicit _ -> StringSet.empty
351 | Cic.Var (u,exp_named_subst) -> StringSet.empty
352 | Cic.Const (u,exp_named_subst) ->
353 StringSet.singleton (UriManager.string_of_uri u)
354 | Cic.MutInd (u, t, exp_named_subst) ->
355 let uri = UriManager.string_of_uriref (u, [t]) in
356 StringSet.singleton uri
357 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
358 let uri = UriManager.string_of_uriref (u, [t;c]) in
359 StringSet.singleton uri
360 | Cic.Cast (t, _) -> signature_concl t
361 | Cic.Prod (_, s, t) ->
362 StringSet.union (signature_concl s) (signature_concl t)
363 | Cic.Lambda (_, s, t) ->
364 StringSet.union (signature_concl s) (signature_concl t)
365 | Cic.LetIn (_, s, t) ->
366 StringSet.union (signature_concl s) (signature_concl t)
367 | Cic.Appl l -> add l
373 let rec signature_of = function
374 | Cic.Cast (t, _) -> signature_of t
375 | Cic.Prod (_, _, t) -> signature_of t
376 | Cic.LetIn (_, _, t) -> signature_of t
377 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
378 let suri = UriManager.string_of_uri u in
379 Some (suri, []), add l
380 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
381 let suri = UriManager.string_of_uriref (u, [t]) in
382 if u = HelmLibraryObjects.Logic.eq_URI then
383 (* equality is handled in a special way: in particular,
384 the type, if defined, is always added to the prefix,
385 and n is not decremented - it should have been n-2 *)
387 Cic.Const (u1,exp_named_subst1)::l1 ->
388 let suri1 = UriManager.string_of_uri u1 in
389 let inconcl = StringSet.remove suri1 (add l1) in
390 Some (suri, [suri1]), inconcl
391 | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
392 let suri1 = UriManager.string_of_uriref (u1, [t1]) in
393 let inconcl = StringSet.remove suri1 (add l1) in
394 Some (suri, [suri1]), inconcl
395 | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
396 let suri1 = UriManager.string_of_uriref (u1, [t1;c1]) in
397 let inconcl = StringSet.remove suri1 (add l1) in
398 Some (suri, [suri1]), inconcl
399 | _ :: _ -> Some (suri, []), StringSet.empty
400 | _ -> assert false (* args number must be > 0 *)
402 Some (suri, []), add l
403 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
404 let suri = UriManager.string_of_uriref (u, [t;c]) in
405 Some (suri, []), add l
406 | t -> None, signature_concl t
408 (* takes a list of lists and returns the list of all elements
409 without repetitions *)
411 let rec drop_repetitions = function
414 | u1::u2::l when u1 = u2 -> drop_repetitions (u2::l)
415 | u::l -> u::(drop_repetitions l) in
416 drop_repetitions (List.sort Pervasives.compare (List.concat l))
418 let must_of_prefix ?(where = `Conclusion) m s =
421 | `Conclusion -> [`InConclusion]
422 | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
424 let s' = List.map (fun u -> `Obj (u, positions)) s in
425 `Obj (m, [`MainConclusion None]) :: s'
427 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
429 let get_constants (dbd:Mysql.dbd) ~where uri =
430 let uri = escape uri in
433 | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
435 [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
436 MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
441 (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
443 sprintf ("SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s) UNION "^^
444 "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)")
445 (MetadataTypes.obj_tbl ()) uri pos_predicate
446 MetadataTypes.library_obj_tbl uri pos_predicate
449 let result = Mysql.exec dbd query in
450 let set = ref StringSet.empty in
454 | Some uri -> set := StringSet.add uri !set
455 | _ -> assert false);
458 let at_most ~(dbd:Mysql.dbd) ?(where = `Conclusion) only u =
459 let inconcl = get_constants dbd ~where u in
460 StringSet.subset inconcl only
462 (* Special handling of equality. The problem is filtering out theorems just
463 * containing variables (e.g. all the theorems in cic:/Coq/Ring/). Really
464 * ad-hoc, no better solution found at the moment *)
465 let myspeciallist_of_facts =
466 [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
468 [0,"cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
469 (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
470 0,"cic:/Coq/Init/Logic/trans_eq.con";
471 0,"cic:/Coq/Init/Logic/f_equal.con";
472 0,"cic:/Coq/Init/Logic/f_equal2.con";
473 0,"cic:/Coq/Init/Logic/f_equal3.con"]
476 let compute_exactly ~(dbd:Mysql.dbd) ?(facts=false) ~where main prefixes =
480 if ((m = 0) && (main = HelmLibraryObjects.Logic.eq_XURI)) then
481 (if facts then myspeciallist_of_facts
485 let must = must_of_prefix ~where main s in
487 | `Conclusion -> at_least ~dbd ~concl_card:(Eq (m+1)) must
488 | `Statement -> at_least ~dbd ~full_card:(Eq (m+1)) must
490 List.map (fun uri -> (m, uri)) res)
493 (* critical value reached, fallback to "only" constraints *)
495 let compute_with_only ~(dbd:Mysql.dbd) ?(facts=false) ?(where = `Conclusion)
496 main prefixes constants
498 let max_prefix_length =
501 | (max,_)::_ -> max in
502 let maximal_prefixes =
503 let rec filter res = function
505 | (n,s)::l when n = max_prefix_length -> filter ((n,s)::res) l
507 filter [] prefixes in
513 let must = must_of_prefix ~where main s in
516 | `Conclusion -> at_least ~dbd ~concl_card:(Gt (m+1)) must
517 | `Statement -> at_least ~dbd ~full_card:(Gt (m+1)) must
519 (* we tag the uri with m+1, for sorting purposes *)
520 List.map (fun uri -> (m+1, uri)) res))
523 List.filter (function (_,uri) -> at_most ~dbd ~where constants uri) all in
524 let equal_to = compute_exactly ~dbd ~facts ~where main prefixes in
525 greater_than @ equal_to
527 (* real match query implementation *)
529 let cmatch ~(dbd:Mysql.dbd) ?(facts=false) t =
530 let (main, constants) = signature_of t in
533 | Some (main, types) ->
534 (* the type of eq is not counted in constants_no *)
535 let types_no = List.length types in
536 let constants_no = StringSet.cardinal constants in
537 if (constants_no > critical_value) then
538 let prefixes = prefixes just_factor t in
540 | Some main, all_concl ->
542 List.fold_right StringSet.add types (StringSet.add main constants)
544 compute_with_only ~dbd ~facts main all_concl all_constants
547 (* in this case we compute all prefixes, and we do not need
548 to apply the only constraints *)
550 if constants_no = 0 then
551 (if types_no = 0 then
554 Some main, [0, []; types_no, types])
556 prefixes (constants_no+types_no+1) t
559 Some main, all_concl ->
560 compute_exactly ~dbd ~facts ~where:`Conclusion main all_concl
565 let must = must_of_prefix ~where:`Conclusion main s in
566 let res = at_least ~dbd ~concl_card:(Eq (m+1)) must in
567 List.map (fun uri -> (m, uri)) res)
571 let power_upto upto consts =
572 let l = StringSet.elements consts in
573 List.sort (fun (n,_) (m,_) -> m - n)
576 List.filter (function (n,l) -> n <= upto)
577 res@(List.map (function (n,l) -> (n+1,a::l)) res))
581 let l = StringSet.elements consts in
582 List.sort (fun (n,_) (m,_) -> m - n)
584 (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res))
587 type where = [ `Conclusion | `Statement ]
589 let sigmatch ~(dbd:Mysql.dbd)
590 ?(facts=false) ?(where = `Conclusion) (main, constants) =
593 | Some (main, types) ->
594 let constants_no = StringSet.cardinal constants in
595 if (constants_no > critical_value) then
597 let subsets = power_upto just_factor constants in
598 let types_no = List.length types in
599 List.map (function (n,l) -> (n+types_no,types@l)) subsets
602 List.fold_right StringSet.add types (StringSet.add main constants)
604 compute_with_only ~dbd ~where main subsets all_constants
607 let subsets = power constants in
608 let types_no = List.length types in
610 (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
613 compute_exactly ~dbd ~facts ~where main subsets
615 (* match query wrappers *)
619 let cmatch ~dbd ?(facts=false) term =
622 (fun x y -> Pervasives.compare (fst y) (fst x))
623 (cmatch' ~dbd ~facts term))
625 let constants_of = signature_concl