]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/eliminationTactics.ml
name specifications added for elim_intros, elim_intros_simpl and elim_type
[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 let debug_print = fun _ -> ()
31
32   (** debugging print *)
33 let warn s =
34   if debug then
35     debug_print ("DECOMPOSE: " ^ s)
36
37
38
39 (*
40 let induction_tac ~term status =
41   let (proof, goal) = status in
42   let module C = Cic in
43   let module R = CicReduction in
44   let module P = PrimitiveTactics in
45   let module T = Tacticals in
46   let module S = ProofEngineStructuralRules in
47   let module U = UriManager in 
48    let (_,metasenv,_,_) = proof in
49     let _,context,ty = CicUtil.lookup_meta goal metasenv in
50      let termty = CicTypeChecker.type_of_aux' metasenv context term in  (* per ora non serve *)
51
52      T.then_ ~start:(T.repeat_tactic 
53                        ~tactic:(T.then_ ~start:(VariousTactics.generalize_tac ~term) (* chissa' se cosi' funziona? *)
54                        ~continuation:(P.intros))
55              ~continuation:(P.elim_intros_simpl ~term)
56              status
57 ;;
58 *)
59
60 let elim_type_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) 
61                   ?depth ?using what =
62  let elim_type_tac status =
63    let module C = Cic in
64    let module P = PrimitiveTactics in
65    let module T = Tacticals in
66     ProofEngineTypes.apply_tactic 
67      (T.thens
68       ~start: (P.cut_tac what)
69       ~continuations:[ P.elim_intros_simpl_tac ?depth ?using ~mk_fresh_name_callback (C.Rel 1) ; T.id_tac ])
70      status
71  in
72   ProofEngineTypes.mk_tactic elim_type_tac
73 ;;
74
75
76 (* Decompose related stuff *)
77
78 exception InteractiveUserUriChoiceNotRegistered
79
80 let interactive_user_uri_choice =
81  (ref (fun ~selection_mode -> raise InteractiveUserUriChoiceNotRegistered) :
82   (selection_mode:[`SINGLE | `EXTENDED] ->
83       ?ok:string ->
84       ?enable_button_for_non_vars:bool ->
85       title:string -> msg:string -> string list -> string list) ref)
86 ;;
87
88 let decompose_tac ?(uris_choice_callback=(function l -> l)) term =
89  let decompose_tac uris_choice_callback term status =
90   let (proof, goal) = status in
91   let module C = Cic in
92   let module R = CicReduction in
93   let module P = PrimitiveTactics in
94   let module T = Tacticals in
95   let module S = ProofEngineStructuralRules in
96    let _,metasenv,_,_ = proof in
97     let _,context,ty = CicUtil.lookup_meta goal metasenv in
98      let old_context_len = List.length context in
99      let termty,_ = 
100        CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
101      let rec make_list termty = 
102       (* N.B.: altamente inefficente? *)
103        let rec search_inductive_types urilist termty =
104         (* search in term the Inductive Types and return a list of uris as triples like this: (uri,typeno,exp_named_subst) *)
105         match termty with
106            (C.MutInd (uri,typeno,exp_named_subst)) (* when (not (List.mem (uri,typeno,exp_named_subst) urilist)) *) -> 
107                (uri,typeno,exp_named_subst)::urilist
108          | (C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::applist)) (* when (not (List.mem (uri,typeno,exp_named_subst) urilist)) *) -> 
109                (uri,typeno,exp_named_subst)::(List.fold_left search_inductive_types urilist applist)
110          | _ -> urilist
111          (* N.B: in un caso tipo (and A forall C:Prop.(or B C)) l'or *non* viene selezionato! *)
112        in 
113        let rec purge_duplicates urilist = 
114         let rec aux triple urilist =
115          match urilist with 
116             [] -> []
117           | hd::tl -> 
118              if (hd = triple) 
119               then aux triple tl
120               else hd::(aux triple tl)
121         in
122         match urilist with
123            [] -> []
124          | hd::tl -> hd::(purge_duplicates (aux hd tl))
125        in
126         purge_duplicates (search_inductive_types [] termty) 
127       in
128
129        let urilist =  
130           (* list of triples (uri,typeno,exp_named_subst) of Inductive Types found in term and chosen by the user *)
131           (* N.B.: due to a bug in uris_choice_callback exp_named_subst are not significant (they all are []) *)
132          uris_choice_callback (make_list termty) in
133
134         let rec elim_clear_tac ~term' ~nr_of_hyp_still_to_elim status =
135          let (proof, goal) = status in
136          warn ("nr_of_hyp_still_to_elim=" ^ (string_of_int nr_of_hyp_still_to_elim));
137          if nr_of_hyp_still_to_elim <> 0 then
138           let _,metasenv,_,_ = proof in
139            let _,context,_ = CicUtil.lookup_meta goal metasenv in
140             let old_context_len = List.length context in
141             let termty,_ = 
142               CicTypeChecker.type_of_aux' metasenv context term' 
143                 CicUniv.empty_ugraph in
144              warn ("elim_clear termty= " ^ CicPp.ppterm termty);
145              match termty with
146                 C.MutInd (uri,typeno,exp_named_subst)
147               | C.Appl((C.MutInd (uri,typeno,exp_named_subst))::_) 
148                  when (List.mem (uri,typeno,exp_named_subst) urilist) ->
149                    warn ("elim " ^ CicPp.ppterm termty);
150                    ProofEngineTypes.apply_tactic 
151                    (T.then_ 
152                       ~start:(P.elim_intros_simpl_tac term')
153                       ~continuation:(
154                         (* clear the hyp that has just been eliminated *)
155                         ProofEngineTypes.mk_tactic (fun status -> 
156                           let (proof, goal) = status in
157                           let _,metasenv,_,_ = proof in
158                            let _,context,_ = CicUtil.lookup_meta goal metasenv in
159                             let new_context_len = List.length context in   
160                              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));
161                              let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim + (new_context_len - old_context_len) - 1 in
162                              let hyp_name =
163                               match List.nth context new_nr_of_hyp_still_to_elim with
164                                  None
165                                | Some (Cic.Anonymous,_) -> assert false
166                                | Some (Cic.Name name,_) -> name
167                              in
168                              ProofEngineTypes.apply_tactic
169                              (T.then_ 
170                                 ~start:(
171                                   if (term'==term) (* if it's the first application of elim, there's no need to clear the hyp *) 
172                                    then begin debug_print ("%%%%%%% no clear"); T.id_tac end
173                                    else begin debug_print ("%%%%%%% clear " ^ (string_of_int (new_nr_of_hyp_still_to_elim))); (S.clear ~hyp:hyp_name) end)
174                                 ~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)))
175                                 status
176                         )))
177                       status
178               | _ ->
179                    let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim - 1 in 
180                     warn ("fail; hyp=" ^ (string_of_int new_nr_of_hyp_still_to_elim));
181                     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
182          else (* no hyp to elim left in this goal *)
183           ProofEngineTypes.apply_tactic T.id_tac status
184
185         in
186          elim_clear_tac ~term':term ~nr_of_hyp_still_to_elim:1 status
187  in
188   ProofEngineTypes.mk_tactic (decompose_tac uris_choice_callback term)
189 ;;
190
191