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
34 let debug_print s = if debug then prerr_endline (Lazy.force s)
36 (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
37 * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
38 let sqlpat_of_shellglob =
39 let star_RE, qmark_RE, percent_RE, uscore_RE =
40 Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
43 Pcre.replace ~rex:star_RE ~templ:"%"
44 (Pcre.replace ~rex:qmark_RE ~templ:"_"
45 (Pcre.replace ~rex:percent_RE ~templ:"\\%"
46 (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
49 let nonvar uri = not (UriManager.uri_is_var uri)
51 let locate ~(dbd:HMysql.dbd) ?(vars = false) pat =
52 let sql_pat = sqlpat_of_shellglob pat in
54 sprintf ("SELECT source FROM %s WHERE value LIKE \"%s\" UNION "^^
55 "SELECT source FROM %s WHERE value LIKE \"%s\"")
56 (MetadataTypes.name_tbl ()) sql_pat
57 MetadataTypes.library_name_tbl sql_pat
59 let result = HMysql.exec dbd query in
62 (fun cols -> match cols.(0) with Some s -> UriManager.uri_of_string s | _ -> assert false))
64 let match_term ~(dbd:HMysql.dbd) ty =
65 (* debug_print (lazy (CicPp.ppterm ty)); *)
66 let metadata = MetadataExtractor.compute ~body:None ~ty in
68 MetadataConstraints.UriManagerSet.cardinal (MetadataConstraints.constants_of ty)
71 if CicUtil.is_meta_closed ty then
72 Some (MetadataConstraints.Eq constants_no), None
75 let (hyp_constants, concl_constants) =
76 (* collect different constants in hypotheses and conclusions *)
78 (fun ((hyp, concl) as acc) metadata ->
79 match (metadata: MetadataTypes.metadata) with
80 | `Sort _ | `Rel _ -> acc
81 | `Obj (uri, `InConclusion) | `Obj (uri, `MainConclusion _)
82 when not (List.mem uri concl) -> (hyp, uri :: concl)
83 | `Obj (uri, `InHypothesis) | `Obj (uri, `MainHypothesis _)
84 when not (List.mem uri hyp) -> (uri :: hyp, concl)
89 List.length hyp_constants - List.length concl_constants
91 let (concl_metas, hyp_metas) = MetadataExtractor.compute_metas ty in
93 if MetadataExtractor.IntSet.equal concl_metas hyp_metas then
94 Some (MetadataConstraints.Eq diff_no)
95 else if MetadataExtractor.IntSet.subset concl_metas hyp_metas then
96 Some (MetadataConstraints.Gt (diff_no - 1))
97 else if MetadataExtractor.IntSet.subset hyp_metas concl_metas then
98 Some (MetadataConstraints.Lt (diff_no + 1))
104 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
105 Constr.at_least ~dbd ?full_card ?diff constraints
107 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
109 let signature_of_hypothesis context =
114 | Some (_, Cic.Decl t)
115 | Some (_, Cic.Def (t, _)) ->
116 Constr.UriManagerSet.union set (Constr.constants_of t))
117 Constr.UriManagerSet.empty context
119 let intersect uris siguris =
120 let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
122 List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
124 let inter = Constr.UriManagerSet.inter set1 set2 in
125 List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
129 let profiler = CicUtil.profile "at_most" in
130 fun ~dbd ~where uri -> profiler.profile (Constr.at_most ~dbd ~where) uri
133 let profiler = CicUtil.profile "sigmatch" in
134 fun ~dbd ~facts ~where signature ->
135 profiler.profile (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
136 *) let at_most = Constr.at_most let sigmatch = MetadataConstraints.sigmatch
138 let filter_uris_forward ~dbd (main, constants) uris =
142 | Some (main, types) -> main :: types
145 List.fold_right Constr.UriManagerSet.add main_uris constants
147 List.filter (at_most ~dbd ~where:`Statement full_signature) uris
149 let filter_uris_backward ~dbd ~facts signature uris =
152 (sigmatch ~dbd ~facts ~where:`Statement signature)
154 intersect uris siguris
156 let compare_goal_list proof goal1 goal2 =
157 let _,metasenv,_,_ = proof in
158 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
159 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
161 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
164 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
168 CicReduction.are_convertible
169 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
176 CicReduction.are_convertible
177 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
184 (* experimental_hint is a version of hint for experimental
185 purposes. It uses auto_tac_verbose instead of auto tac.
186 Auto_tac verbose also returns a substitution - for the moment
187 as a function from cic to cic, to be changed into an association
188 list in the future -. This substitution is used to build a
189 hash table of the inspected goals with their associated proofs.
190 The cose is a cut and paste of the previous one: at the end
191 of the experimentation we shall make a choice. *)
193 let close_with_types s metasenv context =
194 Constr.UriManagerSet.fold
196 let t = CicUtil.term_of_uri e in
198 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
200 Constr.UriManagerSet.union bag (Constr.constants_of ty))
203 let close_with_constructors s metasenv context =
204 Constr.UriManagerSet.fold
206 let t = CicUtil.term_of_uri e in
209 | Cic.MutConstruct (uri,_,_,_) ->
210 (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
211 Cic.InductiveDefinition(tl,_,_,_) ->
214 (fun (i,s) (_,_,_,cl) ->
218 let curi = UriManager.uri_of_uriref uri i (Some j) in
219 j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
226 let apply_tac_verbose =
227 let profiler = CicUtil.profile "apply_tac_verbose" in
228 fun ~term status -> profiler.profile (PrimitiveTactics.apply_tac_verbose ~term) status
231 let profiler = CicUtil.profile "sigmatch" in
232 fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler.profile (Constr.sigmatch ~dbd ~facts ~where) signature
235 let profiler = CicUtil.profile "cmatch'" in
236 fun ~dbd ~facts signature -> profiler.profile (Constr.cmatch' ~dbd ~facts) signature
237 *) let apply_tac_verbose = PrimitiveTactics.apply_tac_verbose let cmatch' = Constr.cmatch'
239 let signature_of_goal ~(dbd:HMysql.dbd) ((proof, goal) as status) =
240 let (_, metasenv, _, _) = proof in
241 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
242 let main, sig_constants = Constr.signature_of ty in
243 let set = signature_of_hypothesis context in
248 List.fold_right Constr.UriManagerSet.add (main::l) set in
249 let set = Constr.UriManagerSet.union set sig_constants in
250 let all_constants_closed = close_with_types set metasenv context in
252 sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
253 let uris = List.filter nonvar (List.map snd uris) in
254 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
257 let equations_for_goal ~(dbd:HMysql.dbd) ((proof, goal) as status) =
261 (Constr.UriManagerSet.fold
262 (fun u l -> (UriManager.string_of_uri u)::l) set []))
265 let (_, metasenv, _, _) = proof in
266 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
267 let main, sig_constants = Constr.signature_of ty in
268 (* Printf.printf "\nsig_constants: %s\n\n" (to_string sig_constants); *)
269 (* match main with *)
270 (* None -> raise Goal_is_not_an_equation *)
271 (* | Some (m,l) -> *)
273 let eq_URI = UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI in
276 | Some (m, l) when UriManager.eq m eq_URI -> m, l
277 | Some (m, l) -> eq_URI, []
279 Printf.printf "\nSome (m, l): %s, [%s]\n\n"
280 (UriManager.string_of_uri m)
281 (String.concat "; " (List.map UriManager.string_of_uri l));
282 (* if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then ( *)
283 let set = signature_of_hypothesis context in
284 (* Printf.printf "\nsignature_of_hypothesis: %s\n\n" (to_string set); *)
285 let set = Constr.UriManagerSet.union set sig_constants in
286 let set = close_with_types set metasenv context in
287 (* Printf.printf "\ndopo close_with_types: %s\n\n" (to_string set); *)
288 let set = close_with_constructors set metasenv context in
289 (* Printf.printf "\ndopo close_with_constructors: %s\n\n" (to_string set); *)
290 let set = List.fold_right Constr.UriManagerSet.remove (m::l) set in
292 sigmatch ~dbd ~facts:false ~where:`Statement (main,set) in
293 let uris = List.filter nonvar (List.map snd uris) in
294 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
297 (* else raise Goal_is_not_an_equation *)
299 let experimental_hint
300 ~(dbd:HMysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
301 let (_, metasenv, _, _) = proof in
302 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
303 let (uris, (main, sig_constants)) =
306 (sigmatch ~dbd ~facts signature, signature)
308 (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
310 let uris = List.filter nonvar (List.map snd uris) in
311 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
312 let types_constants =
314 | None -> Constr.UriManagerSet.empty
315 | Some (main, types) ->
316 List.fold_right Constr.UriManagerSet.add (main :: types)
317 Constr.UriManagerSet.empty
321 Constr.UriManagerSet.union
322 (signature_of_hypothesis context)
327 | None -> Constr.UriManagerSet.empty
330 CicTypeChecker.type_of_aux'
331 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
333 Constr.constants_of ty
335 Constr.UriManagerSet.union main hyp_and_sug
337 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
338 let all_constants_closed = close_with_types all_constants metasenv context in
339 let other_constants =
340 Constr.UriManagerSet.diff all_constants_closed types_constants
342 debug_print (lazy "all_constants_closed");
343 if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) all_constants_closed;
344 debug_print (lazy "other_constants");
345 if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) other_constants;
347 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
348 if ((List.length uris < pow) or (pow <= 0))
350 debug_print (lazy "MetadataQuery: large sig, falling back to old method");
351 filter_uris_forward ~dbd (main, other_constants) uris
353 filter_uris_backward ~dbd ~facts (main, other_constants) uris
355 let rec aux = function
360 let (subst,(proof, goal_list)) =
361 (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
363 ~term:(CicUtil.term_of_uri uri)
367 List.stable_sort (compare_goal_list proof) goal_list
369 Some (uri, (subst,(proof, goal_list)))
370 with ProofEngineTypes.Fail _ -> None
374 | Some status' -> status' :: aux tl)
377 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
378 Pervasives.compare (List.length goals1) (List.length goals2))
381 let new_experimental_hint
382 ~(dbd:HMysql.dbd) ?(facts=false) ?signature ~universe
383 ((proof, goal) as status)
385 let (_, metasenv, _, _) = proof in
386 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
387 let (uris, (main, sig_constants)) =
390 (sigmatch ~dbd ~facts signature, signature)
392 (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in
395 (fun res u -> Constr.UriManagerSet.add u res)
396 Constr.UriManagerSet.empty universe in
399 (fun res (_,u) -> Constr.UriManagerSet.add u res)
400 Constr.UriManagerSet.empty uris in
401 let uris = Constr.UriManagerSet.inter uris universe in
402 let uris = Constr.UriManagerSet.elements uris in
403 let rec aux = function
408 let (subst,(proof, goal_list)) =
409 (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
411 ~term:(CicUtil.term_of_uri uri)
415 List.stable_sort (compare_goal_list proof) goal_list
417 Some (uri, (subst,(proof, goal_list)))
418 with ProofEngineTypes.Fail _ -> None
422 | Some status' -> status' :: aux tl)
425 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
426 Pervasives.compare (List.length goals1) (List.length goals2))
431 [`Rel [`MainConclusion None];
432 `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
433 `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
434 `Obj (uri,[`InHypothesis]);
437 MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
440 let fill_with_dummy_constants t =
441 let rec aux i types =
443 Cic.Lambda (n,s,t) ->
445 UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)^".con") in
446 (aux (i+1) (s::types)
447 (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
450 let t,types = aux 0 [] t in
453 let instance ~dbd t =
454 let t',types = fill_with_dummy_constants t in
455 let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
459 (lazy (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x))))
461 let no_concl = MetadataDb.count_distinct `Conclusion metadata in
462 let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
463 let no_full = MetadataDb.count_distinct `Statement metadata in
464 let is_dummy = function
465 | `Obj(s, _) -> (String.sub (UriManager.string_of_uri s) 0 10) <> "cic:/dummy"
468 let rec look_for_dummy_main = function
470 | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_
471 when (String.sub (UriManager.string_of_uri s) 0 10 = "cic:/dummy") ->
472 let s = UriManager.string_of_uri s in
473 let len = String.length s in
474 let dummy_index = int_of_string (String.sub s 11 (len-15)) in
475 let dummy_type = List.nth types dummy_index in
477 | _::l -> look_for_dummy_main l
479 match (look_for_dummy_main metadata) with
481 (* debug_print (lazy "Caso None"); *)
482 (* no dummy in main position *)
483 let metadata = List.filter is_dummy metadata in
484 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
485 let concl_card = Some (MetadataConstraints.Eq no_concl) in
486 let full_card = Some (MetadataConstraints.Eq no_full) in
487 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
488 Constr.at_least ~dbd ?concl_card ?full_card ?diff constraints
489 | Some (depth, dummy_type) ->
491 (lazy (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type))); *)
492 (* a dummy in main position *)
493 let metadata_for_dummy_type =
494 MetadataExtractor.compute ~body:None ~ty:dummy_type in
495 (* Let us skip this for the moment
496 let main_of_dummy_type =
497 look_for_dummy_main metadata_for_dummy_type in *)
498 let metadata = List.filter is_dummy metadata in
499 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
500 let metadata_for_dummy_type =
501 List.filter is_dummy metadata_for_dummy_type in
502 let metadata_for_dummy_type, depth' =
503 (* depth' = the depth of the A -> A -> Prop *)
504 List.fold_left (fun (acc,dep) c ->
506 | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
507 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
508 | `Obj (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
509 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
510 | `Rel (`MainConclusion (Some (MetadataTypes.Eq i))) ->
511 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
512 | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
514 let constraints_for_dummy_type =
515 List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
516 (* start with the dummy constant in main conlusion *)
517 let from = ["refObj as table0"] in
519 [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
520 sprintf "table0.h_depth >= %d" depth] in
523 (MetadataConstraints.add_constraint ~start:2)
524 (2,from,where) constraints in
525 let concl_card = Some (MetadataConstraints.Eq no_concl) in
526 let full_card = Some (MetadataConstraints.Eq no_full) in
527 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
529 MetadataConstraints.add_all_constr
530 (n,from,where) concl_card full_card diff in
531 (* join with the constraints over the type of the constant *)
533 (sprintf "table0.h_occurrence = table%d.source" n)::where in
535 sprintf "table0.h_depth - table%d.h_depth = %d"
536 n (depth - depth')::where
540 (MetadataConstraints.add_constraint ~start:n)
541 (n,from,where) constraints_for_dummy_type in
542 Constr.exec ~dbd (m,from,where)
544 (* fwd_simpl ****************************************************************)
546 let rec map_filter f n = function
550 | None -> map_filter f (succ n) tl
551 | Some hd -> hd :: map_filter f (succ n) tl
555 | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
556 | hd -> Some (CicUtil.uri_of_term hd, [])
559 | Invalid_argument "uri_of_term" -> None
565 | Some (uri, _) -> Some (n, uri)
569 | Some (uri, args) -> Some (uri, map_filter f 1 args)
571 let debug_metadata = function
573 | Some (outer, inners) ->
574 let f (n, uri) = Printf.eprintf "%s: %i %s\n" "fwd" n (UriManager.string_of_uri uri) in
575 Printf.eprintf "\n%s: %s\n" "fwd" (UriManager.string_of_uri outer);
576 List.iter f inners; prerr_newline ()
578 let fwd_simpl ~dbd t =
580 match row.(0), row.(1), row.(2) with
581 | Some source, Some inner, Some index ->
584 (int_of_string index, (UriManager.uri_of_string inner)) inners
587 let rec rank ranks (source, ok) =
589 | [], false -> [source, 0]
590 | [], true -> [source, 1]
591 | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
592 | (uri, 0) :: tl, true when uri = source -> (uri, 0) :: tl
593 | (uri, i) :: tl, true when uri = source -> (uri, succ i) :: tl
594 | hd :: tl, _ -> hd :: rank tl (source, ok)
596 let compare (_, x) (_, y) = compare x y in
597 let filter n (uri, rank) =
598 if rank > 0 then Some (UriManager.uri_of_string uri) else None
600 let metadata = get_metadata t in debug_metadata metadata;
603 | Some (outer, inners) ->
604 let select = "source, h_inner, h_index" in
605 let from = "genLemma" in
607 Printf.sprintf "h_outer = \"%s\""
608 (HMysql.escape (UriManager.string_of_uri outer)) in
609 let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
610 let result = HMysql.exec dbd query in
611 let lemmas = HMysql.map ~f:(map inners) result in
612 let ranked = List.fold_left rank [] lemmas in
613 let ordered = List.rev (List.fast_sort compare ranked) in
614 map_filter filter 0 ordered
616 (* get_decomposables ********************************************************)
618 let decomposables ~dbd =
619 let map row = match row.(0) with
622 match CicUtil.term_of_uri (UriManager.uri_of_string str) with
623 | Cic.MutInd (uri, typeno, _) -> Some (uri, typeno)
625 raise (UriManager.IllFormedUri str)
627 let select, from = "source", "decomposables" in
628 let query = Printf.sprintf "SELECT %s FROM %s" select from in
629 let decomposables = HMysql.map ~f:map (HMysql.exec dbd query) in
630 map_filter (fun _ x -> x) 0 decomposables