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 _ _ -> ()) uri number_of_types =
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,t) -> C.LetIn (n, aux k s, 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 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)
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 n 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,dest) ->
277 does_not_occur ~subst context n nn so &&
278 does_not_occur ~subst ((Some (name,(C.Def (so,None))))::context)
279 (n + 1) (nn + 1) dest
281 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
282 | C.Var (_,exp_named_subst)
283 | C.Const (_,exp_named_subst)
284 | C.MutInd (_,_,exp_named_subst)
285 | C.MutConstruct (_,_,_,exp_named_subst) ->
286 List.fold_right (fun (_,x) i -> i && does_not_occur ~subst context n nn x)
288 | C.MutCase (_,_,out,te,pl) ->
289 does_not_occur ~subst context n nn out && does_not_occur ~subst context n nn te &&
290 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) pl true
292 let len = List.length fl in
293 let n_plus_len = n + len in
294 let nn_plus_len = nn + len in
296 List.map (fun (n,_,ty,_) -> Some (C.Name n,(Cic.Decl ty))) fl
299 (fun (_,_,ty,bo) i ->
300 i && does_not_occur ~subst context n nn ty &&
301 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
304 let len = List.length fl in
305 let n_plus_len = n + len in
306 let nn_plus_len = nn + len in
308 List.map (fun (n,ty,_) -> Some (C.Name n,(Cic.Decl ty))) fl
312 i && does_not_occur ~subst context n nn ty &&
313 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
316 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
317 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
318 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
319 (*CSC strictly_positive *)
320 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
321 and weakly_positive context n nn uri te =
322 let module C = Cic in
323 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
325 C.MutInd (HelmLibraryObjects.Datatypes.nat_URI,0,[])
327 (*CSC: mettere in cicSubstitution *)
328 let rec subst_inductive_type_with_dummy_mutind =
330 C.MutInd (uri',0,_) when UriManager.eq uri' uri ->
332 | C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri ->
334 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
335 | C.Prod (name,so,ta) ->
336 C.Prod (name, subst_inductive_type_with_dummy_mutind so,
337 subst_inductive_type_with_dummy_mutind ta)
338 | C.Lambda (name,so,ta) ->
339 C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
340 subst_inductive_type_with_dummy_mutind ta)
342 C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
343 | C.MutCase (uri,i,outtype,term,pl) ->
345 subst_inductive_type_with_dummy_mutind outtype,
346 subst_inductive_type_with_dummy_mutind term,
347 List.map subst_inductive_type_with_dummy_mutind pl)
349 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
350 subst_inductive_type_with_dummy_mutind ty,
351 subst_inductive_type_with_dummy_mutind bo)) fl)
353 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
354 subst_inductive_type_with_dummy_mutind ty,
355 subst_inductive_type_with_dummy_mutind bo)) fl)
356 | C.Const (uri,exp_named_subst) ->
357 let exp_named_subst' =
359 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
362 C.Const (uri,exp_named_subst')
363 | C.MutInd (uri,typeno,exp_named_subst) ->
364 let exp_named_subst' =
366 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
369 C.MutInd (uri,typeno,exp_named_subst')
370 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
371 let exp_named_subst' =
373 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
376 C.MutConstruct (uri,typeno,consno,exp_named_subst')
379 match CicReduction.whd context te with
381 C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri -> true
383 C.Appl ((C.MutInd (uri',_,_))::tl) when UriManager.eq uri' uri -> true
384 | C.MutInd (uri',0,_) when UriManager.eq uri' uri -> true
385 | C.Prod (C.Anonymous,source,dest) ->
386 strictly_positive context n nn
387 (subst_inductive_type_with_dummy_mutind source) &&
388 weakly_positive ((Some (C.Anonymous,(C.Decl source)))::context)
389 (n + 1) (nn + 1) uri dest
390 | C.Prod (name,source,dest) when
391 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
392 (* dummy abstraction, so we behave as in the anonimous case *)
393 strictly_positive context n nn
394 (subst_inductive_type_with_dummy_mutind source) &&
395 weakly_positive ((Some (name,(C.Decl source)))::context)
396 (n + 1) (nn + 1) uri dest
397 | C.Prod (name,source,dest) ->
398 does_not_occur context n nn
399 (subst_inductive_type_with_dummy_mutind source)&&
400 weakly_positive ((Some (name,(C.Decl source)))::context)
401 (n + 1) (nn + 1) uri dest
403 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
405 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
406 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
407 and instantiate_parameters params c =
408 let module C = Cic in
409 match (c,params) with
411 | (C.Prod (_,_,ta), he::tl) ->
412 instantiate_parameters tl
413 (CicSubstitution.subst he ta)
414 | (C.Cast (te,_), _) -> instantiate_parameters params te
415 | (t,l) -> raise (AssertFailure (lazy "1"))
417 and strictly_positive context n nn te =
418 let module C = Cic in
419 let module U = UriManager in
420 match CicReduction.whd context te with
421 | t when does_not_occur context n nn t -> true
424 (*CSC: bisogna controllare ty????*)
425 strictly_positive context n nn te
426 | C.Prod (name,so,ta) ->
427 does_not_occur context n nn so &&
428 strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
429 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
430 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
431 | C.Appl ((C.MutInd (uri,i,exp_named_subst))::tl) ->
432 let (ok,paramsno,ity,cl,name) =
433 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
435 C.InductiveDefinition (tl,_,paramsno,_) ->
436 let (name,_,ity,cl) = List.nth tl i in
437 (List.length tl = 1, paramsno, ity, cl, name)
438 (* (true, paramsno, ity, cl, name) *)
442 (lazy ("Unknown inductive type:" ^ U.string_of_uri uri)))
444 let (params,arguments) = split tl paramsno in
445 let lifted_params = List.map (CicSubstitution.lift 1) params in
449 instantiate_parameters lifted_params
450 (CicSubstitution.subst_vars exp_named_subst te)
455 (fun x i -> i && does_not_occur context n nn x)
457 (*CSC: MEGAPATCH3 (sara' quella giusta?)*)
462 ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri
467 (* the inductive type indexes are s.t. n < x <= nn *)
468 and are_all_occurrences_positive context uri indparamsno i n nn te =
469 let module C = Cic in
470 match CicReduction.whd context te with
471 C.Appl ((C.Rel m)::tl) when m = i ->
472 (*CSC: riscrivere fermandosi a 0 *)
473 (* let's check if the inductive type is applied at least to *)
474 (* indparamsno parameters *)
480 match CicReduction.whd context x with
481 C.Rel m when m = n - (indparamsno - k) -> k - 1
483 raise (TypeCheckerFailure
485 ("Non-positive occurence in mutual inductive definition(s) [1]" ^
486 UriManager.string_of_uri uri)))
490 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
492 raise (TypeCheckerFailure
493 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
494 UriManager.string_of_uri uri)))
495 | C.Rel m when m = i ->
496 if indparamsno = 0 then
499 raise (TypeCheckerFailure
500 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
501 UriManager.string_of_uri uri)))
502 | C.Prod (C.Anonymous,source,dest) ->
503 let b = strictly_positive context n nn source in
505 are_all_occurrences_positive
506 ((Some (C.Anonymous,(C.Decl source)))::context) uri indparamsno
507 (i+1) (n + 1) (nn + 1) dest
508 | C.Prod (name,source,dest) when
509 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
510 (* dummy abstraction, so we behave as in the anonimous case *)
511 strictly_positive context n nn source &&
512 are_all_occurrences_positive
513 ((Some (name,(C.Decl source)))::context) uri indparamsno
514 (i+1) (n + 1) (nn + 1) dest
515 | C.Prod (name,source,dest) ->
516 does_not_occur context n nn source &&
517 are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
518 uri indparamsno (i+1) (n + 1) (nn + 1) dest
521 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
522 (UriManager.string_of_uri uri))))
524 (* Main function to checks the correctness of a mutual *)
525 (* inductive block definition. This is the function *)
526 (* exported to the proof-engine. *)
527 and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
528 let module U = UriManager in
529 (* let's check if the arity of the inductive types are well *)
531 let ugrap1 = List.fold_left
532 (fun ugraph (_,_,x,_) -> let _,ugraph' =
533 type_of ~logger x ugraph in ugraph')
536 (* let's check if the types of the inductive constructors *)
537 (* are well formed. *)
538 (* In order not to use type_of_aux we put the types of the *)
539 (* mutual inductive types at the head of the types of the *)
540 (* constructors using Prods *)
541 let len = List.length itl in
543 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
546 (fun (_,_,_,cl) (i,ugraph) ->
549 (fun ugraph (name,te) ->
550 let debrujinedte = debrujin_constructor uri len te in
553 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
556 let _,ugraph' = type_of ~logger augmented_term ugraph in
557 (* let's check also the positivity conditions *)
560 (are_all_occurrences_positive tys uri indparamsno i 0 len
564 prerr_endline (UriManager.string_of_uri uri);
565 prerr_endline (string_of_int (List.length tys));
568 (lazy ("Non positive occurence in " ^ U.string_of_uri uri))) end
577 (* Main function to checks the correctness of a mutual *)
578 (* inductive block definition. *)
579 and check_mutual_inductive_defs uri obj ugraph =
581 Cic.InductiveDefinition (itl, params, indparamsno, _) ->
582 typecheck_mutual_inductive_defs uri (itl,params,indparamsno) ugraph
584 raise (TypeCheckerFailure (
585 lazy ("Unknown mutual inductive definition:" ^
586 UriManager.string_of_uri uri)))
588 and type_of_mutual_inductive_defs ~logger uri i ugraph =
589 let module C = Cic in
590 let module R = CicReduction in
591 let module U = UriManager in
593 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
594 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
595 | CicEnvironment.UncheckedObj uobj ->
596 logger#log (`Start_type_checking uri) ;
598 check_mutual_inductive_defs ~logger uri uobj ugraph
600 (* TASSI: FIXME: check ugraph1 == ugraph ritornato da env *)
602 CicEnvironment.set_type_checking_info uri ;
603 logger#log (`Type_checking_completed uri) ;
604 (match CicEnvironment.is_type_checked ~trust:false ugraph uri with
605 CicEnvironment.CheckedObj (cobj,ugraph') -> (cobj,ugraph')
606 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
609 Invalid_argument s ->
610 (*debug_print (lazy s);*)
614 C.InductiveDefinition (dl,_,_,_) ->
615 let (_,_,arity,_) = List.nth dl i in
618 raise (TypeCheckerFailure
619 (lazy ("Unknown mutual inductive definition:" ^ U.string_of_uri uri)))
621 and type_of_mutual_inductive_constr ~logger uri i j ugraph =
622 let module C = Cic in
623 let module R = CicReduction in
624 let module U = UriManager in
626 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
627 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
628 | CicEnvironment.UncheckedObj uobj ->
629 logger#log (`Start_type_checking uri) ;
631 check_mutual_inductive_defs ~logger uri uobj ugraph
633 (* check ugraph1 validity ??? == ugraph' *)
635 CicEnvironment.set_type_checking_info uri ;
636 logger#log (`Type_checking_completed uri) ;
638 CicEnvironment.is_type_checked ~trust:false ugraph uri
640 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
641 | CicEnvironment.UncheckedObj _ ->
642 raise CicEnvironmentError)
644 Invalid_argument s ->
645 (*debug_print (lazy s);*)
649 C.InductiveDefinition (dl,_,_,_) ->
650 let (_,_,_,cl) = List.nth dl i in
651 let (_,ty) = List.nth cl (j-1) in
654 raise (TypeCheckerFailure
655 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
657 and recursive_args context n nn te =
658 let module C = Cic in
659 match CicReduction.whd context te with
665 | C.Cast _ (*CSC ??? *) ->
666 raise (AssertFailure (lazy "3")) (* due to type-checking *)
667 | C.Prod (name,so,de) ->
668 (not (does_not_occur context n nn so)) ::
669 (recursive_args ((Some (name,(C.Decl so)))::context) (n+1) (nn + 1) de)
672 raise (AssertFailure (lazy "4")) (* due to type-checking *)
674 | C.Const _ -> raise (AssertFailure (lazy "5"))
679 | C.CoFix _ -> raise (AssertFailure (lazy "6")) (* due to type-checking *)
681 and get_new_safes ~subst context p c rl safes n nn x =
682 let module C = Cic in
683 let module U = UriManager in
684 let module R = CicReduction in
685 match (R.whd ~subst context c, R.whd ~subst context p, rl) with
686 (C.Prod (_,so,ta1), C.Lambda (name,_,ta2), b::tl) ->
687 (* we are sure that the two sources are convertible because we *)
688 (* have just checked this. So let's go along ... *)
690 List.map (fun x -> x + 1) safes
693 if b then 1::safes' else safes'
695 get_new_safes ~subst ((Some (name,(C.Decl so)))::context)
696 ta2 ta1 tl safes'' (n+1) (nn+1) (x+1)
697 | (C.Prod _, (C.MutConstruct _ as e), _)
698 | (C.Prod _, (C.Rel _ as e), _)
699 | (C.MutInd _, e, [])
700 | (C.Appl _, e, []) -> (e,safes,n,nn,x,context)
702 (* CSC: If the next exception is raised, it just means that *)
703 (* CSC: the proof-assistant allows to use very strange things *)
704 (* CSC: as a branch of a case whose type is a Prod. In *)
705 (* CSC: particular, this means that a new (C.Prod, x,_) case *)
706 (* CSC: must be considered in this match. (e.g. x = MutCase) *)
709 (Printf.sprintf "Get New Safes: c=%s ; p=%s"
710 (CicPp.ppterm c) (CicPp.ppterm p))))
712 and split_prods ~subst context n te =
713 let module C = Cic in
714 let module R = CicReduction in
715 match (n, R.whd ~subst context te) with
717 | (n, C.Prod (name,so,ta)) when n > 0 ->
718 split_prods ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
719 | (_, _) -> raise (AssertFailure (lazy "8"))
721 and eat_lambdas ~subst context n te =
722 let module C = Cic in
723 let module R = CicReduction in
724 match (n, R.whd ~subst context te) with
725 (0, _) -> (te, 0, context)
726 | (n, C.Lambda (name,so,ta)) when n > 0 ->
727 let (te, k, context') =
728 eat_lambdas ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
730 (te, k + 1, context')
732 raise (AssertFailure (lazy (sprintf "9 (%d, %s)" n (CicPp.ppterm te))))
734 (*CSC: Tutto quello che segue e' l'intuzione di luca ;-) *)
735 and check_is_really_smaller_arg ~subst context n nn kl x safes te =
736 (*CSC: forse la whd si puo' fare solo quando serve veramente. *)
737 (*CSC: cfr guarded_by_destructors *)
738 let module C = Cic in
739 let module U = UriManager in
740 match CicReduction.whd ~subst context te with
741 C.Rel m when List.mem m safes -> true
748 (* | C.Cast (te,ty) ->
749 check_is_really_smaller_arg ~subst n nn kl x safes te &&
750 check_is_really_smaller_arg ~subst n nn kl x safes ty*)
751 (* | C.Prod (_,so,ta) ->
752 check_is_really_smaller_arg ~subst n nn kl x safes so &&
753 check_is_really_smaller_arg ~subst (n+1) (nn+1) kl (x+1)
754 (List.map (fun x -> x + 1) safes) ta*)
755 | C.Prod _ -> raise (AssertFailure (lazy "10"))
756 | C.Lambda (name,so,ta) ->
757 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
758 check_is_really_smaller_arg ~subst ((Some (name,(C.Decl so)))::context)
759 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
760 | C.LetIn (name,so,ta) ->
761 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
762 check_is_really_smaller_arg ~subst ((Some (name,(C.Def (so,None))))::context)
763 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
765 (*CSC: sulla coda ci vogliono dei controlli? secondo noi no, ma *)
766 (*CSC: solo perche' non abbiamo trovato controesempi *)
767 check_is_really_smaller_arg ~subst context n nn kl x safes he
768 | C.Appl [] -> raise (AssertFailure (lazy "11"))
770 | C.MutInd _ -> raise (AssertFailure (lazy "12"))
771 | C.MutConstruct _ -> false
772 | C.MutCase (uri,i,outtype,term,pl) ->
774 C.Rel m when List.mem m safes || m = x ->
775 let (tys,len,isinductive,paramsno,cl) =
776 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
778 C.InductiveDefinition (tl,_,paramsno,_) ->
781 (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) tl
783 let (_,isinductive,_,cl) = List.nth tl i in
787 (id, snd (split_prods ~subst tys paramsno ty))) cl
789 (tys,List.length tl,isinductive,paramsno,cl')
791 raise (TypeCheckerFailure
792 (lazy ("Unknown mutual inductive definition:" ^
793 UriManager.string_of_uri uri)))
795 if not isinductive then
798 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
805 Invalid_argument _ ->
806 raise (TypeCheckerFailure (lazy "not enough patterns"))
811 let debrujinedte = debrujin_constructor uri len c in
812 recursive_args tys 0 len debrujinedte
814 let (e,safes',n',nn',x',context') =
815 get_new_safes ~subst context p c rl' safes n nn x
818 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
820 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
821 let (tys,len,isinductive,paramsno,cl) =
822 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
824 C.InductiveDefinition (tl,_,paramsno,_) ->
825 let (_,isinductive,_,cl) = List.nth tl i in
827 List.map (fun (n,_,ty,_) ->
828 Some(Cic.Name n,(Cic.Decl ty))) tl
833 (id, snd (split_prods ~subst tys paramsno ty))) cl
835 (tys,List.length tl,isinductive,paramsno,cl')
837 raise (TypeCheckerFailure
838 (lazy ("Unknown mutual inductive definition:" ^
839 UriManager.string_of_uri uri)))
841 if not isinductive then
844 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
851 Invalid_argument _ ->
852 raise (TypeCheckerFailure (lazy "not enough patterns"))
854 (*CSC: supponiamo come prima che nessun controllo sia necessario*)
855 (*CSC: sugli argomenti di una applicazione *)
859 let debrujinedte = debrujin_constructor uri len c in
860 recursive_args tys 0 len debrujinedte
862 let (e, safes',n',nn',x',context') =
863 get_new_safes ~subst context p c rl' safes n nn x
866 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
871 i && check_is_really_smaller_arg ~subst context n nn kl x safes p
875 let len = List.length fl in
876 let n_plus_len = n + len
877 and nn_plus_len = nn + len
878 and x_plus_len = x + len
879 and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
880 and safes' = List.map (fun x -> x + len) safes in
882 (fun (_,_,ty,bo) i ->
884 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
888 let len = List.length fl in
889 let n_plus_len = n + len
890 and nn_plus_len = nn + len
891 and x_plus_len = x + len
892 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
893 and safes' = List.map (fun x -> x + len) safes in
897 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
901 and guarded_by_destructors ~subst context n nn kl x safes =
902 let module C = Cic in
903 let module U = UriManager in
905 C.Rel m when m > n && m <= nn -> false
907 (match List.nth context (n-1) with
908 Some (_,C.Decl _) -> true
909 | Some (_,C.Def (bo,_)) ->
910 guarded_by_destructors ~subst context m nn kl x safes
911 (CicSubstitution.lift m bo)
912 | None -> raise (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
916 | C.Implicit _ -> true
918 guarded_by_destructors ~subst context n nn kl x safes te &&
919 guarded_by_destructors ~subst context n nn kl x safes ty
920 | C.Prod (name,so,ta) ->
921 guarded_by_destructors ~subst context n nn kl x safes so &&
922 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
923 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
924 | C.Lambda (name,so,ta) ->
925 guarded_by_destructors ~subst context n nn kl x safes so &&
926 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
927 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
928 | C.LetIn (name,so,ta) ->
929 guarded_by_destructors ~subst context n nn kl x safes so &&
930 guarded_by_destructors ~subst ((Some (name,(C.Def (so,None))))::context)
931 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
932 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
933 let k = List.nth kl (m - n - 1) in
934 if not (List.length tl > k) then false
938 i && guarded_by_destructors ~subst context n nn kl x safes param
940 check_is_really_smaller_arg ~subst context n nn kl x safes (List.nth tl k)
943 (fun t i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
945 | C.Var (_,exp_named_subst)
946 | C.Const (_,exp_named_subst)
947 | C.MutInd (_,_,exp_named_subst)
948 | C.MutConstruct (_,_,_,exp_named_subst) ->
950 (fun (_,t) i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
952 | C.MutCase (uri,i,outtype,term,pl) ->
953 (match CicReduction.whd ~subst context term with
954 C.Rel m when List.mem m safes || m = x ->
955 let (lefts_and_tys,len,isinductive,paramsno,cl) =
956 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
958 C.InductiveDefinition (tl,_,paramsno,_) ->
959 let len = List.length tl in
960 let (_,isinductive,_,cl) = List.nth tl i in
962 List.map (fun (n,_,ty,_) ->
963 Some(Cic.Name n,(Cic.Decl ty))) tl
968 let debrujinedty = debrujin_constructor uri len ty in
969 (id, snd (split_prods ~subst tys paramsno ty),
970 snd (split_prods ~subst tys paramsno debrujinedty)
976 fst (split_prods ~subst [] paramsno ty)
978 (tys@lefts,len,isinductive,paramsno,cl')
980 raise (TypeCheckerFailure
981 (lazy ("Unknown mutual inductive definition:" ^
982 UriManager.string_of_uri uri)))
984 if not isinductive then
985 guarded_by_destructors ~subst context n nn kl x safes outtype &&
986 guarded_by_destructors ~subst context n nn kl x safes term &&
987 (*CSC: manca ??? il controllo sul tipo di term? *)
990 i && guarded_by_destructors ~subst context n nn kl x safes p)
997 Invalid_argument _ ->
998 raise (TypeCheckerFailure (lazy "not enough patterns"))
1000 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1001 (*CSC: manca ??? il controllo sul tipo di term? *)
1003 (fun (p,(_,c,brujinedc)) i ->
1004 let rl' = recursive_args lefts_and_tys 0 len brujinedc in
1005 let (e,safes',n',nn',x',context') =
1006 get_new_safes ~subst context p c rl' safes n nn x
1009 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1011 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
1012 let (lefts_and_tys,len,isinductive,paramsno,cl) =
1013 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1015 C.InductiveDefinition (tl,_,paramsno,_) ->
1016 let (_,isinductive,_,cl) = List.nth tl i in
1019 (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
1024 (id, snd (split_prods ~subst tys paramsno ty))) cl in
1029 fst (split_prods ~subst [] paramsno ty)
1031 (tys@lefts,List.length tl,isinductive,paramsno,cl')
1033 raise (TypeCheckerFailure
1034 (lazy ("Unknown mutual inductive definition:" ^
1035 UriManager.string_of_uri uri)))
1037 if not isinductive then
1038 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1039 guarded_by_destructors ~subst context n nn kl x safes term &&
1040 (*CSC: manca ??? il controllo sul tipo di term? *)
1043 i && guarded_by_destructors ~subst context n nn kl x safes p)
1050 Invalid_argument _ ->
1051 raise (TypeCheckerFailure (lazy "not enough patterns"))
1053 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1054 (*CSC: manca ??? il controllo sul tipo di term? *)
1057 i && guarded_by_destructors ~subst context n nn kl x safes t)
1062 let debrujinedte = debrujin_constructor uri len c in
1063 recursive_args lefts_and_tys 0 len debrujinedte
1065 let (e, safes',n',nn',x',context') =
1066 get_new_safes ~subst context p c rl' safes n nn x
1069 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1072 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1073 guarded_by_destructors ~subst context n nn kl x safes term &&
1074 (*CSC: manca ??? il controllo sul tipo di term? *)
1076 (fun p i -> i && guarded_by_destructors ~subst context n nn kl x safes p)
1080 let len = List.length fl in
1081 let n_plus_len = n + len
1082 and nn_plus_len = nn + len
1083 and x_plus_len = x + len
1084 and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
1085 and safes' = List.map (fun x -> x + len) safes in
1087 (fun (_,_,ty,bo) i ->
1088 i && guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1089 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1090 x_plus_len safes' bo
1092 | C.CoFix (_, fl) ->
1093 let len = List.length fl in
1094 let n_plus_len = n + len
1095 and nn_plus_len = nn + len
1096 and x_plus_len = x + len
1097 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
1098 and safes' = List.map (fun x -> x + len) safes in
1102 guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1103 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1104 x_plus_len safes' bo
1107 (* the boolean h means already protected *)
1108 (* args is the list of arguments the type of the constructor that may be *)
1109 (* found in head position must be applied to. *)
1110 and guarded_by_constructors ~subst context n nn h te args coInductiveTypeURI =
1111 let module C = Cic in
1112 (*CSC: There is a lot of code replication between the cases X and *)
1113 (*CSC: (C.Appl X tl). Maybe it will be better to define a function *)
1114 (*CSC: that maps X into (C.Appl X []) when X is not already a C.Appl *)
1115 match CicReduction.whd ~subst context te with
1116 C.Rel m when m > n && m <= nn -> h
1124 (* the term has just been type-checked *)
1125 raise (AssertFailure (lazy "17"))
1126 | C.Lambda (name,so,de) ->
1127 does_not_occur ~subst context n nn so &&
1128 guarded_by_constructors ~subst ((Some (name,(C.Decl so)))::context)
1129 (n + 1) (nn + 1) h de args coInductiveTypeURI
1130 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
1132 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) tl true
1133 | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
1137 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1138 with Not_found -> assert false
1141 C.InductiveDefinition (itl,_,_,_) ->
1142 let (_,_,_,cl) = List.nth itl i in
1143 let (_,cons) = List.nth cl (j - 1) in
1144 CicSubstitution.subst_vars exp_named_subst cons
1146 raise (TypeCheckerFailure
1147 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
1149 let rec analyse_branch context ty te =
1150 match CicReduction.whd ~subst context ty with
1151 C.Meta _ -> raise (AssertFailure (lazy "34"))
1155 does_not_occur ~subst context n nn te
1158 raise (AssertFailure (lazy "24"))(* due to type-checking *)
1159 | C.Prod (name,so,de) ->
1160 analyse_branch ((Some (name,(C.Decl so)))::context) de te
1163 raise (AssertFailure (lazy "25"))(* due to type-checking *)
1164 | C.Appl ((C.MutInd (uri,_,_))::_) when uri == coInductiveTypeURI ->
1165 guarded_by_constructors ~subst context n nn true te []
1167 | C.Appl ((C.MutInd (uri,_,_))::_) ->
1168 guarded_by_constructors ~subst context n nn true te tl
1171 does_not_occur ~subst context n nn te
1172 | C.Const _ -> raise (AssertFailure (lazy "26"))
1173 | C.MutInd (uri,_,_) when uri == coInductiveTypeURI ->
1174 guarded_by_constructors ~subst context n nn true te []
1177 does_not_occur ~subst context n nn te
1178 | C.MutConstruct _ -> raise (AssertFailure (lazy "27"))
1179 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1180 (*CSC: in head position. *)
1184 raise (AssertFailure (lazy "28"))(* due to type-checking *)
1186 let rec analyse_instantiated_type context ty l =
1187 match CicReduction.whd ~subst context ty with
1193 | C.Cast _ -> raise (AssertFailure (lazy "29"))(* due to type-checking *)
1194 | C.Prod (name,so,de) ->
1199 analyse_branch context so he &&
1200 analyse_instantiated_type
1201 ((Some (name,(C.Decl so)))::context) de tl
1205 raise (AssertFailure (lazy "30"))(* due to type-checking *)
1208 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1209 | C.Const _ -> raise (AssertFailure (lazy "31"))
1212 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1213 | C.MutConstruct _ -> raise (AssertFailure (lazy "32"))
1214 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1215 (*CSC: in head position. *)
1219 raise (AssertFailure (lazy "33"))(* due to type-checking *)
1221 let rec instantiate_type args consty =
1224 | tlhe::tltl as l ->
1225 let consty' = CicReduction.whd ~subst context consty in
1231 let instantiated_de = CicSubstitution.subst he de in
1232 (*CSC: siamo sicuri che non sia troppo forte? *)
1233 does_not_occur ~subst context n nn tlhe &
1234 instantiate_type tl instantiated_de tltl
1236 (*CSC:We do not consider backbones with a MutCase, a *)
1237 (*CSC:FixPoint, a CoFixPoint and so on in head position.*)
1238 raise (AssertFailure (lazy "23"))
1240 | [] -> analyse_instantiated_type context consty' l
1241 (* These are all the other cases *)
1243 instantiate_type args consty tl
1244 | C.Appl ((C.CoFix (_,fl))::tl) ->
1245 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1246 let len = List.length fl in
1247 let n_plus_len = n + len
1248 and nn_plus_len = nn + len
1249 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1250 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
1253 i && does_not_occur ~subst context n nn ty &&
1254 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1255 h bo args coInductiveTypeURI
1257 | C.Appl ((C.MutCase (_,_,out,te,pl))::tl) ->
1258 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1259 does_not_occur ~subst context n nn out &&
1260 does_not_occur ~subst context n nn te &&
1264 guarded_by_constructors ~subst context n nn h x args
1268 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
1269 | C.Var (_,exp_named_subst)
1270 | C.Const (_,exp_named_subst) ->
1272 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1273 | C.MutInd _ -> assert false
1274 | C.MutConstruct (_,_,_,exp_named_subst) ->
1276 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1277 | C.MutCase (_,_,out,te,pl) ->
1278 does_not_occur ~subst context n nn out &&
1279 does_not_occur ~subst context n nn te &&
1283 guarded_by_constructors ~subst context n nn h x args
1287 let len = List.length fl in
1288 let n_plus_len = n + len
1289 and nn_plus_len = nn + len
1290 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1291 and tys = List.map (fun (n,_,ty,_)-> Some (C.Name n,(C.Decl ty))) fl in
1293 (fun (_,_,ty,bo) i ->
1294 i && does_not_occur ~subst context n nn ty &&
1295 does_not_occur ~subst (tys@context) n_plus_len nn_plus_len bo
1298 let len = List.length fl in
1299 let n_plus_len = n + len
1300 and nn_plus_len = nn + len
1301 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1302 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
1305 i && does_not_occur ~subst context n nn ty &&
1306 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1308 args coInductiveTypeURI
1311 and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1312 need_dummy ind arity1 arity2 ugraph =
1313 let module C = Cic in
1314 let module U = UriManager in
1315 let arity1 = CicReduction.whd ~subst context arity1 in
1316 let rec check_allowed_sort_elimination_aux ugraph context arity2 need_dummy =
1317 match arity1, CicReduction.whd ~subst context arity2 with
1318 (C.Prod (_,so1,de1), C.Prod (_,so2,de2)) ->
1320 CicReduction.are_convertible ~subst ~metasenv context so1 so2 ugraph in
1322 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1323 need_dummy (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1327 | (C.Sort _, C.Prod (name,so,ta)) when not need_dummy ->
1329 CicReduction.are_convertible ~subst ~metasenv context so ind ugraph in
1333 check_allowed_sort_elimination_aux ugraph1
1334 ((Some (name,C.Decl so))::context) ta true
1335 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
1336 | (C.Sort C.Prop, C.Sort C.Set)
1337 | (C.Sort C.Prop, C.Sort C.CProp)
1338 | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
1339 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1341 C.InductiveDefinition (itl,_,paramsno,_) ->
1342 let itl_len = List.length itl in
1343 let (name,_,ty,cl) = List.nth itl i in
1344 let cl_len = List.length cl in
1345 if (cl_len = 0 || (itl_len = 1 && cl_len = 1)) then
1346 let non_informative,ugraph =
1347 if cl_len = 0 then true,ugraph
1349 is_non_informative ~logger [Some (C.Name name,C.Decl ty)]
1350 paramsno (snd (List.nth cl 0)) ugraph
1352 (* is it a singleton or empty non recursive and non informative
1354 non_informative, ugraph
1358 raise (TypeCheckerFailure
1359 (lazy ("Unknown mutual inductive definition:" ^
1360 UriManager.string_of_uri uri)))
1362 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
1363 | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
1364 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
1365 | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
1366 | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
1367 | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
1368 | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
1370 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1372 C.InductiveDefinition (itl,_,paramsno,_) ->
1374 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1376 let (_,_,_,cl) = List.nth itl i in
1378 (fun (_,x) (i,ugraph) ->
1380 is_small ~logger tys paramsno x ugraph
1385 raise (TypeCheckerFailure
1386 (lazy ("Unknown mutual inductive definition:" ^
1387 UriManager.string_of_uri uri)))
1389 | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
1390 | (_,_) -> false,ugraph
1392 check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
1394 and type_of_branch ~subst context argsno need_dummy outtype term constype =
1395 let module C = Cic in
1396 let module R = CicReduction in
1397 match R.whd ~subst context constype with
1402 C.Appl [outtype ; term]
1403 | C.Appl (C.MutInd (_,_,_)::tl) ->
1404 let (_,arguments) = split tl argsno
1406 if need_dummy && arguments = [] then
1409 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1410 | C.Prod (name,so,de) ->
1412 match CicSubstitution.lift 1 term with
1413 C.Appl l -> C.Appl (l@[C.Rel 1])
1414 | t -> C.Appl [t ; C.Rel 1]
1416 C.Prod (name,so,type_of_branch ~subst
1417 ((Some (name,(C.Decl so)))::context) argsno need_dummy
1418 (CicSubstitution.lift 1 outtype) term' de)
1419 | _ -> raise (AssertFailure (lazy "20"))
1421 (* check_metasenv_consistency checks that the "canonical" context of a
1422 metavariable is consitent - up to relocation via the relocation list l -
1423 with the actual context *)
1426 and check_metasenv_consistency ~logger ~subst metasenv context
1427 canonical_context l ugraph
1429 let module C = Cic in
1430 let module R = CicReduction in
1431 let module S = CicSubstitution in
1432 let lifted_canonical_context =
1436 | (Some (n,C.Decl t))::tl ->
1437 (Some (n,C.Decl (S.subst_meta l (S.lift i t))))::(aux (i+1) tl)
1438 | (Some (n,C.Def (t,None)))::tl ->
1439 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),None)))::(aux (i+1) tl)
1440 | None::tl -> None::(aux (i+1) tl)
1441 | (Some (n,C.Def (t,Some ty)))::tl ->
1442 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),Some (S.subst_meta l (S.lift i ty)))))::(aux (i+1) tl)
1444 aux 1 canonical_context
1450 | Some t,Some (_,C.Def (ct,_)) ->
1452 R.are_convertible ~subst ~metasenv context t ct ugraph
1457 (lazy (sprintf "Not well typed metavariable local context: expected a term convertible with %s, found %s" (CicPp.ppterm ct) (CicPp.ppterm t))))
1460 | Some t,Some (_,C.Decl ct) ->
1461 let type_t,ugraph1 =
1462 type_of_aux' ~logger ~subst metasenv context t ugraph
1465 R.are_convertible ~subst ~metasenv context type_t ct ugraph1
1468 raise (TypeCheckerFailure
1469 (lazy (sprintf "Not well typed metavariable local context: expected a term of type %s, found %s of type %s"
1470 (CicPp.ppterm ct) (CicPp.ppterm t)
1471 (CicPp.ppterm type_t))))
1475 raise (TypeCheckerFailure
1476 (lazy ("Not well typed metavariable local context: "^
1477 "an hypothesis, that is not hidden, is not instantiated")))
1478 ) ugraph l lifted_canonical_context
1482 type_of_aux' is just another name (with a different scope)
1486 and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
1487 let rec type_of_aux ~logger context t ugraph =
1488 let module C = Cic in
1489 let module R = CicReduction in
1490 let module S = CicSubstitution in
1491 let module U = UriManager in
1495 match List.nth context (n - 1) with
1496 Some (_,C.Decl t) -> S.lift n t,ugraph
1497 | Some (_,C.Def (_,Some ty)) -> S.lift n ty,ugraph
1498 | Some (_,C.Def (bo,None)) ->
1499 debug_print (lazy "##### CASO DA INVESTIGARE E CAPIRE") ;
1500 type_of_aux ~logger context (S.lift n bo) ugraph
1502 (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
1505 raise (TypeCheckerFailure (lazy "unbound variable"))
1507 | C.Var (uri,exp_named_subst) ->
1510 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1512 let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
1513 let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
1518 let (canonical_context,term,ty) = CicUtil.lookup_subst n subst in
1520 check_metasenv_consistency ~logger
1521 ~subst metasenv context canonical_context l ugraph
1523 (* assuming subst is well typed !!!!! *)
1524 ((CicSubstitution.subst_meta l ty), ugraph1)
1525 (* type_of_aux context (CicSubstitution.subst_meta l term) *)
1526 with CicUtil.Subst_not_found _ ->
1527 let (_,canonical_context,ty) = CicUtil.lookup_meta n metasenv in
1529 check_metasenv_consistency ~logger
1530 ~subst metasenv context canonical_context l ugraph
1532 ((CicSubstitution.subst_meta l ty),ugraph1))
1533 (* TASSI: CONSTRAINTS *)
1534 | C.Sort (C.Type t) ->
1535 let t' = CicUniv.fresh() in
1536 let ugraph1 = CicUniv.add_gt t' t ugraph in
1537 (C.Sort (C.Type t')),ugraph1
1538 (* TASSI: CONSTRAINTS *)
1539 | C.Sort s -> (C.Sort (C.Type (CicUniv.fresh ()))),ugraph
1540 | C.Implicit _ -> raise (AssertFailure (lazy "21"))
1541 | C.Cast (te,ty) as t ->
1542 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1543 let ty_te,ugraph2 = type_of_aux ~logger context te ugraph1 in
1545 R.are_convertible ~subst ~metasenv context ty_te ty ugraph2
1550 raise (TypeCheckerFailure
1551 (lazy (sprintf "Invalid cast %s" (CicPp.ppterm t))))
1552 | C.Prod (name,s,t) ->
1553 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1555 type_of_aux ~logger ((Some (name,(C.Decl s)))::context) t ugraph1
1557 sort_of_prod ~subst context (name,s) (sort1,sort2) ugraph2
1558 | C.Lambda (n,s,t) ->
1559 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1560 (match R.whd ~subst context sort1 with
1565 (TypeCheckerFailure (lazy (sprintf
1566 "Not well-typed lambda-abstraction: the source %s should be a type; instead it is a term of type %s" (CicPp.ppterm s)
1567 (CicPp.ppterm sort1))))
1570 type_of_aux ~logger ((Some (n,(C.Decl s)))::context) t ugraph1
1572 (C.Prod (n,s,type2)),ugraph2
1573 | C.LetIn (n,s,t) ->
1574 (* only to check if s is well-typed *)
1575 let ty,ugraph1 = type_of_aux ~logger context s ugraph in
1576 (* The type of a LetIn is a LetIn. Extremely slow since the computed
1577 LetIn is later reduced and maybe also re-checked.
1578 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t))
1580 (* The type of the LetIn is reduced. Much faster than the previous
1581 solution. Moreover the inferred type is probably very different
1582 from the expected one.
1583 (CicReduction.whd ~subst context
1584 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)))
1586 (* One-step LetIn reduction. Even faster than the previous solution.
1587 Moreover the inferred type is closer to the expected one. *)
1590 ((Some (n,(C.Def (s,Some ty))))::context) t ugraph1
1592 (CicSubstitution.subst ~avoid_beta_redexes:true s ty1),ugraph2
1593 | C.Appl (he::tl) when List.length tl > 0 ->
1594 let hetype,ugraph1 = type_of_aux ~logger context he ugraph in
1595 let tlbody_and_type,ugraph2 =
1598 let ty,ugraph1 = type_of_aux ~logger context x ugraph in
1599 let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in
1600 ((x,ty)::l,ugraph1))
1603 (* TASSI: questa c'era nel mio... ma non nel CVS... *)
1604 (* let _,ugraph2 = type_of_aux context hetype ugraph2 in *)
1605 eat_prods ~subst context hetype tlbody_and_type ugraph2
1606 | C.Appl _ -> raise (AssertFailure (lazy "Appl: no arguments"))
1607 | C.Const (uri,exp_named_subst) ->
1610 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1612 let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
1614 CicSubstitution.subst_vars exp_named_subst cty
1618 | C.MutInd (uri,i,exp_named_subst) ->
1621 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1623 (* TASSI: da me c'era anche questa, ma in CVS no *)
1624 let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
1625 (* fine parte dubbia *)
1627 CicSubstitution.subst_vars exp_named_subst mty
1631 | C.MutConstruct (uri,i,j,exp_named_subst) ->
1633 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1635 (* TASSI: idem come sopra *)
1637 type_of_mutual_inductive_constr ~logger uri i j ugraph1
1640 CicSubstitution.subst_vars exp_named_subst mty
1643 | C.MutCase (uri,i,outtype,term,pl) ->
1644 let outsort,ugraph1 = type_of_aux ~logger context outtype ugraph in
1645 let (need_dummy, k) =
1646 let rec guess_args context t =
1647 let outtype = CicReduction.whd ~subst context t in
1649 C.Sort _ -> (true, 0)
1650 | C.Prod (name, s, t) ->
1652 guess_args ((Some (name,(C.Decl s)))::context) t in
1654 (* last prod before sort *)
1655 match CicReduction.whd ~subst context s with
1656 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1657 C.MutInd (uri',i',_) when U.eq uri' uri && i' = i ->
1659 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1660 | C.Appl ((C.MutInd (uri',i',_)) :: _)
1661 when U.eq uri' uri && i' = i -> (false, 1)
1669 "Malformed case analasys' output type %s"
1670 (CicPp.ppterm outtype))))
1673 let (parameters, arguments, exp_named_subst),ugraph2 =
1674 let ty,ugraph2 = type_of_aux context term ugraph1 in
1675 match R.whd ~subst context ty with
1676 (*CSC manca il caso dei CAST *)
1677 (*CSC: ma servono i parametri (uri,i)? Se si', perche' non serve anche il *)
1678 (*CSC: parametro exp_named_subst? Se no, perche' non li togliamo? *)
1679 (*CSC: Hint: nella DTD servono per gli stylesheet. *)
1680 C.MutInd (uri',i',exp_named_subst) as typ ->
1681 if U.eq uri uri' && i = i' then
1682 ([],[],exp_named_subst),ugraph2
1687 ("Case analysys: analysed term type is %s, but is expected to be (an application of) %s#1/%d{_}")
1688 (CicPp.ppterm typ) (U.string_of_uri uri) i)))
1690 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1691 if U.eq uri uri' && i = i' then
1693 split tl (List.length tl - k)
1694 in (params,args,exp_named_subst),ugraph2
1699 ("Case analysys: analysed term type is %s, "^
1700 "but is expected to be (an application of) "^
1702 (CicPp.ppterm typ') (U.string_of_uri uri) i)))
1708 "analysed term %s is not an inductive one")
1709 (CicPp.ppterm term))))
1711 let (b, k) = guess_args context outsort in
1712 if not b then (b, k - 1) else (b, k) in
1713 let (parameters, arguments, exp_named_subst),ugraph2 =
1714 let ty,ugraph2 = type_of_aux ~logger context term ugraph1 in
1715 match R.whd ~subst context ty with
1716 C.MutInd (uri',i',exp_named_subst) as typ ->
1717 if U.eq uri uri' && i = i' then
1718 ([],[],exp_named_subst),ugraph2
1722 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1723 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1724 | C.Appl ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) ->
1725 if U.eq uri uri' && i = i' then
1727 split tl (List.length tl - k)
1728 in (params,args,exp_named_subst),ugraph2
1732 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1733 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1738 "Case analysis: analysed term %s is not an inductive one"
1739 (CicPp.ppterm term))))
1742 let's control if the sort elimination is allowed:
1745 let sort_of_ind_type =
1746 if parameters = [] then
1747 C.MutInd (uri,i,exp_named_subst)
1749 C.Appl ((C.MutInd (uri,i,exp_named_subst))::parameters)
1751 let type_of_sort_of_ind_ty,ugraph3 =
1752 type_of_aux ~logger context sort_of_ind_type ugraph2 in
1754 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1755 need_dummy sort_of_ind_type type_of_sort_of_ind_ty outsort ugraph3
1759 (TypeCheckerFailure (lazy ("Case analasys: sort elimination not allowed")));
1760 (* let's check if the type of branches are right *)
1764 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1765 with Not_found -> assert false
1768 C.InductiveDefinition (_,_,parsno,_) -> parsno
1770 raise (TypeCheckerFailure
1771 (lazy ("Unknown mutual inductive definition:" ^
1772 UriManager.string_of_uri uri)))
1774 let (_,branches_ok,ugraph5) =
1776 (fun (j,b,ugraph) p ->
1779 if parameters = [] then
1780 (C.MutConstruct (uri,i,j,exp_named_subst))
1783 (C.MutConstruct (uri,i,j,exp_named_subst)::parameters))
1785 let ty_p,ugraph1 = type_of_aux ~logger context p ugraph in
1786 let ty_cons,ugraph3 = type_of_aux ~logger context cons ugraph1 in
1789 type_of_branch ~subst context parsno need_dummy outtype cons
1793 ~subst ~metasenv context ty_p ty_branch ugraph3
1798 prerr_endline ("\n!OUTTYPE= " ^ CicPp.ppterm outtype);
1799 prerr_endline ("!CONS= " ^ CicPp.ppterm cons);
1800 prerr_endline ("!TY_CONS= " ^ CicPp.ppterm ty_cons);
1801 prerr_endline ("#### " ^ CicPp.ppterm ty_p ^ "\n<==>\n" ^ CicPp.ppterm ty_branch);
1806 ("#### " ^ CicPp.ppterm ty_p ^
1807 " <==> " ^ CicPp.ppterm ty_branch));
1811 ) (1,true,ugraph4) pl
1813 if not branches_ok then
1815 (TypeCheckerFailure (lazy "Case analysys: wrong branch type"));
1817 if not need_dummy then outtype::arguments@[term]
1818 else outtype::arguments in
1820 if need_dummy && arguments = [] then outtype
1821 else CicReduction.head_beta_reduce (C.Appl arguments')
1825 let types_times_kl,ugraph1 =
1826 (* WAS: list rev list map *)
1828 (fun (l,ugraph) (n,k,ty,_) ->
1829 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1830 ((Some (C.Name n,(C.Decl ty)),k)::l,ugraph1)
1833 let (types,kl) = List.split types_times_kl in
1834 let len = List.length types in
1837 (fun ugraph (name,x,ty,bo) ->
1839 type_of_aux ~logger (types@context) bo ugraph
1842 R.are_convertible ~subst ~metasenv (types@context)
1843 ty_bo (CicSubstitution.lift len ty) ugraph1 in
1846 let (m, eaten, context') =
1847 eat_lambdas ~subst (types @ context) (x + 1) bo
1850 let's control the guarded by
1851 destructors conditions D{f,k,x,M}
1853 if not (guarded_by_destructors ~subst context' eaten
1854 (len + eaten) kl 1 [] m) then
1857 (lazy ("Fix: not guarded by destructors")))
1862 raise (TypeCheckerFailure (lazy ("Fix: ill-typed bodies")))
1864 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1865 let (_,_,ty,_) = List.nth fl i in
1870 (fun (l,ugraph) (n,ty,_) ->
1872 type_of_aux ~logger context ty ugraph in
1873 (Some (C.Name n,(C.Decl ty))::l,ugraph1)
1876 let len = List.length types in
1879 (fun ugraph (_,ty,bo) ->
1881 type_of_aux ~logger (types @ context) bo ugraph
1884 R.are_convertible ~subst ~metasenv (types @ context) ty_bo
1885 (CicSubstitution.lift len ty) ugraph1
1889 (* let's control that the returned type is coinductive *)
1890 match returns_a_coinductive ~subst context ty with
1894 (lazy "CoFix: does not return a coinductive type"))
1897 let's control the guarded by constructors
1900 if not (guarded_by_constructors ~subst
1901 (types @ context) 0 len false bo [] uri) then
1904 (lazy "CoFix: not guarded by constructors"))
1910 (TypeCheckerFailure (lazy "CoFix: ill-typed bodies"))
1913 let (_,ty,_) = List.nth fl i in
1916 and check_exp_named_subst ~logger ~subst context ugraph =
1917 let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
1920 | ((uri,t) as item)::tl ->
1921 let ty_uri,ugraph1 = type_of_variable ~logger uri ugraph in
1923 CicSubstitution.subst_vars esubsts ty_uri in
1924 let typeoft,ugraph2 = type_of_aux ~logger context t ugraph1 in
1926 CicReduction.are_convertible ~subst ~metasenv
1927 context typeoft typeofvar ugraph2
1930 check_exp_named_subst_aux ~logger (esubsts@[item]) tl ugraph3
1933 CicReduction.fdebug := 0 ;
1935 (CicReduction.are_convertible
1936 ~subst ~metasenv context typeoft typeofvar ugraph2) ;
1938 debug typeoft [typeofvar] ;
1939 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
1942 check_exp_named_subst_aux ~logger [] ugraph
1944 and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
1945 let module C = Cic in
1946 let t1' = CicReduction.whd ~subst context t1 in
1947 let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
1948 match (t1', t2') with
1949 (C.Sort s1, C.Sort s2)
1950 when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) ->
1951 (* different from Coq manual!!! *)
1953 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
1954 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1955 let t' = CicUniv.fresh() in
1956 let ugraph1 = CicUniv.add_ge t' t1 ugraph in
1957 let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
1958 C.Sort (C.Type t'),ugraph2
1959 | (C.Sort _,C.Sort (C.Type t1)) ->
1960 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1961 C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
1962 | (C.Meta _, C.Sort _) -> t2',ugraph
1963 | (C.Meta _, (C.Meta (_,_) as t))
1964 | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
1966 | (_,_) -> raise (TypeCheckerFailure (lazy (sprintf
1967 "Prod: expected two sorts, found = %s, %s" (CicPp.ppterm t1')
1968 (CicPp.ppterm t2'))))
1970 and eat_prods ~subst context hetype l ugraph =
1971 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
1975 | (hete, hety)::tl ->
1976 (match (CicReduction.whd ~subst context hetype) with
1979 CicReduction.are_convertible
1980 ~subst ~metasenv context hety s ugraph
1984 CicReduction.fdebug := -1 ;
1985 eat_prods ~subst context
1986 (CicSubstitution.subst ~avoid_beta_redexes:true hete t)
1988 (*TASSI: not sure *)
1992 CicReduction.fdebug := 0 ;
1993 ignore (CicReduction.are_convertible
1994 ~subst ~metasenv context s hety ugraph) ;
2000 ("Appl: wrong parameter-type, expected %s, found %s")
2001 (CicPp.ppterm hetype) (CicPp.ppterm s))))
2004 raise (TypeCheckerFailure
2005 (lazy "Appl: this is not a function, it cannot be applied"))
2008 and returns_a_coinductive ~subst context ty =
2009 let module C = Cic in
2010 match CicReduction.whd ~subst context ty with
2011 C.MutInd (uri,i,_) ->
2012 (*CSC: definire una funzioncina per questo codice sempre replicato *)
2015 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
2016 with Not_found -> assert false
2019 C.InductiveDefinition (itl,_,_,_) ->
2020 let (_,is_inductive,_,_) = List.nth itl i in
2021 if is_inductive then None else (Some uri)
2023 raise (TypeCheckerFailure
2024 (lazy ("Unknown mutual inductive definition:" ^
2025 UriManager.string_of_uri uri)))
2027 | C.Appl ((C.MutInd (uri,i,_))::_) ->
2028 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
2030 C.InductiveDefinition (itl,_,_,_) ->
2031 let (_,is_inductive,_,_) = List.nth itl i in
2032 if is_inductive then None else (Some uri)
2034 raise (TypeCheckerFailure
2035 (lazy ("Unknown mutual inductive definition:" ^
2036 UriManager.string_of_uri uri)))
2038 | C.Prod (n,so,de) ->
2039 returns_a_coinductive ~subst ((Some (n,C.Decl so))::context) de
2044 debug_print (lazy ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t)) ; flush stderr ;
2047 type_of_aux ~logger context t ugraph
2049 in debug_print (lazy "FINE TYPE_OF_AUX") ; flush stderr ; res
2052 (* is a small constructor? *)
2053 (*CSC: ottimizzare calcolando staticamente *)
2054 and is_small_or_non_informative ~condition ~logger context paramsno c ugraph =
2055 let rec is_small_or_non_informative_aux ~logger context c ugraph =
2056 let module C = Cic in
2057 match CicReduction.whd context c with
2059 let s,ugraph1 = type_of_aux' ~logger [] context so ugraph in
2060 let b = condition s in
2062 is_small_or_non_informative_aux
2063 ~logger ((Some (n,(C.Decl so)))::context) de ugraph1
2066 | _ -> true,ugraph (*CSC: we trust the type-checker *)
2068 let (context',dx) = split_prods ~subst:[] context paramsno c in
2069 is_small_or_non_informative_aux ~logger context' dx ugraph
2071 and is_small ~logger =
2072 is_small_or_non_informative
2073 ~condition:(fun s -> s=Cic.Sort Cic.Prop || s=Cic.Sort Cic.Set)
2076 and is_non_informative ~logger =
2077 is_small_or_non_informative
2078 ~condition:(fun s -> s=Cic.Sort Cic.Prop)
2081 and type_of ~logger t ugraph =
2083 debug_print (lazy ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t)) ; flush stderr ;
2086 type_of_aux' ~logger [] [] t ugraph
2088 in debug_print (lazy "FINE TYPE_OF_AUX'") ; flush stderr ; res
2092 let typecheck_obj0 ~logger uri ugraph =
2093 let module C = Cic in
2095 C.Constant (_,Some te,ty,_,_) ->
2096 let _,ugraph = type_of ~logger ty ugraph in
2097 let ty_te,ugraph = type_of ~logger te ugraph in
2098 let b,ugraph = (CicReduction.are_convertible [] ty_te ty ugraph) in
2100 raise (TypeCheckerFailure
2102 ("the type of the body is not the one expected:\n" ^
2103 CicPp.ppterm ty_te ^ "\nvs\n" ^
2107 | C.Constant (_,None,ty,_,_) ->
2108 (* only to check that ty is well-typed *)
2109 let _,ugraph = type_of ~logger ty ugraph in
2111 | C.CurrentProof (_,conjs,te,ty,_,_) ->
2114 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
2116 type_of_aux' ~logger metasenv context ty ugraph
2118 metasenv @ [conj],ugraph
2121 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
2122 let type_of_te,ugraph =
2123 type_of_aux' ~logger conjs [] te ugraph
2125 let b,ugraph = CicReduction.are_convertible [] type_of_te ty ugraph in
2127 raise (TypeCheckerFailure (lazy (sprintf
2128 "the current proof is not well typed because the type %s of the body is not convertible to the declared type %s"
2129 (CicPp.ppterm type_of_te) (CicPp.ppterm ty))))
2132 | C.Variable (_,bo,ty,_,_) ->
2133 (* only to check that ty is well-typed *)
2134 let _,ugraph = type_of ~logger ty ugraph in
2138 let ty_bo,ugraph = type_of ~logger bo ugraph in
2139 let b,ugraph = CicReduction.are_convertible [] ty_bo ty ugraph in
2141 raise (TypeCheckerFailure
2142 (lazy "the body is not the one expected"))
2146 | (C.InductiveDefinition _ as obj) ->
2147 check_mutual_inductive_defs ~logger uri obj ugraph
2150 let module C = Cic in
2151 let module R = CicReduction in
2152 let module U = UriManager in
2153 let logger = new CicLogger.logger in
2154 (* ??? match CicEnvironment.is_type_checked ~trust:true uri with ???? *)
2155 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2156 CicEnvironment.CheckedObj (cobj,ugraph') ->
2157 (* debug_print (lazy ("NON-INIZIO A TYPECHECKARE " ^ U.string_of_uri uri));*)
2159 | CicEnvironment.UncheckedObj uobj ->
2160 (* let's typecheck the uncooked object *)
2161 logger#log (`Start_type_checking uri) ;
2162 (* debug_print (lazy ("INIZIO A TYPECHECKARE " ^ U.string_of_uri uri)); *)
2163 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph uobj in
2165 CicEnvironment.set_type_checking_info uri;
2166 logger#log (`Type_checking_completed uri);
2167 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
2168 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2169 | _ -> raise CicEnvironmentError
2172 this is raised if set_type_checking_info is called on an object
2173 that has no associated universe file. If we are in univ_maker
2174 phase this is OK since univ_maker will properly commit the
2177 Invalid_argument s ->
2178 (*debug_print (lazy s);*)
2182 let typecheck_obj ~logger uri obj =
2183 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph obj in
2184 let ugraph, univlist, obj = CicUnivUtils.clean_and_fill uri obj ugraph in
2185 CicEnvironment.add_type_checked_obj uri (obj,ugraph,univlist)
2187 (** wrappers which instantiate fresh loggers *)
2189 let profiler = HExtlib.profile "K/CicTypeChecker.type_of_aux'"
2191 let type_of_aux' ?(subst = []) metasenv context t ugraph =
2192 let logger = new CicLogger.logger in
2193 profiler.HExtlib.profile
2194 (type_of_aux' ~logger ~subst metasenv context t) ugraph
2196 let typecheck_obj uri obj =
2197 let logger = new CicLogger.logger in
2198 typecheck_obj ~logger uri obj
2200 (* check_allowed_sort_elimination uri i s1 s2
2201 This function is used outside the kernel to determine in advance whether
2202 a MutCase will be allowed or not.
2203 [uri,i] is the type of the term to match
2204 [s1] is the sort of the term to eliminate (i.e. the head of the arity
2205 of the inductive type [uri,i])
2206 [s2] is the sort of the goal (i.e. the head of the type of the outtype
2208 let check_allowed_sort_elimination uri i s1 s2 =
2209 fst (check_allowed_sort_elimination ~subst:[] ~metasenv:[]
2210 ~logger:(new CicLogger.logger) [] uri i true
2211 (Cic.Implicit None) (* never used *) (Cic.Sort s1) (Cic.Sort s2)
2212 CicUniv.empty_ugraph)