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 *)
37 let info s = debug_print (lazy ("TACTICALS INFO: " ^ (Lazy.force s)))
40 let id_tac (proof,goal) =
41 let _, metasenv, _, _ = proof in
42 let _, _, _ = CicUtil.lookup_meta goal metasenv in
45 ProofEngineTypes.mk_tactic id_tac
48 let fail_tac (proof,goal) =
49 let _, metasenv, _, _ = proof in
50 let _, _, _ = CicUtil.lookup_meta goal metasenv in
51 raise (ProofEngineTypes.Fail (lazy "fail tactical"))
53 ProofEngineTypes.mk_tactic fail_tac
55 type goal = ProofEngineTypes.goal
57 (** TODO needed until tactics start returning both opened and closed goals
58 * First part of the function performs a diff among goals ~before tactic
59 * application and ~after it. Second part will add as both opened and closed
60 * the goals which are returned as opened by the tactic *)
61 let goals_diff ~before ~after ~opened =
62 let sort_opened opened add =
63 opened @ (List.filter (fun g -> not (List.mem g opened)) add)
67 (fun remove e -> if List.mem e after then remove else e :: remove)
72 (fun add e -> if List.mem e before then add else e :: add)
76 let add, remove = (* adds goals which have been both opened _and_ closed *)
78 (fun (add, remove) opened_goal ->
79 if List.mem opened_goal before
80 then opened_goal :: add, opened_goal :: remove
85 sort_opened opened add, remove
90 val first: tactics: (string * tactic) list -> tactic
91 val thens: start: tactic -> continuations: tactic list -> tactic
92 val then_: start: tactic -> continuation: tactic -> tactic
93 val seq: tactics: tactic list -> tactic
94 val repeat_tactic: tactic: tactic -> tactic
95 val do_tactic: n: int -> tactic: tactic -> tactic
96 val try_tactic: tactic: tactic -> tactic
97 val solve_tactics: tactics: (string * tactic) list -> tactic
99 val tactic: tactic -> tactic
102 val semicolon: tactic
105 val pos: int -> tactic
107 val focus: int list -> tactic
111 module Make (S: Continuationals.Status) : T with type tactic = S.tactic =
113 module C = Continuationals.Make (S)
115 type tactic = S.tactic
117 let fold_eval status ts =
119 List.fold_left (fun istatus t -> S.focus ~-1 (C.eval t istatus)) status ts
124 naive implementation of ORELSE tactical, try a sequence of tactics in turn:
125 if one fails pass to the next one and so on, eventually raises (failure "no
129 let rec first ~(tactics: (string * tactic) list) istatus =
130 info (lazy "in Tacticals.first");
132 | (descr, tac)::tactics ->
133 info (lazy ("Tacticals.first IS TRYING " ^ descr));
135 let res = S.apply_tactic tac istatus in
136 info (lazy ("Tacticals.first: " ^ descr ^ " succedeed!!!"));
141 | (ProofEngineTypes.Fail _)
142 | (CicTypeChecker.TypeCheckerFailure _)
143 | (CicUnification.UnificationFailure _) ->
145 "Tacticals.first failed with exn: " ^
146 Printexc.to_string e));
147 first ~tactics istatus
148 | _ -> raise e) (* [e] must not be caught ; let's re-raise it *)
149 | [] -> raise (ProofEngineTypes.Fail (lazy "first: no tactics left"))
151 S.mk_tactic (first ~tactics)
153 let thens ~start ~continuations =
157 ([ C.Tactical (C.Tactic start); C.Branch ]
158 @ (HExtlib.list_concat ~sep:[ C.Shift ]
159 (List.map (fun t -> [ C.Tactical (C.Tactic t) ]) continuations))
162 (* let thens ~start ~continuations =
163 let thens ~start ~continuations status =
164 let output_status = S.apply_tactic start status in
165 let new_goals = S.goals output_status in
167 let output_status,goals =
169 (fun (output_status,goals) goal tactic ->
170 let status = S.focus goal output_status in
171 let output_status' = S.apply_tactic tactic status in
172 let new_goals' = S.goals output_status' in
173 (output_status',goals@new_goals')
174 ) (output_status,[]) new_goals continuations
176 S.set_goals output_status goals
178 Invalid_argument _ ->
179 let debug = Printf.sprintf "thens: expected %i new goals, found %i"
180 (List.length continuations) (List.length new_goals)
184 S.mk_tactic (thens ~start ~continuations ) *)
186 let then_ ~start ~continuation =
190 [ C.Tactical (C.Tactic start);
192 C.Tactical (C.Tactic continuation) ])
194 (* let then_ ~start ~continuation =
195 let then_ ~start ~continuation status =
196 let output_status = S.apply_tactic start status in
197 let new_goals = S.goals output_status in
198 let output_status,goals =
200 (fun (output_status,goals) goal ->
201 let status = S.focus goal output_status in
202 let output_status' = S.apply_tactic continuation status in
203 let new_goals' = S.goals output_status' in
204 (output_status',goals@new_goals')
205 ) (output_status,[]) new_goals
207 S.set_goals output_status goals
209 S.mk_tactic (then_ ~start ~continuation) *)
215 (HExtlib.list_concat ~sep:[ C.Semicolon ]
216 (List.map (fun t -> [ C.Tactical (C.Tactic t) ]) tactics)))
218 (* let rec seq ~tactics =
222 | hd :: tl -> then_ ~start:hd ~continuation:(seq ~tactics:tl) *)
224 (* TODO: x debug: i due tatticali seguenti non contano quante volte hanno
225 * applicato la tattica *)
227 let rec step f output_status opened closed =
229 | [] -> output_status, [], closed
231 let status = S.focus head output_status in
232 let output_status' = f status in
233 let opened', closed' = S.goals output_status' in
234 let output_status'', opened'', closed'' =
235 step f output_status' tail []
237 output_status'', opened' @ opened'', closed' @ closed''
239 (* This keep on appling tactic until it fails. When <tactic> generates more
240 * than one goal, you have a tree of application on the tactic, repeat_tactic
241 * works in depth on this tree *)
242 let repeat_tactic ~tactic =
243 let rec repeat_tactic ~tactic status =
244 info (lazy "in repeat_tactic");
246 (* let rec step output_status opened closed =
248 | [] -> output_status, [], closed
250 let status = S.focus head output_status in
251 let output_status' = repeat_tactic ~tactic status in
252 let opened', closed' = S.goals output_status' in
253 let output_status'', opened'', closed'' =
254 step output_status' tail []
256 output_status'', opened' @ opened'', closed' @ closed''
258 let output_status = S.apply_tactic tactic status in
259 let opened, closed = S.goals output_status in
260 let output_status, opened', closed' =
261 step (repeat_tactic ~tactic) output_status opened closed
263 S.set_goals (opened', closed') output_status
265 (ProofEngineTypes.Fail _) as e ->
267 ("Tacticals.repeat_tactic failed after nth time with exception: "
268 ^ Printexc.to_string e));
269 S.apply_tactic S.id_tactic status
271 S.mk_tactic (repeat_tactic ~tactic)
273 (* This tries to apply tactic n times *)
274 let do_tactic ~n ~tactic =
275 let rec do_tactic ~n ~tactic status =
277 S.apply_tactic S.id_tactic status
280 let output_status = S.apply_tactic tactic status in
281 let opened, closed = S.goals output_status in
282 (* let rec step output_status goallist =
284 [] -> output_status, []
286 let status = S.focus head output_status in
287 let output_status' = do_tactic ~n:(n-1) ~tactic status in
288 let goallist' = S.goals output_status' in
289 let (output_status'', goallist'') = step output_status' tail in
290 output_status'', goallist'@goallist''
292 let output_status, opened', closed' =
293 step (do_tactic ~n:(n-1) ~tactic) output_status opened closed
295 S.set_goals (opened', closed') output_status
297 (ProofEngineTypes.Fail _) as e ->
299 ("Tacticals.do_tactic failed after nth time with exception: "
300 ^ Printexc.to_string e)) ;
301 S.apply_tactic S.id_tactic status
303 S.mk_tactic (do_tactic ~n ~tactic)
305 (* This applies tactic and catches its possible failure *)
306 let try_tactic ~tactic =
307 let rec try_tactic ~tactic status =
308 info (lazy "in Tacticals.try_tactic");
310 S.apply_tactic tactic status
312 (ProofEngineTypes.Fail _) as e ->
314 "Tacticals.try_tactic failed with exn: " ^ Printexc.to_string e));
315 S.apply_tactic S.id_tactic status
317 S.mk_tactic (try_tactic ~tactic)
319 (* This tries tactics until one of them doesn't _solve_ the goal *)
320 (* TODO: si puo' unificare le 2(due) chiamate ricorsive? *)
321 let solve_tactics ~tactics =
322 let rec solve_tactics ~(tactics: (string * tactic) list) status =
323 info (lazy "in Tacticals.solve_tactics");
325 | (descr, currenttactic)::moretactics ->
326 info (lazy ("Tacticals.solve_tactics is trying " ^ descr));
328 let output_status = S.apply_tactic currenttactic status in
329 let opened, closed = S.goals output_status in
331 | [] -> info (lazy ("Tacticals.solve_tactics: " ^ descr ^
332 " solved the goal!!!"));
333 (* questo significa che non ci sono piu' goal, o che current_tactic non ne ha
334 * aperti di nuovi? (la 2a!) ##### nel secondo caso basta per dire che
335 * solve_tactics has solved the goal? (si!) *)
337 | _ -> info (lazy ("Tacticals.solve_tactics: try the next tactic"));
338 solve_tactics ~tactics:(moretactics) status
340 (ProofEngineTypes.Fail _) as e ->
342 "Tacticals.solve_tactics: current tactic failed with exn: "
343 ^ Printexc.to_string e));
344 solve_tactics ~tactics status
347 raise (ProofEngineTypes.Fail
348 (lazy "solve_tactics cannot solve the goal"))
350 S.mk_tactic (solve_tactics ~tactics)
352 let cont_proxy cont = S.mk_tactic (C.eval cont)
354 let tactic t = cont_proxy (C.Tactical (C.Tactic t))
355 let skip = cont_proxy (C.Tactical C.Skip)
356 let dot = cont_proxy C.Dot
357 let semicolon = cont_proxy C.Semicolon
358 let branch = cont_proxy C.Branch
359 let shift = cont_proxy C.Shift
360 let pos i = cont_proxy (C.Pos i)
361 let merge = cont_proxy C.Merge
362 let focus goals = cont_proxy (C.Focus goals)
363 let unfocus = cont_proxy C.Unfocus
366 module ProofEngineStatus =
368 module Stack = Continuationals.Stack
371 ProofEngineTypes.status (* (proof, goal) *) * Stack.t
374 (ProofEngineTypes.proof * goal list * goal list) * Stack.t
376 type tactic = ProofEngineTypes.tactic
378 let id_tactic = id_tac
381 ProofEngineTypes.mk_tactic
382 (fun (proof, goal) as pstatus ->
383 let stack = [ [ 1, Stack.Open goal ], [], [], Stack.NoTag ] in
384 let istatus = pstatus, stack in
385 (* let ostatus = f istatus in
386 let ((proof, opened, _), _) = ostatus in *)
387 let (proof, _, _), stack = f istatus in
388 let opened = Continuationals.Stack.open_goals stack in
391 let apply_tactic tac ((proof, _) as pstatus, stack) =
392 let proof', opened = ProofEngineTypes.apply_tactic tac pstatus in
393 (* let _ = prerr_endline ("goal aperti dalla tattica " ^ String.concat "," (List.map string_of_int opened)) in *)
394 let before = ProofEngineTypes.goals_of_proof proof in
395 let after = ProofEngineTypes.goals_of_proof proof' in
396 let opened_goals, closed_goals = goals_diff ~before ~after ~opened in
397 (* let _ = prerr_endline ("goal ritornati dalla tattica " ^ String.concat "," (List.map string_of_int opened_goals)) in *)
398 (proof', opened_goals, closed_goals), stack
400 let goals ((_, opened, closed), _) = opened, closed
401 let set_goals (opened, closed) ((proof, _, _), stack) =
402 (proof, opened, closed), stack
405 let set_stack stack (opstatus, _) = opstatus, stack
407 let inject ((proof, _), stack) = ((proof, [], []), stack)
408 let focus goal ((proof, _, _), stack) = (proof, goal), stack
411 module ProofEngineTacticals = Make (ProofEngineStatus)
413 include ProofEngineTacticals