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 exception TheTypeOfTheCurrentGoalIsAMetaICannotChooseTheRightElimiantionPrinciple
31 exception NotAnInductiveTypeToEliminate
32 exception WrongUriToVariable of string
33 exception NotAnEliminator
35 (* lambda_abstract newmeta ty *)
36 (* returns a triple [bo],[context],[ty'] where *)
37 (* [ty] = Pi/LetIn [context].[ty'] ([context] is a vector!) *)
38 (* and [bo] = Lambda/LetIn [context].(Meta [newmeta]) *)
39 (* So, lambda_abstract is the core of the implementation of *)
40 (* the Intros tactic. *)
41 (* howmany = -1 means Intros, howmany > 0 means Intros n *)
42 let lambda_abstract ?(howmany=(-1)) metasenv context newmeta ty mk_fresh_name =
44 let rec collect_context context howmany do_whd ty =
48 CicMkImplicit.identity_relocation_list_for_metavariable context
50 context, ty, (C.Meta (newmeta,irl))
53 C.Cast (te,_) -> collect_context context howmany do_whd te
55 let n' = mk_fresh_name metasenv context n ~typ:s in
56 let (context',ty,bo) =
57 let ctx = (Some (n',(C.Decl s)))::context in
58 collect_context ctx (howmany - 1) do_whd t
60 (context',ty,C.Lambda(n',s,bo))
62 let (context',ty,bo) =
63 collect_context ((Some (n,(C.Def (s,None))))::context) (howmany - 1) do_whd t
65 (context',ty,C.LetIn(n,s,bo))
69 CicMkImplicit.identity_relocation_list_for_metavariable context
71 context, t, (C.Meta (newmeta,irl))
73 let t = CicReduction.whd ~delta:true context t in
74 collect_context context howmany false t
76 raise (Fail (lazy "intro(s): not enough products or let-ins"))
78 collect_context context howmany true ty
80 let eta_expand metasenv context t arg =
81 let module T = CicTypeChecker in
82 let module S = CicSubstitution in
86 t' when t' = S.lift n arg -> C.Rel (1 + n)
87 | C.Rel m -> if m <= n then C.Rel m else C.Rel (m+1)
88 | C.Var (uri,exp_named_subst) ->
89 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
90 C.Var (uri,exp_named_subst')
93 List.map (function None -> None | Some t -> Some (aux n t)) l
97 | C.Implicit _ as t -> t
98 | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
99 | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
100 | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
101 | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
102 | C.Appl l -> C.Appl (List.map (aux n) l)
103 | C.Const (uri,exp_named_subst) ->
104 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
105 C.Const (uri,exp_named_subst')
106 | C.MutInd (uri,i,exp_named_subst) ->
107 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
108 C.MutInd (uri,i,exp_named_subst')
109 | C.MutConstruct (uri,i,j,exp_named_subst) ->
110 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
111 C.MutConstruct (uri,i,j,exp_named_subst')
112 | C.MutCase (sp,i,outt,t,pl) ->
113 C.MutCase (sp,i,aux n outt, aux n t,
116 let tylen = List.length fl in
119 (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
122 C.Fix (i, substitutedfl)
124 let tylen = List.length fl in
127 (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
130 C.CoFix (i, substitutedfl)
131 and aux_exp_named_subst n =
132 List.map (function uri,t -> uri,aux n t)
135 T.type_of_aux' metasenv context arg CicUniv.empty_ugraph (* TASSI: FIXME *)
138 FreshNamesGenerator.mk_fresh_name ~subst:[]
139 metasenv context (Cic.Name "Heta") ~typ:argty
141 (C.Appl [C.Lambda (fresh_name,argty,aux 0 t) ; arg])
143 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
144 let classify_metas newmeta in_subst_domain subst_in metasenv =
146 (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
147 if in_subst_domain i then
148 old_uninst,new_uninst
150 let ty' = subst_in canonical_context ty in
151 let canonical_context' =
153 (fun entry canonical_context' ->
156 Some (n,Cic.Decl s) ->
157 Some (n,Cic.Decl (subst_in canonical_context' s))
158 | Some (n,Cic.Def (s,None)) ->
159 Some (n,Cic.Def ((subst_in canonical_context' s),None))
161 | Some (n,Cic.Def (bo,Some ty)) ->
165 (subst_in canonical_context' bo,
166 Some (subst_in canonical_context' ty)))
168 entry'::canonical_context'
169 ) canonical_context []
172 ((i,canonical_context',ty')::old_uninst),new_uninst
174 old_uninst,((i,canonical_context',ty')::new_uninst)
177 (* Useful only inside apply_tac *)
179 generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
181 let module C = Cic in
183 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
184 CicUtil.params_of_obj o
186 let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
187 let next_fresh_meta = ref newmeta in
188 let newmetasenvfragment = ref [] in
189 let exp_named_subst_diff = ref [] in
195 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
197 C.Variable (_,_,ty,_,_) ->
198 CicSubstitution.subst_vars !exp_named_subst_diff ty
199 | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
201 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
203 C.Sort (C.Type _) as s -> (* TASSI: ?? *)
204 let fresh_meta = !next_fresh_meta in
205 let fresh_meta' = fresh_meta + 1 in
206 next_fresh_meta := !next_fresh_meta + 2 ;
207 let subst_item = uri,C.Meta (fresh_meta',[]) in
208 newmetasenvfragment :=
209 (fresh_meta,[],C.Sort (C.Type (CicUniv.fresh()))) ::
211 (fresh_meta',[],C.Meta (fresh_meta,[])) :: !newmetasenvfragment ;
212 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
213 subst_item::(aux (tl,[]))
217 CicMkImplicit.identity_relocation_list_for_metavariable context
219 let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
220 newmetasenvfragment :=
221 (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
222 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
223 incr next_fresh_meta ;
224 subst_item::(aux (tl,[]))(*)*)
225 | uri::tl1,((uri',_) as s)::tl2 ->
226 assert (UriManager.eq uri uri') ;
228 | [],_ -> assert false
230 let exp_named_subst' = aux (params,exp_named_subst) in
231 !exp_named_subst_diff,!next_fresh_meta,
232 List.rev !newmetasenvfragment, exp_named_subst'
234 new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
237 let new_metasenv_and_unify_and_t newmeta' metasenv' subst context term' ty termty goal_arity =
238 let (consthead,newmetasenv,arguments,_) =
239 TermUtil.saturate_term newmeta' metasenv' context termty
241 let subst,newmetasenv',_ =
242 CicUnification.fo_unif_subst
243 subst context newmetasenv consthead ty CicUniv.empty_ugraph
246 if List.length arguments = 0 then term' else Cic.Appl (term'::arguments)
250 let rec count_prods context ty =
251 match CicReduction.whd context ty with
252 Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
255 let apply_with_subst ~term ~subst ~maxmeta (proof, goal) =
256 (* Assumption: The term "term" must be closed in the current context *)
257 let module T = CicTypeChecker in
258 let module R = CicReduction in
259 let module C = Cic in
260 let (_,metasenv,_,_, _) = proof in
261 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
262 let newmeta = max (CicMkImplicit.new_meta metasenv subst) maxmeta in
263 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
265 C.Var (uri,exp_named_subst) ->
266 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
267 generalize_exp_named_subst_with_fresh_metas context newmeta uri
270 exp_named_subst_diff,newmeta',newmetasenvfragment,
271 C.Var (uri,exp_named_subst')
272 | C.Const (uri,exp_named_subst) ->
273 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
274 generalize_exp_named_subst_with_fresh_metas context newmeta uri
277 exp_named_subst_diff,newmeta',newmetasenvfragment,
278 C.Const (uri,exp_named_subst')
279 | C.MutInd (uri,tyno,exp_named_subst) ->
280 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
281 generalize_exp_named_subst_with_fresh_metas context newmeta uri
284 exp_named_subst_diff,newmeta',newmetasenvfragment,
285 C.MutInd (uri,tyno,exp_named_subst')
286 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
287 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
288 generalize_exp_named_subst_with_fresh_metas context newmeta uri
291 exp_named_subst_diff,newmeta',newmetasenvfragment,
292 C.MutConstruct (uri,tyno,consno,exp_named_subst')
293 | _ -> [],newmeta,[],term
295 let metasenv' = metasenv@newmetasenvfragment in
297 CicTypeChecker.type_of_aux' metasenv' context term' CicUniv.empty_ugraph
300 CicSubstitution.subst_vars exp_named_subst_diff termty in
301 let goal_arity = count_prods context ty in
302 let subst,newmetasenv',t =
303 let rec add_one_argument n =
305 new_metasenv_and_unify_and_t newmeta' metasenv' subst context term' ty
307 with CicUnification.UnificationFailure _ when n > 0 ->
308 add_one_argument (n - 1)
310 add_one_argument goal_arity
312 let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
313 let apply_subst = CicMetaSubst.apply_subst subst in
314 let old_uninstantiatedmetas,new_uninstantiatedmetas =
315 (* subst_in doesn't need the context. Hence the underscore. *)
316 let subst_in _ = CicMetaSubst.apply_subst subst in
317 classify_metas newmeta in_subst_domain subst_in newmetasenv'
319 let bo' = apply_subst t in
320 let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
322 (* if we just apply the subtitution, the type is irrelevant:
323 we may use Implicit, since it will be dropped *)
324 CicMetaSubst.apply_subst ((metano,(context,bo',Cic.Implicit None))::subst)
326 let (newproof, newmetasenv''') =
327 ProofEngineHelpers.subst_meta_and_metasenv_in_proof proof metano subst_in
330 let subst = ((metano,(context,bo',ty))::subst) in
332 (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas),
333 max maxmeta (CicMkImplicit.new_meta newmetasenv''' subst)
337 let apply_with_subst ~term ?(subst=[]) ?(maxmeta=0) status =
339 (* apply_tac_verbose ~term status *)
340 apply_with_subst ~term ~subst ~maxmeta status
341 (* TODO cacciare anche altre eccezioni? *)
343 | CicUnification.UnificationFailure msg
344 | CicTypeChecker.TypeCheckerFailure msg -> raise (Fail msg)
347 let apply_tac_verbose ~term status =
348 let subst, status, _ = apply_with_subst ~term status in
349 (CicMetaSubst.apply_subst subst), status
351 let apply_tac ~term status = snd (apply_tac_verbose ~term status)
353 (* TODO per implementare i tatticali e' necessario che tutte le tattiche
354 sollevino _solamente_ Fail *)
355 let apply_tac ~term =
356 let apply_tac ~term status =
358 apply_tac ~term status
359 (* TODO cacciare anche altre eccezioni? *)
361 | CicUnification.UnificationFailure msg
362 | CicTypeChecker.TypeCheckerFailure msg ->
365 mk_tactic (apply_tac ~term)
367 let intros_tac ?howmany ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) ()=
369 ?(mk_fresh_name_callback = (FreshNamesGenerator.mk_fresh_name ~subst:[])) ()
372 let module C = Cic in
373 let module R = CicReduction in
374 let (_,metasenv,_,_, _) = proof in
375 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
376 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
377 let (context',ty',bo') =
378 lambda_abstract ?howmany metasenv context newmeta ty mk_fresh_name_callback
381 ProofEngineHelpers.subst_meta_in_proof proof metano bo'
382 [newmeta,context',ty']
384 (newproof, [newmeta])
386 mk_tactic (intros_tac ~mk_fresh_name_callback ())
388 let cut_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
390 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
393 let module C = Cic in
394 let curi,metasenv,pbo,pty, attrs = proof in
395 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
396 let newmeta1 = ProofEngineHelpers.new_meta_of_proof ~proof in
397 let newmeta2 = newmeta1 + 1 in
399 mk_fresh_name_callback metasenv context (Cic.Name "Hcut") ~typ:term in
400 let context_for_newmeta1 =
401 (Some (fresh_name,C.Decl term))::context in
403 CicMkImplicit.identity_relocation_list_for_metavariable
407 CicMkImplicit.identity_relocation_list_for_metavariable context
409 let newmeta1ty = CicSubstitution.lift 1 ty in
412 [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
413 C.Meta (newmeta2,irl2)]
416 ProofEngineHelpers.subst_meta_in_proof proof metano bo'
417 [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
419 (newproof, [newmeta1 ; newmeta2])
421 mk_tactic (cut_tac ~mk_fresh_name_callback term)
423 let letin_tac ?(mk_fresh_name_callback=FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
425 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
428 let module C = Cic in
429 let curi,metasenv,pbo,pty, attrs = proof in
432 let m = CicUtil.metas_of_term t in
433 List.exists (fun (j,_) -> i=j) m
435 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
436 if occur metano term then
438 (ProofEngineTypes.Fail (lazy
439 "You can't letin a term containing the current goal"));
441 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
442 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
444 mk_fresh_name_callback metasenv context (Cic.Name "Hletin") ~typ:term in
445 let context_for_newmeta =
446 (Some (fresh_name,C.Def (term,None)))::context in
448 CicMkImplicit.identity_relocation_list_for_metavariable
451 let newmetaty = CicSubstitution.lift 1 ty in
452 let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
454 ProofEngineHelpers.subst_meta_in_proof
455 proof metano bo'[newmeta,context_for_newmeta,newmetaty]
457 (newproof, [newmeta])
459 mk_tactic (letin_tac ~mk_fresh_name_callback term)
461 (** functional part of the "exact" tactic *)
462 let exact_tac ~term =
463 let exact_tac ~term (proof, goal) =
464 (* Assumption: the term bo must be closed in the current context *)
465 let (_,metasenv,_,_, _) = proof in
466 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
467 let module T = CicTypeChecker in
468 let module R = CicReduction in
469 let ty_term,u = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in
470 let b,_ = R.are_convertible context ty_term ty u in (* TASSI: FIXME *)
473 let (newproof, metasenv') =
474 ProofEngineHelpers.subst_meta_in_proof proof metano term [] in
478 raise (Fail (lazy "The type of the provided term is not the one expected."))
480 mk_tactic (exact_tac ~term)
482 (* not really "primitive" tactics .... *)
484 module TC = CicTypeChecker
485 module U = UriManager
486 module R = CicReduction
488 module PET = ProofEngineTypes
489 module PEH = ProofEngineHelpers
490 module PER = ProofEngineReduction
491 module MS = CicMetaSubst
492 module S = CicSubstitution
494 module RT = ReductionTactics
496 let elim_tac ?using ?(pattern = PET.conclusion_pattern None) term =
497 let elim_tac (proof, goal) =
498 let ugraph = CicUniv.empty_ugraph in
499 let curi, metasenv, proofbo, proofty, attrs = proof in
500 let conjecture = CicUtil.lookup_meta goal metasenv in
501 let metano, context, ty = conjecture in
502 (* let (term, metasenv, _ugraph), cpatt = match pattern with
503 | Some f, [], Some cpatt -> f context metasenv ugraph, cpatt
507 let termty,_ugraph = TC.type_of_aux' metasenv context term ugraph in
508 let termty = CicReduction.whd context termty in
509 let (termty,metasenv',arguments,_fresh_meta) =
510 TermUtil.saturate_term
511 (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in
512 let term = if arguments = [] then term else Cic.Appl (term::arguments) in
513 let uri,exp_named_subst,typeno,args =
515 C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
516 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
517 (uri,exp_named_subst,typeno,args)
518 | _ -> raise NotAnInductiveTypeToEliminate
521 let buri = U.buri_of_uri uri in
523 let o,_ugraph = CicEnvironment.get_obj ugraph uri in
525 C.InductiveDefinition (tys,_,_,_) ->
526 let (name,_,_,_) = List.nth tys typeno in
530 let ty_ty,_ugraph = TC.type_of_aux' metasenv' context ty ugraph in
533 C.Sort C.Prop -> "_ind"
534 | C.Sort C.Set -> "_rec"
535 | C.Sort C.CProp -> "_rec"
536 | C.Sort (C.Type _)-> "_rect"
537 | C.Meta (_,_) -> raise TheTypeOfTheCurrentGoalIsAMetaICannotChooseTheRightElimiantionPrinciple
540 U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
542 let eliminator_ref = match using with
543 | None -> C.Const (eliminator_uri,exp_named_subst)
547 TC.type_of_aux' metasenv' context eliminator_ref ugraph in
548 (* FG: ADDED PART ***********************************************************)
549 (* FG: we can not assume eliminator is the default eliminator ***************)
551 let add_lambdas n t =
554 else C.Lambda (C.Anonymous, C.Implicit None, aux (pred n) t)
559 let rec args_init n f =
560 if n <= 0 then [] else f n :: args_init (pred n) f
562 let splits, args_no = PEH.split_with_whd (context, ety) in
563 let pred_pos = match List.hd splits with
564 | _, C.Rel i when i > 1 && i <= args_no -> i
565 | _, C.Appl (C.Rel i :: _) when i > 1 && i <= args_no -> i
566 | _ -> raise NotAnEliminator
569 let _, lambdas = PEH.split_with_whd (List.nth splits pred_pos) in
571 let termty_ty,_ugraph = TC.type_of_aux' metasenv' context termty ugraph in
572 CicReduction.whd context termty_ty
576 let metasenv', term, pred, upto = match cpatt, termty_ty with
577 | C.Implicit (Some `Hole), _
578 | _, C.Sort C.Prop when lambdas = 0 -> metasenv', term, C.Implicit None, 0
580 (* FG: we find the predicate for the eliminator as in the rewrite tactic ****)
582 FreshNamesGenerator.mk_fresh_name
583 ~subst:[] metasenv' context C.Anonymous ~typ:termty
585 let lazy_term c m u =
586 let distance = List.length c - List.length context in
587 S.lift distance term, m, u
589 let pattern = Some lazy_term, [], Some cpatt in
590 let subst, metasenv', _ugraph, _conjecture, selected_terms =
591 ProofEngineHelpers.select
592 ~metasenv:metasenv' ~ugraph ~conjecture ~pattern
594 let metasenv' = MS.apply_subst_metasenv subst metasenv' in
595 let map (_context_of_t, t) l = t :: l in
596 let what = List.fold_right map selected_terms [] in
597 let ty = MS.apply_subst subst ty in
598 let term = MS.apply_subst subst term in
599 let termty = MS.apply_subst subst termty in
600 let abstr_ty = PER.replace_with_rel_1_from ~equality:(==) ~what 1 ty in
601 let abstr_ty = MS.apply_subst subst abstr_ty in
602 let pred_body = C.Lambda (fresh_name, termty, abstr_ty) in
603 metasenv', term, add_lambdas (pred lambdas) pred_body, lambdas
605 (* FG: END OF ADDED PART ****************************************************)
607 let pred, upto = C.Implicit None, 0 in
611 if n = pred_pos then pred else
612 if n = 1 then term else C.Implicit None
614 C.Appl (eliminator_ref :: args_init args_no f)
616 let refined_term,_refined_termty,metasenv'',_ugraph =
617 CicRefine.type_of_aux' metasenv' context term_to_refine
621 ProofEngineHelpers.compare_metasenvs
622 ~oldmetasenv:metasenv ~newmetasenv:metasenv''
624 let proof' = curi,metasenv'',proofbo,proofty, attrs in
625 let proof'', new_goals' =
626 apply_tactic (apply_tac ~term:refined_term) (proof',goal)
628 (* The apply_tactic can have closed some of the new_goals *)
629 let patched_new_goals =
630 let (_,metasenv''',_,_, _) = proof'' in
632 (function i -> List.exists (function (j,_,_) -> j=i) metasenv'''
633 ) new_goals @ new_goals'
635 let res = proof'', patched_new_goals in
636 if upto = 0 then res else
637 let pattern = PET.conclusion_pattern None in
639 RT.simpl_tac ~pattern
640 (* RT.head_beta_reduce_tac ~delta:false ~upto ~pattern *)
642 let dummy_status = proof,goal in
644 (T.then_ ~start:(PET.mk_tactic (fun _ -> res)) ~continuation)
650 let cases_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
651 let cases_tac ~term (proof, goal) =
652 let module TC = CicTypeChecker in
653 let module U = UriManager in
654 let module R = CicReduction in
655 let module C = Cic in
656 let (curi,metasenv,proofbo,proofty, attrs) = proof in
657 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
658 let termty,_ = TC.type_of_aux' metasenv context term CicUniv.empty_ugraph in
659 let termty = CicReduction.whd context termty in
660 let (termty,metasenv',arguments,fresh_meta) =
661 TermUtil.saturate_term
662 (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in
663 let term = if arguments = [] then term else Cic.Appl (term::arguments) in
664 let uri,exp_named_subst,typeno,args =
666 | C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
667 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
668 (uri,exp_named_subst,typeno,args)
669 | _ -> raise NotAnInductiveTypeToEliminate
671 let paramsno,itty,patterns,right_args =
672 match CicEnvironment.get_obj CicUniv.empty_ugraph uri with
673 | C.InductiveDefinition (tys,_,paramsno,_),_ ->
674 let _,left_parameters,right_args =
676 (fun x (n,acc1,acc2) ->
677 if n > 0 then (n-1,acc1,x::acc2) else (n,x::acc1,acc2))
678 args (List.length args - paramsno, [],[])
680 let _,_,itty,cl = List.nth tys typeno in
681 let rec aux left_parameters context t =
682 match left_parameters,CicReduction.whd context t with
683 | [],C.Prod (name,source,target) ->
685 mk_fresh_name_callback metasenv' context name ~typ:source
687 C.Lambda (fresh_name,C.Implicit None,
688 aux [] (Some (fresh_name,C.Decl source)::context) target)
689 | hd::tl,C.Prod (name,source,target) ->
690 (* left parameters instantiation *)
691 aux tl context (CicSubstitution.subst hd target)
692 | [],_ -> C.Implicit None
696 List.map (function (_,cty) -> aux left_parameters context cty) cl,
701 let n_right_args = List.length right_args in
702 let n_lambdas = n_right_args + 1 in
703 let lifted_ty = CicSubstitution.lift n_lambdas ty in
706 List.map (CicSubstitution.lift n_lambdas) (right_args)
709 let rec mkargs = function
713 (if meta then Cic.Implicit None else Cic.Rel n)::(mkargs (n-1))
717 let replaced = ref false in
718 let replace = ProofEngineReduction.replace_lifting
719 ~equality:(fun _ a b -> let rc = CicUtil.alpha_equivalence a b in
720 if rc then replaced := true; rc)
724 replace ~what:[CicSubstitution.lift n_lambdas term]
725 ~with_what:[Cic.Rel 1] ~where:lifted_ty
727 if not !replaced then
728 (* this means the matched term is not there,
729 * but maybe right params are: we user rels (to right args lambdas) *)
730 replace ~what ~with_what:(with_what false) ~where:captured
732 (* since the matched is there, rights should be inferrable *)
733 replace ~what ~with_what:(with_what true) ~where:captured
735 let captured_term_ty =
736 let term_ty = CicSubstitution.lift n_right_args termty in
737 let rec mkrels = function 0 -> []|n -> (Cic.Rel n)::(mkrels (n-1)) in
738 let rec fstn acc l n =
739 if n = 0 then acc else fstn (acc@[List.hd l]) (List.tl l) (n-1)
742 | C.MutInd _ -> term_ty
743 | C.Appl ((C.MutInd (a,b,c))::args) ->
744 C.Appl ((C.MutInd (a,b,c))::
745 fstn [] args paramsno @ mkrels n_right_args)
746 | _ -> raise NotAnInductiveTypeToEliminate
748 let rec add_lambdas = function
751 C.Lambda (C.Name "matched", captured_term_ty, (add_lambdas 0))
753 C.Lambda (C.Name ("right_"^(string_of_int (n-1))),
754 C.Implicit None, (add_lambdas (n-1)))
756 add_lambdas n_lambdas
758 let term_to_refine = C.MutCase (uri,typeno,outtype,term,patterns) in
759 let refined_term,_,metasenv'',_ =
760 CicRefine.type_of_aux' metasenv' context term_to_refine
764 ProofEngineHelpers.compare_metasenvs
765 ~oldmetasenv:metasenv ~newmetasenv:metasenv''
767 let proof' = curi,metasenv'',proofbo,proofty, attrs in
768 let proof'', new_goals' =
769 apply_tactic (apply_tac ~term:refined_term) (proof',goal)
771 (* The apply_tactic can have closed some of the new_goals *)
772 let patched_new_goals =
773 let (_,metasenv''',_,_,_) = proof'' in
775 (function i -> List.exists (function (j,_,_) -> j=i) metasenv''')
776 new_goals @ new_goals'
778 proof'', patched_new_goals
780 mk_tactic (cases_tac ~term)
784 let elim_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
785 ?depth ?using ?pattern what =
786 Tacticals.then_ ~start:(elim_tac ?using ?pattern what)
787 ~continuation:(intros_tac ~mk_fresh_name_callback ?howmany:depth ())
790 (* The simplification is performed only on the conclusion *)
791 let elim_intros_simpl_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
792 ?depth ?using ?pattern what =
793 Tacticals.then_ ~start:(elim_tac ?using ?pattern what)
796 ~start:(intros_tac ~mk_fresh_name_callback ?howmany:depth ())
798 [ReductionTactics.simpl_tac
799 ~pattern:(ProofEngineTypes.conclusion_pattern None)])
802 (* FG: insetrts a "hole" in the context (derived from letin_tac) *)
805 let mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[] in
806 let term = C.Sort C.Set in
807 let letout_tac (proof, goal) =
808 let curi, metasenv, pbo, pty, attrs = proof in
809 let metano, context, ty = CicUtil.lookup_meta goal metasenv in
810 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
811 let fresh_name = mk_fresh_name_callback metasenv context (Cic.Name "hole") ~typ:term in
812 let context_for_newmeta = None :: context in
813 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context_for_newmeta in
814 let newmetaty = CicSubstitution.lift 1 ty in
815 let bo' = C.LetIn (fresh_name, term, C.Meta (newmeta,irl)) in
816 let newproof, _ = ProofEngineHelpers.subst_meta_in_proof proof metano bo'[newmeta,context_for_newmeta,newmetaty] in