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? *)
34 let debug_print = fun _ -> ()
36 (** debugging print *)
39 debug_print ("TACTICALS WARNING: " ^ s)
44 (* not a tactical, but it's used only here (?) *)
47 let id_tac (proof,goal) =
48 let _, metasenv, _, _ = proof in
49 let _, _, _ = CicUtil.lookup_meta goal metasenv in
55 naive implementation of ORELSE tactical, try a sequence of tactics in turn:
56 if one fails pass to the next one and so on, eventually raises (failure "no
58 TODO warning: not tail recursive due to "try .. with" boxing
60 Galla: is this exactly Coq's "First"?
63 let try_tactics ~tactics =
64 let rec try_tactics ~(tactics: (string * tactic) list) status =
65 warn "in Tacticals.try_tactics";
67 | (descr, tac)::tactics ->
68 warn ("Tacticals.try_tactics IS TRYING " ^ descr);
70 let res = apply_tactic tac status in
71 warn ("Tacticals.try_tactics: " ^ descr ^ " succedeed!!!");
77 | (CicTypeChecker.TypeCheckerFailure _)
78 | (CicUnification.UnificationFailure _) ->
80 "Tacticals.try_tactics failed with exn: " ^
81 Printexc.to_string e);
82 try_tactics ~tactics status
83 | _ -> raise e (* [e] must not be caught ; let's re-raise it *)
85 | [] -> raise (Fail "try_tactics: no tactics left")
87 mk_tactic (try_tactics ~tactics)
90 let thens ~start ~continuations =
91 let thens ~start ~continuations status =
92 let (proof,new_goals) = apply_tactic start status in
95 (fun (proof,goals) goal tactic ->
96 let (proof',new_goals') = apply_tactic tactic (proof,goal) in
97 (proof',goals@new_goals')
98 ) (proof,[]) new_goals continuations
100 Invalid_argument _ ->
101 (* FG: more debugging information *)
102 let debug = Printf.sprintf "thens: expected %i new goals, found %i"
103 (List.length continuations) (List.length new_goals)
107 mk_tactic (thens ~start ~continuations )
110 let then_ ~start ~continuation =
111 let then_ ~start ~continuation status =
112 let (proof,new_goals) = apply_tactic start status in
114 (fun (proof,goals) goal ->
115 let (proof',new_goals') = apply_tactic continuation (proof,goal) in
116 (proof',goals@new_goals')
117 ) (proof,[]) new_goals
119 mk_tactic (then_ ~start ~continuation)
121 let rec seq ~tactics =
125 | hd :: tl -> then_ ~start:hd ~continuation:(seq ~tactics:tl)
127 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno applicato la tattica *)
129 (* This keep on appling tactic until it fails *)
130 (* When <tactic> generates more than one goal, you have a tree of
131 application on the tactic, repeat_tactic works in depth on this tree *)
133 let repeat_tactic ~tactic =
134 let rec repeat_tactic ~tactic status =
135 warn "in repeat_tactic";
136 try let (proof, goallist) = apply_tactic tactic status in
137 let rec step proof goallist =
141 let (proof', goallist') = repeat_tactic ~tactic (proof, head) in
142 let (proof'', goallist'') = step proof' tail in
143 proof'', goallist'@goallist''
148 warn ("Tacticals.repeat_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
149 apply_tactic id_tac status
151 mk_tactic (repeat_tactic ~tactic)
156 (* This tries to apply tactic n times *)
157 let do_tactic ~n ~tactic =
158 let rec do_tactic ~n ~tactic status =
161 let (proof, goallist) =
162 if (n>0) then apply_tactic tactic status
163 else apply_tactic id_tac status in
164 (* else (proof, []) in *)
165 (* perche' non va bene questo? stessa questione di ##### ? *)
166 let rec step proof goallist =
170 let (proof', goallist') =
171 do_tactic ~n:(n-1) ~tactic (proof, head) in
172 let (proof'', goallist'') = step proof' tail in
173 proof'', goallist'@goallist''
178 warn ("Tacticals.do_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
179 apply_tactic id_tac status
181 mk_tactic (do_tactic ~n ~tactic)
186 (* This applies tactic and catches its possible failure *)
187 let try_tactic ~tactic =
188 let rec try_tactic ~tactic status =
189 warn "in Tacticals.try_tactic";
191 apply_tactic tactic status
194 warn ( "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e);
195 apply_tactic id_tac status
197 mk_tactic (try_tactic ~tactic)
200 let fail_tac = mk_tactic (fun _ -> raise (Fail "fail tactical"))
202 (* This tries tactics until one of them doesn't _solve_ the goal *)
203 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
204 let solve_tactics ~tactics =
205 let rec solve_tactics ~(tactics: (string * tactic) list) status =
206 warn "in Tacticals.solve_tactics";
208 | (descr, currenttactic)::moretactics ->
209 warn ("Tacticals.solve_tactics is trying " ^ descr);
211 let (proof, goallist) = apply_tactic currenttactic status in
213 [] -> warn ("Tacticals.solve_tactics: " ^ descr ^
214 " solved the goal!!!");
215 (* questo significa che non ci sono piu' goal, o che current_tactic non ne
216 ha aperti di nuovi? (la 2a!) #####
217 nel secondo caso basta per dire che solve_tactics has solved the goal? (si!) *)
219 | _ -> warn ("Tacticals.solve_tactics: try the next tactic");
220 solve_tactics ~tactics:(moretactics) status
223 warn ("Tacticals.solve_tactics: current tactic failed with exn: " ^
224 Printexc.to_string e);
225 solve_tactics ~tactics status
227 | [] -> raise (Fail "solve_tactics cannot solve the goal");
228 apply_tactic id_tac status
230 mk_tactic (solve_tactics ~tactics)