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"))
58 let ugraph_convertibility ug1 ug2 ul2 = true;;
60 let check_and_clean_ugraph inferred_ugraph unchecked_ugraph uri obj =
61 match unchecked_ugraph with
63 if not (ugraph_convertibility inferred_ugraph ug ul) then
64 raise (TypeCheckerFailure (lazy
65 ("inferred univ graph not equal with declared ugraph")))
69 CicUnivUtils.clean_and_fill uri obj inferred_ugraph
72 let debrujin_constructor ?(cb=fun _ _ -> ()) ?(check_exp_named_subst=true) uri number_of_types context =
77 C.Rel n as t when n <= k -> t
79 raise (TypeCheckerFailure (lazy "unbound variable found in constructor type"))
80 | C.Var (uri,exp_named_subst) ->
81 let exp_named_subst' =
82 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
84 C.Var (uri,exp_named_subst')
86 let l' = List.map (function None -> None | Some t -> Some (aux k t)) l in
89 | C.Implicit _ as t -> t
90 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
91 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k+1) t)
92 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k+1) t)
93 | C.LetIn (n,s,ty,t) -> C.LetIn (n, aux k s, aux k ty, aux (k+1) t)
94 | C.Appl l -> C.Appl (List.map (aux k) l)
95 | C.Const (uri,exp_named_subst) ->
96 let exp_named_subst' =
97 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
99 C.Const (uri,exp_named_subst')
100 | C.MutInd (uri',tyno,exp_named_subst) when UriManager.eq uri uri' ->
101 if check_exp_named_subst && exp_named_subst != [] then
102 raise (TypeCheckerFailure
103 (lazy ("non-empty explicit named substitution is applied to "^
104 "a mutual inductive type which is being defined"))) ;
105 C.Rel (k + number_of_types - tyno) ;
106 | C.MutInd (uri',tyno,exp_named_subst) ->
107 let exp_named_subst' =
108 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
110 C.MutInd (uri',tyno,exp_named_subst')
111 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
112 let exp_named_subst' =
113 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
115 C.MutConstruct (uri,tyno,consno,exp_named_subst')
116 | C.MutCase (sp,i,outty,t,pl) ->
117 C.MutCase (sp, i, aux k outty, aux k t,
120 let len = List.length fl in
123 (fun (name, i, ty, bo) -> (name, i, aux k ty, aux (k+len) bo))
128 let len = List.length fl in
131 (fun (name, ty, bo) -> (name, aux k ty, aux (k+len) bo))
134 C.CoFix (i, liftedfl)
139 aux (List.length context)
142 exception CicEnvironmentError;;
144 let rec type_of_constant ~logger uri orig_ugraph =
145 let module C = Cic in
146 let module R = CicReduction in
147 let module U = UriManager in
149 match CicEnvironment.is_type_checked ~trust:true orig_ugraph uri with
150 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
151 | CicEnvironment.UncheckedObj (uobj,unchecked_ugraph) ->
152 logger#log (`Start_type_checking uri) ;
153 (* let's typecheck the uncooked obj *)
154 let inferred_ugraph =
156 C.Constant (_,Some te,ty,_,_) ->
157 let _,ugraph = type_of ~logger ty CicUniv.empty_ugraph in
158 let type_of_te,ugraph = type_of ~logger te ugraph in
159 let b,ugraph = R.are_convertible [] type_of_te ty ugraph in
161 raise (TypeCheckerFailure (lazy (sprintf
162 "the constant %s is not well typed because the type %s of the body is not convertible to the declared type %s"
163 (U.string_of_uri uri) (CicPp.ppterm type_of_te)
167 | C.Constant (_,None,ty,_,_) ->
168 (* only to check that ty is well-typed *)
169 let _,ugraph = type_of ~logger ty CicUniv.empty_ugraph in
171 | C.CurrentProof (_,conjs,te,ty,_,_) ->
174 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
176 type_of_aux' ~logger metasenv context ty ugraph
178 (metasenv @ [conj],ugraph)
179 ) ([],CicUniv.empty_ugraph) conjs
181 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
182 let type_of_te,ugraph = type_of_aux' ~logger conjs [] te ugraph in
183 let b,ugraph = R.are_convertible [] type_of_te ty ugraph in
185 raise (TypeCheckerFailure (lazy (sprintf
186 "the current proof %s is not well typed because the type %s of the body is not convertible to the declared type %s"
187 (U.string_of_uri uri) (CicPp.ppterm type_of_te)
193 (TypeCheckerFailure (lazy ("Unknown constant:" ^ U.string_of_uri uri)))
195 let ugraph, ul, obj = check_and_clean_ugraph inferred_ugraph unchecked_ugraph uri uobj in
196 CicEnvironment.set_type_checking_info uri (obj, ugraph, ul);
197 logger#log (`Type_checking_completed uri) ;
198 match CicEnvironment.is_type_checked ~trust:false orig_ugraph uri with
199 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
200 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
202 match cobj,ugraph with
203 (C.Constant (_,_,ty,_,_)),g -> ty,g
204 | (C.CurrentProof (_,_,_,ty,_,_)),g -> ty,g
206 raise (TypeCheckerFailure (lazy ("Unknown constant:" ^ U.string_of_uri uri)))
208 and type_of_variable ~logger uri orig_ugraph =
209 let module C = Cic in
210 let module R = CicReduction in
211 let module U = UriManager in
212 (* 0 because a variable is never cooked => no partial cooking at one level *)
213 match CicEnvironment.is_type_checked ~trust:true orig_ugraph uri with
214 | CicEnvironment.CheckedObj ((C.Variable (_,_,ty,_,_)),ugraph') -> ty,ugraph'
215 | CicEnvironment.UncheckedObj
216 (C.Variable (_,bo,ty,_,_) as uobj, unchecked_ugraph)
218 logger#log (`Start_type_checking uri) ;
219 (* only to check that ty is well-typed *)
220 let _,ugraph = type_of ~logger ty CicUniv.empty_ugraph in
221 let inferred_ugraph =
225 let ty_bo,ugraph = type_of ~logger bo ugraph in
226 let b,ugraph = R.are_convertible [] ty_bo ty ugraph in
228 raise (TypeCheckerFailure
229 (lazy ("Unknown variable:" ^ U.string_of_uri uri)))
233 let ugraph, ul, obj =
234 check_and_clean_ugraph inferred_ugraph unchecked_ugraph uri uobj
236 CicEnvironment.set_type_checking_info uri (obj, ugraph, ul);
237 logger#log (`Type_checking_completed uri) ;
238 (match CicEnvironment.is_type_checked ~trust:false orig_ugraph uri with
239 CicEnvironment.CheckedObj((C.Variable(_,_,ty,_,_)),ugraph)->ty,ugraph
240 | CicEnvironment.CheckedObj _
241 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError)
243 raise (TypeCheckerFailure (lazy
244 ("Unknown variable:" ^ U.string_of_uri uri)))
246 and does_not_occur ?(subst=[]) context n nn te =
247 let module C = Cic in
249 C.Rel m when m > n && m <= nn -> false
252 (match List.nth context (m-1) with
253 Some (_,C.Def (bo,_)) ->
254 does_not_occur ~subst context n nn (CicSubstitution.lift m bo)
257 Failure _ -> assert false)
259 | C.Implicit _ -> true
265 | Some x -> i && does_not_occur ~subst context n nn x) l true &&
267 let (canonical_context,term,ty) = CicUtil.lookup_subst mno subst in
268 does_not_occur ~subst context n nn (CicSubstitution.subst_meta l term)
270 CicUtil.Subst_not_found _ -> true)
272 does_not_occur ~subst context n nn te &&
273 does_not_occur ~subst context n nn ty
274 | C.Prod (name,so,dest) ->
275 does_not_occur ~subst context n nn so &&
276 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n + 1)
278 | C.Lambda (name,so,dest) ->
279 does_not_occur ~subst context n nn so &&
280 does_not_occur ~subst ((Some (name,(C.Decl so)))::context) (n+1) (nn+1)
282 | C.LetIn (name,so,ty,dest) ->
283 does_not_occur ~subst context n nn so &&
284 does_not_occur ~subst context n nn ty &&
285 does_not_occur ~subst ((Some (name,(C.Def (so,ty))))::context)
286 (n + 1) (nn + 1) dest
288 List.for_all (does_not_occur ~subst context n nn) l
289 | C.Var (_,exp_named_subst)
290 | C.Const (_,exp_named_subst)
291 | C.MutInd (_,_,exp_named_subst)
292 | C.MutConstruct (_,_,_,exp_named_subst) ->
293 List.for_all (fun (_,x) -> does_not_occur ~subst context n nn x)
295 | C.MutCase (_,_,out,te,pl) ->
296 does_not_occur ~subst context n nn out &&
297 does_not_occur ~subst context n nn te &&
298 List.for_all (does_not_occur ~subst context n nn) pl
300 let len = List.length fl in
301 let n_plus_len = n + len in
302 let nn_plus_len = nn + len in
305 (fun (types,len) (n,_,ty,_) ->
306 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
311 (fun (_,_,ty,bo) i ->
312 i && does_not_occur ~subst context n nn ty &&
313 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
316 let len = List.length fl in
317 let n_plus_len = n + len in
318 let nn_plus_len = nn + len in
321 (fun (types,len) (n,ty,_) ->
322 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
328 i && does_not_occur ~subst context n nn ty &&
329 does_not_occur ~subst (tys @ context) n_plus_len nn_plus_len bo
332 (*CSC l'indice x dei tipi induttivi e' t.c. n < x <= nn *)
333 (*CSC questa funzione e' simile alla are_all_occurrences_positive, ma fa *)
334 (*CSC dei controlli leggermente diversi. Viene invocata solamente dalla *)
335 (*CSC strictly_positive *)
336 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-) *)
337 and weakly_positive context n nn uri te =
338 let module C = Cic in
339 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
341 C.MutInd (HelmLibraryObjects.Datatypes.nat_URI,0,[])
343 (*CSC: mettere in cicSubstitution *)
344 let rec subst_inductive_type_with_dummy_mutind =
346 C.MutInd (uri',0,_) when UriManager.eq uri' uri ->
348 | C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri ->
350 | C.Cast (te,ty) -> subst_inductive_type_with_dummy_mutind te
351 | C.Prod (name,so,ta) ->
352 C.Prod (name, subst_inductive_type_with_dummy_mutind so,
353 subst_inductive_type_with_dummy_mutind ta)
354 | C.Lambda (name,so,ta) ->
355 C.Lambda (name, subst_inductive_type_with_dummy_mutind so,
356 subst_inductive_type_with_dummy_mutind ta)
357 | C.LetIn (name,so,ty,ta) ->
358 C.LetIn (name, subst_inductive_type_with_dummy_mutind so,
359 subst_inductive_type_with_dummy_mutind ty,
360 subst_inductive_type_with_dummy_mutind ta)
362 C.Appl (List.map subst_inductive_type_with_dummy_mutind tl)
363 | C.MutCase (uri,i,outtype,term,pl) ->
365 subst_inductive_type_with_dummy_mutind outtype,
366 subst_inductive_type_with_dummy_mutind term,
367 List.map subst_inductive_type_with_dummy_mutind pl)
369 C.Fix (i,List.map (fun (name,i,ty,bo) -> (name,i,
370 subst_inductive_type_with_dummy_mutind ty,
371 subst_inductive_type_with_dummy_mutind bo)) fl)
373 C.CoFix (i,List.map (fun (name,ty,bo) -> (name,
374 subst_inductive_type_with_dummy_mutind ty,
375 subst_inductive_type_with_dummy_mutind bo)) fl)
376 | C.Const (uri,exp_named_subst) ->
377 let exp_named_subst' =
379 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
382 C.Const (uri,exp_named_subst')
383 | C.Var (uri,exp_named_subst) ->
384 let exp_named_subst' =
386 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
389 C.Var (uri,exp_named_subst')
390 | C.MutInd (uri,typeno,exp_named_subst) ->
391 let exp_named_subst' =
393 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
396 C.MutInd (uri,typeno,exp_named_subst')
397 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
398 let exp_named_subst' =
400 (function (uri,t) -> (uri,subst_inductive_type_with_dummy_mutind t))
403 C.MutConstruct (uri,typeno,consno,exp_named_subst')
406 match CicReduction.whd context te with
408 C.Appl ((C.MutInd (uri',0,_))::tl) when UriManager.eq uri' uri -> true
410 C.Appl ((C.MutInd (uri',_,_))::tl) when UriManager.eq uri' uri -> true
411 | C.MutInd (uri',0,_) when UriManager.eq uri' uri -> true
412 | C.Prod (name,source,dest) when
413 does_not_occur ((Some (name,(C.Decl source)))::context) 0 1 dest ->
414 (* dummy abstraction, so we behave as in the anonimous case *)
415 strictly_positive context n nn
416 (subst_inductive_type_with_dummy_mutind source) &&
417 weakly_positive ((Some (name,(C.Decl source)))::context)
418 (n + 1) (nn + 1) uri dest
419 | C.Prod (name,source,dest) ->
420 does_not_occur context n nn
421 (subst_inductive_type_with_dummy_mutind source)&&
422 weakly_positive ((Some (name,(C.Decl source)))::context)
423 (n + 1) (nn + 1) uri dest
425 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
427 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
428 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
429 and instantiate_parameters params c =
430 let module C = Cic in
431 match (c,params) with
433 | (C.Prod (_,_,ta), he::tl) ->
434 instantiate_parameters tl
435 (CicSubstitution.subst he ta)
436 | (C.Cast (te,_), _) -> instantiate_parameters params te
437 | (t,l) -> raise (AssertFailure (lazy "1"))
439 and strictly_positive context n nn te =
440 let module C = Cic in
441 let module U = UriManager in
442 match CicReduction.whd context te with
443 | t when does_not_occur context n nn t -> true
446 (*CSC: bisogna controllare ty????*)
447 strictly_positive context n nn te
448 | C.Prod (name,so,ta) ->
449 does_not_occur context n nn so &&
450 strictly_positive ((Some (name,(C.Decl so)))::context) (n+1) (nn+1) ta
451 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
452 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
453 | C.Appl ((C.MutInd (uri,i,exp_named_subst))::_)
454 | (C.MutInd (uri,i,exp_named_subst)) as t ->
455 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
456 let (ok,paramsno,ity,cl,name) =
457 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
459 C.InductiveDefinition (tl,_,paramsno,_) ->
460 let (name,_,ity,cl) = List.nth tl i in
461 (List.length tl = 1, paramsno, ity, cl, name)
462 (* (true, paramsno, ity, cl, name) *)
466 (lazy ("Unknown inductive type:" ^ U.string_of_uri uri)))
468 let (params,arguments) = split tl paramsno in
469 let lifted_params = List.map (CicSubstitution.lift 1) params in
473 instantiate_parameters lifted_params
474 (CicSubstitution.subst_vars exp_named_subst te)
479 (fun x i -> i && does_not_occur context n nn x)
485 ((Some (C.Name name,(Cic.Decl ity)))::context) (n+1) (nn+1) uri
490 (* the inductive type indexes are s.t. n < x <= nn *)
491 and are_all_occurrences_positive context uri indparamsno i n nn te =
492 let module C = Cic in
493 match CicReduction.whd context te with
494 C.Appl ((C.Rel m)::tl) when m = i ->
495 (*CSC: riscrivere fermandosi a 0 *)
496 (* let's check if the inductive type is applied at least to *)
497 (* indparamsno parameters *)
503 match CicReduction.whd context x with
504 C.Rel m when m = n - (indparamsno - k) -> k - 1
506 raise (TypeCheckerFailure
508 ("Non-positive occurence in mutual inductive definition(s) [1]" ^
509 UriManager.string_of_uri uri)))
513 List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
515 raise (TypeCheckerFailure
516 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
517 UriManager.string_of_uri uri)))
518 | C.Rel m when m = i ->
519 if indparamsno = 0 then
522 raise (TypeCheckerFailure
523 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
524 UriManager.string_of_uri uri)))
525 | C.Prod (name,source,dest) when
526 does_not_occur ((Some (name,(C.Decl source)))::context) 0 1 dest ->
527 (* dummy abstraction, so we behave as in the anonimous case *)
528 strictly_positive context n nn source &&
529 are_all_occurrences_positive
530 ((Some (name,(C.Decl source)))::context) uri indparamsno
531 (i+1) (n + 1) (nn + 1) dest
532 | C.Prod (name,source,dest) ->
533 does_not_occur context n nn source &&
534 are_all_occurrences_positive ((Some (name,(C.Decl source)))::context)
535 uri indparamsno (i+1) (n + 1) (nn + 1) dest
538 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
539 (UriManager.string_of_uri uri))))
541 (* Main function to checks the correctness of a mutual *)
542 (* inductive block definition. This is the function *)
543 (* exported to the proof-engine. *)
544 and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
545 let module U = UriManager in
546 (* let's check if the arity of the inductive types are well *)
548 let ugrap1 = List.fold_left
549 (fun ugraph (_,_,x,_) -> let _,ugraph' =
550 type_of ~logger x ugraph in ugraph')
553 (* let's check if the types of the inductive constructors *)
554 (* are well formed. *)
555 (* In order not to use type_of_aux we put the types of the *)
556 (* mutual inductive types at the head of the types of the *)
557 (* constructors using Prods *)
558 let len = List.length itl in
560 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
563 (fun (_,_,_,cl) (i,ugraph) ->
566 (fun ugraph (name,te) ->
567 let debrujinedte = debrujin_constructor uri len [] te in
570 (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
573 let _,ugraph' = type_of ~logger augmented_term ugraph in
574 (* let's check also the positivity conditions *)
577 (are_all_occurrences_positive tys uri indparamsno i 0 len
581 prerr_endline (UriManager.string_of_uri uri);
582 prerr_endline (string_of_int (List.length tys));
585 (lazy ("Non positive occurence in " ^ U.string_of_uri uri))) end
594 (* Main function to checks the correctness of a mutual *)
595 (* inductive block definition. *)
596 and check_mutual_inductive_defs uri obj ugraph =
598 Cic.InductiveDefinition (itl, params, indparamsno, _) ->
599 typecheck_mutual_inductive_defs uri (itl,params,indparamsno) ugraph
601 raise (TypeCheckerFailure (
602 lazy ("Unknown mutual inductive definition:" ^
603 UriManager.string_of_uri uri)))
605 and type_of_mutual_inductive_defs ~logger uri i orig_ugraph =
606 let module C = Cic in
607 let module R = CicReduction in
608 let module U = UriManager in
610 match CicEnvironment.is_type_checked ~trust:true orig_ugraph uri with
611 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
612 | CicEnvironment.UncheckedObj (uobj,unchecked_ugraph) ->
613 logger#log (`Start_type_checking uri) ;
614 let inferred_ugraph =
615 check_mutual_inductive_defs ~logger uri uobj CicUniv.empty_ugraph
617 let ugraph, ul, obj = check_and_clean_ugraph inferred_ugraph unchecked_ugraph uri uobj in
618 CicEnvironment.set_type_checking_info uri (obj,ugraph,ul);
619 logger#log (`Type_checking_completed uri) ;
620 (match CicEnvironment.is_type_checked ~trust:false orig_ugraph uri with
621 CicEnvironment.CheckedObj (cobj,ugraph') -> (cobj,ugraph')
622 | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
626 | C.InductiveDefinition (dl,_,_,_) ->
627 let (_,_,arity,_) = List.nth dl i in
630 raise (TypeCheckerFailure
631 (lazy ("Unknown mutual inductive definition:" ^ U.string_of_uri uri)))
633 and type_of_mutual_inductive_constr ~logger uri i j orig_ugraph =
634 let module C = Cic in
635 let module R = CicReduction in
636 let module U = UriManager in
638 match CicEnvironment.is_type_checked ~trust:true orig_ugraph uri with
639 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
640 | CicEnvironment.UncheckedObj (uobj,unchecked_ugraph) ->
641 logger#log (`Start_type_checking uri) ;
642 let inferred_ugraph =
643 check_mutual_inductive_defs ~logger uri uobj CicUniv.empty_ugraph
645 let ugraph, ul, obj = check_and_clean_ugraph inferred_ugraph unchecked_ugraph uri uobj in
646 CicEnvironment.set_type_checking_info uri (obj, ugraph, ul);
647 logger#log (`Type_checking_completed uri) ;
649 CicEnvironment.is_type_checked ~trust:false orig_ugraph uri
651 CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
652 | CicEnvironment.UncheckedObj _ ->
653 raise CicEnvironmentError)
656 C.InductiveDefinition (dl,_,_,_) ->
657 let (_,_,_,cl) = List.nth dl i in
658 let (_,ty) = List.nth cl (j-1) in
661 raise (TypeCheckerFailure
662 (lazy ("Unknown mutual inductive definition:" ^ UriManager.string_of_uri uri)))
664 and recursive_args context n nn te =
665 let module C = Cic in
666 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"))
686 | C.CoFix _ -> raise (AssertFailure (lazy "6")) (* due to type-checking *)
688 and get_new_safes ~subst context p rl safes n nn x =
689 let module C = Cic in
690 let module U = UriManager in
691 let module R = CicReduction in
692 match R.whd ~subst context p, rl with
693 | C.Lambda (name,so,ta), b::tl ->
694 let safes = List.map (fun x -> x + 1) safes in
695 let safes = if b then 1::safes else safes in
696 get_new_safes ~subst ((Some (name,(C.Decl so)))::context)
697 ta tl safes (n+1) (nn+1) (x+1)
698 | C.MutConstruct _ as e, _
700 | e, [] -> (e,safes,n,nn,x,context)
704 (Printf.sprintf "Get New Safes: p=%s" (CicPp.ppterm p))))
706 and split_prods ~subst context n te =
707 let module C = Cic in
708 let module R = CicReduction in
709 match (n, R.whd ~subst context te) with
711 | (n, C.Prod (name,so,ta)) when n > 0 ->
712 split_prods ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
713 | (_, _) -> raise (AssertFailure (lazy "8"))
715 and eat_lambdas ~subst context n te =
716 let module C = Cic in
717 let module R = CicReduction in
718 match (n, R.whd ~subst context te) with
719 (0, _) -> (te, 0, context)
720 | (n, C.Lambda (name,so,ta)) when n > 0 ->
721 let (te, k, context') =
722 eat_lambdas ~subst ((Some (name,(C.Decl so)))::context) (n - 1) ta
724 (te, k + 1, context')
726 raise (AssertFailure (lazy (sprintf "9 (%d, %s)" n (CicPp.ppterm te))))
728 and specialize_inductive_type ~logger ~subst ~metasenv context t =
729 let ty,_= type_of_aux' ~logger ~subst metasenv context t CicUniv.oblivion_ugraph in
730 match CicReduction.whd ~subst context ty with
731 | Cic.MutInd (uri,_,exp)
732 | Cic.Appl (Cic.MutInd (uri,_,exp) :: _) as ty ->
733 let args = match ty with Cic.Appl (_::tl) -> tl | _ -> [] in
734 let o,_ = CicEnvironment.get_obj CicUniv.oblivion_ugraph uri in
736 | Cic.InductiveDefinition (tl,_,paramsno,_) ->
737 let left_args,_ = HExtlib.split_nth paramsno args in
738 List.map (fun (name, isind, arity, cl) ->
739 let arity = CicSubstitution.subst_vars exp arity in
740 let arity = instantiate_parameters left_args arity in
744 let ty = CicSubstitution.subst_vars exp ty in
745 id, instantiate_parameters left_args ty)
748 name, isind, arity, cl)
753 and check_is_really_smaller_arg
754 ~logger ~metasenv ~subst rec_uri rec_uri_len context n nn kl x safes te
756 let module C = Cic in
757 let module U = UriManager in
758 (*CSC: we could perform beta-iota(-zeta?) immediately, and
759 delta only on-demand when it fails without *)
760 match CicReduction.whd ~subst context te with
761 C.Rel m when List.mem m safes -> true
767 check_is_really_smaller_arg rec_uri rec_uri_len
768 ~logger ~metasenv ~subst context n nn kl x safes he
769 | C.Lambda (name,ty,ta) ->
770 check_is_really_smaller_arg rec_uri rec_uri_len
771 ~logger ~metasenv ~subst (Some (name,Cic.Decl ty)::context)
772 (n+1) (nn+1) kl (x+1) (List.map (fun n -> n+1) safes) ta
773 | C.MutCase (uri,i,outtype,term,pl) ->
775 | C.Rel m | C.Appl ((C.Rel m)::_) when List.mem m safes || m = x ->
777 specialize_inductive_type ~logger ~subst ~metasenv context term
781 (fun (types,len) (n,_,ty,_) ->
782 Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
786 let _,isinductive,_,cl = List.nth tys i in
787 if not isinductive then
789 (check_is_really_smaller_arg rec_uri rec_uri_len
790 ~logger ~metasenv ~subst context n nn kl x safes)
797 debrujin_constructor ~check_exp_named_subst:false
798 rec_uri rec_uri_len context c in
799 let len_ctx = List.length context in
800 recursive_args (context@tys_ctx) len_ctx (len_ctx+rec_uri_len) c
802 let (e, safes',n',nn',x',context') =
803 get_new_safes ~subst context p rec_params safes n nn x
805 check_is_really_smaller_arg rec_uri rec_uri_len
806 ~logger ~metasenv ~subst context' n' nn' kl x' safes' e
810 (check_is_really_smaller_arg
811 rec_uri rec_uri_len ~logger ~metasenv ~subst
812 context n nn kl x safes) pl
815 let len = List.length fl in
816 let n_plus_len = n + len
817 and nn_plus_len = nn + len
818 and x_plus_len = x + len
821 (fun (types,len) (n,_,ty,_) ->
822 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
825 and safes' = List.map (fun x -> x + len) safes in
828 check_is_really_smaller_arg
829 rec_uri rec_uri_len ~logger ~metasenv ~subst
830 (tys@context) n_plus_len nn_plus_len kl
834 raise (AssertFailure (lazy ("An inhabitant of an inductive type in normal form cannot have this shape: " ^ CicPp.ppterm t)))
836 and guarded_by_destructors
837 ~logger ~metasenv ~subst rec_uri rec_uri_len context n nn kl x safes t
839 let module C = Cic in
840 let module U = UriManager in
841 match CicReduction.whd ~subst context t with
842 C.Rel m when m > n && m <= nn -> false
844 (match List.nth context (m-1) with
845 Some (_,C.Decl _) -> true
846 | Some (_,C.Def (bo,_)) ->
847 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes
848 (CicSubstitution.lift m bo)
849 | None -> raise (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
853 | C.Implicit _ -> true
855 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes te &&
856 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes ty
857 | C.Prod (name,so,ta) ->
858 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes so &&
859 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst ((Some (name,(C.Decl so)))::context)
860 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
861 | C.Lambda (name,so,ta) ->
862 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes so &&
863 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst ((Some (name,(C.Decl so)))::context)
864 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
865 | C.LetIn (name,so,ty,ta) ->
866 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes so &&
867 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes ty &&
868 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst ((Some (name,(C.Def (so,ty))))::context)
869 (n+1) (nn+1) kl (x+1) (List.map (fun x -> x + 1) safes) ta
870 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
871 let k = List.nth kl (m - n - 1) in
872 if not (List.length tl > k) then false
875 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes) tl &&
876 check_is_really_smaller_arg
878 ~logger ~metasenv ~subst context n nn kl x safes (List.nth tl k)
879 | C.Var (_,exp_named_subst)
880 | C.Const (_,exp_named_subst)
881 | C.MutInd (_,_,exp_named_subst)
882 | C.MutConstruct (_,_,_,exp_named_subst) ->
884 (fun (_,t) -> guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes t)
886 | C.MutCase (uri,i,outtype,term,pl) ->
887 (match CicReduction.whd ~subst context term with
889 | C.Appl ((C.Rel m)::_) as t when List.mem m safes || m = x ->
890 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
892 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes)
895 specialize_inductive_type ~logger ~subst ~metasenv context t
899 (fun (types,len) (n,_,ty,_) ->
900 Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
904 let _,isinductive,_,cl = List.nth tys i in
905 if not isinductive then
906 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes outtype &&
907 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes term &&
909 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes)
912 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes outtype &&
917 debrujin_constructor ~check_exp_named_subst:false
918 rec_uri rec_uri_len context c in
919 let len_ctx = List.length context in
920 recursive_args (context@tys_ctx) len_ctx (len_ctx+rec_uri_len) c
922 let (e, safes',n',nn',x',context') =
923 get_new_safes ~subst context p rec_params safes n nn x
925 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context' n' nn' kl x' safes' e
928 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes outtype &&
929 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes term &&
930 (*CSC: manca ??? il controllo sul tipo di term? *)
932 (fun p i -> i && guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes p)
935 | C.Appl (C.Fix (fixno, fl)::_) | C.Fix (fixno,fl) as t->
936 let l = match t with C.Appl (_::tl) -> tl | _ -> [] in
937 let len = List.length fl in
938 let n_plus_len = n + len in
939 let nn_plus_len = nn + len in
940 let x_plus_len = x + len in
943 (fun (types,len) (n,_,ty,_) ->
944 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
947 let safes' = List.map (fun x -> x + len) safes in
949 (guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes) l &&
951 (fun (fixno',i) (_,recno,ty,bo) ->
954 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x_plus_len safes' ty &&
957 List.length l > recno &&
958 (*case where the recursive argument is already really_smaller *)
959 check_is_really_smaller_arg
960 rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes
963 let bo_without_lambdas,_,context =
964 eat_lambdas ~subst (tys@context) (recno+1) bo
966 (* we assume the formal argument to be safe *)
967 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context (n_plus_len+recno+1)
968 (nn_plus_len+recno+1) kl (x_plus_len+recno+1)
969 (1::List.map (fun x -> x+recno+1) safes')
972 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst (tys@context) n_plus_len nn_plus_len
973 kl x_plus_len safes' bo
976 let len = List.length fl in
977 let n_plus_len = n + len
978 and nn_plus_len = nn + len
979 and x_plus_len = x + len
982 (fun (types,len) (n,ty,_) ->
983 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
986 and safes' = List.map (fun x -> x + len) safes in
990 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x_plus_len safes' ty &&
991 guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst (tys@context) n_plus_len nn_plus_len kl
996 (fun t i -> i && guarded_by_destructors rec_uri rec_uri_len ~logger ~metasenv ~subst context n nn kl x safes t)
999 (* the boolean h means already protected *)
1000 (* args is the list of arguments the type of the constructor that may be *)
1001 (* found in head position must be applied to. *)
1002 and guarded_by_constructors ~logger ~subst ~metasenv indURI =
1003 let module C = Cic in
1004 let rec aux context n nn h te =
1005 match CicReduction.whd ~subst context te with
1006 | C.Rel m when m > n && m <= nn -> h
1014 | C.LetIn _ -> raise (AssertFailure (lazy "17"))
1015 | C.Lambda (name,so,de) ->
1016 does_not_occur ~subst context n nn so &&
1017 aux ((Some (name,(C.Decl so)))::context) (n + 1) (nn + 1) h de
1018 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
1019 h && List.for_all (does_not_occur ~subst context n nn) tl
1020 | C.MutConstruct (_,_,_,exp_named_subst) ->
1022 (fun (_,x) -> does_not_occur ~subst context n nn x) exp_named_subst
1023 | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) as t ->
1025 (fun (_,x) -> does_not_occur ~subst context n nn x) exp_named_subst &&
1026 let consty, len_tys, tys_ctx, paramsno =
1028 specialize_inductive_type ~logger ~subst ~metasenv context t in
1029 let _,_,_,cl = List.nth tys i in
1030 let _,ty = List.nth cl (j-1) in
1031 ty, List.length tys,
1033 (fun (types,len) (n,_,ty,_) ->
1034 Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types, len+1)
1035 ([],0) tys), paramsno
1039 debrujin_constructor ~check_exp_named_subst:false
1040 indURI len_tys context consty
1042 let len_ctx = List.length context in
1043 recursive_args (context@tys_ctx) len_ctx (len_ctx+len_tys) c
1045 let rec analyse_instantiated_type rec_spec args =
1046 match rec_spec, args with
1047 | h::rec_spec, he::args ->
1048 aux context n nn h he &&
1049 analyse_instantiated_type rec_spec args
1051 | _ -> raise (AssertFailure (lazy
1052 ("Too many args for constructor: " ^ String.concat " "
1053 (List.map (fun x-> CicPp.ppterm x) args))))
1055 let left, args = HExtlib.split_nth paramsno tl in
1056 List.for_all (does_not_occur ~subst context n nn) left &&
1057 analyse_instantiated_type rec_params args
1058 | C.Appl ((C.MutCase (_,_,out,te,pl))::_)
1059 | C.MutCase (_,_,out,te,pl) as t ->
1060 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
1061 List.for_all (does_not_occur ~subst context n nn) tl &&
1062 does_not_occur ~subst context n nn out &&
1063 does_not_occur ~subst context n nn te &&
1064 List.for_all (aux context n nn h ) pl
1066 | C.Appl (C.Fix (_,fl)::_) as t ->
1067 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
1068 let len = List.length fl in
1069 let n_plus_len = n + len
1070 and nn_plus_len = nn + len
1073 (fun (types,len) (n,_,ty,_) ->
1074 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1078 List.for_all (does_not_occur ~subst context n nn) tl &&
1081 does_not_occur ~subst context n nn ty &&
1082 aux (tys@context) n_plus_len nn_plus_len h bo)
1084 | C.Appl ((C.CoFix (_,fl))::_)
1085 | C.CoFix (_,fl) as t ->
1086 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
1087 let len = List.length fl in
1088 let n_plus_len = n + len
1089 and nn_plus_len = nn + len
1092 (fun (types,len) (n,ty,_) ->
1093 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1097 List.for_all (does_not_occur ~subst context n nn) tl &&
1100 does_not_occur ~subst context n nn ty &&
1101 aux (tys@context) n_plus_len nn_plus_len h bo)
1105 | C.Appl _ as t -> does_not_occur ~subst context n nn t
1109 and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1110 need_dummy ind arity1 arity2 ugraph =
1111 let module C = Cic in
1112 let module U = UriManager in
1113 let arity1 = CicReduction.whd ~subst context arity1 in
1114 let rec check_allowed_sort_elimination_aux ugraph context arity2 need_dummy =
1115 match arity1, CicReduction.whd ~subst context arity2 with
1116 (C.Prod (name,so1,de1), C.Prod (_,so2,de2)) ->
1118 CicReduction.are_convertible ~subst ~metasenv context so1 so2 ugraph in
1120 check_allowed_sort_elimination ~subst ~metasenv ~logger
1121 ((Some (name,C.Decl so1))::context) uri i
1122 need_dummy (C.Appl [CicSubstitution.lift 1 ind ; C.Rel 1]) de1 de2
1126 | (C.Sort _, C.Prod (name,so,ta)) when not need_dummy ->
1128 CicReduction.are_convertible ~subst ~metasenv context so ind ugraph in
1132 check_allowed_sort_elimination_aux ugraph1
1133 ((Some (name,C.Decl so))::context) ta true
1134 | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
1135 | (C.Sort C.Prop, C.Sort C.Set)
1136 | (C.Sort C.Prop, C.Sort C.CProp)
1137 | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
1138 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1140 C.InductiveDefinition (itl,_,paramsno,_) ->
1141 let itl_len = List.length itl in
1142 let (name,_,ty,cl) = List.nth itl i in
1143 let cl_len = List.length cl in
1144 if (cl_len = 0 || (itl_len = 1 && cl_len = 1)) then
1145 let non_informative,ugraph =
1146 if cl_len = 0 then true,ugraph
1148 is_non_informative ~logger [Some (C.Name name,C.Decl ty)]
1149 paramsno (snd (List.nth cl 0)) ugraph
1151 (* is it a singleton or empty non recursive and non informative
1153 non_informative, ugraph
1157 raise (TypeCheckerFailure
1158 (lazy ("Unknown mutual inductive definition:" ^
1159 UriManager.string_of_uri uri)))
1161 | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
1162 | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
1163 | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
1164 | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
1165 | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
1166 | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
1167 | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
1169 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1171 C.InductiveDefinition (itl,_,paramsno,_) ->
1173 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl
1175 let (_,_,_,cl) = List.nth itl i in
1177 (fun (_,x) (i,ugraph) ->
1179 is_small ~logger tys paramsno x ugraph
1184 raise (TypeCheckerFailure
1185 (lazy ("Unknown mutual inductive definition:" ^
1186 UriManager.string_of_uri uri)))
1188 | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
1189 | (_,_) -> false,ugraph
1191 check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
1193 and type_of_branch ~subst context argsno need_dummy outtype term constype =
1194 let module C = Cic in
1195 let module R = CicReduction in
1196 match R.whd ~subst context constype with
1201 C.Appl [outtype ; term]
1202 | C.Appl (C.MutInd (_,_,_)::tl) ->
1203 let (_,arguments) = split tl argsno
1205 if need_dummy && arguments = [] then
1208 C.Appl (outtype::arguments@(if need_dummy then [] else [term]))
1209 | C.Prod (name,so,de) ->
1211 match CicSubstitution.lift 1 term with
1212 C.Appl l -> C.Appl (l@[C.Rel 1])
1213 | t -> C.Appl [t ; C.Rel 1]
1215 C.Prod (name,so,type_of_branch ~subst
1216 ((Some (name,(C.Decl so)))::context) argsno need_dummy
1217 (CicSubstitution.lift 1 outtype) term' de)
1218 | _ -> raise (AssertFailure (lazy "20"))
1220 (* check_metasenv_consistency checks that the "canonical" context of a
1221 metavariable is consitent - up to relocation via the relocation list l -
1222 with the actual context *)
1225 and check_metasenv_consistency ~logger ~subst metasenv context
1226 canonical_context l ugraph
1228 let module C = Cic in
1229 let module R = CicReduction in
1230 let module S = CicSubstitution in
1231 let lifted_canonical_context =
1235 | (Some (n,C.Decl t))::tl ->
1236 (Some (n,C.Decl (S.subst_meta l (S.lift i t))))::(aux (i+1) tl)
1237 | None::tl -> None::(aux (i+1) tl)
1238 | (Some (n,C.Def (t,ty)))::tl ->
1239 (Some (n,C.Def ((S.subst_meta l (S.lift i t)),S.subst_meta l (S.lift i ty))))::(aux (i+1) tl)
1241 aux 1 canonical_context
1247 | Some t,Some (_,C.Def (ct,_)) ->
1248 (*CSC: the following optimization is to avoid a possibly expensive
1249 reduction that can be easily avoided and that is quite
1250 frequent. However, this is better handled using levels to
1251 control reduction *)
1256 match List.nth context (n - 1) with
1257 Some (_,C.Def (te,_)) -> S.lift n te
1263 (*if t <> optimized_t && optimized_t = ct then prerr_endline "!!!!!!!!!!!!!!!"
1264 else if t <> optimized_t then prerr_endline ("@@ " ^ CicPp.ppterm t ^ " ==> " ^ CicPp.ppterm optimized_t ^ " <==> " ^ CicPp.ppterm ct);*)
1266 R.are_convertible ~subst ~metasenv context optimized_t ct ugraph
1271 (lazy (sprintf "Not well typed metavariable local context: expected a term convertible with %s, found %s" (CicPp.ppterm ct) (CicPp.ppterm t))))
1274 | Some t,Some (_,C.Decl ct) ->
1275 let type_t,ugraph1 =
1276 type_of_aux' ~logger ~subst metasenv context t ugraph
1279 R.are_convertible ~subst ~metasenv context type_t ct ugraph1
1282 raise (TypeCheckerFailure
1283 (lazy (sprintf "Not well typed metavariable local context: expected a term of type %s, found %s of type %s"
1284 (CicPp.ppterm ct) (CicPp.ppterm t)
1285 (CicPp.ppterm type_t))))
1289 raise (TypeCheckerFailure
1290 (lazy ("Not well typed metavariable local context: "^
1291 "an hypothesis, that is not hidden, is not instantiated")))
1292 ) ugraph l lifted_canonical_context
1296 type_of_aux' is just another name (with a different scope)
1300 and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
1301 let rec type_of_aux ~logger context t ugraph =
1302 let module C = Cic in
1303 let module R = CicReduction in
1304 let module S = CicSubstitution in
1305 let module U = UriManager in
1309 match List.nth context (n - 1) with
1310 Some (_,C.Decl t) -> S.lift n t,ugraph
1311 | Some (_,C.Def (_,ty)) -> S.lift n ty,ugraph
1313 (TypeCheckerFailure (lazy "Reference to deleted hypothesis"))
1316 raise (TypeCheckerFailure (lazy "unbound variable"))
1318 | C.Var (uri,exp_named_subst) ->
1321 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1323 let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
1324 let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
1329 let (canonical_context,term,ty) = CicUtil.lookup_subst n subst in
1331 check_metasenv_consistency ~logger
1332 ~subst metasenv context canonical_context l ugraph
1334 (* assuming subst is well typed !!!!! *)
1335 ((CicSubstitution.subst_meta l ty), ugraph1)
1336 (* type_of_aux context (CicSubstitution.subst_meta l term) *)
1337 with CicUtil.Subst_not_found _ ->
1338 let (_,canonical_context,ty) = CicUtil.lookup_meta n metasenv in
1340 check_metasenv_consistency ~logger
1341 ~subst metasenv context canonical_context l ugraph
1343 ((CicSubstitution.subst_meta l ty),ugraph1))
1344 (* TASSI: CONSTRAINTS *)
1345 | C.Sort (C.Type t) ->
1346 let t' = CicUniv.fresh() in
1348 let ugraph1 = CicUniv.add_gt t' t ugraph in
1349 (C.Sort (C.Type t')),ugraph1
1351 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
1352 | C.Sort s -> (C.Sort (C.Type (CicUniv.fresh ()))),ugraph
1353 | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
1354 | C.Cast (te,ty) as t ->
1355 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1356 let ty_te,ugraph2 = type_of_aux ~logger context te ugraph1 in
1358 R.are_convertible ~subst ~metasenv context ty_te ty ugraph2
1363 raise (TypeCheckerFailure
1364 (lazy (sprintf "Invalid cast %s" (CicPp.ppterm t))))
1365 | C.Prod (name,s,t) ->
1366 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1368 type_of_aux ~logger ((Some (name,(C.Decl s)))::context) t ugraph1
1370 sort_of_prod ~subst context (name,s) (sort1,sort2) ugraph2
1371 | C.Lambda (n,s,t) ->
1372 let sort1,ugraph1 = type_of_aux ~logger context s ugraph in
1373 (match R.whd ~subst context sort1 with
1378 (TypeCheckerFailure (lazy (sprintf
1379 "Not well-typed lambda-abstraction: the source %s should be a type; instead it is a term of type %s" (CicPp.ppterm s)
1380 (CicPp.ppterm sort1))))
1383 type_of_aux ~logger ((Some (n,(C.Decl s)))::context) t ugraph1
1385 (C.Prod (n,s,type2)),ugraph2
1386 | C.LetIn (n,s,ty,t) ->
1387 (* only to check if s is well-typed *)
1388 let ty',ugraph1 = type_of_aux ~logger context s ugraph in
1389 let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in
1391 R.are_convertible ~subst ~metasenv context ty ty' ugraph1
1397 "The type of %s is %s but it is expected to be %s"
1398 (CicPp.ppterm s) (CicPp.ppterm ty') (CicPp.ppterm ty))))
1400 (* The type of a LetIn is a LetIn. Extremely slow since the computed
1401 LetIn is later reduced and maybe also re-checked.
1402 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t))
1404 (* The type of the LetIn is reduced. Much faster than the previous
1405 solution. Moreover the inferred type is probably very different
1406 from the expected one.
1407 (CicReduction.whd ~subst context
1408 (C.LetIn (n,s, type_of_aux ((Some (n,(C.Def s)))::context) t)))
1410 (* One-step LetIn reduction. Even faster than the previous solution.
1411 Moreover the inferred type is closer to the expected one. *)
1414 ((Some (n,(C.Def (s,ty))))::context) t ugraph1
1416 (CicSubstitution.subst ~avoid_beta_redexes:true s ty1),ugraph2
1417 | C.Appl (he::tl) when List.length tl > 0 ->
1418 let hetype,ugraph1 = type_of_aux ~logger context he ugraph in
1419 let tlbody_and_type,ugraph2 =
1422 let ty,ugraph1 = type_of_aux ~logger context x ugraph in
1423 (*let _,ugraph1 = type_of_aux ~logger context ty ugraph1 in*)
1424 ((x,ty)::l,ugraph1))
1427 (* TASSI: questa c'era nel mio... ma non nel CVS... *)
1428 (* let _,ugraph2 = type_of_aux context hetype ugraph2 in *)
1429 eat_prods ~subst context hetype tlbody_and_type ugraph2
1430 | C.Appl _ -> raise (AssertFailure (lazy "Appl: no arguments"))
1431 | C.Const (uri,exp_named_subst) ->
1434 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1436 let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
1438 CicSubstitution.subst_vars exp_named_subst cty
1442 | C.MutInd (uri,i,exp_named_subst) ->
1445 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1447 (* TASSI: da me c'era anche questa, ma in CVS no *)
1448 let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
1449 (* fine parte dubbia *)
1451 CicSubstitution.subst_vars exp_named_subst mty
1455 | C.MutConstruct (uri,i,j,exp_named_subst) ->
1457 check_exp_named_subst ~logger ~subst context exp_named_subst ugraph
1459 (* TASSI: idem come sopra *)
1461 type_of_mutual_inductive_constr ~logger uri i j ugraph1
1464 CicSubstitution.subst_vars exp_named_subst mty
1467 | C.MutCase (uri,i,outtype,term,pl) ->
1468 let outsort,ugraph1 = type_of_aux ~logger context outtype ugraph in
1469 let (need_dummy, k) =
1470 let rec guess_args context t =
1471 let outtype = CicReduction.whd ~subst context t in
1473 C.Sort _ -> (true, 0)
1474 | C.Prod (name, s, t) ->
1476 guess_args ((Some (name,(C.Decl s)))::context) t in
1478 (* last prod before sort *)
1479 match CicReduction.whd ~subst context s with
1480 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1481 C.MutInd (uri',i',_) when U.eq uri' uri && i' = i ->
1483 (*CSC: for _ see comment below about the missing named_exp_subst ?????????? *)
1484 | C.Appl ((C.MutInd (uri',i',_)) :: _)
1485 when U.eq uri' uri && i' = i -> (false, 1)
1493 "Malformed case analasys' output type %s"
1494 (CicPp.ppterm outtype))))
1497 let (parameters, arguments, exp_named_subst),ugraph2 =
1498 let ty,ugraph2 = type_of_aux context term ugraph1 in
1499 match R.whd ~subst context ty with
1500 (*CSC manca il caso dei CAST *)
1501 (*CSC: ma servono i parametri (uri,i)? Se si', perche' non serve anche il *)
1502 (*CSC: parametro exp_named_subst? Se no, perche' non li togliamo? *)
1503 (*CSC: Hint: nella DTD servono per gli stylesheet. *)
1504 C.MutInd (uri',i',exp_named_subst) as typ ->
1505 if U.eq uri uri' && i = i' then
1506 ([],[],exp_named_subst),ugraph2
1511 ("Case analysys: analysed term type is %s, but is expected to be (an application of) %s#1/%d{_}")
1512 (CicPp.ppterm typ) (U.string_of_uri uri) i)))
1514 ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) as typ' ->
1515 if U.eq uri uri' && i = i' then
1517 split tl (List.length tl - k)
1518 in (params,args,exp_named_subst),ugraph2
1523 ("Case analysys: analysed term type is %s, "^
1524 "but is expected to be (an application of) "^
1526 (CicPp.ppterm typ') (U.string_of_uri uri) i)))
1532 "analysed term %s is not an inductive one")
1533 (CicPp.ppterm term))))
1535 let (b, k) = guess_args context outsort in
1536 if not b then (b, k - 1) else (b, k) in
1537 let (parameters, arguments, exp_named_subst),ugraph2 =
1538 let ty,ugraph2 = type_of_aux ~logger context term ugraph1 in
1539 match R.whd ~subst context ty with
1540 C.MutInd (uri',i',exp_named_subst) as typ ->
1541 if U.eq uri uri' && i = i' then
1542 ([],[],exp_named_subst),ugraph2
1546 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1547 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1548 | C.Appl ((C.MutInd (uri',i',exp_named_subst) as typ):: tl) ->
1549 if U.eq uri uri' && i = i' then
1551 split tl (List.length tl - k)
1552 in (params,args,exp_named_subst),ugraph2
1556 ("Case analysys: analysed term type is %s (%s#1/%d{_}), but is expected to be (an application of) %s#1/%d{_}")
1557 (CicPp.ppterm typ) (U.string_of_uri uri') i' (U.string_of_uri uri) i)))
1562 "Case analysis: analysed term %s is not an inductive one"
1563 (CicPp.ppterm term))))
1566 let's control if the sort elimination is allowed:
1569 let sort_of_ind_type =
1570 if parameters = [] then
1571 C.MutInd (uri,i,exp_named_subst)
1573 C.Appl ((C.MutInd (uri,i,exp_named_subst))::parameters)
1575 let type_of_sort_of_ind_ty,ugraph3 =
1576 type_of_aux ~logger context sort_of_ind_type ugraph2 in
1578 check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
1579 need_dummy sort_of_ind_type type_of_sort_of_ind_ty outsort ugraph3
1583 (TypeCheckerFailure (lazy ("Case analysis: sort elimination not allowed")));
1584 (* let's check if the type of branches are right *)
1585 let parsno,constructorsno =
1588 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1589 with Not_found -> assert false
1592 C.InductiveDefinition (il,_,parsno,_) ->
1594 try List.nth il i with Failure _ -> assert false
1596 parsno, List.length cl
1598 raise (TypeCheckerFailure
1599 (lazy ("Unknown mutual inductive definition:" ^
1600 UriManager.string_of_uri uri)))
1602 if List.length pl <> constructorsno then
1603 raise (TypeCheckerFailure
1604 (lazy ("Wrong number of cases in case analysis"))) ;
1605 let (_,branches_ok,ugraph5) =
1607 (fun (j,b,ugraph) p ->
1610 if parameters = [] then
1611 (C.MutConstruct (uri,i,j,exp_named_subst))
1614 (C.MutConstruct (uri,i,j,exp_named_subst)::parameters))
1616 let ty_p,ugraph1 = type_of_aux ~logger context p ugraph in
1617 let ty_cons,ugraph3 = type_of_aux ~logger context cons ugraph1 in
1620 type_of_branch ~subst context parsno need_dummy outtype cons
1624 ~subst ~metasenv context ty_p ty_branch ugraph3
1629 prerr_endline ("\n!OUTTYPE= " ^ CicPp.ppterm outtype);
1630 prerr_endline ("!CONS= " ^ CicPp.ppterm cons);
1631 prerr_endline ("!TY_CONS= " ^ CicPp.ppterm ty_cons);
1632 prerr_endline ("#### " ^ CicPp.ppterm ty_p ^ "\n<==>\n" ^ CicPp.ppterm ty_branch);
1637 ("#### " ^ CicPp.ppterm ty_p ^
1638 " <==> " ^ CicPp.ppterm ty_branch));
1642 ) (1,true,ugraph4) pl
1644 if not branches_ok then
1646 (TypeCheckerFailure (lazy "Case analysys: wrong branch type"));
1648 if not need_dummy then outtype::arguments@[term]
1649 else outtype::arguments in
1651 if need_dummy && arguments = [] then outtype
1652 else CicReduction.head_beta_reduce (C.Appl arguments')
1656 let types,kl,ugraph1,len =
1658 (fun (types,kl,ugraph,len) (n,k,ty,_) ->
1659 let _,ugraph1 = type_of_aux ~logger context ty ugraph in
1660 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types,
1661 k::kl,ugraph1,len+1)
1662 ) ([],[],ugraph,0) fl
1666 (fun ugraph (name,x,ty,bo) ->
1668 type_of_aux ~logger (types@context) bo ugraph
1671 R.are_convertible ~subst ~metasenv (types@context)
1672 ty_bo (CicSubstitution.lift len ty) ugraph1 in
1675 let (m, eaten, context') =
1676 eat_lambdas ~subst (types @ context) (x + 1) bo
1678 let rec_uri, rec_uri_len =
1680 match List.hd context' with
1681 Some (_,Cic.Decl he) -> he
1684 match CicReduction.whd ~subst (List.tl context') he with
1685 | Cic.MutInd (uri,_,_)
1686 | Cic.Appl (Cic.MutInd (uri,_,_)::_) ->
1689 CicEnvironment.get_obj
1690 CicUniv.oblivion_ugraph uri
1692 | Cic.InductiveDefinition (tl,_,_,_), _ ->
1694 | _ -> assert false)
1698 let's control the guarded by
1699 destructors conditions D{f,k,x,M}
1701 if not (guarded_by_destructors ~logger ~metasenv ~subst
1702 rec_uri rec_uri_len context' eaten (len + eaten) kl
1707 (lazy ("Fix: not guarded by destructors:"^CicPp.ppterm t)))
1712 raise (TypeCheckerFailure (lazy ("Fix: ill-typed bodies")))
1714 (*CSC: controlli mancanti solo su D{f,k,x,M} *)
1715 let (_,_,ty,_) = List.nth fl i in
1718 let types,ugraph1,len =
1720 (fun (l,ugraph,len) (n,ty,_) ->
1722 type_of_aux ~logger context ty ugraph in
1723 (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::l,
1729 (fun ugraph (_,ty,bo) ->
1731 type_of_aux ~logger (types @ context) bo ugraph
1734 R.are_convertible ~subst ~metasenv (types @ context) ty_bo
1735 (CicSubstitution.lift len ty) ugraph1
1739 (* let's control that the returned type is coinductive *)
1740 match returns_a_coinductive ~subst context ty with
1744 (lazy "CoFix: does not return a coinductive type"))
1747 let's control the guarded by constructors
1750 if not (guarded_by_constructors ~logger ~subst ~metasenv uri
1751 (types @ context) 0 len false bo) then
1754 (lazy "CoFix: not guarded by constructors"))
1760 (TypeCheckerFailure (lazy "CoFix: ill-typed bodies"))
1763 let (_,ty,_) = List.nth fl i in
1766 and check_exp_named_subst ~logger ~subst context =
1767 let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
1770 | ((uri,t) as item)::tl ->
1771 let ty_uri,ugraph1 = type_of_variable ~logger uri ugraph in
1773 CicSubstitution.subst_vars esubsts ty_uri in
1774 let typeoft,ugraph2 = type_of_aux ~logger context t ugraph1 in
1776 CicReduction.are_convertible ~subst ~metasenv
1777 context typeoft typeofvar ugraph2
1780 check_exp_named_subst_aux ~logger (esubsts@[item]) tl ugraph3
1783 CicReduction.fdebug := 0 ;
1785 (CicReduction.are_convertible
1786 ~subst ~metasenv context typeoft typeofvar ugraph2) ;
1788 debug typeoft [typeofvar] ;
1789 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
1792 check_exp_named_subst_aux ~logger []
1794 and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
1795 let module C = Cic in
1796 let t1' = CicReduction.whd ~subst context t1 in
1797 let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
1798 match (t1', t2') with
1799 (C.Sort s1, C.Sort s2)
1800 when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) ->
1801 (* different from Coq manual!!! *)
1803 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
1804 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1805 let t' = CicUniv.fresh() in
1807 let ugraph1 = CicUniv.add_ge t' t1 ugraph in
1808 let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
1809 C.Sort (C.Type t'),ugraph2
1811 CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
1812 | (C.Sort _,C.Sort (C.Type t1)) ->
1813 (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
1814 C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
1815 | (C.Meta _, C.Sort _) -> t2',ugraph
1816 | (C.Meta _, (C.Meta (_,_) as t))
1817 | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
1819 | (_,_) -> raise (TypeCheckerFailure (lazy (sprintf
1820 "Prod: expected two sorts, found = %s, %s" (CicPp.ppterm t1')
1821 (CicPp.ppterm t2'))))
1823 and eat_prods ~subst context hetype l ugraph =
1824 (*CSC: siamo sicuri che le are_convertible non lavorino con termini non *)
1828 | (hete, hety)::tl ->
1829 (match (CicReduction.whd ~subst context hetype) with
1832 (*if (match hety,s with Cic.Sort _,Cic.Sort _ -> false | _,_ -> true) && hety <> s then(
1833 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*)
1834 CicReduction.are_convertible
1835 ~subst ~metasenv context hety s ugraph
1839 CicReduction.fdebug := -1 ;
1840 eat_prods ~subst context
1841 (CicSubstitution.subst ~avoid_beta_redexes:true hete t)
1843 (*TASSI: not sure *)
1847 CicReduction.fdebug := 0 ;
1848 ignore (CicReduction.are_convertible
1849 ~subst ~metasenv context s hety ugraph) ;
1855 ("Appl: wrong parameter-type, expected %s, found %s")
1856 (CicPp.ppterm hetype) (CicPp.ppterm s))))
1859 raise (TypeCheckerFailure
1860 (lazy "Appl: this is not a function, it cannot be applied"))
1863 and returns_a_coinductive ~subst context ty =
1864 let module C = Cic in
1865 match CicReduction.whd ~subst context ty with
1866 C.MutInd (uri,i,_) ->
1867 (*CSC: definire una funzioncina per questo codice sempre replicato *)
1870 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
1871 with Not_found -> assert false
1874 C.InductiveDefinition (itl,_,_,_) ->
1875 let (_,is_inductive,_,_) = List.nth itl i in
1876 if is_inductive then None else (Some uri)
1878 raise (TypeCheckerFailure
1879 (lazy ("Unknown mutual inductive definition:" ^
1880 UriManager.string_of_uri uri)))
1882 | C.Appl ((C.MutInd (uri,i,_))::_) ->
1883 (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
1885 C.InductiveDefinition (itl,_,_,_) ->
1886 let (_,is_inductive,_,_) = List.nth itl i in
1887 if is_inductive then None else (Some uri)
1889 raise (TypeCheckerFailure
1890 (lazy ("Unknown mutual inductive definition:" ^
1891 UriManager.string_of_uri uri)))
1893 | C.Prod (n,so,de) ->
1894 returns_a_coinductive ~subst ((Some (n,C.Decl so))::context) de
1899 debug_print (lazy ("INIZIO TYPE_OF_AUX " ^ CicPp.ppterm t)) ; flush stderr ;
1902 type_of_aux ~logger context t ugraph
1904 in debug_print (lazy "FINE TYPE_OF_AUX") ; flush stderr ; res
1907 (* is a small constructor? *)
1908 (*CSC: ottimizzare calcolando staticamente *)
1909 and is_small_or_non_informative ~condition ~logger context paramsno c ugraph =
1910 let rec is_small_or_non_informative_aux ~logger context c ugraph =
1911 let module C = Cic in
1912 match CicReduction.whd context c with
1914 let s,ugraph1 = type_of_aux' ~logger [] context so ugraph in
1915 let b = condition s in
1917 is_small_or_non_informative_aux
1918 ~logger ((Some (n,(C.Decl so)))::context) de ugraph1
1921 | _ -> true,ugraph (*CSC: we trust the type-checker *)
1923 let (context',dx) = split_prods ~subst:[] context paramsno c in
1924 is_small_or_non_informative_aux ~logger context' dx ugraph
1926 and is_small ~logger =
1927 is_small_or_non_informative
1928 ~condition:(fun s -> s=Cic.Sort Cic.Prop || s=Cic.Sort Cic.Set)
1931 and is_non_informative ~logger =
1932 is_small_or_non_informative
1933 ~condition:(fun s -> s=Cic.Sort Cic.Prop)
1936 and type_of ~logger t ugraph =
1938 debug_print (lazy ("INIZIO TYPE_OF_AUX' " ^ CicPp.ppterm t)) ; flush stderr ;
1941 type_of_aux' ~logger [] [] t ugraph
1943 in debug_print (lazy "FINE TYPE_OF_AUX'") ; flush stderr ; res
1947 let typecheck_obj0 ~logger uri (obj,unchecked_ugraph) =
1948 let module C = Cic in
1949 let ugraph = CicUniv.empty_ugraph in
1950 let inferred_ugraph =
1952 | C.Constant (_,Some te,ty,_,_) ->
1953 let _,ugraph = type_of ~logger ty ugraph in
1954 let ty_te,ugraph = type_of ~logger te ugraph in
1955 let b,ugraph = (CicReduction.are_convertible [] ty_te ty ugraph) in
1957 raise (TypeCheckerFailure
1959 ("the type of the body is not the one expected:\n" ^
1960 CicPp.ppterm ty_te ^ "\nvs\n" ^
1964 | C.Constant (_,None,ty,_,_) ->
1965 (* only to check that ty is well-typed *)
1966 let _,ugraph = type_of ~logger ty ugraph in
1968 | C.CurrentProof (_,conjs,te,ty,_,_) ->
1969 (* this block is broken since the metasenv should
1970 * be topologically sorted before typing metas *)
1971 ignore(assert false);
1974 (fun (metasenv,ugraph) ((_,context,ty) as conj) ->
1976 type_of_aux' ~logger metasenv context ty ugraph
1978 metasenv @ [conj],ugraph
1981 let _,ugraph = type_of_aux' ~logger conjs [] ty ugraph in
1982 let type_of_te,ugraph =
1983 type_of_aux' ~logger conjs [] te ugraph
1985 let b,ugraph = CicReduction.are_convertible [] type_of_te ty ugraph in
1987 raise (TypeCheckerFailure (lazy (sprintf
1988 "the current proof is not well typed because the type %s of the body is not convertible to the declared type %s"
1989 (CicPp.ppterm type_of_te) (CicPp.ppterm ty))))
1992 | C.Variable (_,bo,ty,_,_) ->
1993 (* only to check that ty is well-typed *)
1994 let _,ugraph = type_of ~logger ty ugraph in
1998 let ty_bo,ugraph = type_of ~logger bo ugraph in
1999 let b,ugraph = CicReduction.are_convertible [] ty_bo ty ugraph in
2001 raise (TypeCheckerFailure
2002 (lazy "the body is not the one expected"))
2006 | (C.InductiveDefinition _ as obj) ->
2007 check_mutual_inductive_defs ~logger uri obj ugraph
2009 check_and_clean_ugraph inferred_ugraph unchecked_ugraph uri obj
2013 let module C = Cic in
2014 let module R = CicReduction in
2015 let module U = UriManager in
2016 let logger = new CicLogger.logger in
2017 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2018 | CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2019 | CicEnvironment.UncheckedObj (uobj,unchecked_ugraph) ->
2020 (* let's typecheck the uncooked object *)
2021 logger#log (`Start_type_checking uri) ;
2022 let ugraph, ul, obj = typecheck_obj0 ~logger uri (uobj,unchecked_ugraph) in
2023 CicEnvironment.set_type_checking_info uri (obj,ugraph,ul);
2024 logger#log (`Type_checking_completed uri);
2025 match CicEnvironment.is_type_checked ~trust:false CicUniv.empty_ugraph uri with
2026 | CicEnvironment.CheckedObj (cobj,ugraph') -> cobj,ugraph'
2027 | _ -> raise CicEnvironmentError
2030 let typecheck_obj ~logger uri obj =
2031 let ugraph,univlist,obj = typecheck_obj0 ~logger uri (obj,None) in
2032 CicEnvironment.add_type_checked_obj uri (obj,ugraph,univlist)
2034 (** wrappers which instantiate fresh loggers *)
2036 let profiler = HExtlib.profile "K/CicTypeChecker.type_of_aux'"
2038 let type_of_aux' ?(subst = []) metasenv context t ugraph =
2039 let logger = new CicLogger.logger in
2040 profiler.HExtlib.profile
2041 (type_of_aux' ~logger ~subst metasenv context t) ugraph
2043 let typecheck_obj uri obj =
2044 let logger = new CicLogger.logger in
2045 typecheck_obj ~logger uri obj
2047 (* check_allowed_sort_elimination uri i s1 s2
2048 This function is used outside the kernel to determine in advance whether
2049 a MutCase will be allowed or not.
2050 [uri,i] is the type of the term to match
2051 [s1] is the sort of the term to eliminate (i.e. the head of the arity
2052 of the inductive type [uri,i])
2053 [s2] is the sort of the goal (i.e. the head of the type of the outtype
2055 let check_allowed_sort_elimination uri i s1 s2 =
2056 fst (check_allowed_sort_elimination ~subst:[] ~metasenv:[]
2057 ~logger:(new CicLogger.logger) [] uri i true
2058 (Cic.Implicit None) (* never used *) (Cic.Sort s1) (Cic.Sort s2)
2059 CicUniv.empty_ugraph)
2062 Deannotate.type_of_aux' :=
2069 | Some (_,Cic.Decl ty) ->
2070 ignore (type_of_aux' [] context ty CicUniv.empty_ugraph)
2071 | Some (_,Cic.Def (bo,ty)) ->
2072 ignore (type_of_aux' [] context ty CicUniv.empty_ugraph);
2073 ignore (type_of_aux' [] context bo CicUniv.empty_ugraph));
2076 fst (type_of_aux' [] context t CicUniv.empty_ugraph);;