]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/tacticals.ml
8d4eb891e049a8e0601daabf7d8289898f20bf02
[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
35   (** debugging print *)
36 let warn s =
37   if debug then
38     prerr_endline ("TACTICALS WARNING: " ^ s)
39
40
41 (** TACTIC{,AL}S *)
42
43   (* not a tactical, but it's used only here (?) *)
44
45 let id_tac (proof,goal) = (proof,[goal])
46
47
48   (**
49     naive implementation of ORELSE tactical, try a sequence of tactics in turn:
50     if one fails pass to the next one and so on, eventually raises (failure "no
51     tactics left")
52     TODO warning: not tail recursive due to "try .. with" boxing
53
54     Galla: is this exactly Coq's "First"?
55
56   *)
57 let rec try_tactics ~(tactics: (string * tactic) list) status =
58   warn "in Tacticals.try_tactics";
59   match tactics with
60   | (descr, tac)::tactics ->
61       warn ("Tacticals.try_tactics IS TRYING " ^ descr);
62       (try
63         let res = tac status in
64         warn ("Tacticals.try_tactics: " ^ descr ^ " succedeed!!!");
65         res
66        with
67         e ->
68          match e with
69             (Fail _)
70           | (CicTypeChecker.TypeCheckerFailure _)
71           | (CicUnification.UnificationFailure _) ->
72               warn (
73                 "Tacticals.try_tactics failed with exn: " ^
74                 Printexc.to_string e);
75               try_tactics ~tactics status
76         | _ -> raise e (* [e] must not be caught ; let's re-raise it *)
77       )
78   | [] -> raise (Fail "try_tactics: no tactics left")
79
80
81
82 let thens ~start ~continuations status =
83  let (proof,new_goals) = start status in
84   try
85    List.fold_left2
86     (fun (proof,goals) goal tactic ->
87       let (proof',new_goals') = tactic (proof,goal) in
88        (proof',goals@new_goals')
89     ) (proof,[]) new_goals continuations
90   with
91    Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
92
93
94
95 let then_ ~start ~continuation status =
96  let (proof,new_goals) = start status in
97   List.fold_left
98    (fun (proof,goals) goal ->
99      let (proof',new_goals') = continuation (proof,goal) in
100       (proof',goals@new_goals')
101    ) (proof,[]) new_goals
102
103
104 (* Galla *)
105 (* si suppone che tutte le tattiche sollevino solamente Fail? *)
106
107
108 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno applicato la tattica *)
109
110 (* This keep on appling tactic until it fails *)
111 (* When <tactic> generates more than one goal, you have a tree of
112    application on the tactic, repeat_tactic works in depth on this tree *)
113
114 let rec repeat_tactic ~tactic status =
115   warn "in repeat_tactic";
116   try let (proof, goallist) = tactic status in
117    let rec step proof goallist =
118     match goallist with
119        [] -> (proof, [])
120      | head::tail -> 
121         let (proof', goallist') = repeat_tactic ~tactic (proof, head) in
122          let (proof'', goallist'') = step proof' tail in
123           proof'', goallist'@goallist''
124    in
125     step proof goallist
126   with 
127    (Fail _) as e ->
128     warn ("Tacticals.repeat_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
129     id_tac status
130 ;;
131
132
133
134 (* This tries to apply tactic n times *)
135
136 let rec do_tactic ~n ~tactic status =
137   warn "in do_tactic";
138   try 
139    let (proof, goallist) = 
140     if (n>0) then tactic status 
141              else id_tac status in
142 (*             else (proof, []) in *)(* perche' non va bene questo? stessa questione di ##### ? *)
143    let rec step proof goallist =
144     match goallist with
145        [] -> (proof, [])
146      | head::tail -> 
147         let (proof', goallist') = do_tactic ~n:(n-1) ~tactic (proof, head) in
148         let (proof'', goallist'') = step proof' tail in
149          proof'', goallist'@goallist''
150    in
151     step proof goallist
152   with 
153    (Fail _) as e ->
154     warn ("Tacticals.do_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
155     id_tac status
156 ;;
157
158
159
160 (* This applies tactic and catches its possible failure *)
161
162 let rec try_tactic ~tactic status =
163   warn "in Tacticals.try_tactic";
164   try
165    tactic status
166   with
167    (Fail _) as e -> 
168     warn ( "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e);
169     id_tac status
170 ;;
171
172
173 (* This tries tactics until one of them doesn't _solve_ the goal *)
174 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
175
176 let rec solve_tactics ~(tactics: (string * tactic) list) status =
177   warn "in Tacticals.solve_tactics";
178   match tactics with
179   | (descr, currenttactic)::moretactics ->
180       warn ("Tacticals.solve_tactics is trying " ^ descr);
181       (try
182         let (proof, goallist) = currenttactic status in
183          match goallist with 
184             [] -> warn ("Tacticals.solve_tactics: " ^ descr ^ " solved the goal!!!");
185 (* questo significa che non ci sono piu' goal, o che current_tactic non ne ha aperti di nuovi? (la 2a!) ##### *)
186 (* nel secondo caso basta per dire che solve_tactics has solved the goal? (si!) *)
187                   (proof, goallist)
188           | _ -> warn ("Tacticals.solve_tactics: try the next tactic");
189                  solve_tactics ~tactics:(moretactics) status
190        with
191         (Fail _) as e ->
192          warn ("Tacticals.solve_tactics: current tactic failed with exn: " ^ Printexc.to_string e);
193          solve_tactics ~tactics status
194       )
195   | [] -> raise (Fail "solve_tactics cannot solve the goal");
196           id_tac status
197 ;;
198
199
200
201
202
203
204
205
206
207
208   (** tattica di prova per debuggare i tatticali *)
209 (*
210 let thens' ~start ~continuations status =
211  let (proof,new_goals) = start status in
212   try
213    List.fold_left2
214     (fun (proof,goals) goal tactic ->
215       let (proof',new_goals') = tactic (proof,goal) in
216        (proof',goals@new_goals')
217     ) (proof,[]) new_goals continuations
218   with
219    Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
220
221 let prova_tac =
222  let apply_T_tac status =
223   let (proof, goal) = status in
224   let curi,metasenv,pbo,pty = proof in
225   let metano,context,gty = CicUtil.lookup_meta goal metasenv in
226    let rel =
227     let rec find n =
228      function
229         [] -> assert false
230       | (Some (Cic.Name name,_))::_ when name = "T" -> n
231       | _::tl -> find (n+1) tl
232     in
233      prerr_endline ("eseguo find");
234      find 1 context
235    in
236     prerr_endline ("eseguo apply");    
237     apply_tac ~term:(Cic.Rel rel) status
238  in
239 (*  do_tactic ~n:2 *)
240   repeat_tactic
241    ~tactic:
242     (then_
243       ~start:(intros_tac ~name:"pippo")
244       ~continuation:(thens' ~start:apply_T_tac ~continuations:[id_tac ; apply_tac ~term:(Cic.Rel 1)]))
245 (* id_tac *)
246 ;;
247 *)
248
249