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 | C.Implicit _ as t -> t
78 | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
79 | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
80 | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
81 | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
82 | C.Appl l -> C.Appl (List.map (aux n) l)
83 | C.Const (uri,exp_named_subst) ->
84 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
85 C.Const (uri,exp_named_subst')
86 | C.MutInd (uri,i,exp_named_subst) ->
87 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
88 C.MutInd (uri,i,exp_named_subst')
89 | C.MutConstruct (uri,i,j,exp_named_subst) ->
90 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
91 C.MutConstruct (uri,i,j,exp_named_subst')
92 | C.MutCase (sp,i,outt,t,pl) ->
93 C.MutCase (sp,i,aux n outt, aux n t,
96 let tylen = List.length fl in
99 (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
102 C.Fix (i, substitutedfl)
104 let tylen = List.length fl in
107 (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
110 C.CoFix (i, substitutedfl)
111 and aux_exp_named_subst n =
112 List.map (function uri,t -> uri,aux n t)
115 T.type_of_aux' metasenv context arg
118 FreshNamesGenerator.mk_fresh_name
119 metasenv context (Cic.Name "Heta") ~typ:argty
121 (C.Appl [C.Lambda (fresh_name,argty,aux 0 t) ; arg])
123 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
124 let classify_metas newmeta in_subst_domain subst_in metasenv =
126 (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
127 if in_subst_domain i then
128 old_uninst,new_uninst
130 let ty' = subst_in canonical_context ty in
131 let canonical_context' =
133 (fun entry canonical_context' ->
136 Some (n,Cic.Decl s) ->
137 Some (n,Cic.Decl (subst_in canonical_context' s))
138 | Some (n,Cic.Def (s,None)) ->
139 Some (n,Cic.Def ((subst_in canonical_context' s),None))
141 | Some (_,Cic.Def (_,Some _)) -> assert false
143 entry'::canonical_context'
144 ) canonical_context []
147 ((i,canonical_context',ty')::old_uninst),new_uninst
149 old_uninst,((i,canonical_context',ty')::new_uninst)
152 (* Auxiliary function for apply: given a type (a backbone), it returns its *)
153 (* head, a META environment in which there is new a META for each hypothesis,*)
154 (* a list of arguments for the new applications and the indexes of the first *)
155 (* and last new METAs introduced. The nth argument in the list of arguments *)
156 (* is just the nth new META. *)
157 let new_metasenv_for_apply newmeta proof context ty =
158 let module C = Cic in
159 let module S = CicSubstitution in
160 let rec aux newmeta =
162 C.Cast (he,_) -> aux newmeta he
163 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
164 (* If the expected type is a Type, then also Set is OK ==>
165 * we accept any term of type Type *)
166 (*CSC: BUG HERE: in this way it is possible for the term of
167 * type Type to be different from a Sort!!! *)
168 | C.Prod (name,(C.Sort (C.Type _) as s),t) ->
169 (* TASSI: ask CSC if BUG HERE refers to the C.Cast or C.Propd case *)
171 CicMkImplicit.identity_relocation_list_for_metavariable context
173 let newargument = C.Meta (newmeta+1,irl) in
174 let (res,newmetasenv,arguments,lastmeta) =
175 aux (newmeta + 2) (S.subst newargument t)
178 (newmeta,[],s)::(newmeta+1,context,C.Meta (newmeta,[]))::newmetasenv,
179 newargument::arguments,lastmeta
181 | C.Prod (name,s,t) ->
183 CicMkImplicit.identity_relocation_list_for_metavariable context
185 let newargument = C.Meta (newmeta,irl) in
186 let (res,newmetasenv,arguments,lastmeta) =
187 aux (newmeta + 1) (S.subst newargument t)
189 res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
190 | t -> t,[],[],newmeta
192 (* WARNING: here we are using the invariant that above the most *)
193 (* recente new_meta() there are no used metas. *)
194 let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
195 res,newmetasenv,arguments,lastmeta
197 (* Useful only inside apply_tac *)
199 generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
201 let module C = Cic in
203 match CicEnvironment.get_obj uri with
204 C.Constant (_,_,_,params)
205 | C.CurrentProof (_,_,_,_,params)
206 | C.Variable (_,_,_,params)
207 | C.InductiveDefinition (_,params,_) -> params
209 let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
210 let next_fresh_meta = ref newmeta in
211 let newmetasenvfragment = ref [] in
212 let exp_named_subst_diff = ref [] in
218 match CicEnvironment.get_obj uri with
219 C.Variable (_,_,ty,_) ->
220 CicSubstitution.subst_vars !exp_named_subst_diff ty
221 | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
223 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
225 C.Sort (C.Type _) as s -> (* TASSI: ?? *)
226 let fresh_meta = !next_fresh_meta in
227 let fresh_meta' = fresh_meta + 1 in
228 next_fresh_meta := !next_fresh_meta + 2 ;
229 let subst_item = uri,C.Meta (fresh_meta',[]) in
230 newmetasenvfragment :=
231 (fresh_meta,[],C.Sort (C.Type (CicUniv.fresh()))) ::
233 (fresh_meta',[],C.Meta (fresh_meta,[])) :: !newmetasenvfragment ;
234 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
235 subst_item::(aux (tl,[]))
239 CicMkImplicit.identity_relocation_list_for_metavariable context
241 let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
242 newmetasenvfragment :=
243 (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
244 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
245 incr next_fresh_meta ;
246 subst_item::(aux (tl,[]))(*)*)
247 | uri::tl1,((uri',_) as s)::tl2 ->
248 assert (UriManager.eq uri uri') ;
250 | [],_ -> assert false
252 let exp_named_subst' = aux (params,exp_named_subst) in
253 !exp_named_subst_diff,!next_fresh_meta,
254 List.rev !newmetasenvfragment, exp_named_subst'
256 new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
259 let apply_tac ~term (proof, goal) =
260 (* Assumption: The term "term" must be closed in the current context *)
261 let module T = CicTypeChecker in
262 let module R = CicReduction in
263 let module C = Cic in
264 let (_,metasenv,_,_) = proof in
265 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
266 let newmeta = new_meta_of_proof ~proof in
267 let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
269 C.Var (uri,exp_named_subst) ->
270 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
271 generalize_exp_named_subst_with_fresh_metas context newmeta uri
274 exp_named_subst_diff,newmeta',newmetasenvfragment,
275 C.Var (uri,exp_named_subst')
276 | C.Const (uri,exp_named_subst) ->
277 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
278 generalize_exp_named_subst_with_fresh_metas context newmeta uri
281 exp_named_subst_diff,newmeta',newmetasenvfragment,
282 C.Const (uri,exp_named_subst')
283 | C.MutInd (uri,tyno,exp_named_subst) ->
284 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
285 generalize_exp_named_subst_with_fresh_metas context newmeta uri
288 exp_named_subst_diff,newmeta',newmetasenvfragment,
289 C.MutInd (uri,tyno,exp_named_subst')
290 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
291 let newmeta',newmetasenvfragment,exp_named_subst',exp_named_subst_diff =
292 generalize_exp_named_subst_with_fresh_metas context newmeta uri
295 exp_named_subst_diff,newmeta',newmetasenvfragment,
296 C.MutConstruct (uri,tyno,consno,exp_named_subst')
297 | _ -> [],newmeta,[],term
299 let metasenv' = metasenv@newmetasenvfragment in
301 CicSubstitution.subst_vars exp_named_subst_diff
302 (CicTypeChecker.type_of_aux' metasenv' context term)
304 (* newmeta is the lowest index of the new metas introduced *)
305 let (consthead,newmetas,arguments,_) =
306 new_metasenv_for_apply newmeta' proof context termty
308 let newmetasenv = metasenv'@newmetas in
309 let subst,newmetasenv' =
310 CicUnification.fo_unif newmetasenv context consthead ty
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'
321 (if List.length newmetas = 0 then
324 Cic.Appl (term'::arguments)
327 let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
328 let (newproof, newmetasenv''') =
329 let subst_in = CicMetaSubst.apply_subst ((metano,bo')::subst) in
330 subst_meta_and_metasenv_in_proof
331 proof metano subst_in newmetasenv''
333 (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
335 (* TODO per implementare i tatticali e' necessario che tutte le tattiche
336 sollevino _solamente_ Fail *)
337 let apply_tac ~term status =
339 apply_tac ~term status
340 (* TODO cacciare anche altre eccezioni? *)
341 with CicUnification.UnificationFailure _ as e ->
342 raise (Fail (Printexc.to_string e))
345 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name) ()
348 let module C = Cic in
349 let module R = CicReduction in
350 let (_,metasenv,_,_) = proof in
351 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
352 let newmeta = new_meta_of_proof ~proof in
353 let (context',ty',bo') =
354 lambda_abstract metasenv context newmeta ty mk_fresh_name_callback
357 subst_meta_in_proof proof metano bo' [newmeta,context',ty']
359 (newproof, [newmeta])
362 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
365 let module C = Cic in
366 let curi,metasenv,pbo,pty = proof in
367 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
368 let newmeta1 = new_meta_of_proof ~proof in
369 let newmeta2 = newmeta1 + 1 in
371 mk_fresh_name_callback metasenv context (Cic.Name "Hcut") ~typ:term in
372 let context_for_newmeta1 =
373 (Some (fresh_name,C.Decl term))::context in
375 CicMkImplicit.identity_relocation_list_for_metavariable
379 CicMkImplicit.identity_relocation_list_for_metavariable context
381 let newmeta1ty = CicSubstitution.lift 1 ty in
384 [C.Lambda (fresh_name,term,C.Meta (newmeta1,irl1)) ;
385 C.Meta (newmeta2,irl2)]
388 subst_meta_in_proof proof metano bo'
389 [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
391 (newproof, [newmeta1 ; newmeta2])
394 ?(mk_fresh_name_callback = FreshNamesGenerator.mk_fresh_name)
397 let module C = Cic in
398 let curi,metasenv,pbo,pty = proof in
399 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
400 let _ = CicTypeChecker.type_of_aux' metasenv context term in
401 let newmeta = new_meta_of_proof ~proof in
403 mk_fresh_name_callback metasenv context (Cic.Name "Hletin") ~typ:term in
404 let context_for_newmeta =
405 (Some (fresh_name,C.Def (term,None)))::context in
407 CicMkImplicit.identity_relocation_list_for_metavariable
410 let newmetaty = CicSubstitution.lift 1 ty in
411 let bo' = C.LetIn (fresh_name,term,C.Meta (newmeta,irl)) in
414 proof metano bo'[newmeta,context_for_newmeta,newmetaty]
416 (newproof, [newmeta])
418 (** functional part of the "exact" tactic *)
419 let exact_tac ~term (proof, goal) =
420 (* Assumption: the term bo must be closed in the current context *)
421 let (_,metasenv,_,_) = proof in
422 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
423 let module T = CicTypeChecker in
424 let module R = CicReduction in
425 if R.are_convertible context (T.type_of_aux' metasenv context term) ty then
427 let (newproof, metasenv') =
428 subst_meta_in_proof proof metano term [] in
432 raise (Fail "The type of the provided term is not the one expected.")
435 (* not really "primitive" tactics .... *)
437 let elim_tac ~term (proof, goal) =
438 let module T = CicTypeChecker in
439 let module U = UriManager in
440 let module R = CicReduction in
441 let module C = Cic in
442 let (curi,metasenv,_,_) = proof in
443 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
444 let termty = T.type_of_aux' metasenv context term in
445 let uri,exp_named_subst,typeno,args =
447 C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
448 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
449 (uri,exp_named_subst,typeno,args)
450 | _ -> raise NotAnInductiveTypeToEliminate
453 let buri = U.buri_of_uri uri in
455 match CicEnvironment.get_obj uri with
456 C.InductiveDefinition (tys,_,_) ->
457 let (name,_,_,_) = List.nth tys typeno in
462 match T.type_of_aux' metasenv context ty with
463 C.Sort C.Prop -> "_ind"
464 | C.Sort C.Set -> "_rec"
465 | C.Sort C.CProp -> "_rec"
466 | C.Sort (C.Type _)-> "_rect" (* TASSI *)
469 U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
471 let eliminator_ref = C.Const (eliminator_uri,exp_named_subst) in
472 let ety = T.type_of_aux' metasenv context eliminator_ref in
473 let newmeta = new_meta_of_proof ~proof in
474 let (econclusion,newmetas,arguments,lastmeta) =
475 new_metasenv_for_apply newmeta proof context ety
477 (* Here we assume that we have only one inductive hypothesis to *)
478 (* eliminate and that it is the last hypothesis of the theorem. *)
479 (* A better approach would be fingering the hypotheses in some *)
482 let (_,canonical_context,_) =
483 CicUtil.lookup_meta (lastmeta - 1) newmetas
486 CicMkImplicit.identity_relocation_list_for_metavariable
489 Cic.Meta (lastmeta - 1, irl)
491 let newmetasenv = newmetas @ metasenv in
492 let subst1,newmetasenv' =
493 CicUnification.fo_unif newmetasenv context term meta_of_corpse
495 let ueconclusion = CicMetaSubst.apply_subst subst1 econclusion in
496 (* The conclusion of our elimination principle is *)
497 (* (?i farg1 ... fargn) *)
498 (* The conclusion of our goal is ty. So, we can *)
499 (* eta-expand ty w.r.t. farg1 .... fargn to get *)
500 (* a new ty equal to (P farg1 ... fargn). Now *)
501 (* ?i can be instantiated with P and we are ready *)
502 (* to refine the term. *)
504 match ueconclusion with
505 C.Appl ((C.Meta (emeta,_))::fargs) -> emeta,fargs
506 | C.Meta (emeta,_) -> emeta,[]
507 | _ -> raise NotTheRightEliminatorShape
509 let ty' = CicMetaSubst.apply_subst subst1 ty in
510 let eta_expanded_ty =
511 (*CSC: newmetasenv' era metasenv ??????????? *)
512 List.fold_left (eta_expand newmetasenv' context) ty' fargs
514 let subst2,newmetasenv'' =
515 (*CSC: passo newmetasenv', ma alcune variabili sono gia' state sostituite
516 da subst1!!!! Dovrei rimuoverle o sono innocue?*)
517 CicUnification.fo_unif
518 newmetasenv' context ueconclusion eta_expanded_ty
520 let in_subst_domain i =
521 let eq_to_i = function (j,_) -> i=j in
522 List.exists eq_to_i subst1 ||
523 List.exists eq_to_i subst2
525 (* When unwinding the META that corresponds to the elimination *)
526 (* predicate (which is emeta), we must also perform one-step *)
527 (* beta-reduction. apply_subst doesn't need the context. Hence *)
528 (* the underscore. *)
529 let apply_subst _ t =
530 let t' = CicMetaSubst.apply_subst subst1 t in
531 CicMetaSubst.apply_subst_reducing
532 (Some (emeta,List.length fargs)) subst2 t'
534 let old_uninstantiatedmetas,new_uninstantiatedmetas =
535 classify_metas newmeta in_subst_domain apply_subst
538 let arguments' = List.map (apply_subst context) arguments in
539 let bo' = Cic.Appl (eliminator_ref::arguments') in
541 new_uninstantiatedmetas@old_uninstantiatedmetas
543 let (newproof, newmetasenv'''') =
544 (* When unwinding the META that corresponds to the *)
545 (* elimination predicate (which is emeta), we must *)
546 (* also perform one-step beta-reduction. *)
547 (* The only difference w.r.t. apply_subst is that *)
548 (* we also substitute metano with bo'. *)
549 (*CSC: Nota: sostituire nuovamente subst1 e' superfluo, *)
552 let t' = CicMetaSubst.apply_subst subst1 t in
553 CicMetaSubst.apply_subst_reducing
554 (Some (emeta,List.length fargs))
555 ((metano,bo')::subst2) t'
557 subst_meta_and_metasenv_in_proof
558 proof metano apply_subst' newmetasenv'''
561 List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
564 (* The simplification is performed only on the conclusion *)
565 let elim_intros_simpl_tac ~term =
566 Tacticals.then_ ~start:(elim_tac ~term)
569 ~start:(intros_tac ())
571 [ReductionTactics.simpl_tac ~also_in_hypotheses:false ~terms:None])
574 exception NotConvertible
576 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal, *)
577 (*CSC: while [what] can have a richer context (because of binders) *)
578 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
579 (*CSC: Is that evident? Is that right? Or should it be changed? *)
580 let change_tac ~what ~with_what (proof, goal) =
581 let curi,metasenv,pbo,pty = proof in
582 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
583 (* are_convertible works only on well-typed terms *)
584 ignore (CicTypeChecker.type_of_aux' metasenv context with_what) ;
585 if CicReduction.are_convertible context what with_what then
588 ProofEngineReduction.replace
589 ~equality:(==) ~what:[what] ~with_what:[with_what]
591 let ty' = replace ty in
595 Some (name,Cic.Def (t,None)) -> Some (name,Cic.Def ((replace t),None))
596 | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
598 | Some (_,Cic.Def (_,Some _)) -> assert false
604 (n,_,_) when n = metano -> (metano,context',ty')
608 (curi,metasenv',pbo,pty), [metano]
611 raise (ProofEngineTypes.Fail "Not convertible")