2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
15 module Ref = NReference
16 module R = NCicReduction
17 module S = NCicSubstitution
19 module E = NCicEnvironment
22 exception TypeCheckerFailure of string Lazy.t
23 exception AssertFailure of string Lazy.t
27 | TypeCheckerFailure s as e -> prerr_endline (Lazy.force s); raise e
33 | Evil of int (* rno *)
34 | UnfFix of bool list (* fixed arguments *)
38 let is_dangerous i l =
39 List.exists (function (j,Evil _) when j=i -> true | _ -> false) l
43 List.exists (function (j,UnfFix _) when j=i -> true | _ -> false) l
47 List.exists (function (j,Safe) when j=i -> true | _ -> false) l
51 try match List.assoc i l with Evil rno -> rno | _ -> assert false
52 with Not_found -> assert false
55 let get_fixed_args i l =
56 try match List.assoc i l with UnfFix fa -> fa | _ -> assert false
57 with Not_found -> assert false
60 let shift_k e (c,rf,x) = e::c,List.map (fun (k,v) -> k+1,v) rf,x+1;;
63 let string_of_recfuns ~subst ~metasenv ~context l =
64 let pp = PP.ppterm ~subst ~metasenv ~context in
65 let safe, rest = List.partition (function (_,Safe) -> true | _ -> false) l in
66 let dang,unf = List.partition (function (_,UnfFix _)-> false | _->true)rest in
67 "\n\tsafes: "^String.concat "," (List.map (fun (i,_)->pp (C.Rel i)) safe) ^
68 "\n\tfix : "^String.concat ","
70 (function (i,UnfFix l)-> pp(C.Rel i)^"/"^String.concat "," (List.map
72 | _ ->assert false) unf) ^
73 "\n\trec : "^String.concat ","
75 (function (i,Evil rno)->pp(C.Rel i)^"/"^string_of_int rno
76 | _ -> assert false) dang)
80 let fixed_args bos j n nn =
81 let rec aux k acc = function
82 | C.Appl (C.Rel i::args) when i-k > n && i-k <= nn ->
83 let rec combine l1 l2 =
86 | he1::tl1, he2::tl2 -> (he1,he2)::combine tl1 tl2
87 | _::tl, [] -> (false,C.Rel ~-1)::combine tl [] (* dummy term *)
88 | [],_::_ -> assert false
90 let lefts, _ = HExtlib.split_nth (min j (List.length args)) args in
91 List.map (fun ((b,x),i) -> b && x = C.Rel (k-i))
92 (HExtlib.list_mapi (fun x i -> x,i) (combine acc lefts))
93 | t -> U.fold (fun _ k -> k+1) k aux acc t
95 List.fold_left (aux 0)
96 (let rec f = function 0 -> [] | n -> true :: f (n-1) in f j) bos
99 (* if n < 0, then splits all prods from an arity, returning a sort *)
100 let rec split_prods ~subst context n te =
101 match (n, R.whd ~subst context te) with
102 | (0, _) -> context,te
103 | (n, C.Sort _) when n <= 0 -> context,te
104 | (n, C.Prod (name,so,ta)) ->
105 split_prods ~subst ((name,(C.Decl so))::context) (n - 1) ta
106 | (_, _) -> raise (AssertFailure (lazy "split_prods"))
109 let debruijn uri number_of_types context =
112 | C.Meta (i,(s,C.Ctx l)) ->
113 let l1 = HExtlib.sharing_map (aux (k-s)) l in
114 if l1 == l then t else C.Meta (i,(s,C.Ctx l1))
116 | C.Const (Ref.Ref (uri1,(Ref.Fix (no,_,_) | Ref.CoFix no)))
117 | C.Const (Ref.Ref (uri1,Ref.Ind (_,no,_))) when NUri.eq uri uri1 ->
118 C.Rel (k + number_of_types - no)
119 | t -> U.map (fun _ k -> k+1) k aux t
121 aux (List.length context)
124 let sort_of_prod ~metasenv ~subst context (name,s) (t1, t2) =
125 let t1 = R.whd ~subst context t1 in
126 let t2 = R.whd ~subst ((name,C.Decl s)::context) t2 in
128 | C.Sort _, C.Sort C.Prop -> t2
129 | C.Sort (C.Type u1), C.Sort (C.Type u2) -> C.Sort (C.Type (u1@u2))
130 | C.Sort C.Prop,C.Sort (C.Type _) -> t2
131 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))), C.Sort _
132 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))), C.Meta (_,(_,(C.Irl 0 | C.Ctx [])))
133 | C.Sort _, C.Meta (_,(_,(C.Irl 0 | C.Ctx []))) -> t2
135 raise (TypeCheckerFailure (lazy (Printf.sprintf
136 "Prod: expected two sorts, found = %s, %s"
137 (PP.ppterm ~subst ~metasenv ~context t1)
138 (PP.ppterm ~subst ~metasenv ~context t2))))
141 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
142 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
143 let rec instantiate_parameters params c =
146 | C.Prod (_,_,ta), he::tl -> instantiate_parameters tl (S.subst he ta)
147 | _,_ -> raise (AssertFailure (lazy "1"))
150 let specialize_inductive_type_constrs ~subst context ty_term =
151 match R.whd ~subst context ty_term with
152 | C.Const (Ref.Ref (_,Ref.Ind _) as ref)
153 | C.Appl (C.Const (Ref.Ref (_,Ref.Ind _) as ref) :: _ ) as ty ->
154 let args = match ty with C.Appl (_::tl) -> tl | _ -> [] in
155 let _, leftno, itl, _, i = E.get_checked_indtys ref in
156 let left_args,_ = HExtlib.split_nth leftno args in
157 let _,_,_,cl = List.nth itl i in
159 (fun (rel,name,ty) -> rel, name, instantiate_parameters left_args ty) cl
163 let specialize_and_abstract_constrs ~subst r_uri r_len context ty_term =
164 let cl = specialize_inductive_type_constrs ~subst context ty_term in
165 let len = List.length context in
167 match E.get_checked_obj r_uri with
168 | _,_,_,_, C.Inductive (_,_,tys,_) ->
169 context @ List.map (fun (_,name,arity,_) -> name,C.Decl arity) tys
173 List.map (fun (_,id,ty) -> id, debruijn r_uri r_len context ty) cl,
177 exception DoesOccur;;
179 let does_not_occur ~subst context n nn t =
180 let rec aux k _ = function
181 | C.Rel m when m > n+k && m <= nn+k -> raise DoesOccur
182 | C.Rel m when m <= k || m > nn+k -> ()
184 (try match List.nth context (m-1-k) with
185 | _,C.Def (bo,_) -> aux (n-m) () bo
187 with Failure _ -> assert false)
188 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))) -> (* closed meta *) ()
189 | C.Meta (mno,(s,l)) ->
191 (* possible optimization here: try does_not_occur on l and
192 perform substitution only if DoesOccur is raised *)
193 let _,_,term,_ = U.lookup_subst mno subst in
194 aux (k-s) () (S.subst_meta (0,l) term)
195 with U.Subst_not_found _ -> match l with
196 | C.Irl len -> if not (n+k >= s+len || s > nn+k) then raise DoesOccur
197 | C.Ctx lc -> List.iter (aux (k-s) ()) lc)
198 | t -> U.fold (fun _ k -> k + 1) k aux () t
201 with DoesOccur -> false
204 let rec eat_lambdas ~subst ~metasenv context n te =
205 match (n, R.whd ~subst context te) with
206 | (0, _) -> (te, context)
207 | (n, C.Lambda (name,so,ta)) when n > 0 ->
208 eat_lambdas ~subst ~metasenv ((name,(C.Decl so))::context) (n - 1) ta
210 raise (AssertFailure (lazy (Printf.sprintf "eat_lambdas (%d, %s)" n
211 (PP.ppterm ~subst ~metasenv ~context te))))
214 let rec eat_or_subst_lambdas
215 ~subst ~metasenv n te to_be_subst args (context,_,_ as k)
217 match n, R.whd ~subst context te, to_be_subst, args with
218 | (n, C.Lambda (_,_,ta),true::to_be_subst,arg::args) when n > 0 ->
219 eat_or_subst_lambdas ~subst ~metasenv (n - 1) (S.subst arg ta)
221 | (n, C.Lambda (name,so,ta),false::to_be_subst,_::args) when n > 0 ->
222 eat_or_subst_lambdas ~subst ~metasenv (n - 1) ta to_be_subst args
223 (shift_k (name,(C.Decl so)) k)
224 | (_, te, _, _) -> te, k
227 let check_homogeneous_call ~subst context indparamsno n uri reduct tl =
233 match R.whd context x with
234 | C.Rel m when m = n - (indparamsno - k) -> k - 1
235 | _ -> raise (TypeCheckerFailure (lazy
236 ("Argument "^string_of_int (indparamsno - k + 1) ^ " (of " ^
237 string_of_int indparamsno ^ " fixed) is not homogeneous in "^
238 "appl:\n"^ PP.ppterm ~context ~subst ~metasenv:[] reduct))))
242 raise (TypeCheckerFailure
243 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
244 NUri.string_of_uri uri)))
247 (* Inductive types being checked for positivity have *)
248 (* indexes x s.t. n < x <= nn. *)
249 let rec weakly_positive ~subst context n nn uri indparamsno posuri te =
250 (*CSC: Not very nice. *)
251 let dummy = C.Sort C.Prop in
252 (*CSC: to be moved in cicSubstitution? *)
253 let rec subst_inductive_type_with_dummy _ = function
254 | C.Const (Ref.Ref (uri',Ref.Ind (true,0,_))) when NUri.eq uri' uri -> dummy
255 | C.Appl ((C.Const (Ref.Ref (uri',Ref.Ind (true,0,lno))))::tl)
256 when NUri.eq uri' uri ->
257 let _, rargs = HExtlib.split_nth lno tl in
258 if rargs = [] then dummy else C.Appl (dummy :: rargs)
259 | t -> U.map (fun _ x->x) () subst_inductive_type_with_dummy t
261 (* this function has the same semantics of are_all_occurrences_positive
262 but the i-th context entry role is played by dummy and some checks
263 are skipped because we already know that are_all_occurrences_positive
265 let rec aux context n nn te =
266 match R.whd context te with
267 | t when t = dummy -> true
268 | C.Appl (te::rargs) when te = dummy ->
269 List.for_all (does_not_occur ~subst context n nn) rargs
270 | C.Prod (name,source,dest) when
271 does_not_occur ~subst ((name,C.Decl source)::context) 0 1 dest ->
272 (* dummy abstraction, so we behave as in the anonimous case *)
273 strictly_positive ~subst context n nn indparamsno posuri source &&
274 aux ((name,C.Decl source)::context) (n + 1) (nn + 1) dest
275 | C.Prod (name,source,dest) ->
276 does_not_occur ~subst context n nn source &&
277 aux ((name,C.Decl source)::context) (n + 1) (nn + 1) dest
279 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
281 aux context n nn (subst_inductive_type_with_dummy () te)
283 and strictly_positive ~subst context n nn indparamsno posuri te =
284 match R.whd context te with
285 | t when does_not_occur ~subst context n nn t -> true
286 | C.Rel _ when indparamsno = 0 -> true
287 | C.Appl ((C.Rel m)::tl) as reduct when m > n && m <= nn ->
288 check_homogeneous_call ~subst context indparamsno n posuri reduct tl;
289 List.for_all (does_not_occur ~subst context n nn) tl
290 | C.Prod (name,so,ta) ->
291 does_not_occur ~subst context n nn so &&
292 strictly_positive ~subst ((name,C.Decl so)::context) (n+1) (nn+1)
293 indparamsno posuri ta
294 | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind _) as r)::tl) ->
295 let _,paramsno,tyl,_,i = E.get_checked_indtys r in
296 let _,name,ity,cl = List.nth tyl i in
297 let ok = List.length tyl = 1 in
298 let params, arguments = HExtlib.split_nth paramsno tl in
299 let lifted_params = List.map (S.lift 1) params in
301 List.map (fun (_,_,te) -> instantiate_parameters lifted_params te) cl
304 List.for_all (does_not_occur ~subst context n nn) arguments &&
306 (weakly_positive ~subst ((name,C.Decl ity)::context) (n+1) (nn+1)
307 uri indparamsno posuri) cl
310 (* the inductive type indexes are s.t. n < x <= nn *)
311 and are_all_occurrences_positive ~subst context uri indparamsno i n nn te =
312 match R.whd context te with
313 | C.Appl ((C.Rel m)::tl) as reduct when m = i ->
314 check_homogeneous_call ~subst context indparamsno n uri reduct tl;
315 List.for_all (does_not_occur ~subst context n nn) tl
316 | C.Rel m when m = i ->
317 if indparamsno = 0 then
320 raise (TypeCheckerFailure
321 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
322 NUri.string_of_uri uri)))
323 | C.Prod (name,source,dest) when
324 does_not_occur ~subst ((name,C.Decl source)::context) 0 1 dest ->
325 strictly_positive ~subst context n nn indparamsno uri source &&
326 are_all_occurrences_positive ~subst
327 ((name,C.Decl source)::context) uri indparamsno
328 (i+1) (n + 1) (nn + 1) dest
329 | C.Prod (name,source,dest) ->
330 if not (does_not_occur ~subst context n nn source) then
331 raise (TypeCheckerFailure (lazy ("Non-positive occurrence in "^
332 PP.ppterm ~context ~metasenv:[] ~subst te)));
333 are_all_occurrences_positive ~subst ((name,C.Decl source)::context)
334 uri indparamsno (i+1) (n + 1) (nn + 1) dest
337 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
338 (NUri.string_of_uri uri))))
341 exception NotGuarded of string Lazy.t;;
343 let rec typeof ~subst ~metasenv context term =
344 let rec typeof_aux context =
345 fun t -> (*prerr_endline (PP.ppterm ~metasenv ~subst ~context t);*)
349 match List.nth context (n - 1) with
350 | (_,C.Decl ty) -> S.lift n ty
351 | (_,C.Def (_,ty)) -> S.lift n ty
352 with Failure _ -> raise (TypeCheckerFailure (lazy "unbound variable")))
353 | C.Sort (C.Type [false,u]) -> C.Sort (C.Type [true, u])
354 | C.Sort (C.Type _) ->
355 raise (AssertFailure (lazy ("Cannot type an inferred type: "^
356 NCicPp.ppterm ~subst ~metasenv ~context t)))
357 | C.Sort _ -> C.Sort (C.Type NCicEnvironment.type0)
358 | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
359 | C.Meta (n,l) as t ->
360 let canonical_ctx,ty =
362 let _,c,_,ty = U.lookup_subst n subst in c,ty
363 with U.Subst_not_found _ -> try
364 let _,c,ty = U.lookup_meta n metasenv in c,ty
365 with U.Meta_not_found _ ->
366 raise (AssertFailure (lazy (Printf.sprintf
367 "%s not found" (PP.ppterm ~subst ~metasenv ~context t))))
369 check_metasenv_consistency t ~subst ~metasenv context canonical_ctx l;
371 | C.Const ref -> type_of_constant ref
372 | C.Prod (name,s,t) ->
373 let sort1 = typeof_aux context s in
374 let sort2 = typeof_aux ((name,(C.Decl s))::context) t in
375 sort_of_prod ~metasenv ~subst context (name,s) (sort1,sort2)
376 | C.Lambda (n,s,t) ->
377 let sort = typeof_aux context s in
378 (match R.whd ~subst context sort with
379 | C.Meta _ | C.Sort _ -> ()
382 (TypeCheckerFailure (lazy (Printf.sprintf
383 ("Not well-typed lambda-abstraction: " ^^
384 "the source %s should be a type; instead it is a term " ^^
385 "of type %s") (PP.ppterm ~subst ~metasenv ~context s)
386 (PP.ppterm ~subst ~metasenv ~context sort)))));
387 let ty = typeof_aux ((n,(C.Decl s))::context) t in
389 | C.LetIn (n,ty,t,bo) ->
390 let ty_t = typeof_aux context t in
391 let _ = typeof_aux context ty in
392 if not (R.are_convertible ~subst get_relevance context ty_t ty) then
395 (lazy (Printf.sprintf
396 "The type of %s is %s but it is expected to be %s"
397 (PP.ppterm ~subst ~metasenv ~context t)
398 (PP.ppterm ~subst ~metasenv ~context ty_t)
399 (PP.ppterm ~subst ~metasenv ~context ty))))
401 let ty_bo = typeof_aux ((n,C.Def (t,ty))::context) bo in
402 S.subst ~avoid_beta_redexes:true t ty_bo
403 | C.Appl (he::(_::_ as args)) ->
404 let ty_he = typeof_aux context he in
405 let args_with_ty = List.map (fun t -> t, typeof_aux context t) args in
406 eat_prods ~subst ~metasenv context he ty_he args_with_ty
407 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
408 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,outtype,term,pl) ->
409 let outsort = typeof_aux context outtype in
410 let _,leftno,itl,_,_ = E.get_checked_indtys r in
412 let _,_,_,cl = List.nth itl tyno in List.length cl
414 let parameters, arguments =
415 let ty = R.whd ~subst context (typeof_aux context term) in
418 C.Const (Ref.Ref (_,Ref.Ind _) as r') -> r',[]
419 | C.Appl (C.Const (Ref.Ref (_,Ref.Ind _) as r') :: tl) -> r',tl
422 (TypeCheckerFailure (lazy (Printf.sprintf
423 "Case analysis: analysed term %s is not an inductive one"
424 (PP.ppterm ~subst ~metasenv ~context term)))) in
425 if not (Ref.eq r r') then
427 (TypeCheckerFailure (lazy (Printf.sprintf
428 ("Case analysys: analysed term type is %s, but is expected " ^^
429 "to be (an application of) %s")
430 (PP.ppterm ~subst ~metasenv ~context ty)
431 (PP.ppterm ~subst ~metasenv ~context (C.Const r')))))
433 try HExtlib.split_nth leftno tl
436 raise (TypeCheckerFailure (lazy (Printf.sprintf
437 "%s is partially applied"
438 (PP.ppterm ~subst ~metasenv ~context ty)))) in
439 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
440 let sort_of_ind_type =
441 if parameters = [] then C.Const r
442 else C.Appl ((C.Const r)::parameters) in
443 let type_of_sort_of_ind_ty = typeof_aux context sort_of_ind_type in
444 check_allowed_sort_elimination ~subst ~metasenv r context
445 sort_of_ind_type type_of_sort_of_ind_ty outsort;
446 (* let's check if the type of branches are right *)
447 if List.length pl <> constructorsno then
448 raise (TypeCheckerFailure (lazy ("Wrong number of cases in a match")));
449 let j,branches_ok,p_ty, exp_p_ty =
451 (fun (j,b,old_p_ty,old_exp_p_ty) p ->
454 let cons = Ref.mk_constructor j r in
455 if parameters = [] then C.Const cons
456 else C.Appl (C.Const cons::parameters)
458 let ty_p = typeof_aux context p in
459 let ty_cons = typeof_aux context cons in
461 type_of_branch ~subst context leftno outtype cons ty_cons 0
463 j+1, R.are_convertible ~subst get_relevance context ty_p ty_branch,
466 j,false,old_p_ty,old_exp_p_ty
467 ) (1,true,C.Sort C.Prop,C.Sort C.Prop) pl
469 if not branches_ok then
472 (lazy (Printf.sprintf ("Branch for constructor %s :=\n%s\n"^^
473 "has type %s\nnot convertible with %s")
474 (PP.ppterm ~subst ~metasenv ~context
475 (C.Const (Ref.mk_constructor (j-1) r)))
476 (PP.ppterm ~metasenv ~subst ~context (List.nth pl (j-2)))
477 (PP.ppterm ~metasenv ~subst ~context p_ty)
478 (PP.ppterm ~metasenv ~subst ~context exp_p_ty))));
479 let res = outtype::arguments@[term] in
480 R.head_beta_reduce (C.Appl res)
481 | C.Match _ -> assert false
483 and type_of_branch ~subst context leftno outty cons tycons liftno =
484 match R.whd ~subst context tycons with
485 | C.Const (Ref.Ref (_,Ref.Ind _)) -> C.Appl [S.lift liftno outty ; cons]
486 | C.Appl (C.Const (Ref.Ref (_,Ref.Ind _))::tl) ->
487 let _,arguments = HExtlib.split_nth leftno tl in
488 C.Appl (S.lift liftno outty::arguments@[cons])
489 | C.Prod (name,so,de) ->
491 match S.lift 1 cons with
492 | C.Appl l -> C.Appl (l@[C.Rel 1])
493 | t -> C.Appl [t ; C.Rel 1]
496 type_of_branch ~subst ((name,(C.Decl so))::context)
497 leftno outty cons de (liftno+1))
498 | _ -> raise (AssertFailure (lazy "type_of_branch"))
500 (* check_metasenv_consistency checks that the "canonical" context of a
501 metavariable is consitent - up to relocation via the relocation list l -
502 with the actual context *)
503 and check_metasenv_consistency
504 ~subst ~metasenv term context canonical_context l
508 let context = snd (HExtlib.split_nth shift context) in
509 let rec compare = function
513 raise (AssertFailure (lazy (Printf.sprintf
514 "Local and canonical context %s have different lengths"
515 (PP.ppterm ~subst ~context ~metasenv term))))
517 raise (TypeCheckerFailure (lazy (Printf.sprintf
518 "Unbound variable -%d in %s" m
519 (PP.ppterm ~subst ~metasenv ~context term))))
522 (_,C.Decl t1), (_,C.Decl t2)
523 | (_,C.Def (t1,_)), (_,C.Def (t2,_))
524 | (_,C.Def (_,t1)), (_,C.Decl t2) ->
525 if not (R.are_convertible ~subst get_relevance tl t1 t2) then
528 (lazy (Printf.sprintf
529 ("Not well typed metavariable local context for %s: " ^^
530 "%s expected, which is not convertible with %s")
531 (PP.ppterm ~subst ~metasenv ~context term)
532 (PP.ppterm ~subst ~metasenv ~context t2)
533 (PP.ppterm ~subst ~metasenv ~context t1))))
536 (TypeCheckerFailure (lazy (Printf.sprintf
537 ("Not well typed metavariable local context for %s: " ^^
538 "a definition expected, but a declaration found")
539 (PP.ppterm ~subst ~metasenv ~context term)))));
540 compare (m - 1,tl,ctl)
542 compare (n,context,canonical_context)
544 (* we avoid useless lifting by shortening the context*)
545 let l,context = (0,lc_kind), snd (HExtlib.split_nth shift context) in
546 let lifted_canonical_context =
547 let rec lift_metas i = function
549 | (n,C.Decl t)::tl ->
550 (n,C.Decl (S.subst_meta l (S.lift i t)))::(lift_metas (i+1) tl)
551 | (n,C.Def (t,ty))::tl ->
552 (n,C.Def ((S.subst_meta l (S.lift i t)),
553 S.subst_meta l (S.lift i ty)))::(lift_metas (i+1) tl)
555 lift_metas 1 canonical_context in
556 let l = U.expand_local_context lc_kind in
561 | t, (_,C.Def (ct,_)) ->
562 (*CSC: the following optimization is to avoid a possibly expensive
563 reduction that can be easily avoided and that is quite
564 frequent. However, this is better handled using levels to
570 match List.nth context (n - 1) with
571 | (_,C.Def (te,_)) -> S.lift n te
576 if not (R.are_convertible ~subst get_relevance context optimized_t ct)
580 (lazy (Printf.sprintf
581 ("Not well typed metavariable local context: " ^^
582 "expected a term convertible with %s, found %s")
583 (PP.ppterm ~subst ~metasenv ~context ct)
584 (PP.ppterm ~subst ~metasenv ~context t))))
585 | t, (_,C.Decl ct) ->
586 let type_t = typeof_aux context t in
587 if not (R.are_convertible ~subst get_relevance context type_t ct) then
588 raise (TypeCheckerFailure
589 (lazy (Printf.sprintf
590 ("Not well typed metavariable local context: "^^
591 "expected a term of type %s, found %s of type %s")
592 (PP.ppterm ~subst ~metasenv ~context ct)
593 (PP.ppterm ~subst ~metasenv ~context t)
594 (PP.ppterm ~subst ~metasenv ~context type_t))))
595 ) l lifted_canonical_context
597 Invalid_argument _ ->
598 raise (AssertFailure (lazy (Printf.sprintf
599 "Local and canonical context %s have different lengths"
600 (PP.ppterm ~subst ~metasenv ~context term))))
602 and check_allowed_sort_elimination ~subst ~metasenv r =
605 | C.Appl l -> C.Appl (l @ [arg])
606 | t -> C.Appl [t;arg] in
607 let rec aux context ind arity1 arity2 =
608 let arity1 = R.whd ~subst context arity1 in
609 let arity2 = R.whd ~subst context arity2 in
610 match arity1,arity2 with
611 | C.Prod (name,so1,de1), C.Prod (_,so2,de2) ->
612 if not (R.are_convertible ~subst get_relevance context so1 so2) then
613 raise (TypeCheckerFailure (lazy (Printf.sprintf
614 "In outtype: expected %s, found %s"
615 (PP.ppterm ~subst ~metasenv ~context so1)
616 (PP.ppterm ~subst ~metasenv ~context so2)
618 aux ((name, C.Decl so1)::context)
619 (mkapp (S.lift 1 ind) (C.Rel 1)) de1 de2
620 | C.Sort _, C.Prod (name,so,ta) ->
621 if not (R.are_convertible ~subst get_relevance context so ind) then
622 raise (TypeCheckerFailure (lazy (Printf.sprintf
623 "In outtype: expected %s, found %s"
624 (PP.ppterm ~subst ~metasenv ~context ind)
625 (PP.ppterm ~subst ~metasenv ~context so)
627 (match arity1, R.whd ~subst ((name,C.Decl so)::context) ta with
628 | (C.Sort C.Type _, C.Sort _)
629 | (C.Sort C.Prop, C.Sort C.Prop) -> ()
630 | (C.Sort C.Prop, C.Sort C.Type _) ->
631 (* TODO: we should pass all these parameters since we
632 * have them already *)
633 let _,leftno,itl,_,i = E.get_checked_indtys r in
634 let itl_len = List.length itl in
635 let _,itname,ittype,cl = List.nth itl i in
636 let cl_len = List.length cl in
637 (* is it a singleton, non recursive and non informative
638 definition or an empty one? *)
641 (itl_len = 1 && cl_len = 1 &&
642 let _,_,constrty = List.hd cl in
643 is_non_recursive_singleton r itname ittype constrty &&
644 is_non_informative leftno constrty))
646 raise (TypeCheckerFailure (lazy
647 ("Sort elimination not allowed")));
654 typeof_aux context term
656 and eat_prods ~subst ~metasenv context he ty_he args_with_ty =
657 let rec aux ty_he = function
659 | (arg, ty_arg)::tl ->
660 match R.whd ~subst context ty_he with
662 if R.are_convertible ~subst get_relevance context ty_arg s then
663 aux (S.subst ~avoid_beta_redexes:true arg t) tl
667 (lazy (Printf.sprintf
668 ("Appl: wrong application of %s: the parameter %s has type"^^
669 "\n%s\nbut it should have type \n%s\nContext:\n%s\n")
670 (PP.ppterm ~subst ~metasenv ~context he)
671 (PP.ppterm ~subst ~metasenv ~context arg)
672 (PP.ppterm ~subst ~metasenv ~context ty_arg)
673 (PP.ppterm ~subst ~metasenv ~context s)
674 (PP.ppcontext ~subst ~metasenv context))))
678 (lazy (Printf.sprintf
679 "Appl: %s is not a function, it cannot be applied"
680 (PP.ppterm ~subst ~metasenv ~context
681 (let res = List.length tl in
682 let eaten = List.length args_with_ty - res in
685 (fst (HExtlib.split_nth eaten args_with_ty)))))))))
687 aux ty_he args_with_ty
689 and is_non_recursive_singleton (Ref.Ref (uri,_)) iname ity cty =
690 let ctx = [iname, C.Decl ity] in
691 let cty = debruijn uri 1 [] cty in
692 does_not_occur ~subst:[] ctx 0 1 cty
694 and is_non_informative paramsno c =
695 let rec aux context c =
696 match R.whd context c with
697 | C.Prod (n,so,de) ->
698 let s = typeof ~metasenv:[] ~subst:[] context so in
699 s = C.Sort C.Prop && aux ((n,(C.Decl so))::context) de
701 let context',dx = split_prods ~subst:[] [] paramsno c in
704 and check_mutual_inductive_defs uri ~metasenv ~subst leftno tyl =
705 (* let's check if the arity of the inductive types are well formed *)
706 List.iter (fun (_,_,x,_) -> ignore (typeof ~subst ~metasenv [] x)) tyl;
707 (* let's check if the types of the inductive constructors are well formed. *)
708 let len = List.length tyl in
709 let tys = List.rev_map (fun (_,n,ty,_) -> (n,(C.Decl ty))) tyl in
712 (fun (it_relev,_,ty,cl) i ->
713 let context,ty_sort = split_prods ~subst [] ~-1 ty in
714 let sx_context_ty_rev,_ = HExtlib.split_nth leftno (List.rev context) in
716 (fun (k_relev,_,te) ->
717 let _,k_relev = HExtlib.split_nth leftno k_relev in
718 let te = debruijn uri len [] te in
719 let context,te = split_prods ~subst tys leftno te in
720 let _,chopped_context_rev =
721 HExtlib.split_nth (List.length tys) (List.rev context) in
722 let sx_context_te_rev,_ =
723 HExtlib.split_nth leftno chopped_context_rev in
725 ignore (List.fold_left2
726 (fun context item1 item2 ->
728 match item1,item2 with
729 (n1,C.Decl ty1),(n2,C.Decl ty2) ->
730 n1 = n2 && R.are_convertible ~subst get_relevance context ty1 ty2
731 | (n1,C.Def (bo1,ty1)),(n2,C.Def (bo2,ty2)) ->
733 && R.are_convertible ~subst get_relevance context ty1 ty2
734 && R.are_convertible ~subst get_relevance context bo1 bo2
737 if not convertible then
738 raise (TypeCheckerFailure (lazy
739 ("Mismatch between the left parameters of the constructor " ^
740 "and those of its inductive type")))
743 ) [] sx_context_ty_rev sx_context_te_rev)
744 with Invalid_argument _ -> assert false);
745 let con_sort = typeof ~subst ~metasenv context te in
746 (match R.whd ~subst context con_sort, R.whd ~subst [] ty_sort with
747 (C.Sort (C.Type u1) as s1), (C.Sort (C.Type u2) as s2) ->
748 if not (E.universe_leq u1 u2) then
751 (lazy ("The type " ^ PP.ppterm ~metasenv ~subst ~context s1^
752 " of the constructor is not included in the inductive" ^
753 " type sort " ^ PP.ppterm ~metasenv ~subst ~context s2)))
754 | C.Sort _, C.Sort C.Prop
755 | C.Sort _, C.Sort C.Type _ -> ()
759 (lazy ("Wrong constructor or inductive arity shape"))));
760 (* let's check also the positivity conditions *)
763 (are_all_occurrences_positive ~subst context uri leftno
764 (i+leftno) leftno (len+leftno) te)
768 (lazy ("Non positive occurence in "^NUri.string_of_uri
770 else check_relevance ~subst ~metasenv context k_relev te)
772 check_relevance ~subst ~metasenv [] it_relev ty;
776 and check_relevance ~subst ~metasenv context relevance ty =
777 let error context ty =
778 raise (TypeCheckerFailure
779 (lazy ("Wrong relevance declaration: " ^
780 String.concat "," (List.map string_of_bool relevance)^
781 "\nfor type: "^PP.ppterm ~metasenv ~subst ~context ty)))
783 let rec aux context relevance ty =
784 match R.whd ~subst context ty with
785 | C.Prod (name,so,de) ->
786 let sort = typeof ~subst ~metasenv context so in
787 (match (relevance,R.whd ~subst context sort) with
789 | false::tl,C.Sort C.Prop -> aux ((name,(C.Decl so))::context) tl de
790 | true::_,C.Sort C.Prop
792 | false::_,C.Meta _ -> error context ty
794 | true::tl,C.Meta _ -> aux ((name,(C.Decl so))::context) tl de
795 | _ -> raise (AssertFailure (lazy (Printf.sprintf
796 "Prod: the type %s of the source of %s is not a sort"
797 (PP.ppterm ~subst ~metasenv ~context sort)
798 (PP.ppterm ~subst ~metasenv ~context so)))))
799 | _ -> (match relevance with
801 | _::_ -> error context ty)
802 in aux context relevance ty
804 and guarded_by_destructors r_uri r_len ~subst ~metasenv context recfuns t =
805 let recursor f k t = U.fold shift_k k (fun k () -> f k) () t in
806 let rec aux (context, recfuns, x as k) t =
808 prerr_endline ("GB:\n" ^
809 PP.ppcontext ~subst ~metasenv context^
810 PP.ppterm ~metasenv ~subst ~context t^
811 string_of_recfuns ~subst ~metasenv ~context recfuns);
815 | C.Rel m as t when is_dangerous m recfuns ->
816 raise (NotGuarded (lazy
817 (PP.ppterm ~subst ~metasenv ~context t ^
818 " is a partial application of a fix")))
819 | C.Appl ((C.Rel m)::tl) as t when is_dangerous m recfuns ->
820 let rec_no = get_recno m recfuns in
821 if not (List.length tl > rec_no) then
822 raise (NotGuarded (lazy
823 (PP.ppterm ~context ~subst ~metasenv t ^
824 " is a partial application of a fix")))
826 let rec_arg = List.nth tl rec_no in
827 if not (is_really_smaller r_uri r_len ~subst ~metasenv k rec_arg) then
828 raise (NotGuarded (lazy (Printf.sprintf ("Recursive call %s, %s is not"
829 ^^ " smaller.\ncontext:\n%s") (PP.ppterm ~context ~subst ~metasenv
830 t) (PP.ppterm ~context ~subst ~metasenv rec_arg)
831 (PP.ppcontext ~subst ~metasenv context))));
833 | C.Appl ((C.Rel m)::tl) when is_unfolded m recfuns ->
834 let fixed_args = get_fixed_args m recfuns in
835 HExtlib.list_iter_default2
836 (fun x b -> if not b then aux k x) tl false fixed_args
838 (match List.nth context (m-1) with
840 | _,C.Def (bo,_) -> aux k (S.lift m bo))
842 | C.Appl (C.Const ((Ref.Ref (uri,Ref.Fix (i,recno,_))) as r)::args) ->
843 if List.exists (fun t -> try aux k t;false with NotGuarded _ -> true) args
845 let fl,_,_ = E.get_checked_fixes_or_cofixes r in
847 List.split (List.map (fun (_,name,_,ty,bo) -> (name, C.Decl ty), bo) fl)
849 let fl_len = List.length fl in
850 let bos = List.map (debruijn uri fl_len context) bos in
851 let j = List.fold_left min max_int (List.map (fun (_,_,i,_,_)->i) fl) in
852 let ctx_len = List.length context in
853 (* we may look for fixed params not only up to j ... *)
854 let fa = fixed_args bos j ctx_len (ctx_len + fl_len) in
855 HExtlib.list_iter_default2
856 (fun x b -> if not b then aux k x) args false fa;
857 let context = context@ctx_tys in
858 let ctx_len = List.length context in
860 HExtlib.list_mapi (fun _ i -> ctx_len - i, UnfFix fa) ctx_tys
862 let new_k = context, extra_recfuns@recfuns, x in
867 eat_or_subst_lambdas ~subst ~metasenv j bo fa args new_k
871 List.length args > recno &&
872 (*case where the recursive argument is already really_smaller *)
873 is_really_smaller r_uri r_len ~subst ~metasenv k
874 (List.nth args recno)
876 let bo,(context, _, _ as new_k) = bo_and_k in
878 eat_lambdas ~subst ~metasenv context (recno + 1 - j) bo in
879 let new_context_part,_ =
880 HExtlib.split_nth (List.length context' - List.length context)
882 let k = List.fold_right shift_k new_context_part new_k in
883 let context, recfuns, x = k in
884 let k = context, (1,Safe)::recfuns, x in
890 List.iter (fun (bo,k) -> aux k bo) bos_and_ks
891 | C.Match (Ref.Ref (_,Ref.Ind (true,_,_)),outtype,term,pl) as t ->
892 (match R.whd ~subst context term with
893 | C.Rel m | C.Appl (C.Rel m :: _ ) as t when is_safe m recfuns || m = x ->
894 let ty = typeof ~subst ~metasenv context term in
895 let dc_ctx, dcl, start, stop =
896 specialize_and_abstract_constrs ~subst r_uri r_len context ty in
897 let args = match t with C.Appl (_::tl) -> tl | _ -> [] in
899 List.iter (aux k) args;
902 let rl = recursive_args ~subst ~metasenv dc_ctx start stop dc in
903 let p, k = get_new_safes ~subst k p rl in
906 | _ -> recursor aux k t)
907 | t -> recursor aux k t
909 NotGuarded _ as exc ->
910 let t' = R.whd ~delta:0 ~subst context t in
911 if t = t' then raise exc
914 try aux (context, recfuns, 1) t
915 with NotGuarded s -> raise (TypeCheckerFailure s)
917 and guarded_by_constructors ~subst ~metasenv context t indURI indlen nn =
918 let rec aux context n nn h te =
919 match R.whd ~subst context te with
920 | C.Rel m when m > n && m <= nn -> h
921 | C.Rel _ | C.Meta _ -> true
925 | C.Const (Ref.Ref (_,Ref.Ind _))
926 | C.LetIn _ -> raise (AssertFailure (lazy "17"))
927 | C.Lambda (name,so,de) ->
928 does_not_occur ~subst context n nn so &&
929 aux ((name,C.Decl so)::context) (n + 1) (nn + 1) h de
930 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
931 h && List.for_all (does_not_occur ~subst context n nn) tl
932 | C.Const (Ref.Ref (_,Ref.Con _)) -> true
933 | C.Appl (C.Const (Ref.Ref (_, Ref.Con (_,j,paramsno))) :: tl) as t ->
934 let ty_t = typeof ~subst ~metasenv context t in
935 let dc_ctx, dcl, start, stop =
936 specialize_and_abstract_constrs ~subst indURI indlen context ty_t in
937 let _, dc = List.nth dcl (j-1) in
939 prerr_endline (PP.ppterm ~subst ~metasenv ~context:dc_ctx dc);
940 prerr_endline (PP.ppcontext ~subst ~metasenv dc_ctx);
942 let rec_params = recursive_args ~subst ~metasenv dc_ctx start stop dc in
943 let rec analyse_instantiated_type rec_spec args =
944 match rec_spec, args with
945 | h::rec_spec, he::args ->
946 aux context n nn h he && analyse_instantiated_type rec_spec args
948 | _ -> raise (AssertFailure (lazy
949 ("Too many args for constructor: " ^ String.concat " "
950 (List.map (fun x-> PP.ppterm ~subst ~metasenv ~context x) args))))
952 let _, args = HExtlib.split_nth paramsno tl in
953 analyse_instantiated_type rec_params args
954 | C.Appl ((C.Match (_,out,te,pl))::_)
955 | C.Match (_,out,te,pl) as t ->
956 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
957 List.for_all (does_not_occur ~subst context n nn) tl &&
958 does_not_occur ~subst context n nn out &&
959 does_not_occur ~subst context n nn te &&
960 List.for_all (aux context n nn h) pl
961 (* IMPOSSIBLE unsless we allow to pass cofix to other fix/cofix as we do for
962 higher order fix in g_b_destructors.
964 | C.Const (Ref.Ref (u,(Ref.Fix _| Ref.CoFix _)) as ref)
965 | C.Appl(C.Const (Ref.Ref(u,(Ref.Fix _| Ref.CoFix _)) as ref) :: _) as t ->
966 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
967 let fl,_,_ = E.get_checked_fixes_or_cofixes ref in
968 let len = List.length fl in
969 let tys = List.map (fun (_,n,_,ty,_) -> n, C.Decl ty) fl in
970 List.for_all (does_not_occur ~subst context n nn) tl &&
973 aux (context@tys) n nn h (debruijn u len context bo))
977 | C.Appl _ as t -> does_not_occur ~subst context n nn t
979 aux context 0 nn false t
981 and recursive_args ~subst ~metasenv context n nn te =
982 match R.whd context te with
983 | C.Rel _ | C.Appl _ | C.Const _ -> []
984 | C.Prod (name,so,de) ->
985 (not (does_not_occur ~subst context n nn so)) ::
986 (recursive_args ~subst ~metasenv
987 ((name,(C.Decl so))::context) (n+1) (nn + 1) de)
989 raise (AssertFailure (lazy ("recursive_args:" ^ PP.ppterm ~subst
990 ~metasenv ~context:[] t)))
992 and get_new_safes ~subst (context, recfuns, x as k) p rl =
993 match R.whd ~subst context p, rl with
994 | C.Lambda (name,so,ta), b::tl ->
995 let recfuns = (if b then [0,Safe] else []) @ recfuns in
997 (shift_k (name,(C.Decl so)) (context, recfuns, x)) ta tl
998 | C.Meta _ as e, _ | e, [] -> e, k
999 | _ -> raise (AssertFailure (lazy "Ill formed pattern"))
1001 and is_really_smaller
1002 r_uri r_len ~subst ~metasenv (context, recfuns, x as k) te
1004 match R.whd ~subst context te with
1005 | C.Rel m when is_safe m recfuns -> true
1006 | C.Lambda (name, s, t) ->
1007 is_really_smaller r_uri r_len ~subst ~metasenv (shift_k (name,C.Decl s) k) t
1009 is_really_smaller r_uri r_len ~subst ~metasenv k he
1011 | C.Const (Ref.Ref (_,Ref.Con _)) -> false
1013 | C.Const (Ref.Ref (_,Ref.Fix _)) -> assert false
1015 | C.Match (Ref.Ref (_,Ref.Ind (isinductive,_,_)),_,term,pl) ->
1017 | C.Rel m | C.Appl (C.Rel m :: _ ) when is_safe m recfuns || m = x ->
1018 if not isinductive then
1019 List.for_all (is_really_smaller r_uri r_len ~subst ~metasenv k) pl
1021 let ty = typeof ~subst ~metasenv context term in
1022 let dc_ctx, dcl, start, stop =
1023 specialize_and_abstract_constrs ~subst r_uri r_len context ty in
1026 let rl = recursive_args ~subst ~metasenv dc_ctx start stop dc in
1027 let e, k = get_new_safes ~subst k p rl in
1028 is_really_smaller r_uri r_len ~subst ~metasenv k e)
1030 | _ -> List.for_all (is_really_smaller r_uri r_len ~subst ~metasenv k) pl)
1033 and returns_a_coinductive ~subst context ty =
1034 match R.whd ~subst context ty with
1035 | C.Const (Ref.Ref (uri,Ref.Ind (false,_,_)) as ref)
1036 | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind (false,_,_)) as ref)::_) ->
1037 let _, _, itl, _, _ = E.get_checked_indtys ref in
1038 Some (uri,List.length itl)
1039 | C.Prod (n,so,de) ->
1040 returns_a_coinductive ~subst ((n,C.Decl so)::context) de
1043 and type_of_constant ((Ref.Ref (uri,_)) as ref) =
1045 raise (TypeCheckerFailure (lazy "Inconsistent cached infos in reference"))
1047 match E.get_checked_obj uri, ref with
1048 | (_,_,_,_,C.Inductive(isind1,lno1,tl,_)),Ref.Ref(_,Ref.Ind (isind2,i,lno2))->
1049 if isind1 <> isind2 || lno1 <> lno2 then error ();
1050 let _,_,arity,_ = List.nth tl i in arity
1051 | (_,_,_,_,C.Inductive (_,lno1,tl,_)), Ref.Ref (_,Ref.Con (i,j,lno2)) ->
1052 if lno1 <> lno2 then error ();
1053 let _,_,_,cl = List.nth tl i in
1054 let _,_,arity = List.nth cl (j-1) in
1056 | (_,_,_,_,C.Fixpoint (false,fl,_)), Ref.Ref (_,Ref.CoFix i) ->
1057 let _,_,_,arity,_ = List.nth fl i in
1059 | (_,h1,_,_,C.Fixpoint (true,fl,_)), Ref.Ref (_,Ref.Fix (i,recno2,h2)) ->
1060 let _,_,recno1,arity,_ = List.nth fl i in
1061 if h1 <> h2 || recno1 <> recno2 then error ();
1063 | (_,_,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Decl) -> ty
1064 | (_,h1,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Def h2) ->
1065 if h1 <> h2 then error ();
1067 | _ -> raise (AssertFailure (lazy "type_of_constant: environment/reference"))
1069 and get_relevance ~subst context t args =
1070 let ty = typeof ~subst ~metasenv:[] context t in
1071 let rec aux context ty = function
1073 | arg::tl -> match R.whd ~subst context ty with
1074 | C.Prod (_,so,de) ->
1075 let sort = typeof ~subst ~metasenv:[] context so in
1076 let new_ty = S.subst ~avoid_beta_redexes:true arg de in
1077 (*prerr_endline ("so: " ^ PP.ppterm ~subst ~metasenv:[]
1079 prerr_endline ("sort: " ^ PP.ppterm ~subst ~metasenv:[]
1081 (match R.whd ~subst context sort with
1083 false::(aux context new_ty tl)
1085 | C.Meta _ -> true::(aux context new_ty tl)
1086 | _ -> raise (TypeCheckerFailure (lazy (Printf.sprintf
1087 "Prod: the type %s of the source of %s is not a sort"
1088 (PP.ppterm ~subst ~metasenv:[] ~context sort)
1089 (PP.ppterm ~subst ~metasenv:[] ~context so)))))
1093 (lazy (Printf.sprintf
1094 "Appl: %s is not a function, it cannot be applied"
1095 (PP.ppterm ~subst ~metasenv:[] ~context
1096 (let res = List.length tl in
1097 let eaten = List.length args - res in
1100 (HExtlib.split_nth eaten args))))))))
1101 in aux context ty args
1104 let typecheck_context ~metasenv ~subst context =
1110 _,C.Decl t -> ignore (typeof ~metasenv ~subst:[] context t)
1111 | name,C.Def (te,ty) ->
1112 ignore (typeof ~metasenv ~subst:[] context ty);
1113 let ty' = typeof ~metasenv ~subst:[] context te in
1114 if not (R.are_convertible ~subst get_relevance context ty' ty) then
1115 raise (AssertFailure (lazy (Printf.sprintf (
1116 "the type of the definiens for %s in the context is not "^^
1117 "convertible with the declared one.\n"^^
1118 "inferred type:\n%s\nexpected type:\n%s")
1119 name (PP.ppterm ~subst ~metasenv ~context ty')
1120 (PP.ppterm ~subst ~metasenv ~context ty))))
1126 let typecheck_metasenv metasenv =
1129 (fun metasenv (i,(_,context,ty) as conj) ->
1130 if List.mem_assoc i metasenv then
1131 raise (TypeCheckerFailure (lazy ("duplicate meta " ^ string_of_int i ^
1133 typecheck_context ~metasenv ~subst:[] context;
1134 ignore (typeof ~metasenv ~subst:[] context ty);
1139 let typecheck_subst ~metasenv subst =
1142 (fun subst (i,(_,context,ty,bo) as conj) ->
1143 if List.mem_assoc i subst then
1144 raise (AssertFailure (lazy ("duplicate meta " ^ string_of_int i ^
1145 " in substitution")));
1146 if List.mem_assoc i metasenv then
1147 raise (AssertFailure (lazy ("meta " ^ string_of_int i ^
1148 " is both in the metasenv and in the substitution")));
1149 typecheck_context ~metasenv ~subst context;
1150 ignore (typeof ~metasenv ~subst context ty);
1151 let ty' = typeof ~metasenv ~subst context bo in
1152 if not (R.are_convertible ~subst get_relevance context ty' ty) then
1153 raise (AssertFailure (lazy (Printf.sprintf (
1154 "the type of the definiens for %d in the substitution is not "^^
1155 "convertible with the declared one.\n"^^
1156 "inferred type:\n%s\nexpected type:\n%s")
1158 (PP.ppterm ~subst ~metasenv ~context ty')
1159 (PP.ppterm ~subst ~metasenv ~context ty))));
1165 let typecheck_obj (uri,_height,metasenv,subst,kind) =
1166 (* height is not checked since it is only used to implement an optimization *)
1167 typecheck_metasenv metasenv;
1168 typecheck_subst ~metasenv subst;
1170 | C.Constant (relevance,_,Some te,ty,_) ->
1171 let _ = typeof ~subst ~metasenv [] ty in
1172 let ty_te = typeof ~subst ~metasenv [] te in
1173 if not (R.are_convertible ~subst get_relevance [] ty_te ty) then
1174 raise (TypeCheckerFailure (lazy (Printf.sprintf (
1175 "the type of the body is not convertible with the declared one.\n"^^
1176 "inferred type:\n%s\nexpected type:\n%s")
1177 (PP.ppterm ~subst ~metasenv ~context:[] ty_te)
1178 (PP.ppterm ~subst ~metasenv ~context:[] ty))));
1179 check_relevance ~subst ~metasenv [] relevance ty
1180 (*check_relevance ~in_type:false ~subst ~metasenv relevance te*)
1181 | C.Constant (relevance,_,None,ty,_) ->
1182 ignore (typeof ~subst ~metasenv [] ty);
1183 check_relevance ~subst ~metasenv [] relevance ty
1184 | C.Inductive (_, leftno, tyl, _) ->
1185 check_mutual_inductive_defs uri ~metasenv ~subst leftno tyl
1186 | C.Fixpoint (inductive,fl,_) ->
1189 (fun (types,kl) (relevance,name,k,ty,_) ->
1190 let _ = typeof ~subst ~metasenv [] ty in
1191 check_relevance ~subst ~metasenv [] relevance ty;
1192 ((name,C.Decl ty)::types, k::kl)
1195 let len = List.length types in
1197 List.split (List.map2
1198 (fun (_,_,_,_,bo) rno ->
1199 let dbo = debruijn uri len [] bo in
1203 List.iter2 (fun (_,_,x,ty,_) bo ->
1204 let ty_bo = typeof ~subst ~metasenv types bo in
1205 if not (R.are_convertible ~subst get_relevance types ty_bo ty)
1206 then raise (TypeCheckerFailure (lazy ("(Co)Fix: ill-typed bodies")))
1208 if inductive then begin
1209 let m, context = eat_lambdas ~subst ~metasenv types (x + 1) bo in
1212 match List.hd context with _,C.Decl t -> t | _ -> assert false
1214 match R.whd ~subst (List.tl context) he with
1215 | C.Const (Ref.Ref (uri,Ref.Ind _) as ref)
1216 | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind _) as ref) :: _) ->
1217 let _,_,itl,_,_ = E.get_checked_indtys ref in
1218 uri, List.length itl
1221 (* guarded by destructors conditions D{f,k,x,M} *)
1222 let rec enum_from k =
1223 function [] -> [] | v::tl -> (k,v)::enum_from (k+1) tl
1225 guarded_by_destructors r_uri r_len
1226 ~subst ~metasenv context (enum_from (x+2) kl) m
1228 match returns_a_coinductive ~subst [] ty with
1230 raise (TypeCheckerFailure
1231 (lazy "CoFix: does not return a coinductive type"))
1232 | Some (r_uri, r_len) ->
1233 (* guarded by constructors conditions C{f,M} *)
1235 (guarded_by_constructors ~subst ~metasenv types bo r_uri r_len len)
1237 raise (TypeCheckerFailure
1238 (lazy "CoFix: not guarded by constructors"))
1244 let trust = ref (fun _ -> false);;
1245 let set_trust f = trust := f
1246 let trust_obj obj = !trust obj
1249 (* web interface stuff *)
1252 ref (function (`Start_type_checking _|`Type_checking_completed _|`Type_checking_interrupted _|`Type_checking_failed _|`Trust_obj _) -> ())
1255 let set_logger f = logger := f;;
1257 let typecheck_obj obj =
1258 let u,_,_,_,_ = obj in
1260 !logger (`Start_type_checking u);
1262 !logger (`Type_checking_completed u)
1265 !logger (`Type_checking_interrupted u);
1268 !logger (`Type_checking_failed u);
1274 if trust_obj obj then
1275 let u,_,_,_,_ = obj in
1276 !logger (`Trust_obj u)