]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/tacticals.ml
2c1b1883b930c5165aed955c41e864af88465f99
[helm.git] / helm / gTopLevel / 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 (** AUX TACTIC{,AL}S *)
41
42   (**
43     naive implementation of ORELSE tactical, try a sequence of tactics in turn:
44     if one fails pass to the next one and so on, eventually raises (failure "no
45     tactics left")
46     TODO warning: not tail recursive due to "try .. with" boxing
47   *)
48 let rec try_tactics ~(tactics: (string * tactic) list) ~status =
49   warn "in Ring.try_tactics";
50   match tactics with
51   | (descr, tac)::tactics ->
52       warn ("Ring.try_tactics IS TRYING " ^ descr);
53       (try
54         let res = tac ~status in
55         warn ("Ring.try_tactics: " ^ descr ^ " succedeed!!!");
56         res
57        with
58         e ->
59          match e with
60             (Fail _)
61           | (CicTypeChecker.NotWellTyped _)
62           | (CicUnification.UnificationFailed) ->
63               warn (
64                 "Ring.try_tactics failed with exn: " ^
65                 Printexc.to_string e);
66               try_tactics ~tactics ~status
67         | _ -> raise e (* [e] must not be caught ; let's re-raise it *)
68       )
69   | [] -> raise (Fail "try_tactics: no tactics left")
70
71 let thens ~start ~continuations ~status =
72  let (proof,new_goals) = start ~status in
73   try
74    List.fold_left2
75     (fun (proof,goals) goal tactic ->
76       let (proof',new_goals') = tactic ~status:(proof,goal) in
77        (proof',goals@new_goals')
78     ) (proof,[]) new_goals continuations
79   with
80    Invalid_argument _ -> raise (Fail "thens: wrong number of new goals")
81
82 let then_ ~start ~continuation ~status =
83  let (proof,new_goals) = start ~status in
84   List.fold_left
85    (fun (proof,goals) goal ->
86      let (proof',new_goals') = continuation ~status:(proof,goal) in
87       (proof',goals@new_goals')
88    ) (proof,[]) new_goals