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 (* TODO problemone del fresh_name, aggiungerlo allo status? *)
35 let fresh_name () = "FOO"
37 (* lambda_abstract newmeta ty *)
38 (* returns a triple [bo],[context],[ty'] where *)
39 (* [ty] = Pi/LetIn [context].[ty'] ([context] is a vector!) *)
40 (* and [bo] = Lambda/LetIn [context].(Meta [newmeta]) *)
41 (* So, lambda_abstract is the core of the implementation of *)
42 (* the Intros tactic. *)
43 let lambda_abstract context newmeta ty name =
45 let rec collect_context context =
47 C.Cast (te,_) -> collect_context context te
52 (*CSC: generatore di nomi? Chiedere il nome? *)
53 | C.Anonymous -> C.Name name
55 let (context',ty,bo) =
56 collect_context ((Some (n',(C.Decl s)))::context) t
58 (context',ty,C.Lambda(n',s,bo))
60 let (context',ty,bo) =
61 collect_context ((Some (n,(C.Def s)))::context) t
63 (context',ty,C.LetIn(n,s,bo))
65 let irl = identity_relocation_list_for_metavariable context in
66 context, t, (C.Meta (newmeta,irl))
68 collect_context context ty
70 let eta_expand metasenv context t arg =
71 let module T = CicTypeChecker in
72 let module S = CicSubstitution in
76 t' when t' = S.lift n arg -> C.Rel (1 + n)
77 | C.Rel m -> if m <= n then C.Rel m else C.Rel (m+1)
78 | C.Var (uri,exp_named_subst) ->
79 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
80 C.Var (uri,exp_named_subst')
83 | C.Implicit as t -> t
84 | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
85 | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
86 | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
87 | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
88 | C.Appl l -> C.Appl (List.map (aux n) l)
89 | C.Const (uri,exp_named_subst) ->
90 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
91 C.Const (uri,exp_named_subst')
92 | C.MutInd (uri,i,exp_named_subst) ->
93 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
94 C.MutInd (uri,i,exp_named_subst')
95 | C.MutConstruct (uri,i,j,exp_named_subst) ->
96 let exp_named_subst' = aux_exp_named_subst n exp_named_subst in
97 C.MutConstruct (uri,i,j,exp_named_subst')
98 | C.MutCase (sp,i,outt,t,pl) ->
99 C.MutCase (sp,i,aux n outt, aux n t,
102 let tylen = List.length fl in
105 (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
108 C.Fix (i, substitutedfl)
110 let tylen = List.length fl in
113 (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
116 C.CoFix (i, substitutedfl)
117 and aux_exp_named_subst n =
118 List.map (function uri,t -> uri,aux n t)
121 T.type_of_aux' metasenv context arg
123 (C.Appl [C.Lambda ((C.Name "dummy"),argty,aux 0 t) ; arg])
125 (*CSC: The call to the Intros tactic is embedded inside the code of the *)
126 (*CSC: Elim tactic. Do we already need tacticals? *)
127 (* Auxiliary function for apply: given a type (a backbone), it returns its *)
128 (* head, a META environment in which there is new a META for each hypothesis,*)
129 (* a list of arguments for the new applications and the indexes of the first *)
130 (* and last new METAs introduced. The nth argument in the list of arguments *)
131 (* is the nth new META lambda-abstracted as much as possible. Hence, this *)
132 (* functions already provides the behaviour of Intros on the new goals. *)
133 let new_metasenv_for_apply_intros proof context ty =
134 let module C = Cic in
135 let module S = CicSubstitution in
136 let rec aux newmeta =
138 C.Cast (he,_) -> aux newmeta he
139 | C.Prod (name,s,t) ->
140 let newcontext,ty',newargument =
141 lambda_abstract context newmeta s (fresh_name ())
143 let (res,newmetasenv,arguments,lastmeta) =
144 aux (newmeta + 1) (S.subst newargument t)
146 res,(newmeta,newcontext,ty')::newmetasenv,newargument::arguments,lastmeta
147 | t -> t,[],[],newmeta
149 let newmeta = new_meta ~proof in
150 (* WARNING: here we are using the invariant that above the most *)
151 (* recente new_meta() there are no used metas. *)
152 let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
153 res,newmetasenv,arguments,newmeta,lastmeta
155 (*CSC: ma serve solamente la prima delle new_uninst e l'unione delle due!!! *)
156 let classify_metas newmeta in_subst_domain subst_in metasenv =
158 (fun (i,canonical_context,ty) (old_uninst,new_uninst) ->
159 if in_subst_domain i then
160 old_uninst,new_uninst
162 let ty' = subst_in canonical_context ty in
163 let canonical_context' =
165 (fun entry canonical_context' ->
168 Some (n,Cic.Decl s) ->
169 Some (n,Cic.Decl (subst_in canonical_context' s))
170 | Some (n,Cic.Def s) ->
171 Some (n,Cic.Def (subst_in canonical_context' s))
174 entry'::canonical_context'
175 ) canonical_context []
178 ((i,canonical_context',ty')::old_uninst),new_uninst
180 old_uninst,((i,canonical_context',ty')::new_uninst)
183 (* Auxiliary function for apply: given a type (a backbone), it returns its *)
184 (* head, a META environment in which there is new a META for each hypothesis,*)
185 (* a list of arguments for the new applications and the indexes of the first *)
186 (* and last new METAs introduced. The nth argument in the list of arguments *)
187 (* is just the nth new META. *)
188 let new_metasenv_for_apply newmeta proof context ty =
189 let module C = Cic in
190 let module S = CicSubstitution in
191 let rec aux newmeta =
193 C.Cast (he,_) -> aux newmeta he
194 | C.Prod (name,s,t) ->
195 let irl = identity_relocation_list_for_metavariable context in
196 let newargument = C.Meta (newmeta,irl) in
197 let (res,newmetasenv,arguments,lastmeta) =
198 aux (newmeta + 1) (S.subst newargument t)
200 res,(newmeta,context,s)::newmetasenv,newargument::arguments,lastmeta
201 | t -> t,[],[],newmeta
203 (* WARNING: here we are using the invariant that above the most *)
204 (* recente new_meta() there are no used metas. *)
205 let (res,newmetasenv,arguments,lastmeta) = aux newmeta ty in
206 res,newmetasenv,arguments,lastmeta
208 (* Useful only inside apply_tac *)
210 generalize_exp_named_subst_with_fresh_metas context newmeta uri exp_named_subst
212 let module C = Cic in
214 match CicEnvironment.get_obj uri with
215 C.Constant (_,_,_,params)
216 | C.CurrentProof (_,_,_,_,params)
217 | C.Variable (_,_,_,params)
218 | C.InductiveDefinition (_,params,_) -> params
220 let exp_named_subst_diff,new_fresh_meta,newmetasenvfragment,exp_named_subst'=
221 let next_fresh_meta = ref newmeta in
222 let newmetasenvfragment = ref [] in
223 let exp_named_subst_diff = ref [] in
229 match CicEnvironment.get_obj uri with
230 C.Variable (_,_,ty,_) ->
231 CicSubstitution.subst_vars !exp_named_subst_diff ty
232 | _ -> raise (WrongUriToVariable (UriManager.string_of_uri uri))
234 let irl = identity_relocation_list_for_metavariable context in
235 let subst_item = uri,C.Meta (!next_fresh_meta,irl) in
236 newmetasenvfragment :=
237 (!next_fresh_meta,context,ty)::!newmetasenvfragment ;
238 exp_named_subst_diff := !exp_named_subst_diff @ [subst_item] ;
239 incr next_fresh_meta ;
240 subst_item::(aux (tl,[]))
241 | uri::tl1,((uri',_) as s)::tl2 ->
242 assert (UriManager.eq uri uri') ;
244 | [],_ -> assert false
246 let exp_named_subst' = aux (params,exp_named_subst) in
247 !exp_named_subst_diff,!next_fresh_meta,
248 List.rev !newmetasenvfragment, exp_named_subst'
250 prerr_endline ("@@@ " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst)) ^ " |--> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst'))) ;
251 new_fresh_meta,newmetasenvfragment,exp_named_subst',exp_named_subst_diff
254 let apply_tac ~term ~status:(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 = List.find (function (m,_,_) -> m=goal) metasenv in
261 let newmeta = new_meta ~proof 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' = newmetasenvfragment@metasenv in
295 prerr_endline ("^^^^^TERM': " ^ CicPp.ppterm term') ;
297 CicSubstitution.subst_vars exp_named_subst_diff
298 (CicTypeChecker.type_of_aux' metasenv' context term)
300 prerr_endline ("^^^^^TERMTY: " ^ CicPp.ppterm termty) ;
301 (* newmeta is the lowest index of the new metas introduced *)
302 let (consthead,newmetas,arguments,_) =
303 new_metasenv_for_apply newmeta' proof context termty
305 let newmetasenv = newmetas@metasenv' in
306 let subst,newmetasenv' =
307 CicUnification.fo_unif newmetasenv context consthead ty
309 let in_subst_domain i = List.exists (function (j,_) -> i=j) subst in
310 let apply_subst = CicUnification.apply_subst subst in
311 let old_uninstantiatedmetas,new_uninstantiatedmetas =
312 (* subst_in doesn't need the context. Hence the underscore. *)
313 let subst_in _ = CicUnification.apply_subst subst in
314 classify_metas newmeta' in_subst_domain subst_in newmetasenv'
318 (if List.length newmetas = 0 then
321 Cic.Appl (term'::arguments)
324 prerr_endline ("XXXX " ^ CicPp.ppterm (if List.length newmetas = 0 then term' else Cic.Appl (term'::arguments)) ^ " |>>> " ^ CicPp.ppterm bo') ;
325 let newmetasenv'' = new_uninstantiatedmetas@old_uninstantiatedmetas in
326 let (newproof, newmetasenv''') =
327 let subst_in = CicUnification.apply_subst ((metano,bo')::subst) in
328 subst_meta_and_metasenv_in_proof
329 proof metano subst_in newmetasenv''
331 (newproof, List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
333 (* TODO per implementare i tatticali e' necessario che tutte le tattiche
334 sollevino _solamente_ Fail *)
335 let apply_tac ~term ~status =
337 apply_tac ~term ~status
338 (* TODO cacciare anche altre eccezioni? *)
339 with CicUnification.UnificationFailed as e ->
340 raise (Fail (Printexc.to_string e))
342 let intros_tac ~name ~status:(proof, goal) =
343 let module C = Cic in
344 let module R = CicReduction in
345 let (_,metasenv,_,_) = proof in
346 let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
347 let newmeta = new_meta ~proof in
348 let (context',ty',bo') = lambda_abstract context newmeta ty name in
350 subst_meta_in_proof proof metano bo' [newmeta,context',ty']
352 (newproof, [newmeta])
354 let cut_tac ~term ~status:(proof, goal) =
355 let module C = Cic in
356 let curi,metasenv,pbo,pty = proof in
357 let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
358 let newmeta1 = new_meta ~proof in
359 let newmeta2 = newmeta1 + 1 in
360 let context_for_newmeta1 =
361 (Some (C.Name "dummy_for_cut",C.Decl term))::context in
363 identity_relocation_list_for_metavariable context_for_newmeta1 in
364 let irl2 = identity_relocation_list_for_metavariable context in
365 let newmeta1ty = CicSubstitution.lift 1 ty in
368 [C.Lambda (C.Name "dummy_for_cut",term,C.Meta (newmeta1,irl1)) ;
369 C.Meta (newmeta2,irl2)]
372 subst_meta_in_proof proof metano bo'
373 [newmeta2,context,term; newmeta1,context_for_newmeta1,newmeta1ty];
375 (newproof, [newmeta1 ; newmeta2])
377 let letin_tac ~term ~status:(proof, goal) =
378 let module C = Cic in
379 let curi,metasenv,pbo,pty = proof in
380 let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
381 let _ = CicTypeChecker.type_of_aux' metasenv context term in
382 let newmeta = new_meta ~proof in
383 let context_for_newmeta =
384 (Some (C.Name "dummy_for_letin",C.Def term))::context in
386 identity_relocation_list_for_metavariable context_for_newmeta in
387 let newmetaty = CicSubstitution.lift 1 ty in
388 let bo' = C.LetIn (C.Name "dummy_for_letin",term,C.Meta (newmeta,irl)) in
391 proof metano bo'[newmeta,context_for_newmeta,newmetaty]
393 (newproof, [newmeta])
395 (** functional part of the "exact" tactic *)
396 let exact_tac ~term ~status:(proof, goal) =
397 (* Assumption: the term bo must be closed in the current context *)
398 let (_,metasenv,_,_) = proof in
399 let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
400 let module T = CicTypeChecker in
401 let module R = CicReduction in
402 if R.are_convertible context (T.type_of_aux' metasenv context term) ty then
404 let (newproof, metasenv') =
405 subst_meta_in_proof proof metano term [] in
409 raise (Fail "The type of the provided term is not the one expected.")
412 (* not really "primite" tactics .... *)
414 let elim_intros_simpl_tac ~term ~status:(proof, goal) =
415 let module T = CicTypeChecker in
416 let module U = UriManager in
417 let module R = CicReduction in
418 let module C = Cic in
419 let (curi,metasenv,_,_) = proof in
420 let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
421 let termty = T.type_of_aux' metasenv context term in
422 let uri,exp_named_subst,typeno,args =
424 C.MutInd (uri,typeno,exp_named_subst) -> (uri,exp_named_subst,typeno,[])
425 | C.Appl ((C.MutInd (uri,typeno,exp_named_subst))::args) ->
426 (uri,exp_named_subst,typeno,args)
428 prerr_endline ("MALFATTORE" ^ (CicPp.ppterm termty));
430 raise NotAnInductiveTypeToEliminate
433 let buri = U.buri_of_uri uri in
435 match CicEnvironment.get_obj uri with
436 C.InductiveDefinition (tys,_,_) ->
437 let (name,_,_,_) = List.nth tys typeno in
442 match T.type_of_aux' metasenv context ty with
443 C.Sort C.Prop -> "_ind"
444 | C.Sort C.Set -> "_rec"
445 | C.Sort C.Type -> "_rect"
448 U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
450 (*CSC: BUG HERE!!! [] MUST BE COMPUTED SOMEHOW. USING UNIFICATION? *)
451 let eliminator_ref = C.Const (eliminator_uri,[]) in
453 T.type_of_aux' [] [] eliminator_ref
455 let (econclusion,newmetas,arguments,newmeta,lastmeta) =
457 new_metasenv_for_apply context ety
459 new_metasenv_for_apply_intros proof context ety
461 (* Here we assume that we have only one inductive hypothesis to *)
462 (* eliminate and that it is the last hypothesis of the theorem. *)
463 (* A better approach would be fingering the hypotheses in some *)
466 let (_,canonical_context,_) =
467 List.find (function (m,_,_) -> m=(lastmeta - 1)) newmetas
470 identity_relocation_list_for_metavariable canonical_context
472 Cic.Meta (lastmeta - 1, irl)
474 let newmetasenv = newmetas @ metasenv in
475 let subst1,newmetasenv' =
476 CicUnification.fo_unif newmetasenv context term meta_of_corpse
478 let ueconclusion = CicUnification.apply_subst subst1 econclusion in
479 (* The conclusion of our elimination principle is *)
480 (* (?i farg1 ... fargn) *)
481 (* The conclusion of our goal is ty. So, we can *)
482 (* eta-expand ty w.r.t. farg1 .... fargn to get *)
483 (* a new ty equal to (P farg1 ... fargn). Now *)
484 (* ?i can be instantiated with P and we are ready *)
485 (* to refine the term. *)
487 match ueconclusion with
488 (*CSC: Code to be used for Apply
489 C.Appl ((C.Meta (emeta,_))::fargs) -> emeta,fargs
490 | C.Meta (emeta,_) -> emeta,[]
492 (*CSC: Code to be used for ApplyIntros *)
493 C.Appl (he::fargs) ->
496 C.Meta (emeta,_) -> emeta
497 | C.Lambda (_,_,t) -> find_head t
498 | C.LetIn (_,_,t) -> find_head t
499 | _ ->raise NotTheRightEliminatorShape
502 | C.Meta (emeta,_) -> emeta,[]
504 | _ -> raise NotTheRightEliminatorShape
506 let ty' = CicUnification.apply_subst subst1 ty in
507 let eta_expanded_ty =
508 (*CSC: newmetasenv' era metasenv ??????????? *)
509 List.fold_left (eta_expand newmetasenv' context) ty' fargs
511 let subst2,newmetasenv'' =
512 (*CSC: passo newmetasenv', ma alcune variabili sono gia' state sostituite
513 da subst1!!!! Dovrei rimuoverle o sono innocue?*)
514 CicUnification.fo_unif
515 newmetasenv' context ueconclusion eta_expanded_ty
517 let in_subst_domain i =
518 let eq_to_i = function (j,_) -> i=j in
519 List.exists eq_to_i subst1 ||
520 List.exists eq_to_i subst2
522 (*CSC: codice per l'elim
523 (* When unwinding the META that corresponds to the elimination *)
524 (* predicate (which is emeta), we must also perform one-step *)
525 (* beta-reduction. apply_subst doesn't need the context. Hence *)
526 (* the underscore. *)
527 let apply_subst _ t =
528 let t' = CicUnification.apply_subst subst1 t in
529 CicUnification.apply_subst_reducing
530 subst2 (Some (emeta,List.length fargs)) t'
533 (*CSC: codice per l'elim_intros_simpl. Non effettua semplificazione. *)
534 let apply_subst context t =
535 let t' = CicUnification.apply_subst (subst1@subst2) t in
536 ProofEngineReduction.simpl context t'
539 let old_uninstantiatedmetas,new_uninstantiatedmetas =
540 classify_metas newmeta in_subst_domain apply_subst
543 let arguments' = List.map (apply_subst context) arguments in
544 let bo' = Cic.Appl (eliminator_ref::arguments') in
546 new_uninstantiatedmetas@old_uninstantiatedmetas
548 let (newproof, newmetasenv'''') =
549 (* When unwinding the META that corresponds to the *)
550 (* elimination predicate (which is emeta), we must *)
551 (* also perform one-step beta-reduction. *)
552 (* The only difference w.r.t. apply_subst is that *)
553 (* we also substitute metano with bo'. *)
554 (*CSC: Nota: sostituire nuovamente subst1 e' superfluo, *)
556 (*CSC: codice per l'elim
558 let t' = CicUnification.apply_subst subst1 t in
559 CicUnification.apply_subst_reducing
560 ((metano,bo')::subst2)
561 (Some (emeta,List.length fargs)) t'
564 (*CSC: codice per l'elim_intros_simpl *)
566 CicUnification.apply_subst
567 ((metano,bo')::(subst1@subst2)) t
570 subst_meta_and_metasenv_in_proof
571 proof metano apply_subst' newmetasenv'''
574 List.map (function (i,_,_) -> i) new_uninstantiatedmetas)
577 exception NotConvertible
579 (*CSC: Bug (or feature?). [with_what] is parsed in the context of the goal, *)
580 (*CSC: while [what] can have a richer context (because of binders) *)
581 (*CSC: So it is _NOT_ possible to use those binders in the [with_what] term. *)
582 (*CSC: Is that evident? Is that right? Or should it be changed? *)
583 let change_tac ~what ~with_what ~status:(proof, goal) =
584 let curi,metasenv,pbo,pty = proof in
585 let metano,context,ty = List.find (function (m,_,_) -> m=goal) metasenv in
586 (* are_convertible works only on well-typed terms *)
587 ignore (CicTypeChecker.type_of_aux' metasenv context with_what) ;
588 if CicReduction.are_convertible context what with_what then
591 ProofEngineReduction.replace ~equality:(==) ~what ~with_what
593 let ty' = replace ty in
597 Some (name,Cic.Def t) -> Some (name,Cic.Def (replace t))
598 | Some (name,Cic.Decl t) -> Some (name,Cic.Decl (replace t))
605 (n,_,_) when n = metano -> (metano,context',ty')
609 (curi,metasenv',pbo,pty), [metano]
612 raise (ProofEngineTypes.Fail "Not convertible")