2 Declaration of Cic.name * Cic.term
3 | Definition of Cic.name * Cic.term
6 type metasenv = (int * Cic.term) list;;
8 type named_context = binder_type list;;
10 type sequent = named_context * Cic.term;;
13 ref (None : (UriManager.uri * metasenv * Cic.term * Cic.term) option)
15 (*CSC: Quando facciamo Clear di una ipotesi, cosa succede? *)
16 (* Note: the sequent is redundant: it can be computed from the type of the *)
17 (* metavariable and its context in the proof. We keep it just for efficiency *)
18 (* because computing the context of a term may be quite expensive. *)
19 let goal = ref (None : (int * sequent) option);;
21 exception NotImplemented
23 let cic_context_of_named_context =
26 Declaration (_,t) -> Cic.Decl t
27 | Definition (_,t) -> Cic.Def t
31 let refine_meta meta term newmetasenv =
32 let (uri,metasenv,bo,ty) =
35 | Some (uri,metasenv,bo,ty) -> uri,metasenv,bo,ty
37 let metasenv' = newmetasenv @ (List.remove_assoc meta metasenv) in
43 | C.Meta meta' when meta=meta' -> term
46 | C.Implicit as t -> t
47 | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
48 | C.Prod (n,s,t) -> C.Prod (n, aux s, aux t)
49 | C.Lambda (n,s,t) -> C.Lambda (n, aux s, aux t)
50 | C.LetIn (n,s,t) -> C.LetIn (n, aux s, aux t)
51 | C.Appl l -> C.Appl (List.map aux l)
54 | C.MutInd _ as t -> t
55 | C.MutConstruct _ as t -> t
56 | C.MutCase (sp,cookingsno,i,outt,t,pl) ->
57 C.MutCase (sp,cookingsno,i,aux outt, aux t,
62 (fun (name,i,ty,bo) -> (name, i, aux ty, aux bo))
65 C.Fix (i, substitutedfl)
69 (fun (name,ty,bo) -> (name, aux ty, aux bo))
72 C.CoFix (i, substitutedfl)
74 let metasenv'' = List.map (function i,ty -> i,(aux ty)) metasenv' in
76 proof := Some (uri,metasenv'',bo',ty)
79 (* Returns the first meta whose number is above the number of the higher meta. *)
84 | Some (_,metasenv,_,_) -> metasenv
90 | None,(n,_)::tl -> aux (Some n,tl)
91 | Some m,(n,_)::tl -> if n > m then aux (Some n,tl) else aux (Some m,tl)
93 1 + aux (None,metasenv)
96 (* metas_in_term term *)
97 (* Returns the ordered list of the metas that occur in [term]. *)
98 (* Duplicates are removed. The implementation is not very efficient. *)
99 let metas_in_term term =
100 let module C = Cic in
108 | C.Cast (te,ty) -> (aux te) @ (aux ty)
109 | C.Prod (_,s,t) -> (aux s) @ (aux t)
110 | C.Lambda (_,s,t) -> (aux s) @ (aux t)
111 | C.LetIn (_,s,t) -> (aux s) @ (aux t)
112 | C.Appl l -> List.fold_left (fun i t -> i @ (aux t)) [] l
116 | C.MutConstruct _ -> []
117 | C.MutCase (sp,cookingsno,i,outt,t,pl) ->
118 (aux outt) @ (aux t) @
119 (List.fold_left (fun i t -> i @ (aux t)) [] pl)
121 List.fold_left (fun i (_,_,ty,bo) -> i @ (aux bo) @ (aux ty)) [] fl
123 List.fold_left (fun i (_,ty,bo) -> i @ (aux bo) @ (aux ty)) [] fl
125 let metas = aux term in
126 let rec elim_duplicates =
130 he::(elim_duplicates (List.filter (function el -> he <> el) tl))
132 elim_duplicates metas
135 (* perforate context term ty *)
136 (* replaces the term [term] in the proof with a new metavariable whose type *)
137 (* is [ty]. [context] must be the context of [term] in the whole proof. This *)
138 (* could be easily computed; so the only reasons to have it as an argument *)
139 (* are efficiency reasons. *)
140 let perforate context term ty =
141 let module C = Cic in
142 let newmeta = new_meta () in
145 | Some (uri,metasenv,bo,gty) ->
146 (* We push the new meta at the end of the list for pretty-printing *)
147 (* purposes: in this way metas are ordered. *)
148 let metasenv' = metasenv@[newmeta,ty] in
149 let bo' = ProofEngineReduction.replace term (C.Meta newmeta) bo in
150 (* It may be possible that some metavariables occurred only in *)
151 (* the term we are perforating and they now occurs no more. We *)
152 (* get rid of them, collecting the really useful metavariables *)
154 let newmetas = metas_in_term bo' in
156 List.filter (function (n,_) -> List.mem n newmetas) metasenv'
158 proof := Some (uri,metasenv'',bo',gty) ;
159 goal := Some (newmeta,(context,ty)) ;
163 (************************************************************)
164 (* Some easy tactics. *)
165 (************************************************************)
167 exception Fail of string;;
170 let module C = Cic in
171 let module R = CicReduction in
175 | Some (_,metasenv,_,_) -> metasenv
177 let (metano,context,ty) =
180 | Some (metano,(context,ty)) -> metano,context,ty
182 let newmeta = new_meta () in
183 let rec collect_context =
185 C.Cast (te,_) -> collect_context te
187 let (ctx,ty,bo) = collect_context t in
191 (*CSC: generatore di nomi? Chiedere il nome? *)
192 | C.Anonimous -> C.Name "fresh_name"
194 ((Declaration (n',s))::ctx,ty,C.Lambda(n',s,bo))
196 let (ctx,ty,bo) = collect_context t in
197 ((Definition (n,s))::ctx,ty,C.LetIn(n,s,bo))
198 | _ as t -> [], t, (C.Meta newmeta)
200 let revcontext',ty',bo' = collect_context ty in
201 let context'' = (List.rev revcontext') @ context in
202 refine_meta metano bo' [newmeta,ty'] ;
203 goal := Some (newmeta,(context'',ty'))
206 (* The term bo must be closed in the current context *)
208 let module T = CicTypeChecker in
209 let module R = CicReduction in
213 | Some (_,metasenv,_,_) -> metasenv
215 let (metano,context,ty) =
218 | Some (metano,(context,ty)) ->
219 assert (ty = List.assoc metano metasenv) ;
220 (* Invariant: context is the actual context of the meta in the proof *)
223 let context = cic_context_of_named_context context in
224 if R.are_convertible (T.type_of_aux' metasenv context bo) ty then
226 refine_meta metano bo [] ;
230 raise (Fail "The type of the provided term is not the one expected.")
233 let fix_andreas_meta mgu mgut =
234 let mgul = Array.to_list mgu in
235 let mgutl = Array.to_list mgut in
236 let applymetas_to_metas =
237 let newmeta = new_meta () in
238 (* WARNING: here we are using the invariant that above the most *)
239 (* recente new_meta() there are no used metas. *)
240 Array.init (List.length mgul) (function i -> newmeta + i) in
241 (* WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *)
242 (* Here we assume that either a META has been instantiated with *)
243 (* a close term or with itself. *)
244 let uninstantiatedmetas =
246 (fun bo ty newmetas ->
247 let module C = Cic in
250 let newmeta = applymetas_to_metas.(i) in
251 (*CSC: se ty contiene metas, queste hanno il numero errato!!! *)
252 let ty_with_newmetas =
253 (* Substitues (META n) with (META (applymetas_to_metas.(n))) *)
258 | C.Meta n -> C.Meta (applymetas_to_metas.(n))
260 | C.Implicit as t -> t
261 | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
262 | C.Prod (n,s,t) -> C.Prod (n, aux s, aux t)
263 | C.Lambda (n,s,t) -> C.Lambda (n, aux s, aux t)
264 | C.LetIn (n,s,t) -> C.LetIn (n, aux s, aux t)
265 | C.Appl l -> C.Appl (List.map aux l)
266 | C.Const _ as t -> t
267 | C.Abst _ -> assert false
269 | C.MutConstruct _ as t -> t
270 | C.MutCase (sp,cookingsno,i,outt,t,pl) ->
271 C.MutCase (sp,cookingsno,i,aux outt, aux t,
276 (fun (name,i,ty,bo) -> (name, i, aux ty, aux bo))
279 C.Fix (i, substitutedfl)
283 (fun (name,ty,bo) -> (name, aux ty, aux bo))
286 C.CoFix (i, substitutedfl)
290 (newmeta,ty_with_newmetas)::newmetas
297 Cic.Meta i -> Cic.Meta (applymetas_to_metas.(i))
301 mgul',uninstantiatedmetas
304 (* The term bo must be closed in the current context *)
306 let module T = CicTypeChecker in
307 let module R = CicReduction in
308 let module C = Cic in
312 | Some (_,metasenv,_,_) -> metasenv
314 let (metano,context,ty) =
317 | Some (metano,(context,ty)) ->
318 assert (ty = List.assoc metano metasenv) ;
319 (* Invariant: context is the actual context of the meta in the proof *)
322 let ciccontext = cic_context_of_named_context context in
323 let mgu,mgut = CicUnification.apply metasenv ciccontext term ty in
324 let mgul',uninstantiatedmetas = fix_andreas_meta mgu mgut in
326 if List.length mgul' = 0 then
329 Cic.Appl (term::mgul')
331 refine_meta metano bo' uninstantiatedmetas ;
332 match uninstantiatedmetas with
333 (n,ty)::tl -> goal := Some (n,(context,ty))
338 let eta_expand metasenv ciccontext t arg =
339 let module T = CicTypeChecker in
340 let module S = CicSubstitution in
341 let module C = Cic in
344 t' when t' = S.lift n arg -> C.Rel (1 + n)
345 | C.Rel m -> if m <= n then C.Rel m else C.Rel (m+1)
349 | C.Implicit as t -> t
350 | C.Cast (te,ty) -> C.Cast (aux n te, aux n ty)
351 | C.Prod (nn,s,t) -> C.Prod (nn, aux n s, aux (n+1) t)
352 | C.Lambda (nn,s,t) -> C.Lambda (nn, aux n s, aux (n+1) t)
353 | C.LetIn (nn,s,t) -> C.LetIn (nn, aux n s, aux (n+1) t)
354 | C.Appl l -> C.Appl (List.map (aux n) l)
355 | C.Const _ as t -> t
356 | C.Abst _ -> assert false
358 | C.MutConstruct _ as t -> t
359 | C.MutCase (sp,cookingsno,i,outt,t,pl) ->
360 C.MutCase (sp,cookingsno,i,aux n outt, aux n t,
363 let tylen = List.length fl in
366 (fun (name,i,ty,bo) -> (name, i, aux n ty, aux (n+tylen) bo))
369 C.Fix (i, substitutedfl)
371 let tylen = List.length fl in
374 (fun (name,ty,bo) -> (name, aux n ty, aux (n+tylen) bo))
377 C.CoFix (i, substitutedfl)
380 T.type_of_aux' metasenv ciccontext arg
382 (C.Appl [C.Lambda ((C.Name "dummy"),argty,aux 0 t) ; arg])
385 exception NotAnInductiveTypeToEliminate;;
386 exception NotTheRightEliminatorShape;;
387 exception NoHypothesesFound;;
390 let module T = CicTypeChecker in
391 let module U = UriManager in
392 let module R = CicReduction in
393 let module C = Cic in
397 | Some (curi,metasenv,_,_) -> curi,metasenv
399 let (metano,context,ty) =
402 | Some (metano,(context,ty)) ->
403 assert (ty = List.assoc metano metasenv) ;
404 (* Invariant: context is the actual context of the meta in the proof *)
407 let ciccontext = cic_context_of_named_context context in
408 let termty = T.type_of_aux' metasenv ciccontext term in
409 let uri,cookingno,typeno,args =
411 C.MutInd (uri,cookingno,typeno) -> (uri,cookingno,typeno,[])
412 | C.Appl ((C.MutInd (uri,cookingno,typeno))::args) ->
413 (uri,cookingno,typeno,args)
414 | _ -> raise NotAnInductiveTypeToEliminate
417 let buri = U.buri_of_uri uri in
419 match CicEnvironment.get_cooked_obj uri cookingno with
420 C.InductiveDefinition (tys,_,_) ->
421 let (name,_,_,_) = List.nth tys typeno in
426 match T.type_of_aux' metasenv ciccontext ty with
427 C.Sort C.Prop -> "_ind"
428 | C.Sort C.Set -> "_rec"
429 | C.Sort C.Type -> "_rect"
432 U.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con")
434 let eliminator_cookingno =
435 UriManager.relative_depth curi eliminator_uri 0
437 let eliminator_ref = C.Const (eliminator_uri,eliminator_cookingno) in
439 T.type_of_aux' [] [] eliminator_ref
442 let earity = CicUnification.get_arity ety in
443 let mgu = Array.init earity (fun i -> (C.Meta i)) in
444 let mgut = Array.make earity C.Implicit in
445 (* Here we assume that we have only one inductive hypothesis to *)
446 (* eliminate and that it is the last hypothesis of the theorem. *)
447 (* A better approach would be fingering the hypotheses in some *)
449 let hypothesis_to_eliminate,econclusion =
451 (* traverses the backbone [t] looking for the last hypothesis *)
452 (* and substituting Pi-abstractions with META declarations. *)
453 (* [h] is the last hypothesis met up to now. [n] is the next *)
459 aux (n+1) (Some s) (CicSubstitution.subst (C.Meta n) t)
460 | C.Cast (te,_) -> aux n h te
462 None -> raise NoHypothesesFound
467 prerr_endline ("HTOELIM: " ^ CicPp.ppterm hypothesis_to_eliminate) ;
468 prerr_endline ("ECONCLUSION: " ^ CicPp.ppterm econclusion) ;
470 ignore (CicUnification.fo_unif_mgu 0 hypothesis_to_eliminate termty mgu) ;
471 ignore (CicUnification.fo_unif_mgu 0 term (C.Meta (earity - 1)) mgu) ;
472 let mgu = CicUnification.unwind mgu in
473 prerr_endline "Dopo l'unwind dell'mgu"; flush stderr ;
474 let mark = Array.make earity 1 in
476 CicUnification.unwind_meta mgu mark econclusion
478 prerr_endline ("ECONCLUSION DOPO UNWIND: " ^ CicPp.ppterm ueconclusion) ;
480 (* The conclusion of our elimination principle is *)
481 (* (?i farg1 ... fargn) *)
482 (* The conclusion of our goal is ty. So, we can *)
483 (* eta-expand ty w.r.t. farg1 .... fargn to get *)
484 (* a new ty equal to (P farg1 ... fargn). Now *)
485 (* ?i can be instantiated with P and we are ready *)
486 (* to refine the term. *)
488 match ueconclusion with
489 C.Appl ((C.Meta emeta)::fargs) -> emeta,fargs
490 | _ -> raise NotTheRightEliminatorShape
492 let eta_expanded_ty =
493 (*CSC: metasenv e ?????????????*)
494 List.fold_left (eta_expand metasenv ciccontext) ty fargs
497 prerr_endline ("ETAEXPANDEDTY:" ^ CicPp.ppterm eta_expanded_ty) ; flush stdout ;
498 ignore (CicUnification.fo_unif_mgu 0 ueconclusion eta_expanded_ty mgu) ;
499 prerr_endline "Dopo la seconda unificazione" ; flush stdout ;
500 let mgu = CicUnification.unwind mgu in
501 print_endline "unwind"; flush stdout;
502 (* When unwinding the META that corresponds to the elimination *)
503 (* predicate (which is emeta), we must also perform one-step *)
504 (* beta-reduction. *)
506 let mark = Array.make (Array.length mgu) 1 in
508 (CicUnification.unwind_meta_reducing mgu mark (Some emeta))
511 print_endline "unwind_array"; flush stdout;
512 let mgu' = Array.copy mgu in
513 let mgut' = CicUnification.list_of_array mgut in
514 print_endline "list"; flush stdout;
517 prerr_endline ("META " ^ string_of_int i ^ ": " ^ CicPp.ppterm mgu'.(i) ^
518 " == " ^ CicPp.ppterm ty) ; flush stderr ;
520 CicTypeChecker.type_of_aux' mgut' ciccontext mgu'.(i)
522 ignore (CicUnification.fo_unif_mgu 0 ty ty' mgu)
524 let mgu = CicUnification.unwind mgu in
525 let mgut = CicUnification.unwind_array mgu mgut in
526 prerr_endline "Dopo le unwind dell'mgut" ; flush stdout ;
527 let mgul',uninstantiatedmetas = fix_andreas_meta mgu mgut in
528 prerr_endline "Dopo il fissaggio" ; flush stdout ;
529 let bo' = Cic.Appl (eliminator_ref::mgul') in
530 prerr_endline ("BODY': " ^ CicPp.ppterm bo') ; flush stdout ;
531 refine_meta metano bo' uninstantiatedmetas ;
532 prerr_endline "dopo refine meta" ; flush stdout ;
533 match uninstantiatedmetas with
534 (n,ty)::tl -> goal := Some (n,(context,ty))
538 let elim_intros term =
543 let reduction_tactic reduction_function term =
544 let curi,metasenv,pbo,pty =
547 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
549 let (metano,context,ty) =
552 | Some (metano,(context,ty)) -> metano,context,ty
554 let term' = reduction_function term in
555 (* We don't know if [term] is a subterm of [ty] or a subterm of *)
556 (* the type of one metavariable. So we replace it everywhere. *)
557 (*CSC: ma si potrebbe ovviare al problema. Ma non credo *)
558 (*CSC: che si guadagni nulla in fatto di efficienza. *)
559 let replace = ProofEngineReduction.replace ~what:term ~with_what:term' in
560 let ty' = replace ty in
564 Definition (n,t) -> Definition (n,replace t)
565 | Declaration (n,t) -> Declaration (n,replace t)
571 (n,_) when n = metano -> (metano,ty')
575 proof := Some (curi,metasenv',pbo,pty) ;
576 goal := Some (metano,(context',ty'))
579 let reduction_tactic_in_scratch reduction_function ty term =
583 | Some (_,metasenv,_,_) -> metasenv
588 | Some (_,(context,_)) -> context
590 let term' = reduction_function term in
591 ProofEngineReduction.replace ~what:term ~with_what:term' ~where:ty
594 let whd = reduction_tactic CicReduction.whd;;
595 let reduce = reduction_tactic ProofEngineReduction.reduce;;
596 let simpl = reduction_tactic ProofEngineReduction.simpl;;
598 let whd_in_scratch = reduction_tactic_in_scratch CicReduction.whd;;
599 let reduce_in_scratch =
600 reduction_tactic_in_scratch ProofEngineReduction.reduce;;
601 let simpl_in_scratch =
602 reduction_tactic_in_scratch ProofEngineReduction.simpl;;
604 (* It is just the opposite of whd. The code should probably be merged. *)
606 let curi,metasenv,pbo,pty =
609 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
611 let (metano,context,ty) =
614 | Some (metano,(context,ty)) -> metano,context,ty
616 let term' = CicReduction.whd term in
617 (* We don't know if [term] is a subterm of [ty] or a subterm of *)
618 (* the type of one metavariable. So we replace it everywhere. *)
619 (*CSC: ma si potrebbe ovviare al problema. Ma non credo *)
620 (*CSC: che si guadagni nulla in fatto di efficienza. *)
621 let replace = ProofEngineReduction.replace ~what:term' ~with_what:term in
622 let ty' = replace ty in
626 Declaration (n,t) -> Declaration (n,replace t)
627 | Definition (n,t) -> Definition (n,replace t)
633 (n,_) when n = metano -> (metano,ty')
637 proof := Some (curi,metasenv',pbo,pty) ;
638 goal := Some (metano,(context',ty'))
642 let module C = Cic in
643 let curi,metasenv,pbo,pty =
646 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
648 let (metano,context,ty) =
651 | Some (metano,(context,ty)) -> metano,context,ty
653 let newmeta1 = new_meta () in
654 let newmeta2 = newmeta1 + 1 in
655 let newmeta1ty = CicSubstitution.lift 1 ty in
658 [C.Lambda (C.Name "dummy_for_cut",term,C.Meta newmeta1) ;
661 prerr_endline ("BO': " ^ CicPp.ppterm bo') ; flush stderr ;
662 refine_meta metano bo' [newmeta2,term; newmeta1,newmeta1ty];
665 (newmeta1,((Declaration (C.Name "dummy_for_cut", term))::context,
670 let module C = Cic in
671 let curi,metasenv,pbo,pty =
674 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
676 let (metano,context,ty) =
679 | Some (metano,(context,ty)) -> metano,context,ty
681 let ciccontext = cic_context_of_named_context context in
682 let _ = CicTypeChecker.type_of_aux' metasenv ciccontext term in
683 let newmeta = new_meta () in
684 let newmetaty = CicSubstitution.lift 1 ty in
685 let bo' = C.LetIn (C.Name "dummy_for_letin",term,C.Meta newmeta) in
686 refine_meta metano bo' [newmeta,newmetaty];
690 ((Definition (C.Name "dummy_for_letin", term))::context, newmetaty))
693 exception NotConvertible;;
695 (*CSC: Bug (or feature?). [input] is parsed in the context of the goal, *)
696 (*CSC: while [goal_input] can have a richer context (because of binders) *)
697 (*CSC: So it is _NOT_ possible to use those binders in the [input] term. *)
698 (*CSC: Is that evident? Is that right? Or should it be changed? *)
699 let change ~goal_input ~input =
700 let curi,metasenv,pbo,pty =
703 | Some (curi,metasenv,bo,ty) -> curi,metasenv,bo,ty
705 let (metano,context,ty) =
708 | Some (metano,(context,ty)) -> metano,context,ty
710 let ciccontext = cic_context_of_named_context context in
711 (* are_convertible works only on well-typed terms *)
712 ignore (CicTypeChecker.type_of_aux' metasenv ciccontext input) ;
713 if CicReduction.are_convertible goal_input input then
715 let ty' = ProofEngineReduction.replace goal_input input ty in
719 (n,_) when n = metano -> (metano,ty')
723 proof := Some (curi,metasenv',pbo,pty) ;
724 goal := Some (metano,(context,ty'))