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/.
30 module PET = ProofEngineTypes
31 module PER = ProofEngineReduction
32 module PEH = ProofEngineHelpers
33 module PESR = ProofEngineStructuralRules
34 module P = PrimitiveTactics
36 module R = CicReduction
37 module S = CicSubstitution
38 module TC = CicTypeChecker
39 module LO = LibraryObjects
40 module DTI = DoubleTypeInference
43 let rec rewrite_tac ~direction ~pattern:(wanted,hyps_pat,concl_pat) equality =
44 let _rewrite_tac status =
45 assert (wanted = None); (* this should be checked syntactically *)
46 let proof,goal = status in
47 let curi, metasenv, _subst, pbo, pty, attrs = proof in
48 let (metano,context,gty) = CicUtil.lookup_meta goal metasenv in
50 CicTypeChecker.type_of_aux' metasenv context gty CicUniv.oblivion_ugraph in
55 (rewrite_tac ~direction
56 ~pattern:(None,[he],None) equality)
57 (rewrite_tac ~direction ~pattern:(None,tl,concl_pat)
60 | [_] as hyps_pat when concl_pat <> None ->
63 (rewrite_tac ~direction
64 ~pattern:(None,hyps_pat,None) equality)
65 (rewrite_tac ~direction ~pattern:(None,[],concl_pat)
69 let arg,dir2,tac,concl_pat,gty =
71 [] -> None,true,(fun ~term _ -> P.exact_tac term),concl_pat,gty
76 | Some (Cic.Name s,Cic.Decl ty)::_ when name = s ->
77 Cic.Rel n, S.lift n ty
78 | Some (Cic.Name s,Cic.Def _)::_ when name = s -> assert false (*CSC: not implemented yet! But does this make any sense?*)
79 | _::tl -> find_hyp (n+1) tl
81 let arg,gty = find_hyp 1 context in
82 let dummy = "dummy" in
87 [PESR.rename [name] [dummy];
89 ~mk_fresh_name_callback:(fun _ _ _ ~typ -> Cic.Name name) term;
91 ReductionTactics.change_tac
93 (None,[name,Cic.Implicit (Some `Hole)], None)
94 (ProofEngineTypes.const_lazy_term typ);
98 | _::_ -> assert false
100 let if_right_to_left do_not_change a b =
102 | `RightToLeft -> if do_not_change then a else b
103 | `LeftToRight -> if do_not_change then b else a
106 CicTypeChecker.type_of_aux' metasenv context equality
107 CicUniv.empty_ugraph in
108 let (ty_eq,metasenv',arguments,fresh_meta) =
109 TermUtil.saturate_term
110 (ProofEngineHelpers.new_meta_of_proof proof) metasenv context ty_eq 0 in
112 if List.length arguments = 0 then
115 C.Appl (equality :: arguments) in
116 (* t1x is t2 if we are rewriting in an hypothesis *)
117 let eq_ind, ty, t1, t2, t1x =
119 | C.Appl [C.MutInd (uri, 0, []); ty; t1; t2]
120 when LibraryObjects.is_eq_URI uri ->
124 if_right_to_left dir2
125 LibraryObjects.eq_ind_URI LibraryObjects.eq_ind_r_URI
127 if_right_to_left dir2
128 LibraryObjects.eq_rec_URI LibraryObjects.eq_rec_r_URI
130 if_right_to_left dir2
131 LibraryObjects.eq_rect_URI LibraryObjects.eq_rect_r_URI
133 let eq_ind = C.Const (ind_uri uri,[]) in
135 if_right_to_left true (eq_ind,ty,t2,t1,t2) (eq_ind,ty,t1,t2,t1)
137 if_right_to_left true (eq_ind,ty,t1,t2,t2) (eq_ind,ty,t2,t1,t1)
138 | _ -> raise (PET.Fail (lazy "Rewrite: argument is not a proof of an equality")) in
139 (* now we always do as if direction was `LeftToRight *)
141 FreshNamesGenerator.mk_fresh_name
142 ~subst:[] metasenv' context C.Anonymous ~typ:ty in
143 let lifted_t1 = S.lift 1 t1x in
144 let lifted_gty = S.lift 1 gty in
145 let lifted_conjecture =
146 metano,(Some (fresh_name,Cic.Decl ty))::context,lifted_gty in
148 let lifted_concl_pat =
151 | Some term -> Some (S.lift 1 term) in
153 let distance = pred (List.length c - List.length context) in
154 S.lift distance lifted_t1, m, u),[],lifted_concl_pat
156 let subst,metasenv',ugraph,_,selected_terms_with_context =
157 ProofEngineHelpers.select
158 ~metasenv:metasenv' ~ugraph ~conjecture:lifted_conjecture
159 ~pattern:lifted_pattern in
160 let metasenv' = CicMetaSubst.apply_subst_metasenv subst metasenv' in
162 (* Note: Rel 1 does not live in the context context_of_t *)
163 (* The replace_lifting_csc 0 function will take care of lifting it *)
164 (* to context_of_t *)
166 (fun (context_of_t,t) (l1,l2) -> t::l1, Cic.Rel 1::l2)
167 selected_terms_with_context ([],[]) in
168 let t1 = CicMetaSubst.apply_subst subst t1 in
169 let t2 = CicMetaSubst.apply_subst subst t2 in
170 let ty = CicMetaSubst.apply_subst subst ty in
171 let pbo = CicMetaSubst.apply_subst subst pbo in
172 let pty = CicMetaSubst.apply_subst subst pty in
173 let equality = CicMetaSubst.apply_subst subst equality in
175 ProofEngineReduction.replace_lifting_csc 0
176 ~equality:(==) ~what ~with_what:with_what ~where:lifted_gty in
177 let abstr_gty = CicMetaSubst.apply_subst subst abstr_gty in
178 let pred = C.Lambda (fresh_name, ty, abstr_gty) in
179 (* The argument is either a meta if we are rewriting in the conclusion
180 or the hypothesis if we are rewriting in an hypothesis *)
181 let metasenv',arg,newtyp =
184 let gty' = S.subst t2 abstr_gty in
186 CicMkImplicit.identity_relocation_list_for_metavariable context in
187 let metasenv' = (fresh_meta,context,gty')::metasenv' in
188 metasenv', C.Meta (fresh_meta,irl), Cic.Rel (-1) (* dummy term, never used *)
190 let gty' = S.subst t1 abstr_gty in
194 C.Appl [eq_ind ; ty ; t2 ; pred ; arg ; t1 ;equality]
199 (tac ~term:exact_proof newtyp) ((curi,metasenv',_subst,pbo,pty, attrs),goal)
202 goals@(ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv
203 ~newmetasenv:metasenv')
206 with (* FG: this should be PET.Fail _ *)
207 TC.TypeCheckerFailure _ ->
208 let msg = lazy "rewrite: nothing to rewrite" in
211 PET.mk_tactic _rewrite_tac
213 let rewrite_tac ~direction ~pattern equality names =
214 let _, hyps_pat, _ = pattern in
215 let froms = List.map fst hyps_pat in
216 let start = rewrite_tac ~direction ~pattern equality in
217 let continuation = PESR.rename ~froms ~tos:names in
218 if names = [] then start else T.then_ ~start ~continuation
220 let rewrite_simpl_tac ~direction ~pattern equality names =
222 ~start:(rewrite_tac ~direction ~pattern equality names)
224 (ReductionTactics.simpl_tac
225 ~pattern:(ProofEngineTypes.conclusion_pattern None))
228 let replace_tac ~(pattern: ProofEngineTypes.lazy_pattern) ~with_what =
229 let replace_tac ~(pattern: ProofEngineTypes.lazy_pattern) ~with_what status =
230 let _wanted, hyps_pat, concl_pat = pattern in
231 let (proof, goal) = status in
232 let uri,metasenv,_subst,pbo,pty, attrs = proof in
233 let (_,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
234 assert (hyps_pat = []); (*CSC: not implemented yet *)
236 match LibraryObjects.eq_URI () with
238 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default equality first. Please use the \"default\" command"))
240 let context_len = List.length context in
241 let subst,metasenv,u,_,selected_terms_with_context =
242 ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph
243 ~conjecture ~pattern in
244 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
245 let with_what, metasenv, u = with_what context metasenv u in
246 let with_what = CicMetaSubst.apply_subst subst with_what in
247 let pbo = CicMetaSubst.apply_subst subst pbo in
248 let pty = CicMetaSubst.apply_subst subst pty in
249 let status = (uri,metasenv,_subst,pbo,pty, attrs),goal in
250 let ty_of_with_what,u =
251 CicTypeChecker.type_of_aux'
252 metasenv context with_what CicUniv.empty_ugraph in
254 match selected_terms_with_context with
255 [] -> raise (ProofEngineTypes.Fail (lazy "Replace: no term selected"))
258 (fun (context_of_t,t) ->
261 let context_of_t_len = List.length context_of_t in
262 if context_of_t_len = context_len then t
264 (let t_in_context,subst,metasenv' =
265 CicMetaSubst.delift_rels [] metasenv
266 (context_of_t_len - context_len) t
269 assert (metasenv = metasenv');
272 CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
273 (*CSC: we could implement something stronger by completely changing
274 the semantics of the tactic *)
275 raise (ProofEngineTypes.Fail
276 (lazy "Replace: one of the selected terms is not closed")) in
277 let ty_of_t_in_context,u = (* TASSI: FIXME *)
278 CicTypeChecker.type_of_aux' metasenv context t_in_context
279 CicUniv.empty_ugraph in
280 let b,u = CicReduction.are_convertible ~metasenv context
281 ty_of_with_what ty_of_t_in_context u in
283 let concl_pat_for_t = ProofEngineHelpers.pattern_of ~term:ty [t] in
284 let pattern_for_t = None,[],Some concl_pat_for_t in
285 t_in_context,pattern_for_t
288 (ProofEngineTypes.Fail
289 (lazy "Replace: one of the selected terms and the term to be replaced with have not convertible types"))
291 let rec aux n whats (status : ProofEngineTypes.status) =
293 [] -> ProofEngineTypes.apply_tactic T.id_tac status
294 | (what,lazy_pattern)::tl ->
295 let what = S.lift n what in
296 let with_what = S.lift n with_what in
297 let ty_of_with_what = S.lift n ty_of_with_what in
298 ProofEngineTypes.apply_tactic
303 (C.MutInd (eq_URI, 0, [])) ;
310 rewrite_tac ~direction:`LeftToRight ~pattern:lazy_pattern (C.Rel 1) [])
314 ProofEngineTypes.mk_tactic
315 (function ((proof,goal) as status) ->
316 let _,metasenv,_subst,_,_, _ = proof in
317 let _,context,_ = CicUtil.lookup_meta goal metasenv in
320 match List.hd context with
321 Some (Cic.Name name,_) -> [name]
323 with (Failure "hd") -> assert false
325 ProofEngineTypes.apply_tactic
326 (PESR.clear ~hyps) status))
327 ~continuation:(aux_tac (n + 1) tl));
330 and aux_tac n tl = ProofEngineTypes.mk_tactic (aux n tl) in
331 aux 0 whats (status : ProofEngineTypes.status)
333 ProofEngineTypes.mk_tactic (replace_tac ~pattern ~with_what)
337 (* All these tacs do is applying the right constructor/theorem *)
339 let reflexivity_tac =
340 IntroductionTactics.constructor_tac ~n:1
344 let symmetry_tac (proof, goal) =
345 let (_,metasenv,_subst,_,_, _) = proof in
346 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
347 match (R.whd context ty) with
348 (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
349 when LibraryObjects.is_eq_URI uri ->
350 ProofEngineTypes.apply_tactic
351 (PrimitiveTactics.apply_tac
352 ~term: (C.Const (LibraryObjects.sym_eq_URI uri, [])))
355 | _ -> raise (ProofEngineTypes.Fail (lazy "Symmetry failed"))
357 ProofEngineTypes.mk_tactic symmetry_tac
360 let transitivity_tac ~term =
361 let transitivity_tac ~term status =
362 let (proof, goal) = status in
363 let (_,metasenv,_subst,_,_, _) = proof in
364 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
365 match (R.whd context ty) with
366 (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
367 when LibraryObjects.is_eq_URI uri ->
368 ProofEngineTypes.apply_tactic
370 ~start:(PrimitiveTactics.apply_tac
371 ~term: (C.Const (LibraryObjects.trans_eq_URI uri, [])))
373 [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
376 | _ -> raise (ProofEngineTypes.Fail (lazy "Transitivity failed"))
378 ProofEngineTypes.mk_tactic (transitivity_tac ~term)