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,ty,t) -> C.LetIn (n, aux k s, aux k ty, aux (k+1) t)
79 | C.Appl l -> C.Appl (List.map (aux k) l)
80 | C.Const (uri,exp_named_subst) ->
81 let exp_named_subst' =
82 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
84 C.Const (uri,exp_named_subst')
85 | C.MutInd (uri',tyno,exp_named_subst) when UriManager.eq uri uri' ->
86 if 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 mno subst in
263 does_not_occur ~subst context n nn (CicSubstitution.subst_meta l term)
265 CicUtil.Subst_not_found _ -> true)
267 does_not_occur ~subst context n nn te && does_not_occur ~subst context n nn ty
268 | C.Prod (name,so,dest) ->
269 does_not_occur ~subst context n nn so &&
270 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1)
272 | C.Lambda (name,so,dest) ->
273 does_not_occur ~subst context n nn so &&
274 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1) (nn + 1)
276 | C.LetIn (name,so,ty,dest) ->
277 does_not_occur ~subst context n nn so &&
278 does_not_occur ~subst context n nn ty &&
279 does_not_occur ~subst ((Some (name,(C.Def (so,ty))))::context)
280 (n + 1) (nn + 1) dest
282 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
283 | C.Var (_,exp_named_subst)
284 | C.Const (_,exp_named_subst)
285 | C.MutInd (_,_,exp_named_subst)
286 | C.MutConstruct (_,_,_,exp_named_subst) ->
287 List.fold_right (fun (_,x) i -> i && does_not_occur ~subst context n nn x)
289 | C.MutCase (_,_,out,te,pl) ->
290 does_not_occur ~subst context n nn out && does_not_occur ~subst context n nn te &&
291 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) pl true
293 let len = List.length fl in
294 let n_plus_len = n + len in
295 let nn_plus_len = nn + len in
298 (fun (types,len) (n,_,ty,_) ->
299 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
304 (fun (_,_,ty,bo) i ->
305 i && does_not_occur ~subst context n nn ty &&
306 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
309 let len = List.length fl in
310 let n_plus_len = n + len in
311 let nn_plus_len = nn + len in
314 (fun (types,len) (n,ty,_) ->
315 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
321 i && does_not_occur ~subst context n nn ty &&
322 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
325 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
326 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
327 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
328 (*CSC strictly_positive *)
329 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
330 and weakly_positive context n nn uri te =
331 let module C = Cic in
332 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
334 C.MutInd (HelmLibraryObjects.Datatypes.nat_URI,0,[])
336 (*CSC: mettere in cicSubstitution *)
337 let rec subst_inductive_type_with_dummy_mutind =
339 C.MutInd (uri',0,_) when UriManager.eq uri' uri ->
341 | C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri ->
343 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
344 | C.Prod (name,so,ta) ->
345 C.Prod (name, subst_inductive_type_with_dummy_mutind so,
346 subst_inductive_type_with_dummy_mutind ta)
347 | C.Lambda (name,so,ta) ->
348 C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
349 subst_inductive_type_with_dummy_mutind ta)
351 C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
352 | C.MutCase (uri,i,outtype,term,pl) ->
354 subst_inductive_type_with_dummy_mutind outtype,
355 subst_inductive_type_with_dummy_mutind term,
356 List.map subst_inductive_type_with_dummy_mutind pl)
358 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
359 subst_inductive_type_with_dummy_mutind ty,
360 subst_inductive_type_with_dummy_mutind bo)) fl)
362 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
363 subst_inductive_type_with_dummy_mutind ty,
364 subst_inductive_type_with_dummy_mutind bo)) fl)
365 | C.Const (uri,exp_named_subst) ->
366 let exp_named_subst' =
368 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
371 C.Const (uri,exp_named_subst')
372 | C.MutInd (uri,typeno,exp_named_subst) ->
373 let exp_named_subst' =
375 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
378 C.MutInd (uri,typeno,exp_named_subst')
379 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
380 let exp_named_subst' =
382 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
385 C.MutConstruct (uri,typeno,consno,exp_named_subst')
388 match CicReduction.whd context te with
390 C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri -> true
392 C.Appl ((C.MutInd (uri',_,_))::tl) when UriManager.eq uri' uri -> true
393 | C.MutInd (uri',0,_) when UriManager.eq uri' uri -> true
394 | C.Prod (C.Anonymous,source,dest) ->
395 strictly_positive context n nn
396 (subst_inductive_type_with_dummy_mutind source) &&
397 weakly_positive ((Some (C.Anonymous,(C.Decl source)))::context)
398 (n + 1) (nn + 1) uri dest
399 | C.Prod (name,source,dest) when
400 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
401 (* dummy abstraction, so we behave as in the anonimous case *)
402 strictly_positive context n nn
403 (subst_inductive_type_with_dummy_mutind source) &&
404 weakly_positive ((Some (name,(C.Decl source)))::context)
405 (n + 1) (nn + 1) uri dest
406 | C.Prod (name,source,dest) ->
407 does_not_occur context n nn
408 (subst_inductive_type_with_dummy_mutind source)&&
409 weakly_positive ((Some (name,(C.Decl source)))::context)
410 (n + 1) (nn + 1) uri dest
412 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
414 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
415 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
416 and instantiate_parameters params c =
417 let module C = Cic in
418 match (c,params) with
420 | (C.Prod (_,_,ta), he::tl) ->
421 instantiate_parameters tl
422 (CicSubstitution.subst he ta)
423 | (C.Cast (te,_), _) -> instantiate_parameters params te
424 | (t,l) -> raise (AssertFailure (lazy "1"))
426 and strictly_positive context n nn te =
427 let module C = Cic in
428 let module U = UriManager in
429 match CicReduction.whd context te with
430 | t when does_not_occur context n nn t -> true
433 (*CSC: bisogna controllare ty????*)
434 strictly_positive context n nn te
435 | C.Prod (name,so,ta) ->
436 does_not_occur context n nn so &&
437 strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
438 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
439 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
440 | C.Appl ((C.MutInd (uri,i,exp_named_subst))::tl) ->
441 let (ok,paramsno,ity,cl,name) =
442 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
444 C.InductiveDefinition (tl,_,paramsno,_) ->
445 let (name,_,ity,cl) = List.nth tl i in
446 (List.length tl = 1, paramsno, ity, cl, name)
447 (* (true, paramsno, ity, cl, name) *)
451 (lazy ("Unknown inductive type:" ^ U.string_of_uri uri)))
453 let (params,arguments) = split tl paramsno in
454 let lifted_params = List.map (CicSubstitution.lift 1) params in
458 instantiate_parameters lifted_params
459 (CicSubstitution.subst_vars exp_named_subst te)
464 (fun x i -> i && does_not_occur context n nn x)
466 (*CSC: MEGAPATCH3 (sara' quella giusta?)*)
471 ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri
476 (* the inductive type indexes are s.t. n < x <= nn *)
477 and are_all_occurrences_positive context uri indparamsno i n nn te =
478 let module C = Cic in
479 match CicReduction.whd context te with
480 C.Appl ((C.Rel m)::tl) when m = i ->
481 (*CSC: riscrivere fermandosi a 0 *)
482 (* let's check if the inductive type is applied at least to *)
483 (* indparamsno parameters *)
489 match CicReduction.whd context x with
490 C.Rel m when m = n - (indparamsno - k) -> k - 1
492 raise (TypeCheckerFailure
494 ("Non-positive occurence in mutual inductive definition(s) [1]" ^
495 UriManager.string_of_uri uri)))
499 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
501 raise (TypeCheckerFailure
502 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
503 UriManager.string_of_uri uri)))
504 | C.Rel m when m = i ->
505 if indparamsno = 0 then
508 raise (TypeCheckerFailure
509 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
510 UriManager.string_of_uri uri)))
511 | C.Prod (C.Anonymous,source,dest) ->
512 let b = strictly_positive context n nn source in
514 are_all_occurrences_positive
515 ((Some (C.Anonymous,(C.Decl source)))::context) uri indparamsno
516 (i+1) (n + 1) (nn + 1) dest
517 | C.Prod (name,source,dest) when
518 does_not_occur ((Some (name,(C.Decl source)))::context) 0 n dest ->
519 (* dummy abstraction, so we behave as in the anonimous case *)
520 strictly_positive context n nn source &&
521 are_all_occurrences_positive
522 ((Some (name,(C.Decl source)))::context) uri indparamsno
523 (i+1) (n + 1) (nn + 1) dest
524 | C.Prod (name,source,dest) ->
525 does_not_occur context n nn source &&
526 are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
527 uri indparamsno (i+1) (n + 1) (nn + 1) dest
530 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
531 (UriManager.string_of_uri uri))))
533 (* Main function to checks the correctness of a mutual *)
534 (* inductive block definition. This is the function *)
535 (* exported to the proof-engine. *)
536 and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
537 let module U = UriManager in
538 (* let's check if the arity of the inductive types are well *)
540 let ugrap1 = List.fold_left
541 (fun ugraph (_,_,x,_) -> let _,ugraph' =
542 type_of ~logger x ugraph in ugraph')
545 (* let's check if the types of the inductive constructors *)
546 (* are well formed. *)
547 (* In order not to use type_of_aux we put the types of the *)
548 (* mutual inductive types at the head of the types of the *)
549 (* constructors using Prods *)
550 let len = List.length itl in
552 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
555 (fun (_,_,_,cl) (i,ugraph) ->
558 (fun ugraph (name,te) ->
559 let debrujinedte = debrujin_constructor uri len te in
562 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
565 let _,ugraph' = type_of ~logger augmented_term ugraph in
566 (* let's check also the positivity conditions *)
569 (are_all_occurrences_positive tys uri indparamsno i 0 len
573 prerr_endline (UriManager.string_of_uri uri);
574 prerr_endline (string_of_int (List.length tys));
577 (lazy ("Non positive occurence in " ^ U.string_of_uri uri))) end
586 (* Main function to checks the correctness of a mutual *)
587 (* inductive block definition. *)
588 and check_mutual_inductive_defs uri obj ugraph =
590 Cic.InductiveDefinition (itl, params, indparamsno, _) ->
591 typecheck_mutual_inductive_defs uri (itl,params,indparamsno) ugraph
593 raise (TypeCheckerFailure (
594 lazy ("Unknown mutual inductive definition:" ^
595 UriManager.string_of_uri uri)))
597 and type_of_mutual_inductive_defs ~logger uri i ugraph =
598 let module C = Cic in
599 let module R = CicReduction in
600 let module U = UriManager in
602 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
603 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
604 | CicEnvironment.UncheckedObj uobj ->
605 logger#log (`Start_type_checking uri) ;
607 check_mutual_inductive_defs ~logger uri uobj ugraph
609 (* TASSI: FIXME: check ugraph1 == ugraph ritornato da env *)
611 CicEnvironment.set_type_checking_info uri ;
612 logger#log (`Type_checking_completed uri) ;
613 (match CicEnvironment.is_type_checked ~trust:false ugraph uri with
614 CicEnvironment.CheckedObj (cobj,ugraph') -> (cobj,ugraph')
615 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
618 Invalid_argument s ->
619 (*debug_print (lazy s);*)
623 C.InductiveDefinition (dl,_,_,_) ->
624 let (_,_,arity,_) = List.nth dl i in
627 raise (TypeCheckerFailure
628 (lazy ("Unknown mutual inductive definition:" ^ U.string_of_uri uri)))
630 and type_of_mutual_inductive_constr ~logger uri i j ugraph =
631 let module C = Cic in
632 let module R = CicReduction in
633 let module U = UriManager in
635 match CicEnvironment.is_type_checked ~trust:true ugraph uri with
636 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
637 | CicEnvironment.UncheckedObj uobj ->
638 logger#log (`Start_type_checking uri) ;
640 check_mutual_inductive_defs ~logger uri uobj ugraph
642 (* check ugraph1 validity ??? == ugraph' *)
644 CicEnvironment.set_type_checking_info uri ;
645 logger#log (`Type_checking_completed uri) ;
647 CicEnvironment.is_type_checked ~trust:false ugraph uri
649 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
650 | CicEnvironment.UncheckedObj _ ->
651 raise CicEnvironmentError)
653 Invalid_argument s ->
654 (*debug_print (lazy s);*)
658 C.InductiveDefinition (dl,_,_,_) ->
659 let (_,_,_,cl) = List.nth dl i in
660 let (_,ty) = List.nth cl (j-1) in
663 raise (TypeCheckerFailure
664 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
666 and recursive_args context n nn te =
667 let module C = Cic in
668 match CicReduction.whd context te with
674 | C.Cast _ (*CSC ??? *) ->
675 raise (AssertFailure (lazy "3")) (* due to type-checking *)
676 | C.Prod (name,so,de) ->
677 (not (does_not_occur context n nn so)) ::
678 (recursive_args ((Some (name,(C.Decl so)))::context) (n+1) (nn + 1) de)
681 raise (AssertFailure (lazy "4")) (* due to type-checking *)
683 | C.Const _ -> raise (AssertFailure (lazy "5"))
688 | C.CoFix _ -> raise (AssertFailure (lazy "6")) (* due to type-checking *)
690 and get_new_safes ~subst context p c rl safes n nn x =
691 let module C = Cic in
692 let module U = UriManager in
693 let module R = CicReduction in
694 match (R.whd ~subst context c, R.whd ~subst context p, rl) with
695 (C.Prod (_,so,ta1), C.Lambda (name,_,ta2), b::tl) ->
696 (* we are sure that the two sources are convertible because we *)
697 (* have just checked this. So let's go along ... *)
699 List.map (fun x -> x + 1) safes
702 if b then 1::safes' else safes'
704 get_new_safes ~subst ((Some (name,(C.Decl so)))::context)
705 ta2 ta1 tl safes'' (n+1) (nn+1) (x+1)
706 | (C.Prod _, (C.MutConstruct _ as e), _)
707 | (C.Prod _, (C.Rel _ as e), _)
708 | (C.MutInd _, e, [])
709 | (C.Appl _, e, []) -> (e,safes,n,nn,x,context)
711 (* CSC: If the next exception is raised, it just means that *)
712 (* CSC: the proof-assistant allows to use very strange things *)
713 (* CSC: as a branch of a case whose type is a Prod. In *)
714 (* CSC: particular, this means that a new (C.Prod, x,_) case *)
715 (* CSC: must be considered in this match. (e.g. x = MutCase) *)
718 (Printf.sprintf "Get New Safes: c=%s ; p=%s"
719 (CicPp.ppterm c) (CicPp.ppterm p))))
721 and split_prods ~subst context n te =
722 let module C = Cic in
723 let module R = CicReduction in
724 match (n, R.whd ~subst context te) with
726 | (n, C.Prod (name,so,ta)) when n > 0 ->
727 split_prods ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
728 | (_, _) -> raise (AssertFailure (lazy "8"))
730 and eat_lambdas ~subst context n te =
731 let module C = Cic in
732 let module R = CicReduction in
733 match (n, R.whd ~subst context te) with
734 (0, _) -> (te, 0, context)
735 | (n, C.Lambda (name,so,ta)) when n > 0 ->
736 let (te, k, context') =
737 eat_lambdas ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
739 (te, k + 1, context')
741 raise (AssertFailure (lazy (sprintf "9 (%d, %s)" n (CicPp.ppterm te))))
743 (*CSC: Tutto quello che segue e' l'intuzione di luca ;-) *)
744 and check_is_really_smaller_arg ~subst context n nn kl x safes te =
745 (*CSC: forse la whd si puo' fare solo quando serve veramente. *)
746 (*CSC: cfr guarded_by_destructors *)
747 let module C = Cic in
748 let module U = UriManager in
749 match CicReduction.whd ~subst context te with
750 C.Rel m when List.mem m safes -> true
757 (* | C.Cast (te,ty) ->
758 check_is_really_smaller_arg ~subst n nn kl x safes te &&
759 check_is_really_smaller_arg ~subst n nn kl x safes ty*)
760 (* | C.Prod (_,so,ta) ->
761 check_is_really_smaller_arg ~subst n nn kl x safes so &&
762 check_is_really_smaller_arg ~subst (n+1) (nn+1) kl (x+1)
763 (List.map (fun x -> x + 1) safes) ta*)
764 | C.Prod _ -> raise (AssertFailure (lazy "10"))
765 | C.Lambda (name,so,ta) ->
766 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
767 check_is_really_smaller_arg ~subst ((Some (name,(C.Decl so)))::context)
768 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
769 | C.LetIn (name,so,ty,ta) ->
770 check_is_really_smaller_arg ~subst context n nn kl x safes so &&
771 check_is_really_smaller_arg ~subst context n nn kl x safes ty &&
772 check_is_really_smaller_arg ~subst ((Some (name,(C.Def (so,ty))))::context)
773 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
775 (*CSC: sulla coda ci vogliono dei controlli? secondo noi no, ma *)
776 (*CSC: solo perche' non abbiamo trovato controesempi *)
777 check_is_really_smaller_arg ~subst context n nn kl x safes he
778 | C.Appl [] -> raise (AssertFailure (lazy "11"))
780 | C.MutInd _ -> raise (AssertFailure (lazy "12"))
781 | C.MutConstruct _ -> false
782 | C.MutCase (uri,i,outtype,term,pl) ->
784 C.Rel m when List.mem m safes || m = x ->
785 let (lefts_and_tys,len,isinductive,paramsno,cl) =
786 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
788 C.InductiveDefinition (tl,_,paramsno,_) ->
791 (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) tl
793 let (_,isinductive,_,cl) = List.nth tl i in
797 (id, snd (split_prods ~subst tys paramsno ty))) cl in
802 fst (split_prods ~subst [] paramsno ty)
804 (lefts@tys,List.length tl,isinductive,paramsno,cl')
806 raise (TypeCheckerFailure
807 (lazy ("Unknown mutual inductive definition:" ^
808 UriManager.string_of_uri uri)))
810 if not isinductive then
813 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
820 Invalid_argument _ ->
821 raise (TypeCheckerFailure (lazy "not enough patterns"))
826 let debrujinedte = debrujin_constructor uri len c in
827 recursive_args lefts_and_tys 0 len debrujinedte
829 let (e,safes',n',nn',x',context') =
830 get_new_safes ~subst context p c rl' safes n nn x
833 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
835 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
836 let (lefts_and_tys,len,isinductive,paramsno,cl) =
837 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
839 C.InductiveDefinition (tl,_,paramsno,_) ->
840 let (_,isinductive,_,cl) = List.nth tl i in
842 List.map (fun (n,_,ty,_) ->
843 Some(Cic.Name n,(Cic.Decl ty))) tl
848 (id, snd (split_prods ~subst tys paramsno ty))) cl in
853 fst (split_prods ~subst [] paramsno ty)
855 (lefts@tys,List.length tl,isinductive,paramsno,cl')
857 raise (TypeCheckerFailure
858 (lazy ("Unknown mutual inductive definition:" ^
859 UriManager.string_of_uri uri)))
861 if not isinductive then
864 i && check_is_really_smaller_arg ~subst context n nn kl x safes p)
871 Invalid_argument _ ->
872 raise (TypeCheckerFailure (lazy "not enough patterns"))
874 (*CSC: supponiamo come prima che nessun controllo sia necessario*)
875 (*CSC: sugli argomenti di una applicazione *)
879 let debrujinedte = debrujin_constructor uri len c in
880 recursive_args lefts_and_tys 0 len debrujinedte
882 let (e, safes',n',nn',x',context') =
883 get_new_safes ~subst context p c rl' safes n nn x
886 check_is_really_smaller_arg ~subst context' n' nn' kl x' safes' e
891 i && check_is_really_smaller_arg ~subst context n nn kl x safes p
895 let len = List.length fl in
896 let n_plus_len = n + len
897 and nn_plus_len = nn + len
898 and x_plus_len = x + len
901 (fun (types,len) (n,_,ty,_) ->
902 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
905 and safes' = List.map (fun x -> x + len) safes in
907 (fun (_,_,ty,bo) i ->
909 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
913 let len = List.length fl in
914 let n_plus_len = n + len
915 and nn_plus_len = nn + len
916 and x_plus_len = x + len
919 (fun (types,len) (n,ty,_) ->
920 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
923 and safes' = List.map (fun x -> x + len) safes in
927 check_is_really_smaller_arg ~subst (tys@context) n_plus_len nn_plus_len kl
931 and guarded_by_destructors ~subst context n nn kl x safes =
932 let module C = Cic in
933 let module U = UriManager in
935 C.Rel m when m > n && m <= nn -> false
937 (match List.nth context (m-1) with
938 Some (_,C.Decl _) -> true
939 | Some (_,C.Def (bo,_)) ->
940 guarded_by_destructors ~subst context n nn kl x safes
941 (CicSubstitution.lift m bo)
942 | None -> raise (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
946 | C.Implicit _ -> true
948 guarded_by_destructors ~subst context n nn kl x safes te &&
949 guarded_by_destructors ~subst context n nn kl x safes ty
950 | C.Prod (name,so,ta) ->
951 guarded_by_destructors ~subst context n nn kl x safes so &&
952 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
953 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
954 | C.Lambda (name,so,ta) ->
955 guarded_by_destructors ~subst context n nn kl x safes so &&
956 guarded_by_destructors ~subst ((Some (name,(C.Decl so)))::context)
957 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
958 | C.LetIn (name,so,ty,ta) ->
959 guarded_by_destructors ~subst context n nn kl x safes so &&
960 guarded_by_destructors ~subst context n nn kl x safes ty &&
961 guarded_by_destructors ~subst ((Some (name,(C.Def (so,ty))))::context)
962 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
963 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
964 let k = List.nth kl (m - n - 1) in
965 if not (List.length tl > k) then false
969 i && guarded_by_destructors ~subst context n nn kl x safes param
971 check_is_really_smaller_arg ~subst context n nn kl x safes (List.nth tl k)
974 (fun t i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
976 | C.Var (_,exp_named_subst)
977 | C.Const (_,exp_named_subst)
978 | C.MutInd (_,_,exp_named_subst)
979 | C.MutConstruct (_,_,_,exp_named_subst) ->
981 (fun (_,t) i -> i && guarded_by_destructors ~subst context n nn kl x safes t)
983 | C.MutCase (uri,i,outtype,term,pl) ->
984 (match CicReduction.whd ~subst context term with
985 C.Rel m when List.mem m safes || m = x ->
986 let (lefts_and_tys,len,isinductive,paramsno,cl) =
987 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
989 C.InductiveDefinition (tl,_,paramsno,_) ->
990 let len = List.length tl in
991 let (_,isinductive,_,cl) = List.nth tl i in
993 List.map (fun (n,_,ty,_) ->
994 Some(Cic.Name n,(Cic.Decl ty))) tl
999 let debrujinedty = debrujin_constructor uri len ty in
1000 (id, snd (split_prods ~subst tys paramsno ty),
1001 snd (split_prods ~subst tys paramsno debrujinedty)
1007 fst (split_prods ~subst [] paramsno ty)
1009 (lefts@tys,len,isinductive,paramsno,cl')
1011 raise (TypeCheckerFailure
1012 (lazy ("Unknown mutual inductive definition:" ^
1013 UriManager.string_of_uri uri)))
1015 if not isinductive then
1016 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1017 guarded_by_destructors ~subst context n nn kl x safes term &&
1018 (*CSC: manca ??? il controllo sul tipo di term? *)
1021 i && guarded_by_destructors ~subst context n nn kl x safes p)
1028 Invalid_argument _ ->
1029 raise (TypeCheckerFailure (lazy "not enough patterns"))
1031 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1032 (*CSC: manca ??? il controllo sul tipo di term? *)
1034 (fun (p,(_,c,brujinedc)) i ->
1035 let rl' = recursive_args lefts_and_tys 0 len brujinedc in
1036 let (e,safes',n',nn',x',context') =
1037 get_new_safes ~subst context p c rl' safes n nn x
1040 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1042 | C.Appl ((C.Rel m)::tl) when List.mem m safes || m = x ->
1043 let (lefts_and_tys,len,isinductive,paramsno,cl) =
1044 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1046 C.InductiveDefinition (tl,_,paramsno,_) ->
1047 let (_,isinductive,_,cl) = List.nth tl i in
1050 (fun (n,_,ty,_) -> Some(Cic.Name n,(Cic.Decl ty))) tl
1055 (id, snd (split_prods ~subst tys paramsno ty))) cl in
1060 fst (split_prods ~subst [] paramsno ty)
1062 (lefts@tys,List.length tl,isinductive,paramsno,cl')
1064 raise (TypeCheckerFailure
1065 (lazy ("Unknown mutual inductive definition:" ^
1066 UriManager.string_of_uri uri)))
1068 if not isinductive then
1069 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1070 guarded_by_destructors ~subst context n nn kl x safes term &&
1071 (*CSC: manca ??? il controllo sul tipo di term? *)
1074 i && guarded_by_destructors ~subst context n nn kl x safes p)
1081 Invalid_argument _ ->
1082 raise (TypeCheckerFailure (lazy "not enough patterns"))
1084 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1085 (*CSC: manca ??? il controllo sul tipo di term? *)
1088 i && guarded_by_destructors ~subst context n nn kl x safes t)
1093 let debrujinedte = debrujin_constructor uri len c in
1094 recursive_args lefts_and_tys 0 len debrujinedte
1096 let (e, safes',n',nn',x',context') =
1097 get_new_safes ~subst context p c rl' safes n nn x
1100 guarded_by_destructors ~subst context' n' nn' kl x' safes' e
1103 guarded_by_destructors ~subst context n nn kl x safes outtype &&
1104 guarded_by_destructors ~subst context n nn kl x safes term &&
1105 (*CSC: manca ??? il controllo sul tipo di term? *)
1107 (fun p i -> i && guarded_by_destructors ~subst context n nn kl x safes p)
1111 let len = List.length fl in
1112 let n_plus_len = n + len
1113 and nn_plus_len = nn + len
1114 and x_plus_len = x + len
1117 (fun (types,len) (n,_,ty,_) ->
1118 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1121 and safes' = List.map (fun x -> x + len) safes in
1123 (fun (_,_,ty,bo) i ->
1124 i && guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1125 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1126 x_plus_len safes' bo
1128 | C.CoFix (_, fl) ->
1129 let len = List.length fl in
1130 let n_plus_len = n + len
1131 and nn_plus_len = nn + len
1132 and x_plus_len = x + len
1135 (fun (types,len) (n,ty,_) ->
1136 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1139 and safes' = List.map (fun x -> x + len) safes in
1143 guarded_by_destructors ~subst context n nn kl x_plus_len safes' ty &&
1144 guarded_by_destructors ~subst (tys@context) n_plus_len nn_plus_len kl
1145 x_plus_len safes' bo
1148 (* the boolean h means already protected *)
1149 (* args is the list of arguments the type of the constructor that may be *)
1150 (* found in head position must be applied to. *)
1151 and guarded_by_constructors ~subst context n nn h te args coInductiveTypeURI =
1152 let module C = Cic in
1153 (*CSC: There is a lot of code replication between the cases X and *)
1154 (*CSC: (C.Appl X tl). Maybe it will be better to define a function *)
1155 (*CSC: that maps X into (C.Appl X []) when X is not already a C.Appl *)
1156 match CicReduction.whd ~subst context te with
1157 C.Rel m when m > n && m <= nn -> h
1165 (* the term has just been type-checked *)
1166 raise (AssertFailure (lazy "17"))
1167 | C.Lambda (name,so,de) ->
1168 does_not_occur ~subst context n nn so &&
1169 guarded_by_constructors ~subst ((Some (name,(C.Decl so)))::context)
1170 (n + 1) (nn + 1) h de args coInductiveTypeURI
1171 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
1173 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) tl true
1174 | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
1178 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1179 with Not_found -> assert false
1182 C.InductiveDefinition (itl,_,_,_) ->
1183 let (_,_,_,cl) = List.nth itl i in
1184 let (_,cons) = List.nth cl (j - 1) in
1185 CicSubstitution.subst_vars exp_named_subst cons
1187 raise (TypeCheckerFailure
1188 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
1190 let rec analyse_branch context ty te =
1191 match CicReduction.whd ~subst context ty with
1192 C.Meta _ -> raise (AssertFailure (lazy "34"))
1196 does_not_occur ~subst context n nn te
1199 raise (AssertFailure (lazy "24"))(* due to type-checking *)
1200 | C.Prod (name,so,de) ->
1201 analyse_branch ((Some (name,(C.Decl so)))::context) de te
1204 raise (AssertFailure (lazy "25"))(* due to type-checking *)
1205 | C.Appl ((C.MutInd (uri,_,_))::_) when uri == coInductiveTypeURI ->
1206 guarded_by_constructors ~subst context n nn true te []
1208 | C.Appl ((C.MutInd (uri,_,_))::_) ->
1209 guarded_by_constructors ~subst context n nn true te tl
1212 does_not_occur ~subst context n nn te
1213 | C.Const _ -> raise (AssertFailure (lazy "26"))
1214 | C.MutInd (uri,_,_) when uri == coInductiveTypeURI ->
1215 guarded_by_constructors ~subst context n nn true te []
1218 does_not_occur ~subst context n nn te
1219 | C.MutConstruct _ -> raise (AssertFailure (lazy "27"))
1220 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1221 (*CSC: in head position. *)
1225 raise (AssertFailure (lazy "28"))(* due to type-checking *)
1227 let rec analyse_instantiated_type context ty l =
1228 match CicReduction.whd ~subst context ty with
1234 | C.Cast _ -> raise (AssertFailure (lazy "29"))(* due to type-checking *)
1235 | C.Prod (name,so,de) ->
1240 analyse_branch context so he &&
1241 analyse_instantiated_type
1242 ((Some (name,(C.Decl so)))::context) de tl
1246 raise (AssertFailure (lazy "30"))(* due to type-checking *)
1249 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1250 | C.Const _ -> raise (AssertFailure (lazy "31"))
1253 (fun i x -> i && does_not_occur ~subst context n nn x) true l
1254 | C.MutConstruct _ -> raise (AssertFailure (lazy "32"))
1255 (*CSC: we do not consider backbones with a MutCase, Fix, Cofix *)
1256 (*CSC: in head position. *)
1260 raise (AssertFailure (lazy "33"))(* due to type-checking *)
1262 let rec instantiate_type args consty =
1265 | tlhe::tltl as l ->
1266 let consty' = CicReduction.whd ~subst context consty in
1272 let instantiated_de = CicSubstitution.subst he de in
1273 (*CSC: siamo sicuri che non sia troppo forte? *)
1274 does_not_occur ~subst context n nn tlhe &
1275 instantiate_type tl instantiated_de tltl
1277 (*CSC:We do not consider backbones with a MutCase, a *)
1278 (*CSC:FixPoint, a CoFixPoint and so on in head position.*)
1279 raise (AssertFailure (lazy "23"))
1281 | [] -> analyse_instantiated_type context consty' l
1282 (* These are all the other cases *)
1284 instantiate_type args consty tl
1285 | C.Appl ((C.CoFix (_,fl))::tl) ->
1286 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1287 let len = List.length fl in
1288 let n_plus_len = n + len
1289 and nn_plus_len = nn + len
1290 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1293 (fun (types,len) (n,ty,_) ->
1294 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1300 i && does_not_occur ~subst context n nn ty &&
1301 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1302 h bo args coInductiveTypeURI
1304 | C.Appl ((C.MutCase (_,_,out,te,pl))::tl) ->
1305 List.fold_left (fun i x -> i && does_not_occur ~subst context n nn x) true tl &&
1306 does_not_occur ~subst context n nn out &&
1307 does_not_occur ~subst context n nn te &&
1311 guarded_by_constructors ~subst context n nn h x args
1315 List.fold_right (fun x i -> i && does_not_occur ~subst context n nn x) l true
1316 | C.Var (_,exp_named_subst)
1317 | C.Const (_,exp_named_subst) ->
1319 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1320 | C.MutInd _ -> assert false
1321 | C.MutConstruct (_,_,_,exp_named_subst) ->
1323 (fun (_,x) i -> i && does_not_occur ~subst context n nn x) exp_named_subst true
1324 | C.MutCase (_,_,out,te,pl) ->
1325 does_not_occur ~subst context n nn out &&
1326 does_not_occur ~subst context n nn te &&
1330 guarded_by_constructors ~subst context n nn h x args
1334 let len = List.length fl in
1335 let n_plus_len = n + len
1336 and nn_plus_len = nn + len
1337 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1340 (fun (types,len) (n,_,ty,_) ->
1341 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1346 (fun (_,_,ty,bo) i ->
1347 i && does_not_occur ~subst context n nn ty &&
1348 does_not_occur ~subst (tys@context) n_plus_len nn_plus_len bo
1351 let len = List.length fl in
1352 let n_plus_len = n + len
1353 and nn_plus_len = nn + len
1354 (*CSC: Is a Decl of the ty ok or should I use Def of a Fix? *)
1357 (fun (types,len) (n,ty,_) ->
1358 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1364 i && does_not_occur ~subst context n nn ty &&
1365 guarded_by_constructors ~subst (tys@context) n_plus_len nn_plus_len
1367 args coInductiveTypeURI
1370 and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1371 need_dummy ind arity1 arity2 ugraph =
1372 let module C = Cic in
1373 let module U = UriManager in
1374 let arity1 = CicReduction.whd ~subst context arity1 in
1375 let rec check_allowed_sort_elimination_aux ugraph context arity2 need_dummy =
1376 match arity1, CicReduction.whd ~subst context arity2 with
1377 (C.Prod (name,so1,de1), C.Prod (_,so2,de2)) ->
1379 CicReduction.are_convertible ~subst ~metasenv context so1 so2 ugraph in
1381 check_allowed_sort_elimination ~subst ~metasenv ~logger
1382 ((Some (name,C.Decl so1))::context) uri i
1383 need_dummy (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1387 | (C.Sort _, C.Prod (name,so,ta)) when not need_dummy ->
1389 CicReduction.are_convertible ~subst ~metasenv context so ind ugraph in
1393 check_allowed_sort_elimination_aux ugraph1
1394 ((Some (name,C.Decl so))::context) ta true
1395 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
1396 | (C.Sort C.Prop, C.Sort C.Set)
1397 | (C.Sort C.Prop, C.Sort C.CProp)
1398 | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
1399 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1401 C.InductiveDefinition (itl,_,paramsno,_) ->
1402 let itl_len = List.length itl in
1403 let (name,_,ty,cl) = List.nth itl i in
1404 let cl_len = List.length cl in
1405 if (cl_len = 0 || (itl_len = 1 && cl_len = 1)) then
1406 let non_informative,ugraph =
1407 if cl_len = 0 then true,ugraph
1409 is_non_informative ~logger [Some (C.Name name,C.Decl ty)]
1410 paramsno (snd (List.nth cl 0)) ugraph
1412 (* is it a singleton or empty non recursive and non informative
1414 non_informative, ugraph
1418 raise (TypeCheckerFailure
1419 (lazy ("Unknown mutual inductive definition:" ^
1420 UriManager.string_of_uri uri)))
1422 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
1423 | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
1424 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
1425 | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
1426 | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
1427 | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
1428 | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
1430 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1432 C.InductiveDefinition (itl,_,paramsno,_) ->
1434 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1436 let (_,_,_,cl) = List.nth itl i in
1438 (fun (_,x) (i,ugraph) ->
1440 is_small ~logger tys paramsno x ugraph
1445 raise (TypeCheckerFailure
1446 (lazy ("Unknown mutual inductive definition:" ^
1447 UriManager.string_of_uri uri)))
1449 | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
1450 | (_,_) -> false,ugraph
1452 check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
1454 and type_of_branch ~subst context argsno need_dummy outtype term constype =
1455 let module C = Cic in
1456 let module R = CicReduction in
1457 match R.whd ~subst context constype with
1462 C.Appl [outtype ; term]
1463 | C.Appl (C.MutInd (_,_,_)::tl) ->
1464 let (_,arguments) = split tl argsno
1466 if need_dummy && arguments = [] then
1469 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1470 | C.Prod (name,so,de) ->
1472 match CicSubstitution.lift 1 term with
1473 C.Appl l -> C.Appl (l@[C.Rel 1])
1474 | t -> C.Appl [t ; C.Rel 1]
1476 C.Prod (name,so,type_of_branch ~subst
1477 ((Some (name,(C.Decl so)))::context) argsno need_dummy
1478 (CicSubstitution.lift 1 outtype) term' de)
1479 | _ -> raise (AssertFailure (lazy "20"))
1481 (* check_metasenv_consistency checks that the "canonical" context of a
1482 metavariable is consitent - up to relocation via the relocation list l -
1483 with the actual context *)
1486 and check_metasenv_consistency ~logger ~subst metasenv context
1487 canonical_context l ugraph
1489 let module C = Cic in
1490 let module R = CicReduction in
1491 let module S = CicSubstitution in
1492 let lifted_canonical_context =
1496 | (Some (n,C.Decl t))::tl ->
1497 (Some (n,C.Decl (S.subst_meta l (S.lift i t))))::(aux (i+1) tl)
1498 | None::tl -> None::(aux (i+1) tl)
1499 | (Some (n,C.Def (t,ty)))::tl ->
1500 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),S.subst_meta l (S.lift i ty))))::(aux (i+1) tl)
1502 aux 1 canonical_context
1508 | Some t,Some (_,C.Def (ct,_)) ->
1509 (*CSC: the following optimization is to avoid a possibly expensive
1510 reduction that can be easily avoided and that is quite
1511 frequent. However, this is better handled using levels to
1512 control reduction *)
1517 match List.nth context (n - 1) with
1518 Some (_,C.Def (te,_)) -> S.lift n te
1524 (*if t <> optimized_t && optimized_t = ct then prerr_endline "!!!!!!!!!!!!!!!"
1525 else if t <> optimized_t then prerr_endline ("@@ " ^ CicPp.ppterm t ^ " ==> " ^ CicPp.ppterm optimized_t ^ " <==> " ^ CicPp.ppterm ct);*)
1527 R.are_convertible ~subst ~metasenv context optimized_t ct ugraph
1532 (lazy (sprintf "Not well typed metavariable local context: expected a term convertible with %s, found %s" (CicPp.ppterm ct) (CicPp.ppterm t))))
1535 | Some t,Some (_,C.Decl ct) ->
1536 let type_t,ugraph1 =
1537 type_of_aux' ~logger ~subst metasenv context t ugraph
1540 R.are_convertible ~subst ~metasenv context type_t ct ugraph1
1543 raise (TypeCheckerFailure
1544 (lazy (sprintf "Not well typed metavariable local context: expected a term of type %s, found %s of type %s"
1545 (CicPp.ppterm ct) (CicPp.ppterm t)
1546 (CicPp.ppterm type_t))))
1550 raise (TypeCheckerFailure
1551 (lazy ("Not well typed metavariable local context: "^
1552 "an hypothesis, that is not hidden, is not instantiated")))
1553 ) ugraph l lifted_canonical_context
1557 type_of_aux' is just another name (with a different scope)
1561 and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
1562 let rec type_of_aux ~logger context t ugraph =
1563 let module C = Cic in
1564 let module R = CicReduction in
1565 let module S = CicSubstitution in
1566 let module U = UriManager in
1570 match List.nth context (n - 1) with
1571 Some (_,C.Decl t) -> S.lift n t,ugraph
1572 | Some (_,C.Def (_,ty)) -> S.lift n ty,ugraph
1574 (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
1577 raise (TypeCheckerFailure (lazy "unbound variable"))
1579 | C.Var (uri,exp_named_subst) ->
1582 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1584 let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
1585 let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
1590 let (canonical_context,term,ty) = CicUtil.lookup_subst n subst in
1592 check_metasenv_consistency ~logger
1593 ~subst metasenv context canonical_context l ugraph
1595 (* assuming subst is well typed !!!!! *)
1596 ((CicSubstitution.subst_meta l ty), ugraph1)
1597 (* type_of_aux context (CicSubstitution.subst_meta l term) *)
1598 with CicUtil.Subst_not_found _ ->
1599 let (_,canonical_context,ty) = CicUtil.lookup_meta n metasenv in
1601 check_metasenv_consistency ~logger
1602 ~subst metasenv context canonical_context l ugraph
1604 ((CicSubstitution.subst_meta l ty),ugraph1))
1605 (* TASSI: CONSTRAINTS *)
1606 | C.Sort (C.Type t) ->
1607 let t' = CicUniv.fresh() in
1609 let ugraph1 = CicUniv.add_gt t' t ugraph in
1610 (C.Sort (C.Type t')),ugraph1
1612 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
1613 | C.Sort s -> (C.Sort (C.Type (CicUniv.fresh ()))),ugraph
1614 | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
1615 | C.Cast (te,ty) as t ->
1616 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1617 let ty_te,ugraph2 = type_of_aux ~logger context te ugraph1 in
1619 R.are_convertible ~subst ~metasenv context ty_te ty ugraph2
1624 raise (TypeCheckerFailure
1625 (lazy (sprintf "Invalid cast %s" (CicPp.ppterm t))))
1626 | C.Prod (name,s,t) ->
1627 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1629 type_of_aux ~logger ((Some (name,(C.Decl s)))::context) t ugraph1
1631 sort_of_prod ~subst context (name,s) (sort1,sort2) ugraph2
1632 | C.Lambda (n,s,t) ->
1633 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1634 (match R.whd ~subst context sort1 with
1639 (TypeCheckerFailure (lazy (sprintf
1640 "Not well-typed lambda-abstraction: the source %s should be a type; instead it is a term of type %s" (CicPp.ppterm s)
1641 (CicPp.ppterm sort1))))
1644 type_of_aux ~logger ((Some (n,(C.Decl s)))::context) t ugraph1
1646 (C.Prod (n,s,type2)),ugraph2
1647 | C.LetIn (n,s,ty,t) ->
1648 (* only to check if s is well-typed *)
1649 let ty',ugraph1 = type_of_aux ~logger context s ugraph in
1651 R.are_convertible ~subst ~metasenv context ty ty' ugraph1
1657 "The type of %s is %s but it is expected to be %s"
1658 (CicPp.ppterm s) (CicPp.ppterm ty') (CicPp.ppterm ty))))
1660 (* The type of a LetIn is a LetIn. Extremely slow since the computed
1661 LetIn is later reduced and maybe also re-checked.
1662 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t))
1664 (* The type of the LetIn is reduced. Much faster than the previous
1665 solution. Moreover the inferred type is probably very different
1666 from the expected one.
1667 (CicReduction.whd ~subst context
1668 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)))
1670 (* One-step LetIn reduction. Even faster than the previous solution.
1671 Moreover the inferred type is closer to the expected one. *)
1674 ((Some (n,(C.Def (s,ty))))::context) t ugraph1
1676 (CicSubstitution.subst ~avoid_beta_redexes:true s ty1),ugraph2
1677 | C.Appl (he::tl) when List.length tl > 0 ->
1678 let hetype,ugraph1 = type_of_aux ~logger context he ugraph in
1679 let tlbody_and_type,ugraph2 =
1682 let ty,ugraph1 = type_of_aux ~logger context x ugraph in
1683 (*let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in*)
1684 ((x,ty)::l,ugraph1))
1687 (* TASSI: questa c'era nel mio... ma non nel CVS... *)
1688 (* let _,ugraph2 = type_of_aux context hetype ugraph2 in *)
1689 eat_prods ~subst context hetype tlbody_and_type ugraph2
1690 | C.Appl _ -> raise (AssertFailure (lazy "Appl: no arguments"))
1691 | C.Const (uri,exp_named_subst) ->
1694 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1696 let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
1698 CicSubstitution.subst_vars exp_named_subst cty
1702 | C.MutInd (uri,i,exp_named_subst) ->
1705 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1707 (* TASSI: da me c'era anche questa, ma in CVS no *)
1708 let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
1709 (* fine parte dubbia *)
1711 CicSubstitution.subst_vars exp_named_subst mty
1715 | C.MutConstruct (uri,i,j,exp_named_subst) ->
1717 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1719 (* TASSI: idem come sopra *)
1721 type_of_mutual_inductive_constr ~logger uri i j ugraph1
1724 CicSubstitution.subst_vars exp_named_subst mty
1727 | C.MutCase (uri,i,outtype,term,pl) ->
1728 let outsort,ugraph1 = type_of_aux ~logger context outtype ugraph in
1729 let (need_dummy, k) =
1730 let rec guess_args context t =
1731 let outtype = CicReduction.whd ~subst context t in
1733 C.Sort _ -> (true, 0)
1734 | C.Prod (name, s, t) ->
1736 guess_args ((Some (name,(C.Decl s)))::context) t in
1738 (* last prod before sort *)
1739 match CicReduction.whd ~subst context s with
1740 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1741 C.MutInd (uri',i',_) when U.eq uri' uri && i' = i ->
1743 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1744 | C.Appl ((C.MutInd (uri',i',_)) :: _)
1745 when U.eq uri' uri && i' = i -> (false, 1)
1753 "Malformed case analasys' output type %s"
1754 (CicPp.ppterm outtype))))
1757 let (parameters, arguments, exp_named_subst),ugraph2 =
1758 let ty,ugraph2 = type_of_aux context term ugraph1 in
1759 match R.whd ~subst context ty with
1760 (*CSC manca il caso dei CAST *)
1761 (*CSC: ma servono i parametri (uri,i)? Se si', perche' non serve anche il *)
1762 (*CSC: parametro exp_named_subst? Se no, perche' non li togliamo? *)
1763 (*CSC: Hint: nella DTD servono per gli stylesheet. *)
1764 C.MutInd (uri',i',exp_named_subst) as typ ->
1765 if U.eq uri uri' && i = i' then
1766 ([],[],exp_named_subst),ugraph2
1771 ("Case analysys: analysed term type is %s, but is expected to be (an application of) %s#1/%d{_}")
1772 (CicPp.ppterm typ) (U.string_of_uri uri) i)))
1774 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1775 if U.eq uri uri' && i = i' then
1777 split tl (List.length tl - k)
1778 in (params,args,exp_named_subst),ugraph2
1783 ("Case analysys: analysed term type is %s, "^
1784 "but is expected to be (an application of) "^
1786 (CicPp.ppterm typ') (U.string_of_uri uri) i)))
1792 "analysed term %s is not an inductive one")
1793 (CicPp.ppterm term))))
1795 let (b, k) = guess_args context outsort in
1796 if not b then (b, k - 1) else (b, k) in
1797 let (parameters, arguments, exp_named_subst),ugraph2 =
1798 let ty,ugraph2 = type_of_aux ~logger context term ugraph1 in
1799 match R.whd ~subst context ty with
1800 C.MutInd (uri',i',exp_named_subst) as typ ->
1801 if U.eq uri uri' && i = i' then
1802 ([],[],exp_named_subst),ugraph2
1806 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1807 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1808 | C.Appl ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) ->
1809 if U.eq uri uri' && i = i' then
1811 split tl (List.length tl - k)
1812 in (params,args,exp_named_subst),ugraph2
1816 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1817 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1822 "Case analysis: analysed term %s is not an inductive one"
1823 (CicPp.ppterm term))))
1826 let's control if the sort elimination is allowed:
1829 let sort_of_ind_type =
1830 if parameters = [] then
1831 C.MutInd (uri,i,exp_named_subst)
1833 C.Appl ((C.MutInd (uri,i,exp_named_subst))::parameters)
1835 let type_of_sort_of_ind_ty,ugraph3 =
1836 type_of_aux ~logger context sort_of_ind_type ugraph2 in
1838 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1839 need_dummy sort_of_ind_type type_of_sort_of_ind_ty outsort ugraph3
1843 (TypeCheckerFailure (lazy ("Case analysis: sort elimination not allowed")));
1844 (* let's check if the type of branches are right *)
1845 let parsno,constructorsno =
1848 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1849 with Not_found -> assert false
1852 C.InductiveDefinition (il,_,parsno,_) ->
1854 try List.nth il i with Failure _ -> assert false
1856 parsno, List.length cl
1858 raise (TypeCheckerFailure
1859 (lazy ("Unknown mutual inductive definition:" ^
1860 UriManager.string_of_uri uri)))
1862 if List.length pl <> constructorsno then
1863 raise (TypeCheckerFailure
1864 (lazy ("Wrong number of cases in case analysis"))) ;
1865 let (_,branches_ok,ugraph5) =
1867 (fun (j,b,ugraph) p ->
1870 if parameters = [] then
1871 (C.MutConstruct (uri,i,j,exp_named_subst))
1874 (C.MutConstruct (uri,i,j,exp_named_subst)::parameters))
1876 let ty_p,ugraph1 = type_of_aux ~logger context p ugraph in
1877 let ty_cons,ugraph3 = type_of_aux ~logger context cons ugraph1 in
1880 type_of_branch ~subst context parsno need_dummy outtype cons
1884 ~subst ~metasenv context ty_p ty_branch ugraph3
1889 prerr_endline ("\n!OUTTYPE= " ^ CicPp.ppterm outtype);
1890 prerr_endline ("!CONS= " ^ CicPp.ppterm cons);
1891 prerr_endline ("!TY_CONS= " ^ CicPp.ppterm ty_cons);
1892 prerr_endline ("#### " ^ CicPp.ppterm ty_p ^ "\n<==>\n" ^ CicPp.ppterm ty_branch);
1897 ("#### " ^ CicPp.ppterm ty_p ^
1898 " <==> " ^ CicPp.ppterm ty_branch));
1902 ) (1,true,ugraph4) pl
1904 if not branches_ok then
1906 (TypeCheckerFailure (lazy "Case analysys: wrong branch type"));
1908 if not need_dummy then outtype::arguments@[term]
1909 else outtype::arguments in
1911 if need_dummy && arguments = [] then outtype
1912 else CicReduction.head_beta_reduce (C.Appl arguments')
1916 let types,kl,ugraph1,len =
1918 (fun (types,kl,ugraph,len) (n,k,ty,_) ->
1919 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1920 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1921 k::kl,ugraph1,len+1)
1922 ) ([],[],ugraph,0) fl
1926 (fun ugraph (name,x,ty,bo) ->
1928 type_of_aux ~logger (types@context) bo ugraph
1931 R.are_convertible ~subst ~metasenv (types@context)
1932 ty_bo (CicSubstitution.lift len ty) ugraph1 in
1935 let (m, eaten, context') =
1936 eat_lambdas ~subst (types @ context) (x + 1) bo
1939 let's control the guarded by
1940 destructors conditions D{f,k,x,M}
1942 if not (guarded_by_destructors ~subst context' eaten
1943 (len + eaten) kl 1 [] m) then
1946 (lazy ("Fix: not guarded by destructors")))
1951 raise (TypeCheckerFailure (lazy ("Fix: ill-typed bodies")))
1953 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1954 let (_,_,ty,_) = List.nth fl i in
1957 let types,ugraph1,len =
1959 (fun (l,ugraph,len) (n,ty,_) ->
1961 type_of_aux ~logger context ty ugraph in
1962 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::l,
1968 (fun ugraph (_,ty,bo) ->
1970 type_of_aux ~logger (types @ context) bo ugraph
1973 R.are_convertible ~subst ~metasenv (types @ context) ty_bo
1974 (CicSubstitution.lift len ty) ugraph1
1978 (* let's control that the returned type is coinductive *)
1979 match returns_a_coinductive ~subst context ty with
1983 (lazy "CoFix: does not return a coinductive type"))
1986 let's control the guarded by constructors
1989 if not (guarded_by_constructors ~subst
1990 (types @ context) 0 len false bo [] uri) then
1993 (lazy "CoFix: not guarded by constructors"))
1999 (TypeCheckerFailure (lazy "CoFix: ill-typed bodies"))
2002 let (_,ty,_) = List.nth fl i in
2005 and check_exp_named_subst ~logger ~subst context ugraph =
2006 let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
2009 | ((uri,t) as item)::tl ->
2010 let ty_uri,ugraph1 = type_of_variable ~logger uri ugraph in
2012 CicSubstitution.subst_vars esubsts ty_uri in
2013 let typeoft,ugraph2 = type_of_aux ~logger context t ugraph1 in
2015 CicReduction.are_convertible ~subst ~metasenv
2016 context typeoft typeofvar ugraph2
2019 check_exp_named_subst_aux ~logger (esubsts@[item]) tl ugraph3
2022 CicReduction.fdebug := 0 ;
2024 (CicReduction.are_convertible
2025 ~subst ~metasenv context typeoft typeofvar ugraph2) ;
2027 debug typeoft [typeofvar] ;
2028 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
2031 check_exp_named_subst_aux ~logger [] ugraph
2033 and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
2034 let module C = Cic in
2035 let t1' = CicReduction.whd ~subst context t1 in
2036 let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
2037 match (t1', t2') with
2038 (C.Sort s1, C.Sort s2)
2039 when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) ->
2040 (* different from Coq manual!!! *)
2042 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
2043 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
2044 let t' = CicUniv.fresh() in
2046 let ugraph1 = CicUniv.add_ge t' t1 ugraph in
2047 let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
2048 C.Sort (C.Type t'),ugraph2
2050 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
2051 | (C.Sort _,C.Sort (C.Type t1)) ->
2052 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
2053 C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
2054 | (C.Meta _, C.Sort _) -> t2',ugraph
2055 | (C.Meta _, (C.Meta (_,_) as t))
2056 | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
2058 | (_,_) -> raise (TypeCheckerFailure (lazy (sprintf
2059 "Prod: expected two sorts, found = %s, %s" (CicPp.ppterm t1')
2060 (CicPp.ppterm t2'))))
2062 and eat_prods ~subst context hetype l ugraph =
2063 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
2067 | (hete, hety)::tl ->
2068 (match (CicReduction.whd ~subst context hetype) with
2071 (*if (match hety,s with Cic.Sort _,Cic.Sort _ -> false | _,_ -> true) && hety <> s then(
2072 prerr_endline ("AAA22: " ^ CicPp.ppterm hete ^ ": " ^ CicPp.ppterm hety ^ " <==> " ^ CicPp.ppterm s); let res = CicReduction.are_convertible ~subst ~metasenv context hety s ugraph in prerr_endline "#"; res) else*)
2073 CicReduction.are_convertible
2074 ~subst ~metasenv context hety s ugraph
2078 CicReduction.fdebug := -1 ;
2079 eat_prods ~subst context
2080 (CicSubstitution.subst ~avoid_beta_redexes:true hete t)
2082 (*TASSI: not sure *)
2086 CicReduction.fdebug := 0 ;
2087 ignore (CicReduction.are_convertible
2088 ~subst ~metasenv context s hety ugraph) ;
2094 ("Appl: wrong parameter-type, expected %s, found %s")
2095 (CicPp.ppterm hetype) (CicPp.ppterm s))))
2098 raise (TypeCheckerFailure
2099 (lazy "Appl: this is not a function, it cannot be applied"))
2102 and returns_a_coinductive ~subst context ty =
2103 let module C = Cic in
2104 match CicReduction.whd ~subst context ty with
2105 C.MutInd (uri,i,_) ->
2106 (*CSC: definire una funzioncina per questo codice sempre replicato *)
2109 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
2110 with Not_found -> assert false
2113 C.InductiveDefinition (itl,_,_,_) ->
2114 let (_,is_inductive,_,_) = List.nth itl i in
2115 if is_inductive then None else (Some uri)
2117 raise (TypeCheckerFailure
2118 (lazy ("Unknown mutual inductive definition:" ^
2119 UriManager.string_of_uri uri)))
2121 | C.Appl ((C.MutInd (uri,i,_))::_) ->
2122 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
2124 C.InductiveDefinition (itl,_,_,_) ->
2125 let (_,is_inductive,_,_) = List.nth itl i in
2126 if is_inductive then None else (Some uri)
2128 raise (TypeCheckerFailure
2129 (lazy ("Unknown mutual inductive definition:" ^
2130 UriManager.string_of_uri uri)))
2132 | C.Prod (n,so,de) ->
2133 returns_a_coinductive ~subst ((Some (n,C.Decl so))::context) de
2138 debug_print (lazy ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t)) ; flush stderr ;
2141 type_of_aux ~logger context t ugraph
2143 in debug_print (lazy "FINE TYPE_OF_AUX") ; flush stderr ; res
2146 (* is a small constructor? *)
2147 (*CSC: ottimizzare calcolando staticamente *)
2148 and is_small_or_non_informative ~condition ~logger context paramsno c ugraph =
2149 let rec is_small_or_non_informative_aux ~logger context c ugraph =
2150 let module C = Cic in
2151 match CicReduction.whd context c with
2153 let s,ugraph1 = type_of_aux' ~logger [] context so ugraph in
2154 let b = condition s in
2156 is_small_or_non_informative_aux
2157 ~logger ((Some (n,(C.Decl so)))::context) de ugraph1
2160 | _ -> true,ugraph (*CSC: we trust the type-checker *)
2162 let (context',dx) = split_prods ~subst:[] context paramsno c in
2163 is_small_or_non_informative_aux ~logger context' dx ugraph
2165 and is_small ~logger =
2166 is_small_or_non_informative
2167 ~condition:(fun s -> s=Cic.Sort Cic.Prop || s=Cic.Sort Cic.Set)
2170 and is_non_informative ~logger =
2171 is_small_or_non_informative
2172 ~condition:(fun s -> s=Cic.Sort Cic.Prop)
2175 and type_of ~logger t ugraph =
2177 debug_print (lazy ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t)) ; flush stderr ;
2180 type_of_aux' ~logger [] [] t ugraph
2182 in debug_print (lazy "FINE TYPE_OF_AUX'") ; flush stderr ; res
2186 let typecheck_obj0 ~logger uri ugraph =
2187 let module C = Cic in
2189 C.Constant (_,Some te,ty,_,_) ->
2190 let _,ugraph = type_of ~logger ty ugraph in
2191 let ty_te,ugraph = type_of ~logger te ugraph in
2192 let b,ugraph = (CicReduction.are_convertible [] ty_te ty ugraph) in
2194 raise (TypeCheckerFailure
2196 ("the type of the body is not the one expected:\n" ^
2197 CicPp.ppterm ty_te ^ "\nvs\n" ^
2201 | C.Constant (_,None,ty,_,_) ->
2202 (* only to check that ty is well-typed *)
2203 let _,ugraph = type_of ~logger ty ugraph in
2205 | C.CurrentProof (_,conjs,te,ty,_,_) ->
2206 (* this block is broken since the metasenv should
2207 * be topologically sorted before typing metas *)
2208 ignore(assert false);
2211 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
2213 type_of_aux' ~logger metasenv context ty ugraph
2215 metasenv @ [conj],ugraph
2218 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
2219 let type_of_te,ugraph =
2220 type_of_aux' ~logger conjs [] te ugraph
2222 let b,ugraph = CicReduction.are_convertible [] type_of_te ty ugraph in
2224 raise (TypeCheckerFailure (lazy (sprintf
2225 "the current proof is not well typed because the type %s of the body is not convertible to the declared type %s"
2226 (CicPp.ppterm type_of_te) (CicPp.ppterm ty))))
2229 | C.Variable (_,bo,ty,_,_) ->
2230 (* only to check that ty is well-typed *)
2231 let _,ugraph = type_of ~logger ty ugraph in
2235 let ty_bo,ugraph = type_of ~logger bo ugraph in
2236 let b,ugraph = CicReduction.are_convertible [] ty_bo ty ugraph in
2238 raise (TypeCheckerFailure
2239 (lazy "the body is not the one expected"))
2243 | (C.InductiveDefinition _ as obj) ->
2244 check_mutual_inductive_defs ~logger uri obj ugraph
2247 let module C = Cic in
2248 let module R = CicReduction in
2249 let module U = UriManager in
2250 let logger = new CicLogger.logger in
2251 (* ??? match CicEnvironment.is_type_checked ~trust:true uri with ???? *)
2252 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2253 CicEnvironment.CheckedObj (cobj,ugraph') ->
2254 (* debug_print (lazy ("NON-INIZIO A TYPECHECKARE " ^ U.string_of_uri uri));*)
2256 | CicEnvironment.UncheckedObj uobj ->
2257 (* let's typecheck the uncooked object *)
2258 logger#log (`Start_type_checking uri) ;
2259 (* debug_print (lazy ("INIZIO A TYPECHECKARE " ^ U.string_of_uri uri)); *)
2260 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph uobj in
2262 CicEnvironment.set_type_checking_info uri;
2263 logger#log (`Type_checking_completed uri);
2264 match CicEnvironment.is_type_checked ~trust:false ugraph uri with
2265 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2266 | _ -> raise CicEnvironmentError
2269 this is raised if set_type_checking_info is called on an object
2270 that has no associated universe file. If we are in univ_maker
2271 phase this is OK since univ_maker will properly commit the
2274 Invalid_argument s ->
2275 (*debug_print (lazy s);*)
2279 let typecheck_obj ~logger uri obj =
2280 let ugraph = typecheck_obj0 ~logger uri CicUniv.empty_ugraph obj in
2281 let ugraph, univlist, obj = CicUnivUtils.clean_and_fill uri obj ugraph in
2282 CicEnvironment.add_type_checked_obj uri (obj,ugraph,univlist)
2284 (** wrappers which instantiate fresh loggers *)
2286 let profiler = HExtlib.profile "K/CicTypeChecker.type_of_aux'"
2288 let type_of_aux' ?(subst = []) metasenv context t ugraph =
2289 let logger = new CicLogger.logger in
2290 profiler.HExtlib.profile
2291 (type_of_aux' ~logger ~subst metasenv context t) ugraph
2293 let typecheck_obj uri obj =
2294 let logger = new CicLogger.logger in
2295 typecheck_obj ~logger uri obj
2297 (* check_allowed_sort_elimination uri i s1 s2
2298 This function is used outside the kernel to determine in advance whether
2299 a MutCase will be allowed or not.
2300 [uri,i] is the type of the term to match
2301 [s1] is the sort of the term to eliminate (i.e. the head of the arity
2302 of the inductive type [uri,i])
2303 [s2] is the sort of the goal (i.e. the head of the type of the outtype
2305 let check_allowed_sort_elimination uri i s1 s2 =
2306 fst (check_allowed_sort_elimination ~subst:[] ~metasenv:[]
2307 ~logger:(new CicLogger.logger) [] uri i true
2308 (Cic.Implicit None) (* never used *) (Cic.Sort s1) (Cic.Sort s2)
2309 CicUniv.empty_ugraph)
2312 Deannotate.type_of_aux' := fun context t -> fst (type_of_aux' [] context t CicUniv.oblivion_ugraph);;