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: ProofEngineTypes.lazy_pattern) equality =
44 let _rewrite_tac ~direction ~pattern:(wanted,hyps_pat,concl_pat) equality status
46 assert (wanted = None); (* this should be checked syntactically *)
47 let proof,goal = status in
48 let curi, metasenv, pbo, pty = proof in
49 let (metano,context,gty) = CicUtil.lookup_meta goal metasenv in
54 (rewrite_tac ~direction
55 ~pattern:(None,[he],None) equality)
56 (rewrite_tac ~direction ~pattern:(None,tl,concl_pat)
59 | [_] as hyps_pat when concl_pat <> None ->
62 (rewrite_tac ~direction
63 ~pattern:(None,hyps_pat,None) equality)
64 (rewrite_tac ~direction ~pattern:(None,[],concl_pat)
68 let arg,dir2,tac,concl_pat,gty =
70 [] -> None,true,(fun ~term _ -> P.exact_tac term),concl_pat,gty
75 | Some (Cic.Name s,Cic.Decl ty)::_ when name = s ->
76 Cic.Rel n, S.lift n ty
77 | Some (Cic.Name s,Cic.Def _)::_ -> assert false (*CSC: not implemented yet! But does this make any sense?*)
78 | _::tl -> find_hyp (n+1) tl
80 let arg,gty = find_hyp 1 context in
81 let dummy = "dummy" in
86 [ProofEngineStructuralRules.rename name dummy;
88 ~mk_fresh_name_callback:(fun _ _ _ ~typ -> Cic.Name name) term;
89 ProofEngineStructuralRules.clearbody name;
90 ReductionTactics.change_tac
92 (None,[name,Cic.Implicit (Some `Hole)], None)
93 (ProofEngineTypes.const_lazy_term typ);
94 ProofEngineStructuralRules.clear [dummy]
97 | _::_ -> assert false
99 let if_right_to_left do_not_change a b =
101 | `RightToLeft -> if do_not_change then a else b
102 | `LeftToRight -> if do_not_change then b else a
105 CicTypeChecker.type_of_aux' metasenv context equality
106 CicUniv.empty_ugraph in
107 let (ty_eq,metasenv',arguments,fresh_meta) =
108 TermUtil.saturate_term
109 (ProofEngineHelpers.new_meta_of_proof proof) metasenv context ty_eq 0 in
111 if List.length arguments = 0 then
114 C.Appl (equality :: arguments) in
115 (* t1x is t2 if we are rewriting in an hypothesis *)
116 let eq_ind, ty, t1, t2, t1x =
118 | C.Appl [C.MutInd (uri, 0, []); ty; t1; t2]
119 when LibraryObjects.is_eq_URI uri ->
121 if_right_to_left dir2
122 LibraryObjects.eq_ind_URI LibraryObjects.eq_ind_r_URI
124 let eq_ind = C.Const (ind_uri uri,[]) in
126 if_right_to_left true (eq_ind,ty,t2,t1,t2) (eq_ind,ty,t1,t2,t1)
128 if_right_to_left true (eq_ind,ty,t1,t2,t2) (eq_ind,ty,t2,t1,t1)
129 | _ -> raise (PET.Fail (lazy "Rewrite: argument is not a proof of an equality")) in
130 (* now we always do as if direction was `LeftToRight *)
132 FreshNamesGenerator.mk_fresh_name
133 ~subst:[] metasenv' context C.Anonymous ~typ:ty in
134 let lifted_t1 = S.lift 1 t1x in
135 let lifted_gty = S.lift 1 gty in
136 let lifted_conjecture =
137 metano,(Some (fresh_name,Cic.Decl ty))::context,lifted_gty in
139 let lifted_concl_pat =
142 | Some term -> Some (S.lift 1 term) in
143 Some (fun _ m u -> lifted_t1, m, u),[],lifted_concl_pat
145 let subst,metasenv',ugraph,_,selected_terms_with_context =
146 ProofEngineHelpers.select
147 ~metasenv:metasenv' ~ugraph ~conjecture:lifted_conjecture
148 ~pattern:lifted_pattern in
149 let metasenv' = CicMetaSubst.apply_subst_metasenv subst metasenv' in
151 (* Note: Rel 1 does not live in the context context_of_t *)
152 (* The replace_lifting_csc 0 function will take care of lifting it *)
153 (* to context_of_t *)
155 (fun (context_of_t,t) (l1,l2) -> t::l1, Cic.Rel 1::l2)
156 selected_terms_with_context ([],[]) in
157 let t1 = CicMetaSubst.apply_subst subst t1 in
158 let t2 = CicMetaSubst.apply_subst subst t2 in
159 let ty = CicMetaSubst.apply_subst subst ty in
160 let pbo = CicMetaSubst.apply_subst subst pbo in
161 let pty = CicMetaSubst.apply_subst subst pty in
162 let equality = CicMetaSubst.apply_subst subst equality in
164 ProofEngineReduction.replace_lifting_csc 0
165 ~equality:(==) ~what ~with_what:with_what ~where:lifted_gty in
166 let abstr_gty = CicMetaSubst.apply_subst subst abstr_gty in
167 let pred = C.Lambda (fresh_name, ty, abstr_gty) in
168 (* The argument is either a meta if we are rewriting in the conclusion
169 or the hypothesis if we are rewriting in an hypothesis *)
170 let metasenv',arg,newtyp =
173 let gty' = S.subst t2 abstr_gty in
175 CicMkImplicit.identity_relocation_list_for_metavariable context in
176 let metasenv' = (fresh_meta,context,gty')::metasenv' in
177 metasenv', C.Meta (fresh_meta,irl), Cic.Rel (-1) (* dummy term, never used *)
179 let gty' = S.subst t1 abstr_gty in
183 C.Appl [eq_ind ; ty ; t2 ; pred ; arg ; t1 ;equality]
188 (tac ~term:exact_proof newtyp) ((curi,metasenv',pbo,pty),goal)
191 goals@(ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv
192 ~newmetasenv:metasenv')
195 with (* FG: this should be PET.Fail _ *)
196 TC.TypeCheckerFailure _ ->
197 let msg = lazy "rewrite: nothing to rewrite" in
200 ProofEngineTypes.mk_tactic (_rewrite_tac ~direction ~pattern equality)
203 let rewrite_simpl_tac ~direction ~pattern equality =
204 let rewrite_simpl_tac ~direction ~pattern equality status =
205 ProofEngineTypes.apply_tactic
207 ~start:(rewrite_tac ~direction ~pattern equality)
209 (ReductionTactics.simpl_tac
210 ~pattern:(ProofEngineTypes.conclusion_pattern None)))
213 ProofEngineTypes.mk_tactic (rewrite_simpl_tac ~direction ~pattern equality)
216 let replace_tac ~(pattern: ProofEngineTypes.lazy_pattern) ~with_what =
217 let replace_tac ~(pattern: ProofEngineTypes.lazy_pattern) ~with_what status =
218 let _wanted, hyps_pat, concl_pat = pattern in
219 let (proof, goal) = status in
220 let uri,metasenv,pbo,pty = proof in
221 let (_,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
222 assert (hyps_pat = []); (*CSC: not implemented yet *)
224 match LibraryObjects.eq_URI () with
226 | None -> raise (ProofEngineTypes.Fail (lazy "You need to register the default equality first. Please use the \"default\" command"))
228 let context_len = List.length context in
229 let subst,metasenv,u,_,selected_terms_with_context =
230 ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph
231 ~conjecture ~pattern in
232 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
233 let with_what, metasenv, u = with_what context metasenv u in
234 let with_what = CicMetaSubst.apply_subst subst with_what in
235 let pbo = CicMetaSubst.apply_subst subst pbo in
236 let pty = CicMetaSubst.apply_subst subst pty in
237 let status = (uri,metasenv,pbo,pty),goal in
238 let ty_of_with_what,u =
239 CicTypeChecker.type_of_aux'
240 metasenv context with_what CicUniv.empty_ugraph in
242 match selected_terms_with_context with
243 [] -> raise (ProofEngineTypes.Fail (lazy "Replace: no term selected"))
246 (fun (context_of_t,t) ->
249 let context_of_t_len = List.length context_of_t in
250 if context_of_t_len = context_len then t
252 (let t_in_context,subst,metasenv' =
253 CicMetaSubst.delift_rels [] metasenv
254 (context_of_t_len - context_len) t
257 assert (metasenv = metasenv');
260 CicMetaSubst.DeliftingARelWouldCaptureAFreeVariable ->
261 (*CSC: we could implement something stronger by completely changing
262 the semantics of the tactic *)
263 raise (ProofEngineTypes.Fail
264 (lazy "Replace: one of the selected terms is not closed")) in
265 let ty_of_t_in_context,u = (* TASSI: FIXME *)
266 CicTypeChecker.type_of_aux' metasenv context t_in_context
267 CicUniv.empty_ugraph in
268 let b,u = CicReduction.are_convertible ~metasenv context
269 ty_of_with_what ty_of_t_in_context u in
271 let concl_pat_for_t = ProofEngineHelpers.pattern_of ~term:ty [t] in
272 let pattern_for_t = None,[],Some concl_pat_for_t in
273 t_in_context,pattern_for_t
276 (ProofEngineTypes.Fail
277 (lazy "Replace: one of the selected terms and the term to be replaced with have not convertible types"))
279 let rec aux n whats status =
281 [] -> ProofEngineTypes.apply_tactic T.id_tac status
282 | (what,lazy_pattern)::tl ->
283 let what = S.lift n what in
284 let with_what = S.lift n with_what in
285 let ty_of_with_what = S.lift n ty_of_with_what in
286 ProofEngineTypes.apply_tactic
291 (C.MutInd (eq_URI, 0, [])) ;
298 rewrite_tac ~direction:`LeftToRight ~pattern:lazy_pattern (C.Rel 1))
302 ProofEngineTypes.mk_tactic
303 (function ((proof,goal) as status) ->
304 let _,metasenv,_,_ = proof in
305 let _,context,_ = CicUtil.lookup_meta goal metasenv in
308 match List.hd context with
309 Some (Cic.Name name,_) -> [name]
311 with (Failure "hd") -> assert false
313 ProofEngineTypes.apply_tactic
314 (ProofEngineStructuralRules.clear ~hyps) status))
315 ~continuation:(aux_tac (n + 1) tl));
318 and aux_tac n tl = ProofEngineTypes.mk_tactic (aux n tl) in
321 ProofEngineTypes.mk_tactic (replace_tac ~pattern ~with_what)
325 (* All these tacs do is applying the right constructor/theorem *)
327 let reflexivity_tac =
328 IntroductionTactics.constructor_tac ~n:1
332 let symmetry_tac (proof, goal) =
333 let (_,metasenv,_,_) = proof in
334 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
335 match (R.whd context ty) with
336 (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
337 when LibraryObjects.is_eq_URI uri ->
338 ProofEngineTypes.apply_tactic
339 (PrimitiveTactics.apply_tac
340 ~term: (C.Const (LibraryObjects.sym_eq_URI uri, [])))
343 | _ -> raise (ProofEngineTypes.Fail (lazy "Symmetry failed"))
345 ProofEngineTypes.mk_tactic symmetry_tac
348 let transitivity_tac ~term =
349 let transitivity_tac ~term status =
350 let (proof, goal) = status in
351 let (_,metasenv,_,_) = proof in
352 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
353 match (R.whd context ty) with
354 (C.Appl [(C.MutInd (uri, 0, [])); _; _; _])
355 when LibraryObjects.is_eq_URI uri ->
356 ProofEngineTypes.apply_tactic
358 ~start:(PrimitiveTactics.apply_tac
359 ~term: (C.Const (LibraryObjects.trans_eq_URI uri, [])))
361 [PrimitiveTactics.exact_tac ~term ; T.id_tac ; T.id_tac])
364 | _ -> raise (ProofEngineTypes.Fail (lazy "Transitivity failed"))
366 ProofEngineTypes.mk_tactic (transitivity_tac ~term)
369 (* FG: subst tactic *********************************************************)
371 (* FG: This should be replaced by T.try_tactic *)
372 let try_tactic ~tactic =
373 let try_tactic status =
374 try PET.apply_tactic tactic status with
375 | PET.Fail _ -> PET.apply_tactic T.id_tac status
377 PET.mk_tactic try_tactic
379 let rec lift_rewrite_tac ~context ~direction ~pattern equality =
380 let lift_rewrite_tac status =
381 let (proof, goal) = status in
382 let (_, metasenv, _, _) = proof in
383 let _, new_context, _ = CicUtil.lookup_meta goal metasenv in
384 let n = List.length new_context - List.length context in
385 let equality = if n > 0 then S.lift n equality else equality in
386 PET.apply_tactic (rewrite_tac ~direction ~pattern equality) status
388 PET.mk_tactic lift_rewrite_tac
391 let msg0 = lazy "Subst: not found in context"
392 let msg1 = lazy "Subst: not a simple equality"
393 let msg2 = lazy "Subst: recursive equation"
396 let hole = C.Implicit (Some `Hole) in
397 let subst_tac status =
398 let (proof, goal) = status in
399 let (_, metasenv, _, _) = proof in
400 let _, context, _ = CicUtil.lookup_meta goal metasenv in
401 let what = match PEH.get_rel context hyp with
403 | None -> raise (PET.Fail msg0)
405 let ty, _ = TC.type_of_aux' metasenv context what CicUniv.empty_ugraph in
406 let direction, i, t = match ty with
407 | (C.Appl [(C.MutInd (uri, 0, [])); _; C.Rel i; t])
408 when LO.is_eq_URI uri -> `LeftToRight, i, t
409 | (C.Appl [(C.MutInd (uri, 0, [])); _; t; C.Rel i])
410 when LO.is_eq_URI uri -> `RightToLeft, i, t
411 | _ -> raise (PET.Fail msg1)
413 let rewrite pattern =
414 let tactic = lift_rewrite_tac ~context ~direction ~pattern what in
417 let var = match PEH.get_name context i with
419 | None -> raise (PET.Fail msg0)
421 if DTI.does_not_occur i t then () else raise (PET.Fail msg2);
422 let map self = function
423 | Some (C.Name s, _) when s <> self ->
424 Some (rewrite (None, [(s, hole)], None))
427 let rew_hips = HEL.list_rev_map_filter (map hyp) context in
428 let rew_concl = rewrite (None, [], Some hole) in
429 let clear = PESR.clear ~hyps:[hyp; var] in
430 let tactics = List.rev_append (rew_concl :: rew_hips) [clear] in
431 PET.apply_tactic (T.seq ~tactics) status
433 PET.mk_tactic subst_tac
436 let subst hyp = try_tactic ~tactic:(subst_tac hyp) in
438 | Some (C.Name s, _) -> Some (subst s)
441 let subst_tac status =
442 let (proof, goal) = status in
443 let (_, metasenv, _, _) = proof in
444 let _, context, _ = CicUtil.lookup_meta goal metasenv in
445 let tactics = HEL.list_rev_map_filter map context in
446 PET.apply_tactic (T.seq ~tactics) status
448 PET.mk_tactic subst_tac