1 (* Copyright (C) 2002, 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://cs.unibo.it/helm/.
27 (* Da rimuovere, solo per debug*)
28 let print_context ctx =
32 | Cic.Anonymous -> "_"
35 (fun i (output,context) ->
36 let (newoutput,context') =
38 Some (n,Cic.Decl t) ->
39 print_name n ^ ":" ^ CicPp.pp t context ^ "\n", (Some n)::context
40 | Some (n,Cic.Def (t,None)) ->
41 print_name n ^ ":=" ^ CicPp.pp t context ^ "\n", (Some n)::context
43 "_ ?= _\n", None::context
44 | Some (_,Cic.Def (_,Some _)) -> assert false
46 output^newoutput,context'
54 let search_theorems_in_context status =
55 let (proof, goal) = status in
57 let module R = CicReduction in
58 let module S = CicSubstitution in
59 prerr_endline "Entro in search_context";
60 let _,metasenv,_,_ = proof in
61 let _,context,ty = CicUtil.lookup_meta goal metasenv in
62 let rec find n = function
67 Some (PrimitiveTactics.apply_tac status ~term:(C.Rel n))
69 ProofEngineTypes.Fail _ -> None in
71 Some res -> res::(find (n+1) tl)
72 | None -> find (n+1) tl)
75 let res = find 1 context in
76 prerr_endline "Ho finito context";
79 prerr_endline ("SIAM QUI = " ^ s); []
82 exception NotAProposition;;
83 exception NotApplicableTheorem;;
89 let rec auto_tac_aux mqi_handle level proof goal =
90 prerr_endline ("Entro in Auto_rec; level = " ^ (string_of_int level));
93 (prerr_endline ("MaxDepth");
96 (* let us verify that the metavariable is still an open goal --
97 it could have been closed by closing other goals -- and that
99 let _,metasenv,_,_ = proof in
102 let (_, ey ,ty) = CicUtil.lookup_meta goal metasenv in
107 prerr_endline ("CURRENT GOAL = " ^ (CicPp.ppterm ty));
108 prerr_endline ("CURRENT HYP = " ^ (fst (print_context ey)));
110 let time1 = Unix.gettimeofday() in
111 let _, all_paths = NewConstraints.prefixes 5 ty in
112 let time2 = Unix.gettimeofday() in
114 (Printf.sprintf "TEMPO DI CALCOLO = %1.3f" (time2 -. time1) );
116 ("ALL PATHS: n = " ^ string_of_int
117 (List.length all_paths));
118 prerr_endline (NewConstraints.pp_prefixes all_paths);
120 (* if the goal does not have a sort Prop we return the
121 current proof and a list containing the goal *)
122 let ty_sort = CicTypeChecker.type_of_aux' metasenv ey ty in
123 if CicReduction.are_convertible
124 ey (Cic.Sort Cic.Prop) ty_sort then
126 (* choices is a list of pairs proof and goallist *)
128 (search_theorems_in_context (proof,goal))@
129 (TacticChaser.searchTheorems mqi_handle (proof,goal))
131 let rec try_choices = function
132 [] -> raise NotApplicableTheorem
133 | (proof,goallist)::tl ->
134 prerr_endline ("GOALLIST = " ^ string_of_int (List.length goallist));
138 auto_tac_aux mqi_handle (level-1) proof goal)
142 | NotApplicableTheorem
147 (* CUT AND PASTE DI PROVA !! *)
149 (search_theorems_in_context (proof,goal))@
150 (TacticChaser.searchTheorems mqi_handle (proof,goal))
152 let rec try_choices = function
153 [] -> raise NotApplicableTheorem
154 | (proof,[])::tl -> proof
155 | _::tl -> try_choices tl in
157 (* raise NotAProposition *)
161 let auto_tac mqi_handle (proof,goal) =
162 prerr_endline "Entro in Auto";
164 let proof = auto_tac_aux mqi_handle depth proof goal in
165 prerr_endline "AUTO_TAC HA FINITO";
168 | MaxDepth -> assert false (* this should happens only if depth is 0 above *)
169 | NotApplicableTheorem ->
170 prerr_endline("No applicable theorem");
171 raise (ProofEngineTypes.Fail "No Applicable theorem");;
174 (**** ESPERIMENTO ************************)
176 let new_search_theorems f proof goal depth gtl =
177 let local_choices = f (proof,goal)
180 (function (proof, goallist) ->
181 (proof, (List.map (function g -> (g,depth)) goallist)@gtl))
185 exception NoOtherChoices;;
187 let rec auto_new mqi_handle = function
188 [] -> raise NoOtherChoices
189 | (proof, [])::tl -> (proof, [])::tl
190 | (proof, (goal,0)::gtl)::tl -> auto_new mqi_handle tl
191 | (proof, (goal,depth)::gtl)::tl ->
192 let _,metasenv,_,_ = proof in
195 let (_, ey ,ty) = CicUtil.lookup_meta goal metasenv in
202 search_theorems_in_context proof goal (depth-1) gtl in
205 (TacticChaser.searchTheorems mqi_handle)
206 proof goal (depth-1) gtl in
208 local_choices@global_choices@tl in
209 let reorder = all_choices in
210 auto_new mqi_handle reorder
211 | None -> auto_new mqi_handle ((proof,gtl)::tl)
215 let auto_tac mqi_handle (proof,goal) =
216 prerr_endline "Entro in Auto";
218 let (proof,_)::_ = auto_new mqi_handle [(proof, [(goal,depth)])] in
219 prerr_endline "AUTO_TAC HA FINITO";
223 prerr_endline("Auto failed");
224 raise (ProofEngineTypes.Fail "No Applicable theorem");;
226 (* TODO se ce n'e' piu' di una, prende la prima che trova... sarebbe meglio
227 chiedere: find dovrebbe restituire una lista di hyp (?) da passare all'utonto con una
228 funzione di callback che restituisce la (sola) hyp da applicare *)
230 let assumption_tac status =
231 let (proof, goal) = status in
232 let module C = Cic in
233 let module R = CicReduction in
234 let module S = CicSubstitution in
235 let _,metasenv,_,_ = proof in
236 let _,context,ty = CicUtil.lookup_meta goal metasenv in
237 let rec find n = function
240 (Some (_, C.Decl t)) when
241 (R.are_convertible context (S.lift n t) ty) -> n
242 | (Some (_, C.Def (_,Some ty'))) when
243 (R.are_convertible context ty' ty) -> n
244 | (Some (_, C.Def (t,None))) when
245 (R.are_convertible context
246 (CicTypeChecker.type_of_aux' metasenv context (S.lift n t)) ty) -> n
249 | [] -> raise (ProofEngineTypes.Fail "Assumption: No such assumption")
250 in PrimitiveTactics.apply_tac status ~term:(C.Rel (find 1 context))
253 (* ANCORA DA DEBUGGARE *)
255 exception AllSelectedTermsMustBeConvertible;;
257 (* serve una funzione che cerchi nel ty dal basso a partire da term, i lambda
258 e li aggiunga nel context, poi si conta la lunghezza di questo nuovo
259 contesto e si lifta di tot... COSA SIGNIFICA TUTTO CIO'?????? *)
262 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
265 let (proof, goal) = status in
266 let module C = Cic in
267 let module P = PrimitiveTactics in
268 let module T = Tacticals in
269 let _,metasenv,_,_ = proof in
270 let _,context,ty = CicUtil.lookup_meta goal metasenv in
275 (* We need to check that all the convertibility of all the terms *)
278 if not (CicReduction.are_convertible context he t) then
279 raise AllSelectedTermsMustBeConvertible
281 (CicTypeChecker.type_of_aux' metasenv context he)
287 (mk_fresh_name_callback metasenv context C.Anonymous typ),
289 (ProofEngineReduction.replace_lifting_csc 1
292 ~with_what:(List.map (function _ -> C.Rel 1) terms)
295 ~continuations: [(P.apply_tac ~term:(C.Rel 1)) ; T.id_tac]