]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/tacticals.ml
A few other tactics made available to matita.
[helm.git] / helm / ocaml / tactics / tacticals.ml
1 (* Copyright (C) 2002, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 open CicReduction
27 open ProofEngineTypes
28 open UriManager
29
30 (** DEBUGGING *)
31
32   (** perform debugging output? *)
33 let debug = false
34 let debug_print = fun _ -> ()
35
36   (** debugging print *)
37 let warn s =
38   if debug then
39     debug_print ("TACTICALS WARNING: " ^ s)
40
41
42 (** TACTIC{,AL}S *)
43
44   (* not a tactical, but it's used only here (?) *)
45
46 let id_tac = 
47  let tac (proof,goal) = (proof,[goal])
48  in 
49   mk_tactic tac
50
51   (**
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
54     tactics left")
55     TODO warning: not tail recursive due to "try .. with" boxing
56
57     Galla: is this exactly Coq's "First"?
58
59   *)
60 let try_tactics ~tactics =
61  let rec try_tactics ~(tactics: (string * tactic) list) status =
62   warn "in Tacticals.try_tactics";
63   match tactics with
64   | (descr, tac)::tactics ->
65       warn ("Tacticals.try_tactics IS TRYING " ^ descr);
66       (try
67         let res = apply_tactic tac status in
68         warn ("Tacticals.try_tactics: " ^ descr ^ " succedeed!!!");
69         res
70        with
71         e ->
72          match e with
73             (Fail _)
74           | (CicTypeChecker.TypeCheckerFailure _)
75           | (CicUnification.UnificationFailure _) ->
76               warn (
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 *)
81       )
82   | [] -> raise (Fail "try_tactics: no tactics left")
83  in
84   mk_tactic (try_tactics ~tactics)
85
86
87 let thens ~start ~continuations =
88  let thens ~start ~continuations status =
89  let (proof,new_goals) = apply_tactic start status in
90   try
91    List.fold_left2
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
96   with
97    Invalid_argument _ -> 
98 (* FG: more debugging information *)
99     let debug = Printf.sprintf "thens: expected %i new goals, found %i"
100      (List.length continuations) (List.length new_goals)
101     in
102     raise (Fail debug)
103  in
104   mk_tactic (thens ~start ~continuations )
105
106
107 let then_ ~start ~continuation =
108  let then_ ~start ~continuation status =
109  let (proof,new_goals) = apply_tactic start status in
110   List.fold_left
111    (fun (proof,goals) goal ->
112      let (proof',new_goals') = apply_tactic continuation (proof,goal) in
113       (proof',goals@new_goals')
114    ) (proof,[]) new_goals
115  in
116   mk_tactic (then_ ~start ~continuation)
117
118 let rec seq ~tactics =
119   match tactics with
120   | [] -> assert false
121   | [tac] -> tac
122   | hd :: tl -> then_ ~start:hd ~continuation:(seq ~tactics:tl)
123
124 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno applicato la tattica *)
125
126 (* This keep on appling tactic until it fails *)
127 (* When <tactic> generates more than one goal, you have a tree of
128    application on the tactic, repeat_tactic works in depth on this tree *)
129
130 let repeat_tactic ~tactic =
131  let rec repeat_tactic ~tactic status =
132   warn "in repeat_tactic";
133   try let (proof, goallist) = apply_tactic tactic status in
134    let rec step proof goallist =
135     match goallist with
136        [] -> (proof, [])
137      | head::tail -> 
138         let (proof', goallist') = repeat_tactic ~tactic (proof, head) in
139          let (proof'', goallist'') = step proof' tail in
140           proof'', goallist'@goallist''
141    in
142     step proof goallist
143   with 
144    (Fail _) as e ->
145     warn ("Tacticals.repeat_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
146     apply_tactic id_tac status
147  in 
148   mk_tactic (repeat_tactic ~tactic)
149 ;;
150
151
152
153 (* This tries to apply tactic n times *)
154 let do_tactic ~n ~tactic =
155  let rec do_tactic ~n ~tactic status =
156   warn "in do_tactic";
157   try 
158    let (proof, goallist) = 
159     if (n>0) then apply_tactic tactic status 
160              else apply_tactic id_tac status in
161 (*             else (proof, []) in *)
162 (* perche' non va bene questo? stessa questione di ##### ? *)
163    let rec step proof goallist =
164     match goallist with
165        [] -> (proof, [])
166      | head::tail -> 
167         let (proof', goallist') = 
168          do_tactic ~n:(n-1) ~tactic (proof, head) in
169         let (proof'', goallist'') = step proof' tail in
170          proof'', goallist'@goallist''
171    in
172     step proof goallist
173   with 
174    (Fail _) as e ->
175     warn ("Tacticals.do_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
176     apply_tactic id_tac status
177  in
178   mk_tactic (do_tactic ~n ~tactic)
179 ;;
180
181
182
183 (* This applies tactic and catches its possible failure *)
184 let try_tactic ~tactic =
185  let rec try_tactic ~tactic status =
186   warn "in Tacticals.try_tactic";
187   try
188    apply_tactic tactic status
189   with
190    (Fail _) as e -> 
191     warn ( "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e);
192     apply_tactic id_tac status
193  in
194   mk_tactic (try_tactic ~tactic)
195 ;;
196
197 let fail_tac = mk_tactic (fun _ -> raise (Fail "fail tactical"))
198
199 (* This tries tactics until one of them doesn't _solve_ the goal *)
200 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
201 let solve_tactics ~tactics =
202  let rec solve_tactics ~(tactics: (string * tactic) list) status =
203   warn "in Tacticals.solve_tactics";
204   match tactics with
205   | (descr, currenttactic)::moretactics ->
206       warn ("Tacticals.solve_tactics is trying " ^ descr);
207       (try
208         let (proof, goallist) = apply_tactic currenttactic status in
209          match goallist with 
210             [] -> warn ("Tacticals.solve_tactics: " ^ descr ^ 
211                    " solved the goal!!!");
212 (* questo significa che non ci sono piu' goal, o che current_tactic non ne 
213    ha aperti di nuovi? (la 2a!) ##### 
214    nel secondo caso basta per dire che solve_tactics has solved the goal? (si!) *)
215                   (proof, goallist)
216           | _ -> warn ("Tacticals.solve_tactics: try the next tactic");
217                  solve_tactics ~tactics:(moretactics) status
218        with
219         (Fail _) as e ->
220          warn ("Tacticals.solve_tactics: current tactic failed with exn: " ^ 
221           Printexc.to_string e);
222          solve_tactics ~tactics status
223       )
224   | [] -> raise (Fail "solve_tactics cannot solve the goal");
225           apply_tactic id_tac status
226  in
227   mk_tactic (solve_tactics ~tactics)
228 ;;