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 (** maps a shell like pattern (which uses '*' and '?') to a sql pattern for
32 * the "like" operator (which uses '%' and '_'). Does not support escaping. *)
33 let sqlpat_of_shellglob =
34 let star_RE, qmark_RE, percent_RE, uscore_RE =
35 Pcre.regexp "\\*", Pcre.regexp "\\?", Pcre.regexp "%", Pcre.regexp "_"
38 Pcre.replace ~rex:star_RE ~templ:"%"
39 (Pcre.replace ~rex:qmark_RE ~templ:"_"
40 (Pcre.replace ~rex:percent_RE ~templ:"\\%"
41 (Pcre.replace ~rex:uscore_RE ~templ:"\\_"
45 let len = String.length s in
46 let suffix = String.sub s (len-4) 4 in
49 let locate ~(dbd:Mysql.dbd) ?(vars = false) pat =
50 let sql_pat = sqlpat_of_shellglob pat in
52 sprintf ("SELECT source FROM %s WHERE value LIKE \"%s\" UNION "^^
53 "SELECT source FROM %s WHERE value LIKE \"%s\"")
54 (MetadataTypes.name_tbl ()) sql_pat
55 MetadataTypes.library_name_tbl sql_pat
57 let result = Mysql.exec dbd query in
60 (fun cols -> match cols.(0) with Some s -> s | _ -> assert false))
62 let match_term ~(dbd:Mysql.dbd) ty =
63 (* prerr_endline (CicPp.ppterm ty); *)
64 let metadata = MetadataExtractor.compute ~body:None ~ty in
66 MetadataConstraints.StringSet.cardinal (MetadataConstraints.constants_of ty)
69 if CicUtil.is_meta_closed ty then
70 Some (MetadataConstraints.Eq constants_no), None
73 let (hyp_constants, concl_constants) =
74 (* collect different constants in hypotheses and conclusions *)
76 (fun ((hyp, concl) as acc) metadata ->
77 match (metadata: MetadataTypes.metadata) with
78 | `Sort _ | `Rel _ -> acc
79 | `Obj (uri, `InConclusion) | `Obj (uri, `MainConclusion _)
80 when not (List.mem uri concl) -> (hyp, uri :: concl)
81 | `Obj (uri, `InHypothesis) | `Obj (uri, `MainHypothesis _)
82 when not (List.mem uri hyp) -> (uri :: hyp, concl)
87 List.length hyp_constants - List.length concl_constants
89 let (concl_metas, hyp_metas) = MetadataExtractor.compute_metas ty in
91 if MetadataExtractor.IntSet.equal concl_metas hyp_metas then
92 Some (MetadataConstraints.Eq diff_no)
93 else if MetadataExtractor.IntSet.subset concl_metas hyp_metas then
94 Some (MetadataConstraints.Gt (diff_no - 1))
95 else if MetadataExtractor.IntSet.subset hyp_metas concl_metas then
96 Some (MetadataConstraints.Lt (diff_no + 1))
102 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
103 Constr.at_least ~dbd ?full_card ?diff constraints
105 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
107 let signature_of_hypothesis context =
112 | Some (_, Cic.Decl t)
113 | Some (_, Cic.Def (t, _)) ->
114 Constr.StringSet.union set (Constr.constants_of t))
115 Constr.StringSet.empty context
117 let intersect uris siguris =
118 let set1 = List.fold_right Constr.StringSet.add uris Constr.StringSet.empty in
120 List.fold_right Constr.StringSet.add siguris Constr.StringSet.empty
122 let inter = Constr.StringSet.inter set1 set2 in
123 List.filter (fun s -> Constr.StringSet.mem s inter) uris
125 let filter_uris_forward ~dbd (main, constants) uris =
129 | Some (main, types) -> main :: types
132 List.fold_right Constr.StringSet.add main_uris constants
134 List.filter (Constr.at_most ~dbd ~where:`Statement full_signature) uris
136 let filter_uris_backward ~dbd ~facts signature uris =
139 (MetadataConstraints.sigmatch ~dbd ~facts ~where:`Statement signature)
141 intersect uris siguris
143 let compare_goal_list proof goal1 goal2 =
144 let _,metasenv,_,_ = proof in
145 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
146 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
148 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
151 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
155 CicReduction.are_convertible
156 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
163 CicReduction.are_convertible
164 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
171 let hint ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
172 let (_, metasenv, _, _) = proof in
173 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
174 let (uris, (main, sig_constants)) =
176 | Some signature -> (Constr.sigmatch ~dbd ~facts signature, signature)
177 | None -> (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
179 let uris = List.filter nonvar (List.map snd uris) in
180 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
181 let types_constants =
183 | None -> Constr.StringSet.empty
184 | Some (main, types) ->
185 List.fold_right Constr.StringSet.add (main :: types)
186 Constr.StringSet.empty
189 Constr.StringSet.diff (signature_of_hypothesis context) types_constants
191 (* Constr.StringSet.iter prerr_endline hyp_constants; *)
192 let other_constants = Constr.StringSet.union sig_constants hyp_constants in
194 let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
195 if ((List.length uris < pow) or (pow <= 0))
197 (* prerr_endline "MetadataQuery: large sig, falling back to old method"; *)
198 filter_uris_forward ~dbd (main, other_constants) uris
200 filter_uris_backward ~dbd ~facts (main, other_constants) uris
202 let rec aux = function
207 let (proof, goal_list) =
208 (* prerr_endline ("STO APPLICANDO " ^ uri); *)
210 (PrimitiveTactics.apply_tac
211 ~term:(CicUtil.term_of_uri uri))
215 List.stable_sort (compare_goal_list proof) goal_list
217 Some (uri, (proof, goal_list))
218 with ProofEngineTypes.Fail _ -> None
226 (fun (_, (_, goals1)) (_, (_, goals2)) ->
227 Pervasives.compare (List.length goals1) (List.length goals2))
230 (* experimental_hint is a version of hint for experimental
231 purposes. It uses auto_tac_verbose instead of auto tac.
232 Auto_tac verbose also returns a substitution - for the moment
233 as a function from cic to cic, to be changed into an association
234 list in the future -. This substitution is used to build a
235 hash table of the inspected goals with their associated proofs.
236 The cose is a cut and paste of the previous one: at the end
237 of the experimentation we shall make a choice. *)
239 let close_with_types s metasenv context =
240 Constr.StringSet.fold
242 let t = CicUtil.term_of_uri e in
244 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
246 Constr.StringSet.union bag (Constr.constants_of ty))
249 let experimental_hint
250 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
251 let (_, metasenv, _, _) = proof in
252 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
253 let (uris, (main, sig_constants)) =
256 (Constr.sigmatch ~dbd ~facts signature, signature)
258 (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
260 let uris = List.filter nonvar (List.map snd uris) in
261 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
262 let types_constants =
264 | None -> Constr.StringSet.empty
265 | Some (main, types) ->
266 List.fold_right Constr.StringSet.add (main :: types)
267 Constr.StringSet.empty
271 Constr.StringSet.union
272 (signature_of_hypothesis context)
277 | None -> Constr.StringSet.empty
280 CicTypeChecker.type_of_aux'
281 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
283 Constr.constants_of ty
285 Constr.StringSet.union main hyp_and_sug
287 (* Constr.StringSet.iter prerr_endline hyp_constants; *)
288 let all_constants_closed = close_with_types all_constants metasenv context in
289 let other_constants =
290 Constr.StringSet.diff all_constants_closed types_constants
292 prerr_endline "all_constants_closed";
293 Constr.StringSet.iter prerr_endline all_constants_closed;
294 prerr_endline "other_constants";
295 Constr.StringSet.iter prerr_endline other_constants;
297 let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
298 if ((List.length uris < pow) or (pow <= 0))
300 prerr_endline "MetadataQuery: large sig, falling back to old method";
301 filter_uris_forward ~dbd (main, other_constants) uris
303 filter_uris_backward ~dbd ~facts (main, other_constants) uris
305 let rec aux = function
310 let (subst,(proof, goal_list)) =
311 (* prerr_endline ("STO APPLICANDO" ^ uri); *)
312 PrimitiveTactics.apply_tac_verbose
313 ~term:(CicUtil.term_of_uri uri)
317 List.stable_sort (compare_goal_list proof) goal_list
319 Some (uri, (subst,(proof, goal_list)))
320 with ProofEngineTypes.Fail _ -> None
324 | Some status' -> status' :: aux tl)
327 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
328 Pervasives.compare (List.length goals1) (List.length goals2))
333 [`Rel [`MainConclusion None];
334 `Sort (Cic.Prop,[`MainHypothesis (Some (MetadataTypes.Ge 1))]);
335 `Obj (uri,[`MainHypothesis (Some (MetadataTypes.Eq 0))]);
336 `Obj (uri,[`InHypothesis]);
339 MetadataConstraints.at_least ~rating:`Hits ~dbd constraints
342 let fill_with_dummy_constants t =
343 let rec aux i types =
345 Cic.Lambda (n,s,t) ->
347 UriManager.uri_of_string ("cic:/dummy_"^(string_of_int i)) in
348 (aux (i+1) (s::types)
349 (CicSubstitution.subst (Cic.Const(dummy_uri,[])) t))
352 let t,types = aux 0 [] t in
355 let instance ~dbd t =
356 let t',types = fill_with_dummy_constants t in
357 let metadata = MetadataExtractor.compute ~body:None ~ty:t' in
361 (MetadataPp.pp_constr (MetadataTypes.constr_of_metadata x)))
363 let no_concl = MetadataDb.count_distinct `Conclusion metadata in
364 let no_hyp = MetadataDb.count_distinct `Hypothesis metadata in
365 let no_full = MetadataDb.count_distinct `Statement metadata in
366 let is_dummy = function
367 | `Obj(s, _) -> (String.sub s 0 10) <> "cic:/dummy"
370 let rec look_for_dummy_main = function
372 | `Obj(s,`MainConclusion (Some (MetadataTypes.Eq d)))::_
373 when ((String.sub s 0 10) = "cic:/dummy") ->
374 let len = String.length s in
375 let dummy_index = int_of_string (String.sub s 11 (len-11)) in
376 let dummy_type = List.nth types dummy_index in
378 | _::l -> look_for_dummy_main l
380 match (look_for_dummy_main metadata) with
382 (* prerr_endline "Caso None"; *)
383 (* no dummy in main position *)
384 let metadata = List.filter is_dummy metadata in
385 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
386 let concl_card = Some (MetadataConstraints.Eq no_concl) in
387 let full_card = Some (MetadataConstraints.Eq no_full) in
388 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
389 Constr.at_least ~dbd ?concl_card ?full_card ?diff constraints
390 | Some (depth, dummy_type) ->
392 (sprintf "Caso Some %d %s" depth (CicPp.ppterm dummy_type)); *)
393 (* a dummy in main position *)
394 let metadata_for_dummy_type =
395 MetadataExtractor.compute ~body:None ~ty:dummy_type in
396 (* Let us skip this for the moment
397 let main_of_dummy_type =
398 look_for_dummy_main metadata_for_dummy_type in *)
399 let metadata = List.filter is_dummy metadata in
400 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
401 let metadata_for_dummy_type =
402 List.filter is_dummy metadata_for_dummy_type in
403 let metadata_for_dummy_type, depth' =
404 (* depth' = the depth of the A -> A -> Prop *)
405 List.fold_left (fun (acc,dep) c ->
407 | `Sort (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
408 (`Sort (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
409 | `Obj (s,`MainConclusion (Some (MetadataTypes.Eq i))) ->
410 (`Obj (s,`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
411 | `Rel (`MainConclusion (Some (MetadataTypes.Eq i))) ->
412 (`Rel (`MainConclusion (Some (MetadataTypes.Ge i))))::acc, i
413 | _ -> (c::acc,dep)) ([],0) metadata_for_dummy_type
415 let constraints_for_dummy_type =
416 List.map MetadataTypes.constr_of_metadata metadata_for_dummy_type in
417 (* start with the dummy constant in main conlusion *)
418 let from = ["refObj as table0"] in
420 [sprintf "table0.h_position = \"%s\"" MetadataTypes.mainconcl_pos;
421 sprintf "table0.h_depth >= %d" depth] in
424 (MetadataConstraints.add_constraint ~start:2)
425 (2,from,where) constraints in
426 let concl_card = Some (MetadataConstraints.Eq no_concl) in
427 let full_card = Some (MetadataConstraints.Eq no_full) in
428 let diff = Some (MetadataConstraints.Eq (no_hyp - no_concl)) in
430 MetadataConstraints.add_all_constr
431 (n,from,where) concl_card full_card diff in
432 (* join with the constraints over the type of the constant *)
434 (sprintf "table0.h_occurrence = table%d.source" n)::where in
436 sprintf "table0.h_depth - table%d.h_depth = %d"
437 n (depth - depth')::where
441 (MetadataConstraints.add_constraint ~start:n)
442 (n,from,where) constraints_for_dummy_type in
443 Constr.exec ~dbd (m,from,where)