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
32 let len = String.length s in
33 let suffix = String.sub s (len-4) 4 in
36 let locate ~(dbd:Mysql.dbd) ?(vars = false) name =
38 sprintf "SELECT source FROM %s WHERE value = \"%s\""
39 MetadataTypes.name_tbl name
41 let result = Mysql.exec dbd query in
44 (fun cols -> match cols.(0) with Some s -> s | _ -> assert false))
46 let match_term ~(dbd:Mysql.dbd) ty =
47 let metadata = MetadataExtractor.compute ~body:None ~ty in
49 MetadataConstraints.StringSet.cardinal (MetadataConstraints.constants_of ty)
51 let constraints = List.map MetadataTypes.constr_of_metadata metadata in
52 Constr.at_least ~dbd ~full_card:(MetadataConstraints.Eq constants_no)
55 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
57 let signature_of_hypothesis context =
62 | Some (_, Cic.Decl t)
63 | Some (_, Cic.Def (t, _)) ->
64 Constr.StringSet.union set (Constr.constants_of t))
65 Constr.StringSet.empty context
67 let intersect uris siguris =
68 let set1 = List.fold_right Constr.StringSet.add uris Constr.StringSet.empty in
70 List.fold_right Constr.StringSet.add siguris Constr.StringSet.empty
72 let inter = Constr.StringSet.inter set1 set2 in
73 List.filter (fun s -> Constr.StringSet.mem s inter) uris
75 let filter_uris_forward ~dbd (main, constants) uris =
79 | Some (main, types) -> main :: types
82 List.fold_right Constr.StringSet.add main_uris constants
84 List.filter (Constr.at_most ~dbd ~where:`Statement full_signature) uris
86 let filter_uris_backward ~dbd ~facts signature uris =
89 (MetadataConstraints.sigmatch ~dbd ~facts ~where:`Statement signature)
91 intersect uris siguris
93 let compare_goal_list proof goal1 goal2 =
94 let _,metasenv,_,_ = proof in
95 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
96 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
98 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
101 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
105 CicReduction.are_convertible
106 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
113 CicReduction.are_convertible
114 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
121 let hint ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
122 let (_, metasenv, _, _) = proof in
123 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
124 let (uris, (main, sig_constants)) =
126 | Some signature -> (Constr.sigmatch ~dbd ~facts signature, signature)
127 | None -> (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
129 let uris = List.filter nonvar (List.map snd uris) in
130 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
131 let types_constants =
133 | None -> Constr.StringSet.empty
134 | Some (main, types) ->
135 List.fold_right Constr.StringSet.add (main :: types)
136 Constr.StringSet.empty
139 Constr.StringSet.diff (signature_of_hypothesis context) types_constants
141 Constr.StringSet.iter prerr_endline hyp_constants;
142 let other_constants = Constr.StringSet.union sig_constants hyp_constants in
144 let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
145 if ((List.length uris < pow) or (pow <= 0))
147 prerr_endline "MetadataQuery: large sig, falling back to old method";
148 filter_uris_forward ~dbd (main, other_constants) uris
150 filter_uris_backward ~dbd ~facts (main, other_constants) uris
152 let rec aux = function
157 let (proof, goal_list) =
158 (* prerr_endline ("STO APPLICANDO " ^ uri); *)
160 (PrimitiveTactics.apply_tac
161 ~term:(CicUtil.term_of_uri uri))
165 List.stable_sort (compare_goal_list proof) goal_list
167 Some (uri, (proof, goal_list))
168 with ProofEngineTypes.Fail _ -> None
176 (fun (_, (_, goals1)) (_, (_, goals2)) ->
177 Pervasives.compare (List.length goals1) (List.length goals2))
180 (* experimental_hint is a version of hint for experimental
181 purposes. It uses auto_tac_verbose instead of auto tac.
182 Auto_tac verbose also returns a substitution - for the moment
183 as a function from cic to cic, to be changed into an association
184 list in the future -. This substitution is used to build a
185 hash table of the inspected goals with their associated proofs.
186 The cose is a cut and paste of the previous one: at the end
187 of the experimentation we shall make a choice. *)
189 let experimental_hint
190 ~(dbd:Mysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
191 let (_, metasenv, _, _) = proof in
192 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
193 let (uris, (main, sig_constants)) =
196 (Constr.sigmatch ~dbd ~facts signature, signature)
198 (Constr.cmatch' ~dbd ~facts ty, Constr.signature_of ty)
200 let uris = List.filter nonvar (List.map snd uris) in
201 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
202 let types_constants =
204 | None -> Constr.StringSet.empty
205 | Some (main, types) ->
206 List.fold_right Constr.StringSet.add (main :: types)
207 Constr.StringSet.empty
210 Constr.StringSet.diff (signature_of_hypothesis context) types_constants
212 Constr.StringSet.iter prerr_endline hyp_constants;
213 let other_constants = Constr.StringSet.union sig_constants hyp_constants in
215 let pow = 2 ** (Constr.StringSet.cardinal other_constants) in
216 if ((List.length uris < pow) or (pow <= 0))
218 prerr_endline "MetadataQuery: large sig, falling back to old method";
219 filter_uris_forward ~dbd (main, other_constants) uris
221 filter_uris_backward ~dbd ~facts (main, other_constants) uris
223 let rec aux = function
228 let (subst,(proof, goal_list)) =
229 (* prerr_endline ("STO APPLICANDO" ^ uri); *)
230 PrimitiveTactics.apply_tac_verbose
231 ~term:(CicUtil.term_of_uri uri)
235 List.stable_sort (compare_goal_list proof) goal_list
237 Some (uri, (subst,(proof, goal_list)))
238 with ProofEngineTypes.Fail _ -> None
242 | Some status' -> status' :: aux tl)
245 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
246 Pervasives.compare (List.length goals1) (List.length goals2))
251 [`Rel [`MainConclusion None];
252 `Sort (Cic.Prop,[`MainHypothesis (Some 1)]);
253 `Obj (uri,[`MainHypothesis (Some 0)]);
254 `Obj (uri,[`InHypothesis]);
257 MetadataConstraints.at_least ~dbd constraints