1 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
27 module P = PrimitiveTactics
29 module S = ProofEngineStructuralRules
30 module F = FreshNamesGenerator
31 module E = ProofEngineTypes
32 module H = ProofEngineHelpers
33 module Q = MetadataQuery
36 let induction_tac ~term status =
37 let (proof, goal) = status 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 *)
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)
56 (* unexported tactics *******************************************************)
58 let get_name context index =
59 try match List.nth context (pred index) with
60 | Some (Cic.Name name, _) -> Some name
62 with Invalid_argument "List.nth" -> None
64 let rec scan_tac ~old_context_length ~index ~tactic =
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
71 match get_name context index with
72 | _ when index <= 0 -> (proof, [goal])
73 | None -> aux (pred index)
75 let tac = T.then_ ~start:(tactic ~what)
76 ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
78 try E.apply_tactic tac status
79 with E.Fail _ -> aux (pred index)
80 in aux (index + context_length - old_context_length - 1)
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
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)
99 E.apply_tactic tac status
100 else raise (E.Fail "unexported elim_clear: not an eliminable type")
102 E.mk_tactic elim_clear_tac
104 (* elim type ****************************************************************)
106 let elim_type_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[])
108 let elim what = P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what in
109 let elim_type_tac status =
110 let tac = T.thens ~start: (P.cut_tac what)
111 ~continuations:[elim (C.Rel 1); T.id_tac]
113 E.apply_tactic tac status
115 E.mk_tactic elim_type_tac
117 (* decompose ****************************************************************)
119 (* robaglia --------------------------------------------------------------- *)
121 (** perform debugging output? *)
123 let debug_print = fun _ -> ()
125 (** debugging print *)
128 debug_print ("DECOMPOSE: " ^ s)
130 (* search in term the Inductive Types and return a list of uris as triples like this: (uri,typeno,exp_named_subst) *)
131 let search_inductive_types ty =
132 let rec aux types = function
133 | C.MutInd (uri, typeno, _) when (not (List.mem (uri, typeno) types)) ->
134 (uri, typeno) :: types
135 | C.Appl applist -> List.fold_left aux types applist
139 (* N.B: in un caso tipo (and A forall C:Prop.(or B C)) l'or *non* viene selezionato! *)
141 (* roba seria ------------------------------------------------------------- *)
143 let decompose_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[])
144 ?(user_types=[]) ~dbd what =
145 let decompose_tac status =
146 let (proof, goal) = status in
147 let _, metasenv,_,_ = proof in
148 let _, context, _ = CicUtil.lookup_meta goal metasenv in
149 let types = List.rev_append user_types (Q.decomposables dbd) in
150 let tactic = elim_clear_tac ~mk_fresh_name_callback ~types in
151 let old_context_length = List.length context in
152 let tac = T.then_ ~start:(tactic ~what)
153 ~continuation:(scan_tac ~old_context_length ~index:1 ~tactic)
155 E.apply_tactic tac status
157 E.mk_tactic decompose_tac
160 module R = CicReduction
162 let rec elim_clear_tac ~term' ~nr_of_hyp_still_to_elim status =
163 let (proof, goal) = status in
164 warn ("nr_of_hyp_still_to_elim=" ^ (string_of_int nr_of_hyp_still_to_elim));
165 if nr_of_hyp_still_to_elim <> 0 then
166 let _,metasenv,_,_ = proof in
167 let _,context,_ = CicUtil.lookup_meta goal metasenv in
168 let old_context_len = List.length context in
170 CicTypeChecker.type_of_aux' metasenv context term'
171 CicUniv.empty_ugraph in
172 warn ("elim_clear termty= " ^ CicPp.ppterm termty);
174 C.MutInd (uri,typeno,exp_named_subst)
175 | C.Appl((C.MutInd (uri,typeno,exp_named_subst))::_)
176 when (List.mem (uri,typeno,exp_named_subst) urilist) ->
177 warn ("elim " ^ CicPp.ppterm termty);
178 ProofEngineTypes.apply_tactic
180 ~start:(P.elim_intros_simpl_tac term')
182 (* clear the hyp that has just been eliminated *)
183 ProofEngineTypes.mk_tactic (fun status ->
184 let (proof, goal) = status in
185 let _,metasenv,_,_ = proof in
186 let _,context,_ = CicUtil.lookup_meta goal metasenv in
187 let new_context_len = List.length context in
188 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));
189 let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim + (new_context_len - old_context_len) - 1 in
191 match List.nth context new_nr_of_hyp_still_to_elim with
193 | Some (Cic.Anonymous,_) -> assert false
194 | Some (Cic.Name name,_) -> name
196 ProofEngineTypes.apply_tactic
199 if (term'==term) (* if it's the first application of elim, there's no need to clear the hyp *)
200 then begin debug_print ("%%%%%%% no clear"); T.id_tac end
201 else begin debug_print ("%%%%%%% clear " ^ (string_of_int (new_nr_of_hyp_still_to_elim))); (S.clear ~hyp:hyp_name) end)
202 ~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)))
207 let new_nr_of_hyp_still_to_elim = nr_of_hyp_still_to_elim - 1 in
208 warn ("fail; hyp=" ^ (string_of_int new_nr_of_hyp_still_to_elim));
209 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
210 else (* no hyp to elim left in this goal *)
211 ProofEngineTypes.apply_tactic T.id_tac status
214 elim_clear_tac ~term':term ~nr_of_hyp_still_to_elim:1 status