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\""
53 MetadataTypes.name_tbl sql_pat
55 let result = Mysql.exec dbd query in
58 (fun cols -> match cols.(0) with Some s -> s | _ -> assert false))
60 let match_term ~(dbd:Mysql.dbd) ty =
61 let metadata = MetadataExtractor.compute ~body:None ~ty in
63 MetadataConstraints.StringSet.cardinal (MetadataConstraints.constants_of ty)
65 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
66 Constr.at_least ~dbd ~full_card:(MetadataConstraints.Eq constants_no)
69 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
71 let signature_of_hypothesis context =
76 | Some (_, Cic.Decl t)
77 | Some (_, Cic.Def (t, _)) ->
78 Constr.StringSet.union set (Constr.constants_of t))
79 Constr.StringSet.empty context
81 let intersect uris siguris =
82 let set1 = List.fold_right Constr.StringSet.add uris Constr.StringSet.empty in
84 List.fold_right Constr.StringSet.add siguris Constr.StringSet.empty
86 let inter = Constr.StringSet.inter set1 set2 in
87 List.filter (fun s -> Constr.StringSet.mem s inter) uris
89 let filter_uris_forward ~dbd (main, constants) uris =
93 | Some (main, types) -> main :: types
96 List.fold_right Constr.StringSet.add main_uris constants
98 List.filter (Constr.at_most ~dbd ~where:`Statement full_signature) uris
100 let filter_uris_backward ~dbd ~facts signature uris =
103 (MetadataConstraints.sigmatch ~dbd ~facts ~where:`Statement signature)
105 intersect uris siguris
107 let compare_goal_list proof goal1 goal2 =
108 let _,metasenv,_,_ = proof in
109 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
110 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
112 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
115 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
119 CicReduction.are_convertible
120 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
127 CicReduction.are_convertible
128 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
135 let hint ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
136 let (_, metasenv, _, _) = proof in
137 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
138 let (uris, (main, sig_constants)) =
140 | Some signature -> (Constr.sigmatch ~dbd ~facts signature, signature)
141 | None -> (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
143 let uris = List.filter nonvar (List.map snd uris) in
144 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
145 let types_constants =
147 | None -> Constr.StringSet.empty
148 | Some (main, types) ->
149 List.fold_right Constr.StringSet.add (main :: types)
150 Constr.StringSet.empty
153 Constr.StringSet.diff (signature_of_hypothesis context) types_constants
155 Constr.StringSet.iter prerr_endline hyp_constants;
156 let other_constants = Constr.StringSet.union sig_constants hyp_constants in
158 let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
159 if ((List.length uris < pow) or (pow <= 0))
161 prerr_endline "MetadataQuery: large sig, falling back to old method";
162 filter_uris_forward ~dbd (main, other_constants) uris
164 filter_uris_backward ~dbd ~facts (main, other_constants) uris
166 let rec aux = function
171 let (proof, goal_list) =
172 (* prerr_endline ("STO APPLICANDO " ^ uri); *)
174 (PrimitiveTactics.apply_tac
175 ~term:(CicUtil.term_of_uri uri))
179 List.stable_sort (compare_goal_list proof) goal_list
181 Some (uri, (proof, goal_list))
182 with ProofEngineTypes.Fail _ -> None
190 (fun (_, (_, goals1)) (_, (_, goals2)) ->
191 Pervasives.compare (List.length goals1) (List.length goals2))
194 (* experimental_hint is a version of hint for experimental
195 purposes. It uses auto_tac_verbose instead of auto tac.
196 Auto_tac verbose also returns a substitution - for the moment
197 as a function from cic to cic, to be changed into an association
198 list in the future -. This substitution is used to build a
199 hash table of the inspected goals with their associated proofs.
200 The cose is a cut and paste of the previous one: at the end
201 of the experimentation we shall make a choice. *)
203 let experimental_hint
204 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
205 let (_, metasenv, _, _) = proof in
206 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
207 let (uris, (main, sig_constants)) =
210 (Constr.sigmatch ~dbd ~facts signature, signature)
212 (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
214 let uris = List.filter nonvar (List.map snd uris) in
215 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
216 let types_constants =
218 | None -> Constr.StringSet.empty
219 | Some (main, types) ->
220 List.fold_right Constr.StringSet.add (main :: types)
221 Constr.StringSet.empty
224 Constr.StringSet.diff (signature_of_hypothesis context) types_constants
226 Constr.StringSet.iter prerr_endline hyp_constants;
227 let other_constants = Constr.StringSet.union sig_constants hyp_constants in
229 let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
230 if ((List.length uris < pow) or (pow <= 0))
232 prerr_endline "MetadataQuery: large sig, falling back to old method";
233 filter_uris_forward ~dbd (main, other_constants) uris
235 filter_uris_backward ~dbd ~facts (main, other_constants) uris
237 let rec aux = function
242 let (subst,(proof, goal_list)) =
243 (* prerr_endline ("STO APPLICANDO" ^ uri); *)
244 PrimitiveTactics.apply_tac_verbose
245 ~term:(CicUtil.term_of_uri uri)
249 List.stable_sort (compare_goal_list proof) goal_list
251 Some (uri, (subst,(proof, goal_list)))
252 with ProofEngineTypes.Fail _ -> None
256 | Some status' -> status' :: aux tl)
259 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
260 Pervasives.compare (List.length goals1) (List.length goals2))
265 [`Rel [`MainConclusion None];
266 `Sort (Cic.Prop,[`MainHypothesis (Some 1)]);
267 `Obj (uri,[`MainHypothesis (Some 0)]);
268 `Obj (uri,[`InHypothesis]);
271 MetadataConstraints.at_least ~dbd constraints