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/
31 let critical_value = 7
34 module UriManagerSet = UriManager.UriSet
35 module SetSet = Set.Make (UriManagerSet)
37 type term_signature = (UriManager.uri * UriManager.uri list) option * UriManagerSet.t
39 type cardinality_condition =
44 type rating_criterion =
45 [ `Hits (** order by number of hits, most used objects first *)
49 (library_obj_tbl,library_rel_tbl,library_sort_tbl,library_count_tbl)
51 let current_tables () =
52 (obj_tbl (),rel_tbl (),sort_tbl (), count_tbl ())
54 let tbln n = "table" ^ string_of_int n
57 let add_depth_constr depth_opt cur_tbl where =
60 | Some depth -> (sprintf "%s.h_depth = %d" cur_tbl depth) :: where
63 let mk_positions positions cur_tbl =
68 let pos_str = MetadataPp.pp_position_tag pos in
73 | `MainConclusion None
74 | `MainHypothesis None ->
75 sprintf "%s.h_position = \"%s\"" cur_tbl pos_str
76 | `MainConclusion (Some r)
77 | `MainHypothesis (Some r) ->
78 let depth = MetadataPp.pp_relation r in
79 sprintf "(%s.h_position = \"%s\" and %s.h_depth %s)"
80 cur_tbl pos_str cur_tbl depth)
81 (positions :> MetadataTypes.position list)) ^
84 let explode_card_constr = function
85 | Eq card -> "=", card
86 | Gt card -> ">", card
87 | Lt card -> "<", card
89 let add_card_constr tbl col where = function
92 let op, card = explode_card_constr constr in
93 (* count(_utente).hypothesis = 3 *)
94 (sprintf "%s.%s %s %d" tbl col op card :: where)
96 let add_diff_constr tbl where = function
99 let op, card = explode_card_constr constr in
100 (sprintf "%s.hypothesis - %s.conclusion %s %d" tbl tbl op card :: where)
102 let add_all_constr ?(tbl=library_count_tbl) (n,from,where) concl full diff =
103 match (concl, full, diff) with
104 | None, None, None -> (n,from,where)
106 let cur_tbl = tbln n in
107 let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
108 let where = add_card_constr cur_tbl "conclusion" where concl in
109 let where = add_card_constr cur_tbl "statement" where full in
110 let where = add_diff_constr cur_tbl where diff in
113 sprintf "table0.source = %s.source" cur_tbl :: where
118 let add_constraint ?(start=0) ?(tables=default_tables) (n,from,where) metadata =
119 let obj_tbl,rel_tbl,sort_tbl,count_tbl = tables
121 let cur_tbl = tbln n in
122 let start_table = tbln start in
124 | `Obj (uri, positions) ->
125 let from = (sprintf "%s as %s" obj_tbl cur_tbl) :: from in
127 (sprintf "(%s.h_occurrence = \"%s\")" cur_tbl (UriManager.string_of_uri uri)) ::
128 mk_positions positions cur_tbl ::
130 else [sprintf "%s.source = %s.source" start_table cur_tbl]) @
135 let from = (sprintf "%s as %s" rel_tbl cur_tbl) :: from in
137 mk_positions positions cur_tbl ::
139 else [sprintf "%s.source = %s.source" start_table cur_tbl]) @
143 | `Sort (sort, positions) ->
144 let sort_str = CicPp.ppsort sort in
145 let from = (sprintf "%s as %s" sort_tbl cur_tbl) :: from in
147 (sprintf "%s.h_sort = \"%s\"" cur_tbl sort_str ) ::
148 mk_positions positions cur_tbl ::
152 [sprintf "%s.source = %s.source" start_table cur_tbl ]) @ where
156 let exec dbtype ~(dbd:HSql.dbd) ?rating (n,from,where) =
157 let from = String.concat ", " from in
158 let where = String.concat " and " where in
161 | None -> sprintf "select distinct table0.source from %s where %s" from where
164 ("select distinct table0.source from %s, hits where %s
165 and table0.source = hits.source order by hits.no desc")
168 (* prerr_endline query; *)
169 let result = HSql.exec dbtype dbd query in
172 match row.(0) with Some s -> UriManager.uri_of_string s
176 let at_least dbtype ~(dbd:HSql.dbd) ?concl_card ?full_card ?diff ?rating tables
177 (metadata: MetadataTypes.constr list)
179 let obj_tbl,rel_tbl,sort_tbl, count_tbl = tables in
180 if (metadata = []) && concl_card = None && full_card = None then
182 HLog.warn "MetadataConstraints.at_least: no constraints given";
187 List.fold_left (add_constraint ~tables) (0,[],[]) metadata
190 add_all_constr ~tbl:count_tbl (n,from,where) concl_card full_card diff
192 exec dbtype ~dbd ?rating (n,from,where)
196 ~(dbd:HSql.dbd) ?concl_card ?full_card ?diff ?rating
197 (metadata: MetadataTypes.constr list)
199 if are_tables_ownerized () then
201 HSql.Library ~dbd ?concl_card ?full_card ?diff ?rating
202 default_tables metadata
205 HSql.Legacy ~dbd ?concl_card ?full_card ?diff ?rating
206 default_tables metadata
209 HSql.User ~dbd ?concl_card ?full_card ?diff ?rating
210 (current_tables ()) metadata
214 HSql.Library ~dbd ?concl_card ?full_card ?diff ?rating
215 default_tables metadata
218 HSql.Legacy ~dbd ?concl_card ?full_card ?diff ?rating
219 default_tables metadata
222 (** Prefix handling *)
224 let filter_by_card n =
225 SetSet.filter (fun t -> (UriManagerSet.cardinal t) <= n)
228 let init = SetSet.union a b in
229 let merge_single_set s1 b =
231 (fun s2 res -> SetSet.add (UriManagerSet.union s1 s2) res)
234 SetSet.fold (fun s1 res -> SetSet.union (merge_single_set s1 b) res) a init
238 let rec inspect_children n childs =
240 (fun res term -> merge n (inspect_conclusion n term) res)
243 and add_root n root childs =
244 let childunion = inspect_children n childs in
245 let addroot = UriManagerSet.add root in
247 (fun child newsets -> SetSet.add (addroot child) newsets)
249 (SetSet.singleton (UriManagerSet.singleton root))
251 and inspect_conclusion n t =
252 if n = 0 then SetSet.empty
257 | Cic.Implicit _ -> SetSet.empty
258 | Cic.Var (u,exp_named_subst) -> SetSet.empty
259 | Cic.Const (u,exp_named_subst) ->
260 SetSet.singleton (UriManagerSet.singleton u)
261 | Cic.MutInd (u, t, exp_named_subst) ->
262 SetSet.singleton (UriManagerSet.singleton
263 (UriManager.uri_of_uriref u t None))
264 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
265 SetSet.singleton (UriManagerSet.singleton
266 (UriManager.uri_of_uriref u t (Some c)))
267 | Cic.Cast (t, _) -> inspect_conclusion n t
268 | Cic.Prod (_, s, t) ->
269 merge n (inspect_conclusion n s) (inspect_conclusion n t)
270 | Cic.Lambda (_, s, t) ->
271 merge n (inspect_conclusion n s) (inspect_conclusion n t)
272 | Cic.LetIn (_, s, t) ->
273 merge n (inspect_conclusion n s) (inspect_conclusion n t)
274 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
276 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
277 let uri = UriManager.uri_of_uriref u t None in
279 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
280 let suri = UriManager.uri_of_uriref u t (Some c) in
281 add_root (n-1) suri l
284 | Cic.MutCase (u, t, tt, uu, m) ->
288 | Cic.CoFix (_, m) ->
291 let rec inspect_term n t =
299 | Cic.Implicit _ -> None, SetSet.empty
300 | Cic.Var (u,exp_named_subst) -> None, SetSet.empty
301 | Cic.Const (u,exp_named_subst) ->
303 | Cic.MutInd (u, t, exp_named_subst) ->
304 let uri = UriManager.uri_of_uriref u t None in
305 Some uri, SetSet.empty
306 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
307 let uri = UriManager.uri_of_uriref u t (Some c) in
308 Some uri, SetSet.empty
309 | Cic.Cast (t, _) -> inspect_term n t
310 | Cic.Prod (_, _, t) -> inspect_term n t
311 | Cic.LetIn (_, _, t) -> inspect_term n t
312 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
313 let childunion = inspect_children (n-1) l in
315 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
316 let suri = UriManager.uri_of_uriref u t None in
317 if u = HelmLibraryObjects.Logic.eq_URI && n>1 then
318 (* equality is handled in a special way: in particular,
319 the type, if defined, is always added to the prefix,
320 and n is not decremented - it should have been n-2 *)
322 Cic.Const (u1,exp_named_subst1)::l1 ->
323 let inconcl = add_root (n-1) u1 l1 in
325 | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
326 let suri1 = UriManager.uri_of_uriref u1 t1 None in
327 let inconcl = add_root (n-1) suri1 l1 in
329 | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
330 let suri1 = UriManager.uri_of_uriref u1 t1 (Some c1) in
331 let inconcl = add_root (n-1) suri1 l1 in
333 | _ :: _ -> Some suri, SetSet.empty
334 | _ -> assert false (* args number must be > 0 *)
336 let childunion = inspect_children (n-1) l in
337 Some suri, childunion
338 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
339 let suri = UriManager.uri_of_uriref u t(Some c) in
340 let childunion = inspect_children (n-1) l in
341 Some suri, childunion
342 | _ -> None, SetSet.empty
344 let add_cardinality s =
345 let l = SetSet.elements s in
349 let el = UriManagerSet.elements set in
350 (List.length el, el)) l in
351 (* ordered by descending cardinality *)
352 List.sort (fun (n,_) (m,_) -> m - n) ((0,[])::res)
355 match inspect_term n t with
356 Some a, set -> Some a, add_cardinality set
357 | None, set when (SetSet.is_empty set) -> None, []
358 | _, _ -> assert false
361 let rec add children =
363 (fun acc t -> UriManagerSet.union (signature_concl t) acc)
364 (UriManagerSet.empty) children
366 (* this function creates the set of all different constants appearing in
367 the conclusion of the term *)
368 and signature_concl =
373 | Cic.Implicit _ -> UriManagerSet.empty
374 | Cic.Var (u,exp_named_subst) ->
375 (*CSC: TODO if the var has a body it must be processed *)
377 | Cic.Const (u,exp_named_subst) ->
378 UriManagerSet.singleton u
379 | Cic.MutInd (u, t, exp_named_subst) ->
380 let uri = UriManager.uri_of_uriref u t None in
381 UriManagerSet.singleton uri
382 | Cic.MutConstruct (u, t, c, exp_named_subst) ->
383 let uri = UriManager.uri_of_uriref u t (Some c) in
384 UriManagerSet.singleton uri
385 | Cic.Cast (t, _) -> signature_concl t
386 | Cic.Prod (_, s, t) ->
387 UriManagerSet.union (signature_concl s) (signature_concl t)
388 | Cic.Lambda (_, s, t) ->
389 UriManagerSet.union (signature_concl s) (signature_concl t)
390 | Cic.LetIn (_, s, t) ->
391 UriManagerSet.union (signature_concl s) (signature_concl t)
392 | Cic.Appl l -> add l
398 let rec signature_of = function
399 | Cic.Cast (t, _) -> signature_of t
400 | Cic.Prod (_, _, t) -> signature_of t
401 | Cic.LetIn (_, _, t) -> signature_of t
402 | Cic.Appl ((Cic.Const (u,exp_named_subst))::l) ->
404 | Cic.Appl ((Cic.MutInd (u, t, exp_named_subst))::l) ->
405 let suri = UriManager.uri_of_uriref u t None in
406 if LibraryObjects.is_eq_URI u then
407 (* equality is handled in a special way: in particular,
408 the type, if defined, is always added to the prefix,
409 and n is not decremented - it should have been n-2 *)
411 Cic.Const (u1,exp_named_subst1)::l1 ->
412 let inconcl = UriManagerSet.remove u1 (add l1) in
413 Some (suri, [u1]), inconcl
414 | Cic.MutInd (u1, t1, exp_named_subst1)::l1 ->
415 let suri1 = UriManager.uri_of_uriref u1 t1 None in
416 let inconcl = UriManagerSet.remove suri1 (add l1) in
417 Some (suri, [suri1]), inconcl
418 | Cic.MutConstruct (u1, t1, c1, exp_named_subst1)::l1 ->
419 let suri1 = UriManager.uri_of_uriref u1 t1 (Some c1) in
420 let inconcl = UriManagerSet.remove suri1 (add l1) in
421 Some (suri, [suri1]), inconcl
422 | _ :: tl -> Some (suri, []), add tl
423 | _ -> assert false (* args number must be > 0 *)
425 Some (suri, []), add l
426 | Cic.Appl ((Cic.MutConstruct (u, t, c, exp_named_subst))::l) ->
427 let suri = UriManager.uri_of_uriref u t (Some c) in
428 Some (suri, []), add l
429 | t -> None, signature_concl t
431 (* takes a list of lists and returns the list of all elements
432 without repetitions *)
434 let rec drop_repetitions = function
437 | u1::u2::l when u1 = u2 -> drop_repetitions (u2::l)
438 | u::l -> u::(drop_repetitions l) in
439 drop_repetitions (List.sort Pervasives.compare (List.concat l))
441 let must_of_prefix ?(where = `Conclusion) m s =
444 | `Conclusion -> [`InConclusion]
445 | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
448 if m = None then `MainConclusion None :: positions else positions in
449 let s' = List.map (fun (u:UriManager.uri) -> `Obj (u, positions)) s in
452 | Some m -> `Obj (m, [`MainConclusion None]) :: s'
454 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
456 let get_constants (dbd:HSql.dbd) ~where uri =
457 let uri = escape (UriManager.string_of_uri uri) in
460 | `Conclusion -> [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos ]
462 [ MetadataTypes.mainconcl_pos; MetadataTypes.inconcl_pos;
463 MetadataTypes.inhyp_pos; MetadataTypes.mainhyp_pos ]
467 (List.map (fun pos -> sprintf "(h_position = \"%s\")" pos) positions)
470 sprintf "SELECT h_occurrence FROM %s WHERE source=\"%s\" AND (%s)"
471 tbl uri pos_predicate
474 HSql.Library, MetadataTypes.library_obj_tbl;
475 HSql.Legacy, MetadataTypes.library_obj_tbl;
476 HSql.User, MetadataTypes.obj_tbl ()]
478 let set = ref UriManagerSet.empty in
480 (fun (dbtype, table) ->
481 let result = HSql.exec dbtype dbd (query table) in
486 set := UriManagerSet.add (UriManager.uri_of_string uri) !set
487 | _ -> assert false))
491 let at_most ~(dbd:HSql.dbd) ?(where = `Conclusion) only u =
492 let inconcl = get_constants dbd ~where u in
493 UriManagerSet.subset inconcl only
495 (* Special handling of equality. The problem is filtering out theorems just
496 * containing variables (e.g. all the theorems in cic:/Coq/Ring/). Really
497 * ad-hoc, no better solution found at the moment *)
498 let myspeciallist_of_facts =
499 [0,UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)"]
501 [0,UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
502 (* 0,"cic:/Coq/Init/Logic/sym_eq.con"; *)
503 0,UriManager.uri_of_string "cic:/Coq/Init/Logic/trans_eq.con";
504 0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal.con";
505 0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal2.con";
506 0,UriManager.uri_of_string "cic:/Coq/Init/Logic/f_equal3.con"]
509 let compute_exactly ~(dbd:HSql.dbd) ?(facts=false) ~where main prefixes =
519 (UriManager.uri_of_string (HelmLibraryObjects.Logic.eq_XURI))),
522 if m = 0 && is_eq then
523 (if facts then myspeciallist_of_facts
527 (* this gets rid of the ~750 objects of type Set/Prop/Type *)
530 let must = must_of_prefix ~where main s in
532 | `Conclusion -> at_least ~dbd ~concl_card:(Eq card) must
533 | `Statement -> at_least ~dbd ~full_card:(Eq card) must
535 List.map (fun uri -> (card, uri)) res)
538 (* critical value reached, fallback to "only" constraints *)
540 let compute_with_only ~(dbd:HSql.dbd) ?(facts=false) ?(where = `Conclusion)
541 main prefixes constants
543 let max_prefix_length =
546 | (max,_)::_ -> max in
547 let maximal_prefixes =
548 let rec filter res = function
550 | (n,s)::l when n = max_prefix_length -> filter ((n,s)::res) l
552 filter [] prefixes in
558 let card = if main = None then m else m + 1 in
559 let must = must_of_prefix ~where main s in
562 | `Conclusion -> at_least ~dbd ~concl_card:(Gt card) must
563 | `Statement -> at_least ~dbd ~full_card:(Gt card) must
565 (* we tag the uri with m+1, for sorting purposes *)
566 List.map (fun uri -> (card, uri)) res))
569 (* Printf.fprintf stderr "all: %d\n" (List.length all);flush_all (); *)
571 List.filter (function (_,uri) ->
572 at_most ~dbd ~where constants uri)
576 let equal_to = compute_exactly ~dbd ~facts ~where main prefixes in
577 greater_than @ equal_to
579 (* real match query implementation *)
581 let cmatch ~(dbd:HSql.dbd) ?(facts=false) t =
582 let (main, constants) = signature_of t in
585 | Some (main, types) ->
586 (* the type of eq is not counted in constants_no *)
587 let types_no = List.length types in
588 let constants_no = UriManagerSet.cardinal constants in
589 if (constants_no > critical_value) then
590 let prefixes = prefixes just_factor t in
592 | Some main, all_concl ->
594 List.fold_right UriManagerSet.add types (UriManagerSet.add main constants)
596 compute_with_only ~dbd ~facts (Some main) all_concl all_constants
599 (* in this case we compute all prefixes, and we do not need
600 to apply the only constraints *)
602 if constants_no = 0 then
603 (if types_no = 0 then
606 Some main, [0, []; types_no, types])
608 prefixes (constants_no+types_no+1) t
611 Some main, all_concl ->
612 compute_exactly ~dbd ~facts ~where:`Conclusion (Some main) all_concl
615 let power_upto upto consts =
616 let l = UriManagerSet.elements consts in
617 List.sort (fun (n,_) (m,_) -> m - n)
621 List.filter (function (n,l) -> n <= upto)
622 (List.map (function (n,l) -> (n+1,a::l)) res) in
627 let l = UriManagerSet.elements consts in
628 List.sort (fun (n,_) (m,_) -> m - n)
630 (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res))
633 type where = [ `Conclusion | `Statement ]
635 let sigmatch ~(dbd:HSql.dbd) ?(facts=false) ?(where = `Conclusion)
641 | Some (main, types) -> Some main,types
643 let constants_no = UriManagerSet.cardinal constants in
644 (* prerr_endline (("constants_no: ")^(string_of_int constants_no)); *)
645 if (constants_no > critical_value) then
647 let subsets = power_upto just_factor constants in
648 (* let _ = prerr_endline (("subsets: ")^
649 (string_of_int (List.length subsets))) in *)
650 let types_no = List.length types in
652 List.map (function (n,l) -> (n+types_no,types@l)) subsets
655 prerr_endline ("critical_value exceded..." ^ string_of_int constants_no);
657 let all = match main with None -> types | Some m -> m::types in
658 List.fold_right UriManagerSet.add all constants
660 compute_with_only ~dbd ~where main subsets all_constants
662 (prerr_endline ("all subsets..." ^ string_of_int constants_no);
664 let subsets = power constants in
665 let types_no = List.length types in
667 (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
670 prerr_endline "fine1";
671 compute_exactly ~dbd ~facts ~where main subsets)
673 (* match query wrappers *)
677 let cmatch ~dbd ?(facts=false) term =
680 (fun x y -> Pervasives.compare (fst y) (fst x))
681 (cmatch' ~dbd ~facts term))
683 let constants_of = signature_concl