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/.
26 open ProofEngineHelpers
29 exception NotAnInductiveTypeToEliminate
30 exception NotTheRightEliminatorShape
31 exception NoHypothesesFound
32 exception WrongUriToVariable of string
34 (* lambda_abstract newmeta ty *)
35 (* returns a triple [bo],[context],[ty'] where *)
36 (* [ty] = Pi/LetIn [context].[ty'] ([context] is a vector!) *)
37 (* and [bo] = Lambda/LetIn [context].(Meta [newmeta]) *)
38 (* So, lambda_abstract is the core of the implementation of *)
39 (* the Intros tactic. *)
40 let lambda_abstract metasenv context newmeta ty mk_fresh_name =
42 let rec collect_context context =
44 C.Cast (te,_) -> collect_context context te
46 let n' = mk_fresh_name metasenv context n ~typ:s in
47 let (context',ty,bo) =
48 collect_context ((Some (n',(C.Decl s)))::context) t
50 (context',ty,C.Lambda(n',s,bo))
52 let (context',ty,bo) =
53 collect_context ((Some (n,(C.Def (s,None))))::context) t
55 (context',ty,C.LetIn(n,s,bo))
58 CicMkImplicit.identity_relocation_list_for_metavariable context
60 context, t, (C.Meta (newmeta,irl))
62 collect_context context ty
64 let eta_expand metasenv context t arg =
65 let module T = CicTypeChecker in
66 let module S = CicSubstitution in
70 t' when t' = S.lift n arg -> C.Rel (1 + n)
71 | C.Rel m -> if m <= n then C.Rel m else C.Rel (m+1)
72 | C.Var (uri,exp_named_subst) ->
73 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
74 C.Var (uri,exp_named_subst')
77 List.map (function None -> None | Some t -> Some (aux n t)) l
81 | C.Implicit _ as t -> t
82 | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
83 | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
84 | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
85 | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
86 | C.Appl l -> C.Appl (List.map (aux n) l)
87 | C.Const (uri,exp_named_subst) ->
88 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
89 C.Const (uri,exp_named_subst')
90 | C.MutInd (uri,i,exp_named_subst) ->
91 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
92 C.MutInd (uri,i,exp_named_subst')
93 | C.MutConstruct (uri,i,j,exp_named_subst) ->
94 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
95 C.MutConstruct (uri,i,j,exp_named_subst')
96 | C.MutCase (sp,i,outt,t,pl) ->
97 C.MutCase (sp,i,aux n outt, aux n t,
100 let tylen = List.length fl in
103 (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
106 C.Fix (i, substitutedfl)
108 let tylen = List.length fl in
111 (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
114 C.CoFix (i, substitutedfl)
115 and aux_exp_named_subst n =
116 List.map (function uri,t -> uri,aux n t)
119 T.type_of_aux' metasenv context arg CicUniv.empty_ugraph (* TASSI: FIXME *)
122 FreshNamesGenerator.mk_fresh_name ~subst:[]
123 metasenv context (Cic.Name "Heta") ~typ:argty
125 (C.Appl [C.Lambda (fresh_name,argty,aux 0 t) ; arg])
127 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
128 let classify_metas newmeta in_subst_domain subst_in metasenv =
130 (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
131 if in_subst_domain i then
132 old_uninst,new_uninst
134 let ty' = subst_in canonical_context ty in
135 let canonical_context' =
137 (fun entry canonical_context' ->
140 Some (n,Cic.Decl s) ->
141 Some (n,Cic.Decl (subst_in canonical_context' s))
142 | Some (n,Cic.Def (s,None)) ->
143 Some (n,Cic.Def ((subst_in canonical_context' s),None))
145 | Some (_,Cic.Def (_,Some _)) -> assert false
147 entry'::canonical_context'
148 ) canonical_context []
151 ((i,canonical_context',ty')::old_uninst),new_uninst
153 old_uninst,((i,canonical_context',ty')::new_uninst)
156 (* Auxiliary function for apply: given a type (a backbone), it returns its *)
157 (* head, a META environment in which there is new a META for each hypothesis,*)
158 (* a list of arguments for the new applications and the indexes of the first *)
159 (* and last new METAs introduced. The nth argument in the list of arguments *)
160 (* is just the nth new META. *)
161 let new_metasenv_for_apply newmeta proof context ty =
162 let module C = Cic in
163 let module S = CicSubstitution in
164 let rec aux newmeta ty =
165 let ty' = (*CicReduction.whd context*) ty in
167 C.Cast (he,_) -> aux newmeta he
168 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
169 (* If the expected type is a Type, then also Set is OK ==>
170 * we accept any term of type Type *)
171 (*CSC: BUG HERE: in this way it is possible for the term of
172 * type Type to be different from a Sort!!! *)
173 | C.Prod (name,(C.Sort (C.Type _) as s),t) ->
174 (* TASSI: ask CSC if BUG HERE refers to the C.Cast or C.Propd case *)
176 CicMkImplicit.identity_relocation_list_for_metavariable context
178 let newargument = C.Meta (newmeta+1,irl) in
179 let (res,newmetasenv,arguments,lastmeta) =
180 aux (newmeta + 2) (S.subst newargument t)
183 (newmeta,[],s)::(newmeta+1,context,C.Meta (newmeta,[]))::newmetasenv,
184 newargument::arguments,lastmeta
186 | C.Prod (name,s,t) ->
188 CicMkImplicit.identity_relocation_list_for_metavariable context
190 let newargument = C.Meta (newmeta,irl) in
191 let (res,newmetasenv,arguments,lastmeta) =
192 aux (newmeta + 1) (S.subst newargument t)
194 res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
195 | t -> t,[],[],newmeta
197 (* WARNING: here we are using the invariant that above the most *)
198 (* recente new_meta() there are no used metas. *)
199 let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
200 res,newmetasenv,arguments,lastmeta
202 (* Useful only inside apply_tac *)
204 generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
206 let module C = Cic in
208 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
209 CicUtil.params_of_obj o
211 let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
212 let next_fresh_meta = ref newmeta in
213 let newmetasenvfragment = ref [] in
214 let exp_named_subst_diff = ref [] in
220 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
222 C.Variable (_,_,ty,_,_) ->
223 CicSubstitution.subst_vars !exp_named_subst_diff ty
224 | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
226 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
228 C.Sort (C.Type _) as s -> (* TASSI: ?? *)
229 let fresh_meta = !next_fresh_meta in
230 let fresh_meta' = fresh_meta + 1 in
231 next_fresh_meta := !next_fresh_meta + 2 ;
232 let subst_item = uri,C.Meta (fresh_meta',[]) in
233 newmetasenvfragment :=
234 (fresh_meta,[],C.Sort (C.Type (CicUniv.fresh()))) ::
236 (fresh_meta',[],C.Meta (fresh_meta,[])) :: !newmetasenvfragment ;
237 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
238 subst_item::(aux (tl,[]))
242 CicMkImplicit.identity_relocation_list_for_metavariable context
244 let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
245 newmetasenvfragment :=
246 (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
247 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
248 incr next_fresh_meta ;
249 subst_item::(aux (tl,[]))(*)*)
250 | uri::tl1,((uri',_) as s)::tl2 ->
251 assert (UriManager.eq uri uri') ;
253 | [],_ -> assert false
255 let exp_named_subst' = aux (params,exp_named_subst) in
256 !exp_named_subst_diff,!next_fresh_meta,
257 List.rev !newmetasenvfragment, exp_named_subst'
259 new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
262 let new_metasenv_and_unify_and_t newmeta' metasenv' proof context term' ty termty =
263 let (consthead,newmetas,arguments,_) =
264 new_metasenv_for_apply newmeta' proof context termty
266 let newmetasenv = metasenv'@newmetas in
267 let subst,newmetasenv',_ =
268 CicUnification.fo_unif newmetasenv context consthead ty CicUniv.empty_ugraph
271 if List.length newmetas = 0 then term' else Cic.Appl (term'::arguments)
275 let apply_tac_verbose ~term (proof, goal) =
276 (* Assumption: The term "term" must be closed in the current context *)
277 let module T = CicTypeChecker in
278 let module R = CicReduction in
279 let module C = Cic in
280 let (_,metasenv,_,_) = proof in
281 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
282 let newmeta = new_meta_of_proof ~proof in
283 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
285 C.Var (uri,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.Var (uri,exp_named_subst')
292 | C.Const (uri,exp_named_subst) ->
293 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
294 generalize_exp_named_subst_with_fresh_metas context newmeta uri
297 exp_named_subst_diff,newmeta',newmetasenvfragment,
298 C.Const (uri,exp_named_subst')
299 | C.MutInd (uri,tyno,exp_named_subst) ->
300 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
301 generalize_exp_named_subst_with_fresh_metas context newmeta uri
304 exp_named_subst_diff,newmeta',newmetasenvfragment,
305 C.MutInd (uri,tyno,exp_named_subst')
306 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
307 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
308 generalize_exp_named_subst_with_fresh_metas context newmeta uri
311 exp_named_subst_diff,newmeta',newmetasenvfragment,
312 C.MutConstruct (uri,tyno,consno,exp_named_subst')
313 | _ -> [],newmeta,[],term
315 let metasenv' = metasenv@newmetasenvfragment in
317 CicTypeChecker.type_of_aux' metasenv' context term CicUniv.empty_ugraph in
319 CicSubstitution.subst_vars exp_named_subst_diff termty
321 let subst,newmetasenv',t =
323 new_metasenv_and_unify_and_t newmeta' metasenv' proof context term' ty
325 with CicUnification.UnificationFailure _ ->
326 new_metasenv_and_unify_and_t newmeta' metasenv' proof context term' ty
327 (CicReduction.whd context termty)
329 let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
330 let apply_subst = CicMetaSubst.apply_subst subst in
331 let old_uninstantiatedmetas,new_uninstantiatedmetas =
332 (* subst_in doesn't need the context. Hence the underscore. *)
333 let subst_in _ = CicMetaSubst.apply_subst subst in
334 classify_metas newmeta in_subst_domain subst_in newmetasenv'
336 let bo' = apply_subst t in
337 let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
339 (* if we just apply the subtitution, the type is irrelevant:
340 we may use Implicit, since it will be dropped *)
341 CicMetaSubst.apply_subst ((metano,(context,bo',Cic.Implicit None))::subst)
343 let (newproof, newmetasenv''') =
344 subst_meta_and_metasenv_in_proof proof metano subst_in newmetasenv''
348 List.map (function (i,_,_) -> i) new_uninstantiatedmetas))
350 let apply_tac ~term status = snd (apply_tac_verbose ~term status)
352 let apply_tac_verbose ~term status =
354 apply_tac_verbose ~term status
355 (* TODO cacciare anche altre eccezioni? *)
356 with CicUnification.UnificationFailure _ as e ->
357 raise (Fail (Printexc.to_string e))
359 (* TODO per implementare i tatticali e' necessario che tutte le tattiche
360 sollevino _solamente_ Fail *)
361 let apply_tac ~term =
362 let apply_tac ~term status =
364 apply_tac ~term status
365 (* TODO cacciare anche altre eccezioni? *)
366 with CicUnification.UnificationFailure _ as e ->
367 raise (Fail (Printexc.to_string e))
369 mk_tactic (apply_tac ~term)
371 let intros_tac ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) ()=
373 ?(mk_fresh_name_callback = (FreshNamesGenerator.mk_fresh_name ~subst:[])) ()
376 let module C = Cic in
377 let module R = CicReduction in
378 let (_,metasenv,_,_) = proof in
379 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
380 let newmeta = new_meta_of_proof ~proof in
381 let (context',ty',bo') =
382 lambda_abstract metasenv context newmeta ty mk_fresh_name_callback
385 subst_meta_in_proof proof metano bo' [newmeta,context',ty']
387 (newproof, [newmeta])
389 mk_tactic (intros_tac ~mk_fresh_name_callback ())
391 let cut_tac?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[]) ~term=
393 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
396 let module C = Cic in
397 let curi,metasenv,pbo,pty = proof in
398 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
399 let newmeta1 = new_meta_of_proof ~proof in
400 let newmeta2 = newmeta1 + 1 in
402 mk_fresh_name_callback metasenv context (Cic.Name "Hcut") ~typ:term in
403 let context_for_newmeta1 =
404 (Some (fresh_name,C.Decl term))::context in
406 CicMkImplicit.identity_relocation_list_for_metavariable
410 CicMkImplicit.identity_relocation_list_for_metavariable context
412 let newmeta1ty = CicSubstitution.lift 1 ty in
415 [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
416 C.Meta (newmeta2,irl2)]
419 subst_meta_in_proof proof metano bo'
420 [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
422 (newproof, [newmeta1 ; newmeta2])
424 mk_tactic (cut_tac ~mk_fresh_name_callback term)
426 let letin_tac ?(mk_fresh_name_callback=FreshNamesGenerator.mk_fresh_name ~subst:[]) ~term=
428 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name ~subst:[])
431 let module C = Cic in
432 let curi,metasenv,pbo,pty = proof in
433 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
434 let _,_ = (* TASSI: FIXME *)
435 CicTypeChecker.type_of_aux' metasenv context term CicUniv.empty_ugraph in
436 let newmeta = new_meta_of_proof ~proof in
438 mk_fresh_name_callback metasenv context (Cic.Name "Hletin") ~typ:term in
439 let context_for_newmeta =
440 (Some (fresh_name,C.Def (term,None)))::context in
442 CicMkImplicit.identity_relocation_list_for_metavariable
445 let newmetaty = CicSubstitution.lift 1 ty in
446 let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
449 proof metano bo'[newmeta,context_for_newmeta,newmetaty]
451 (newproof, [newmeta])
453 mk_tactic (letin_tac ~mk_fresh_name_callback term)
455 (** functional part of the "exact" tactic *)
456 let exact_tac ~term =
457 let exact_tac ~term (proof, goal) =
458 (* Assumption: the term bo must be closed in the current context *)
459 let (_,metasenv,_,_) = proof in
460 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
461 let module T = CicTypeChecker in
462 let module R = CicReduction in
463 let ty_term,u = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in
464 let b,_ = R.are_convertible context ty_term ty u in (* TASSI: FIXME *)
467 let (newproof, metasenv') =
468 subst_meta_in_proof proof metano term [] in
472 raise (Fail "The type of the provided term is not the one expected.")
474 mk_tactic (exact_tac ~term)
476 (* not really "primitive" tactics .... *)
478 let elim_tac ~term (proof, goal) =
479 let module T = CicTypeChecker in
480 let module U = UriManager in
481 let module R = CicReduction in
482 let module C = Cic in
483 let (curi,metasenv,proofbo,proofty) = proof in
484 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
485 let termty,_ = T.type_of_aux' metasenv context term CicUniv.empty_ugraph in
487 let uri,exp_named_subst,typeno,args =
489 C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
490 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
491 (uri,exp_named_subst,typeno,args)
492 | _ -> raise NotAnInductiveTypeToEliminate
495 let buri = U.buri_of_uri uri in
497 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
499 C.InductiveDefinition (tys,_,_,_) ->
500 let (name,_,_,_) = List.nth tys typeno in
504 let ty_ty,_ = T.type_of_aux' metasenv context ty CicUniv.empty_ugraph in
508 C.Sort C.Prop -> "_ind"
509 | C.Sort C.Set -> "_rec"
510 | C.Sort C.CProp -> "_rec"
511 | C.Sort (C.Type _)-> "_rect"
514 U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
516 let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in
518 T.type_of_aux' metasenv context eliminator_ref CicUniv.empty_ugraph in
519 let rec find_args_no =
521 C.Prod (_,_,t) -> 1 + find_args_no t
522 | C.Cast (s,_) -> find_args_no s
523 | C.LetIn (_,_,t) -> 0 + find_args_no t
526 let args_no = find_args_no ety in
528 let rec make_tl base_case =
531 | n -> (C.Implicit None)::(make_tl base_case (n - 1))
533 C.Appl (eliminator_ref :: make_tl term (args_no - 1))
535 let metasenv', term_to_refine' =
536 CicMkImplicit.expand_implicits metasenv [] context term_to_refine in
537 let refined_term,_,metasenv'',_ = (* TASSI: FIXME *)
538 CicRefine.type_of_aux' metasenv' context term_to_refine'
542 ProofEngineHelpers.compare_metasenvs
543 ~oldmetasenv:metasenv ~newmetasenv:metasenv''
545 let proof' = curi,metasenv'',proofbo,proofty in
546 let proof'', new_goals' =
547 apply_tactic (apply_tac ~term:refined_term) (proof',goal)
549 (* The apply_tactic can have closed some of the new_goals *)
550 let patched_new_goals =
551 let (_,metasenv''',_,_) = proof'' in
553 (function i -> List.exists (function (j,_,_) -> j=i) metasenv'''
554 ) new_goals @ new_goals'
556 proof'', patched_new_goals
558 mk_tactic (elim_tac ~term)
561 let elim_intros_tac ~term =
562 Tacticals.then_ ~start:(elim_tac ~term)
563 ~continuation:(intros_tac ())
566 (* The simplification is performed only on the conclusion *)
567 let elim_intros_simpl_tac ~term =
568 Tacticals.then_ ~start:(elim_tac ~term)
571 ~start:(intros_tac ())
573 [ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None])
576 exception NotConvertible
578 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal, *)
579 (*CSC: while [what] can have a richer context (because of binders) *)
580 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
581 (*CSC: Is that evident? Is that right? Or should it be changed? *)
582 let change_tac ~what ~with_what =
583 let change_tac ~what ~with_what (proof, goal) =
584 let curi,metasenv,pbo,pty = proof in
585 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
586 (* are_convertible works only on well-typed terms *)
588 CicTypeChecker.type_of_aux' metasenv context with_what
590 in (* TASSI: FIXME *)
592 CicReduction.are_convertible context what with_what u
597 ProofEngineReduction.replace
598 ~equality:(==) ~what:[what] ~with_what:[with_what]
600 let ty' = replace ty in
604 Some (name,Cic.Def (t,None))->
605 Some (name,Cic.Def ((replace t),None))
606 | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
608 | Some (_,Cic.Def (_,Some _)) -> assert false
614 (n,_,_) when n = metano -> (metano,context',ty')
618 (curi,metasenv',pbo,pty), [metano]
621 raise (ProofEngineTypes.Fail "Not convertible")
623 mk_tactic (change_tac ~what ~with_what)