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' 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 newmetasenv context consthead ty CicUniv.empty_ugraph
245 if List.length arguments = 0 then term' else Cic.Appl (term'::arguments)
249 let rec count_prods context ty =
250 match CicReduction.whd context ty with
251 Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
254 let apply_with_subst ~term ~subst ~maxmeta (proof, goal) =
255 (* Assumption: The term "term" must be closed in the current context *)
256 let module T = CicTypeChecker in
257 let module R = CicReduction in
258 let module C = Cic in
259 let (_,metasenv,_,_, _) = proof in
260 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
261 let newmeta = max (CicMkImplicit.new_meta metasenv subst) maxmeta in
262 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
264 C.Var (uri,exp_named_subst) ->
265 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
266 generalize_exp_named_subst_with_fresh_metas context newmeta uri
269 exp_named_subst_diff,newmeta',newmetasenvfragment,
270 C.Var (uri,exp_named_subst')
271 | C.Const (uri,exp_named_subst) ->
272 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
273 generalize_exp_named_subst_with_fresh_metas context newmeta uri
276 exp_named_subst_diff,newmeta',newmetasenvfragment,
277 C.Const (uri,exp_named_subst')
278 | C.MutInd (uri,tyno,exp_named_subst) ->
279 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
280 generalize_exp_named_subst_with_fresh_metas context newmeta uri
283 exp_named_subst_diff,newmeta',newmetasenvfragment,
284 C.MutInd (uri,tyno,exp_named_subst')
285 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
286 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
287 generalize_exp_named_subst_with_fresh_metas context newmeta uri
290 exp_named_subst_diff,newmeta',newmetasenvfragment,
291 C.MutConstruct (uri,tyno,consno,exp_named_subst')
292 | _ -> [],newmeta,[],term
294 let metasenv' = metasenv@newmetasenvfragment in
296 CicTypeChecker.type_of_aux' metasenv' context term' CicUniv.empty_ugraph
299 CicSubstitution.subst_vars exp_named_subst_diff termty in
300 let goal_arity = count_prods context ty in
301 let subst,newmetasenv',t =
302 let rec add_one_argument n =
304 new_metasenv_and_unify_and_t newmeta' metasenv' context term' ty
306 with CicUnification.UnificationFailure _ when n > 0 ->
307 add_one_argument (n - 1)
309 add_one_argument goal_arity
311 let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
312 let apply_subst = CicMetaSubst.apply_subst subst in
313 let old_uninstantiatedmetas,new_uninstantiatedmetas =
314 (* subst_in doesn't need the context. Hence the underscore. *)
315 let subst_in _ = CicMetaSubst.apply_subst subst in
316 classify_metas newmeta in_subst_domain subst_in newmetasenv'
318 let bo' = apply_subst t in
319 let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
321 (* if we just apply the subtitution, the type is irrelevant:
322 we may use Implicit, since it will be dropped *)
323 CicMetaSubst.apply_subst ((metano,(context,bo',Cic.Implicit None))::subst)
325 let (newproof, newmetasenv''') =
326 ProofEngineHelpers.subst_meta_and_metasenv_in_proof proof metano subst_in
329 let subst = ((metano,(context,bo',Cic.Implicit None))::subst) in
331 (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas),
332 max maxmeta (CicMkImplicit.new_meta newmetasenv''' subst)
336 let apply_with_subst ~term ?(subst=[]) ?(maxmeta=0) status =
338 (* apply_tac_verbose ~term status *)
339 apply_with_subst ~term ~subst ~maxmeta status
340 (* TODO cacciare anche altre eccezioni? *)
342 | CicUnification.UnificationFailure msg
343 | CicTypeChecker.TypeCheckerFailure msg -> raise (Fail msg)
346 let apply_tac_verbose ~term status =
347 let subst, status, _ = apply_with_subst ~term status in
348 (CicMetaSubst.apply_subst subst), status
350 let apply_tac ~term status = snd (apply_tac_verbose ~term status)
352 (* TODO per implementare i tatticali e' necessario che tutte le tattiche
353 sollevino _solamente_ Fail *)
354 let apply_tac ~term =
355 let apply_tac ~term status =
357 apply_tac ~term status
358 (* TODO cacciare anche altre eccezioni? *)
360 | CicUnification.UnificationFailure msg
361 | CicTypeChecker.TypeCheckerFailure msg ->
364 mk_tactic (apply_tac ~term)
366 let intros_tac ?howmany ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) ()=
368 ?(mk_fresh_name_callback = (FreshNamesGenerator.mk_fresh_name ~subst:[])) ()
371 let module C = Cic in
372 let module R = CicReduction in
373 let (_,metasenv,_,_, _) = proof in
374 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
375 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
376 let (context',ty',bo') =
377 lambda_abstract ?howmany metasenv context newmeta ty mk_fresh_name_callback
380 ProofEngineHelpers.subst_meta_in_proof proof metano bo'
381 [newmeta,context',ty']
383 (newproof, [newmeta])
385 mk_tactic (intros_tac ~mk_fresh_name_callback ())
387 let cut_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
389 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
392 let module C = Cic in
393 let curi,metasenv,pbo,pty, attrs = proof in
394 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
395 let newmeta1 = ProofEngineHelpers.new_meta_of_proof ~proof in
396 let newmeta2 = newmeta1 + 1 in
398 mk_fresh_name_callback metasenv context (Cic.Name "Hcut") ~typ:term in
399 let context_for_newmeta1 =
400 (Some (fresh_name,C.Decl term))::context in
402 CicMkImplicit.identity_relocation_list_for_metavariable
406 CicMkImplicit.identity_relocation_list_for_metavariable context
408 let newmeta1ty = CicSubstitution.lift 1 ty in
411 [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
412 C.Meta (newmeta2,irl2)]
415 ProofEngineHelpers.subst_meta_in_proof proof metano bo'
416 [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
418 (newproof, [newmeta1 ; newmeta2])
420 mk_tactic (cut_tac ~mk_fresh_name_callback term)
422 let letin_tac ?(mk_fresh_name_callback=FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
424 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
427 let module C = Cic in
428 let curi,metasenv,pbo,pty, attrs = proof in
431 let m = CicUtil.metas_of_term t in
432 List.exists (fun (j,_) -> i=j) m
434 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
435 if occur metano term then
437 (ProofEngineTypes.Fail (lazy
438 "You can't letin a term containing the current goal"));
440 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
441 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
443 mk_fresh_name_callback metasenv context (Cic.Name "Hletin") ~typ:term in
444 let context_for_newmeta =
445 (Some (fresh_name,C.Def (term,None)))::context in
447 CicMkImplicit.identity_relocation_list_for_metavariable
450 let newmetaty = CicSubstitution.lift 1 ty in
451 let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
453 ProofEngineHelpers.subst_meta_in_proof
454 proof metano bo'[newmeta,context_for_newmeta,newmetaty]
456 (newproof, [newmeta])
458 mk_tactic (letin_tac ~mk_fresh_name_callback term)
460 (** functional part of the "exact" tactic *)
461 let exact_tac ~term =
462 let exact_tac ~term (proof, goal) =
463 (* Assumption: the term bo must be closed in the current context *)
464 let (_,metasenv,_,_, _) = proof in
465 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
466 let module T = CicTypeChecker in
467 let module R = CicReduction in
468 let ty_term,u = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in
469 let b,_ = R.are_convertible context ty_term ty u in (* TASSI: FIXME *)
472 let (newproof, metasenv') =
473 ProofEngineHelpers.subst_meta_in_proof proof metano term [] in
477 raise (Fail (lazy "The type of the provided term is not the one expected."))
479 mk_tactic (exact_tac ~term)
481 (* not really "primitive" tactics .... *)
483 module TC = CicTypeChecker
484 module U = UriManager
485 module R = CicReduction
487 module PET = ProofEngineTypes
488 module PEH = ProofEngineHelpers
489 module PER = ProofEngineReduction
490 module MS = CicMetaSubst
491 module S = CicSubstitution
493 module RT = ReductionTactics
495 let elim_tac ?using ?(pattern = PET.conclusion_pattern None) term =
496 let elim_tac (proof, goal) =
497 let ugraph = CicUniv.empty_ugraph in
498 let curi, metasenv, proofbo, proofty, attrs = proof in
499 let conjecture = CicUtil.lookup_meta goal metasenv in
500 let metano, context, ty = conjecture in
501 (* let (term, metasenv, _ugraph), cpatt = match pattern with
502 | Some f, [], Some cpatt -> f context metasenv ugraph, cpatt
506 let termty,_ugraph = TC.type_of_aux' metasenv context term ugraph in
507 let termty = CicReduction.whd context termty in
508 let (termty,metasenv',arguments,_fresh_meta) =
509 TermUtil.saturate_term
510 (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in
511 let term = if arguments = [] then term else Cic.Appl (term::arguments) in
512 let uri,exp_named_subst,typeno,args =
514 C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
515 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
516 (uri,exp_named_subst,typeno,args)
517 | _ -> raise NotAnInductiveTypeToEliminate
520 let buri = U.buri_of_uri uri in
522 let o,_ugraph = CicEnvironment.get_obj ugraph uri in
524 C.InductiveDefinition (tys,_,_,_) ->
525 let (name,_,_,_) = List.nth tys typeno in
529 let ty_ty,_ugraph = TC.type_of_aux' metasenv' context ty ugraph in
532 C.Sort C.Prop -> "_ind"
533 | C.Sort C.Set -> "_rec"
534 | C.Sort C.CProp -> "_rec"
535 | C.Sort (C.Type _)-> "_rect"
536 | C.Meta (_,_) -> raise TheTypeOfTheCurrentGoalIsAMetaICannotChooseTheRightElimiantionPrinciple
539 U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
541 let eliminator_ref = match using with
542 | None -> C.Const (eliminator_uri,exp_named_subst)
546 TC.type_of_aux' metasenv' context eliminator_ref ugraph in
547 (* FG: ADDED PART ***********************************************************)
548 (* FG: we can not assume eliminator is the default eliminator ***************)
550 let add_lambdas n t =
553 else C.Lambda (C.Anonymous, C.Implicit None, aux (pred n) t)
558 let rec args_init n f =
559 if n <= 0 then [] else f n :: args_init (pred n) f
561 let splits, args_no = PEH.split_with_whd (context, ety) in
562 let pred_pos = match List.hd splits with
563 | _, C.Rel i when i > 1 && i <= args_no -> i
564 | _, C.Appl (C.Rel i :: _) when i > 1 && i <= args_no -> i
565 | _ -> raise NotAnEliminator
568 let _, lambdas = PEH.split_with_whd (List.nth splits pred_pos) in
570 let termty_ty,_ugraph = TC.type_of_aux' metasenv' context termty ugraph in
571 CicReduction.whd context termty_ty
575 let metasenv', term, pred, upto = match cpatt, termty_ty with
576 | C.Implicit (Some `Hole), _
577 | _, C.Sort C.Prop when lambdas = 0 -> metasenv', term, C.Implicit None, 0
579 (* FG: we find the predicate for the eliminator as in the rewrite tactic ****)
581 FreshNamesGenerator.mk_fresh_name
582 ~subst:[] metasenv' context C.Anonymous ~typ:termty
584 let lazy_term c m u =
585 let distance = List.length c - List.length context in
586 S.lift distance term, m, u
588 let pattern = Some lazy_term, [], Some cpatt in
589 let subst, metasenv', _ugraph, _conjecture, selected_terms =
590 ProofEngineHelpers.select
591 ~metasenv:metasenv' ~ugraph ~conjecture ~pattern
593 let metasenv' = MS.apply_subst_metasenv subst metasenv' in
594 let map (_context_of_t, t) l = t :: l in
595 let what = List.fold_right map selected_terms [] in
596 let ty = MS.apply_subst subst ty in
597 let term = MS.apply_subst subst term in
598 let termty = MS.apply_subst subst termty in
599 let abstr_ty = PER.replace_with_rel_1_from ~equality:(==) ~what 1 ty in
600 let abstr_ty = MS.apply_subst subst abstr_ty in
601 let pred_body = C.Lambda (fresh_name, termty, abstr_ty) in
602 metasenv', term, add_lambdas (pred lambdas) pred_body, lambdas
604 (* FG: END OF ADDED PART ****************************************************)
606 let pred, upto = C.Implicit None, 0 in
610 if n = pred_pos then pred else
611 if n = 1 then term else C.Implicit None
613 C.Appl (eliminator_ref :: args_init args_no f)
615 let refined_term,_refined_termty,metasenv'',_ugraph =
616 CicRefine.type_of_aux' metasenv' context term_to_refine
620 ProofEngineHelpers.compare_metasenvs
621 ~oldmetasenv:metasenv ~newmetasenv:metasenv''
623 let proof' = curi,metasenv'',proofbo,proofty, attrs in
624 let proof'', new_goals' =
625 apply_tactic (apply_tac ~term:refined_term) (proof',goal)
627 (* The apply_tactic can have closed some of the new_goals *)
628 let patched_new_goals =
629 let (_,metasenv''',_,_, _) = proof'' in
631 (function i -> List.exists (function (j,_,_) -> j=i) metasenv'''
632 ) new_goals @ new_goals'
634 let res = proof'', patched_new_goals in
635 if upto = 0 then res else
636 let pattern = PET.conclusion_pattern None in
638 RT.simpl_tac ~pattern
639 (* RT.head_beta_reduce_tac ~delta:false ~upto ~pattern *)
641 let dummy_status = proof,goal in
643 (T.then_ ~start:(PET.mk_tactic (fun _ -> res)) ~continuation)
649 let cases_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) term =
650 let cases_tac ~term (proof, goal) =
651 let module TC = CicTypeChecker in
652 let module U = UriManager in
653 let module R = CicReduction in
654 let module C = Cic in
655 let (curi,metasenv,proofbo,proofty, attrs) = proof in
656 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
657 let termty,_ = TC.type_of_aux' metasenv context term CicUniv.empty_ugraph in
658 let termty = CicReduction.whd context termty in
659 let (termty,metasenv',arguments,fresh_meta) =
660 TermUtil.saturate_term
661 (ProofEngineHelpers.new_meta_of_proof proof) metasenv context termty 0 in
662 let term = if arguments = [] then term else Cic.Appl (term::arguments) in
663 let uri,exp_named_subst,typeno,args =
665 C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
666 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
667 (uri,exp_named_subst,typeno,args)
668 | _ -> raise NotAnInductiveTypeToEliminate
670 let paramsno,itty,patterns =
671 match CicEnvironment.get_obj CicUniv.empty_ugraph uri with
672 C.InductiveDefinition (tys,_,paramsno,_),_ ->
673 let _,_,itty,cl = List.nth tys typeno in
674 let rec aux n context t =
675 match n,CicReduction.whd context t with
676 0,C.Prod (name,source,target) ->
678 mk_fresh_name_callback metasenv' context name
679 (*CSC: WRONG TYPE HERE: I can get a "bad" name*)
682 C.Lambda (fresh_name,C.Implicit None,
683 aux 0 (Some (fresh_name,C.Decl source)::context) target)
684 | n,C.Prod (name,source,target) ->
686 mk_fresh_name_callback metasenv' context name
687 (*CSC: WRONG TYPE HERE: I can get a "bad" name*)
690 aux (n-1) (Some (fresh_name,C.Decl source)::context) target
691 | 0,_ -> C.Implicit None
692 | _,_ -> assert false
695 List.map (function (_,cty) -> aux paramsno context cty) cl
700 (fun x (n,acc) -> if n > 0 then (n-1,x::acc) else (n,acc))
701 args (List.length args - paramsno, [])
704 let n_lambdas = List.length right_args + 1 in
705 let lifted_ty = CicSubstitution.lift n_lambdas ty in
706 let replace = ProofEngineReduction.replace_lifting
707 ~equality:(ProofEngineReduction.alpha_equivalence)
711 List.map (CicSubstitution.lift n_lambdas) (right_args@[term])
714 let rec mkargs = function
717 | n -> (Cic.Implicit None)::(mkargs (n-1))
721 replace ~what ~with_what ~where:lifted_ty
723 let captured_term_ty =
724 let term_ty = CicSubstitution.lift (n_lambdas-1) termty in
725 let rec mkrels = function 0 -> []|n -> (Cic.Rel n)::(mkrels (n-1)) in
726 let rec fstn acc l n =
727 if n = 0 then acc else fstn (acc@[List.hd l]) (List.tl l) (n-1)
730 | C.MutInd _ -> term_ty
731 | C.Appl ((C.MutInd (a,b,c))::args) ->
732 C.Appl ((C.MutInd (a,b,c))::
733 fstn [] args paramsno @ mkrels (n_lambdas -1))
734 | _ -> raise NotAnInductiveTypeToEliminate
736 let rec add_lambdas = function
739 C.Lambda (C.Name "matched", captured_term_ty, (add_lambdas 0))
741 C.Lambda (C.Name ("right_"^(string_of_int (n-1))),
742 C.Implicit None, (add_lambdas (n-1)))
744 add_lambdas n_lambdas
747 C.MutCase (uri,typeno,outtype,term,patterns)
749 let refined_term,_,metasenv'',_ =
750 CicRefine.type_of_aux' metasenv' context term_to_refine
754 ProofEngineHelpers.compare_metasenvs
755 ~oldmetasenv:metasenv ~newmetasenv:metasenv''
757 let proof' = curi,metasenv'',proofbo,proofty, attrs in
758 let proof'', new_goals' =
759 apply_tactic (apply_tac ~term:refined_term) (proof',goal)
761 (* The apply_tactic can have closed some of the new_goals *)
762 let patched_new_goals =
763 let (_,metasenv''',_,_,_) = proof'' in
765 (function i -> List.exists (function (j,_,_) -> j=i) metasenv'''
766 ) new_goals @ new_goals'
768 proof'', patched_new_goals
770 mk_tactic (cases_tac ~term)
774 let elim_intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
775 ?depth ?using ?pattern what =
776 Tacticals.then_ ~start:(elim_tac ?using ?pattern what)
777 ~continuation:(intros_tac ~mk_fresh_name_callback ?howmany:depth ())
780 (* The simplification is performed only on the conclusion *)
781 let elim_intros_simpl_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
782 ?depth ?using ?pattern what =
783 Tacticals.then_ ~start:(elim_tac ?using ?pattern what)
786 ~start:(intros_tac ~mk_fresh_name_callback ?howmany:depth ())
788 [ReductionTactics.simpl_tac
789 ~pattern:(ProofEngineTypes.conclusion_pattern None)])
792 (* FG: insetrts a "hole" in the context (derived from letin_tac) *)
795 let mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[] in
796 let term = C.Sort C.Set in
797 let letout_tac (proof, goal) =
798 let curi, metasenv, pbo, pty, attrs = proof in
799 let metano, context, ty = CicUtil.lookup_meta goal metasenv in
800 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
801 let fresh_name = mk_fresh_name_callback metasenv context (Cic.Name "hole") ~typ:term in
802 let context_for_newmeta = None :: context in
803 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context_for_newmeta in
804 let newmetaty = CicSubstitution.lift 1 ty in
805 let bo' = C.LetIn (fresh_name, term, C.Meta (newmeta,irl)) in
806 let newproof, _ = ProofEngineHelpers.subst_meta_in_proof proof metano bo'[newmeta,context_for_newmeta,newmetaty] in