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/
30 let nonvar uri = not (UriManager.uri_is_var uri)
32 module Constr = MetadataConstraints
34 exception Goal_is_not_an_equation
37 let debug_print s = if debug then prerr_endline (Lazy.force s)
39 let ( ** ) x y = int_of_float ((float_of_int x) ** (float_of_int y))
41 let signature_of_hypothesis context =
46 | Some (_, Cic.Decl t)
47 | Some (_, Cic.Def (t, _)) ->
48 Constr.UriManagerSet.union set (Constr.constants_of t))
49 Constr.UriManagerSet.empty context
51 let intersect uris siguris =
52 let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
54 List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
56 let inter = Constr.UriManagerSet.inter set1 set2 in
57 List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
61 let profiler = CicUtil.profile "at_most" in
62 fun ~dbd ~where uri -> profiler.profile (Constr.at_most ~dbd ~where) uri
65 let profiler = CicUtil.profile "sigmatch" in
66 fun ~dbd ~facts ~where signature ->
67 profiler.profile (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
69 let at_most = Constr.at_most
70 let sigmatch = MetadataConstraints.sigmatch
72 let filter_uris_forward ~dbd (main, constants) uris =
76 | Some (main, types) -> main :: types
79 List.fold_right Constr.UriManagerSet.add main_uris constants
81 List.filter (at_most ~dbd ~where:`Statement full_signature) uris
83 let filter_uris_backward ~dbd ~facts signature uris =
86 (sigmatch ~dbd ~facts ~where:`Statement signature)
88 intersect uris siguris
90 let compare_goal_list proof goal1 goal2 =
91 let _,metasenv,_,_ = proof in
92 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
93 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
95 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
98 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
102 CicReduction.are_convertible
103 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
110 CicReduction.are_convertible
111 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
118 (* experimental_hint is a version of hint for experimental
119 purposes. It uses auto_tac_verbose instead of auto tac.
120 Auto_tac verbose also returns a substitution - for the moment
121 as a function from cic to cic, to be changed into an association
122 list in the future -. This substitution is used to build a
123 hash table of the inspected goals with their associated proofs.
124 The cose is a cut and paste of the previous one: at the end
125 of the experimentation we shall make a choice. *)
127 let close_with_types s metasenv context =
128 Constr.UriManagerSet.fold
130 let t = CicUtil.term_of_uri e in
132 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
134 Constr.UriManagerSet.union bag (Constr.constants_of ty))
137 let close_with_constructors s metasenv context =
138 Constr.UriManagerSet.fold
140 let t = CicUtil.term_of_uri e in
143 | Cic.MutConstruct (uri,_,_,_) ->
144 (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
145 Cic.InductiveDefinition(tl,_,_,_) ->
148 (fun (i,s) (_,_,_,cl) ->
152 let curi = UriManager.uri_of_uriref uri i (Some j) in
153 j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
160 let apply_tac_verbose =
161 let profiler = CicUtil.profile "apply_tac_verbose" in
162 fun ~term status -> profiler.profile (PrimitiveTactics.apply_tac_verbose ~term) status
165 let profiler = CicUtil.profile "sigmatch" in
166 fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler.profile (Constr.sigmatch ~dbd ~facts ~where) signature
169 let profiler = CicUtil.profile "cmatch'" in
170 fun ~dbd ~facts signature -> profiler.profile (Constr.cmatch' ~dbd ~facts) signature
172 let apply_tac_verbose = PrimitiveTactics.apply_tac_verbose
173 let cmatch' = Constr.cmatch'
175 let signature_of_goal ~(dbd:HMysql.dbd) ((proof, goal) as _status) =
176 let (_, metasenv, _, _) = proof in
177 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
178 let main, sig_constants = Constr.signature_of ty in
179 let set = signature_of_hypothesis context in
184 List.fold_right Constr.UriManagerSet.add (main::l) set in
185 let set = Constr.UriManagerSet.union set sig_constants in
186 let all_constants_closed = close_with_types set metasenv context in
188 sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
189 let uris = List.filter nonvar (List.map snd uris) in
190 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
193 let equations_for_goal ~(dbd:HMysql.dbd) ((proof, goal) as _status) =
194 (* let to_string set =
197 (Constr.UriManagerSet.fold
198 (fun u l -> (UriManager.string_of_uri u)::l) set []))
201 let (_, metasenv, _, _) = proof in
202 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
203 let main, sig_constants = Constr.signature_of ty in
204 (* Printf.printf "\nsig_constants: %s\n\n" (to_string sig_constants); *)
205 (* match main with *)
206 (* None -> raise Goal_is_not_an_equation *)
207 (* | Some (m,l) -> *)
210 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
211 UriManager.uri_of_string (us ^ "#xpointer(1/1)")
215 | Some (m, l) when UriManager.eq m eq_URI -> m, l
216 | Some (m, l) -> eq_URI, []
218 Printf.printf "\nSome (m, l): %s, [%s]\n\n"
219 (UriManager.string_of_uri m)
220 (String.concat "; " (List.map UriManager.string_of_uri l));
221 (* if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then ( *)
222 let set = signature_of_hypothesis context in
223 (* Printf.printf "\nsignature_of_hypothesis: %s\n\n" (to_string set); *)
224 let set = Constr.UriManagerSet.union set sig_constants in
225 let set = close_with_types set metasenv context in
226 (* Printf.printf "\ndopo close_with_types: %s\n\n" (to_string set); *)
227 let set = close_with_constructors set metasenv context in
228 (* Printf.printf "\ndopo close_with_constructors: %s\n\n" (to_string set); *)
229 let set = List.fold_right Constr.UriManagerSet.remove (m::l) set in
231 sigmatch ~dbd ~facts:false ~where:`Statement (main,set) in
232 let uris = List.filter nonvar (List.map snd uris) in
233 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
236 (* else raise Goal_is_not_an_equation *)
238 let experimental_hint
239 ~(dbd:HMysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
240 let (_, metasenv, _, _) = proof in
241 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
242 let (uris, (main, sig_constants)) =
245 (sigmatch ~dbd ~facts signature, signature)
247 (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
249 let uris = List.filter nonvar (List.map snd uris) in
250 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
251 let types_constants =
253 | None -> Constr.UriManagerSet.empty
254 | Some (main, types) ->
255 List.fold_right Constr.UriManagerSet.add (main :: types)
256 Constr.UriManagerSet.empty
260 Constr.UriManagerSet.union
261 (signature_of_hypothesis context)
266 | None -> Constr.UriManagerSet.empty
269 CicTypeChecker.type_of_aux'
270 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
272 Constr.constants_of ty
274 Constr.UriManagerSet.union main hyp_and_sug
276 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
277 let all_constants_closed = close_with_types all_constants metasenv context in
278 let other_constants =
279 Constr.UriManagerSet.diff all_constants_closed types_constants
281 debug_print (lazy "all_constants_closed");
282 if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) all_constants_closed;
283 debug_print (lazy "other_constants");
284 if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) other_constants;
286 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
287 if ((List.length uris < pow) or (pow <= 0))
289 debug_print (lazy "MetadataQuery: large sig, falling back to old method");
290 filter_uris_forward ~dbd (main, other_constants) uris
292 filter_uris_backward ~dbd ~facts (main, other_constants) uris
294 let rec aux = function
299 let (subst,(proof, goal_list)) =
300 (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
302 ~term:(CicUtil.term_of_uri uri)
306 List.stable_sort (compare_goal_list proof) goal_list
308 Some (uri, (subst,(proof, goal_list)))
309 with ProofEngineTypes.Fail _ -> None
313 | Some status' -> status' :: aux tl)
316 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
317 Pervasives.compare (List.length goals1) (List.length goals2))
320 let new_experimental_hint
321 ~(dbd:HMysql.dbd) ?(facts=false) ?signature ~universe
322 ((proof, goal) as status)
324 let (_, metasenv, _, _) = proof in
325 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
326 let (uris, (main, sig_constants)) =
329 (sigmatch ~dbd ~facts signature, signature)
331 (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in
334 (fun res u -> Constr.UriManagerSet.add u res)
335 Constr.UriManagerSet.empty universe in
338 (fun res (_,u) -> Constr.UriManagerSet.add u res)
339 Constr.UriManagerSet.empty uris in
340 let uris = Constr.UriManagerSet.inter uris universe in
341 let uris = Constr.UriManagerSet.elements uris in
342 let rec aux = function
347 let (subst,(proof, goal_list)) =
348 (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
350 ~term:(CicUtil.term_of_uri uri)
354 List.stable_sort (compare_goal_list proof) goal_list
356 Some (uri, (subst,(proof, goal_list)))
357 with ProofEngineTypes.Fail _ -> None
361 | Some status' -> status' :: aux tl)
364 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
365 Pervasives.compare (List.length goals1) (List.length goals2))