]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/eliminationTactics.ml
Every exception that used to have type string is now a string Lazy.t.
[helm.git] / helm / ocaml / tactics / eliminationTactics.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 module C = Cic
27 module P = PrimitiveTactics
28 module T = Tacticals
29 module S = ProofEngineStructuralRules
30 module F = FreshNamesGenerator
31 module E = ProofEngineTypes
32 module H = ProofEngineHelpers
33 module Q = MetadataQuery
34
35 (*
36 let induction_tac ~term status =
37   let (proof, goal) = status in
38   let module C = Cic in
39   let module R = CicReduction in
40   let module P = PrimitiveTactics in
41   let module T = Tacticals in
42   let module S = ProofEngineStructuralRules in
43   let module U = UriManager in 
44    let (_,metasenv,_,_) = proof in
45     let _,context,ty = CicUtil.lookup_meta goal metasenv in
46      let termty = CicTypeChecker.type_of_aux' metasenv context term in  (* per ora non serve *)
47
48      T.then_ ~start:(T.repeat_tactic 
49                        ~tactic:(T.then_ ~start:(VariousTactics.generalize_tac ~term) (* chissa' se cosi' funziona? *)
50                        ~continuation:(P.intros))
51              ~continuation:(P.elim_intros_simpl ~term)
52              status
53 ;;
54 *)
55
56 (* unexported tactics *******************************************************)
57
58 let get_name context index =
59    try match List.nth context (pred index) with
60       | Some (Cic.Name name, _)     -> Some name
61       | _                           -> None
62    with Invalid_argument "List.nth" -> None
63
64 let rec scan_tac ~old_context_length ~index ~tactic =
65    let scan_tac status =
66       let (proof, goal) = status in
67       let _, metasenv, _, _ = proof in
68       let _, context, _ = CicUtil.lookup_meta goal metasenv in
69       let context_length = List.length context in
70       let rec aux index =
71          match get_name context index with 
72             | _ when index <= 0 -> (proof, [goal])
73             | None              -> aux (pred index)
74             | Some what         -> 
75                let tac = T.then_ ~start:(tactic ~what)
76                                  ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
77                in
78                try E.apply_tactic tac status 
79                with E.Fail _ -> aux (pred index)
80       in aux (index + context_length - old_context_length - 1)
81    in
82    E.mk_tactic scan_tac
83
84 let rec check_inductive_types types = function 
85    | C.MutInd (uri, typeno, _) -> List.mem (uri, typeno) types
86    | C.Appl (hd :: tl)         -> check_inductive_types types hd
87    | _                         -> false
88
89 let elim_clear_tac ~mk_fresh_name_callback ~types ~what =
90    let elim_clear_tac status =
91       let (proof, goal) = status in
92       let _, metasenv, _, _ = proof in
93       let _, context, _ = CicUtil.lookup_meta goal metasenv in
94       let index, ty = H.lookup_type metasenv context what in
95       if check_inductive_types types ty then 
96          let tac = T.then_ ~start:(P.elim_intros_tac ~mk_fresh_name_callback (C.Rel index))
97                            ~continuation:(S.clear what)
98          in
99          E.apply_tactic tac status
100       else raise (E.Fail (lazy "unexported elim_clear: not an eliminable type"))
101    in
102    E.mk_tactic elim_clear_tac
103
104 (* elim type ****************************************************************)
105
106 let elim_type_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) 
107                   ?depth ?using what =
108    let elim what = P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what in
109    let elim_type_tac status =
110       let tac = T.thens ~start: (P.cut_tac what)
111                         ~continuations:[elim (C.Rel 1); T.id_tac]
112       in
113       E.apply_tactic tac status
114    in
115    E.mk_tactic elim_type_tac
116
117 (* decompose ****************************************************************)
118
119 (* robaglia --------------------------------------------------------------- *)
120
121   (** perform debugging output? *)
122 let debug = false
123 let debug_print = fun _ -> ()
124
125   (** debugging print *)
126 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
127
128 (* search in term the Inductive Types and return a list of uris as triples like this: (uri,typeno,exp_named_subst) *)
129 let search_inductive_types ty =
130    let rec aux types = function 
131       | C.MutInd (uri, typeno, _) when (not (List.mem (uri, typeno) types)) -> 
132          (uri, typeno) :: types
133       | C.Appl applist -> List.fold_left aux types applist
134       | _              -> types
135    in
136    aux [] ty
137 (* N.B: in un caso tipo (and A forall C:Prop.(or B C)) l'or *non* viene selezionato! *)
138
139 (* roba seria ------------------------------------------------------------- *)
140
141 let decompose_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[])
142                   ?(user_types=[]) ~dbd what =
143    let decompose_tac status =
144       let (proof, goal) = status in
145       let _, metasenv,_,_ = proof in
146       let _, context, _ = CicUtil.lookup_meta goal metasenv in
147       let types = List.rev_append user_types (Q.decomposables dbd) in
148       let tactic = elim_clear_tac ~mk_fresh_name_callback ~types in
149       let old_context_length = List.length context in
150       let tac = T.then_ ~start:(tactic ~what)
151                         ~continuation:(scan_tac ~old_context_length ~index:1 ~tactic)
152       in
153       E.apply_tactic tac status
154    in
155    E.mk_tactic decompose_tac
156       
157 (*
158 module R = CicReduction
159
160         let rec elim_clear_tac ~term' ~nr_of_hyp_still_to_elim status =
161          let (proof, goal) = status in
162          warn (lazy ("nr_of_hyp_still_to_elim=" ^ (string_of_int nr_of_hyp_still_to_elim)));
163          if nr_of_hyp_still_to_elim <> 0 then
164           let _,metasenv,_,_ = proof in
165            let _,context,_ = CicUtil.lookup_meta goal metasenv in
166             let old_context_len = List.length context in
167             let termty,_ = 
168               CicTypeChecker.type_of_aux' metasenv context term' 
169                 CicUniv.empty_ugraph in
170              warn (lazy ("elim_clear termty= " ^ CicPp.ppterm termty));
171              match termty with
172                 C.MutInd (uri,typeno,exp_named_subst)
173               | C.Appl((C.MutInd (uri,typeno,exp_named_subst))::_) 
174                  when (List.mem (uri,typeno,exp_named_subst) urilist) ->
175                    warn (lazy ("elim " ^ CicPp.ppterm termty));
176                    ProofEngineTypes.apply_tactic 
177                    (T.then_ 
178                       ~start:(P.elim_intros_simpl_tac term')
179                       ~continuation:(
180                         (* clear the hyp that has just been eliminated *)
181                         ProofEngineTypes.mk_tactic (fun status -> 
182                           let (proof, goal) = status in
183                           let _,metasenv,_,_ = proof in
184                            let _,context,_ = CicUtil.lookup_meta goal metasenv in
185                             let new_context_len = List.length context in   
186                              warn (lazy ("newcon=" ^ (string_of_int new_context_len) ^ " & oldcon=" ^ (string_of_int old_context_len) ^ " & old_nr_of_hyp=" ^ (string_of_int nr_of_hyp_still_to_elim)));
187                              let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim + (new_context_len - old_context_len) - 1 in
188                              let hyp_name =
189                               match List.nth context new_nr_of_hyp_still_to_elim with
190                                  None
191                                | Some (Cic.Anonymous,_) -> assert false
192                                | Some (Cic.Name name,_) -> name
193                              in
194                              ProofEngineTypes.apply_tactic
195                              (T.then_ 
196                                 ~start:(
197                                   if (term'==term) (* if it's the first application of elim, there's no need to clear the hyp *) 
198                                    then begin debug_print (lazy ("%%%%%%% no clear")); T.id_tac end
199                                    else begin debug_print (lazy ("%%%%%%% clear " ^ (string_of_int (new_nr_of_hyp_still_to_elim)))); (S.clear ~hyp:hyp_name) end)
200                                 ~continuation:(ProofEngineTypes.mk_tactic (elim_clear_tac ~term':(C.Rel new_nr_of_hyp_still_to_elim) ~nr_of_hyp_still_to_elim:new_nr_of_hyp_still_to_elim)))
201                                 status
202                         )))
203                       status
204               | _ ->
205                    let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim - 1 in 
206                     warn (lazy ("fail; hyp=" ^ (string_of_int new_nr_of_hyp_still_to_elim)));
207                     elim_clear_tac ~term':(C.Rel new_nr_of_hyp_still_to_elim) ~nr_of_hyp_still_to_elim:new_nr_of_hyp_still_to_elim status
208          else (* no hyp to elim left in this goal *)
209           ProofEngineTypes.apply_tactic T.id_tac status
210
211         in
212          elim_clear_tac ~term':term ~nr_of_hyp_still_to_elim:1 status
213 *)