]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/eliminationTactics.ml
ocaml 3.09 transition
[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:[]) ?depth
107   ?using what
108 =
109   let elim what =
110     P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what
111   in
112   let elim_type_tac status =
113     let tac =
114       T.thens ~start: (P.cut_tac what) ~continuations:[elim (C.Rel 1); T.id_tac]
115     in
116     E.apply_tactic tac status
117   in
118   E.mk_tactic elim_type_tac
119
120 (* decompose ****************************************************************)
121
122 (* robaglia --------------------------------------------------------------- *)
123
124   (** perform debugging output? *)
125 let debug = false
126 let debug_print = fun _ -> ()
127
128   (** debugging print *)
129 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
130
131 (* search in term the Inductive Types and return a list of uris as triples like this: (uri,typeno,exp_named_subst) *)
132 let search_inductive_types ty =
133    let rec aux types = function 
134       | C.MutInd (uri, typeno, _) when (not (List.mem (uri, typeno) types)) -> 
135          (uri, typeno) :: types
136       | C.Appl applist -> List.fold_left aux types applist
137       | _              -> types
138    in
139    aux [] ty
140 (* N.B: in un caso tipo (and A forall C:Prop.(or B C)) l'or *non* viene selezionato! *)
141
142 (* roba seria ------------------------------------------------------------- *)
143
144 let decompose_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[])
145                   ?(user_types=[]) ~dbd what =
146    let decompose_tac status =
147       let (proof, goal) = status in
148       let _, metasenv,_,_ = proof in
149       let _, context, _ = CicUtil.lookup_meta goal metasenv in
150       let types = List.rev_append user_types (Q.decomposables dbd) in
151       let tactic = elim_clear_tac ~mk_fresh_name_callback ~types in
152       let old_context_length = List.length context in
153       let tac = T.then_ ~start:(tactic ~what)
154                         ~continuation:(scan_tac ~old_context_length ~index:1 ~tactic)
155       in
156       E.apply_tactic tac status
157    in
158    E.mk_tactic decompose_tac
159       
160 (*
161 module R = CicReduction
162
163         let rec elim_clear_tac ~term' ~nr_of_hyp_still_to_elim status =
164          let (proof, goal) = status in
165          warn (lazy ("nr_of_hyp_still_to_elim=" ^ (string_of_int nr_of_hyp_still_to_elim)));
166          if nr_of_hyp_still_to_elim <> 0 then
167           let _,metasenv,_,_ = proof in
168            let _,context,_ = CicUtil.lookup_meta goal metasenv in
169             let old_context_len = List.length context in
170             let termty,_ = 
171               CicTypeChecker.type_of_aux' metasenv context term' 
172                 CicUniv.empty_ugraph in
173              warn (lazy ("elim_clear termty= " ^ CicPp.ppterm termty));
174              match termty with
175                 C.MutInd (uri,typeno,exp_named_subst)
176               | C.Appl((C.MutInd (uri,typeno,exp_named_subst))::_) 
177                  when (List.mem (uri,typeno,exp_named_subst) urilist) ->
178                    warn (lazy ("elim " ^ CicPp.ppterm termty));
179                    ProofEngineTypes.apply_tactic 
180                    (T.then_ 
181                       ~start:(P.elim_intros_simpl_tac term')
182                       ~continuation:(
183                         (* clear the hyp that has just been eliminated *)
184                         ProofEngineTypes.mk_tactic (fun status -> 
185                           let (proof, goal) = status in
186                           let _,metasenv,_,_ = proof in
187                            let _,context,_ = CicUtil.lookup_meta goal metasenv in
188                             let new_context_len = List.length context in   
189                              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)));
190                              let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim + (new_context_len - old_context_len) - 1 in
191                              let hyp_name =
192                               match List.nth context new_nr_of_hyp_still_to_elim with
193                                  None
194                                | Some (Cic.Anonymous,_) -> assert false
195                                | Some (Cic.Name name,_) -> name
196                              in
197                              ProofEngineTypes.apply_tactic
198                              (T.then_ 
199                                 ~start:(
200                                   if (term'==term) (* if it's the first application of elim, there's no need to clear the hyp *) 
201                                    then begin debug_print (lazy ("%%%%%%% no clear")); T.id_tac end
202                                    else begin debug_print (lazy ("%%%%%%% clear " ^ (string_of_int (new_nr_of_hyp_still_to_elim)))); (S.clear ~hyp:hyp_name) end)
203                                 ~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)))
204                                 status
205                         )))
206                       status
207               | _ ->
208                    let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim - 1 in 
209                     warn (lazy ("fail; hyp=" ^ (string_of_int new_nr_of_hyp_still_to_elim)));
210                     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
211          else (* no hyp to elim left in this goal *)
212           ProofEngineTypes.apply_tactic T.id_tac status
213
214         in
215          elim_clear_tac ~term':term ~nr_of_hyp_still_to_elim:1 status
216 *)