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
128 let profiler = CicUtil.profile "at_most" in
129 fun ~dbd ~where uri -> profiler.profile (Constr.at_most ~dbd ~where) uri
132 let profiler = CicUtil.profile "sigmatch" in
133 fun ~dbd ~facts ~where signature ->
134 profiler.profile (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
135 *) let at_most = Constr.at_most let sigmatch = MetadataConstraints.sigmatch
137 let filter_uris_forward ~dbd (main, constants) uris =
141 | Some (main, types) -> main :: types
144 List.fold_right Constr.UriManagerSet.add main_uris constants
146 List.filter (at_most ~dbd ~where:`Statement full_signature) uris
148 let filter_uris_backward ~dbd ~facts signature uris =
151 (sigmatch ~dbd ~facts ~where:`Statement signature)
153 intersect uris siguris
155 let compare_goal_list proof goal1 goal2 =
156 let _,metasenv,_,_ = proof in
157 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
158 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
160 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
163 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
167 CicReduction.are_convertible
168 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
175 CicReduction.are_convertible
176 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
183 (* experimental_hint is a version of hint for experimental
184 purposes. It uses auto_tac_verbose instead of auto tac.
185 Auto_tac verbose also returns a substitution - for the moment
186 as a function from cic to cic, to be changed into an association
187 list in the future -. This substitution is used to build a
188 hash table of the inspected goals with their associated proofs.
189 The cose is a cut and paste of the previous one: at the end
190 of the experimentation we shall make a choice. *)
192 let close_with_types s metasenv context =
193 Constr.UriManagerSet.fold
195 let t = CicUtil.term_of_uri e in
197 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
199 Constr.UriManagerSet.union bag (Constr.constants_of ty))
202 let close_with_constructors s metasenv context =
203 Constr.UriManagerSet.fold
205 let t = CicUtil.term_of_uri e in
208 | Cic.MutConstruct (uri,_,_,_) ->
209 (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
210 Cic.InductiveDefinition(tl,_,_,_) ->
213 (fun (i,s) (_,_,_,cl) ->
217 let curi = UriManager.uri_of_uriref uri i (Some j) in
218 j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
225 let apply_tac_verbose =
226 let profiler = CicUtil.profile "apply_tac_verbose" in
227 fun ~term status -> profiler.profile (PrimitiveTactics.apply_tac_verbose ~term) status
230 let profiler = CicUtil.profile "sigmatch" in
231 fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler.profile (Constr.sigmatch ~dbd ~facts ~where) signature
234 let profiler = CicUtil.profile "cmatch'" in
235 fun ~dbd ~facts signature -> profiler.profile (Constr.cmatch' ~dbd ~facts) signature
236 *) let apply_tac_verbose = PrimitiveTactics.apply_tac_verbose let cmatch' = Constr.cmatch'
238 let signature_of_goal ~(dbd:Mysql.dbd) ((proof, goal) as status) =
239 let (_, metasenv, _, _) = proof in
240 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
241 let main, sig_constants = Constr.signature_of ty in
242 let set = signature_of_hypothesis context in
247 List.fold_right Constr.UriManagerSet.add (main::l) set in
248 let set = Constr.UriManagerSet.union set sig_constants in
249 let all_constants_closed = close_with_types set metasenv context in
251 sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
252 let uris = List.filter nonvar (List.map snd uris) in
253 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
256 let equations_for_goal ~(dbd:Mysql.dbd) ((proof, goal) as status) =
257 let (_, metasenv, _, _) = proof in
258 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
259 let main, sig_constants = Constr.signature_of ty in
261 None -> raise Goal_is_not_an_equation
263 if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then
264 let set = signature_of_hypothesis context in
265 let set = Constr.UriManagerSet.union set sig_constants in
266 let set = close_with_types set metasenv context in
267 let set = close_with_constructors set metasenv context in
268 let set = List.fold_right Constr.UriManagerSet.remove (m::l) set in
270 sigmatch ~dbd ~facts:false ~where:`Statement (main,set) in
271 let uris = List.filter nonvar (List.map snd uris) in
272 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
274 else raise Goal_is_not_an_equation
276 let experimental_hint
277 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
278 let (_, metasenv, _, _) = proof in
279 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
280 let (uris, (main, sig_constants)) =
283 (sigmatch ~dbd ~facts signature, signature)
285 (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
287 let uris = List.filter nonvar (List.map snd uris) in
288 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
289 let types_constants =
291 | None -> Constr.UriManagerSet.empty
292 | Some (main, types) ->
293 List.fold_right Constr.UriManagerSet.add (main :: types)
294 Constr.UriManagerSet.empty
298 Constr.UriManagerSet.union
299 (signature_of_hypothesis context)
304 | None -> Constr.UriManagerSet.empty
307 CicTypeChecker.type_of_aux'
308 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
310 Constr.constants_of ty
312 Constr.UriManagerSet.union main hyp_and_sug
314 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
315 let all_constants_closed = close_with_types all_constants metasenv context in
316 let other_constants =
317 Constr.UriManagerSet.diff all_constants_closed types_constants
319 debug_print "all_constants_closed";
320 Constr.UriManagerSet.iter debug_print all_constants_closed;
321 debug_print "other_constants";
322 Constr.UriManagerSet.iter debug_print other_constants;
324 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
325 if ((List.length uris < pow) or (pow <= 0))
327 debug_print "MetadataQuery: large sig, falling back to old method";
328 filter_uris_forward ~dbd (main, other_constants) uris
330 filter_uris_backward ~dbd ~facts (main, other_constants) uris
332 let rec aux = function
337 let (subst,(proof, goal_list)) =
338 (* debug_print ("STO APPLICANDO" ^ uri); *)
340 ~term:(CicUtil.term_of_uri uri)
344 List.stable_sort (compare_goal_list proof) goal_list
346 Some (uri, (subst,(proof, goal_list)))
347 with ProofEngineTypes.Fail _ -> None
351 | Some status' -> status' :: aux tl)
354 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
355 Pervasives.compare (List.length goals1) (List.length goals2))
358 let new_experimental_hint
359 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ~universe
360 ((proof, goal) as status)
362 let (_, metasenv, _, _) = proof in
363 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
364 let (uris, (main, sig_constants)) =
367 (sigmatch ~dbd ~facts signature, signature)
369 (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in
372 (fun res u -> Constr.UriManagerSet.add u res)
373 Constr.UriManagerSet.empty universe in
376 (fun res (_,u) -> Constr.UriManagerSet.add u res)
377 Constr.UriManagerSet.empty uris in
378 let uris = Constr.UriManagerSet.inter uris universe in
379 let uris = Constr.UriManagerSet.elements uris in
380 let rec aux = function
385 let (subst,(proof, goal_list)) =
386 (* debug_print ("STO APPLICANDO" ^ uri); *)
388 ~term:(CicUtil.term_of_uri uri)
392 List.stable_sort (compare_goal_list proof) goal_list
394 Some (uri, (subst,(proof, goal_list)))
395 with ProofEngineTypes.Fail _ -> None
399 | Some status' -> status' :: aux tl)
402 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
403 Pervasives.compare (List.length goals1) (List.length goals2))
408 [`Rel [`MainConclusion None];
409 `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
410 `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
411 `Obj (uri,[`InHypothesis]);
414 MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
417 let fill_with_dummy_constants t =
418 let rec aux i types =
420 Cic.Lambda (n,s,t) ->
422 UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)^".con") in
423 (aux (i+1) (s::types)
424 (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
427 let t,types = aux 0 [] t in
430 let instance ~dbd t =
431 let t',types = fill_with_dummy_constants t in
432 let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
436 (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x)))
438 let no_concl = MetadataDb.count_distinct `Conclusion metadata in
439 let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
440 let no_full = MetadataDb.count_distinct `Statement metadata in
441 let is_dummy = function
442 | `Obj(s, _) -> (String.sub (UriManager.string_of_uri s) 0 10) <> "cic:/dummy"
445 let rec look_for_dummy_main = function
447 | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_
448 when (String.sub (UriManager.string_of_uri s) 0 10 = "cic:/dummy") ->
449 let s = UriManager.string_of_uri s in
450 let len = String.length s in
451 let dummy_index = int_of_string (String.sub s 11 (len-15)) in
452 let dummy_type = List.nth types dummy_index in
454 | _::l -> look_for_dummy_main l
456 match (look_for_dummy_main metadata) with
458 (* debug_print "Caso None"; *)
459 (* no dummy in main position *)
460 let metadata = List.filter is_dummy metadata in
461 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
462 let concl_card = Some (MetadataConstraints.Eq no_concl) in
463 let full_card = Some (MetadataConstraints.Eq no_full) in
464 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
465 Constr.at_least ~dbd ?concl_card ?full_card ?diff constraints
466 | Some (depth, dummy_type) ->
468 (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type)); *)
469 (* a dummy in main position *)
470 let metadata_for_dummy_type =
471 MetadataExtractor.compute ~body:None ~ty:dummy_type in
472 (* Let us skip this for the moment
473 let main_of_dummy_type =
474 look_for_dummy_main metadata_for_dummy_type in *)
475 let metadata = List.filter is_dummy metadata in
476 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
477 let metadata_for_dummy_type =
478 List.filter is_dummy metadata_for_dummy_type in
479 let metadata_for_dummy_type, depth' =
480 (* depth' = the depth of the A -> A -> Prop *)
481 List.fold_left (fun (acc,dep) c ->
483 | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
484 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
485 | `Obj (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
486 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
487 | `Rel (`MainConclusion (Some (MetadataTypes.Eq i))) ->
488 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
489 | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
491 let constraints_for_dummy_type =
492 List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
493 (* start with the dummy constant in main conlusion *)
494 let from = ["refObj as table0"] in
496 [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
497 sprintf "table0.h_depth >= %d" depth] in
500 (MetadataConstraints.add_constraint ~start:2)
501 (2,from,where) constraints in
502 let concl_card = Some (MetadataConstraints.Eq no_concl) in
503 let full_card = Some (MetadataConstraints.Eq no_full) in
504 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
506 MetadataConstraints.add_all_constr
507 (n,from,where) concl_card full_card diff in
508 (* join with the constraints over the type of the constant *)
510 (sprintf "table0.h_occurrence = table%d.source" n)::where in
512 sprintf "table0.h_depth - table%d.h_depth = %d"
513 n (depth - depth')::where
517 (MetadataConstraints.add_constraint ~start:n)
518 (n,from,where) constraints_for_dummy_type in
519 Constr.exec ~dbd (m,from,where)
521 (* fwd_simpl ****************************************************************)
523 let rec map_filter f n = function
527 | None -> map_filter f (succ n) tl
528 | Some hd -> hd :: map_filter f (succ n) tl
532 | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
533 | hd -> Some (CicUtil.uri_of_term hd, [])
536 | Invalid_argument "uri_of_term" -> None
542 | Some (uri, _) -> Some (n, uri)
546 | Some (uri, args) -> Some (uri, map_filter f 1 args)
548 let debug_metadata = function
550 | Some (outer, inners) ->
551 let f (n, uri) = Printf.eprintf "%s: %i %s\n" "fwd" n (UriManager.string_of_uri uri) in
552 Printf.eprintf "\n%s: %s\n" "fwd" (UriManager.string_of_uri outer);
553 List.iter f inners; prerr_newline ()
555 let fwd_simpl ~dbd t =
557 match row.(0), row.(1), row.(2) with
558 | Some source, Some inner, Some index ->
561 (int_of_string index, (UriManager.uri_of_string inner)) inners
564 let rec rank ranks (source, ok) =
566 | [], false -> [source, 0]
567 | [], true -> [source, 1]
568 | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
569 | (uri, 0) :: tl, true when uri = source -> (uri, 0) :: tl
570 | (uri, i) :: tl, true when uri = source -> (uri, succ i) :: tl
571 | hd :: tl, _ -> hd :: rank tl (source, ok)
573 let compare (_, x) (_, y) = compare x y in
574 let filter n (uri, rank) =
575 if rank > 0 then Some (UriManager.uri_of_string uri) else None
577 let metadata = get_metadata t in debug_metadata metadata;
580 | Some (outer, inners) ->
581 let select = "source, h_inner, h_index" in
582 let from = "genLemma" in
584 Printf.sprintf "h_outer = \"%s\""
585 (Mysql.escape (UriManager.string_of_uri outer)) in
586 let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
587 let result = Mysql.exec dbd query in
588 let lemmas = Mysql.map ~f:(map inners) result in
589 let ranked = List.fold_left rank [] lemmas in
590 let ordered = List.rev (List.fast_sort compare ranked) in
591 map_filter filter 0 ordered
593 (* get_decomposables ********************************************************)
595 let decomposables ~dbd =
596 let map row = match row.(0) with
599 match CicUtil.term_of_uri (UriManager.uri_of_string str) with
600 | Cic.MutInd (uri, typeno, _) -> Some (uri, typeno)
602 raise (UriManager.IllFormedUri str)
604 let select, from = "source", "decomposables" in
605 let query = Printf.sprintf "SELECT %s FROM %s" select from in
606 let decomposables = Mysql.map ~f:map (Mysql.exec dbd query) in
607 map_filter (fun _ x -> x) 0 decomposables