]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/tacticals.ml
Fixed a bug in the "do" tactical that made it diverge.
[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 let id_tac = 
42  let id_tac (proof,goal) = 
43   let _, metasenv, _, _ = proof in
44   let _, _, _ = CicUtil.lookup_meta goal metasenv in
45   (proof,[goal])
46  in 
47   mk_tactic id_tac
48
49 let fail_tac =
50  let fail_tac (proof,goal) =
51   let _, metasenv, _, _ = proof in
52   let _, _, _ = CicUtil.lookup_meta goal metasenv in
53    raise (Fail "fail tactical")
54  in
55   mk_tactic fail_tac
56
57 module type Status =
58  sig
59    type input_status
60    type output_status
61    type tactic
62    val id_tac : tactic
63    val mk_tactic : (input_status -> output_status) -> tactic
64    val apply_tactic : tactic -> input_status -> output_status
65    val goals : output_status -> ProofEngineTypes.goal list
66    val set_goals: output_status -> ProofEngineTypes.goal list -> output_status
67    val focus : output_status -> ProofEngineTypes.goal -> input_status
68  end
69
70 module type T =
71  sig
72   type tactic
73
74   val first: tactics: (string * tactic) list -> tactic
75
76   val thens: start: tactic -> continuations: tactic list -> tactic
77
78   val then_: start: tactic -> continuation: tactic -> tactic
79
80    (** "folding" of then_ *)
81   val seq: tactics: tactic list -> tactic
82
83   val repeat_tactic: tactic: tactic -> tactic
84
85   val do_tactic: n: int -> tactic: tactic -> tactic 
86
87   val try_tactic: tactic: tactic -> tactic 
88
89   val solve_tactics: tactics: (string * tactic) list -> tactic
90  end
91
92 module Make (S:Status) : T with type tactic = S.tactic =
93 struct
94 type tactic = S.tactic
95
96   (**
97     naive implementation of ORELSE tactical, try a sequence of tactics in turn:
98     if one fails pass to the next one and so on, eventually raises (failure "no
99     tactics left")
100   *)
101 let first ~tactics =
102  let rec first ~(tactics: (string * tactic) list) status =
103   warn "in Tacticals.first";
104   match tactics with
105   | (descr, tac)::tactics ->
106       warn ("Tacticals.first IS TRYING " ^ descr);
107       (try
108         let res = S.apply_tactic tac status in
109         warn ("Tacticals.first: " ^ descr ^ " succedeed!!!");
110         res
111        with
112         e ->
113          match e with
114             (Fail _)
115           | (CicTypeChecker.TypeCheckerFailure _)
116           | (CicUnification.UnificationFailure _) ->
117               warn (
118                 "Tacticals.first failed with exn: " ^
119                 Printexc.to_string e);
120               first ~tactics status
121         | _ -> raise e (* [e] must not be caught ; let's re-raise it *)
122       )
123   | [] -> raise (Fail "first: no tactics left")
124  in
125   S.mk_tactic (first ~tactics)
126
127
128 let thens ~start ~continuations =
129  let thens ~start ~continuations status =
130  let output_status = S.apply_tactic start status in
131  let new_goals = S.goals output_status in
132   try
133    let output_status,goals =
134     List.fold_left2
135      (fun (output_status,goals) goal tactic ->
136        let status = S.focus output_status goal in
137        let output_status' = S.apply_tactic tactic status in
138        let new_goals' = S.goals output_status' in
139         (output_status',goals@new_goals')
140      ) (output_status,[]) new_goals continuations
141    in
142     S.set_goals output_status goals
143   with
144    Invalid_argument _ -> 
145     let debug = Printf.sprintf "thens: expected %i new goals, found %i"
146      (List.length continuations) (List.length new_goals)
147     in
148     raise (Fail debug)
149  in
150   S.mk_tactic (thens ~start ~continuations )
151
152
153 let then_ ~start ~continuation =
154  let then_ ~start ~continuation status =
155  let output_status = S.apply_tactic start status in
156  let new_goals = S.goals output_status in
157   let output_status,goals =
158    List.fold_left
159     (fun (output_status,goals) goal ->
160       let status = S.focus output_status goal in
161       let output_status' = S.apply_tactic continuation status in
162       let new_goals' = S.goals output_status' in
163        (output_status',goals@new_goals')
164     ) (output_status,[]) new_goals
165   in
166    S.set_goals output_status goals
167  in
168   S.mk_tactic (then_ ~start ~continuation)
169
170 let rec seq ~tactics =
171   match tactics with
172   | [] -> assert false
173   | [tac] -> tac
174   | hd :: tl -> then_ ~start:hd ~continuation:(seq ~tactics:tl)
175
176 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno applicato la tattica *)
177
178 (* This keep on appling tactic until it fails *)
179 (* When <tactic> generates more than one goal, you have a tree of
180    application on the tactic, repeat_tactic works in depth on this tree *)
181
182 let repeat_tactic ~tactic =
183  let rec repeat_tactic ~tactic status =
184   warn "in repeat_tactic";
185   try
186    let output_status = S.apply_tactic tactic status in
187    let goallist = S.goals output_status in
188    let rec step output_status goallist =
189     match goallist with
190        [] -> output_status,[]
191      | head::tail -> 
192         let status = S.focus output_status head in
193         let output_status' = repeat_tactic ~tactic status in
194         let goallist' = S.goals output_status' in
195          let output_status'',goallist'' = step output_status' tail in
196           output_status'',goallist'@goallist''
197    in
198     let output_status,goallist = step output_status goallist in
199      S.set_goals output_status goallist
200   with 
201    (Fail _) as e ->
202     warn ("Tacticals.repeat_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
203     S.apply_tactic S.id_tac status
204  in 
205   S.mk_tactic (repeat_tactic ~tactic)
206
207
208 (* This tries to apply tactic n times *)
209 let do_tactic ~n ~tactic =
210  let rec do_tactic ~n ~tactic status =
211   if n = 0 then
212    S.apply_tactic S.id_tac status
213   else
214    try 
215     let output_status = S.apply_tactic tactic status in
216     let goallist = S.goals output_status in
217     let rec step output_status goallist =
218      match goallist with
219         [] -> output_status, []
220       | head::tail -> 
221          let status = S.focus output_status head in
222          let output_status' = do_tactic ~n:(n-1) ~tactic status in
223          let goallist' = S.goals output_status' in 
224          let (output_status'', goallist'') = step output_status' tail in
225           output_status'', goallist'@goallist''
226     in
227      let output_status,goals = step output_status goallist in
228       S.set_goals output_status goals
229    with 
230     (Fail _) as e ->
231      warn ("Tacticals.do_tactic failed after nth time with exception: " ^ Printexc.to_string e) ;
232      S.apply_tactic S.id_tac status
233  in
234   S.mk_tactic (do_tactic ~n ~tactic)
235
236
237
238 (* This applies tactic and catches its possible failure *)
239 let try_tactic ~tactic =
240  let rec try_tactic ~tactic status =
241   warn "in Tacticals.try_tactic";
242   try
243    S.apply_tactic tactic status
244   with
245    (Fail _) as e -> 
246     warn ( "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e);
247     S.apply_tactic S.id_tac status
248  in
249   S.mk_tactic (try_tactic ~tactic)
250
251 (* This tries tactics until one of them doesn't _solve_ the goal *)
252 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
253 let solve_tactics ~tactics =
254  let rec solve_tactics ~(tactics: (string * tactic) list) status =
255   warn "in Tacticals.solve_tactics";
256   match tactics with
257   | (descr, currenttactic)::moretactics ->
258       warn ("Tacticals.solve_tactics is trying " ^ descr);
259       (try
260         let output_status = S.apply_tactic currenttactic status in
261         let goallist = S.goals output_status in
262          match goallist with 
263             [] -> warn ("Tacticals.solve_tactics: " ^ descr ^ 
264                    " solved the goal!!!");
265 (* questo significa che non ci sono piu' goal, o che current_tactic non ne 
266    ha aperti di nuovi? (la 2a!) ##### 
267    nel secondo caso basta per dire che solve_tactics has solved the goal? (si!) *)
268                   output_status
269           | _ -> warn ("Tacticals.solve_tactics: try the next tactic");
270                  solve_tactics ~tactics:(moretactics) status
271        with
272         (Fail _) as e ->
273          warn ("Tacticals.solve_tactics: current tactic failed with exn: " ^ 
274           Printexc.to_string e);
275          solve_tactics ~tactics status
276       )
277   | [] -> raise (Fail "solve_tactics cannot solve the goal");
278           S.apply_tactic S.id_tac status
279  in
280   S.mk_tactic (solve_tactics ~tactics)
281 end
282
283 module ProofEngineStatus =
284  struct
285    type input_status = ProofEngineTypes.status
286    type output_status = ProofEngineTypes.proof * ProofEngineTypes.goal list
287    type tactic = ProofEngineTypes.tactic
288    let id_tac = id_tac
289    let mk_tactic = ProofEngineTypes.mk_tactic
290    let apply_tactic = ProofEngineTypes.apply_tactic
291    let goals (_,goals) = goals
292    let set_goals (proof,_) goals = proof,goals
293    let focus (proof,_) goal = proof,goal
294  end
295
296 module ProofEngineTacticals = Make(ProofEngineStatus)
297
298 include ProofEngineTacticals