]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/eliminationTactics.ml
new universes implementation
[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 (** DEBUGGING *)
27
28   (** perform debugging output? *)
29 let debug = false
30
31   (** debugging print *)
32 let warn s =
33   if debug then
34     prerr_endline ("DECOMPOSE: " ^ s)
35
36
37
38 (*
39 let induction_tac ~term status =
40   let (proof, goal) = status in
41   let module C = Cic in
42   let module R = CicReduction in
43   let module P = PrimitiveTactics in
44   let module T = Tacticals in
45   let module S = ProofEngineStructuralRules in
46   let module U = UriManager in 
47    let (_,metasenv,_,_) = proof in
48     let _,context,ty = CicUtil.lookup_meta goal metasenv in
49      let termty = CicTypeChecker.type_of_aux' metasenv context term in  (* per ora non serve *)
50
51      T.then_ ~start:(T.repeat_tactic 
52                        ~tactic:(T.then_ ~start:(VariousTactics.generalize_tac ~term) (* chissa' se cosi' funziona? *)
53                        ~continuation:(P.intros))
54              ~continuation:(P.elim_intros_simpl ~term)
55              status
56 ;;
57 *)
58
59 let elim_type_tac ~term =
60  let elim_type_tac ~term status =
61    let module C = Cic in
62    let module P = PrimitiveTactics in
63    let module T = Tacticals in
64     ProofEngineTypes.apply_tactic 
65      (T.thens
66       ~start: (P.cut_tac term)
67       ~continuations:[ P.elim_intros_simpl_tac ~term:(C.Rel 1) ; T.id_tac ])
68      status
69  in
70   ProofEngineTypes.mk_tactic (elim_type_tac ~term)
71 ;;
72
73
74 (* Decompose related stuff *)
75
76 exception InteractiveUserUriChoiceNotRegistered
77
78 let interactive_user_uri_choice =
79  (ref (fun ~selection_mode -> raise InteractiveUserUriChoiceNotRegistered) :
80   (selection_mode:[`SINGLE | `EXTENDED] ->
81       ?ok:string ->
82       ?enable_button_for_non_vars:bool ->
83       title:string -> msg:string -> string list -> string list) ref)
84 ;;
85
86 exception IllFormedUri of string
87
88 let cic_textual_parser_uri_of_string uri' =
89  try
90   (* Constant *)
91   if String.sub uri' (String.length uri' - 4) 4 = ".con" then
92    CicTextualParser0.ConUri (UriManager.uri_of_string uri')
93   else
94    if String.sub uri' (String.length uri' - 4) 4 = ".var" then
95     CicTextualParser0.VarUri (UriManager.uri_of_string uri')
96    else
97     (try
98       (* Inductive Type *)
99       let uri'',typeno = CicTextualLexer.indtyuri_of_uri uri' in
100        CicTextualParser0.IndTyUri (uri'',typeno)
101      with
102       _ ->
103        (* Constructor of an Inductive Type *)
104        let uri'',typeno,consno =
105         CicTextualLexer.indconuri_of_uri uri'
106        in
107         CicTextualParser0.IndConUri (uri'',typeno,consno)
108     )
109  with
110   _ -> raise (IllFormedUri uri')
111 ;;
112
113 (*
114 let constructor_uri_of_string uri = 
115   match cic_textual_parser_uri_of_string uri with
116      CicTextualParser0.IndTyUri (uri,typeno) -> (uri,typeno,[])
117    | _ -> assert false
118 ;;
119
120 let call_back uris = 
121 (* N.B.: nella finestra c'e' un campo "nessuno deei precedenti, prova questo" che non ha senso? *)
122 (* N.B.: in questo passaggio perdo l'informazione su exp_named_subst !!!! *)
123 (* domanda: due triple possono essere diverse solo per avere exp_named_subst diverse?? *)
124   let module U = UriManager in 
125    List.map 
126     (constructor_uri_of_string)
127     (!interactive_user_uri_choice 
128       ~selection_mode:`EXTENDED ~ok:"Ok" ~enable_button_for_non_vars:false 
129       ~title:"Decompose" ~msg:"Please, select the Inductive Types to decompose" 
130       (List.map 
131         (function (uri,typeno,_) -> U.string_of_uri uri ^ "#1/" ^ string_of_int (typeno+1)) 
132         uris)
133     ) 
134 ;;
135 *)
136
137 let decompose_tac ?(uris_choice_callback=(function l -> l)) term =
138  let decompose_tac uris_choice_callback term status =
139   let (proof, goal) = status in
140   let module C = Cic in
141   let module R = CicReduction in
142   let module P = PrimitiveTactics in
143   let module T = Tacticals in
144   let module S = ProofEngineStructuralRules in
145    let _,metasenv,_,_ = proof in
146     let _,context,ty = CicUtil.lookup_meta goal metasenv in
147      let old_context_len = List.length context in
148      let termty = CicTypeChecker.type_of_aux' metasenv context term in
149
150       let rec make_list termty = 
151       (* N.B.: altamente inefficente? *)
152        let rec search_inductive_types urilist termty =
153         (* search in term the Inductive Types and return a list of uris as triples like this: (uri,typeno,exp_named_subst) *)
154         match termty with
155            (C.MutInd (uri,typeno,exp_named_subst)) (* when (not (List.mem (uri,typeno,exp_named_subst) urilist)) *) -> 
156                (uri,typeno,exp_named_subst)::urilist
157          | (C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::applist)) (* when (not (List.mem (uri,typeno,exp_named_subst) urilist)) *) -> 
158                (uri,typeno,exp_named_subst)::(List.fold_left search_inductive_types urilist applist)
159          | _ -> urilist
160          (* N.B: in un caso tipo (and A !C:Prop.(or B C)) l'or *non* viene selezionato! *)
161        in 
162        let rec purge_duplicates urilist = 
163         let rec aux triple urilist =
164          match urilist with 
165             [] -> []
166           | hd::tl -> 
167              if (hd = triple) 
168               then aux triple tl
169               else hd::(aux triple tl)
170         in
171         match urilist with
172            [] -> []
173          | hd::tl -> hd::(purge_duplicates (aux hd tl))
174        in
175         purge_duplicates (search_inductive_types [] termty) 
176       in
177
178        let urilist =  
179           (* list of triples (uri,typeno,exp_named_subst) of Inductive Types found in term and chosen by the user *)
180           (* N.B.: due to a bug in uris_choice_callback exp_named_subst are not significant (they all are []) *)
181          uris_choice_callback (make_list termty) in
182
183         let rec elim_clear_tac ~term' ~nr_of_hyp_still_to_elim status =
184          let (proof, goal) = status in
185          warn ("nr_of_hyp_still_to_elim=" ^ (string_of_int nr_of_hyp_still_to_elim));
186          if nr_of_hyp_still_to_elim <> 0 then
187           let _,metasenv,_,_ = proof in
188            let _,context,_ = CicUtil.lookup_meta goal metasenv in
189             let old_context_len = List.length context in
190             let termty = CicTypeChecker.type_of_aux' metasenv context term' in
191              warn ("elim_clear termty= " ^ CicPp.ppterm termty);
192              match termty with
193                 C.MutInd (uri,typeno,exp_named_subst)
194               | C.Appl((C.MutInd (uri,typeno,exp_named_subst))::_) 
195                  when (List.mem (uri,typeno,exp_named_subst) urilist) ->
196                    warn ("elim " ^ CicPp.ppterm termty);
197                    ProofEngineTypes.apply_tactic 
198                    (T.then_ 
199                       ~start:(P.elim_intros_simpl_tac ~term:term')
200                       ~continuation:(
201                         (* clear the hyp that has just been eliminated *)
202                         ProofEngineTypes.mk_tactic (fun status -> 
203                           let (proof, goal) = status in
204                           let _,metasenv,_,_ = proof in
205                            let _,context,_ = CicUtil.lookup_meta goal metasenv in
206                             let new_context_len = List.length context in   
207                              warn ("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));
208                              let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim + (new_context_len - old_context_len) - 1 in
209                              ProofEngineTypes.apply_tactic
210                              (T.then_ 
211                                 ~start:(
212                                   if (term'==term) (* if it's the first application of elim, there's no need to clear the hyp *) 
213                                    then begin prerr_endline ("%%%%%%% no clear"); T.id_tac end
214                                    else begin prerr_endline ("%%%%%%% clear " ^ (string_of_int (new_nr_of_hyp_still_to_elim))); (S.clear ~hyp:(List.nth context (new_nr_of_hyp_still_to_elim))) end)
215                                 ~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)))
216                                 status
217                         )))
218                       status
219               | _ ->
220                    let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim - 1 in 
221                     warn ("fail; hyp=" ^ (string_of_int new_nr_of_hyp_still_to_elim));
222                     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
223          else (* no hyp to elim left in this goal *)
224           ProofEngineTypes.apply_tactic T.id_tac status
225
226         in
227          elim_clear_tac ~term':term ~nr_of_hyp_still_to_elim:1 status
228  in
229   ProofEngineTypes.mk_tactic (decompose_tac uris_choice_callback term)
230 ;;
231
232