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
297 (fun (types,len) (n,_,ty,_) ->
298 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
303 (fun (_,_,ty,bo) i ->
304 i && does_not_occur ~subst context n nn ty &&
305 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
308 let len = List.length fl in
309 let n_plus_len = n + len in
310 let nn_plus_len = nn + len in
313 (fun (types,len) (n,ty,_) ->
314 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
320 i && does_not_occur ~subst context n nn ty &&
321 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
324 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
325 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
326 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
327 (*CSC strictly_positive *)
328 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
329 and weakly_positive context n nn uri te =
330 let module C = Cic in
331 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
333 C.MutInd (HelmLibraryObjects.Datatypes.nat_URI,0,[])
335 (*CSC: mettere in cicSubstitution *)
336 let rec subst_inductive_type_with_dummy_mutind =
338 C.MutInd (uri',0,_) when UriManager.eq uri' uri ->
340 | C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri ->
342 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
343 | C.Prod (name,so,ta) ->
344 C.Prod (name, subst_inductive_type_with_dummy_mutind so,
345 subst_inductive_type_with_dummy_mutind ta)
346 | C.Lambda (name,so,ta) ->
347 C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
348 subst_inductive_type_with_dummy_mutind ta)
350 C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
351 | C.MutCase (uri,i,outtype,term,pl) ->
353 subst_inductive_type_with_dummy_mutind outtype,
354 subst_inductive_type_with_dummy_mutind term,
355 List.map subst_inductive_type_with_dummy_mutind pl)
357 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
358 subst_inductive_type_with_dummy_mutind ty,
359 subst_inductive_type_with_dummy_mutind bo)) fl)
361 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
362 subst_inductive_type_with_dummy_mutind ty,
363 subst_inductive_type_with_dummy_mutind bo)) fl)
364 | C.Const (uri,exp_named_subst) ->
365 let exp_named_subst' =
367 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
370 C.Const (uri,exp_named_subst')
371 | C.MutInd (uri,typeno,exp_named_subst) ->
372 let exp_named_subst' =
374 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
377 C.MutInd (uri,typeno,exp_named_subst')
378 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
379 let exp_named_subst' =
381 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
384 C.MutConstruct (uri,typeno,consno,exp_named_subst')
387 match CicReduction.whd context te with
389 C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri -> true
391 C.Appl ((C.MutInd (uri',_,_))::tl) when UriManager.eq uri' uri -> true
392 | C.MutInd (uri',0,_) when UriManager.eq uri' uri -> true
393 | C.Prod (C.Anonymous,source,dest) ->
394 strictly_positive context n nn
395 (subst_inductive_type_with_dummy_mutind source) &&
396 weakly_positive ((Some (C.Anonymous,(C.Decl source)))::context)
397 (n + 1) (nn + 1) uri dest
398 | C.Prod (name,source,dest) when
399 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
400 (* dummy abstraction, so we behave as in the anonimous case *)
401 strictly_positive context n nn
402 (subst_inductive_type_with_dummy_mutind source) &&
403 weakly_positive ((Some (name,(C.Decl source)))::context)
404 (n + 1) (nn + 1) uri dest
405 | C.Prod (name,source,dest) ->
406 does_not_occur context n nn
407 (subst_inductive_type_with_dummy_mutind source)&&
408 weakly_positive ((Some (name,(C.Decl source)))::context)
409 (n + 1) (nn + 1) uri dest
411 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
413 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
414 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
415 and instantiate_parameters params c =
416 let module C = Cic in
417 match (c,params) with
419 | (C.Prod (_,_,ta), he::tl) ->
420 instantiate_parameters tl
421 (CicSubstitution.subst he ta)
422 | (C.Cast (te,_), _) -> instantiate_parameters params te
423 | (t,l) -> raise (AssertFailure (lazy "1"))
425 and strictly_positive context n nn te =
426 let module C = Cic in
427 let module U = UriManager in
428 match CicReduction.whd context te with
429 | t when does_not_occur context n nn t -> true
432 (*CSC: bisogna controllare ty????*)
433 strictly_positive context n nn te
434 | C.Prod (name,so,ta) ->
435 does_not_occur context n nn so &&
436 strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
437 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
438 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
439 | C.Appl ((C.MutInd (uri,i,exp_named_subst))::tl) ->
440 let (ok,paramsno,ity,cl,name) =
441 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
443 C.InductiveDefinition (tl,_,paramsno,_) ->
444 let (name,_,ity,cl) = List.nth tl i in
445 (List.length tl = 1, paramsno, ity, cl, name)
446 (* (true, paramsno, ity, cl, name) *)
450 (lazy ("Unknown inductive type:" ^ U.string_of_uri uri)))
452 let (params,arguments) = split tl paramsno in
453 let lifted_params = List.map (CicSubstitution.lift 1) params in
457 instantiate_parameters lifted_params
458 (CicSubstitution.subst_vars exp_named_subst te)
463 (fun x i -> i && does_not_occur context n nn x)
465 (*CSC: MEGAPATCH3 (sara' quella giusta?)*)
470 ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri
475 (* the inductive type indexes are s.t. n < x <= nn *)
476 and are_all_occurrences_positive context uri indparamsno i n nn te =
477 let module C = Cic in
478 match CicReduction.whd context te with
479 C.Appl ((C.Rel m)::tl) when m = i ->
480 (*CSC: riscrivere fermandosi a 0 *)
481 (* let's check if the inductive type is applied at least to *)
482 (* indparamsno parameters *)
488 match CicReduction.whd context x with
489 C.Rel m when m = n - (indparamsno - k) -> k - 1
491 raise (TypeCheckerFailure
493 ("Non-positive occurence in mutual inductive definition(s) [1]" ^
494 UriManager.string_of_uri uri)))
498 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
500 raise (TypeCheckerFailure
501 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
502 UriManager.string_of_uri uri)))
503 | C.Rel m when m = i ->
504 if indparamsno = 0 then
507 raise (TypeCheckerFailure
508 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
509 UriManager.string_of_uri uri)))
510 | C.Prod (C.Anonymous,source,dest) ->
511 let b = strictly_positive context n nn source in
513 are_all_occurrences_positive
514 ((Some (C.Anonymous,(C.Decl source)))::context) uri indparamsno
515 (i+1) (n + 1) (nn + 1) dest
516 | C.Prod (name,source,dest) when
517 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
518 (* dummy abstraction, so we behave as in the anonimous case *)
519 strictly_positive context n nn source &&
520 are_all_occurrences_positive
521 ((Some (name,(C.Decl source)))::context) uri indparamsno
522 (i+1) (n + 1) (nn + 1) dest
523 | C.Prod (name,source,dest) ->
524 does_not_occur context n nn source &&
525 are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
526 uri indparamsno (i+1) (n + 1) (nn + 1) dest
529 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
530 (UriManager.string_of_uri uri))))
532 (* Main function to checks the correctness of a mutual *)
533 (* inductive block definition. This is the function *)
534 (* exported to the proof-engine. *)
535 and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
536 let module U = UriManager in
537 (* let's check if the arity of the inductive types are well *)
539 let ugrap1 = List.fold_left
540 (fun ugraph (_,_,x,_) -> let _,ugraph' =
541 type_of ~logger x ugraph in ugraph')
544 (* let's check if the types of the inductive constructors *)
545 (* are well formed. *)
546 (* In order not to use type_of_aux we put the types of the *)
547 (* mutual inductive types at the head of the types of the *)
548 (* constructors using Prods *)
549 let len = List.length itl in
551 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
554 (fun (_,_,_,cl) (i,ugraph) ->
557 (fun ugraph (name,te) ->
558 let debrujinedte = debrujin_constructor uri len te in
561 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
564 let _,ugraph' = type_of ~logger augmented_term ugraph in
565 (* let's check also the positivity conditions *)
568 (are_all_occurrences_positive tys uri indparamsno i 0 len
572 prerr_endline (UriManager.string_of_uri uri);
573 prerr_endline (string_of_int (List.length tys));
576 (lazy ("Non positive occurence in " ^ U.string_of_uri uri))) end
585 (* Main function to checks the correctness of a mutual *)
586 (* inductive block definition. *)
587 and check_mutual_inductive_defs uri obj ugraph =
589 Cic.InductiveDefinition (itl, params, indparamsno, _) ->
590 typecheck_mutual_inductive_defs uri (itl,params,indparamsno) ugraph
592 raise (TypeCheckerFailure (
593 lazy ("Unknown mutual inductive definition:" ^
594 UriManager.string_of_uri uri)))
596 and type_of_mutual_inductive_defs ~logger uri i 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 (* TASSI: FIXME: check ugraph1 == ugraph ritornato da env *)
610 CicEnvironment.set_type_checking_info uri ;
611 logger#log (`Type_checking_completed uri) ;
612 (match CicEnvironment.is_type_checked ~trust:false ugraph uri with
613 CicEnvironment.CheckedObj (cobj,ugraph') -> (cobj,ugraph')
614 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
617 Invalid_argument s ->
618 (*debug_print (lazy s);*)
622 C.InductiveDefinition (dl,_,_,_) ->
623 let (_,_,arity,_) = List.nth dl i in
626 raise (TypeCheckerFailure
627 (lazy ("Unknown mutual inductive definition:" ^ U.string_of_uri uri)))
629 and type_of_mutual_inductive_constr ~logger uri i j ugraph =
630 let module C = Cic in
631 let module R = CicReduction in
632 let module U = UriManager in
634 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
635 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
636 | CicEnvironment.UncheckedObj uobj ->
637 logger#log (`Start_type_checking uri) ;
639 check_mutual_inductive_defs ~logger uri uobj ugraph
641 (* check ugraph1 validity ??? == ugraph' *)
643 CicEnvironment.set_type_checking_info uri ;
644 logger#log (`Type_checking_completed uri) ;
646 CicEnvironment.is_type_checked ~trust:false ugraph uri
648 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
649 | CicEnvironment.UncheckedObj _ ->
650 raise CicEnvironmentError)
652 Invalid_argument s ->
653 (*debug_print (lazy s);*)
657 C.InductiveDefinition (dl,_,_,_) ->
658 let (_,_,_,cl) = List.nth dl i in
659 let (_,ty) = List.nth cl (j-1) in
662 raise (TypeCheckerFailure
663 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
665 and recursive_args context n nn te =
666 let module C = Cic in
667 match CicReduction.whd context te with
673 | C.Cast _ (*CSC ??? *) ->
674 raise (AssertFailure (lazy "3")) (* due to type-checking *)
675 | C.Prod (name,so,de) ->
676 (not (does_not_occur context n nn so)) ::
677 (recursive_args ((Some (name,(C.Decl so)))::context) (n+1) (nn + 1) de)
680 raise (AssertFailure (lazy "4")) (* due to type-checking *)
682 | C.Const _ -> raise (AssertFailure (lazy "5"))
687 | C.CoFix _ -> raise (AssertFailure (lazy "6")) (* due to type-checking *)
689 and get_new_safes ~subst context p c rl safes n nn x =
690 let module C = Cic in
691 let module U = UriManager in
692 let module R = CicReduction in
693 match (R.whd ~subst context c, R.whd ~subst context p, rl) with
694 (C.Prod (_,so,ta1), C.Lambda (name,_,ta2), b::tl) ->
695 (* we are sure that the two sources are convertible because we *)
696 (* have just checked this. So let's go along ... *)
698 List.map (fun x -> x + 1) safes
701 if b then 1::safes' else safes'
703 get_new_safes ~subst ((Some (name,(C.Decl so)))::context)
704 ta2 ta1 tl safes'' (n+1) (nn+1) (x+1)
705 | (C.Prod _, (C.MutConstruct _ as e), _)
706 | (C.Prod _, (C.Rel _ as e), _)
707 | (C.MutInd _, e, [])
708 | (C.Appl _, e, []) -> (e,safes,n,nn,x,context)
710 (* CSC: If the next exception is raised, it just means that *)
711 (* CSC: the proof-assistant allows to use very strange things *)
712 (* CSC: as a branch of a case whose type is a Prod. In *)
713 (* CSC: particular, this means that a new (C.Prod, x,_) case *)
714 (* CSC: must be considered in this match. (e.g. x = MutCase) *)
717 (Printf.sprintf "Get New Safes: c=%s ; p=%s"
718 (CicPp.ppterm c) (CicPp.ppterm p))))
720 and split_prods ~subst context n te =
721 let module C = Cic in
722 let module R = CicReduction in
723 match (n, R.whd ~subst context te) with
725 | (n, C.Prod (name,so,ta)) when n > 0 ->
726 split_prods ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
727 | (_, _) -> raise (AssertFailure (lazy "8"))
729 and eat_lambdas ~subst context n te =
730 let module C = Cic in
731 let module R = CicReduction in
732 match (n, R.whd ~subst context te) with
733 (0, _) -> (te, 0, context)
734 | (n, C.Lambda (name,so,ta)) when n > 0 ->
735 let (te, k, context') =
736 eat_lambdas ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
738 (te, k + 1, context')
740 raise (AssertFailure (lazy (sprintf "9 (%d, %s)" n (CicPp.ppterm te))))
742 (*CSC: Tutto quello che segue e' l'intuzione di luca ;-) *)
743 and check_is_really_smaller_arg ~subst context n nn kl x safes te =
744 (*CSC: forse la whd si puo' fare solo quando serve veramente. *)
745 (*CSC: cfr guarded_by_destructors *)
746 let module C = Cic in
747 let module U = UriManager in
748 match CicReduction.whd ~subst context te with
749 C.Rel m when List.mem m safes -> true
756 (* | C.Cast (te,ty) ->
757 check_is_really_smaller_arg ~subst n nn kl x safes te &&
758 check_is_really_smaller_arg ~subst n nn kl x safes ty*)
759 (* | C.Prod (_,so,ta) ->
760 check_is_really_smaller_arg ~subst n nn kl x safes so &&
761 check_is_really_smaller_arg ~subst (n+1) (nn+1) kl (x+1)
762 (List.map (fun x -> x + 1) safes) ta*)
763 | C.Prod _ -> raise (AssertFailure (lazy "10"))
764 | C.Lambda (name,so,ta) ->
765 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
766 check_is_really_smaller_arg ~subst ((Some (name,(C.Decl so)))::context)
767 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
768 | C.LetIn (name,so,ta) ->
769 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
770 check_is_really_smaller_arg ~subst ((Some (name,(C.Def (so,None))))::context)
771 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
773 (*CSC: sulla coda ci vogliono dei controlli? secondo noi no, ma *)
774 (*CSC: solo perche' non abbiamo trovato controesempi *)
775 check_is_really_smaller_arg ~subst context n nn kl x safes he
776 | C.Appl [] -> raise (AssertFailure (lazy "11"))
778 | C.MutInd _ -> raise (AssertFailure (lazy "12"))
779 | C.MutConstruct _ -> false
780 | C.MutCase (uri,i,outtype,term,pl) ->
782 C.Rel m when List.mem m safes || m = x ->
783 let (lefts_and_tys,len,isinductive,paramsno,cl) =
784 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
786 C.InductiveDefinition (tl,_,paramsno,_) ->
789 (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) tl
791 let (_,isinductive,_,cl) = List.nth tl i in
795 (id, snd (split_prods ~subst tys paramsno ty))) cl in
800 fst (split_prods ~subst [] paramsno ty)
802 (tys@lefts,List.length tl,isinductive,paramsno,cl')
804 raise (TypeCheckerFailure
805 (lazy ("Unknown mutual inductive definition:" ^
806 UriManager.string_of_uri uri)))
808 if not isinductive then
811 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
818 Invalid_argument _ ->
819 raise (TypeCheckerFailure (lazy "not enough patterns"))
824 let debrujinedte = debrujin_constructor uri len c in
825 recursive_args lefts_and_tys 0 len debrujinedte
827 let (e,safes',n',nn',x',context') =
828 get_new_safes ~subst context p c rl' safes n nn x
831 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
833 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
834 let (lefts_and_tys,len,isinductive,paramsno,cl) =
835 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
837 C.InductiveDefinition (tl,_,paramsno,_) ->
838 let (_,isinductive,_,cl) = List.nth tl i in
840 List.map (fun (n,_,ty,_) ->
841 Some(Cic.Name n,(Cic.Decl ty))) tl
846 (id, snd (split_prods ~subst tys paramsno ty))) cl in
851 fst (split_prods ~subst [] paramsno ty)
853 (tys@lefts,List.length tl,isinductive,paramsno,cl')
855 raise (TypeCheckerFailure
856 (lazy ("Unknown mutual inductive definition:" ^
857 UriManager.string_of_uri uri)))
859 if not isinductive then
862 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
869 Invalid_argument _ ->
870 raise (TypeCheckerFailure (lazy "not enough patterns"))
872 (*CSC: supponiamo come prima che nessun controllo sia necessario*)
873 (*CSC: sugli argomenti di una applicazione *)
877 let debrujinedte = debrujin_constructor uri len c in
878 recursive_args lefts_and_tys 0 len debrujinedte
880 let (e, safes',n',nn',x',context') =
881 get_new_safes ~subst context p c rl' safes n nn x
884 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
889 i && check_is_really_smaller_arg ~subst context n nn kl x safes p
893 let len = List.length fl in
894 let n_plus_len = n + len
895 and nn_plus_len = nn + len
896 and x_plus_len = x + len
899 (fun (types,len) (n,_,ty,_) ->
900 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
903 and safes' = List.map (fun x -> x + len) safes in
905 (fun (_,_,ty,bo) i ->
907 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
911 let len = List.length fl in
912 let n_plus_len = n + len
913 and nn_plus_len = nn + len
914 and x_plus_len = x + len
917 (fun (types,len) (n,ty,_) ->
918 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
921 and safes' = List.map (fun x -> x + len) safes in
925 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
929 and guarded_by_destructors ~subst context n nn kl x safes =
930 let module C = Cic in
931 let module U = UriManager in
933 C.Rel m when m > n && m <= nn -> false
935 (match List.nth context (n-1) with
936 Some (_,C.Decl _) -> true
937 | Some (_,C.Def (bo,_)) ->
938 guarded_by_destructors ~subst context m nn kl x safes
939 (CicSubstitution.lift m bo)
940 | None -> raise (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
944 | C.Implicit _ -> true
946 guarded_by_destructors ~subst context n nn kl x safes te &&
947 guarded_by_destructors ~subst context n nn kl x safes ty
948 | C.Prod (name,so,ta) ->
949 guarded_by_destructors ~subst context n nn kl x safes so &&
950 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
951 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
952 | C.Lambda (name,so,ta) ->
953 guarded_by_destructors ~subst context n nn kl x safes so &&
954 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
955 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
956 | C.LetIn (name,so,ta) ->
957 guarded_by_destructors ~subst context n nn kl x safes so &&
958 guarded_by_destructors ~subst ((Some (name,(C.Def (so,None))))::context)
959 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
960 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
961 let k = List.nth kl (m - n - 1) in
962 if not (List.length tl > k) then false
966 i && guarded_by_destructors ~subst context n nn kl x safes param
968 check_is_really_smaller_arg ~subst context n nn kl x safes (List.nth tl k)
971 (fun t i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
973 | C.Var (_,exp_named_subst)
974 | C.Const (_,exp_named_subst)
975 | C.MutInd (_,_,exp_named_subst)
976 | C.MutConstruct (_,_,_,exp_named_subst) ->
978 (fun (_,t) i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
980 | C.MutCase (uri,i,outtype,term,pl) ->
981 (match CicReduction.whd ~subst context term with
982 C.Rel m when List.mem m safes || m = x ->
983 let (lefts_and_tys,len,isinductive,paramsno,cl) =
984 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
986 C.InductiveDefinition (tl,_,paramsno,_) ->
987 let len = List.length tl in
988 let (_,isinductive,_,cl) = List.nth tl i in
990 List.map (fun (n,_,ty,_) ->
991 Some(Cic.Name n,(Cic.Decl ty))) tl
996 let debrujinedty = debrujin_constructor uri len ty in
997 (id, snd (split_prods ~subst tys paramsno ty),
998 snd (split_prods ~subst tys paramsno debrujinedty)
1004 fst (split_prods ~subst [] paramsno ty)
1006 (tys@lefts,len,isinductive,paramsno,cl')
1008 raise (TypeCheckerFailure
1009 (lazy ("Unknown mutual inductive definition:" ^
1010 UriManager.string_of_uri uri)))
1012 if not isinductive then
1013 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1014 guarded_by_destructors ~subst context n nn kl x safes term &&
1015 (*CSC: manca ??? il controllo sul tipo di term? *)
1018 i && guarded_by_destructors ~subst context n nn kl x safes p)
1025 Invalid_argument _ ->
1026 raise (TypeCheckerFailure (lazy "not enough patterns"))
1028 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1029 (*CSC: manca ??? il controllo sul tipo di term? *)
1031 (fun (p,(_,c,brujinedc)) i ->
1032 let rl' = recursive_args lefts_and_tys 0 len brujinedc in
1033 let (e,safes',n',nn',x',context') =
1034 get_new_safes ~subst context p c rl' safes n nn x
1037 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1039 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
1040 let (lefts_and_tys,len,isinductive,paramsno,cl) =
1041 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1043 C.InductiveDefinition (tl,_,paramsno,_) ->
1044 let (_,isinductive,_,cl) = List.nth tl i in
1047 (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
1052 (id, snd (split_prods ~subst tys paramsno ty))) cl in
1057 fst (split_prods ~subst [] paramsno ty)
1059 (tys@lefts,List.length tl,isinductive,paramsno,cl')
1061 raise (TypeCheckerFailure
1062 (lazy ("Unknown mutual inductive definition:" ^
1063 UriManager.string_of_uri uri)))
1065 if not isinductive then
1066 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1067 guarded_by_destructors ~subst context n nn kl x safes term &&
1068 (*CSC: manca ??? il controllo sul tipo di term? *)
1071 i && guarded_by_destructors ~subst context n nn kl x safes p)
1078 Invalid_argument _ ->
1079 raise (TypeCheckerFailure (lazy "not enough patterns"))
1081 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1082 (*CSC: manca ??? il controllo sul tipo di term? *)
1085 i && guarded_by_destructors ~subst context n nn kl x safes t)
1090 let debrujinedte = debrujin_constructor uri len c in
1091 recursive_args lefts_and_tys 0 len debrujinedte
1093 let (e, safes',n',nn',x',context') =
1094 get_new_safes ~subst context p c rl' safes n nn x
1097 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1100 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1101 guarded_by_destructors ~subst context n nn kl x safes term &&
1102 (*CSC: manca ??? il controllo sul tipo di term? *)
1104 (fun p i -> i && guarded_by_destructors ~subst context n nn kl x safes p)
1108 let len = List.length fl in
1109 let n_plus_len = n + len
1110 and nn_plus_len = nn + len
1111 and x_plus_len = x + len
1114 (fun (types,len) (n,_,ty,_) ->
1115 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1118 and safes' = List.map (fun x -> x + len) safes in
1120 (fun (_,_,ty,bo) i ->
1121 i && guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1122 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1123 x_plus_len safes' bo
1125 | C.CoFix (_, fl) ->
1126 let len = List.length fl in
1127 let n_plus_len = n + len
1128 and nn_plus_len = nn + len
1129 and x_plus_len = x + len
1132 (fun (types,len) (n,ty,_) ->
1133 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1136 and safes' = List.map (fun x -> x + len) safes in
1140 guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1141 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1142 x_plus_len safes' bo
1145 (* the boolean h means already protected *)
1146 (* args is the list of arguments the type of the constructor that may be *)
1147 (* found in head position must be applied to. *)
1148 and guarded_by_constructors ~subst context n nn h te args coInductiveTypeURI =
1149 let module C = Cic in
1150 (*CSC: There is a lot of code replication between the cases X and *)
1151 (*CSC: (C.Appl X tl). Maybe it will be better to define a function *)
1152 (*CSC: that maps X into (C.Appl X []) when X is not already a C.Appl *)
1153 match CicReduction.whd ~subst context te with
1154 C.Rel m when m > n && m <= nn -> h
1162 (* the term has just been type-checked *)
1163 raise (AssertFailure (lazy "17"))
1164 | C.Lambda (name,so,de) ->
1165 does_not_occur ~subst context n nn so &&
1166 guarded_by_constructors ~subst ((Some (name,(C.Decl so)))::context)
1167 (n + 1) (nn + 1) h de args coInductiveTypeURI
1168 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
1170 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) tl true
1171 | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
1175 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1176 with Not_found -> assert false
1179 C.InductiveDefinition (itl,_,_,_) ->
1180 let (_,_,_,cl) = List.nth itl i in
1181 let (_,cons) = List.nth cl (j - 1) in
1182 CicSubstitution.subst_vars exp_named_subst cons
1184 raise (TypeCheckerFailure
1185 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
1187 let rec analyse_branch context ty te =
1188 match CicReduction.whd ~subst context ty with
1189 C.Meta _ -> raise (AssertFailure (lazy "34"))
1193 does_not_occur ~subst context n nn te
1196 raise (AssertFailure (lazy "24"))(* due to type-checking *)
1197 | C.Prod (name,so,de) ->
1198 analyse_branch ((Some (name,(C.Decl so)))::context) de te
1201 raise (AssertFailure (lazy "25"))(* due to type-checking *)
1202 | C.Appl ((C.MutInd (uri,_,_))::_) when uri == coInductiveTypeURI ->
1203 guarded_by_constructors ~subst context n nn true te []
1205 | C.Appl ((C.MutInd (uri,_,_))::_) ->
1206 guarded_by_constructors ~subst context n nn true te tl
1209 does_not_occur ~subst context n nn te
1210 | C.Const _ -> raise (AssertFailure (lazy "26"))
1211 | C.MutInd (uri,_,_) when uri == coInductiveTypeURI ->
1212 guarded_by_constructors ~subst context n nn true te []
1215 does_not_occur ~subst context n nn te
1216 | C.MutConstruct _ -> raise (AssertFailure (lazy "27"))
1217 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1218 (*CSC: in head position. *)
1222 raise (AssertFailure (lazy "28"))(* due to type-checking *)
1224 let rec analyse_instantiated_type context ty l =
1225 match CicReduction.whd ~subst context ty with
1231 | C.Cast _ -> raise (AssertFailure (lazy "29"))(* due to type-checking *)
1232 | C.Prod (name,so,de) ->
1237 analyse_branch context so he &&
1238 analyse_instantiated_type
1239 ((Some (name,(C.Decl so)))::context) de tl
1243 raise (AssertFailure (lazy "30"))(* due to type-checking *)
1246 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1247 | C.Const _ -> raise (AssertFailure (lazy "31"))
1250 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1251 | C.MutConstruct _ -> raise (AssertFailure (lazy "32"))
1252 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1253 (*CSC: in head position. *)
1257 raise (AssertFailure (lazy "33"))(* due to type-checking *)
1259 let rec instantiate_type args consty =
1262 | tlhe::tltl as l ->
1263 let consty' = CicReduction.whd ~subst context consty in
1269 let instantiated_de = CicSubstitution.subst he de in
1270 (*CSC: siamo sicuri che non sia troppo forte? *)
1271 does_not_occur ~subst context n nn tlhe &
1272 instantiate_type tl instantiated_de tltl
1274 (*CSC:We do not consider backbones with a MutCase, a *)
1275 (*CSC:FixPoint, a CoFixPoint and so on in head position.*)
1276 raise (AssertFailure (lazy "23"))
1278 | [] -> analyse_instantiated_type context consty' l
1279 (* These are all the other cases *)
1281 instantiate_type args consty tl
1282 | C.Appl ((C.CoFix (_,fl))::tl) ->
1283 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1284 let len = List.length fl in
1285 let n_plus_len = n + len
1286 and nn_plus_len = nn + len
1287 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1290 (fun (types,len) (n,ty,_) ->
1291 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1297 i && does_not_occur ~subst context n nn ty &&
1298 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1299 h bo args coInductiveTypeURI
1301 | C.Appl ((C.MutCase (_,_,out,te,pl))::tl) ->
1302 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1303 does_not_occur ~subst context n nn out &&
1304 does_not_occur ~subst context n nn te &&
1308 guarded_by_constructors ~subst context n nn h x args
1312 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
1313 | C.Var (_,exp_named_subst)
1314 | C.Const (_,exp_named_subst) ->
1316 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1317 | C.MutInd _ -> assert false
1318 | C.MutConstruct (_,_,_,exp_named_subst) ->
1320 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1321 | C.MutCase (_,_,out,te,pl) ->
1322 does_not_occur ~subst context n nn out &&
1323 does_not_occur ~subst context n nn te &&
1327 guarded_by_constructors ~subst context n nn h x args
1331 let len = List.length fl in
1332 let n_plus_len = n + len
1333 and nn_plus_len = nn + len
1334 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1337 (fun (types,len) (n,_,ty,_) ->
1338 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1343 (fun (_,_,ty,bo) i ->
1344 i && does_not_occur ~subst context n nn ty &&
1345 does_not_occur ~subst (tys@context) n_plus_len nn_plus_len bo
1348 let len = List.length fl in
1349 let n_plus_len = n + len
1350 and nn_plus_len = nn + len
1351 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1354 (fun (types,len) (n,ty,_) ->
1355 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1361 i && does_not_occur ~subst context n nn ty &&
1362 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1364 args coInductiveTypeURI
1367 and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1368 need_dummy ind arity1 arity2 ugraph =
1369 let module C = Cic in
1370 let module U = UriManager in
1371 let arity1 = CicReduction.whd ~subst context arity1 in
1372 let rec check_allowed_sort_elimination_aux ugraph context arity2 need_dummy =
1373 match arity1, CicReduction.whd ~subst context arity2 with
1374 (C.Prod (_,so1,de1), C.Prod (_,so2,de2)) ->
1376 CicReduction.are_convertible ~subst ~metasenv context so1 so2 ugraph in
1378 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1379 need_dummy (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1383 | (C.Sort _, C.Prod (name,so,ta)) when not need_dummy ->
1385 CicReduction.are_convertible ~subst ~metasenv context so ind ugraph in
1389 check_allowed_sort_elimination_aux ugraph1
1390 ((Some (name,C.Decl so))::context) ta true
1391 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
1392 | (C.Sort C.Prop, C.Sort C.Set)
1393 | (C.Sort C.Prop, C.Sort C.CProp)
1394 | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
1395 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1397 C.InductiveDefinition (itl,_,paramsno,_) ->
1398 let itl_len = List.length itl in
1399 let (name,_,ty,cl) = List.nth itl i in
1400 let cl_len = List.length cl in
1401 if (cl_len = 0 || (itl_len = 1 && cl_len = 1)) then
1402 let non_informative,ugraph =
1403 if cl_len = 0 then true,ugraph
1405 is_non_informative ~logger [Some (C.Name name,C.Decl ty)]
1406 paramsno (snd (List.nth cl 0)) ugraph
1408 (* is it a singleton or empty non recursive and non informative
1410 non_informative, ugraph
1414 raise (TypeCheckerFailure
1415 (lazy ("Unknown mutual inductive definition:" ^
1416 UriManager.string_of_uri uri)))
1418 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
1419 | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
1420 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
1421 | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
1422 | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
1423 | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
1424 | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
1426 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1428 C.InductiveDefinition (itl,_,paramsno,_) ->
1430 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1432 let (_,_,_,cl) = List.nth itl i in
1434 (fun (_,x) (i,ugraph) ->
1436 is_small ~logger tys paramsno x ugraph
1441 raise (TypeCheckerFailure
1442 (lazy ("Unknown mutual inductive definition:" ^
1443 UriManager.string_of_uri uri)))
1445 | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
1446 | (_,_) -> false,ugraph
1448 check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
1450 and type_of_branch ~subst context argsno need_dummy outtype term constype =
1451 let module C = Cic in
1452 let module R = CicReduction in
1453 match R.whd ~subst context constype with
1458 C.Appl [outtype ; term]
1459 | C.Appl (C.MutInd (_,_,_)::tl) ->
1460 let (_,arguments) = split tl argsno
1462 if need_dummy && arguments = [] then
1465 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1466 | C.Prod (name,so,de) ->
1468 match CicSubstitution.lift 1 term with
1469 C.Appl l -> C.Appl (l@[C.Rel 1])
1470 | t -> C.Appl [t ; C.Rel 1]
1472 C.Prod (name,so,type_of_branch ~subst
1473 ((Some (name,(C.Decl so)))::context) argsno need_dummy
1474 (CicSubstitution.lift 1 outtype) term' de)
1475 | _ -> raise (AssertFailure (lazy "20"))
1477 (* check_metasenv_consistency checks that the "canonical" context of a
1478 metavariable is consitent - up to relocation via the relocation list l -
1479 with the actual context *)
1482 and check_metasenv_consistency ~logger ~subst metasenv context
1483 canonical_context l ugraph
1485 let module C = Cic in
1486 let module R = CicReduction in
1487 let module S = CicSubstitution in
1488 let lifted_canonical_context =
1492 | (Some (n,C.Decl t))::tl ->
1493 (Some (n,C.Decl (S.subst_meta l (S.lift i t))))::(aux (i+1) tl)
1494 | (Some (n,C.Def (t,None)))::tl ->
1495 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),None)))::(aux (i+1) tl)
1496 | None::tl -> None::(aux (i+1) tl)
1497 | (Some (n,C.Def (t,Some ty)))::tl ->
1498 (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)
1500 aux 1 canonical_context
1506 | Some t,Some (_,C.Def (ct,_)) ->
1508 R.are_convertible ~subst ~metasenv context t ct ugraph
1513 (lazy (sprintf "Not well typed metavariable local context: expected a term convertible with %s, found %s" (CicPp.ppterm ct) (CicPp.ppterm t))))
1516 | Some t,Some (_,C.Decl ct) ->
1517 let type_t,ugraph1 =
1518 type_of_aux' ~logger ~subst metasenv context t ugraph
1521 R.are_convertible ~subst ~metasenv context type_t ct ugraph1
1524 raise (TypeCheckerFailure
1525 (lazy (sprintf "Not well typed metavariable local context: expected a term of type %s, found %s of type %s"
1526 (CicPp.ppterm ct) (CicPp.ppterm t)
1527 (CicPp.ppterm type_t))))
1531 raise (TypeCheckerFailure
1532 (lazy ("Not well typed metavariable local context: "^
1533 "an hypothesis, that is not hidden, is not instantiated")))
1534 ) ugraph l lifted_canonical_context
1538 type_of_aux' is just another name (with a different scope)
1542 and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
1543 let rec type_of_aux ~logger context t ugraph =
1544 let module C = Cic in
1545 let module R = CicReduction in
1546 let module S = CicSubstitution in
1547 let module U = UriManager in
1551 match List.nth context (n - 1) with
1552 Some (_,C.Decl t) -> S.lift n t,ugraph
1553 | Some (_,C.Def (_,Some ty)) -> S.lift n ty,ugraph
1554 | Some (_,C.Def (bo,None)) ->
1555 debug_print (lazy "##### CASO DA INVESTIGARE E CAPIRE") ;
1556 type_of_aux ~logger context (S.lift n bo) ugraph
1558 (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
1561 raise (TypeCheckerFailure (lazy "unbound variable"))
1563 | C.Var (uri,exp_named_subst) ->
1566 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1568 let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
1569 let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
1574 let (canonical_context,term,ty) = CicUtil.lookup_subst n subst in
1576 check_metasenv_consistency ~logger
1577 ~subst metasenv context canonical_context l ugraph
1579 (* assuming subst is well typed !!!!! *)
1580 ((CicSubstitution.subst_meta l ty), ugraph1)
1581 (* type_of_aux context (CicSubstitution.subst_meta l term) *)
1582 with CicUtil.Subst_not_found _ ->
1583 let (_,canonical_context,ty) = CicUtil.lookup_meta n metasenv in
1585 check_metasenv_consistency ~logger
1586 ~subst metasenv context canonical_context l ugraph
1588 ((CicSubstitution.subst_meta l ty),ugraph1))
1589 (* TASSI: CONSTRAINTS *)
1590 | C.Sort (C.Type t) ->
1591 let t' = CicUniv.fresh() in
1593 let ugraph1 = CicUniv.add_gt t' t ugraph in
1594 (C.Sort (C.Type t')),ugraph1
1596 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
1597 | C.Sort s -> (C.Sort (C.Type (CicUniv.fresh ()))),ugraph
1598 | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
1599 | C.Cast (te,ty) as t ->
1600 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1601 let ty_te,ugraph2 = type_of_aux ~logger context te ugraph1 in
1603 R.are_convertible ~subst ~metasenv context ty_te ty ugraph2
1608 raise (TypeCheckerFailure
1609 (lazy (sprintf "Invalid cast %s" (CicPp.ppterm t))))
1610 | C.Prod (name,s,t) ->
1611 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1613 type_of_aux ~logger ((Some (name,(C.Decl s)))::context) t ugraph1
1615 sort_of_prod ~subst context (name,s) (sort1,sort2) ugraph2
1616 | C.Lambda (n,s,t) ->
1617 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1618 (match R.whd ~subst context sort1 with
1623 (TypeCheckerFailure (lazy (sprintf
1624 "Not well-typed lambda-abstraction: the source %s should be a type; instead it is a term of type %s" (CicPp.ppterm s)
1625 (CicPp.ppterm sort1))))
1628 type_of_aux ~logger ((Some (n,(C.Decl s)))::context) t ugraph1
1630 (C.Prod (n,s,type2)),ugraph2
1631 | C.LetIn (n,s,t) ->
1632 (* only to check if s is well-typed *)
1633 let ty,ugraph1 = type_of_aux ~logger context s ugraph in
1634 (* The type of a LetIn is a LetIn. Extremely slow since the computed
1635 LetIn is later reduced and maybe also re-checked.
1636 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t))
1638 (* The type of the LetIn is reduced. Much faster than the previous
1639 solution. Moreover the inferred type is probably very different
1640 from the expected one.
1641 (CicReduction.whd ~subst context
1642 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)))
1644 (* One-step LetIn reduction. Even faster than the previous solution.
1645 Moreover the inferred type is closer to the expected one. *)
1648 ((Some (n,(C.Def (s,Some ty))))::context) t ugraph1
1650 (CicSubstitution.subst ~avoid_beta_redexes:true s ty1),ugraph2
1651 | C.Appl (he::tl) when List.length tl > 0 ->
1652 let hetype,ugraph1 = type_of_aux ~logger context he ugraph in
1653 let tlbody_and_type,ugraph2 =
1656 let ty,ugraph1 = type_of_aux ~logger context x ugraph in
1657 let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in
1658 ((x,ty)::l,ugraph1))
1661 (* TASSI: questa c'era nel mio... ma non nel CVS... *)
1662 (* let _,ugraph2 = type_of_aux context hetype ugraph2 in *)
1663 eat_prods ~subst context hetype tlbody_and_type ugraph2
1664 | C.Appl _ -> raise (AssertFailure (lazy "Appl: no arguments"))
1665 | C.Const (uri,exp_named_subst) ->
1668 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1670 let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
1672 CicSubstitution.subst_vars exp_named_subst cty
1676 | C.MutInd (uri,i,exp_named_subst) ->
1679 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1681 (* TASSI: da me c'era anche questa, ma in CVS no *)
1682 let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
1683 (* fine parte dubbia *)
1685 CicSubstitution.subst_vars exp_named_subst mty
1689 | C.MutConstruct (uri,i,j,exp_named_subst) ->
1691 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1693 (* TASSI: idem come sopra *)
1695 type_of_mutual_inductive_constr ~logger uri i j ugraph1
1698 CicSubstitution.subst_vars exp_named_subst mty
1701 | C.MutCase (uri,i,outtype,term,pl) ->
1702 let outsort,ugraph1 = type_of_aux ~logger context outtype ugraph in
1703 let (need_dummy, k) =
1704 let rec guess_args context t =
1705 let outtype = CicReduction.whd ~subst context t in
1707 C.Sort _ -> (true, 0)
1708 | C.Prod (name, s, t) ->
1710 guess_args ((Some (name,(C.Decl s)))::context) t in
1712 (* last prod before sort *)
1713 match CicReduction.whd ~subst context s with
1714 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1715 C.MutInd (uri',i',_) when U.eq uri' uri && i' = i ->
1717 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1718 | C.Appl ((C.MutInd (uri',i',_)) :: _)
1719 when U.eq uri' uri && i' = i -> (false, 1)
1727 "Malformed case analasys' output type %s"
1728 (CicPp.ppterm outtype))))
1731 let (parameters, arguments, exp_named_subst),ugraph2 =
1732 let ty,ugraph2 = type_of_aux context term ugraph1 in
1733 match R.whd ~subst context ty with
1734 (*CSC manca il caso dei CAST *)
1735 (*CSC: ma servono i parametri (uri,i)? Se si', perche' non serve anche il *)
1736 (*CSC: parametro exp_named_subst? Se no, perche' non li togliamo? *)
1737 (*CSC: Hint: nella DTD servono per gli stylesheet. *)
1738 C.MutInd (uri',i',exp_named_subst) as typ ->
1739 if U.eq uri uri' && i = i' then
1740 ([],[],exp_named_subst),ugraph2
1745 ("Case analysys: analysed term type is %s, but is expected to be (an application of) %s#1/%d{_}")
1746 (CicPp.ppterm typ) (U.string_of_uri uri) i)))
1748 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1749 if U.eq uri uri' && i = i' then
1751 split tl (List.length tl - k)
1752 in (params,args,exp_named_subst),ugraph2
1757 ("Case analysys: analysed term type is %s, "^
1758 "but is expected to be (an application of) "^
1760 (CicPp.ppterm typ') (U.string_of_uri uri) i)))
1766 "analysed term %s is not an inductive one")
1767 (CicPp.ppterm term))))
1769 let (b, k) = guess_args context outsort in
1770 if not b then (b, k - 1) else (b, k) in
1771 let (parameters, arguments, exp_named_subst),ugraph2 =
1772 let ty,ugraph2 = type_of_aux ~logger context term ugraph1 in
1773 match R.whd ~subst context ty with
1774 C.MutInd (uri',i',exp_named_subst) as typ ->
1775 if U.eq uri uri' && i = i' then
1776 ([],[],exp_named_subst),ugraph2
1780 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1781 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1782 | C.Appl ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) ->
1783 if U.eq uri uri' && i = i' then
1785 split tl (List.length tl - k)
1786 in (params,args,exp_named_subst),ugraph2
1790 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1791 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1796 "Case analysis: analysed term %s is not an inductive one"
1797 (CicPp.ppterm term))))
1800 let's control if the sort elimination is allowed:
1803 let sort_of_ind_type =
1804 if parameters = [] then
1805 C.MutInd (uri,i,exp_named_subst)
1807 C.Appl ((C.MutInd (uri,i,exp_named_subst))::parameters)
1809 let type_of_sort_of_ind_ty,ugraph3 =
1810 type_of_aux ~logger context sort_of_ind_type ugraph2 in
1812 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1813 need_dummy sort_of_ind_type type_of_sort_of_ind_ty outsort ugraph3
1817 (TypeCheckerFailure (lazy ("Case analysis: sort elimination not allowed")));
1818 (* let's check if the type of branches are right *)
1819 let parsno,constructorsno =
1822 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1823 with Not_found -> assert false
1826 C.InductiveDefinition (il,_,parsno,_) ->
1828 try List.nth il i with Failure _ -> assert false
1830 parsno, List.length cl
1832 raise (TypeCheckerFailure
1833 (lazy ("Unknown mutual inductive definition:" ^
1834 UriManager.string_of_uri uri)))
1836 if List.length pl <> constructorsno then
1837 raise (TypeCheckerFailure
1838 (lazy ("Wrong number of cases in case analysis"))) ;
1839 let (_,branches_ok,ugraph5) =
1841 (fun (j,b,ugraph) p ->
1844 if parameters = [] then
1845 (C.MutConstruct (uri,i,j,exp_named_subst))
1848 (C.MutConstruct (uri,i,j,exp_named_subst)::parameters))
1850 let ty_p,ugraph1 = type_of_aux ~logger context p ugraph in
1851 let ty_cons,ugraph3 = type_of_aux ~logger context cons ugraph1 in
1854 type_of_branch ~subst context parsno need_dummy outtype cons
1858 ~subst ~metasenv context ty_p ty_branch ugraph3
1863 prerr_endline ("\n!OUTTYPE= " ^ CicPp.ppterm outtype);
1864 prerr_endline ("!CONS= " ^ CicPp.ppterm cons);
1865 prerr_endline ("!TY_CONS= " ^ CicPp.ppterm ty_cons);
1866 prerr_endline ("#### " ^ CicPp.ppterm ty_p ^ "\n<==>\n" ^ CicPp.ppterm ty_branch);
1871 ("#### " ^ CicPp.ppterm ty_p ^
1872 " <==> " ^ CicPp.ppterm ty_branch));
1876 ) (1,true,ugraph4) pl
1878 if not branches_ok then
1880 (TypeCheckerFailure (lazy "Case analysys: wrong branch type"));
1882 if not need_dummy then outtype::arguments@[term]
1883 else outtype::arguments in
1885 if need_dummy && arguments = [] then outtype
1886 else CicReduction.head_beta_reduce (C.Appl arguments')
1890 let types,kl,ugraph1,len =
1892 (fun (types,kl,ugraph,len) (n,k,ty,_) ->
1893 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1894 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1895 k::kl,ugraph1,len+1)
1896 ) ([],[],ugraph,0) fl
1900 (fun ugraph (name,x,ty,bo) ->
1902 type_of_aux ~logger (types@context) bo ugraph
1905 R.are_convertible ~subst ~metasenv (types@context)
1906 ty_bo (CicSubstitution.lift len ty) ugraph1 in
1909 let (m, eaten, context') =
1910 eat_lambdas ~subst (types @ context) (x + 1) bo
1913 let's control the guarded by
1914 destructors conditions D{f,k,x,M}
1916 if not (guarded_by_destructors ~subst context' eaten
1917 (len + eaten) kl 1 [] m) then
1920 (lazy ("Fix: not guarded by destructors")))
1925 raise (TypeCheckerFailure (lazy ("Fix: ill-typed bodies")))
1927 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1928 let (_,_,ty,_) = List.nth fl i in
1931 let types,ugraph1,len =
1933 (fun (l,ugraph,len) (n,ty,_) ->
1935 type_of_aux ~logger context ty ugraph in
1936 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::l,
1942 (fun ugraph (_,ty,bo) ->
1944 type_of_aux ~logger (types @ context) bo ugraph
1947 R.are_convertible ~subst ~metasenv (types @ context) ty_bo
1948 (CicSubstitution.lift len ty) ugraph1
1952 (* let's control that the returned type is coinductive *)
1953 match returns_a_coinductive ~subst context ty with
1957 (lazy "CoFix: does not return a coinductive type"))
1960 let's control the guarded by constructors
1963 if not (guarded_by_constructors ~subst
1964 (types @ context) 0 len false bo [] uri) then
1967 (lazy "CoFix: not guarded by constructors"))
1973 (TypeCheckerFailure (lazy "CoFix: ill-typed bodies"))
1976 let (_,ty,_) = List.nth fl i in
1979 and check_exp_named_subst ~logger ~subst context ugraph =
1980 let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
1983 | ((uri,t) as item)::tl ->
1984 let ty_uri,ugraph1 = type_of_variable ~logger uri ugraph in
1986 CicSubstitution.subst_vars esubsts ty_uri in
1987 let typeoft,ugraph2 = type_of_aux ~logger context t ugraph1 in
1989 CicReduction.are_convertible ~subst ~metasenv
1990 context typeoft typeofvar ugraph2
1993 check_exp_named_subst_aux ~logger (esubsts@[item]) tl ugraph3
1996 CicReduction.fdebug := 0 ;
1998 (CicReduction.are_convertible
1999 ~subst ~metasenv context typeoft typeofvar ugraph2) ;
2001 debug typeoft [typeofvar] ;
2002 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
2005 check_exp_named_subst_aux ~logger [] ugraph
2007 and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
2008 let module C = Cic in
2009 let t1' = CicReduction.whd ~subst context t1 in
2010 let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
2011 match (t1', t2') with
2012 (C.Sort s1, C.Sort s2)
2013 when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) ->
2014 (* different from Coq manual!!! *)
2016 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
2017 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
2018 let t' = CicUniv.fresh() in
2020 let ugraph1 = CicUniv.add_ge t' t1 ugraph in
2021 let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
2022 C.Sort (C.Type t'),ugraph2
2024 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
2025 | (C.Sort _,C.Sort (C.Type t1)) ->
2026 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
2027 C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
2028 | (C.Meta _, C.Sort _) -> t2',ugraph
2029 | (C.Meta _, (C.Meta (_,_) as t))
2030 | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
2032 | (_,_) -> raise (TypeCheckerFailure (lazy (sprintf
2033 "Prod: expected two sorts, found = %s, %s" (CicPp.ppterm t1')
2034 (CicPp.ppterm t2'))))
2036 and eat_prods ~subst context hetype l ugraph =
2037 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
2041 | (hete, hety)::tl ->
2042 (match (CicReduction.whd ~subst context hetype) with
2045 CicReduction.are_convertible
2046 ~subst ~metasenv context hety s ugraph
2050 CicReduction.fdebug := -1 ;
2051 eat_prods ~subst context
2052 (CicSubstitution.subst ~avoid_beta_redexes:true hete t)
2054 (*TASSI: not sure *)
2058 CicReduction.fdebug := 0 ;
2059 ignore (CicReduction.are_convertible
2060 ~subst ~metasenv context s hety ugraph) ;
2066 ("Appl: wrong parameter-type, expected %s, found %s")
2067 (CicPp.ppterm hetype) (CicPp.ppterm s))))
2070 raise (TypeCheckerFailure
2071 (lazy "Appl: this is not a function, it cannot be applied"))
2074 and returns_a_coinductive ~subst context ty =
2075 let module C = Cic in
2076 match CicReduction.whd ~subst context ty with
2077 C.MutInd (uri,i,_) ->
2078 (*CSC: definire una funzioncina per questo codice sempre replicato *)
2081 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
2082 with Not_found -> assert false
2085 C.InductiveDefinition (itl,_,_,_) ->
2086 let (_,is_inductive,_,_) = List.nth itl i in
2087 if is_inductive then None else (Some uri)
2089 raise (TypeCheckerFailure
2090 (lazy ("Unknown mutual inductive definition:" ^
2091 UriManager.string_of_uri uri)))
2093 | C.Appl ((C.MutInd (uri,i,_))::_) ->
2094 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
2096 C.InductiveDefinition (itl,_,_,_) ->
2097 let (_,is_inductive,_,_) = List.nth itl i in
2098 if is_inductive then None else (Some uri)
2100 raise (TypeCheckerFailure
2101 (lazy ("Unknown mutual inductive definition:" ^
2102 UriManager.string_of_uri uri)))
2104 | C.Prod (n,so,de) ->
2105 returns_a_coinductive ~subst ((Some (n,C.Decl so))::context) de
2110 debug_print (lazy ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t)) ; flush stderr ;
2113 type_of_aux ~logger context t ugraph
2115 in debug_print (lazy "FINE TYPE_OF_AUX") ; flush stderr ; res
2118 (* is a small constructor? *)
2119 (*CSC: ottimizzare calcolando staticamente *)
2120 and is_small_or_non_informative ~condition ~logger context paramsno c ugraph =
2121 let rec is_small_or_non_informative_aux ~logger context c ugraph =
2122 let module C = Cic in
2123 match CicReduction.whd context c with
2125 let s,ugraph1 = type_of_aux' ~logger [] context so ugraph in
2126 let b = condition s in
2128 is_small_or_non_informative_aux
2129 ~logger ((Some (n,(C.Decl so)))::context) de ugraph1
2132 | _ -> true,ugraph (*CSC: we trust the type-checker *)
2134 let (context',dx) = split_prods ~subst:[] context paramsno c in
2135 is_small_or_non_informative_aux ~logger context' dx ugraph
2137 and is_small ~logger =
2138 is_small_or_non_informative
2139 ~condition:(fun s -> s=Cic.Sort Cic.Prop || s=Cic.Sort Cic.Set)
2142 and is_non_informative ~logger =
2143 is_small_or_non_informative
2144 ~condition:(fun s -> s=Cic.Sort Cic.Prop)
2147 and type_of ~logger t ugraph =
2149 debug_print (lazy ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t)) ; flush stderr ;
2152 type_of_aux' ~logger [] [] t ugraph
2154 in debug_print (lazy "FINE TYPE_OF_AUX'") ; flush stderr ; res
2158 let typecheck_obj0 ~logger uri ugraph =
2159 let module C = Cic in
2161 C.Constant (_,Some te,ty,_,_) ->
2162 let _,ugraph = type_of ~logger ty ugraph in
2163 let ty_te,ugraph = type_of ~logger te ugraph in
2164 let b,ugraph = (CicReduction.are_convertible [] ty_te ty ugraph) in
2166 raise (TypeCheckerFailure
2168 ("the type of the body is not the one expected:\n" ^
2169 CicPp.ppterm ty_te ^ "\nvs\n" ^
2173 | C.Constant (_,None,ty,_,_) ->
2174 (* only to check that ty is well-typed *)
2175 let _,ugraph = type_of ~logger ty ugraph in
2177 | C.CurrentProof (_,conjs,te,ty,_,_) ->
2180 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
2182 type_of_aux' ~logger metasenv context ty ugraph
2184 metasenv @ [conj],ugraph
2187 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
2188 let type_of_te,ugraph =
2189 type_of_aux' ~logger conjs [] te ugraph
2191 let b,ugraph = CicReduction.are_convertible [] type_of_te ty ugraph in
2193 raise (TypeCheckerFailure (lazy (sprintf
2194 "the current proof is not well typed because the type %s of the body is not convertible to the declared type %s"
2195 (CicPp.ppterm type_of_te) (CicPp.ppterm ty))))
2198 | C.Variable (_,bo,ty,_,_) ->
2199 (* only to check that ty is well-typed *)
2200 let _,ugraph = type_of ~logger ty ugraph in
2204 let ty_bo,ugraph = type_of ~logger bo ugraph in
2205 let b,ugraph = CicReduction.are_convertible [] ty_bo ty ugraph in
2207 raise (TypeCheckerFailure
2208 (lazy "the body is not the one expected"))
2212 | (C.InductiveDefinition _ as obj) ->
2213 check_mutual_inductive_defs ~logger uri obj ugraph
2216 let module C = Cic in
2217 let module R = CicReduction in
2218 let module U = UriManager in
2219 let logger = new CicLogger.logger in
2220 (* ??? match CicEnvironment.is_type_checked ~trust:true uri with ???? *)
2221 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2222 CicEnvironment.CheckedObj (cobj,ugraph') ->
2223 (* debug_print (lazy ("NON-INIZIO A TYPECHECKARE " ^ U.string_of_uri uri));*)
2225 | CicEnvironment.UncheckedObj uobj ->
2226 (* let's typecheck the uncooked object *)
2227 logger#log (`Start_type_checking uri) ;
2228 (* debug_print (lazy ("INIZIO A TYPECHECKARE " ^ U.string_of_uri uri)); *)
2229 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph uobj in
2231 CicEnvironment.set_type_checking_info uri;
2232 logger#log (`Type_checking_completed uri);
2233 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
2234 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2235 | _ -> raise CicEnvironmentError
2238 this is raised if set_type_checking_info is called on an object
2239 that has no associated universe file. If we are in univ_maker
2240 phase this is OK since univ_maker will properly commit the
2243 Invalid_argument s ->
2244 (*debug_print (lazy s);*)
2248 let typecheck_obj ~logger uri obj =
2249 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph obj in
2250 let ugraph, univlist, obj = CicUnivUtils.clean_and_fill uri obj ugraph in
2251 CicEnvironment.add_type_checked_obj uri (obj,ugraph,univlist)
2253 (** wrappers which instantiate fresh loggers *)
2255 let profiler = HExtlib.profile "K/CicTypeChecker.type_of_aux'"
2257 let type_of_aux' ?(subst = []) metasenv context t ugraph =
2258 let logger = new CicLogger.logger in
2259 profiler.HExtlib.profile
2260 (type_of_aux' ~logger ~subst metasenv context t) ugraph
2262 let typecheck_obj uri obj =
2263 let logger = new CicLogger.logger in
2264 typecheck_obj ~logger uri obj
2266 (* check_allowed_sort_elimination uri i s1 s2
2267 This function is used outside the kernel to determine in advance whether
2268 a MutCase will be allowed or not.
2269 [uri,i] is the type of the term to match
2270 [s1] is the sort of the term to eliminate (i.e. the head of the arity
2271 of the inductive type [uri,i])
2272 [s2] is the sort of the goal (i.e. the head of the type of the outtype
2274 let check_allowed_sort_elimination uri i s1 s2 =
2275 fst (check_allowed_sort_elimination ~subst:[] ~metasenv:[]
2276 ~logger:(new CicLogger.logger) [] uri i true
2277 (Cic.Implicit None) (* never used *) (Cic.Sort s1) (Cic.Sort s2)
2278 CicUniv.empty_ugraph)