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/.
32 (** perform debugging output? *)
35 (** debugging print *)
38 prerr_endline ("TACTICALS WARNING: " ^ s)
43 (* not a tactical, but it's used only here (?) *)
46 let tac (proof,goal) = (proof,[goal])
52 naive implementation of ORELSE tactical, try a sequence of tactics in turn:
53 if one fails pass to the next one and so on, eventually raises (failure "no
55 TODO warning: not tail recursive due to "try .. with" boxing
57 Galla: is this exactly Coq's "First"?
60 let try_tactics ~tactics =
61 let rec try_tactics ~(tactics: (string * tactic) list) status =
62 warn "in Tacticals.try_tactics";
64 | (descr, tac)::tactics ->
65 warn ("Tacticals.try_tactics IS TRYING " ^ descr);
67 let res = apply_tactic tac status in
68 warn ("Tacticals.try_tactics: " ^ descr ^ " succedeed!!!");
74 | (CicTypeChecker.TypeCheckerFailure _)
75 | (CicUnification.UnificationFailure _) ->
77 "Tacticals.try_tactics failed with exn: " ^
78 Printexc.to_string e);
79 try_tactics ~tactics status
80 | _ -> raise e (* [e] must not be caught ; let's re-raise it *)
82 | [] -> raise (Fail "try_tactics: no tactics left")
84 mk_tactic (try_tactics ~tactics)
87 let thens ~start ~continuations =
88 let thens ~start ~continuations status =
89 let (proof,new_goals) = apply_tactic start status in
92 (fun (proof,goals) goal tactic ->
93 let (proof',new_goals') = apply_tactic tactic (proof,goal) in
94 (proof',goals@new_goals')
95 ) (proof,[]) new_goals continuations
97 Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
99 mk_tactic (thens ~start ~continuations )
102 let then_ ~start ~continuation =
103 let then_ ~start ~continuation status =
104 let (proof,new_goals) = apply_tactic start status in
106 (fun (proof,goals) goal ->
107 let (proof',new_goals') = apply_tactic continuation (proof,goal) in
108 (proof',goals@new_goals')
109 ) (proof,[]) new_goals
111 mk_tactic (then_ ~start ~continuation)
114 (* si suppone che tutte le tattiche sollevino solamente Fail? *)
117 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno applicato la tattica *)
119 (* This keep on appling tactic until it fails *)
120 (* When <tactic> generates more than one goal, you have a tree of
121 application on the tactic, repeat_tactic works in depth on this tree *)
123 let repeat_tactic ~tactic =
124 let rec repeat_tactic ~tactic status =
125 warn "in repeat_tactic";
126 try let (proof, goallist) = apply_tactic tactic status in
127 let rec step proof goallist =
131 let (proof', goallist') = repeat_tactic ~tactic (proof, head) in
132 let (proof'', goallist'') = step proof' tail in
133 proof'', goallist'@goallist''
138 warn ("Tacticals.repeat_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
139 apply_tactic id_tac status
141 mk_tactic (repeat_tactic ~tactic)
146 (* This tries to apply tactic n times *)
147 let do_tactic ~n ~tactic =
148 let rec do_tactic ~n ~tactic status =
151 let (proof, goallist) =
152 if (n>0) then apply_tactic tactic status
153 else apply_tactic id_tac status in
154 (* else (proof, []) in *)
155 (* perche' non va bene questo? stessa questione di ##### ? *)
156 let rec step proof goallist =
160 let (proof', goallist') =
161 do_tactic ~n:(n-1) ~tactic (proof, head) in
162 let (proof'', goallist'') = step proof' tail in
163 proof'', goallist'@goallist''
168 warn ("Tacticals.do_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
169 apply_tactic id_tac status
171 mk_tactic (do_tactic ~n ~tactic)
176 (* This applies tactic and catches its possible failure *)
177 let try_tactic ~tactic =
178 let rec try_tactic ~tactic status =
179 warn "in Tacticals.try_tactic";
181 apply_tactic tactic status
184 warn ( "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e);
185 apply_tactic id_tac status
187 mk_tactic (try_tactic ~tactic)
191 (* This tries tactics until one of them doesn't _solve_ the goal *)
192 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
193 let solve_tactics ~tactics =
194 let rec solve_tactics ~(tactics: (string * tactic) list) status =
195 warn "in Tacticals.solve_tactics";
197 | (descr, currenttactic)::moretactics ->
198 warn ("Tacticals.solve_tactics is trying " ^ descr);
200 let (proof, goallist) = apply_tactic currenttactic status in
202 [] -> warn ("Tacticals.solve_tactics: " ^ descr ^
203 " solved the goal!!!");
204 (* questo significa che non ci sono piu' goal, o che current_tactic non ne
205 ha aperti di nuovi? (la 2a!) #####
206 nel secondo caso basta per dire che solve_tactics has solved the goal? (si!) *)
208 | _ -> warn ("Tacticals.solve_tactics: try the next tactic");
209 solve_tactics ~tactics:(moretactics) status
212 warn ("Tacticals.solve_tactics: current tactic failed with exn: " ^
213 Printexc.to_string e);
214 solve_tactics ~tactics status
216 | [] -> raise (Fail "solve_tactics cannot solve the goal");
217 apply_tactic id_tac status
219 mk_tactic (solve_tactics ~tactics)
231 (** tattica di prova per debuggare i tatticali *)
233 let thens' ~start ~continuations status =
234 let (proof,new_goals) = start status in
237 (fun (proof,goals) goal tactic ->
238 let (proof',new_goals') = tactic (proof,goal) in
239 (proof',goals@new_goals')
240 ) (proof,[]) new_goals continuations
242 Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
245 let apply_T_tac status =
246 let (proof, goal) = status in
247 let curi,metasenv,pbo,pty = proof in
248 let metano,context,gty = CicUtil.lookup_meta goal metasenv in
253 | (Some (Cic.Name name,_))::_ when name = "T" -> n
254 | _::tl -> find (n+1) tl
256 prerr_endline ("eseguo find");
259 prerr_endline ("eseguo apply");
260 apply_tac ~term:(Cic.Rel rel) status
266 ~start:(intros_tac ~name:"pippo")
267 ~continuation:(thens' ~start:apply_T_tac ~continuations:[id_tac ; apply_tac ~term:(Cic.Rel 1)]))