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 module Constr = MetadataConstraints
29 module PET = ProofEngineTypes
31 exception Goal_is_not_an_equation
33 let debug_print = fun _ -> ()
35 (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
36 * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
37 let sqlpat_of_shellglob =
38 let star_RE, qmark_RE, percent_RE, uscore_RE =
39 Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
42 Pcre.replace ~rex:star_RE ~templ:"%"
43 (Pcre.replace ~rex:qmark_RE ~templ:"_"
44 (Pcre.replace ~rex:percent_RE ~templ:"\\%"
45 (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
48 let nonvar uri = not (UriManager.uri_is_var uri)
50 let locate ~(dbd:Mysql.dbd) ?(vars = false) pat =
51 let sql_pat = sqlpat_of_shellglob pat in
53 sprintf ("SELECT source FROM %s WHERE value LIKE \"%s\" UNION "^^
54 "SELECT source FROM %s WHERE value LIKE \"%s\"")
55 (MetadataTypes.name_tbl ()) sql_pat
56 MetadataTypes.library_name_tbl sql_pat
58 let result = Mysql.exec dbd query in
61 (fun cols -> match cols.(0) with Some s -> UriManager.uri_of_string s | _ -> assert false))
63 let match_term ~(dbd:Mysql.dbd) ty =
64 (* debug_print (CicPp.ppterm ty); *)
65 let metadata = MetadataExtractor.compute ~body:None ~ty in
67 MetadataConstraints.UriManagerSet.cardinal (MetadataConstraints.constants_of ty)
70 if CicUtil.is_meta_closed ty then
71 Some (MetadataConstraints.Eq constants_no), None
74 let (hyp_constants, concl_constants) =
75 (* collect different constants in hypotheses and conclusions *)
77 (fun ((hyp, concl) as acc) metadata ->
78 match (metadata: MetadataTypes.metadata) with
79 | `Sort _ | `Rel _ -> acc
80 | `Obj (uri, `InConclusion) | `Obj (uri, `MainConclusion _)
81 when not (List.mem uri concl) -> (hyp, uri :: concl)
82 | `Obj (uri, `InHypothesis) | `Obj (uri, `MainHypothesis _)
83 when not (List.mem uri hyp) -> (uri :: hyp, concl)
88 List.length hyp_constants - List.length concl_constants
90 let (concl_metas, hyp_metas) = MetadataExtractor.compute_metas ty in
92 if MetadataExtractor.IntSet.equal concl_metas hyp_metas then
93 Some (MetadataConstraints.Eq diff_no)
94 else if MetadataExtractor.IntSet.subset concl_metas hyp_metas then
95 Some (MetadataConstraints.Gt (diff_no - 1))
96 else if MetadataExtractor.IntSet.subset hyp_metas concl_metas then
97 Some (MetadataConstraints.Lt (diff_no + 1))
103 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
104 Constr.at_least ~dbd ?full_card ?diff constraints
106 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
108 let signature_of_hypothesis context =
113 | Some (_, Cic.Decl t)
114 | Some (_, Cic.Def (t, _)) ->
115 Constr.UriManagerSet.union set (Constr.constants_of t))
116 Constr.UriManagerSet.empty context
118 let intersect uris siguris =
119 let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
121 List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
123 let inter = Constr.UriManagerSet.inter set1 set2 in
124 List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
127 let profiler = CicUtil.profile "at_most" in
128 fun ~dbd ~where uri -> profiler (Constr.at_most ~dbd ~where) uri
131 let profiler = CicUtil.profile "sigmatch" in
132 fun ~dbd ~facts ~where signature ->
133 profiler (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
135 let filter_uris_forward ~dbd (main, constants) uris =
139 | Some (main, types) -> main :: types
142 List.fold_right Constr.UriManagerSet.add main_uris constants
144 List.filter (at_most ~dbd ~where:`Statement full_signature) uris
146 let filter_uris_backward ~dbd ~facts signature uris =
149 (sigmatch ~dbd ~facts ~where:`Statement signature)
151 intersect uris siguris
153 let compare_goal_list proof goal1 goal2 =
154 let _,metasenv,_,_ = proof in
155 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
156 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
158 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
161 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
165 CicReduction.are_convertible
166 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
173 CicReduction.are_convertible
174 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
181 (* experimental_hint is a version of hint for experimental
182 purposes. It uses auto_tac_verbose instead of auto tac.
183 Auto_tac verbose also returns a substitution - for the moment
184 as a function from cic to cic, to be changed into an association
185 list in the future -. This substitution is used to build a
186 hash table of the inspected goals with their associated proofs.
187 The cose is a cut and paste of the previous one: at the end
188 of the experimentation we shall make a choice. *)
190 let close_with_types s metasenv context =
191 Constr.UriManagerSet.fold
193 let t = CicUtil.term_of_uri e in
195 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
197 Constr.UriManagerSet.union bag (Constr.constants_of ty))
200 let close_with_constructors s metasenv context =
201 Constr.UriManagerSet.fold
203 let t = CicUtil.term_of_uri e in
206 | Cic.MutConstruct (uri,_,_,_) ->
207 (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
208 Cic.InductiveDefinition(tl,_,_,_) ->
211 (fun (i,s) (_,_,_,cl) ->
215 let curi = UriManager.uri_of_uriref uri i (Some j) in
216 j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
222 let apply_tac_verbose =
223 let profiler = CicUtil.profile "apply_tac_verbose" in
224 fun ~term status -> profiler (PrimitiveTactics.apply_tac_verbose ~term) status
227 let profiler = CicUtil.profile "sigmatch" in
228 fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler (Constr.sigmatch ~dbd ~facts ~where) signature
231 let profiler = CicUtil.profile "cmatch'" in
232 fun ~dbd ~facts signature -> profiler (Constr.cmatch' ~dbd ~facts) signature
234 let signature_of_goal ~(dbd:Mysql.dbd) ((proof, goal) as status) =
235 let (_, metasenv, _, _) = proof in
236 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
237 let main, sig_constants = Constr.signature_of ty in
238 let set = signature_of_hypothesis context in
243 List.fold_right Constr.UriManagerSet.add (main::l) set in
244 let set = Constr.UriManagerSet.union set sig_constants in
245 let all_constants_closed = close_with_types set metasenv context in
247 sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
248 let uris = List.filter nonvar (List.map snd uris) in
249 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
252 let equations_for_goal ~(dbd:Mysql.dbd) ((proof, goal) as status) =
253 let (_, metasenv, _, _) = proof in
254 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
255 let main, sig_constants = Constr.signature_of ty in
257 None -> raise Goal_is_not_an_equation
259 if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then
260 let set = signature_of_hypothesis context in
261 let set = Constr.UriManagerSet.union set sig_constants in
262 let set = close_with_types set metasenv context in
263 let set = close_with_constructors set metasenv context in
264 let set = List.fold_right Constr.UriManagerSet.remove (m::l) set in
266 sigmatch ~dbd ~facts:false ~where:`Statement (main,set) in
267 let uris = List.filter nonvar (List.map snd uris) in
268 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
270 else raise Goal_is_not_an_equation
272 let experimental_hint
273 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
274 let (_, metasenv, _, _) = proof in
275 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
276 let (uris, (main, sig_constants)) =
279 (sigmatch ~dbd ~facts signature, signature)
281 (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
283 let uris = List.filter nonvar (List.map snd uris) in
284 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
285 let types_constants =
287 | None -> Constr.UriManagerSet.empty
288 | Some (main, types) ->
289 List.fold_right Constr.UriManagerSet.add (main :: types)
290 Constr.UriManagerSet.empty
294 Constr.UriManagerSet.union
295 (signature_of_hypothesis context)
300 | None -> Constr.UriManagerSet.empty
303 CicTypeChecker.type_of_aux'
304 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
306 Constr.constants_of ty
308 Constr.UriManagerSet.union main hyp_and_sug
310 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
311 let all_constants_closed = close_with_types all_constants metasenv context in
312 let other_constants =
313 Constr.UriManagerSet.diff all_constants_closed types_constants
315 debug_print "all_constants_closed";
316 Constr.UriManagerSet.iter debug_print all_constants_closed;
317 debug_print "other_constants";
318 Constr.UriManagerSet.iter debug_print other_constants;
320 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
321 if ((List.length uris < pow) or (pow <= 0))
323 debug_print "MetadataQuery: large sig, falling back to old method";
324 filter_uris_forward ~dbd (main, other_constants) uris
326 filter_uris_backward ~dbd ~facts (main, other_constants) uris
328 let rec aux = function
333 let (subst,(proof, goal_list)) =
334 (* debug_print ("STO APPLICANDO" ^ uri); *)
336 ~term:(CicUtil.term_of_uri uri)
340 List.stable_sort (compare_goal_list proof) goal_list
342 Some (uri, (subst,(proof, goal_list)))
343 with ProofEngineTypes.Fail _ -> None
347 | Some status' -> status' :: aux tl)
350 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
351 Pervasives.compare (List.length goals1) (List.length goals2))
354 let new_experimental_hint
355 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ~universe
356 ((proof, goal) as status)
358 let (_, metasenv, _, _) = proof in
359 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
360 let (uris, (main, sig_constants)) =
363 (sigmatch ~dbd ~facts signature, signature)
365 (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in
368 (fun res u -> Constr.UriManagerSet.add u res)
369 Constr.UriManagerSet.empty universe in
372 (fun res (_,u) -> Constr.UriManagerSet.add u res)
373 Constr.UriManagerSet.empty uris in
374 let uris = Constr.UriManagerSet.inter uris universe in
375 let uris = Constr.UriManagerSet.elements uris in
376 let rec aux = function
381 let (subst,(proof, goal_list)) =
382 (* debug_print ("STO APPLICANDO" ^ uri); *)
384 ~term:(CicUtil.term_of_uri uri)
388 List.stable_sort (compare_goal_list proof) goal_list
390 Some (uri, (subst,(proof, goal_list)))
391 with ProofEngineTypes.Fail _ -> None
395 | Some status' -> status' :: aux tl)
398 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
399 Pervasives.compare (List.length goals1) (List.length goals2))
404 [`Rel [`MainConclusion None];
405 `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
406 `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
407 `Obj (uri,[`InHypothesis]);
410 MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
413 let fill_with_dummy_constants t =
414 let rec aux i types =
416 Cic.Lambda (n,s,t) ->
418 UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)) in
419 (aux (i+1) (s::types)
420 (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
423 let t,types = aux 0 [] t in
426 let instance ~dbd t =
427 let t',types = fill_with_dummy_constants t in
428 let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
432 (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x)))
434 let no_concl = MetadataDb.count_distinct `Conclusion metadata in
435 let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
436 let no_full = MetadataDb.count_distinct `Statement metadata in
437 let is_dummy = function
438 | `Obj(s, _) -> (String.sub (UriManager.string_of_uri s) 0 10) <> "cic:/dummy"
441 let rec look_for_dummy_main = function
443 | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_
444 when (String.sub (UriManager.string_of_uri s) 0 10 = "cic:/dummy") ->
445 let s = UriManager.string_of_uri s in
446 let len = String.length s in
447 let dummy_index = int_of_string (String.sub s 11 (len-11)) in
448 let dummy_type = List.nth types dummy_index in
450 | _::l -> look_for_dummy_main l
452 match (look_for_dummy_main metadata) with
454 (* debug_print "Caso None"; *)
455 (* no dummy in main position *)
456 let metadata = List.filter is_dummy metadata in
457 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
458 let concl_card = Some (MetadataConstraints.Eq no_concl) in
459 let full_card = Some (MetadataConstraints.Eq no_full) in
460 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
461 Constr.at_least ~dbd ?concl_card ?full_card ?diff constraints
462 | Some (depth, dummy_type) ->
464 (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type)); *)
465 (* a dummy in main position *)
466 let metadata_for_dummy_type =
467 MetadataExtractor.compute ~body:None ~ty:dummy_type in
468 (* Let us skip this for the moment
469 let main_of_dummy_type =
470 look_for_dummy_main metadata_for_dummy_type in *)
471 let metadata = List.filter is_dummy metadata in
472 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
473 let metadata_for_dummy_type =
474 List.filter is_dummy metadata_for_dummy_type in
475 let metadata_for_dummy_type, depth' =
476 (* depth' = the depth of the A -> A -> Prop *)
477 List.fold_left (fun (acc,dep) c ->
479 | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
480 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
481 | `Obj (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
482 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
483 | `Rel (`MainConclusion (Some (MetadataTypes.Eq i))) ->
484 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
485 | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
487 let constraints_for_dummy_type =
488 List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
489 (* start with the dummy constant in main conlusion *)
490 let from = ["refObj as table0"] in
492 [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
493 sprintf "table0.h_depth >= %d" depth] in
496 (MetadataConstraints.add_constraint ~start:2)
497 (2,from,where) constraints in
498 let concl_card = Some (MetadataConstraints.Eq no_concl) in
499 let full_card = Some (MetadataConstraints.Eq no_full) in
500 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
502 MetadataConstraints.add_all_constr
503 (n,from,where) concl_card full_card diff in
504 (* join with the constraints over the type of the constant *)
506 (sprintf "table0.h_occurrence = table%d.source" n)::where in
508 sprintf "table0.h_depth - table%d.h_depth = %d"
509 n (depth - depth')::where
513 (MetadataConstraints.add_constraint ~start:n)
514 (n,from,where) constraints_for_dummy_type in
515 Constr.exec ~dbd (m,from,where)
517 (* fwd_simpl ****************************************************************)
519 let rec map_filter f n = function
523 | None -> map_filter f (succ n) tl
524 | Some hd -> hd :: map_filter f (succ n) tl
528 | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
529 | hd -> Some (CicUtil.uri_of_term hd, [])
532 | Invalid_argument "uri_of_term" -> None
538 | Some (uri, _) -> Some (n, uri)
542 | Some (uri, args) -> Some (uri, map_filter f 1 args)
544 let debug_metadata = function
546 | Some (outer, inners) ->
547 let f (n, uri) = Printf.eprintf "%s: %i %s\n" "fwd" n (UriManager.string_of_uri uri) in
548 Printf.eprintf "\n%s: %s\n" "fwd" (UriManager.string_of_uri outer);
549 List.iter f inners; prerr_newline ()
551 let fwd_simpl ~dbd t =
553 match row.(0), row.(1), row.(2) with
554 | Some source, Some inner, Some index ->
557 (int_of_string index, (UriManager.uri_of_string inner)) inners
560 let rec rank ranks (source, ok) =
562 | [], false -> [source, 0]
563 | [], true -> [source, 1]
564 | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
565 | (uri, 0) :: tl, true when uri = source -> (uri, 0) :: tl
566 | (uri, i) :: tl, true when uri = source -> (uri, succ i) :: tl
567 | hd :: tl, _ -> hd :: rank tl (source, ok)
569 let compare (_, x) (_, y) = compare x y in
570 let filter n (uri, rank) =
571 if rank > 0 then Some (UriManager.uri_of_string uri) else None
573 let metadata = get_metadata t in debug_metadata metadata;
576 | Some (outer, inners) ->
577 let select = "source, h_inner, h_index" in
578 let from = "genLemma" in
580 Printf.sprintf "h_outer = \"%s\""
581 (Mysql.escape (UriManager.string_of_uri outer)) in
582 let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
583 let result = Mysql.exec dbd query in
584 let lemmas = Mysql.map ~f:(map inners) result in
585 let ranked = List.fold_left rank [] lemmas in
586 let ordered = List.rev (List.fast_sort compare ranked) in
587 map_filter filter 0 ordered
589 (* get_decomposables ********************************************************)
591 let decomposables ~dbd =
592 let map row = match row.(0) with
595 match CicUtil.term_of_uri (UriManager.uri_of_string str) with
596 | Cic.MutInd (uri, typeno, _) -> Some (uri, typeno)
598 raise (UriManager.IllFormedUri str)
600 let select, from = "source", "decomposables" in
601 let query = Printf.sprintf "SELECT %s FROM %s" select from in
602 let decomposables = Mysql.map ~f:map (Mysql.exec dbd query) in
603 map_filter (fun _ x -> x) 0 decomposables