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 let debug_print = fun _ -> ()
33 (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
34 * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
35 let sqlpat_of_shellglob =
36 let star_RE, qmark_RE, percent_RE, uscore_RE =
37 Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
40 Pcre.replace ~rex:star_RE ~templ:"%"
41 (Pcre.replace ~rex:qmark_RE ~templ:"_"
42 (Pcre.replace ~rex:percent_RE ~templ:"\\%"
43 (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
47 let s = UriManager.string_of_uri s in
48 let len = String.length s in
49 let suffix = String.sub s (len-4) 4 in
52 let locate ~(dbd:Mysql.dbd) ?(vars = false) pat =
53 let sql_pat = sqlpat_of_shellglob pat in
55 sprintf ("SELECT source FROM %s WHERE value LIKE \"%s\" UNION "^^
56 "SELECT source FROM %s WHERE value LIKE \"%s\"")
57 (MetadataTypes.name_tbl ()) sql_pat
58 MetadataTypes.library_name_tbl sql_pat
60 let result = Mysql.exec dbd query in
63 (fun cols -> match cols.(0) with Some s -> UriManager.uri_of_string s | _ -> assert false))
65 let match_term ~(dbd:Mysql.dbd) ty =
66 (* debug_print (CicPp.ppterm ty); *)
67 let metadata = MetadataExtractor.compute ~body:None ~ty in
69 MetadataConstraints.UriManagerSet.cardinal (MetadataConstraints.constants_of ty)
72 if CicUtil.is_meta_closed ty then
73 Some (MetadataConstraints.Eq constants_no), None
76 let (hyp_constants, concl_constants) =
77 (* collect different constants in hypotheses and conclusions *)
79 (fun ((hyp, concl) as acc) metadata ->
80 match (metadata: MetadataTypes.metadata) with
81 | `Sort _ | `Rel _ -> acc
82 | `Obj (uri, `InConclusion) | `Obj (uri, `MainConclusion _)
83 when not (List.mem uri concl) -> (hyp, uri :: concl)
84 | `Obj (uri, `InHypothesis) | `Obj (uri, `MainHypothesis _)
85 when not (List.mem uri hyp) -> (uri :: hyp, concl)
90 List.length hyp_constants - List.length concl_constants
92 let (concl_metas, hyp_metas) = MetadataExtractor.compute_metas ty in
94 if MetadataExtractor.IntSet.equal concl_metas hyp_metas then
95 Some (MetadataConstraints.Eq diff_no)
96 else if MetadataExtractor.IntSet.subset concl_metas hyp_metas then
97 Some (MetadataConstraints.Gt (diff_no - 1))
98 else if MetadataExtractor.IntSet.subset hyp_metas concl_metas then
99 Some (MetadataConstraints.Lt (diff_no + 1))
105 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
106 Constr.at_least ~dbd ?full_card ?diff constraints
108 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
110 let signature_of_hypothesis context =
115 | Some (_, Cic.Decl t)
116 | Some (_, Cic.Def (t, _)) ->
117 Constr.UriManagerSet.union set (Constr.constants_of t))
118 Constr.UriManagerSet.empty context
120 let intersect uris siguris =
121 let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
123 List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
125 let inter = Constr.UriManagerSet.inter set1 set2 in
126 List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
128 let filter_uris_forward ~dbd (main, constants) uris =
132 | Some (main, types) -> main :: types
135 List.fold_right Constr.UriManagerSet.add main_uris constants
137 List.filter (Constr.at_most ~dbd ~where:`Statement full_signature) uris
139 let filter_uris_backward ~dbd ~facts signature uris =
142 (MetadataConstraints.sigmatch ~dbd ~facts ~where:`Statement signature)
144 intersect uris siguris
146 let compare_goal_list proof goal1 goal2 =
147 let _,metasenv,_,_ = proof in
148 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
149 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
151 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
154 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
158 CicReduction.are_convertible
159 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
166 CicReduction.are_convertible
167 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
174 let hint ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
175 let (_, metasenv, _, _) = proof in
176 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
177 let (uris, (main, sig_constants)) =
179 | Some signature -> (Constr.sigmatch ~dbd ~facts signature, signature)
180 | None -> (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
182 let uris = List.filter nonvar (List.map snd uris) in
183 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
184 let types_constants =
186 | None -> Constr.UriManagerSet.empty
187 | Some (main, types) ->
188 List.fold_right Constr.UriManagerSet.add (main :: types)
189 Constr.UriManagerSet.empty
192 Constr.UriManagerSet.diff (signature_of_hypothesis context) types_constants
194 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
195 let other_constants = Constr.UriManagerSet.union sig_constants hyp_constants in
197 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
198 if ((List.length uris < pow) or (pow <= 0))
200 (* debug_print "MetadataQuery: large sig, falling back to old method"; *)
201 filter_uris_forward ~dbd (main, other_constants) uris
203 filter_uris_backward ~dbd ~facts (main, other_constants) uris
205 let rec aux = function
210 let (proof, goal_list) =
211 (* debug_print ("STO APPLICANDO " ^ uri); *)
213 (PrimitiveTactics.apply_tac
214 ~term:(CicUtil.term_of_uri uri))
218 List.stable_sort (compare_goal_list proof) goal_list
220 Some (uri, (proof, goal_list))
221 with ProofEngineTypes.Fail _ -> None
229 (fun (_, (_, goals1)) (_, (_, goals2)) ->
230 Pervasives.compare (List.length goals1) (List.length goals2))
233 (* experimental_hint is a version of hint for experimental
234 purposes. It uses auto_tac_verbose instead of auto tac.
235 Auto_tac verbose also returns a substitution - for the moment
236 as a function from cic to cic, to be changed into an association
237 list in the future -. This substitution is used to build a
238 hash table of the inspected goals with their associated proofs.
239 The cose is a cut and paste of the previous one: at the end
240 of the experimentation we shall make a choice. *)
242 let close_with_types s metasenv context =
243 Constr.UriManagerSet.fold
245 let t = CicUtil.term_of_uri e in
247 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
249 Constr.UriManagerSet.union bag (Constr.constants_of ty))
252 let experimental_hint
253 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
254 let (_, metasenv, _, _) = proof in
255 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
256 let (uris, (main, sig_constants)) =
259 (Constr.sigmatch ~dbd ~facts signature, signature)
261 (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
263 let uris = List.filter nonvar (List.map snd uris) in
264 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
265 let types_constants =
267 | None -> Constr.UriManagerSet.empty
268 | Some (main, types) ->
269 List.fold_right Constr.UriManagerSet.add (main :: types)
270 Constr.UriManagerSet.empty
274 Constr.UriManagerSet.union
275 (signature_of_hypothesis context)
280 | None -> Constr.UriManagerSet.empty
283 CicTypeChecker.type_of_aux'
284 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
286 Constr.constants_of ty
288 Constr.UriManagerSet.union main hyp_and_sug
290 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
291 let all_constants_closed = close_with_types all_constants metasenv context in
292 let other_constants =
293 Constr.UriManagerSet.diff all_constants_closed types_constants
295 debug_print "all_constants_closed";
296 Constr.UriManagerSet.iter debug_print all_constants_closed;
297 debug_print "other_constants";
298 Constr.UriManagerSet.iter debug_print other_constants;
300 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
301 if ((List.length uris < pow) or (pow <= 0))
303 debug_print "MetadataQuery: large sig, falling back to old method";
304 filter_uris_forward ~dbd (main, other_constants) uris
306 filter_uris_backward ~dbd ~facts (main, other_constants) uris
308 let rec aux = function
313 let (subst,(proof, goal_list)) =
314 (* debug_print ("STO APPLICANDO" ^ uri); *)
315 PrimitiveTactics.apply_tac_verbose
316 ~term:(CicUtil.term_of_uri uri)
320 List.stable_sort (compare_goal_list proof) goal_list
322 Some (uri, (subst,(proof, goal_list)))
323 with ProofEngineTypes.Fail _ -> None
327 | Some status' -> status' :: aux tl)
330 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
331 Pervasives.compare (List.length goals1) (List.length goals2))
336 [`Rel [`MainConclusion None];
337 `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
338 `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
339 `Obj (uri,[`InHypothesis]);
342 MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
345 let fill_with_dummy_constants t =
346 let rec aux i types =
348 Cic.Lambda (n,s,t) ->
350 UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)) in
351 (aux (i+1) (s::types)
352 (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
355 let t,types = aux 0 [] t in
358 let instance ~dbd t =
359 let t',types = fill_with_dummy_constants t in
360 let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
364 (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x)))
366 let no_concl = MetadataDb.count_distinct `Conclusion metadata in
367 let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
368 let no_full = MetadataDb.count_distinct `Statement metadata in
369 let is_dummy = function
370 | `Obj(s, _) -> (String.sub (UriManager.string_of_uri s) 0 10) <> "cic:/dummy"
373 let rec look_for_dummy_main = function
375 | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_
376 when (String.sub (UriManager.string_of_uri s) 0 10 = "cic:/dummy") ->
377 let s = UriManager.string_of_uri s in
378 let len = String.length s in
379 let dummy_index = int_of_string (String.sub s 11 (len-11)) in
380 let dummy_type = List.nth types dummy_index in
382 | _::l -> look_for_dummy_main l
384 match (look_for_dummy_main metadata) with
386 (* debug_print "Caso None"; *)
387 (* no dummy in main position *)
388 let metadata = List.filter is_dummy metadata in
389 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
390 let concl_card = Some (MetadataConstraints.Eq no_concl) in
391 let full_card = Some (MetadataConstraints.Eq no_full) in
392 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
393 Constr.at_least ~dbd ?concl_card ?full_card ?diff constraints
394 | Some (depth, dummy_type) ->
396 (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type)); *)
397 (* a dummy in main position *)
398 let metadata_for_dummy_type =
399 MetadataExtractor.compute ~body:None ~ty:dummy_type in
400 (* Let us skip this for the moment
401 let main_of_dummy_type =
402 look_for_dummy_main metadata_for_dummy_type in *)
403 let metadata = List.filter is_dummy metadata in
404 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
405 let metadata_for_dummy_type =
406 List.filter is_dummy metadata_for_dummy_type in
407 let metadata_for_dummy_type, depth' =
408 (* depth' = the depth of the A -> A -> Prop *)
409 List.fold_left (fun (acc,dep) c ->
411 | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
412 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
413 | `Obj (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
414 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
415 | `Rel (`MainConclusion (Some (MetadataTypes.Eq i))) ->
416 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
417 | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
419 let constraints_for_dummy_type =
420 List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
421 (* start with the dummy constant in main conlusion *)
422 let from = ["refObj as table0"] in
424 [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
425 sprintf "table0.h_depth >= %d" depth] in
428 (MetadataConstraints.add_constraint ~start:2)
429 (2,from,where) constraints in
430 let concl_card = Some (MetadataConstraints.Eq no_concl) in
431 let full_card = Some (MetadataConstraints.Eq no_full) in
432 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
434 MetadataConstraints.add_all_constr
435 (n,from,where) concl_card full_card diff in
436 (* join with the constraints over the type of the constant *)
438 (sprintf "table0.h_occurrence = table%d.source" n)::where in
440 sprintf "table0.h_depth - table%d.h_depth = %d"
441 n (depth - depth')::where
445 (MetadataConstraints.add_constraint ~start:n)
446 (n,from,where) constraints_for_dummy_type in
447 Constr.exec ~dbd (m,from,where)
449 (* fwd_simpl ****************************************************************)
451 let rec map_filter f n = function
455 | None -> map_filter f (succ n) tl
456 | Some hd -> hd :: map_filter f (succ n) tl
460 | Cic.Appl (hd :: tl) -> Some (CicUtil.uri_of_term hd, tl)
461 | hd -> Some (CicUtil.uri_of_term hd, [])
464 | Invalid_argument "uri_of_term" -> None
470 | Some (uri, _) -> Some (n, uri)
474 | Some (uri, args) -> Some (uri, map_filter f 1 args)
476 let debug_metadata = function
478 | Some (outer, inners) ->
479 let f (n, uri) = Printf.eprintf "%s: %i %s\n" "fwd" n uri in
480 Printf.eprintf "\n%s: %s\n" "fwd" outer;
481 List.iter f inners; prerr_newline ()
483 let fwd_simpl ~dbd t =
485 match row.(0), row.(1), row.(2) with
486 | Some source, Some inner, Some index ->
489 (int_of_string index, (UriManager.uri_of_string inner)) inners
492 let rec rank ranks (source, ok) =
494 | [], false -> [source, 0]
495 | [], true -> [source, 1]
496 | (uri, i) :: tl, false when uri = source -> (uri, 0) :: tl
497 | (uri, 0) :: tl, true when uri = source -> (uri, 0) :: tl
498 | (uri, i) :: tl, true when uri = source -> (uri, succ i) :: tl
499 | hd :: tl, _ -> hd :: rank tl (source, ok)
501 let compare (_, x) (_, y) = compare x y in
502 let filter n (uri, rank) =
503 if rank > 0 then Some (UriManager.uri_of_string uri) else None
505 match get_metadata t with
507 | Some (outer, inners) ->
508 let select = "source, h_inner, h_index" in
509 let from = "genLemma" in
511 Printf.sprintf "h_outer = \"%s\""
512 (Mysql.escape (UriManager.string_of_uri outer)) in
513 let query = Printf.sprintf "SELECT %s FROM %s WHERE %s" select from where in
514 let result = Mysql.exec dbd query in
515 let lemmas = Mysql.map result ~f:(map inners) in
516 let ranked = List.fold_left rank [] lemmas in
517 let ordered = List.rev (List.fast_sort compare ranked) in
518 map_filter filter 0 ordered