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/.
26 (* TODO factorize functions to frequent errors (e.g. "Unknwon mutual inductive
31 exception AssertFailure of string Lazy.t;;
32 exception TypeCheckerFailure of string Lazy.t;;
36 let rec debug_aux t i =
38 let module U = UriManager in
39 CicPp.ppobj (C.Variable ("DEBUG", None, t, [], [])) ^ "\n" ^ i
42 raise (TypeCheckerFailure (lazy (List.fold_right debug_aux (t::context) "")))
45 let debug_print = fun _ -> () ;;
50 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
52 raise (TypeCheckerFailure (lazy "Parameters number < left parameters number"))
55 let debrujin_constructor uri number_of_types =
59 C.Rel n as t when n <= k -> t
61 raise (TypeCheckerFailure (lazy "unbound variable found in constructor type"))
62 | C.Var (uri,exp_named_subst) ->
63 let exp_named_subst' =
64 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
66 C.Var (uri,exp_named_subst')
68 let l' = List.map (function None -> None | Some t -> Some (aux k t)) l in
71 | C.Implicit _ as t -> t
72 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
73 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k+1) t)
74 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k+1) t)
75 | C.LetIn (n,s,t) -> C.LetIn (n, aux k s, aux (k+1) t)
76 | C.Appl l -> C.Appl (List.map (aux k) l)
77 | C.Const (uri,exp_named_subst) ->
78 let exp_named_subst' =
79 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
81 C.Const (uri,exp_named_subst')
82 | C.MutInd (uri',tyno,exp_named_subst) when UriManager.eq uri uri' ->
83 if exp_named_subst != [] then
84 raise (TypeCheckerFailure
85 (lazy ("non-empty explicit named substitution is applied to "^
86 "a mutual inductive type which is being defined"))) ;
87 C.Rel (k + number_of_types - tyno) ;
88 | C.MutInd (uri',tyno,exp_named_subst) ->
89 let exp_named_subst' =
90 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
92 C.MutInd (uri',tyno,exp_named_subst')
93 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
94 let exp_named_subst' =
95 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
97 C.MutConstruct (uri,tyno,consno,exp_named_subst')
98 | C.MutCase (sp,i,outty,t,pl) ->
99 C.MutCase (sp, i, aux k outty, aux k t,
102 let len = List.length fl in
105 (fun (name, i, ty, bo) -> (name, i, aux k ty, aux (k+len) bo))
110 let len = List.length fl in
113 (fun (name, ty, bo) -> (name, aux k ty, aux (k+len) bo))
116 C.CoFix (i, liftedfl)
121 exception CicEnvironmentError;;
123 let rec type_of_constant ~logger uri ugraph =
124 let module C = Cic in
125 let module R = CicReduction in
126 let module U = UriManager in
128 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
129 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
130 | CicEnvironment.UncheckedObj uobj ->
131 logger#log (`Start_type_checking uri) ;
132 (* let's typecheck the uncooked obj *)
134 (****************************************************************
135 TASSI: FIXME qui e' inutile ricordarselo,
136 tanto poi lo richiediamo alla cache che da quello su disco
137 *****************************************************************)
141 C.Constant (_,Some te,ty,_,_) ->
142 let _,ugraph = type_of ~logger ty ugraph in
143 let type_of_te,ugraph' = type_of ~logger te ugraph in
144 let b',ugraph'' = (R.are_convertible [] type_of_te ty ugraph') in
146 raise (TypeCheckerFailure (lazy (sprintf
147 "the constant %s is not well typed because the type %s of the body is not convertible to the declared type %s"
148 (U.string_of_uri uri) (CicPp.ppterm type_of_te)
152 | C.Constant (_,None,ty,_,_) ->
153 (* only to check that ty is well-typed *)
154 let _,ugraph' = type_of ~logger ty ugraph in
156 | C.CurrentProof (_,conjs,te,ty,_,_) ->
159 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
161 type_of_aux' ~logger metasenv context ty ugraph
163 (metasenv @ [conj],ugraph')
166 let _,ugraph2 = type_of_aux' ~logger conjs [] ty ugraph1 in
167 let type_of_te,ugraph3 =
168 type_of_aux' ~logger conjs [] te ugraph2
170 let b,ugraph4 = (R.are_convertible [] type_of_te ty ugraph3) in
172 raise (TypeCheckerFailure (lazy (sprintf
173 "the current proof %s is not well typed because the type %s of the body is not convertible to the declared type %s"
174 (U.string_of_uri uri) (CicPp.ppterm type_of_te)
180 (TypeCheckerFailure (lazy ("Unknown constant:" ^ U.string_of_uri uri))))
183 CicEnvironment.set_type_checking_info uri;
184 logger#log (`Type_checking_completed uri) ;
185 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
186 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
187 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
188 with Invalid_argument s ->
189 (*debug_print (lazy s);*)
192 match cobj,ugraph with
193 (C.Constant (_,_,ty,_,_)),g -> ty,g
194 | (C.CurrentProof (_,_,_,ty,_,_)),g -> ty,g
196 raise (TypeCheckerFailure (lazy ("Unknown constant:" ^ U.string_of_uri uri)))
198 and type_of_variable ~logger uri ugraph =
199 let module C = Cic in
200 let module R = CicReduction in
201 let module U = UriManager in
202 (* 0 because a variable is never cooked => no partial cooking at one level *)
203 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
204 CicEnvironment.CheckedObj ((C.Variable (_,_,ty,_,_)),ugraph') -> ty,ugraph'
205 | CicEnvironment.UncheckedObj (C.Variable (_,bo,ty,_,_)) ->
206 logger#log (`Start_type_checking uri) ;
207 (* only to check that ty is well-typed *)
208 let _,ugraph1 = type_of ~logger ty ugraph in
213 let ty_bo,ugraph' = type_of ~logger bo ugraph1 in
214 let b,ugraph'' = (R.are_convertible [] ty_bo ty ugraph') in
216 raise (TypeCheckerFailure
217 (lazy ("Unknown variable:" ^ U.string_of_uri uri)))
222 CicEnvironment.set_type_checking_info uri ;
223 logger#log (`Type_checking_completed uri) ;
224 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
225 CicEnvironment.CheckedObj ((C.Variable (_,_,ty,_,_)),ugraph') ->
227 | CicEnvironment.CheckedObj _
228 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
229 with Invalid_argument s ->
230 (*debug_print (lazy s);*)
233 raise (TypeCheckerFailure (lazy ("Unknown variable:" ^ U.string_of_uri uri)))
235 and does_not_occur ?(subst=[]) context n nn te =
236 let module C = Cic in
237 (*CSC: whd sembra essere superflua perche' un caso in cui l'occorrenza *)
238 (*CSC: venga mangiata durante la whd sembra presentare problemi di *)
240 match CicReduction.whd ~subst context te with
241 C.Rel m when m > n && m <= nn -> false
244 | C.Implicit _ -> true
250 | Some x -> i && does_not_occur ~subst context n nn x) l true
252 does_not_occur ~subst context n nn te && does_not_occur ~subst context n nn ty
253 | C.Prod (name,so,dest) ->
254 does_not_occur ~subst context n nn so &&
255 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1)
257 | C.Lambda (name,so,dest) ->
258 does_not_occur ~subst context n nn so &&
259 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1) (nn + 1)
261 | C.LetIn (name,so,dest) ->
262 does_not_occur ~subst context n nn so &&
263 does_not_occur ~subst ((Some (name,(C.Def (so,None))))::context)
264 (n + 1) (nn + 1) dest
266 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
267 | C.Var (_,exp_named_subst)
268 | C.Const (_,exp_named_subst)
269 | C.MutInd (_,_,exp_named_subst)
270 | C.MutConstruct (_,_,_,exp_named_subst) ->
271 List.fold_right (fun (_,x) i -> i && does_not_occur ~subst context n nn x)
273 | C.MutCase (_,_,out,te,pl) ->
274 does_not_occur ~subst context n nn out && does_not_occur ~subst context n nn te &&
275 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) pl true
277 let len = List.length fl in
278 let n_plus_len = n + len in
279 let nn_plus_len = nn + len in
281 List.map (fun (n,_,ty,_) -> Some (C.Name n,(Cic.Decl ty))) fl
284 (fun (_,_,ty,bo) i ->
285 i && does_not_occur ~subst context n nn ty &&
286 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
289 let len = List.length fl in
290 let n_plus_len = n + len in
291 let nn_plus_len = nn + len in
293 List.map (fun (n,ty,_) -> Some (C.Name n,(Cic.Decl ty))) fl
297 i && does_not_occur ~subst context n nn ty &&
298 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
301 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
302 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
303 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
304 (*CSC strictly_positive *)
305 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
306 and weakly_positive context n nn uri te =
307 let module C = Cic in
308 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
310 C.MutInd (HelmLibraryObjects.Datatypes.nat_URI,0,[])
312 (*CSC: mettere in cicSubstitution *)
313 let rec subst_inductive_type_with_dummy_mutind =
315 C.MutInd (uri',0,_) when UriManager.eq uri' uri ->
317 | C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri ->
319 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
320 | C.Prod (name,so,ta) ->
321 C.Prod (name, subst_inductive_type_with_dummy_mutind so,
322 subst_inductive_type_with_dummy_mutind ta)
323 | C.Lambda (name,so,ta) ->
324 C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
325 subst_inductive_type_with_dummy_mutind ta)
327 C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
328 | C.MutCase (uri,i,outtype,term,pl) ->
330 subst_inductive_type_with_dummy_mutind outtype,
331 subst_inductive_type_with_dummy_mutind term,
332 List.map subst_inductive_type_with_dummy_mutind pl)
334 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
335 subst_inductive_type_with_dummy_mutind ty,
336 subst_inductive_type_with_dummy_mutind bo)) fl)
338 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
339 subst_inductive_type_with_dummy_mutind ty,
340 subst_inductive_type_with_dummy_mutind bo)) fl)
341 | C.Const (uri,exp_named_subst) ->
342 let exp_named_subst' =
344 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
347 C.Const (uri,exp_named_subst')
348 | C.MutInd (uri,typeno,exp_named_subst) ->
349 let exp_named_subst' =
351 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
354 C.MutInd (uri,typeno,exp_named_subst')
355 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
356 let exp_named_subst' =
358 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
361 C.MutConstruct (uri,typeno,consno,exp_named_subst')
364 match CicReduction.whd context te with
365 C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri -> true
366 | C.MutInd (uri',0,_) when UriManager.eq uri' uri -> true
367 | C.Prod (C.Anonymous,source,dest) ->
368 strictly_positive context n nn
369 (subst_inductive_type_with_dummy_mutind source) &&
370 weakly_positive ((Some (C.Anonymous,(C.Decl source)))::context)
371 (n + 1) (nn + 1) uri dest
372 | C.Prod (name,source,dest) when
373 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
374 (* dummy abstraction, so we behave as in the anonimous case *)
375 strictly_positive context n nn
376 (subst_inductive_type_with_dummy_mutind source) &&
377 weakly_positive ((Some (name,(C.Decl source)))::context)
378 (n + 1) (nn + 1) uri dest
379 | C.Prod (name,source,dest) ->
380 does_not_occur context n nn
381 (subst_inductive_type_with_dummy_mutind source)&&
382 weakly_positive ((Some (name,(C.Decl source)))::context)
383 (n + 1) (nn + 1) uri dest
385 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
387 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
388 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
389 and instantiate_parameters params c =
390 let module C = Cic in
391 match (c,params) with
393 | (C.Prod (_,_,ta), he::tl) ->
394 instantiate_parameters tl
395 (CicSubstitution.subst he ta)
396 | (C.Cast (te,_), _) -> instantiate_parameters params te
397 | (t,l) -> raise (AssertFailure (lazy "1"))
399 and strictly_positive context n nn te =
400 let module C = Cic in
401 let module U = UriManager in
402 match CicReduction.whd context te with
405 (*CSC: bisogna controllare ty????*)
406 strictly_positive context n nn te
407 | C.Prod (name,so,ta) ->
408 does_not_occur context n nn so &&
409 strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
410 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
411 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
412 | C.Appl ((C.MutInd (uri,i,exp_named_subst))::tl) ->
413 let (ok,paramsno,ity,cl,name) =
414 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
416 C.InductiveDefinition (tl,_,paramsno,_) ->
417 let (name,_,ity,cl) = List.nth tl i in
418 (List.length tl = 1, paramsno, ity, cl, name)
420 raise (TypeCheckerFailure
421 (lazy ("Unknown inductive type:" ^ U.string_of_uri uri)))
423 let (params,arguments) = split tl paramsno in
424 let lifted_params = List.map (CicSubstitution.lift 1) params in
428 instantiate_parameters lifted_params
429 (CicSubstitution.subst_vars exp_named_subst te)
434 (fun x i -> i && does_not_occur context n nn x)
436 (*CSC: MEGAPATCH3 (sara' quella giusta?)*)
441 ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri
444 | t -> does_not_occur context n nn t
446 (* the inductive type indexes are s.t. n < x <= nn *)
447 and are_all_occurrences_positive context uri indparamsno i n nn te =
448 let module C = Cic in
449 match CicReduction.whd context te with
450 C.Appl ((C.Rel m)::tl) when m = i ->
451 (*CSC: riscrivere fermandosi a 0 *)
452 (* let's check if the inductive type is applied at least to *)
453 (* indparamsno parameters *)
459 match CicReduction.whd context x with
460 C.Rel m when m = n - (indparamsno - k) -> k - 1
462 raise (TypeCheckerFailure
464 ("Non-positive occurence in mutual inductive definition(s) [1]" ^
465 UriManager.string_of_uri uri)))
469 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
471 raise (TypeCheckerFailure
472 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
473 UriManager.string_of_uri uri)))
474 | C.Rel m when m = i ->
475 if indparamsno = 0 then
478 raise (TypeCheckerFailure
479 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
480 UriManager.string_of_uri uri)))
481 | C.Prod (C.Anonymous,source,dest) ->
482 strictly_positive context n nn source &&
483 are_all_occurrences_positive
484 ((Some (C.Anonymous,(C.Decl source)))::context) uri indparamsno
485 (i+1) (n + 1) (nn + 1) dest
486 | C.Prod (name,source,dest) when
487 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
488 (* dummy abstraction, so we behave as in the anonimous case *)
489 strictly_positive context n nn source &&
490 are_all_occurrences_positive
491 ((Some (name,(C.Decl source)))::context) uri indparamsno
492 (i+1) (n + 1) (nn + 1) dest
493 | C.Prod (name,source,dest) ->
494 does_not_occur context n nn source &&
495 are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
496 uri indparamsno (i+1) (n + 1) (nn + 1) dest
499 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
500 (UriManager.string_of_uri uri))))
502 (* Main function to checks the correctness of a mutual *)
503 (* inductive block definition. This is the function *)
504 (* exported to the proof-engine. *)
505 and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
506 let module U = UriManager in
507 (* let's check if the arity of the inductive types are well *)
509 let ugrap1 = List.fold_left
510 (fun ugraph (_,_,x,_) -> let _,ugraph' =
511 type_of ~logger x ugraph in ugraph')
514 (* let's check if the types of the inductive constructors *)
515 (* are well formed. *)
516 (* In order not to use type_of_aux we put the types of the *)
517 (* mutual inductive types at the head of the types of the *)
518 (* constructors using Prods *)
519 let len = List.length itl in
521 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
524 (fun (_,_,_,cl) (i,ugraph) ->
527 (fun ugraph (name,te) ->
528 let debrujinedte = debrujin_constructor uri len te in
531 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
534 let _,ugraph' = type_of ~logger augmented_term ugraph in
535 (* let's check also the positivity conditions *)
538 (are_all_occurrences_positive tys uri indparamsno i 0 len
543 (lazy ("Non positive occurence in " ^ U.string_of_uri uri)))
552 (* Main function to checks the correctness of a mutual *)
553 (* inductive block definition. *)
554 and check_mutual_inductive_defs uri obj ugraph =
556 Cic.InductiveDefinition (itl, params, indparamsno, _) ->
557 typecheck_mutual_inductive_defs uri (itl,params,indparamsno) ugraph
559 raise (TypeCheckerFailure (
560 lazy ("Unknown mutual inductive definition:" ^
561 UriManager.string_of_uri uri)))
563 and type_of_mutual_inductive_defs ~logger uri i ugraph =
564 let module C = Cic in
565 let module R = CicReduction in
566 let module U = UriManager in
568 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
569 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
570 | CicEnvironment.UncheckedObj uobj ->
571 logger#log (`Start_type_checking uri) ;
573 check_mutual_inductive_defs ~logger uri uobj ugraph
575 (* TASSI: FIXME: check ugraph1 == ugraph ritornato da env *)
577 CicEnvironment.set_type_checking_info uri ;
578 logger#log (`Type_checking_completed uri) ;
579 (match CicEnvironment.is_type_checked ~trust:false ugraph uri with
580 CicEnvironment.CheckedObj (cobj,ugraph') -> (cobj,ugraph')
581 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
584 Invalid_argument s ->
585 (*debug_print (lazy s);*)
589 C.InductiveDefinition (dl,_,_,_) ->
590 let (_,_,arity,_) = List.nth dl i in
593 raise (TypeCheckerFailure
594 (lazy ("Unknown mutual inductive definition:" ^ U.string_of_uri uri)))
596 and type_of_mutual_inductive_constr ~logger uri i j ugraph =
597 let module C = Cic in
598 let module R = CicReduction in
599 let module U = UriManager in
601 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
602 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
603 | CicEnvironment.UncheckedObj uobj ->
604 logger#log (`Start_type_checking uri) ;
606 check_mutual_inductive_defs ~logger uri uobj ugraph
608 (* check ugraph1 validity ??? == ugraph' *)
610 CicEnvironment.set_type_checking_info uri ;
611 logger#log (`Type_checking_completed uri) ;
613 CicEnvironment.is_type_checked ~trust:false ugraph uri
615 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
616 | CicEnvironment.UncheckedObj _ ->
617 raise CicEnvironmentError)
619 Invalid_argument s ->
620 (*debug_print (lazy s);*)
624 C.InductiveDefinition (dl,_,_,_) ->
625 let (_,_,_,cl) = List.nth dl i in
626 let (_,ty) = List.nth cl (j-1) in
629 raise (TypeCheckerFailure
630 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
632 and recursive_args context n nn te =
633 let module C = Cic in
634 match CicReduction.whd context te with
640 | C.Cast _ (*CSC ??? *) ->
641 raise (AssertFailure (lazy "3")) (* due to type-checking *)
642 | C.Prod (name,so,de) ->
643 (not (does_not_occur context n nn so)) ::
644 (recursive_args ((Some (name,(C.Decl so)))::context) (n+1) (nn + 1) de)
647 raise (AssertFailure (lazy "4")) (* due to type-checking *)
649 | C.Const _ -> raise (AssertFailure (lazy "5"))
654 | C.CoFix _ -> raise (AssertFailure (lazy "6")) (* due to type-checking *)
656 and get_new_safes ~subst context p c rl safes n nn x =
657 let module C = Cic in
658 let module U = UriManager in
659 let module R = CicReduction in
660 match (R.whd ~subst context c, R.whd ~subst context p, rl) with
661 (C.Prod (_,so,ta1), C.Lambda (name,_,ta2), b::tl) ->
662 (* we are sure that the two sources are convertible because we *)
663 (* have just checked this. So let's go along ... *)
665 List.map (fun x -> x + 1) safes
668 if b then 1::safes' else safes'
670 get_new_safes ~subst ((Some (name,(C.Decl so)))::context)
671 ta2 ta1 tl safes'' (n+1) (nn+1) (x+1)
672 | (C.Prod _, (C.MutConstruct _ as e), _)
673 | (C.Prod _, (C.Rel _ as e), _)
674 | (C.MutInd _, e, [])
675 | (C.Appl _, e, []) -> (e,safes,n,nn,x,context)
677 (* CSC: If the next exception is raised, it just means that *)
678 (* CSC: the proof-assistant allows to use very strange things *)
679 (* CSC: as a branch of a case whose type is a Prod. In *)
680 (* CSC: particular, this means that a new (C.Prod, x,_) case *)
681 (* CSC: must be considered in this match. (e.g. x = MutCase) *)
684 (Printf.sprintf "Get New Safes: c=%s ; p=%s"
685 (CicPp.ppterm c) (CicPp.ppterm p))))
687 and split_prods ~subst context n te =
688 let module C = Cic in
689 let module R = CicReduction in
690 match (n, R.whd ~subst context te) with
692 | (n, C.Prod (name,so,ta)) when n > 0 ->
693 split_prods ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
694 | (_, _) -> raise (AssertFailure (lazy "8"))
696 and eat_lambdas ~subst context n te =
697 let module C = Cic in
698 let module R = CicReduction in
699 match (n, R.whd ~subst context te) with
700 (0, _) -> (te, 0, context)
701 | (n, C.Lambda (name,so,ta)) when n > 0 ->
702 let (te, k, context') =
703 eat_lambdas ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
705 (te, k + 1, context')
707 raise (AssertFailure (lazy (sprintf "9 (%d, %s)" n (CicPp.ppterm te))))
709 (*CSC: Tutto quello che segue e' l'intuzione di luca ;-) *)
710 and check_is_really_smaller_arg ~subst context n nn kl x safes te =
711 (*CSC: forse la whd si puo' fare solo quando serve veramente. *)
712 (*CSC: cfr guarded_by_destructors *)
713 let module C = Cic in
714 let module U = UriManager in
715 match CicReduction.whd ~subst context te with
716 C.Rel m when List.mem m safes -> true
723 (* | C.Cast (te,ty) ->
724 check_is_really_smaller_arg ~subst n nn kl x safes te &&
725 check_is_really_smaller_arg ~subst n nn kl x safes ty*)
726 (* | C.Prod (_,so,ta) ->
727 check_is_really_smaller_arg ~subst n nn kl x safes so &&
728 check_is_really_smaller_arg ~subst (n+1) (nn+1) kl (x+1)
729 (List.map (fun x -> x + 1) safes) ta*)
730 | C.Prod _ -> raise (AssertFailure (lazy "10"))
731 | C.Lambda (name,so,ta) ->
732 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
733 check_is_really_smaller_arg ~subst ((Some (name,(C.Decl so)))::context)
734 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
735 | C.LetIn (name,so,ta) ->
736 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
737 check_is_really_smaller_arg ~subst ((Some (name,(C.Def (so,None))))::context)
738 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
740 (*CSC: sulla coda ci vogliono dei controlli? secondo noi no, ma *)
741 (*CSC: solo perche' non abbiamo trovato controesempi *)
742 check_is_really_smaller_arg ~subst context n nn kl x safes he
743 | C.Appl [] -> raise (AssertFailure (lazy "11"))
745 | C.MutInd _ -> raise (AssertFailure (lazy "12"))
746 | C.MutConstruct _ -> false
747 | C.MutCase (uri,i,outtype,term,pl) ->
749 C.Rel m when List.mem m safes || m = x ->
750 let (tys,len,isinductive,paramsno,cl) =
751 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
753 C.InductiveDefinition (tl,_,paramsno,_) ->
756 (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) tl
758 let (_,isinductive,_,cl) = List.nth tl i in
762 (id, snd (split_prods ~subst tys paramsno ty))) cl
764 (tys,List.length tl,isinductive,paramsno,cl')
766 raise (TypeCheckerFailure
767 (lazy ("Unknown mutual inductive definition:" ^
768 UriManager.string_of_uri uri)))
770 if not isinductive then
773 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
780 Invalid_argument _ ->
781 raise (TypeCheckerFailure (lazy "not enough patterns"))
786 let debrujinedte = debrujin_constructor uri len c in
787 recursive_args tys 0 len debrujinedte
789 let (e,safes',n',nn',x',context') =
790 get_new_safes ~subst context p c rl' safes n nn x
793 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
795 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
796 let (tys,len,isinductive,paramsno,cl) =
797 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
799 C.InductiveDefinition (tl,_,paramsno,_) ->
800 let (_,isinductive,_,cl) = List.nth tl i in
802 List.map (fun (n,_,ty,_) ->
803 Some(Cic.Name n,(Cic.Decl ty))) tl
808 (id, snd (split_prods ~subst tys paramsno ty))) cl
810 (tys,List.length tl,isinductive,paramsno,cl')
812 raise (TypeCheckerFailure
813 (lazy ("Unknown mutual inductive definition:" ^
814 UriManager.string_of_uri uri)))
816 if not isinductive then
819 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
826 Invalid_argument _ ->
827 raise (TypeCheckerFailure (lazy "not enough patterns"))
829 (*CSC: supponiamo come prima che nessun controllo sia necessario*)
830 (*CSC: sugli argomenti di una applicazione *)
834 let debrujinedte = debrujin_constructor uri len c in
835 recursive_args tys 0 len debrujinedte
837 let (e, safes',n',nn',x',context') =
838 get_new_safes ~subst context p c rl' safes n nn x
841 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
846 i && check_is_really_smaller_arg ~subst context n nn kl x safes p
850 let len = List.length fl in
851 let n_plus_len = n + len
852 and nn_plus_len = nn + len
853 and x_plus_len = x + len
854 and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
855 and safes' = List.map (fun x -> x + len) safes in
857 (fun (_,_,ty,bo) i ->
859 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
863 let len = List.length fl in
864 let n_plus_len = n + len
865 and nn_plus_len = nn + len
866 and x_plus_len = x + len
867 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
868 and safes' = List.map (fun x -> x + len) safes in
872 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
876 and guarded_by_destructors ~subst context n nn kl x safes =
877 let module C = Cic in
878 let module U = UriManager in
880 C.Rel m when m > n && m <= nn -> false
882 (match List.nth context (n-1) with
883 Some (_,C.Decl _) -> true
884 | Some (_,C.Def (bo,_)) ->
885 guarded_by_destructors ~subst context m nn kl x safes
886 (CicSubstitution.lift m bo)
887 | None -> raise (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
891 | C.Implicit _ -> true
893 guarded_by_destructors ~subst context n nn kl x safes te &&
894 guarded_by_destructors ~subst context n nn kl x safes ty
895 | C.Prod (name,so,ta) ->
896 guarded_by_destructors ~subst context n nn kl x safes so &&
897 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
898 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
899 | C.Lambda (name,so,ta) ->
900 guarded_by_destructors ~subst context n nn kl x safes so &&
901 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
902 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
903 | C.LetIn (name,so,ta) ->
904 guarded_by_destructors ~subst context n nn kl x safes so &&
905 guarded_by_destructors ~subst ((Some (name,(C.Def (so,None))))::context)
906 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
907 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
908 let k = List.nth kl (m - n - 1) in
909 if not (List.length tl > k) then false
913 i && guarded_by_destructors ~subst context n nn kl x safes param
915 check_is_really_smaller_arg ~subst context n nn kl x safes (List.nth tl k)
918 (fun t i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
920 | C.Var (_,exp_named_subst)
921 | C.Const (_,exp_named_subst)
922 | C.MutInd (_,_,exp_named_subst)
923 | C.MutConstruct (_,_,_,exp_named_subst) ->
925 (fun (_,t) i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
927 | C.MutCase (uri,i,outtype,term,pl) ->
928 (match CicReduction.whd ~subst context term with
929 C.Rel m when List.mem m safes || m = x ->
930 let (tys,len,isinductive,paramsno,cl) =
931 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
933 C.InductiveDefinition (tl,_,paramsno,_) ->
934 let len = List.length tl in
935 let (_,isinductive,_,cl) = List.nth tl i in
937 List.map (fun (n,_,ty,_) ->
938 Some(Cic.Name n,(Cic.Decl ty))) tl
943 let debrujinedty = debrujin_constructor uri len ty in
944 (id, snd (split_prods ~subst tys paramsno ty),
945 snd (split_prods ~subst tys paramsno debrujinedty)
948 (tys,len,isinductive,paramsno,cl')
950 raise (TypeCheckerFailure
951 (lazy ("Unknown mutual inductive definition:" ^
952 UriManager.string_of_uri uri)))
954 if not isinductive then
955 guarded_by_destructors ~subst context n nn kl x safes outtype &&
956 guarded_by_destructors ~subst context n nn kl x safes term &&
957 (*CSC: manca ??? il controllo sul tipo di term? *)
960 i && guarded_by_destructors ~subst context n nn kl x safes p)
967 Invalid_argument _ ->
968 raise (TypeCheckerFailure (lazy "not enough patterns"))
970 guarded_by_destructors ~subst context n nn kl x safes outtype &&
971 (*CSC: manca ??? il controllo sul tipo di term? *)
973 (fun (p,(_,c,brujinedc)) i ->
974 let rl' = recursive_args tys 0 len brujinedc in
975 let (e,safes',n',nn',x',context') =
976 get_new_safes ~subst context p c rl' safes n nn x
979 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
981 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
982 let (tys,len,isinductive,paramsno,cl) =
983 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
985 C.InductiveDefinition (tl,_,paramsno,_) ->
986 let (_,isinductive,_,cl) = List.nth tl i in
989 (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
994 (id, snd (split_prods ~subst tys paramsno ty))) cl
996 (tys,List.length tl,isinductive,paramsno,cl')
998 raise (TypeCheckerFailure
999 (lazy ("Unknown mutual inductive definition:" ^
1000 UriManager.string_of_uri uri)))
1002 if not isinductive then
1003 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1004 guarded_by_destructors ~subst context n nn kl x safes term &&
1005 (*CSC: manca ??? il controllo sul tipo di term? *)
1008 i && guarded_by_destructors ~subst context n nn kl x safes p)
1015 Invalid_argument _ ->
1016 raise (TypeCheckerFailure (lazy "not enough patterns"))
1018 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1019 (*CSC: manca ??? il controllo sul tipo di term? *)
1022 i && guarded_by_destructors ~subst context n nn kl x safes t)
1027 let debrujinedte = debrujin_constructor uri len c in
1028 recursive_args tys 0 len debrujinedte
1030 let (e, safes',n',nn',x',context') =
1031 get_new_safes ~subst context p c rl' safes n nn x
1034 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1037 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1038 guarded_by_destructors ~subst context n nn kl x safes term &&
1039 (*CSC: manca ??? il controllo sul tipo di term? *)
1041 (fun p i -> i && guarded_by_destructors ~subst context n nn kl x safes p)
1045 let len = List.length fl in
1046 let n_plus_len = n + len
1047 and nn_plus_len = nn + len
1048 and x_plus_len = x + len
1049 and tys = List.map (fun (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
1050 and safes' = List.map (fun x -> x + len) safes in
1052 (fun (_,_,ty,bo) i ->
1053 i && guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1054 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1055 x_plus_len safes' bo
1057 | C.CoFix (_, fl) ->
1058 let len = List.length fl in
1059 let n_plus_len = n + len
1060 and nn_plus_len = nn + len
1061 and x_plus_len = x + len
1062 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl
1063 and safes' = List.map (fun x -> x + len) safes in
1067 guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1068 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1069 x_plus_len safes' bo
1072 (* the boolean h means already protected *)
1073 (* args is the list of arguments the type of the constructor that may be *)
1074 (* found in head position must be applied to. *)
1075 and guarded_by_constructors ~subst context n nn h te args coInductiveTypeURI =
1076 let module C = Cic in
1077 (*CSC: There is a lot of code replication between the cases X and *)
1078 (*CSC: (C.Appl X tl). Maybe it will be better to define a function *)
1079 (*CSC: that maps X into (C.Appl X []) when X is not already a C.Appl *)
1080 match CicReduction.whd ~subst context te with
1081 C.Rel m when m > n && m <= nn -> h
1089 (* the term has just been type-checked *)
1090 raise (AssertFailure (lazy "17"))
1091 | C.Lambda (name,so,de) ->
1092 does_not_occur ~subst context n nn so &&
1093 guarded_by_constructors ~subst ((Some (name,(C.Decl so)))::context)
1094 (n + 1) (nn + 1) h de args coInductiveTypeURI
1095 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
1097 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) tl true
1098 | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
1102 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1103 with Not_found -> assert false
1106 C.InductiveDefinition (itl,_,_,_) ->
1107 let (_,_,_,cl) = List.nth itl i in
1108 let (_,cons) = List.nth cl (j - 1) in
1109 CicSubstitution.subst_vars exp_named_subst cons
1111 raise (TypeCheckerFailure
1112 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
1114 let rec analyse_branch context ty te =
1115 match CicReduction.whd ~subst context ty with
1116 C.Meta _ -> raise (AssertFailure (lazy "34"))
1120 does_not_occur ~subst context n nn te
1123 raise (AssertFailure (lazy "24"))(* due to type-checking *)
1124 | C.Prod (name,so,de) ->
1125 analyse_branch ((Some (name,(C.Decl so)))::context) de te
1128 raise (AssertFailure (lazy "25"))(* due to type-checking *)
1129 | C.Appl ((C.MutInd (uri,_,_))::_) as ty
1130 when uri == coInductiveTypeURI ->
1131 guarded_by_constructors ~subst context n nn true te []
1133 | C.Appl ((C.MutInd (uri,_,_))::_) as ty ->
1134 guarded_by_constructors ~subst context n nn true te tl
1137 does_not_occur ~subst context n nn te
1138 | C.Const _ -> raise (AssertFailure (lazy "26"))
1139 | C.MutInd (uri,_,_) when uri == coInductiveTypeURI ->
1140 guarded_by_constructors ~subst context n nn true te []
1143 does_not_occur ~subst context n nn te
1144 | C.MutConstruct _ -> raise (AssertFailure (lazy "27"))
1145 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1146 (*CSC: in head position. *)
1150 raise (AssertFailure (lazy "28"))(* due to type-checking *)
1152 let rec analyse_instantiated_type context ty l =
1153 match CicReduction.whd ~subst context ty with
1159 | C.Cast _ -> raise (AssertFailure (lazy "29"))(* due to type-checking *)
1160 | C.Prod (name,so,de) ->
1165 analyse_branch context so he &&
1166 analyse_instantiated_type
1167 ((Some (name,(C.Decl so)))::context) de tl
1171 raise (AssertFailure (lazy "30"))(* due to type-checking *)
1174 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1175 | C.Const _ -> raise (AssertFailure (lazy "31"))
1178 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1179 | C.MutConstruct _ -> raise (AssertFailure (lazy "32"))
1180 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1181 (*CSC: in head position. *)
1185 raise (AssertFailure (lazy "33"))(* due to type-checking *)
1187 let rec instantiate_type args consty =
1190 | tlhe::tltl as l ->
1191 let consty' = CicReduction.whd ~subst context consty in
1197 let instantiated_de = CicSubstitution.subst he de in
1198 (*CSC: siamo sicuri che non sia troppo forte? *)
1199 does_not_occur ~subst context n nn tlhe &
1200 instantiate_type tl instantiated_de tltl
1202 (*CSC:We do not consider backbones with a MutCase, a *)
1203 (*CSC:FixPoint, a CoFixPoint and so on in head position.*)
1204 raise (AssertFailure (lazy "23"))
1206 | [] -> analyse_instantiated_type context consty' l
1207 (* These are all the other cases *)
1209 instantiate_type args consty tl
1210 | C.Appl ((C.CoFix (_,fl))::tl) ->
1211 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1212 let len = List.length fl in
1213 let n_plus_len = n + len
1214 and nn_plus_len = nn + len
1215 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1216 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
1219 i && does_not_occur ~subst context n nn ty &&
1220 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1221 h bo args coInductiveTypeURI
1223 | C.Appl ((C.MutCase (_,_,out,te,pl))::tl) ->
1224 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1225 does_not_occur ~subst context n nn out &&
1226 does_not_occur ~subst context n nn te &&
1230 guarded_by_constructors ~subst context n nn h x args
1234 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
1235 | C.Var (_,exp_named_subst)
1236 | C.Const (_,exp_named_subst) ->
1238 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1239 | C.MutInd _ -> assert false
1240 | C.MutConstruct (_,_,_,exp_named_subst) ->
1242 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1243 | C.MutCase (_,_,out,te,pl) ->
1244 does_not_occur ~subst context n nn out &&
1245 does_not_occur ~subst context n nn te &&
1249 guarded_by_constructors ~subst context n nn h x args
1253 let len = List.length fl in
1254 let n_plus_len = n + len
1255 and nn_plus_len = nn + len
1256 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1257 and tys = List.map (fun (n,_,ty,_)-> Some (C.Name n,(C.Decl ty))) fl in
1259 (fun (_,_,ty,bo) i ->
1260 i && does_not_occur ~subst context n nn ty &&
1261 does_not_occur ~subst (tys@context) n_plus_len nn_plus_len bo
1264 let len = List.length fl in
1265 let n_plus_len = n + len
1266 and nn_plus_len = nn + len
1267 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1268 and tys = List.map (fun (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl in
1271 i && does_not_occur ~subst context n nn ty &&
1272 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1274 args coInductiveTypeURI
1277 and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1278 need_dummy ind arity1 arity2 ugraph =
1279 let module C = Cic in
1280 let module U = UriManager in
1281 let arity1 = CicReduction.whd ~subst context arity1 in
1282 let rec check_allowed_sort_elimination_aux ugraph context arity2 need_dummy =
1283 match arity1, CicReduction.whd ~subst context arity2 with
1284 (C.Prod (_,so1,de1), C.Prod (_,so2,de2)) ->
1286 CicReduction.are_convertible ~subst ~metasenv context so1 so2 ugraph in
1288 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1289 need_dummy (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1293 | (C.Sort _, C.Prod (name,so,ta)) when not need_dummy ->
1295 CicReduction.are_convertible ~subst ~metasenv context so ind ugraph in
1299 check_allowed_sort_elimination_aux ugraph1
1300 ((Some (name,C.Decl so))::context) ta true
1301 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
1302 | (C.Sort C.Prop, C.Sort C.Set)
1303 | (C.Sort C.Prop, C.Sort C.CProp)
1304 | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
1305 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1307 C.InductiveDefinition (itl,_,paramsno,_) ->
1308 let itl_len = List.length itl in
1309 let (name,_,ty,cl) = List.nth itl i in
1310 let cl_len = List.length cl in
1311 if (cl_len = 0 || (itl_len = 1 && cl_len = 1)) then
1312 let non_informative,ugraph =
1313 if cl_len = 0 then true,ugraph
1315 is_non_informative ~logger [Some (C.Name name,C.Decl ty)]
1316 paramsno (snd (List.nth cl 0)) ugraph
1318 (* is it a singleton or empty non recursive and non informative
1320 non_informative, ugraph
1324 raise (TypeCheckerFailure
1325 (lazy ("Unknown mutual inductive definition:" ^
1326 UriManager.string_of_uri uri)))
1328 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
1329 | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
1330 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
1331 | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
1332 | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
1333 | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
1334 | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
1336 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1338 C.InductiveDefinition (itl,_,paramsno,_) ->
1340 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1342 let (_,_,_,cl) = List.nth itl i in
1344 (fun (_,x) (i,ugraph) ->
1346 is_small ~logger tys paramsno x ugraph
1351 raise (TypeCheckerFailure
1352 (lazy ("Unknown mutual inductive definition:" ^
1353 UriManager.string_of_uri uri)))
1355 | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
1356 | (_,_) -> false,ugraph
1358 check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
1360 and type_of_branch ~subst context argsno need_dummy outtype term constype =
1361 let module C = Cic in
1362 let module R = CicReduction in
1363 match R.whd ~subst context constype with
1368 C.Appl [outtype ; term]
1369 | C.Appl (C.MutInd (_,_,_)::tl) ->
1370 let (_,arguments) = split tl argsno
1372 if need_dummy && arguments = [] then
1375 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1376 | C.Prod (name,so,de) ->
1378 match CicSubstitution.lift 1 term with
1379 C.Appl l -> C.Appl (l@[C.Rel 1])
1380 | t -> C.Appl [t ; C.Rel 1]
1382 C.Prod (C.Anonymous,so,type_of_branch ~subst
1383 ((Some (name,(C.Decl so)))::context) argsno need_dummy
1384 (CicSubstitution.lift 1 outtype) term' de)
1385 | _ -> raise (AssertFailure (lazy "20"))
1387 (* check_metasenv_consistency checks that the "canonical" context of a
1388 metavariable is consitent - up to relocation via the relocation list l -
1389 with the actual context *)
1392 and check_metasenv_consistency ~logger ~subst metasenv context
1393 canonical_context l ugraph
1395 let module C = Cic in
1396 let module R = CicReduction in
1397 let module S = CicSubstitution in
1398 let lifted_canonical_context =
1402 | (Some (n,C.Decl t))::tl ->
1403 (Some (n,C.Decl (S.subst_meta l (S.lift i t))))::(aux (i+1) tl)
1404 | (Some (n,C.Def (t,None)))::tl ->
1405 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),None)))::(aux (i+1) tl)
1406 | None::tl -> None::(aux (i+1) tl)
1407 | (Some (n,C.Def (t,Some ty)))::tl ->
1408 (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)
1410 aux 1 canonical_context
1416 | Some t,Some (_,C.Def (ct,_)) ->
1418 R.are_convertible ~subst ~metasenv context t ct ugraph
1423 (lazy (sprintf "Not well typed metavariable local context: expected a term convertible with %s, found %s" (CicPp.ppterm ct) (CicPp.ppterm t))))
1426 | Some t,Some (_,C.Decl ct) ->
1427 let type_t,ugraph1 =
1428 type_of_aux' ~logger ~subst metasenv context t ugraph
1431 R.are_convertible ~subst ~metasenv context type_t ct ugraph1
1434 raise (TypeCheckerFailure
1435 (lazy (sprintf "Not well typed metavariable local context: expected a term of type %s, found %s of type %s"
1436 (CicPp.ppterm ct) (CicPp.ppterm t)
1437 (CicPp.ppterm type_t))))
1441 raise (TypeCheckerFailure
1442 (lazy ("Not well typed metavariable local context: "^
1443 "an hypothesis, that is not hidden, is not instantiated")))
1444 ) ugraph l lifted_canonical_context
1448 type_of_aux' is just another name (with a different scope)
1452 and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
1453 let rec type_of_aux ~logger context t ugraph =
1454 let module C = Cic in
1455 let module R = CicReduction in
1456 let module S = CicSubstitution in
1457 let module U = UriManager in
1461 match List.nth context (n - 1) with
1462 Some (_,C.Decl t) -> S.lift n t,ugraph
1463 | Some (_,C.Def (_,Some ty)) -> S.lift n ty,ugraph
1464 | Some (_,C.Def (bo,None)) ->
1465 debug_print (lazy "##### CASO DA INVESTIGARE E CAPIRE") ;
1466 type_of_aux ~logger context (S.lift n bo) ugraph
1468 (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
1471 raise (TypeCheckerFailure (lazy "unbound variable"))
1473 | C.Var (uri,exp_named_subst) ->
1476 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1478 let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
1479 let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
1484 let (canonical_context,term,ty) = CicUtil.lookup_subst n subst in
1486 check_metasenv_consistency ~logger
1487 ~subst metasenv context canonical_context l ugraph
1489 (* assuming subst is well typed !!!!! *)
1490 ((CicSubstitution.subst_meta l ty), ugraph1)
1491 (* type_of_aux context (CicSubstitution.subst_meta l term) *)
1492 with CicUtil.Subst_not_found _ ->
1493 let (_,canonical_context,ty) = CicUtil.lookup_meta n metasenv in
1495 check_metasenv_consistency ~logger
1496 ~subst metasenv context canonical_context l ugraph
1498 ((CicSubstitution.subst_meta l ty),ugraph1))
1499 (* TASSI: CONSTRAINTS *)
1500 | C.Sort (C.Type t) ->
1501 let t' = CicUniv.fresh() in
1502 let ugraph1 = CicUniv.add_gt t' t ugraph in
1503 (C.Sort (C.Type t')),ugraph1
1504 (* TASSI: CONSTRAINTS *)
1505 | C.Sort s -> (C.Sort (C.Type (CicUniv.fresh ()))),ugraph
1506 | C.Implicit _ -> raise (AssertFailure (lazy "21"))
1507 | C.Cast (te,ty) as t ->
1508 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1509 let ty_te,ugraph2 = type_of_aux ~logger context te ugraph1 in
1511 R.are_convertible ~subst ~metasenv context ty_te ty ugraph2
1516 raise (TypeCheckerFailure
1517 (lazy (sprintf "Invalid cast %s" (CicPp.ppterm t))))
1518 | C.Prod (name,s,t) ->
1519 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1521 type_of_aux ~logger ((Some (name,(C.Decl s)))::context) t ugraph1
1523 sort_of_prod ~subst context (name,s) (sort1,sort2) ugraph2
1524 | C.Lambda (n,s,t) ->
1525 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1526 (match R.whd ~subst context sort1 with
1531 (TypeCheckerFailure (lazy (sprintf
1532 "Not well-typed lambda-abstraction: the source %s should be a type; instead it is a term of type %s" (CicPp.ppterm s)
1533 (CicPp.ppterm sort1))))
1536 type_of_aux ~logger ((Some (n,(C.Decl s)))::context) t ugraph1
1538 (C.Prod (n,s,type2)),ugraph2
1539 | C.LetIn (n,s,t) ->
1540 (* only to check if s is well-typed *)
1541 let ty,ugraph1 = type_of_aux ~logger context s ugraph in
1542 (* The type of a LetIn is a LetIn. Extremely slow since the computed
1543 LetIn is later reduced and maybe also re-checked.
1544 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t))
1546 (* The type of the LetIn is reduced. Much faster than the previous
1547 solution. Moreover the inferred type is probably very different
1548 from the expected one.
1549 (CicReduction.whd ~subst context
1550 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)))
1552 (* One-step LetIn reduction. Even faster than the previous solution.
1553 Moreover the inferred type is closer to the expected one. *)
1556 ((Some (n,(C.Def (s,Some ty))))::context) t ugraph1
1558 (CicSubstitution.subst s ty1),ugraph2
1559 | C.Appl (he::tl) when List.length tl > 0 ->
1560 let hetype,ugraph1 = type_of_aux ~logger context he ugraph in
1561 let tlbody_and_type,ugraph2 =
1564 let ty,ugraph1 = type_of_aux ~logger context x ugraph in
1565 let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in
1566 ((x,ty)::l,ugraph1))
1569 (* TASSI: questa c'era nel mio... ma non nel CVS... *)
1570 (* let _,ugraph2 = type_of_aux context hetype ugraph2 in *)
1571 eat_prods ~subst context hetype tlbody_and_type ugraph2
1572 | C.Appl _ -> raise (AssertFailure (lazy "Appl: no arguments"))
1573 | C.Const (uri,exp_named_subst) ->
1576 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1578 let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
1580 CicSubstitution.subst_vars exp_named_subst cty
1584 | C.MutInd (uri,i,exp_named_subst) ->
1587 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1589 (* TASSI: da me c'era anche questa, ma in CVS no *)
1590 let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
1591 (* fine parte dubbia *)
1593 CicSubstitution.subst_vars exp_named_subst mty
1597 | C.MutConstruct (uri,i,j,exp_named_subst) ->
1599 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1601 (* TASSI: idem come sopra *)
1603 type_of_mutual_inductive_constr ~logger uri i j ugraph1
1606 CicSubstitution.subst_vars exp_named_subst mty
1609 | C.MutCase (uri,i,outtype,term,pl) ->
1610 let outsort,ugraph1 = type_of_aux ~logger context outtype ugraph in
1611 let (need_dummy, k) =
1612 let rec guess_args context t =
1613 let outtype = CicReduction.whd ~subst context t in
1615 C.Sort _ -> (true, 0)
1616 | C.Prod (name, s, t) ->
1618 guess_args ((Some (name,(C.Decl s)))::context) t in
1620 (* last prod before sort *)
1621 match CicReduction.whd ~subst context s with
1622 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1623 C.MutInd (uri',i',_) when U.eq uri' uri && i' = i ->
1625 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1626 | C.Appl ((C.MutInd (uri',i',_)) :: _)
1627 when U.eq uri' uri && i' = i -> (false, 1)
1635 "Malformed case analasys' output type %s"
1636 (CicPp.ppterm outtype))))
1639 let (parameters, arguments, exp_named_subst),ugraph2 =
1640 let ty,ugraph2 = type_of_aux context term ugraph1 in
1641 match R.whd ~subst context ty with
1642 (*CSC manca il caso dei CAST *)
1643 (*CSC: ma servono i parametri (uri,i)? Se si', perche' non serve anche il *)
1644 (*CSC: parametro exp_named_subst? Se no, perche' non li togliamo? *)
1645 (*CSC: Hint: nella DTD servono per gli stylesheet. *)
1646 C.MutInd (uri',i',exp_named_subst) as typ ->
1647 if U.eq uri uri' && i = i' then
1648 ([],[],exp_named_subst),ugraph2
1653 ("Case analysys: analysed term type is %s, but is expected to be (an application of) %s#1/%d{_}")
1654 (CicPp.ppterm typ) (U.string_of_uri uri) i)))
1656 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1657 if U.eq uri uri' && i = i' then
1659 split tl (List.length tl - k)
1660 in (params,args,exp_named_subst),ugraph2
1665 ("Case analysys: analysed term type is %s, "^
1666 "but is expected to be (an application of) "^
1668 (CicPp.ppterm typ') (U.string_of_uri uri) i)))
1674 "analysed term %s is not an inductive one")
1675 (CicPp.ppterm term))))
1677 let (b, k) = guess_args context outsort in
1678 if not b then (b, k - 1) else (b, k) in
1679 let (parameters, arguments, exp_named_subst),ugraph2 =
1680 let ty,ugraph2 = type_of_aux ~logger context term ugraph1 in
1681 match R.whd ~subst context ty with
1682 C.MutInd (uri',i',exp_named_subst) as typ ->
1683 if U.eq uri uri' && i = i' then
1684 ([],[],exp_named_subst),ugraph2
1688 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1689 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1691 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1692 if U.eq uri uri' && i = i' then
1694 split tl (List.length tl - k)
1695 in (params,args,exp_named_subst),ugraph2
1699 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1700 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1705 "Case analysis: analysed term %s is not an inductive one"
1706 (CicPp.ppterm term))))
1709 let's control if the sort elimination is allowed:
1712 let sort_of_ind_type =
1713 if parameters = [] then
1714 C.MutInd (uri,i,exp_named_subst)
1716 C.Appl ((C.MutInd (uri,i,exp_named_subst))::parameters)
1718 let type_of_sort_of_ind_ty,ugraph3 =
1719 type_of_aux ~logger context sort_of_ind_type ugraph2 in
1721 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1722 need_dummy sort_of_ind_type type_of_sort_of_ind_ty outsort ugraph3
1726 (TypeCheckerFailure (lazy ("Case analasys: sort elimination not allowed")));
1727 (* let's check if the type of branches are right *)
1731 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1732 with Not_found -> assert false
1735 C.InductiveDefinition (_,_,parsno,_) -> parsno
1737 raise (TypeCheckerFailure
1738 (lazy ("Unknown mutual inductive definition:" ^
1739 UriManager.string_of_uri uri)))
1741 let (_,branches_ok,ugraph5) =
1743 (fun (j,b,ugraph) p ->
1746 if parameters = [] then
1747 (C.MutConstruct (uri,i,j,exp_named_subst))
1750 (C.MutConstruct (uri,i,j,exp_named_subst)::parameters))
1752 let ty_p,ugraph1 = type_of_aux ~logger context p ugraph in
1753 let ty_cons,ugraph3 = type_of_aux ~logger context cons ugraph1 in
1756 type_of_branch ~subst context parsno need_dummy outtype cons
1760 ~subst ~metasenv context ty_p ty_branch ugraph3
1764 ("#### " ^ CicPp.ppterm ty_p ^
1765 " <==> " ^ CicPp.ppterm ty_branch));
1769 ) (1,true,ugraph4) pl
1771 if not branches_ok then
1773 (TypeCheckerFailure (lazy "Case analysys: wrong branch type"));
1775 if not need_dummy then outtype::arguments@[term]
1776 else outtype::arguments in
1778 if need_dummy && arguments = [] then outtype
1779 else CicReduction.head_beta_reduce (C.Appl arguments')
1783 let types_times_kl,ugraph1 =
1784 (* WAS: list rev list map *)
1786 (fun (l,ugraph) (n,k,ty,_) ->
1787 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1788 ((Some (C.Name n,(C.Decl ty)),k)::l,ugraph1)
1791 let (types,kl) = List.split types_times_kl in
1792 let len = List.length types in
1795 (fun ugraph (name,x,ty,bo) ->
1797 type_of_aux ~logger (types@context) bo ugraph
1800 R.are_convertible ~subst ~metasenv (types@context)
1801 ty_bo (CicSubstitution.lift len ty) ugraph1 in
1804 let (m, eaten, context') =
1805 eat_lambdas ~subst (types @ context) (x + 1) bo
1808 let's control the guarded by
1809 destructors conditions D{f,k,x,M}
1811 if not (guarded_by_destructors ~subst context' eaten
1812 (len + eaten) kl 1 [] m) then
1815 (lazy ("Fix: not guarded by destructors")))
1820 raise (TypeCheckerFailure (lazy ("Fix: ill-typed bodies")))
1822 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1823 let (_,_,ty,_) = List.nth fl i in
1828 (fun (l,ugraph) (n,ty,_) ->
1830 type_of_aux ~logger context ty ugraph in
1831 (Some (C.Name n,(C.Decl ty))::l,ugraph1)
1834 let len = List.length types in
1837 (fun ugraph (_,ty,bo) ->
1839 type_of_aux ~logger (types @ context) bo ugraph
1842 R.are_convertible ~subst ~metasenv (types @ context) ty_bo
1843 (CicSubstitution.lift len ty) ugraph1
1847 (* let's control that the returned type is coinductive *)
1848 match returns_a_coinductive ~subst context ty with
1852 (lazy "CoFix: does not return a coinductive type"))
1855 let's control the guarded by constructors
1858 if not (guarded_by_constructors ~subst
1859 (types @ context) 0 len false bo [] uri) then
1862 (lazy "CoFix: not guarded by constructors"))
1868 (TypeCheckerFailure (lazy "CoFix: ill-typed bodies"))
1871 let (_,ty,_) = List.nth fl i in
1874 and check_exp_named_subst ~logger ~subst context ugraph =
1875 let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
1878 | ((uri,t) as item)::tl ->
1879 let ty_uri,ugraph1 = type_of_variable ~logger uri ugraph in
1881 CicSubstitution.subst_vars esubsts ty_uri in
1882 let typeoft,ugraph2 = type_of_aux ~logger context t ugraph1 in
1884 CicReduction.are_convertible ~subst ~metasenv
1885 context typeoft typeofvar ugraph2
1888 check_exp_named_subst_aux ~logger (esubsts@[item]) tl ugraph3
1891 CicReduction.fdebug := 0 ;
1893 (CicReduction.are_convertible
1894 ~subst ~metasenv context typeoft typeofvar ugraph2) ;
1896 debug typeoft [typeofvar] ;
1897 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
1900 check_exp_named_subst_aux ~logger [] ugraph
1902 and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
1903 let module C = Cic in
1904 let t1' = CicReduction.whd ~subst context t1 in
1905 let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
1906 match (t1', t2') with
1907 (C.Sort s1, C.Sort s2)
1908 when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) ->
1909 (* different from Coq manual!!! *)
1911 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
1912 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1913 let t' = CicUniv.fresh() in
1914 let ugraph1 = CicUniv.add_ge t' t1 ugraph in
1915 let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
1916 C.Sort (C.Type t'),ugraph2
1917 | (C.Sort _,C.Sort (C.Type t1)) ->
1918 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1919 C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
1920 | (C.Meta _, C.Sort _) -> t2',ugraph
1921 | (C.Meta _, (C.Meta (_,_) as t))
1922 | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
1924 | (_,_) -> raise (TypeCheckerFailure (lazy (sprintf
1925 "Prod: expected two sorts, found = %s, %s" (CicPp.ppterm t1')
1926 (CicPp.ppterm t2'))))
1928 and eat_prods ~subst context hetype l ugraph =
1929 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
1933 | (hete, hety)::tl ->
1934 (match (CicReduction.whd ~subst context hetype) with
1937 CicReduction.are_convertible
1938 ~subst ~metasenv context hety s ugraph
1942 CicReduction.fdebug := -1 ;
1943 eat_prods ~subst context
1944 (CicSubstitution.subst hete t) tl ugraph1
1945 (*TASSI: not sure *)
1949 CicReduction.fdebug := 0 ;
1950 ignore (CicReduction.are_convertible
1951 ~subst ~metasenv context s hety ugraph) ;
1957 ("Appl: wrong parameter-type, expected %s, found %s")
1958 (CicPp.ppterm hetype) (CicPp.ppterm s))))
1961 raise (TypeCheckerFailure
1962 (lazy "Appl: this is not a function, it cannot be applied"))
1965 and returns_a_coinductive ~subst context ty =
1966 let module C = Cic in
1967 match CicReduction.whd ~subst context ty with
1968 C.MutInd (uri,i,_) ->
1969 (*CSC: definire una funzioncina per questo codice sempre replicato *)
1972 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1973 with Not_found -> assert false
1976 C.InductiveDefinition (itl,_,_,_) ->
1977 let (_,is_inductive,_,_) = List.nth itl i in
1978 if is_inductive then None else (Some uri)
1980 raise (TypeCheckerFailure
1981 (lazy ("Unknown mutual inductive definition:" ^
1982 UriManager.string_of_uri uri)))
1984 | C.Appl ((C.MutInd (uri,i,_))::_) ->
1985 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1987 C.InductiveDefinition (itl,_,_,_) ->
1988 let (_,is_inductive,_,_) = List.nth itl i in
1989 if is_inductive then None else (Some uri)
1991 raise (TypeCheckerFailure
1992 (lazy ("Unknown mutual inductive definition:" ^
1993 UriManager.string_of_uri uri)))
1995 | C.Prod (n,so,de) ->
1996 returns_a_coinductive ~subst ((Some (n,C.Decl so))::context) de
2001 debug_print (lazy ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t)) ; flush stderr ;
2004 type_of_aux ~logger context t ugraph
2006 in debug_print (lazy "FINE TYPE_OF_AUX") ; flush stderr ; res
2009 (* is a small constructor? *)
2010 (*CSC: ottimizzare calcolando staticamente *)
2011 and is_small_or_non_informative ~condition ~logger context paramsno c ugraph =
2012 let rec is_small_or_non_informative_aux ~logger context c ugraph =
2013 let module C = Cic in
2014 match CicReduction.whd context c with
2016 let s,ugraph1 = type_of_aux' ~logger [] context so ugraph in
2017 let b = condition s in
2019 is_small_or_non_informative_aux
2020 ~logger ((Some (n,(C.Decl so)))::context) de ugraph1
2023 | _ -> true,ugraph (*CSC: we trust the type-checker *)
2025 let (context',dx) = split_prods ~subst:[] context paramsno c in
2026 is_small_or_non_informative_aux ~logger context' dx ugraph
2028 and is_small ~logger =
2029 is_small_or_non_informative
2030 ~condition:(fun s -> s=Cic.Sort Cic.Prop || s=Cic.Sort Cic.Set)
2033 and is_non_informative ~logger =
2034 is_small_or_non_informative
2035 ~condition:(fun s -> s=Cic.Sort Cic.Prop)
2038 and type_of ~logger t ugraph =
2040 debug_print (lazy ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t)) ; flush stderr ;
2043 type_of_aux' ~logger [] [] t ugraph
2045 in debug_print (lazy "FINE TYPE_OF_AUX'") ; flush stderr ; res
2049 let typecheck_obj0 ~logger uri ugraph =
2050 let module C = Cic in
2052 C.Constant (_,Some te,ty,_,_) ->
2053 let _,ugraph = type_of ~logger ty ugraph in
2054 let ty_te,ugraph = type_of ~logger te ugraph in
2055 let b,ugraph = (CicReduction.are_convertible [] ty_te ty ugraph) in
2057 raise (TypeCheckerFailure
2058 (lazy "the type of the body is not the one expected"))
2061 | C.Constant (_,None,ty,_,_) ->
2062 (* only to check that ty is well-typed *)
2063 let _,ugraph = type_of ~logger ty ugraph in
2065 | C.CurrentProof (_,conjs,te,ty,_,_) ->
2068 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
2070 type_of_aux' ~logger metasenv context ty ugraph
2072 metasenv @ [conj],ugraph
2075 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
2076 let type_of_te,ugraph =
2077 type_of_aux' ~logger conjs [] te ugraph
2079 let b,ugraph = CicReduction.are_convertible [] type_of_te ty ugraph in
2081 raise (TypeCheckerFailure (lazy (sprintf
2082 "the current proof is not well typed because the type %s of the body is not convertible to the declared type %s"
2083 (CicPp.ppterm type_of_te) (CicPp.ppterm ty))))
2086 | C.Variable (_,bo,ty,_,_) ->
2087 (* only to check that ty is well-typed *)
2088 let _,ugraph = type_of ~logger ty ugraph in
2092 let ty_bo,ugraph = type_of ~logger bo ugraph in
2093 let b,ugraph = CicReduction.are_convertible [] ty_bo ty ugraph in
2095 raise (TypeCheckerFailure
2096 (lazy "the body is not the one expected"))
2100 | (C.InductiveDefinition _ as obj) ->
2101 check_mutual_inductive_defs ~logger uri obj ugraph
2104 let module C = Cic in
2105 let module R = CicReduction in
2106 let module U = UriManager in
2107 let logger = new CicLogger.logger in
2108 (* ??? match CicEnvironment.is_type_checked ~trust:true uri with ???? *)
2109 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2110 CicEnvironment.CheckedObj (cobj,ugraph') ->
2111 (* debug_print (lazy ("NON-INIZIO A TYPECHECKARE " ^ U.string_of_uri uri));*)
2113 | CicEnvironment.UncheckedObj uobj ->
2114 (* let's typecheck the uncooked object *)
2115 logger#log (`Start_type_checking uri) ;
2116 (* debug_print (lazy ("INIZIO A TYPECHECKARE " ^ U.string_of_uri uri)); *)
2117 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph uobj in
2119 CicEnvironment.set_type_checking_info uri;
2120 logger#log (`Type_checking_completed uri);
2121 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
2122 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2123 | _ -> raise CicEnvironmentError
2126 this is raised if set_type_checking_info is called on an object
2127 that has no associated universe file. If we are in univ_maker
2128 phase this is OK since univ_maker will properly commit the
2131 Invalid_argument s ->
2132 (*debug_print (lazy s);*)
2136 let typecheck_obj ~logger uri obj =
2137 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph obj in
2138 let ugraph, univlist, obj = CicUnivUtils.clean_and_fill uri obj ugraph in
2139 CicEnvironment.add_type_checked_obj uri (obj,ugraph,univlist)
2141 (** wrappers which instantiate fresh loggers *)
2143 let type_of_aux' ?(subst = []) metasenv context t ugraph =
2144 let logger = new CicLogger.logger in
2145 type_of_aux' ~logger ~subst metasenv context t ugraph
2147 let typecheck_obj uri obj =
2148 let logger = new CicLogger.logger in
2149 typecheck_obj ~logger uri obj
2151 (* check_allowed_sort_elimination uri i s1 s2
2152 This function is used outside the kernel to determine in advance whether
2153 a MutCase will be allowed or not.
2154 [uri,i] is the type of the term to match
2155 [s1] is the sort of the term to eliminate (i.e. the head of the arity
2156 of the inductive type [uri,i])
2157 [s2] is the sort of the goal (i.e. the head of the type of the outtype
2159 let check_allowed_sort_elimination uri i s1 s2 =
2160 fst (check_allowed_sort_elimination ~subst:[] ~metasenv:[]
2161 ~logger:(new CicLogger.logger) [] uri i true
2162 (Cic.Implicit None) (* never used *) (Cic.Sort s1) (Cic.Sort s2)
2163 CicUniv.empty_ugraph)