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