1 (* Copyright (C) 2000, 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/.
28 (* TODO factorize functions to frequent errors (e.g. "Unknwon mutual inductive
33 exception AssertFailure of string Lazy.t;;
34 exception TypeCheckerFailure of string Lazy.t;;
38 let rec debug_aux t i =
40 let module U = UriManager in
41 CicPp.ppobj (C.Variable ("DEBUG", None, t, [], [])) ^ "\n" ^ i
44 raise (TypeCheckerFailure (lazy (List.fold_right debug_aux (t::context) "")))
47 let debug_print = fun _ -> ();;
52 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
54 raise (TypeCheckerFailure (lazy "Parameters number < left parameters number"))
57 let debrujin_constructor ?(cb=fun _ _ -> ()) ?(check_exp_named_subst=true) uri number_of_types context =
62 C.Rel n as t when n <= k -> t
64 raise (TypeCheckerFailure (lazy "unbound variable found in constructor type"))
65 | C.Var (uri,exp_named_subst) ->
66 let exp_named_subst' =
67 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
69 C.Var (uri,exp_named_subst')
71 let l' = List.map (function None -> None | Some t -> Some (aux k t)) l in
74 | C.Implicit _ as t -> t
75 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
76 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k+1) t)
77 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k+1) t)
78 | C.LetIn (n,s,ty,t) -> C.LetIn (n, aux k s, aux k ty, aux (k+1) t)
79 | C.Appl l -> C.Appl (List.map (aux k) l)
80 | C.Const (uri,exp_named_subst) ->
81 let exp_named_subst' =
82 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
84 C.Const (uri,exp_named_subst')
85 | C.MutInd (uri',tyno,exp_named_subst) when UriManager.eq uri uri' ->
86 if check_exp_named_subst && exp_named_subst != [] then
87 raise (TypeCheckerFailure
88 (lazy ("non-empty explicit named substitution is applied to "^
89 "a mutual inductive type which is being defined"))) ;
90 C.Rel (k + number_of_types - tyno) ;
91 | C.MutInd (uri',tyno,exp_named_subst) ->
92 let exp_named_subst' =
93 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
95 C.MutInd (uri',tyno,exp_named_subst')
96 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
97 let exp_named_subst' =
98 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
100 C.MutConstruct (uri,tyno,consno,exp_named_subst')
101 | C.MutCase (sp,i,outty,t,pl) ->
102 C.MutCase (sp, i, aux k outty, aux k t,
105 let len = List.length fl in
108 (fun (name, i, ty, bo) -> (name, i, aux k ty, aux (k+len) bo))
113 let len = List.length fl in
116 (fun (name, ty, bo) -> (name, aux k ty, aux (k+len) bo))
119 C.CoFix (i, liftedfl)
124 aux (List.length context)
127 exception CicEnvironmentError;;
129 let rec type_of_constant ~logger uri ugraph =
130 let module C = Cic in
131 let module R = CicReduction in
132 let module U = UriManager in
134 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
135 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
136 | CicEnvironment.UncheckedObj uobj ->
137 logger#log (`Start_type_checking uri) ;
138 (* let's typecheck the uncooked obj *)
140 (****************************************************************
141 TASSI: FIXME qui e' inutile ricordarselo,
142 tanto poi lo richiediamo alla cache che da quello su disco
143 *****************************************************************)
147 C.Constant (_,Some te,ty,_,_) ->
148 let _,ugraph = type_of ~logger ty ugraph in
149 let type_of_te,ugraph' = type_of ~logger te ugraph in
150 let b',ugraph'' = (R.are_convertible [] type_of_te ty ugraph') in
152 raise (TypeCheckerFailure (lazy (sprintf
153 "the constant %s is not well typed because the type %s of the body is not convertible to the declared type %s"
154 (U.string_of_uri uri) (CicPp.ppterm type_of_te)
158 | C.Constant (_,None,ty,_,_) ->
159 (* only to check that ty is well-typed *)
160 let _,ugraph' = type_of ~logger ty ugraph in
162 | C.CurrentProof (_,conjs,te,ty,_,_) ->
165 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
167 type_of_aux' ~logger metasenv context ty ugraph
169 (metasenv @ [conj],ugraph')
172 let _,ugraph2 = type_of_aux' ~logger conjs [] ty ugraph1 in
173 let type_of_te,ugraph3 =
174 type_of_aux' ~logger conjs [] te ugraph2
176 let b,ugraph4 = (R.are_convertible [] type_of_te ty ugraph3) in
178 raise (TypeCheckerFailure (lazy (sprintf
179 "the current proof %s is not well typed because the type %s of the body is not convertible to the declared type %s"
180 (U.string_of_uri uri) (CicPp.ppterm type_of_te)
186 (TypeCheckerFailure (lazy ("Unknown constant:" ^ U.string_of_uri uri))))
189 CicEnvironment.set_type_checking_info uri;
190 logger#log (`Type_checking_completed uri) ;
191 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
192 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
193 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
194 with Invalid_argument s ->
195 (*debug_print (lazy s);*)
198 match cobj,ugraph with
199 (C.Constant (_,_,ty,_,_)),g -> ty,g
200 | (C.CurrentProof (_,_,_,ty,_,_)),g -> ty,g
202 raise (TypeCheckerFailure (lazy ("Unknown constant:" ^ U.string_of_uri uri)))
204 and type_of_variable ~logger uri ugraph =
205 let module C = Cic in
206 let module R = CicReduction in
207 let module U = UriManager in
208 (* 0 because a variable is never cooked => no partial cooking at one level *)
209 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
210 CicEnvironment.CheckedObj ((C.Variable (_,_,ty,_,_)),ugraph') -> ty,ugraph'
211 | CicEnvironment.UncheckedObj (C.Variable (_,bo,ty,_,_)) ->
212 logger#log (`Start_type_checking uri) ;
213 (* only to check that ty is well-typed *)
214 let _,ugraph1 = type_of ~logger ty ugraph in
219 let ty_bo,ugraph' = type_of ~logger bo ugraph1 in
220 let b,ugraph'' = (R.are_convertible [] ty_bo ty ugraph') in
222 raise (TypeCheckerFailure
223 (lazy ("Unknown variable:" ^ U.string_of_uri uri)))
228 CicEnvironment.set_type_checking_info uri ;
229 logger#log (`Type_checking_completed uri) ;
230 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
231 CicEnvironment.CheckedObj ((C.Variable (_,_,ty,_,_)),ugraph') ->
233 | CicEnvironment.CheckedObj _
234 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
235 with Invalid_argument s ->
236 (*debug_print (lazy s);*)
239 raise (TypeCheckerFailure (lazy ("Unknown variable:" ^ U.string_of_uri uri)))
241 and does_not_occur ?(subst=[]) context n nn te =
242 let module C = Cic in
244 C.Rel m when m > n && m <= nn -> false
247 (match List.nth context (m-1) with
248 Some (_,C.Def (bo,_)) ->
249 does_not_occur ~subst context n nn (CicSubstitution.lift m bo)
252 Failure _ -> assert false)
254 | C.Implicit _ -> true
260 | Some x -> i && does_not_occur ~subst context n nn x) l true &&
262 let (canonical_context,term,ty) = CicUtil.lookup_subst mno subst in
263 does_not_occur ~subst context n nn (CicSubstitution.subst_meta l term)
265 CicUtil.Subst_not_found _ -> true)
267 does_not_occur ~subst context n nn te && does_not_occur ~subst context n nn ty
268 | C.Prod (name,so,dest) ->
269 does_not_occur ~subst context n nn so &&
270 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1)
272 | C.Lambda (name,so,dest) ->
273 does_not_occur ~subst context n nn so &&
274 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1) (nn + 1)
276 | C.LetIn (name,so,ty,dest) ->
277 does_not_occur ~subst context n nn so &&
278 does_not_occur ~subst context n nn ty &&
279 does_not_occur ~subst ((Some (name,(C.Def (so,ty))))::context)
280 (n + 1) (nn + 1) dest
282 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
283 | C.Var (_,exp_named_subst)
284 | C.Const (_,exp_named_subst)
285 | C.MutInd (_,_,exp_named_subst)
286 | C.MutConstruct (_,_,_,exp_named_subst) ->
287 List.fold_right (fun (_,x) i -> i && does_not_occur ~subst context n nn x)
289 | C.MutCase (_,_,out,te,pl) ->
290 does_not_occur ~subst context n nn out && does_not_occur ~subst context n nn te &&
291 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) pl true
293 let len = List.length fl in
294 let n_plus_len = n + len in
295 let nn_plus_len = nn + len in
298 (fun (types,len) (n,_,ty,_) ->
299 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
304 (fun (_,_,ty,bo) i ->
305 i && does_not_occur ~subst context n nn ty &&
306 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
309 let len = List.length fl in
310 let n_plus_len = n + len in
311 let nn_plus_len = nn + len in
314 (fun (types,len) (n,ty,_) ->
315 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
321 i && does_not_occur ~subst context n nn ty &&
322 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
325 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
326 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
327 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
328 (*CSC strictly_positive *)
329 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
330 and weakly_positive context n nn uri te =
331 let module C = Cic in
332 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
334 C.MutInd (HelmLibraryObjects.Datatypes.nat_URI,0,[])
336 (*CSC: mettere in cicSubstitution *)
337 let rec subst_inductive_type_with_dummy_mutind =
339 C.MutInd (uri',0,_) when UriManager.eq uri' uri ->
341 | C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri ->
343 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
344 | C.Prod (name,so,ta) ->
345 C.Prod (name, subst_inductive_type_with_dummy_mutind so,
346 subst_inductive_type_with_dummy_mutind ta)
347 | C.Lambda (name,so,ta) ->
348 C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
349 subst_inductive_type_with_dummy_mutind ta)
350 | C.LetIn (name,so,ty,ta) ->
351 C.LetIn (name, subst_inductive_type_with_dummy_mutind so,
352 subst_inductive_type_with_dummy_mutind ty,
353 subst_inductive_type_with_dummy_mutind ta)
355 C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
356 | C.MutCase (uri,i,outtype,term,pl) ->
358 subst_inductive_type_with_dummy_mutind outtype,
359 subst_inductive_type_with_dummy_mutind term,
360 List.map subst_inductive_type_with_dummy_mutind pl)
362 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
363 subst_inductive_type_with_dummy_mutind ty,
364 subst_inductive_type_with_dummy_mutind bo)) fl)
366 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
367 subst_inductive_type_with_dummy_mutind ty,
368 subst_inductive_type_with_dummy_mutind bo)) fl)
369 | C.Const (uri,exp_named_subst) ->
370 let exp_named_subst' =
372 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
375 C.Const (uri,exp_named_subst')
376 | C.Var (uri,exp_named_subst) ->
377 let exp_named_subst' =
379 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
382 C.Var (uri,exp_named_subst')
383 | C.MutInd (uri,typeno,exp_named_subst) ->
384 let exp_named_subst' =
386 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
389 C.MutInd (uri,typeno,exp_named_subst')
390 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
391 let exp_named_subst' =
393 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
396 C.MutConstruct (uri,typeno,consno,exp_named_subst')
399 match CicReduction.whd context te with
401 C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri -> true
403 C.Appl ((C.MutInd (uri',_,_))::tl) when UriManager.eq uri' uri -> true
404 | C.MutInd (uri',0,_) when UriManager.eq uri' uri -> true
405 | C.Prod (name,source,dest) when
406 does_not_occur ((Some (name,(C.Decl source)))::context) 0 1 dest ->
407 (* dummy abstraction, so we behave as in the anonimous case *)
408 strictly_positive context n nn
409 (subst_inductive_type_with_dummy_mutind source) &&
410 weakly_positive ((Some (name,(C.Decl source)))::context)
411 (n + 1) (nn + 1) uri dest
412 | C.Prod (name,source,dest) ->
413 does_not_occur context n nn
414 (subst_inductive_type_with_dummy_mutind source)&&
415 weakly_positive ((Some (name,(C.Decl source)))::context)
416 (n + 1) (nn + 1) uri dest
418 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
420 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
421 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
422 and instantiate_parameters params c =
423 let module C = Cic in
424 match (c,params) with
426 | (C.Prod (_,_,ta), he::tl) ->
427 instantiate_parameters tl
428 (CicSubstitution.subst he ta)
429 | (C.Cast (te,_), _) -> instantiate_parameters params te
430 | (t,l) -> raise (AssertFailure (lazy "1"))
432 and strictly_positive context n nn te =
433 let module C = Cic in
434 let module U = UriManager in
435 match CicReduction.whd context te with
436 | t when does_not_occur context n nn t -> true
439 (*CSC: bisogna controllare ty????*)
440 strictly_positive context n nn te
441 | C.Prod (name,so,ta) ->
442 does_not_occur context n nn so &&
443 strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
444 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
445 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
446 | C.Appl ((C.MutInd (uri,i,exp_named_subst))::_)
447 | (C.MutInd (uri,i,exp_named_subst)) as t ->
448 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
449 let (ok,paramsno,ity,cl,name) =
450 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
452 C.InductiveDefinition (tl,_,paramsno,_) ->
453 let (name,_,ity,cl) = List.nth tl i in
454 (List.length tl = 1, paramsno, ity, cl, name)
455 (* (true, paramsno, ity, cl, name) *)
459 (lazy ("Unknown inductive type:" ^ U.string_of_uri uri)))
461 let (params,arguments) = split tl paramsno in
462 let lifted_params = List.map (CicSubstitution.lift 1) params in
466 instantiate_parameters lifted_params
467 (CicSubstitution.subst_vars exp_named_subst te)
472 (fun x i -> i && does_not_occur context n nn x)
478 ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri
483 (* the inductive type indexes are s.t. n < x <= nn *)
484 and are_all_occurrences_positive context uri indparamsno i n nn te =
485 let module C = Cic in
486 match CicReduction.whd context te with
487 C.Appl ((C.Rel m)::tl) when m = i ->
488 (*CSC: riscrivere fermandosi a 0 *)
489 (* let's check if the inductive type is applied at least to *)
490 (* indparamsno parameters *)
496 match CicReduction.whd context x with
497 C.Rel m when m = n - (indparamsno - k) -> k - 1
499 raise (TypeCheckerFailure
501 ("Non-positive occurence in mutual inductive definition(s) [1]" ^
502 UriManager.string_of_uri uri)))
506 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
508 raise (TypeCheckerFailure
509 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
510 UriManager.string_of_uri uri)))
511 | C.Rel m when m = i ->
512 if indparamsno = 0 then
515 raise (TypeCheckerFailure
516 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
517 UriManager.string_of_uri uri)))
518 | C.Prod (name,source,dest) when
519 does_not_occur ((Some (name,(C.Decl source)))::context) 0 1 dest ->
520 (* dummy abstraction, so we behave as in the anonimous case *)
521 strictly_positive context n nn source &&
522 are_all_occurrences_positive
523 ((Some (name,(C.Decl source)))::context) uri indparamsno
524 (i+1) (n + 1) (nn + 1) dest
525 | C.Prod (name,source,dest) ->
526 does_not_occur context n nn source &&
527 are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
528 uri indparamsno (i+1) (n + 1) (nn + 1) dest
531 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
532 (UriManager.string_of_uri uri))))
534 (* Main function to checks the correctness of a mutual *)
535 (* inductive block definition. This is the function *)
536 (* exported to the proof-engine. *)
537 and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
538 let module U = UriManager in
539 (* let's check if the arity of the inductive types are well *)
541 let ugrap1 = List.fold_left
542 (fun ugraph (_,_,x,_) -> let _,ugraph' =
543 type_of ~logger x ugraph in ugraph')
546 (* let's check if the types of the inductive constructors *)
547 (* are well formed. *)
548 (* In order not to use type_of_aux we put the types of the *)
549 (* mutual inductive types at the head of the types of the *)
550 (* constructors using Prods *)
551 let len = List.length itl in
553 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
556 (fun (_,_,_,cl) (i,ugraph) ->
559 (fun ugraph (name,te) ->
560 let debrujinedte = debrujin_constructor uri len [] te in
563 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
566 let _,ugraph' = type_of ~logger augmented_term ugraph in
567 (* let's check also the positivity conditions *)
570 (are_all_occurrences_positive tys uri indparamsno i 0 len
574 prerr_endline (UriManager.string_of_uri uri);
575 prerr_endline (string_of_int (List.length tys));
578 (lazy ("Non positive occurence in " ^ U.string_of_uri uri))) end
587 (* Main function to checks the correctness of a mutual *)
588 (* inductive block definition. *)
589 and check_mutual_inductive_defs uri obj ugraph =
591 Cic.InductiveDefinition (itl, params, indparamsno, _) ->
592 typecheck_mutual_inductive_defs uri (itl,params,indparamsno) ugraph
594 raise (TypeCheckerFailure (
595 lazy ("Unknown mutual inductive definition:" ^
596 UriManager.string_of_uri uri)))
598 and type_of_mutual_inductive_defs ~logger uri i ugraph =
599 let module C = Cic in
600 let module R = CicReduction in
601 let module U = UriManager in
603 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
604 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
605 | CicEnvironment.UncheckedObj uobj ->
606 logger#log (`Start_type_checking uri) ;
608 check_mutual_inductive_defs ~logger uri uobj ugraph
610 (* TASSI: FIXME: check ugraph1 == ugraph ritornato da env *)
612 CicEnvironment.set_type_checking_info uri ;
613 logger#log (`Type_checking_completed uri) ;
614 (match CicEnvironment.is_type_checked ~trust:false ugraph uri with
615 CicEnvironment.CheckedObj (cobj,ugraph') -> (cobj,ugraph')
616 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
619 Invalid_argument s ->
620 (*debug_print (lazy s);*)
624 C.InductiveDefinition (dl,_,_,_) ->
625 let (_,_,arity,_) = List.nth dl i in
628 raise (TypeCheckerFailure
629 (lazy ("Unknown mutual inductive definition:" ^ U.string_of_uri uri)))
631 and type_of_mutual_inductive_constr ~logger uri i j ugraph =
632 let module C = Cic in
633 let module R = CicReduction in
634 let module U = UriManager in
636 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
637 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
638 | CicEnvironment.UncheckedObj uobj ->
639 logger#log (`Start_type_checking uri) ;
641 check_mutual_inductive_defs ~logger uri uobj ugraph
643 (* check ugraph1 validity ??? == ugraph' *)
645 CicEnvironment.set_type_checking_info uri ;
646 logger#log (`Type_checking_completed uri) ;
648 CicEnvironment.is_type_checked ~trust:false ugraph uri
650 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
651 | CicEnvironment.UncheckedObj _ ->
652 raise CicEnvironmentError)
654 Invalid_argument s ->
655 (*debug_print (lazy s);*)
659 C.InductiveDefinition (dl,_,_,_) ->
660 let (_,_,_,cl) = List.nth dl i in
661 let (_,ty) = List.nth cl (j-1) in
664 raise (TypeCheckerFailure
665 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
667 and recursive_args context n nn te =
668 let module C = Cic in
669 match CicReduction.whd context te with
676 | C.Cast _ (*CSC ??? *) ->
677 raise (AssertFailure (lazy "3")) (* due to type-checking *)
678 | C.Prod (name,so,de) ->
679 (not (does_not_occur context n nn so)) ::
680 (recursive_args ((Some (name,(C.Decl so)))::context) (n+1) (nn + 1) de)
683 raise (AssertFailure (lazy "4")) (* due to type-checking *)
685 | C.Const _ -> raise (AssertFailure (lazy "5"))
689 | C.CoFix _ -> raise (AssertFailure (lazy "6")) (* due to type-checking *)
691 and get_new_safes ~subst context p rl safes n nn x =
692 let module C = Cic in
693 let module U = UriManager in
694 let module R = CicReduction in
695 match R.whd ~subst context p, rl with
696 | C.Lambda (name,so,ta), b::tl ->
697 let safes = List.map (fun x -> x + 1) safes in
698 let safes = if b then 1::safes else safes in
699 get_new_safes ~subst ((Some (name,(C.Decl so)))::context)
700 ta tl safes (n+1) (nn+1) (x+1)
701 | C.MutConstruct _ as e, _
703 | e, [] -> (e,safes,n,nn,x,context)
707 (Printf.sprintf "Get New Safes: p=%s" (CicPp.ppterm p))))
709 and split_prods ~subst context n te =
710 let module C = Cic in
711 let module R = CicReduction in
712 match (n, R.whd ~subst context te) with
714 | (n, C.Prod (name,so,ta)) when n > 0 ->
715 split_prods ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
716 | (_, _) -> raise (AssertFailure (lazy "8"))
718 and eat_lambdas ~subst context n te =
719 let module C = Cic in
720 let module R = CicReduction in
721 match (n, R.whd ~subst context te) with
722 (0, _) -> (te, 0, context)
723 | (n, C.Lambda (name,so,ta)) when n > 0 ->
724 let (te, k, context') =
725 eat_lambdas ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
727 (te, k + 1, context')
729 raise (AssertFailure (lazy (sprintf "9 (%d, %s)" n (CicPp.ppterm te))))
731 and specialize_inductive_type ~logger ~subst ~metasenv context t =
732 let ty,_= type_of_aux' ~logger ~subst metasenv context t CicUniv.oblivion_ugraph in
733 match CicReduction.whd ~subst context ty with
734 | Cic.MutInd (uri,_,exp)
735 | Cic.Appl (Cic.MutInd (uri,_,exp) :: _) as ty ->
736 let args = match ty with Cic.Appl (_::tl) -> tl | _ -> [] in
737 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
739 | Cic.InductiveDefinition (tl,_,paramsno,_) ->
740 let left_args,_ = HExtlib.split_nth paramsno args in
741 List.map (fun (name, isind, arity, cl) ->
742 let arity = CicSubstitution.subst_vars exp arity in
743 let arity = instantiate_parameters left_args arity in
747 let ty = CicSubstitution.subst_vars exp ty in
748 id, instantiate_parameters left_args ty)
751 name, isind, arity, cl)
756 and check_is_really_smaller_arg
757 ~logger ~metasenv ~subst rec_uri rec_uri_len context n nn kl x safes te
759 let module C = Cic in
760 let module U = UriManager in
761 (*CSC: we could perform beta-iota(-zeta?) immediately, and
762 delta only on-demand when it fails without *)
763 match CicReduction.whd ~subst context te with
764 C.Rel m when List.mem m safes -> true
770 | C.Lambda (name,ty,ta) ->
771 check_is_really_smaller_arg rec_uri rec_uri_len
772 ~logger ~metasenv ~subst (Some (name,Cic.Decl ty)::context)
773 (n+1) (nn+1) kl (x+1) (List.map (fun n -> n+1) safes) ta
774 | C.MutCase (uri,i,outtype,term,pl) ->
776 | C.Rel m | C.Appl ((C.Rel m)::_) when List.mem m safes || m = x ->
778 specialize_inductive_type ~logger ~subst ~metasenv context term
781 List.map (fun (name,_,ty,_) -> Some (Cic.Name name, Cic.Decl ty)) tys
783 let _,isinductive,_,cl = List.nth tys i in
784 if not isinductive then
786 (check_is_really_smaller_arg rec_uri rec_uri_len
787 ~logger ~metasenv ~subst context n nn kl x safes)
794 debrujin_constructor ~check_exp_named_subst:false
795 rec_uri rec_uri_len context c in
796 let len_ctx = List.length context in
797 recursive_args (context@tys_ctx) len_ctx (len_ctx+rec_uri_len) c
799 let (e, safes',n',nn',x',context') =
800 get_new_safes ~subst context p rec_params safes n nn x
802 check_is_really_smaller_arg rec_uri rec_uri_len
803 ~logger ~metasenv ~subst context' n' nn' kl x' safes' e
807 (check_is_really_smaller_arg
808 rec_uri rec_uri_len ~logger ~metasenv ~subst
809 context n nn kl x safes) pl
812 let len = List.length fl in
813 let n_plus_len = n + len
814 and nn_plus_len = nn + len
815 and x_plus_len = x + len
818 (fun (types,len) (n,_,ty,_) ->
819 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
822 and safes' = List.map (fun x -> x + len) safes in
825 check_is_really_smaller_arg
826 rec_uri rec_uri_len ~logger ~metasenv ~subst
827 (tys@context) n_plus_len nn_plus_len kl
831 raise (AssertFailure (lazy ("An inhabitant of an inductive type in normal form cannot have this shape: " ^ CicPp.ppterm t)))
833 and guarded_by_destructors
834 ~logger ~metasenv ~subst rec_uri rec_uri_len context n nn kl x safes t
836 let module C = Cic in
837 let module U = UriManager in
838 match CicReduction.whd ~subst context t with
839 C.Rel m when m > n && m <= nn -> false
841 (match List.nth context (m-1) with
842 Some (_,C.Decl _) -> true
843 | Some (_,C.Def (bo,_)) ->
844 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes
845 (CicSubstitution.lift m bo)
846 | None -> raise (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
850 | C.Implicit _ -> true
852 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes te &&
853 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes ty
854 | C.Prod (name,so,ta) ->
855 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes so &&
856 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst ((Some (name,(C.Decl so)))::context)
857 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
858 | C.Lambda (name,so,ta) ->
859 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes so &&
860 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst ((Some (name,(C.Decl so)))::context)
861 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
862 | C.LetIn (name,so,ty,ta) ->
863 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes so &&
864 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes ty &&
865 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst ((Some (name,(C.Def (so,ty))))::context)
866 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
867 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
868 let k = List.nth kl (m - n - 1) in
869 if not (List.length tl > k) then false
872 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes) tl &&
873 check_is_really_smaller_arg
875 ~logger ~metasenv ~subst context n nn kl x safes (List.nth tl k)
876 | C.Var (_,exp_named_subst)
877 | C.Const (_,exp_named_subst)
878 | C.MutInd (_,_,exp_named_subst)
879 | C.MutConstruct (_,_,_,exp_named_subst) ->
881 (fun (_,t) -> guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes t)
883 | C.MutCase (uri,i,outtype,term,pl) ->
884 (match CicReduction.whd ~subst context term with
886 | C.Appl ((C.Rel m)::_) as t when List.mem m safes || m = x ->
887 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
889 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes)
892 specialize_inductive_type ~logger ~subst ~metasenv context t
896 (fun (name,_,ty,_) -> Some (Cic.Name name, Cic.Decl ty)) tys
898 let _,isinductive,_,cl = List.nth tys i in
899 if not isinductive then
900 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes outtype &&
901 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes term &&
903 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes)
906 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes outtype &&
911 debrujin_constructor ~check_exp_named_subst:false
912 rec_uri rec_uri_len context c in
913 let len_ctx = List.length context in
914 recursive_args (context@tys_ctx) len_ctx (len_ctx+rec_uri_len) c
916 let (e, safes',n',nn',x',context') =
917 get_new_safes ~subst context p rec_params safes n nn x
919 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context' n' nn' kl x' safes' e
922 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes outtype &&
923 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes term &&
924 (*CSC: manca ??? il controllo sul tipo di term? *)
926 (fun p i -> i && guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes p)
929 | C.Appl (C.Fix (fixno, fl)::_) | C.Fix (fixno,fl) as t->
930 let l = match t with C.Appl (_::tl) -> tl | _ -> [] in
931 let len = List.length fl in
932 let n_plus_len = n + len in
933 let nn_plus_len = nn + len in
934 let x_plus_len = x + len in
937 (fun (types,len) (n,_,ty,_) ->
938 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
941 let safes' = List.map (fun x -> x + len) safes in
943 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes) l &&
945 (fun (fixno',i) (_,recno,ty,bo) ->
948 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x_plus_len safes' ty &&
951 List.length l > recno &&
952 (*case where the recursive argument is already really_smaller *)
953 check_is_really_smaller_arg
954 rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes
957 let bo_without_lambdas,_,context =
958 eat_lambdas ~subst (tys@context) (recno+1) bo
960 (* we assume the formal argument to be safe *)
961 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context (n_plus_len+recno+1)
962 (nn_plus_len+recno+1) kl (x_plus_len+recno+1)
963 (1::List.map (fun x -> x+recno+1) safes')
966 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst (tys@context) n_plus_len nn_plus_len
967 kl x_plus_len safes' bo
970 let len = List.length fl in
971 let n_plus_len = n + len
972 and nn_plus_len = nn + len
973 and x_plus_len = x + len
976 (fun (types,len) (n,ty,_) ->
977 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
980 and safes' = List.map (fun x -> x + len) safes in
984 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x_plus_len safes' ty &&
985 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst (tys@context) n_plus_len nn_plus_len kl
990 (fun t i -> i && guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes t)
993 (* the boolean h means already protected *)
994 (* args is the list of arguments the type of the constructor that may be *)
995 (* found in head position must be applied to. *)
996 and guarded_by_constructors ~subst context n nn h te args coInductiveTypeURI =
997 let module C = Cic in
998 (*CSC: There is a lot of code replication between the cases X and *)
999 (*CSC: (C.Appl X tl). Maybe it will be better to define a function *)
1000 (*CSC: that maps X into (C.Appl X []) when X is not already a C.Appl *)
1001 match CicReduction.whd ~subst context te with
1002 C.Rel m when m > n && m <= nn -> h
1010 (* the term has just been type-checked *)
1011 raise (AssertFailure (lazy "17"))
1012 | C.Lambda (name,so,de) ->
1013 does_not_occur ~subst context n nn so &&
1014 guarded_by_constructors ~subst ((Some (name,(C.Decl so)))::context)
1015 (n + 1) (nn + 1) h de args coInductiveTypeURI
1016 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
1018 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) tl true
1019 | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
1023 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1024 with Not_found -> assert false
1027 C.InductiveDefinition (itl,_,_,_) ->
1028 let (_,_,_,cl) = List.nth itl i in
1029 let (_,cons) = List.nth cl (j - 1) in
1030 CicSubstitution.subst_vars exp_named_subst cons
1032 raise (TypeCheckerFailure
1033 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
1035 let rec analyse_branch context ty te =
1036 match CicReduction.whd ~subst context ty with
1037 C.Meta _ -> raise (AssertFailure (lazy "34"))
1041 does_not_occur ~subst context n nn te
1044 raise (AssertFailure (lazy "24"))(* due to type-checking *)
1045 | C.Prod (name,so,de) ->
1046 analyse_branch ((Some (name,(C.Decl so)))::context) de te
1049 raise (AssertFailure (lazy "25"))(* due to type-checking *)
1050 | C.Appl ((C.MutInd (uri,_,_))::_) when uri == coInductiveTypeURI ->
1051 guarded_by_constructors ~subst context n nn true te []
1053 | C.Appl ((C.MutInd (uri,_,_))::_) ->
1054 guarded_by_constructors ~subst context n nn true te tl
1057 does_not_occur ~subst context n nn te
1058 | C.Const _ -> raise (AssertFailure (lazy "26"))
1059 | C.MutInd (uri,_,_) when uri == coInductiveTypeURI ->
1060 guarded_by_constructors ~subst context n nn true te []
1063 does_not_occur ~subst context n nn te
1064 | C.MutConstruct _ -> raise (AssertFailure (lazy "27"))
1065 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1066 (*CSC: in head position. *)
1070 raise (AssertFailure (lazy "28"))(* due to type-checking *)
1072 let rec analyse_instantiated_type context ty l =
1073 match CicReduction.whd ~subst context ty with
1079 | C.Cast _ -> raise (AssertFailure (lazy "29"))(* due to type-checking *)
1080 | C.Prod (name,so,de) ->
1085 analyse_branch context so he &&
1086 analyse_instantiated_type
1087 ((Some (name,(C.Decl so)))::context) de tl
1091 raise (AssertFailure (lazy "30"))(* due to type-checking *)
1094 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1095 | C.Const _ -> raise (AssertFailure (lazy "31"))
1098 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1099 | C.MutConstruct _ -> raise (AssertFailure (lazy "32"))
1100 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1101 (*CSC: in head position. *)
1105 raise (AssertFailure (lazy "33"))(* due to type-checking *)
1107 let rec instantiate_type args consty =
1110 | tlhe::tltl as l ->
1111 let consty' = CicReduction.whd ~subst context consty in
1117 let instantiated_de = CicSubstitution.subst he de in
1118 (*CSC: siamo sicuri che non sia troppo forte? *)
1119 does_not_occur ~subst context n nn tlhe &
1120 instantiate_type tl instantiated_de tltl
1122 (*CSC:We do not consider backbones with a MutCase, a *)
1123 (*CSC:FixPoint, a CoFixPoint and so on in head position.*)
1124 raise (AssertFailure (lazy "23"))
1126 | [] -> analyse_instantiated_type context consty' l
1127 (* These are all the other cases *)
1129 instantiate_type args consty tl
1130 | C.Appl ((C.CoFix (_,fl))::tl) ->
1131 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1132 let len = List.length fl in
1133 let n_plus_len = n + len
1134 and nn_plus_len = nn + len
1135 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1138 (fun (types,len) (n,ty,_) ->
1139 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1145 i && does_not_occur ~subst context n nn ty &&
1146 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1147 h bo args coInductiveTypeURI
1149 | C.Appl ((C.MutCase (_,_,out,te,pl))::tl) ->
1150 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1151 does_not_occur ~subst context n nn out &&
1152 does_not_occur ~subst context n nn te &&
1156 guarded_by_constructors ~subst context n nn h x args
1160 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
1161 | C.Var (_,exp_named_subst)
1162 | C.Const (_,exp_named_subst) ->
1164 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1165 | C.MutInd _ -> assert false
1166 | C.MutConstruct (_,_,_,exp_named_subst) ->
1168 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1169 | C.MutCase (_,_,out,te,pl) ->
1170 does_not_occur ~subst context n nn out &&
1171 does_not_occur ~subst context n nn te &&
1175 guarded_by_constructors ~subst context n nn h x args
1179 let len = List.length fl in
1180 let n_plus_len = n + len
1181 and nn_plus_len = nn + len
1182 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1185 (fun (types,len) (n,_,ty,_) ->
1186 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1191 (fun (_,_,ty,bo) i ->
1192 i && does_not_occur ~subst context n nn ty &&
1193 does_not_occur ~subst (tys@context) n_plus_len nn_plus_len bo
1196 let len = List.length fl in
1197 let n_plus_len = n + len
1198 and nn_plus_len = nn + len
1199 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1202 (fun (types,len) (n,ty,_) ->
1203 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1209 i && does_not_occur ~subst context n nn ty &&
1210 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1212 args coInductiveTypeURI
1215 and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1216 need_dummy ind arity1 arity2 ugraph =
1217 let module C = Cic in
1218 let module U = UriManager in
1219 let arity1 = CicReduction.whd ~subst context arity1 in
1220 let rec check_allowed_sort_elimination_aux ugraph context arity2 need_dummy =
1221 match arity1, CicReduction.whd ~subst context arity2 with
1222 (C.Prod (name,so1,de1), C.Prod (_,so2,de2)) ->
1224 CicReduction.are_convertible ~subst ~metasenv context so1 so2 ugraph in
1226 check_allowed_sort_elimination ~subst ~metasenv ~logger
1227 ((Some (name,C.Decl so1))::context) uri i
1228 need_dummy (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1232 | (C.Sort _, C.Prod (name,so,ta)) when not need_dummy ->
1234 CicReduction.are_convertible ~subst ~metasenv context so ind ugraph in
1238 check_allowed_sort_elimination_aux ugraph1
1239 ((Some (name,C.Decl so))::context) ta true
1240 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
1241 | (C.Sort C.Prop, C.Sort C.Set)
1242 | (C.Sort C.Prop, C.Sort C.CProp)
1243 | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
1244 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1246 C.InductiveDefinition (itl,_,paramsno,_) ->
1247 let itl_len = List.length itl in
1248 let (name,_,ty,cl) = List.nth itl i in
1249 let cl_len = List.length cl in
1250 if (cl_len = 0 || (itl_len = 1 && cl_len = 1)) then
1251 let non_informative,ugraph =
1252 if cl_len = 0 then true,ugraph
1254 is_non_informative ~logger [Some (C.Name name,C.Decl ty)]
1255 paramsno (snd (List.nth cl 0)) ugraph
1257 (* is it a singleton or empty non recursive and non informative
1259 non_informative, ugraph
1263 raise (TypeCheckerFailure
1264 (lazy ("Unknown mutual inductive definition:" ^
1265 UriManager.string_of_uri uri)))
1267 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
1268 | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
1269 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
1270 | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
1271 | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
1272 | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
1273 | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
1275 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1277 C.InductiveDefinition (itl,_,paramsno,_) ->
1279 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1281 let (_,_,_,cl) = List.nth itl i in
1283 (fun (_,x) (i,ugraph) ->
1285 is_small ~logger tys paramsno x ugraph
1290 raise (TypeCheckerFailure
1291 (lazy ("Unknown mutual inductive definition:" ^
1292 UriManager.string_of_uri uri)))
1294 | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
1295 | (_,_) -> false,ugraph
1297 check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
1299 and type_of_branch ~subst context argsno need_dummy outtype term constype =
1300 let module C = Cic in
1301 let module R = CicReduction in
1302 match R.whd ~subst context constype with
1307 C.Appl [outtype ; term]
1308 | C.Appl (C.MutInd (_,_,_)::tl) ->
1309 let (_,arguments) = split tl argsno
1311 if need_dummy && arguments = [] then
1314 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1315 | C.Prod (name,so,de) ->
1317 match CicSubstitution.lift 1 term with
1318 C.Appl l -> C.Appl (l@[C.Rel 1])
1319 | t -> C.Appl [t ; C.Rel 1]
1321 C.Prod (name,so,type_of_branch ~subst
1322 ((Some (name,(C.Decl so)))::context) argsno need_dummy
1323 (CicSubstitution.lift 1 outtype) term' de)
1324 | _ -> raise (AssertFailure (lazy "20"))
1326 (* check_metasenv_consistency checks that the "canonical" context of a
1327 metavariable is consitent - up to relocation via the relocation list l -
1328 with the actual context *)
1331 and check_metasenv_consistency ~logger ~subst metasenv context
1332 canonical_context l ugraph
1334 let module C = Cic in
1335 let module R = CicReduction in
1336 let module S = CicSubstitution in
1337 let lifted_canonical_context =
1341 | (Some (n,C.Decl t))::tl ->
1342 (Some (n,C.Decl (S.subst_meta l (S.lift i t))))::(aux (i+1) tl)
1343 | None::tl -> None::(aux (i+1) tl)
1344 | (Some (n,C.Def (t,ty)))::tl ->
1345 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),S.subst_meta l (S.lift i ty))))::(aux (i+1) tl)
1347 aux 1 canonical_context
1353 | Some t,Some (_,C.Def (ct,_)) ->
1354 (*CSC: the following optimization is to avoid a possibly expensive
1355 reduction that can be easily avoided and that is quite
1356 frequent. However, this is better handled using levels to
1357 control reduction *)
1362 match List.nth context (n - 1) with
1363 Some (_,C.Def (te,_)) -> S.lift n te
1369 (*if t <> optimized_t && optimized_t = ct then prerr_endline "!!!!!!!!!!!!!!!"
1370 else if t <> optimized_t then prerr_endline ("@@ " ^ CicPp.ppterm t ^ " ==> " ^ CicPp.ppterm optimized_t ^ " <==> " ^ CicPp.ppterm ct);*)
1372 R.are_convertible ~subst ~metasenv context optimized_t ct ugraph
1377 (lazy (sprintf "Not well typed metavariable local context: expected a term convertible with %s, found %s" (CicPp.ppterm ct) (CicPp.ppterm t))))
1380 | Some t,Some (_,C.Decl ct) ->
1381 let type_t,ugraph1 =
1382 type_of_aux' ~logger ~subst metasenv context t ugraph
1385 R.are_convertible ~subst ~metasenv context type_t ct ugraph1
1388 raise (TypeCheckerFailure
1389 (lazy (sprintf "Not well typed metavariable local context: expected a term of type %s, found %s of type %s"
1390 (CicPp.ppterm ct) (CicPp.ppterm t)
1391 (CicPp.ppterm type_t))))
1395 raise (TypeCheckerFailure
1396 (lazy ("Not well typed metavariable local context: "^
1397 "an hypothesis, that is not hidden, is not instantiated")))
1398 ) ugraph l lifted_canonical_context
1402 type_of_aux' is just another name (with a different scope)
1406 and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
1407 let rec type_of_aux ~logger context t ugraph =
1408 let module C = Cic in
1409 let module R = CicReduction in
1410 let module S = CicSubstitution in
1411 let module U = UriManager in
1415 match List.nth context (n - 1) with
1416 Some (_,C.Decl t) -> S.lift n t,ugraph
1417 | Some (_,C.Def (_,ty)) -> S.lift n ty,ugraph
1419 (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
1422 raise (TypeCheckerFailure (lazy "unbound variable"))
1424 | C.Var (uri,exp_named_subst) ->
1427 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1429 let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
1430 let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
1435 let (canonical_context,term,ty) = CicUtil.lookup_subst n subst in
1437 check_metasenv_consistency ~logger
1438 ~subst metasenv context canonical_context l ugraph
1440 (* assuming subst is well typed !!!!! *)
1441 ((CicSubstitution.subst_meta l ty), ugraph1)
1442 (* type_of_aux context (CicSubstitution.subst_meta l term) *)
1443 with CicUtil.Subst_not_found _ ->
1444 let (_,canonical_context,ty) = CicUtil.lookup_meta n metasenv in
1446 check_metasenv_consistency ~logger
1447 ~subst metasenv context canonical_context l ugraph
1449 ((CicSubstitution.subst_meta l ty),ugraph1))
1450 (* TASSI: CONSTRAINTS *)
1451 | C.Sort (C.Type t) ->
1452 let t' = CicUniv.fresh() in
1454 let ugraph1 = CicUniv.add_gt t' t ugraph in
1455 (C.Sort (C.Type t')),ugraph1
1457 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
1458 | C.Sort s -> (C.Sort (C.Type (CicUniv.fresh ()))),ugraph
1459 | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
1460 | C.Cast (te,ty) as t ->
1461 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1462 let ty_te,ugraph2 = type_of_aux ~logger context te ugraph1 in
1464 R.are_convertible ~subst ~metasenv context ty_te ty ugraph2
1469 raise (TypeCheckerFailure
1470 (lazy (sprintf "Invalid cast %s" (CicPp.ppterm t))))
1471 | C.Prod (name,s,t) ->
1472 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1474 type_of_aux ~logger ((Some (name,(C.Decl s)))::context) t ugraph1
1476 sort_of_prod ~subst context (name,s) (sort1,sort2) ugraph2
1477 | C.Lambda (n,s,t) ->
1478 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1479 (match R.whd ~subst context sort1 with
1484 (TypeCheckerFailure (lazy (sprintf
1485 "Not well-typed lambda-abstraction: the source %s should be a type; instead it is a term of type %s" (CicPp.ppterm s)
1486 (CicPp.ppterm sort1))))
1489 type_of_aux ~logger ((Some (n,(C.Decl s)))::context) t ugraph1
1491 (C.Prod (n,s,type2)),ugraph2
1492 | C.LetIn (n,s,ty,t) ->
1493 (* only to check if s is well-typed *)
1494 let ty',ugraph1 = type_of_aux ~logger context s ugraph in
1495 let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in
1497 R.are_convertible ~subst ~metasenv context ty ty' ugraph1
1503 "The type of %s is %s but it is expected to be %s"
1504 (CicPp.ppterm s) (CicPp.ppterm ty') (CicPp.ppterm ty))))
1506 (* The type of a LetIn is a LetIn. Extremely slow since the computed
1507 LetIn is later reduced and maybe also re-checked.
1508 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t))
1510 (* The type of the LetIn is reduced. Much faster than the previous
1511 solution. Moreover the inferred type is probably very different
1512 from the expected one.
1513 (CicReduction.whd ~subst context
1514 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)))
1516 (* One-step LetIn reduction. Even faster than the previous solution.
1517 Moreover the inferred type is closer to the expected one. *)
1520 ((Some (n,(C.Def (s,ty))))::context) t ugraph1
1522 (CicSubstitution.subst ~avoid_beta_redexes:true s ty1),ugraph2
1523 | C.Appl (he::tl) when List.length tl > 0 ->
1524 let hetype,ugraph1 = type_of_aux ~logger context he ugraph in
1525 let tlbody_and_type,ugraph2 =
1528 let ty,ugraph1 = type_of_aux ~logger context x ugraph in
1529 (*let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in*)
1530 ((x,ty)::l,ugraph1))
1533 (* TASSI: questa c'era nel mio... ma non nel CVS... *)
1534 (* let _,ugraph2 = type_of_aux context hetype ugraph2 in *)
1535 eat_prods ~subst context hetype tlbody_and_type ugraph2
1536 | C.Appl _ -> raise (AssertFailure (lazy "Appl: no arguments"))
1537 | C.Const (uri,exp_named_subst) ->
1540 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1542 let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
1544 CicSubstitution.subst_vars exp_named_subst cty
1548 | C.MutInd (uri,i,exp_named_subst) ->
1551 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1553 (* TASSI: da me c'era anche questa, ma in CVS no *)
1554 let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
1555 (* fine parte dubbia *)
1557 CicSubstitution.subst_vars exp_named_subst mty
1561 | C.MutConstruct (uri,i,j,exp_named_subst) ->
1563 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1565 (* TASSI: idem come sopra *)
1567 type_of_mutual_inductive_constr ~logger uri i j ugraph1
1570 CicSubstitution.subst_vars exp_named_subst mty
1573 | C.MutCase (uri,i,outtype,term,pl) ->
1574 let outsort,ugraph1 = type_of_aux ~logger context outtype ugraph in
1575 let (need_dummy, k) =
1576 let rec guess_args context t =
1577 let outtype = CicReduction.whd ~subst context t in
1579 C.Sort _ -> (true, 0)
1580 | C.Prod (name, s, t) ->
1582 guess_args ((Some (name,(C.Decl s)))::context) t in
1584 (* last prod before sort *)
1585 match CicReduction.whd ~subst context s with
1586 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1587 C.MutInd (uri',i',_) when U.eq uri' uri && i' = i ->
1589 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1590 | C.Appl ((C.MutInd (uri',i',_)) :: _)
1591 when U.eq uri' uri && i' = i -> (false, 1)
1599 "Malformed case analasys' output type %s"
1600 (CicPp.ppterm outtype))))
1603 let (parameters, arguments, exp_named_subst),ugraph2 =
1604 let ty,ugraph2 = type_of_aux context term ugraph1 in
1605 match R.whd ~subst context ty with
1606 (*CSC manca il caso dei CAST *)
1607 (*CSC: ma servono i parametri (uri,i)? Se si', perche' non serve anche il *)
1608 (*CSC: parametro exp_named_subst? Se no, perche' non li togliamo? *)
1609 (*CSC: Hint: nella DTD servono per gli stylesheet. *)
1610 C.MutInd (uri',i',exp_named_subst) as typ ->
1611 if U.eq uri uri' && i = i' then
1612 ([],[],exp_named_subst),ugraph2
1617 ("Case analysys: analysed term type is %s, but is expected to be (an application of) %s#1/%d{_}")
1618 (CicPp.ppterm typ) (U.string_of_uri uri) i)))
1620 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1621 if U.eq uri uri' && i = i' then
1623 split tl (List.length tl - k)
1624 in (params,args,exp_named_subst),ugraph2
1629 ("Case analysys: analysed term type is %s, "^
1630 "but is expected to be (an application of) "^
1632 (CicPp.ppterm typ') (U.string_of_uri uri) i)))
1638 "analysed term %s is not an inductive one")
1639 (CicPp.ppterm term))))
1641 let (b, k) = guess_args context outsort in
1642 if not b then (b, k - 1) else (b, k) in
1643 let (parameters, arguments, exp_named_subst),ugraph2 =
1644 let ty,ugraph2 = type_of_aux ~logger context term ugraph1 in
1645 match R.whd ~subst context ty with
1646 C.MutInd (uri',i',exp_named_subst) as typ ->
1647 if U.eq uri uri' && i = i' then
1648 ([],[],exp_named_subst),ugraph2
1652 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1653 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1654 | C.Appl ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) ->
1655 if U.eq uri uri' && i = i' then
1657 split tl (List.length tl - k)
1658 in (params,args,exp_named_subst),ugraph2
1662 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1663 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1668 "Case analysis: analysed term %s is not an inductive one"
1669 (CicPp.ppterm term))))
1672 let's control if the sort elimination is allowed:
1675 let sort_of_ind_type =
1676 if parameters = [] then
1677 C.MutInd (uri,i,exp_named_subst)
1679 C.Appl ((C.MutInd (uri,i,exp_named_subst))::parameters)
1681 let type_of_sort_of_ind_ty,ugraph3 =
1682 type_of_aux ~logger context sort_of_ind_type ugraph2 in
1684 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1685 need_dummy sort_of_ind_type type_of_sort_of_ind_ty outsort ugraph3
1689 (TypeCheckerFailure (lazy ("Case analysis: sort elimination not allowed")));
1690 (* let's check if the type of branches are right *)
1691 let parsno,constructorsno =
1694 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1695 with Not_found -> assert false
1698 C.InductiveDefinition (il,_,parsno,_) ->
1700 try List.nth il i with Failure _ -> assert false
1702 parsno, List.length cl
1704 raise (TypeCheckerFailure
1705 (lazy ("Unknown mutual inductive definition:" ^
1706 UriManager.string_of_uri uri)))
1708 if List.length pl <> constructorsno then
1709 raise (TypeCheckerFailure
1710 (lazy ("Wrong number of cases in case analysis"))) ;
1711 let (_,branches_ok,ugraph5) =
1713 (fun (j,b,ugraph) p ->
1716 if parameters = [] then
1717 (C.MutConstruct (uri,i,j,exp_named_subst))
1720 (C.MutConstruct (uri,i,j,exp_named_subst)::parameters))
1722 let ty_p,ugraph1 = type_of_aux ~logger context p ugraph in
1723 let ty_cons,ugraph3 = type_of_aux ~logger context cons ugraph1 in
1726 type_of_branch ~subst context parsno need_dummy outtype cons
1730 ~subst ~metasenv context ty_p ty_branch ugraph3
1735 prerr_endline ("\n!OUTTYPE= " ^ CicPp.ppterm outtype);
1736 prerr_endline ("!CONS= " ^ CicPp.ppterm cons);
1737 prerr_endline ("!TY_CONS= " ^ CicPp.ppterm ty_cons);
1738 prerr_endline ("#### " ^ CicPp.ppterm ty_p ^ "\n<==>\n" ^ CicPp.ppterm ty_branch);
1743 ("#### " ^ CicPp.ppterm ty_p ^
1744 " <==> " ^ CicPp.ppterm ty_branch));
1748 ) (1,true,ugraph4) pl
1750 if not branches_ok then
1752 (TypeCheckerFailure (lazy "Case analysys: wrong branch type"));
1754 if not need_dummy then outtype::arguments@[term]
1755 else outtype::arguments in
1757 if need_dummy && arguments = [] then outtype
1758 else CicReduction.head_beta_reduce (C.Appl arguments')
1762 let types,kl,ugraph1,len =
1764 (fun (types,kl,ugraph,len) (n,k,ty,_) ->
1765 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1766 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1767 k::kl,ugraph1,len+1)
1768 ) ([],[],ugraph,0) fl
1772 (fun ugraph (name,x,ty,bo) ->
1774 type_of_aux ~logger (types@context) bo ugraph
1777 R.are_convertible ~subst ~metasenv (types@context)
1778 ty_bo (CicSubstitution.lift len ty) ugraph1 in
1781 let (m, eaten, context') =
1782 eat_lambdas ~subst (types @ context) (x + 1) bo
1784 let rec_uri, rec_uri_len =
1786 match List.hd context' with
1787 Some (_,Cic.Decl he) -> he
1790 match CicReduction.whd ~subst (List.tl context') he with
1791 | Cic.MutInd (uri,_,_)
1792 | Cic.Appl (Cic.MutInd (uri,_,_)::_) ->
1795 CicEnvironment.get_obj
1796 CicUniv.oblivion_ugraph uri
1798 | Cic.InductiveDefinition (tl,_,_,_), _ ->
1800 | _ -> assert false)
1804 let's control the guarded by
1805 destructors conditions D{f,k,x,M}
1807 if not (guarded_by_destructors ~logger ~metasenv ~subst
1808 rec_uri rec_uri_len context' eaten (len + eaten) kl
1813 (lazy ("Fix: not guarded by destructors:"^CicPp.ppterm t)))
1818 raise (TypeCheckerFailure (lazy ("Fix: ill-typed bodies")))
1820 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1821 let (_,_,ty,_) = List.nth fl i in
1824 let types,ugraph1,len =
1826 (fun (l,ugraph,len) (n,ty,_) ->
1828 type_of_aux ~logger context ty ugraph in
1829 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::l,
1835 (fun ugraph (_,ty,bo) ->
1837 type_of_aux ~logger (types @ context) bo ugraph
1840 R.are_convertible ~subst ~metasenv (types @ context) ty_bo
1841 (CicSubstitution.lift len ty) ugraph1
1845 (* let's control that the returned type is coinductive *)
1846 match returns_a_coinductive ~subst context ty with
1850 (lazy "CoFix: does not return a coinductive type"))
1853 let's control the guarded by constructors
1856 if not (guarded_by_constructors ~subst
1857 (types @ context) 0 len false bo [] uri) then
1860 (lazy "CoFix: not guarded by constructors"))
1866 (TypeCheckerFailure (lazy "CoFix: ill-typed bodies"))
1869 let (_,ty,_) = List.nth fl i in
1872 and check_exp_named_subst ~logger ~subst context =
1873 let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
1876 | ((uri,t) as item)::tl ->
1877 let ty_uri,ugraph1 = type_of_variable ~logger uri ugraph in
1879 CicSubstitution.subst_vars esubsts ty_uri in
1880 let typeoft,ugraph2 = type_of_aux ~logger context t ugraph1 in
1882 CicReduction.are_convertible ~subst ~metasenv
1883 context typeoft typeofvar ugraph2
1886 check_exp_named_subst_aux ~logger (esubsts@[item]) tl ugraph3
1889 CicReduction.fdebug := 0 ;
1891 (CicReduction.are_convertible
1892 ~subst ~metasenv context typeoft typeofvar ugraph2) ;
1894 debug typeoft [typeofvar] ;
1895 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
1898 check_exp_named_subst_aux ~logger []
1900 and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
1901 let module C = Cic in
1902 let t1' = CicReduction.whd ~subst context t1 in
1903 let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
1904 match (t1', t2') with
1905 (C.Sort s1, C.Sort s2)
1906 when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) ->
1907 (* different from Coq manual!!! *)
1909 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
1910 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1911 let t' = CicUniv.fresh() in
1913 let ugraph1 = CicUniv.add_ge t' t1 ugraph in
1914 let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
1915 C.Sort (C.Type t'),ugraph2
1917 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
1918 | (C.Sort _,C.Sort (C.Type t1)) ->
1919 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1920 C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
1921 | (C.Meta _, C.Sort _) -> t2',ugraph
1922 | (C.Meta _, (C.Meta (_,_) as t))
1923 | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
1925 | (_,_) -> raise (TypeCheckerFailure (lazy (sprintf
1926 "Prod: expected two sorts, found = %s, %s" (CicPp.ppterm t1')
1927 (CicPp.ppterm t2'))))
1929 and eat_prods ~subst context hetype l ugraph =
1930 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
1934 | (hete, hety)::tl ->
1935 (match (CicReduction.whd ~subst context hetype) with
1938 (*if (match hety,s with Cic.Sort _,Cic.Sort _ -> false | _,_ -> true) && hety <> s then(
1939 prerr_endline ("AAA22: " ^ CicPp.ppterm hete ^ ": " ^ CicPp.ppterm hety ^ " <==> " ^ CicPp.ppterm s); let res = CicReduction.are_convertible ~subst ~metasenv context hety s ugraph in prerr_endline "#"; res) else*)
1940 CicReduction.are_convertible
1941 ~subst ~metasenv context hety s ugraph
1945 CicReduction.fdebug := -1 ;
1946 eat_prods ~subst context
1947 (CicSubstitution.subst ~avoid_beta_redexes:true hete t)
1949 (*TASSI: not sure *)
1953 CicReduction.fdebug := 0 ;
1954 ignore (CicReduction.are_convertible
1955 ~subst ~metasenv context s hety ugraph) ;
1961 ("Appl: wrong parameter-type, expected %s, found %s")
1962 (CicPp.ppterm hetype) (CicPp.ppterm s))))
1965 raise (TypeCheckerFailure
1966 (lazy "Appl: this is not a function, it cannot be applied"))
1969 and returns_a_coinductive ~subst context ty =
1970 let module C = Cic in
1971 match CicReduction.whd ~subst context ty with
1972 C.MutInd (uri,i,_) ->
1973 (*CSC: definire una funzioncina per questo codice sempre replicato *)
1976 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1977 with Not_found -> assert false
1980 C.InductiveDefinition (itl,_,_,_) ->
1981 let (_,is_inductive,_,_) = List.nth itl i in
1982 if is_inductive then None else (Some uri)
1984 raise (TypeCheckerFailure
1985 (lazy ("Unknown mutual inductive definition:" ^
1986 UriManager.string_of_uri uri)))
1988 | C.Appl ((C.MutInd (uri,i,_))::_) ->
1989 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1991 C.InductiveDefinition (itl,_,_,_) ->
1992 let (_,is_inductive,_,_) = List.nth itl i in
1993 if is_inductive then None else (Some uri)
1995 raise (TypeCheckerFailure
1996 (lazy ("Unknown mutual inductive definition:" ^
1997 UriManager.string_of_uri uri)))
1999 | C.Prod (n,so,de) ->
2000 returns_a_coinductive ~subst ((Some (n,C.Decl so))::context) de
2005 debug_print (lazy ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t)) ; flush stderr ;
2008 type_of_aux ~logger context t ugraph
2010 in debug_print (lazy "FINE TYPE_OF_AUX") ; flush stderr ; res
2013 (* is a small constructor? *)
2014 (*CSC: ottimizzare calcolando staticamente *)
2015 and is_small_or_non_informative ~condition ~logger context paramsno c ugraph =
2016 let rec is_small_or_non_informative_aux ~logger context c ugraph =
2017 let module C = Cic in
2018 match CicReduction.whd context c with
2020 let s,ugraph1 = type_of_aux' ~logger [] context so ugraph in
2021 let b = condition s in
2023 is_small_or_non_informative_aux
2024 ~logger ((Some (n,(C.Decl so)))::context) de ugraph1
2027 | _ -> true,ugraph (*CSC: we trust the type-checker *)
2029 let (context',dx) = split_prods ~subst:[] context paramsno c in
2030 is_small_or_non_informative_aux ~logger context' dx ugraph
2032 and is_small ~logger =
2033 is_small_or_non_informative
2034 ~condition:(fun s -> s=Cic.Sort Cic.Prop || s=Cic.Sort Cic.Set)
2037 and is_non_informative ~logger =
2038 is_small_or_non_informative
2039 ~condition:(fun s -> s=Cic.Sort Cic.Prop)
2042 and type_of ~logger t ugraph =
2044 debug_print (lazy ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t)) ; flush stderr ;
2047 type_of_aux' ~logger [] [] t ugraph
2049 in debug_print (lazy "FINE TYPE_OF_AUX'") ; flush stderr ; res
2053 let typecheck_obj0 ~logger uri ugraph =
2054 let module C = Cic in
2056 C.Constant (_,Some te,ty,_,_) ->
2057 let _,ugraph = type_of ~logger ty ugraph in
2058 let ty_te,ugraph = type_of ~logger te ugraph in
2059 let b,ugraph = (CicReduction.are_convertible [] ty_te ty ugraph) in
2061 raise (TypeCheckerFailure
2063 ("the type of the body is not the one expected:\n" ^
2064 CicPp.ppterm ty_te ^ "\nvs\n" ^
2068 | C.Constant (_,None,ty,_,_) ->
2069 (* only to check that ty is well-typed *)
2070 let _,ugraph = type_of ~logger ty ugraph in
2072 | C.CurrentProof (_,conjs,te,ty,_,_) ->
2073 (* this block is broken since the metasenv should
2074 * be topologically sorted before typing metas *)
2075 ignore(assert false);
2078 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
2080 type_of_aux' ~logger metasenv context ty ugraph
2082 metasenv @ [conj],ugraph
2085 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
2086 let type_of_te,ugraph =
2087 type_of_aux' ~logger conjs [] te ugraph
2089 let b,ugraph = CicReduction.are_convertible [] type_of_te ty ugraph in
2091 raise (TypeCheckerFailure (lazy (sprintf
2092 "the current proof is not well typed because the type %s of the body is not convertible to the declared type %s"
2093 (CicPp.ppterm type_of_te) (CicPp.ppterm ty))))
2096 | C.Variable (_,bo,ty,_,_) ->
2097 (* only to check that ty is well-typed *)
2098 let _,ugraph = type_of ~logger ty ugraph in
2102 let ty_bo,ugraph = type_of ~logger bo ugraph in
2103 let b,ugraph = CicReduction.are_convertible [] ty_bo ty ugraph in
2105 raise (TypeCheckerFailure
2106 (lazy "the body is not the one expected"))
2110 | (C.InductiveDefinition _ as obj) ->
2111 check_mutual_inductive_defs ~logger uri obj ugraph
2114 let module C = Cic in
2115 let module R = CicReduction in
2116 let module U = UriManager in
2117 let logger = new CicLogger.logger in
2118 (* ??? match CicEnvironment.is_type_checked ~trust:true uri with ???? *)
2119 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2120 CicEnvironment.CheckedObj (cobj,ugraph') ->
2121 (* debug_print (lazy ("NON-INIZIO A TYPECHECKARE " ^ U.string_of_uri uri));*)
2123 | CicEnvironment.UncheckedObj uobj ->
2124 (* let's typecheck the uncooked object *)
2125 logger#log (`Start_type_checking uri) ;
2126 (* debug_print (lazy ("INIZIO A TYPECHECKARE " ^ U.string_of_uri uri)); *)
2127 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph uobj in
2129 CicEnvironment.set_type_checking_info uri;
2130 logger#log (`Type_checking_completed uri);
2131 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
2132 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2133 | _ -> raise CicEnvironmentError
2136 this is raised if set_type_checking_info is called on an object
2137 that has no associated universe file. If we are in univ_maker
2138 phase this is OK since univ_maker will properly commit the
2141 Invalid_argument s ->
2142 (*debug_print (lazy s);*)
2146 let typecheck_obj ~logger uri obj =
2147 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph obj in
2148 let ugraph, univlist, obj = CicUnivUtils.clean_and_fill uri obj ugraph in
2149 CicEnvironment.add_type_checked_obj uri (obj,ugraph,univlist)
2151 (** wrappers which instantiate fresh loggers *)
2153 let profiler = HExtlib.profile "K/CicTypeChecker.type_of_aux'"
2155 let type_of_aux' ?(subst = []) metasenv context t ugraph =
2156 let logger = new CicLogger.logger in
2157 profiler.HExtlib.profile
2158 (type_of_aux' ~logger ~subst metasenv context t) ugraph
2160 let typecheck_obj uri obj =
2161 let logger = new CicLogger.logger in
2162 typecheck_obj ~logger uri obj
2164 (* check_allowed_sort_elimination uri i s1 s2
2165 This function is used outside the kernel to determine in advance whether
2166 a MutCase will be allowed or not.
2167 [uri,i] is the type of the term to match
2168 [s1] is the sort of the term to eliminate (i.e. the head of the arity
2169 of the inductive type [uri,i])
2170 [s2] is the sort of the goal (i.e. the head of the type of the outtype
2172 let check_allowed_sort_elimination uri i s1 s2 =
2173 fst (check_allowed_sort_elimination ~subst:[] ~metasenv:[]
2174 ~logger:(new CicLogger.logger) [] uri i true
2175 (Cic.Implicit None) (* never used *) (Cic.Sort s1) (Cic.Sort s2)
2176 CicUniv.empty_ugraph)
2179 Deannotate.type_of_aux' :=
2186 | Some (_,Cic.Decl ty) ->
2187 ignore (type_of_aux' [] context ty CicUniv.oblivion_ugraph)
2188 | Some (_,Cic.Def (bo,ty)) ->
2189 ignore (type_of_aux' [] context ty CicUniv.oblivion_ugraph);
2190 ignore (type_of_aux' [] context bo CicUniv.oblivion_ugraph));
2193 fst (type_of_aux' [] context t CicUniv.oblivion_ugraph);;