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 metasenv =
44 (fun hyp (set,current_ctx) ->
46 | None -> set, hyp::current_ctx
47 | Some (_, Cic.Decl t) ->
48 Constr.UriManagerSet.union set (Constr.constants_of t),
50 | Some (_, Cic.Def (t, _)) ->
53 CicTypeChecker.type_of_aux'
54 metasenv current_ctx t CicUniv.empty_ugraph
57 CicTypeChecker.type_of_aux'
58 metasenv current_ctx ty CicUniv.empty_ugraph
60 let set = Constr.UriManagerSet.union set(Constr.constants_of ty)in
62 | Cic.Sort Cic.Prop -> set, hyp::current_ctx
63 | _ -> Constr.UriManagerSet.union set (Constr.constants_of t),
66 | CicTypeChecker.TypeCheckerFailure _ -> set, hyp::current_ctx)
67 context (Constr.UriManagerSet.empty,[])
72 let intersect uris siguris =
73 let set1 = List.fold_right Constr.UriManagerSet.add uris Constr.UriManagerSet.empty in
75 List.fold_right Constr.UriManagerSet.add siguris Constr.UriManagerSet.empty
77 let inter = Constr.UriManagerSet.inter set1 set2 in
78 List.filter (fun s -> Constr.UriManagerSet.mem s inter) uris
82 let profiler = CicUtil.profile "at_most" in
83 fun ~dbd ~where uri -> profiler.profile (Constr.at_most ~dbd ~where) uri
86 let profiler = CicUtil.profile "sigmatch" in
87 fun ~dbd ~facts ~where signature ->
88 profiler.profile (MetadataConstraints.sigmatch ~dbd ~facts ~where) signature
90 let at_most = Constr.at_most
91 let sigmatch = MetadataConstraints.sigmatch
93 let filter_uris_forward ~dbd (main, constants) uris =
97 | Some (main, types) -> main :: types
100 List.fold_right Constr.UriManagerSet.add main_uris constants
102 List.filter (at_most ~dbd ~where:`Statement full_signature) uris
104 let filter_uris_backward ~dbd ~facts signature uris =
107 (sigmatch ~dbd ~facts ~where:`Statement signature)
109 intersect uris siguris
111 let compare_goal_list proof goal1 goal2 =
112 let _,metasenv,_,_ = proof in
113 let (_, ey1, ty1) = CicUtil.lookup_meta goal1 metasenv in
114 let (_, ey2, ty2) = CicUtil.lookup_meta goal2 metasenv in
116 CicTypeChecker.type_of_aux' metasenv ey1 ty1 CicUniv.empty_ugraph
119 CicTypeChecker.type_of_aux' metasenv ey2 ty2 CicUniv.empty_ugraph
123 CicReduction.are_convertible
124 ey1 (Cic.Sort Cic.Prop) ty_sort1 CicUniv.empty_ugraph
131 CicReduction.are_convertible
132 ey2 (Cic.Sort Cic.Prop) ty_sort2 CicUniv.empty_ugraph
139 (* experimental_hint is a version of hint for experimental
140 purposes. It uses auto_tac_verbose instead of auto tac.
141 Auto_tac verbose also returns a substitution - for the moment
142 as a function from cic to cic, to be changed into an association
143 list in the future -. This substitution is used to build a
144 hash table of the inspected goals with their associated proofs.
145 The cose is a cut and paste of the previous one: at the end
146 of the experimentation we shall make a choice. *)
148 let close_with_types s metasenv context =
149 Constr.UriManagerSet.fold
151 let t = CicUtil.term_of_uri e in
153 CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph
155 Constr.UriManagerSet.union bag (Constr.constants_of ty))
158 let close_with_constructors s metasenv context =
159 Constr.UriManagerSet.fold
161 let t = CicUtil.term_of_uri e in
164 | Cic.MutConstruct (uri,_,_,_) ->
165 (match fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
166 Cic.InductiveDefinition(tl,_,_,_) ->
169 (fun (i,s) (_,_,_,cl) ->
173 let curi = UriManager.uri_of_uriref uri i (Some j) in
174 j+1,Constr.UriManagerSet.add curi s) (1,s) cl in
181 let apply_tac_verbose =
182 let profiler = CicUtil.profile "apply_tac_verbose" in
183 fun ~term status -> profiler.profile (PrimitiveTactics.apply_tac_verbose ~term) status
186 let profiler = CicUtil.profile "sigmatch" in
187 fun ~dbd ~facts ?(where=`Conclusion) signature -> profiler.profile (Constr.sigmatch ~dbd ~facts ~where) signature
190 let profiler = CicUtil.profile "cmatch'" in
191 fun ~dbd ~facts signature -> profiler.profile (Constr.cmatch' ~dbd ~facts) signature
193 let apply_tac_verbose = PrimitiveTactics.apply_tac_verbose
194 let cmatch' = Constr.cmatch'
196 (* used only by te old auto *)
197 let signature_of_goal ~(dbd:HMysql.dbd) ((proof, goal) as _status) =
198 let (_, metasenv, _, _) = proof in
199 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
200 let main, sig_constants = Constr.signature_of ty in
201 let set = signature_of_hypothesis context metasenv in
206 List.fold_right Constr.UriManagerSet.add (main::l) set in
207 let set = Constr.UriManagerSet.union set sig_constants in
208 let all_constants_closed = close_with_types set metasenv context in
210 sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed) in
211 let uris = List.filter nonvar (List.map snd uris) in
212 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
217 try CicTypeChecker.type_of_aux' [] []
218 (CicUtil.term_of_uri u) CicUniv.empty_ugraph
219 with CicTypeChecker.TypeCheckerFailure _ -> assert false
221 let rec check_last_pi = function
222 | Cic.Prod (_,_,tgt) -> check_last_pi tgt
223 | Cic.Sort Cic.Prop -> true
229 let only constants uri =
230 prerr_endline (UriManager.string_of_uri uri);
231 let t = CicUtil.term_of_uri uri in (* FIXME: write ty_of_term *)
232 let ty,_ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
233 let consts = Constr.constants_of ty in
234 (* prerr_endline ("XXX " ^ UriManager.string_of_uri uri);*)
235 (* Constr.UriManagerSet.iter (fun u -> prerr_endline (" - " ^*)
236 (* UriManager.string_of_uri u)) consts;*)
237 (* Constr.UriManagerSet.iter (fun u -> prerr_endline (" + " ^*)
238 (* UriManager.string_of_uri u)) constants;*)
239 Constr.UriManagerSet.subset consts constants
242 let universe_of_goals ~(dbd:HMysql.dbd) proof goals =
243 let (_, metasenv, _, _) = proof in
244 let add_uris_for acc goal =
245 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
246 let main, sig_constants = Constr.signature_of ty in
247 let set = signature_of_hypothesis context metasenv in
252 List.fold_right Constr.UriManagerSet.add (main::l) set in
253 let set = Constr.UriManagerSet.union set sig_constants in
254 let all_constants_closed = close_with_types set metasenv context in
255 Constr.UriManagerSet.union all_constants_closed acc
257 let all_constants_closed =
258 List.fold_left add_uris_for Constr.UriManagerSet.empty goals in
259 (* we split predicates from the rest *)
260 let predicates, rest =
261 Constr.UriManagerSet.partition is_predicate all_constants_closed
263 prerr_endline ("FOO!1");
265 Constr.UriManagerSet.fold
268 sigmatch ~dbd ~facts:false ~where:`Statement
269 (Some (u,[]),all_constants_closed)
274 prerr_endline ("FOO!2");
277 sigmatch ~dbd ~facts:false ~where:`Statement (None,all_constants_closed)
280 let uris = List.filter nonvar (List.map snd uris) in
281 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
282 let uris = List.filter (only all_constants_closed) uris in
286 let filter_out_predicate set ctx menv =
287 Constr.UriManagerSet.filter (fun u -> not (is_predicate u)) set
290 let equations_for_goal ~(dbd:HMysql.dbd) ?signature ((proof, goal) as _status) =
295 (Constr.UriManagerSet.fold
296 (fun u l -> (" "^UriManager.string_of_uri u)::l) set []))
300 let (_, metasenv, _, _) = proof in
301 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
302 let main, sig_constants =
304 | None -> Constr.signature_of ty
307 (* Printf.printf "\nsig_constants: %s\n\n" (to_string sig_constants); *)
308 (* match main with *)
309 (* None -> raise Goal_is_not_an_equation *)
310 (* | Some (m,l) -> *)
313 match LibraryObjects.eq_URI () with
317 (UriManager.uri_of_string
318 (UriManager.string_of_uri s ^ "#xpointer(1/1)"))
320 match eq_URI,main with
321 | Some eq_URI, Some (m, l) when UriManager.eq m eq_URI -> m::l
324 (*Printf.printf "\nSome (m, l): %s, [%s]\n\n"
325 (UriManager.string_of_uri (List.hd l))
326 (String.concat "; " (List.map UriManager.string_of_uri (List.tl l)));
328 (* if m == UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI then ( *)
329 let set = signature_of_hypothesis context metasenv in
330 (* Printf.printf "\nsignature_of_hypothesis: %s\n\n" (to_string set); *)
331 let set = Constr.UriManagerSet.union set sig_constants in
332 let set = filter_out_predicate set context metasenv in
333 let set = close_with_types set metasenv context in
334 (* Printf.printf "\ndopo close_with_types: %s\n\n" (to_string set); *)
335 let set = close_with_constructors set metasenv context in
336 (* Printf.printf "\ndopo close_with_constructors: %s\n\n" (to_string set); *)
337 let set_for_sigmatch = List.fold_right Constr.UriManagerSet.remove l set in
339 sigmatch ~dbd ~facts:false ~where:`Statement (main,set_for_sigmatch) in
340 let uris = List.filter nonvar (List.map snd uris) in
341 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
342 let set = List.fold_right Constr.UriManagerSet.add l set in
343 let uris = List.filter (only set) uris in
346 (* else raise Goal_is_not_an_equation *)
348 let experimental_hint
349 ~(dbd:HMysql.dbd) ?(facts=false) ?signature ((proof, goal) as status) =
350 let (_, metasenv, _, _) = proof in
351 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
352 let (uris, (main, sig_constants)) =
355 (sigmatch ~dbd ~facts signature, signature)
357 (cmatch' ~dbd ~facts ty, Constr.signature_of ty)
359 let uris = List.filter nonvar (List.map snd uris) in
360 let uris = List.filter Hashtbl_equiv.not_a_duplicate uris in
361 let types_constants =
363 | None -> Constr.UriManagerSet.empty
364 | Some (main, types) ->
365 List.fold_right Constr.UriManagerSet.add (main :: types)
366 Constr.UriManagerSet.empty
370 Constr.UriManagerSet.union
371 (signature_of_hypothesis context metasenv)
376 | None -> Constr.UriManagerSet.empty
379 CicTypeChecker.type_of_aux'
380 metasenv context (CicUtil.term_of_uri main) CicUniv.empty_ugraph
382 Constr.constants_of ty
384 Constr.UriManagerSet.union main hyp_and_sug
386 (* Constr.UriManagerSet.iter debug_print hyp_constants; *)
387 let all_constants_closed = close_with_types all_constants metasenv context in
388 let other_constants =
389 Constr.UriManagerSet.diff all_constants_closed types_constants
391 debug_print (lazy "all_constants_closed");
392 if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) all_constants_closed;
393 debug_print (lazy "other_constants");
394 if debug then Constr.UriManagerSet.iter (fun s -> debug_print (lazy (UriManager.string_of_uri s))) other_constants;
396 let pow = 2 ** (Constr.UriManagerSet.cardinal other_constants) in
397 if ((List.length uris < pow) or (pow <= 0))
399 debug_print (lazy "MetadataQuery: large sig, falling back to old method");
400 filter_uris_forward ~dbd (main, other_constants) uris
402 filter_uris_backward ~dbd ~facts (main, other_constants) uris
404 let rec aux = function
409 let (subst,(proof, goal_list)) =
410 (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
412 ~term:(CicUtil.term_of_uri uri)
416 List.stable_sort (compare_goal_list proof) goal_list
418 Some (uri, (subst,(proof, goal_list)))
419 with ProofEngineTypes.Fail _ -> None
423 | Some status' -> status' :: aux tl)
426 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
427 Pervasives.compare (List.length goals1) (List.length goals2))
430 let new_experimental_hint
431 ~(dbd:HMysql.dbd) ?(facts=false) ?signature ~universe
432 ((proof, goal) as status)
434 let (_, metasenv, _, _) = proof in
435 let (_, context, ty) = CicUtil.lookup_meta goal metasenv in
436 let (uris, (main, sig_constants)) =
439 (sigmatch ~dbd ~facts signature, signature)
441 (cmatch' ~dbd ~facts ty, Constr.signature_of ty) in
444 (fun res u -> Constr.UriManagerSet.add u res)
445 Constr.UriManagerSet.empty universe in
448 (fun res (_,u) -> Constr.UriManagerSet.add u res)
449 Constr.UriManagerSet.empty uris in
450 let uris = Constr.UriManagerSet.inter uris universe in
451 let uris = Constr.UriManagerSet.elements uris in
452 let rec aux = function
457 let (subst,(proof, goal_list)) =
458 (* debug_print (lazy ("STO APPLICANDO" ^ uri)); *)
460 ~term:(CicUtil.term_of_uri uri)
464 List.stable_sort (compare_goal_list proof) goal_list
466 Some (uri, (subst,(proof, goal_list)))
467 with ProofEngineTypes.Fail _ -> None
471 | Some status' -> status' :: aux tl)
474 (fun (_,(_, (_, goals1))) (_,(_, (_, goals2))) ->
475 Pervasives.compare (List.length goals1) (List.length goals2))