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])
51 naive implementation of ORELSE tactical, try a sequence of tactics in turn:
52 if one fails pass to the next one and so on, eventually raises (failure "no
54 TODO warning: not tail recursive due to "try .. with" boxing
56 Galla: is this exactly Coq's "First"?
59 let try_tactics ~tactics =
60 let rec try_tactics ~(tactics: (string * tactic) list) status =
61 warn "in Tacticals.try_tactics";
63 | (descr, tac)::tactics ->
64 warn ("Tacticals.try_tactics IS TRYING " ^ descr);
66 let res = apply_tactic tac status in
67 warn ("Tacticals.try_tactics: " ^ descr ^ " succedeed!!!");
73 | (CicTypeChecker.TypeCheckerFailure _)
74 | (CicUnification.UnificationFailure _) ->
76 "Tacticals.try_tactics failed with exn: " ^
77 Printexc.to_string e);
78 try_tactics ~tactics status
79 | _ -> raise e (* [e] must not be caught ; let's re-raise it *)
81 | [] -> raise (Fail "try_tactics: no tactics left")
83 mk_tactic (try_tactics ~tactics)
86 let thens ~start ~continuations =
87 let thens ~start ~continuations status =
88 let (proof,new_goals) = apply_tactic start status in
91 (fun (proof,goals) goal tactic ->
92 let (proof',new_goals') = apply_tactic tactic (proof,goal) in
93 (proof',goals@new_goals')
94 ) (proof,[]) new_goals continuations
96 Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
98 mk_tactic (thens ~start ~continuations )
101 let then_ ~start ~continuation =
102 let then_ ~start ~continuation status =
103 let (proof,new_goals) = apply_tactic start status in
105 (fun (proof,goals) goal ->
106 let (proof',new_goals') = apply_tactic continuation (proof,goal) in
107 (proof',goals@new_goals')
108 ) (proof,[]) new_goals
110 mk_tactic (then_ ~start ~continuation)
112 let rec seq ~tactics =
116 | hd :: tl -> then_ ~start:hd ~continuation:(seq ~tactics:tl)
118 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno applicato la tattica *)
120 (* This keep on appling tactic until it fails *)
121 (* When <tactic> generates more than one goal, you have a tree of
122 application on the tactic, repeat_tactic works in depth on this tree *)
124 let repeat_tactic ~tactic =
125 let rec repeat_tactic ~tactic status =
126 warn "in repeat_tactic";
127 try let (proof, goallist) = apply_tactic tactic status in
128 let rec step proof goallist =
132 let (proof', goallist') = repeat_tactic ~tactic (proof, head) in
133 let (proof'', goallist'') = step proof' tail in
134 proof'', goallist'@goallist''
139 warn ("Tacticals.repeat_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
140 apply_tactic id_tac status
142 mk_tactic (repeat_tactic ~tactic)
147 (* This tries to apply tactic n times *)
148 let do_tactic ~n ~tactic =
149 let rec do_tactic ~n ~tactic status =
152 let (proof, goallist) =
153 if (n>0) then apply_tactic tactic status
154 else apply_tactic id_tac status in
155 (* else (proof, []) in *)
156 (* perche' non va bene questo? stessa questione di ##### ? *)
157 let rec step proof goallist =
161 let (proof', goallist') =
162 do_tactic ~n:(n-1) ~tactic (proof, head) in
163 let (proof'', goallist'') = step proof' tail in
164 proof'', goallist'@goallist''
169 warn ("Tacticals.do_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
170 apply_tactic id_tac status
172 mk_tactic (do_tactic ~n ~tactic)
177 (* This applies tactic and catches its possible failure *)
178 let try_tactic ~tactic =
179 let rec try_tactic ~tactic status =
180 warn "in Tacticals.try_tactic";
182 apply_tactic tactic status
185 warn ( "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e);
186 apply_tactic id_tac status
188 mk_tactic (try_tactic ~tactic)
191 let fail = mk_tactic (fun _ -> raise (Fail "fail tactical"))
193 (* This tries tactics until one of them doesn't _solve_ the goal *)
194 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
195 let solve_tactics ~tactics =
196 let rec solve_tactics ~(tactics: (string * tactic) list) status =
197 warn "in Tacticals.solve_tactics";
199 | (descr, currenttactic)::moretactics ->
200 warn ("Tacticals.solve_tactics is trying " ^ descr);
202 let (proof, goallist) = apply_tactic currenttactic status in
204 [] -> warn ("Tacticals.solve_tactics: " ^ descr ^
205 " solved the goal!!!");
206 (* questo significa che non ci sono piu' goal, o che current_tactic non ne
207 ha aperti di nuovi? (la 2a!) #####
208 nel secondo caso basta per dire che solve_tactics has solved the goal? (si!) *)
210 | _ -> warn ("Tacticals.solve_tactics: try the next tactic");
211 solve_tactics ~tactics:(moretactics) status
214 warn ("Tacticals.solve_tactics: current tactic failed with exn: " ^
215 Printexc.to_string e);
216 solve_tactics ~tactics status
218 | [] -> raise (Fail "solve_tactics cannot solve the goal");
219 apply_tactic id_tac status
221 mk_tactic (solve_tactics ~tactics)
233 (** tattica di prova per debuggare i tatticali *)
235 let thens' ~start ~continuations status =
236 let (proof,new_goals) = start status in
239 (fun (proof,goals) goal tactic ->
240 let (proof',new_goals') = tactic (proof,goal) in
241 (proof',goals@new_goals')
242 ) (proof,[]) new_goals continuations
244 Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
247 let apply_T_tac status =
248 let (proof, goal) = status in
249 let curi,metasenv,pbo,pty = proof in
250 let metano,context,gty = CicUtil.lookup_meta goal metasenv in
255 | (Some (Cic.Name name,_))::_ when name = "T" -> n
256 | _::tl -> find (n+1) tl
258 prerr_endline ("eseguo find");
261 prerr_endline ("eseguo apply");
262 apply_tac ~term:(Cic.Rel rel) status
268 ~start:(intros_tac ~name:"pippo")
269 ~continuation:(thens' ~start:apply_T_tac ~continuations:[id_tac ; apply_tac ~term:(Cic.Rel 1)]))