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