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
21 exception TypeCheckerFailure of string Lazy.t
22 exception AssertFailure of string Lazy.t
26 | TypeCheckerFailure s as e -> prerr_endline (Lazy.force s); raise e
32 | Evil of int (* rno *)
33 | UnfFix of bool list (* fixed arguments *)
37 let is_dangerous i l =
38 List.exists (function (j,Evil _) when j=i -> true | _ -> false) l
42 List.exists (function (j,UnfFix _) when j=i -> true | _ -> false) l
46 List.exists (function (j,Safe) when j=i -> true | _ -> false) l
50 try match List.assoc i l with Evil rno -> rno | _ -> assert false
51 with Not_found -> assert false
54 let get_fixed_args i l =
55 try match List.assoc i l with UnfFix fa -> fa | _ -> assert false
56 with Not_found -> assert false
59 let shift_k e (c,rf,x) = e::c,List.map (fun (k,v) -> k+1,v) rf,x+1;;
62 let string_of_recfuns ~subst ~metasenv ~context l =
63 let pp = status#ppterm ~subst ~metasenv ~context in
64 let safe, rest = List.partition (function (_,Safe) -> true | _ -> false) l in
65 let dang,unf = List.partition (function (_,UnfFix _)-> false | _->true)rest in
66 "\n\tsafes: "^String.concat "," (List.map (fun (i,_)->pp (C.Rel i)) safe) ^
67 "\n\tfix : "^String.concat ","
69 (function (i,UnfFix l)-> pp(C.Rel i)^"/"^String.concat "," (List.map
71 | _ ->assert false) unf) ^
72 "\n\trec : "^String.concat ","
74 (function (i,Evil rno)->pp(C.Rel i)^"/"^string_of_int rno
75 | _ -> assert false) dang)
79 let fixed_args bos j n nn =
80 let rec aux k acc = function
81 | C.Appl (C.Rel i::args) when i-k > n && i-k <= nn ->
82 let rec combine l1 l2 =
85 | he1::tl1, he2::tl2 -> (he1,he2)::combine tl1 tl2
86 | _::tl, [] -> (false,C.Rel ~-1)::combine tl [] (* dummy term *)
87 | [],_::_ -> assert false
89 let lefts, _ = HExtlib.split_nth (min j (List.length args)) args in
90 List.map (fun ((b,x),i) -> b && x = C.Rel (k-i))
91 (HExtlib.list_mapi (fun x i -> x,i) (combine acc lefts))
92 | t -> U.fold (fun _ k -> k+1) k aux acc t
94 List.fold_left (aux 0)
95 (let rec f = function 0 -> [] | n -> true :: f (n-1) in f j) bos
98 let debruijn status uri number_of_types ~subst context =
102 | C.Meta (i,(s,l)) ->
104 let _,_,term,_ = U.lookup_subst i subst in
105 let ts = S.subst_meta status (0,l) term in
106 let ts' = aux (k-s) ts in
107 if ts == ts' then t else ts'
108 with U.Subst_not_found _ ->
111 let l1 = HExtlib.sharing_map (aux (k-s)) l in
112 if l1 == l then t else C.Meta (i,(s,C.Ctx l1))
114 | C.Const (Ref.Ref (uri1,(Ref.Fix (no,_,_) | Ref.CoFix no)))
115 | C.Const (Ref.Ref (uri1,Ref.Ind (_,no,_))) when NUri.eq uri uri1 ->
116 C.Rel (k + number_of_types - no)
117 | t -> U.map status (fun _ k -> k+1) k aux t
119 aux (List.length context)
122 let sort_of_prod (status:#NCic.status) ~metasenv ~subst context (name,s) t (t1, t2) =
123 let t1 = R.whd status ~subst context t1 in
124 let t2 = R.whd status ~subst ((name,C.Decl s)::context) t2 in
126 | C.Sort _, C.Sort C.Prop -> t2
127 | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
128 C.Sort (C.Type (NCicEnvironment.max u1 u2))
129 | C.Sort C.Prop,C.Sort (C.Type _) -> t2
130 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))), C.Sort _ -> t2
131 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))), C.Meta (i,(_,(C.Irl 0 | C.Ctx [])))
132 | C.Sort _, C.Meta (i,(_,(C.Irl 0 | C.Ctx []))) ->
133 NCic.Meta (i,(0, C.Irl 0))
134 | x, (C.Sort _ | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))))
137 if x == t1 then s, context else t, ((name,C.Decl s)::context)
139 raise (TypeCheckerFailure (lazy (Printf.sprintf
140 "%s is expected to be a type, but its type is %s that is not a sort"
141 (status#ppterm ~subst ~metasenv ~context y)
142 (status#ppterm ~subst ~metasenv ~context x))))
145 (* instantiate_parameters ps (x1:T1)...(xn:Tn)C *)
146 (* returns ((x_|ps|:T_|ps|)...(xn:Tn)C){ps_1 / x1 ; ... ; ps_|ps| / x_|ps|} *)
147 let rec instantiate_parameters status params c =
150 | C.Prod (_,_,ta), he::tl -> instantiate_parameters status tl (S.subst status he ta)
151 | _,_ -> raise (AssertFailure (lazy "1"))
154 let specialize_inductive_type_constrs status ~subst context ty_term =
155 match R.whd status ~subst context ty_term with
156 | C.Const (Ref.Ref (_,Ref.Ind _) as ref)
157 | C.Appl (C.Const (Ref.Ref (_,Ref.Ind _) as ref) :: _ ) as ty ->
158 let args = match ty with C.Appl (_::tl) -> tl | _ -> [] in
159 let _, leftno, itl, _, i = E.get_checked_indtys status ref in
160 let left_args,_ = HExtlib.split_nth leftno args in
161 let _,_,_,cl = List.nth itl i in
163 (fun (rel,name,ty) -> rel, name, instantiate_parameters status left_args ty) cl
167 let specialize_and_abstract_constrs status ~subst r_uri r_len context ty_term =
168 let cl = specialize_inductive_type_constrs status ~subst context ty_term in
169 let len = List.length context in
171 match E.get_checked_obj status r_uri with
172 | _,_,_,_, C.Inductive (_,_,tys,_) ->
173 context @ List.map (fun (_,name,arity,_) -> name,C.Decl arity) tys
177 List.map (fun (_,id,ty) -> id, debruijn status r_uri r_len ~subst context ty) cl,
181 exception DoesOccur;;
183 let does_not_occur status ~subst context n nn t =
184 let rec aux k _ = function
185 | C.Rel m when m > n+k && m <= nn+k -> raise DoesOccur
186 | C.Rel m when m <= k || m > nn+k -> ()
188 (try match List.nth context (m-1-k) with
189 | _,C.Def (bo,_) -> aux (n-m) () bo
191 with Failure _ -> assert false)
192 | C.Meta (_,(_,(C.Irl 0 | C.Ctx []))) -> (* closed meta *) ()
193 | C.Meta (mno,(s,l)) ->
195 (* possible optimization here: try does_not_occur on l and
196 perform substitution only if DoesOccur is raised *)
197 let _,_,term,_ = U.lookup_subst mno subst in
198 aux (k-s) () (S.subst_meta status (0,l) term)
199 with U.Subst_not_found _ -> () (*match l with
200 | C.Irl len -> if not (n+k >= s+len || s > nn+k) then raise DoesOccur
201 | C.Ctx lc -> List.iter (aux (k-s) ()) lc*))
202 | t -> U.fold (fun _ k -> k + 1) k aux () t
205 with DoesOccur -> false
208 let rec eat_lambdas (status:#NCic.status) ~subst ~metasenv context n te =
209 match (n, R.whd status ~subst context te) with
210 | (0, _) -> (te, context)
211 | (n, C.Lambda (name,so,ta)) when n > 0 ->
212 eat_lambdas status ~subst ~metasenv ((name,(C.Decl so))::context) (n - 1) ta
214 raise (AssertFailure (lazy (Printf.sprintf "eat_lambdas (%d, %s)" n
215 (status#ppterm ~subst ~metasenv ~context te))))
218 let rec eat_or_subst_lambdas status
219 ~subst ~metasenv n te to_be_subst args (context,_,_ as k)
221 match n, R.whd status ~subst context te, to_be_subst, args with
222 | (n, C.Lambda (_,_,ta),true::to_be_subst,arg::args) when n > 0 ->
223 eat_or_subst_lambdas status ~subst ~metasenv (n - 1) (S.subst status arg ta)
225 | (n, C.Lambda (name,so,ta),false::to_be_subst,_::args) when n > 0 ->
226 eat_or_subst_lambdas status ~subst ~metasenv (n - 1) ta to_be_subst args
227 (shift_k (name,(C.Decl so)) k)
228 | (_, te, _, _) -> te, k
231 let check_homogeneous_call (status:#NCic.status) ~subst context indparamsno n uri reduct tl =
237 match R.whd status ~subst context x with
238 | C.Rel m when m = n - (indparamsno - k) -> k - 1
239 | _ -> raise (TypeCheckerFailure (lazy
240 ("Argument "^string_of_int (indparamsno - k + 1) ^ " (of " ^
241 string_of_int indparamsno ^ " fixed) is not homogeneous in "^
242 "appl:\n"^ status#ppterm ~context ~subst ~metasenv:[] reduct))))
246 raise (TypeCheckerFailure
247 (lazy ("Non-positive occurence in mutual inductive definition(s) [2]"^
248 NUri.string_of_uri uri)))
251 (* Inductive types being checked for positivity have *)
252 (* indexes x s.t. n < x <= nn. *)
253 let rec weakly_positive status ~subst context n nn uri indparamsno posuri te =
254 (*CSC: Not very nice. *)
255 let dummy = C.Sort C.Prop in
256 (*CSC: to be moved in cicSubstitution? *)
257 let rec subst_inductive_type_with_dummy _ = function
258 | C.Meta (_,(_,C.Irl _)) as x -> x
259 | C.Meta (i,(lift,C.Ctx ls)) ->
260 C.Meta (i,(lift,C.Ctx
261 (List.map (subst_inductive_type_with_dummy ()) ls)))
262 | C.Const (Ref.Ref (uri',Ref.Ind (true,0,_))) when NUri.eq uri' uri -> dummy
263 | C.Appl ((C.Const (Ref.Ref (uri',Ref.Ind (true,0,lno))))::tl)
264 when NUri.eq uri' uri ->
265 let _, rargs = HExtlib.split_nth lno tl in
266 if rargs = [] then dummy else C.Appl (dummy :: rargs)
267 | t -> U.map status (fun _ x->x) () subst_inductive_type_with_dummy t
269 (* this function has the same semantics of are_all_occurrences_positive
270 but the i-th context entry role is played by dummy and some checks
271 are skipped because we already know that are_all_occurrences_positive
273 let rec aux context n nn te =
274 match R.whd status ~subst context te with
275 | t when t = dummy -> true
278 let _,_,term,_ = U.lookup_subst i subst in
279 let t = S.subst_meta status lc term in
280 weakly_positive status ~subst context n nn uri indparamsno posuri t
281 with U.Subst_not_found _ -> true)
282 | C.Appl (te::rargs) when te = dummy ->
283 List.for_all (does_not_occur status ~subst context n nn) rargs
284 | C.Prod (name,source,dest) when
285 does_not_occur status ~subst ((name,C.Decl source)::context) 0 1 dest ->
286 (* dummy abstraction, so we behave as in the anonimous case *)
287 strictly_positive status ~subst context n nn indparamsno posuri source &&
288 aux ((name,C.Decl source)::context) (n + 1) (nn + 1) dest
289 | C.Prod (name,source,dest) ->
290 does_not_occur status ~subst context n nn source &&
291 aux ((name,C.Decl source)::context) (n + 1) (nn + 1) dest
293 raise (TypeCheckerFailure (lazy "Malformed inductive constructor type"))
295 aux context n nn (subst_inductive_type_with_dummy () te)
297 and strictly_positive status ~subst context n nn indparamsno posuri te =
298 match R.whd status ~subst context te with
299 | t when does_not_occur status ~subst context n nn t -> true
302 let _,_,term,_ = U.lookup_subst i subst in
303 let t = S.subst_meta status lc term in
304 strictly_positive status ~subst context n nn indparamsno posuri t
305 with U.Subst_not_found _ -> true)
306 | C.Rel _ when indparamsno = 0 -> true
307 | C.Appl ((C.Rel m)::tl) as reduct when m > n && m <= nn ->
308 check_homogeneous_call status ~subst context indparamsno n posuri reduct tl;
309 List.for_all (does_not_occur status ~subst context n nn) tl
310 | C.Prod (name,so,ta) ->
311 does_not_occur status ~subst context n nn so &&
312 strictly_positive status ~subst ((name,C.Decl so)::context) (n+1) (nn+1)
313 indparamsno posuri ta
314 | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind _) as r)::tl) ->
315 let _,paramsno,tyl,_,i = E.get_checked_indtys status r in
316 let _,name,ity,cl = List.nth tyl i in
317 let ok = List.length tyl = 1 in
318 let params, arguments = HExtlib.split_nth paramsno tl in
319 let lifted_params = List.map (S.lift status 1) params in
321 List.map (fun (_,_,te) -> instantiate_parameters status lifted_params te) cl
324 List.for_all (does_not_occur status ~subst context n nn) arguments &&
326 (weakly_positive status ~subst ((name,C.Decl ity)::context) (n+1) (nn+1)
327 uri indparamsno posuri) cl
330 (* the inductive type indexes are s.t. n < x <= nn *)
331 and are_all_occurrences_positive (status:#NCic.status) ~subst context uri indparamsno i n nn te =
332 match R.whd status ~subst context te with
333 | C.Appl ((C.Rel m)::tl) as reduct when m = i ->
334 check_homogeneous_call status ~subst context indparamsno n uri reduct tl;
335 List.for_all (does_not_occur status ~subst context n nn) tl
336 | C.Rel m when m = i ->
337 if indparamsno = 0 then
340 raise (TypeCheckerFailure
341 (lazy ("Non-positive occurence in mutual inductive definition(s) [3]"^
342 NUri.string_of_uri uri)))
343 | C.Prod (name,source,dest) when
344 does_not_occur status ~subst ((name,C.Decl source)::context) 0 1 dest ->
345 strictly_positive status ~subst context n nn indparamsno uri source &&
346 are_all_occurrences_positive status ~subst
347 ((name,C.Decl source)::context) uri indparamsno
348 (i+1) (n + 1) (nn + 1) dest
349 | C.Prod (name,source,dest) ->
350 if not (does_not_occur status ~subst context n nn source) then
351 raise (TypeCheckerFailure (lazy ("Non-positive occurrence in "^
352 status#ppterm ~context ~metasenv:[] ~subst te)));
353 are_all_occurrences_positive status ~subst ((name,C.Decl source)::context)
354 uri indparamsno (i+1) (n + 1) (nn + 1) dest
357 (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
358 (NUri.string_of_uri uri))))
361 exception NotGuarded of string Lazy.t;;
363 let type_of_branch (status:#NCic.status) ~subst context leftno outty cons tycons =
364 let rec aux liftno context cons tycons =
365 match R.whd status ~subst context tycons with
366 | C.Const (Ref.Ref (_,Ref.Ind _)) -> C.Appl [S.lift status liftno outty ; cons]
367 | C.Appl (C.Const (Ref.Ref (_,Ref.Ind _))::tl) ->
368 let _,arguments = HExtlib.split_nth leftno tl in
369 C.Appl (S.lift status liftno outty::arguments@[cons])
370 | C.Prod (name,so,de) ->
372 match S.lift status 1 cons with
373 | C.Appl l -> C.Appl (l@[C.Rel 1])
374 | t -> C.Appl [t ; C.Rel 1]
376 C.Prod (name,so, aux (liftno+1) ((name,(C.Decl so))::context) cons de)
377 | t -> raise (AssertFailure
378 (lazy ("type_of_branch, the contructor has type: " ^ status#ppterm
379 ~metasenv:[] ~context:[] ~subst:[] t)))
381 aux 0 context cons tycons
385 let rec typeof (status:#NCicEnvironment.status) ~subst ~metasenv context term =
386 let rec typeof_aux context =
387 fun t -> (*prerr_endline (status#ppterm ~metasenv ~subst ~context t);*)
391 match List.nth context (n - 1) with
392 | (_,C.Decl ty) -> S.lift status n ty
393 | (_,C.Def (_,ty)) -> S.lift status n ty
395 raise (TypeCheckerFailure (lazy ("unbound variable " ^ string_of_int n
396 ^" under: " ^ status#ppcontext ~metasenv ~subst context))))
398 (try C.Sort (NCicEnvironment.typeof_sort status s)
400 | NCicEnvironment.UntypableSort msg -> raise (TypeCheckerFailure msg)
401 | NCicEnvironment.AssertFailure msg -> raise (AssertFailure msg))
402 | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
403 | C.Meta (n,l) as t ->
404 let canonical_ctx,ty =
406 let _,c,_,ty = U.lookup_subst n subst in c,ty
407 with U.Subst_not_found _ -> try
408 let _,c,ty = U.lookup_meta n metasenv in c, ty
409 (* match ty with C.Implicit _ -> assert false | _ -> c,ty *)
410 with U.Meta_not_found _ ->
411 raise (AssertFailure (lazy (Printf.sprintf
412 "%s not found in:\n%s" (status#ppterm ~subst ~metasenv ~context t)
413 (status#ppmetasenv ~subst metasenv)
416 check_metasenv_consistency t ~subst ~metasenv context canonical_ctx l;
417 S.subst_meta status l ty
418 | C.Const ref -> type_of_constant status ref
419 | C.Prod (name,s,t) ->
420 let sort1 = typeof_aux context s in
421 let sort2 = typeof_aux ((name,(C.Decl s))::context) t in
422 sort_of_prod status ~metasenv ~subst context (name,s) t (sort1,sort2)
423 | C.Lambda (n,s,t) ->
424 let sort = typeof_aux context s in
425 (match R.whd status ~subst context sort with
426 | C.Meta _ | C.Sort _ -> ()
429 (TypeCheckerFailure (lazy (Printf.sprintf
430 ("Not well-typed lambda-abstraction: " ^^
431 "the source %s should be a type; instead it is a term " ^^
432 "of type %s") (status#ppterm ~subst ~metasenv ~context s)
433 (status#ppterm ~subst ~metasenv ~context sort)))));
434 let ty = typeof_aux ((n,(C.Decl s))::context) t in
436 | C.LetIn (n,ty,t,bo) ->
437 let ty_t = typeof_aux context t in
438 let _ = typeof_aux context ty in
439 if not (R.are_convertible status ~metasenv ~subst context ty_t ty) then
442 (lazy (Printf.sprintf
443 "The type of %s is %s but it is expected to be %s"
444 (status#ppterm ~subst ~metasenv ~context t)
445 (status#ppterm ~subst ~metasenv ~context ty_t)
446 (status#ppterm ~subst ~metasenv ~context ty))))
448 let ty_bo = typeof_aux ((n,C.Def (t,ty))::context) bo in
449 S.subst status ~avoid_beta_redexes:true t ty_bo
450 | C.Appl (he::(_::_ as args)) ->
451 let ty_he = typeof_aux context he in
452 let args_with_ty = List.map (fun t -> t, typeof_aux context t) args in
453 eat_prods status ~subst ~metasenv context he ty_he args_with_ty
454 | C.Appl _ -> raise (AssertFailure (lazy "Appl of length < 2"))
455 | C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as r,outtype,term,pl) ->
456 let outsort = typeof_aux context outtype in
457 let _,leftno,itl,_,_ = E.get_checked_indtys status r in
459 let _,_,_,cl = List.nth itl tyno in List.length cl
461 let parameters, arguments =
462 let ty = R.whd status ~subst context (typeof_aux context term) in
465 C.Const (Ref.Ref (_,Ref.Ind _) as r') -> r',[]
466 | C.Appl (C.Const (Ref.Ref (_,Ref.Ind _) as r') :: tl) -> r',tl
469 (TypeCheckerFailure (lazy (Printf.sprintf
470 "Case analysis: analysed term %s is not an inductive one"
471 (status#ppterm ~subst ~metasenv ~context term)))) in
472 if not (Ref.eq r r') then
474 (TypeCheckerFailure (lazy (Printf.sprintf
475 ("Case analysys: analysed term type is %s, but is expected " ^^
476 "to be (an application of) %s")
477 (status#ppterm ~subst ~metasenv ~context ty)
478 (status#ppterm ~subst ~metasenv ~context (C.Const r')))))
480 try HExtlib.split_nth leftno tl
483 raise (TypeCheckerFailure (lazy (Printf.sprintf
484 "%s is partially applied"
485 (status#ppterm ~subst ~metasenv ~context ty)))) in
486 (* let's control if the sort elimination is allowed: [(I q1 ... qr)|B] *)
487 let sort_of_ind_type =
488 if parameters = [] then C.Const r
489 else C.Appl ((C.Const r)::parameters) in
490 let type_of_sort_of_ind_ty = typeof_aux context sort_of_ind_type in
491 check_allowed_sort_elimination status ~subst ~metasenv r context
492 sort_of_ind_type type_of_sort_of_ind_ty outsort;
493 (* let's check if the type of branches are right *)
494 if List.length pl <> constructorsno then
495 raise (TypeCheckerFailure (lazy ("Wrong number of cases in a match")));
496 let j,branches_ok,p_ty, exp_p_ty =
498 (fun (j,b,old_p_ty,old_exp_p_ty) p ->
501 let cons = Ref.mk_constructor j r in
502 if parameters = [] then C.Const cons
503 else C.Appl (C.Const cons::parameters)
505 let ty_p = typeof_aux context p in
506 let ty_cons = typeof_aux context cons in
508 type_of_branch status ~subst context leftno outtype cons ty_cons
510 j+1, R.are_convertible status ~metasenv ~subst context ty_p ty_branch,
513 j,false,old_p_ty,old_exp_p_ty
514 ) (1,true,C.Sort C.Prop,C.Sort C.Prop) pl
516 if not branches_ok then
519 (lazy (Printf.sprintf ("Branch for constructor %s :=\n%s\n"^^
520 "has type %s\nnot convertible with %s")
521 (status#ppterm ~subst ~metasenv ~context
522 (C.Const (Ref.mk_constructor (j-1) r)))
523 (status#ppterm ~metasenv ~subst ~context (List.nth pl (j-2)))
524 (status#ppterm ~metasenv ~subst ~context p_ty)
525 (status#ppterm ~metasenv ~subst ~context exp_p_ty))));
526 let res = outtype::arguments@[term] in
527 R.head_beta_reduce status (C.Appl res)
528 | C.Match _ -> assert false
530 (* check_metasenv_consistency checks that the "canonical" context of a
531 metavariable is consitent - up to relocation via the relocation list l -
532 with the actual context *)
533 and check_metasenv_consistency
534 ~subst ~metasenv term context canonical_context l
538 let context = snd (HExtlib.split_nth shift context) in
539 let rec compare = function
543 raise (AssertFailure (lazy (Printf.sprintf
544 "(2) Local and canonical context %s have different lengths"
545 (status#ppterm ~subst ~context ~metasenv term))))
547 raise (TypeCheckerFailure (lazy (Printf.sprintf
548 "Unbound variable -%d in %s" m
549 (status#ppterm ~subst ~metasenv ~context term))))
552 (_,C.Decl t1), (_,C.Decl t2)
553 | (_,C.Def (t1,_)), (_,C.Def (t2,_))
554 | (_,C.Def (_,t1)), (_,C.Decl t2) ->
555 if not (R.are_convertible status ~metasenv ~subst tl t1 t2) then
558 (lazy (Printf.sprintf
559 ("Not well typed metavariable local context for %s: " ^^
560 "%s expected, which is not convertible with %s")
561 (status#ppterm ~subst ~metasenv ~context term)
562 (status#ppterm ~subst ~metasenv ~context t2)
563 (status#ppterm ~subst ~metasenv ~context t1))))
566 (TypeCheckerFailure (lazy (Printf.sprintf
567 ("Not well typed metavariable local context for %s: " ^^
568 "a definition expected, but a declaration found")
569 (status#ppterm ~subst ~metasenv ~context term)))));
570 compare (m - 1,tl,ctl)
572 compare (n,context,canonical_context)
574 (* we avoid useless lifting by shortening the context*)
575 let l,context = (0,lc_kind), snd (HExtlib.split_nth shift context) in
576 let lifted_canonical_context =
577 let rec lift_metas i = function
579 | (n,C.Decl t)::tl ->
580 (n,C.Decl (S.subst_meta status l (S.lift status i t)))::(lift_metas (i+1) tl)
581 | (n,C.Def (t,ty))::tl ->
582 (n,C.Def ((S.subst_meta status l (S.lift status i t)),
583 S.subst_meta status l (S.lift status i ty)))::(lift_metas (i+1) tl)
585 lift_metas 1 canonical_context in
586 let l = U.expand_local_context lc_kind in
591 | t, (_,C.Def (ct,_)) ->
592 (*CSC: the following optimization is to avoid a possibly expensive
593 reduction that can be easily avoided and that is quite
594 frequent. However, this is better handled using levels to
600 match List.nth context (n - 1) with
601 | (_,C.Def (te,_)) -> S.lift status n te
606 if not (R.are_convertible status ~metasenv ~subst context optimized_t ct)
610 (lazy (Printf.sprintf
611 ("Not well typed metavariable local context: " ^^
612 "expected a term convertible with %s, found %s")
613 (status#ppterm ~subst ~metasenv ~context ct)
614 (status#ppterm ~subst ~metasenv ~context t))))
615 | t, (_,C.Decl ct) ->
616 let type_t = typeof_aux context t in
617 if not (R.are_convertible status ~metasenv ~subst context type_t ct) then
618 raise (TypeCheckerFailure
619 (lazy (Printf.sprintf
620 ("Not well typed metavariable local context: "^^
621 "expected a term of type %s, found %s of type %s")
622 (status#ppterm ~subst ~metasenv ~context ct)
623 (status#ppterm ~subst ~metasenv ~context t)
624 (status#ppterm ~subst ~metasenv ~context type_t))))
625 ) l lifted_canonical_context
627 | Invalid_argument "List.iter2" ->
628 raise (AssertFailure (lazy (Printf.sprintf
629 "(1) Local and canonical context %s have different lengths"
630 (status#ppterm ~subst ~metasenv ~context term))))
633 typeof_aux context term
635 and check_allowed_sort_elimination status ~subst ~metasenv r =
638 | C.Appl l -> C.Appl (l @ [arg])
639 | t -> C.Appl [t;arg] in
640 let rec aux context ind arity1 arity2 =
641 let arity1 = R.whd status ~subst context arity1 in
642 let arity2 = R.whd status ~subst context arity2 in
643 match arity1,arity2 with
644 | C.Prod (name,so1,de1), C.Prod (_,so2,de2) ->
645 if not (R.are_convertible status ~metasenv ~subst context so1 so2) then
646 raise (TypeCheckerFailure (lazy (Printf.sprintf
647 "In outtype: expected %s, found %s"
648 (status#ppterm ~subst ~metasenv ~context so1)
649 (status#ppterm ~subst ~metasenv ~context so2)
651 aux ((name, C.Decl so1)::context)
652 (mkapp (S.lift status 1 ind) (C.Rel 1)) de1 de2
653 | C.Sort _, C.Prod (name,so,ta) ->
654 if not (R.are_convertible status ~metasenv ~subst context so ind) then
655 raise (TypeCheckerFailure (lazy (Printf.sprintf
656 "In outtype: expected %s, found %s"
657 (status#ppterm ~subst ~metasenv ~context ind)
658 (status#ppterm ~subst ~metasenv ~context so)
660 (match arity1, R.whd status ~subst ((name,C.Decl so)::context) ta with
661 | C.Sort s1, (C.Sort s2 as arity2) ->
662 (match NCicEnvironment.allowed_sort_elimination s1 s2 with
665 (* TODO: we should pass all these parameters since we
666 * have them already *)
667 let _,leftno,itl,_,i = E.get_checked_indtys status r in
668 let itl_len = List.length itl in
669 let _,itname,ittype,cl = List.nth itl i in
670 let cl_len = List.length cl in
671 (* is it a singleton, non recursive and non informative
672 definition or an empty one? *)
675 (itl_len = 1 && cl_len = 1 &&
676 let _,_,constrty = List.hd cl in
677 is_non_recursive_singleton status
678 ~subst r itname ittype constrty &&
679 is_non_informative status ~metasenv ~subst leftno constrty))
681 raise (TypeCheckerFailure (lazy
682 ("Sort elimination not allowed: " ^
683 status#ppterm ~metasenv ~subst ~context arity1
685 status#ppterm ~metasenv ~subst ~context arity2
692 and eat_prods status ~subst ~metasenv context he ty_he args_with_ty =
693 let rec aux ty_he = function
695 | (arg, ty_arg)::tl ->
696 match R.whd status ~subst context ty_he with
698 if R.are_convertible status ~metasenv ~subst context ty_arg s then
699 aux (S.subst status ~avoid_beta_redexes:true arg t) tl
703 (lazy (Printf.sprintf
704 ("Appl: wrong application of %s: the argument %s has type"^^
705 "\n%s\nbut it should have type \n%s\nContext:\n%s\n")
706 (status#ppterm ~subst ~metasenv ~context he)
707 (status#ppterm ~subst ~metasenv ~context arg)
708 (status#ppterm ~subst ~metasenv ~context ty_arg)
709 (status#ppterm ~subst ~metasenv ~context s)
710 (status#ppcontext ~subst ~metasenv context))))
714 (lazy (Printf.sprintf
715 "Appl: %s is not a function, it cannot be applied"
716 (status#ppterm ~subst ~metasenv ~context
717 (let res = List.length tl in
718 let eaten = List.length args_with_ty - res in
721 (fst (HExtlib.split_nth eaten args_with_ty)))))))))
723 aux ty_he args_with_ty
725 and is_non_recursive_singleton status ~subst (Ref.Ref (uri,_)) iname ity cty =
726 let ctx = [iname, C.Decl ity] in
727 let cty = debruijn status uri 1 [] ~subst cty in
728 let len = List.length ctx in
729 let rec aux ctx n nn t =
730 match R.whd status ~subst ctx t with
731 | C.Prod (name, src, tgt) ->
732 does_not_occur status ~subst ctx n nn src &&
733 aux ((name, C.Decl src) :: ctx) (n+1) (nn+1) tgt
734 | C.Rel k | C.Appl (C.Rel k :: _) when k = nn -> true
737 aux ctx (len-1) len cty
739 and is_non_informative status ~metasenv ~subst paramsno c =
740 let rec aux context c =
741 match R.whd status ~subst context c with
742 | C.Prod (n,so,de) ->
743 let s = typeof status ~metasenv ~subst context so in
744 (s = C.Sort C.Prop ||
745 match s with C.Sort (C.Type ((`CProp,_)::_)) -> true | _ -> false) &&
746 aux ((n,(C.Decl so))::context) de
748 let context',dx = NCicReduction.split_prods status ~subst [] paramsno c in
751 and check_mutual_inductive_defs status uri ~metasenv ~subst leftno tyl =
752 (* let's check if the arity of the inductive types are well formed *)
753 List.iter (fun (_,_,x,_) -> ignore (typeof status ~subst ~metasenv [] x)) tyl;
754 (* let's check if the types of the inductive constructors are well formed. *)
755 let len = List.length tyl in
756 let tys = List.rev_map (fun (_,n,ty,_) -> (n,(C.Decl ty))) tyl in
759 (fun (it_relev,_,ty,cl) i ->
760 let context,ty_sort = NCicReduction.split_prods status ~subst [] ~-1 ty in
761 let sx_context_ty_rev,_ = HExtlib.split_nth leftno (List.rev context) in
763 (fun (k_relev,_,te) ->
765 try snd (HExtlib.split_nth leftno k_relev)
766 with Failure _ -> k_relev in
767 let te = debruijn status uri len [] ~subst te in
768 let context,te = NCicReduction.split_prods status ~subst tys leftno te in
769 let _,chopped_context_rev =
770 HExtlib.split_nth (List.length tys) (List.rev context) in
771 let sx_context_te_rev,_ =
772 HExtlib.split_nth leftno chopped_context_rev in
774 ignore (List.fold_left2
775 (fun context item1 item2 ->
777 match item1,item2 with
778 (_,C.Decl ty1),(_,C.Decl ty2) ->
779 R.are_convertible status ~metasenv ~subst context ty1 ty2
780 | (_,C.Def (bo1,ty1)),(_,C.Def (bo2,ty2)) ->
781 R.are_convertible status ~metasenv ~subst context ty1 ty2 &&
782 R.are_convertible status ~metasenv ~subst context bo1 bo2
785 if not convertible then
786 raise (TypeCheckerFailure (lazy
787 ("Mismatch between the left parameters of the constructor " ^
788 "and those of its inductive type")))
791 ) [] sx_context_ty_rev sx_context_te_rev)
792 with Invalid_argument "List.fold_left2" -> assert false);
793 let con_sort = typeof status ~subst ~metasenv context te in
794 (match R.whd status ~subst context con_sort, R.whd status ~subst [] ty_sort with
795 (C.Sort (C.Type u1) as s1), (C.Sort (C.Type u2) as s2) ->
796 if not (E.universe_leq status u1 u2) then
799 (lazy ("The type " ^ status#ppterm ~metasenv ~subst ~context s1^
800 " of the constructor is not included in the inductive" ^
801 " type sort " ^ status#ppterm ~metasenv ~subst ~context s2)))
802 | C.Sort _, C.Sort C.Prop
803 | C.Sort _, C.Sort C.Type _ -> ()
807 (lazy ("Wrong constructor or inductive arity shape"))));
808 (* let's check also the positivity conditions *)
811 (are_all_occurrences_positive status ~subst context uri leftno
812 (i+leftno) leftno (len+leftno) te)
816 (lazy ("Non positive occurence in "^NUri.string_of_uri
818 else check_relevance status ~subst ~metasenv context k_relev te)
820 check_relevance status ~subst ~metasenv [] it_relev ty;
824 and check_relevance status ~subst ~metasenv context relevance ty =
825 let error context ty =
826 raise (TypeCheckerFailure
827 (lazy ("Wrong relevance declaration: " ^
828 String.concat "," (List.map string_of_bool relevance)^
829 "\nfor type: "^status#ppterm ~metasenv ~subst ~context ty)))
831 let rec aux context relevance ty =
832 match R.whd status ~subst context ty with
833 | C.Prod (name,so,de) ->
834 let sort = typeof status ~subst ~metasenv context so in
835 (match (relevance,R.whd status ~subst context sort) with
837 | false::tl,C.Sort C.Prop -> aux ((name,(C.Decl so))::context) tl de
838 | true::_,C.Sort C.Prop
840 | false::_,C.Meta _ -> error context ty
842 | true::tl,C.Meta _ -> aux ((name,(C.Decl so))::context) tl de
843 | _ -> raise (AssertFailure (lazy (Printf.sprintf
844 "Prod: the type %s of the source of %s is not a sort"
845 (status#ppterm ~subst ~metasenv ~context sort)
846 (status#ppterm ~subst ~metasenv ~context so)))))
847 | _ -> (match relevance with
849 | _::_ -> error context ty)
850 in aux context relevance ty
852 and guarded_by_destructors (status:#NCic.status) r_uri r_len ~subst ~metasenv context recfuns t =
853 let recursor f k t = U.fold shift_k k (fun k () -> f k) () t in
854 let rec aux (context, recfuns, x as k) t =
856 prerr_endline ("GB:\n" ^
857 status#ppcontext ~subst ~metasenv context^
858 status#ppterm ~metasenv ~subst ~context t^
859 string_of_recfuns ~subst ~metasenv ~context recfuns);
863 | C.Rel m as t when is_dangerous m recfuns ->
864 raise (NotGuarded (lazy
865 (status#ppterm ~subst ~metasenv ~context t ^
866 " is a partial application of a fix")))
867 | C.Appl ((C.Rel m)::tl) as t when is_dangerous m recfuns ->
868 let rec_no = get_recno m recfuns in
869 if not (List.length tl > rec_no) then
870 raise (NotGuarded (lazy
871 (status#ppterm ~context ~subst ~metasenv t ^
872 " is a partial application of a fix")))
874 let rec_arg = List.nth tl rec_no in
875 if not (is_really_smaller status r_uri r_len ~subst ~metasenv k rec_arg) then
876 raise (NotGuarded (lazy (Printf.sprintf ("Recursive call %s, %s is not"
877 ^^ " smaller.\ncontext:\n%s") (status#ppterm ~context ~subst ~metasenv
878 t) (status#ppterm ~context ~subst ~metasenv rec_arg)
879 (status#ppcontext ~subst ~metasenv context))));
881 | C.Appl ((C.Rel m)::tl) when is_unfolded m recfuns ->
882 let fixed_args = get_fixed_args m recfuns in
883 HExtlib.list_iter_default2
884 (fun x b -> if not b then aux k x) tl false fixed_args
886 (match List.nth context (m-1) with
888 | _,C.Def (bo,_) -> aux k (S.lift status m bo))
890 | C.Appl (C.Const ((Ref.Ref (uri,Ref.Fix (i,recno,_))) as r)::args) ->
891 if List.exists (fun t -> try aux k t;false with NotGuarded _ -> true) args
893 let fl,_,_ = E.get_checked_fixes_or_cofixes status r in
895 List.split (List.map (fun (_,name,_,ty,bo) -> (name, C.Decl ty), bo) fl)
897 let fl_len = List.length fl in
898 let bos = List.map (debruijn status uri fl_len context ~subst) bos in
899 let j = List.fold_left min max_int (List.map (fun (_,_,i,_,_)->i) fl) in
900 let ctx_len = List.length context in
901 (* we may look for fixed params not only up to j ... *)
902 let fa = fixed_args bos j ctx_len (ctx_len + fl_len) in
903 HExtlib.list_iter_default2
904 (fun x b -> if not b then aux k x) args false fa;
905 let context = context@ctx_tys in
906 let ctx_len = List.length context in
908 HExtlib.list_mapi (fun _ i -> ctx_len - i, UnfFix fa) ctx_tys
910 let new_k = context, extra_recfuns@recfuns, x in
915 eat_or_subst_lambdas status ~subst ~metasenv j bo fa args new_k
919 List.length args > recno &&
920 (*case where the recursive argument is already really_smaller *)
921 is_really_smaller status r_uri r_len ~subst ~metasenv k
922 (List.nth args recno)
924 let bo,(context, _, _ as new_k) = bo_and_k in
926 eat_lambdas status ~subst ~metasenv context (recno + 1 - j) bo in
927 let new_context_part,_ =
928 HExtlib.split_nth (List.length context' - List.length context)
930 let k = List.fold_right shift_k new_context_part new_k in
931 let context, recfuns, x = k in
932 let k = context, (1,Safe)::recfuns, x in
938 List.iter (fun (bo,k) -> aux k bo) bos_and_ks
939 | C.Match (Ref.Ref (_,Ref.Ind (true,_,_)),outtype,term,pl) as t ->
940 (match R.whd status ~subst context term with
941 | C.Rel m | C.Appl (C.Rel m :: _ ) as t when is_safe m recfuns || m = x ->
942 let ty = typeof status ~subst ~metasenv context term in
943 let dc_ctx, dcl, start, stop =
944 specialize_and_abstract_constrs status ~subst r_uri r_len context ty in
945 let args = match t with C.Appl (_::tl) -> tl | _ -> [] in
947 List.iter (aux k) args;
950 let rl = recursive_args status ~subst ~metasenv dc_ctx start stop dc in
951 let p, k = get_new_safes status ~subst k p rl in
954 | _ -> recursor aux k t)
955 | t -> recursor aux k t
957 NotGuarded _ as exc ->
958 let t' = R.whd status ~delta:0 ~subst context t in
959 if t = t' then raise exc
962 try aux (context, recfuns, 1) t
963 with NotGuarded s -> raise (TypeCheckerFailure s)
965 and guarded_by_constructors status ~subst ~metasenv context t indURI indlen nn =
966 let rec aux context n nn h te =
967 match R.whd status ~subst context te with
968 | C.Rel m when m > n && m <= nn -> h
969 | C.Rel _ | C.Meta _ -> true
973 | C.Const (Ref.Ref (_,Ref.Ind _))
974 | C.LetIn _ -> raise (AssertFailure (lazy "17"))
975 | C.Lambda (name,so,de) ->
976 does_not_occur status ~subst context n nn so &&
977 aux ((name,C.Decl so)::context) (n + 1) (nn + 1) h de
978 | C.Appl ((C.Rel m)::tl) when m > n && m <= nn ->
979 h && List.for_all (does_not_occur status ~subst context n nn) tl
980 | C.Const (Ref.Ref (_,Ref.Con _)) -> true
981 | C.Appl (C.Const (Ref.Ref (_, Ref.Con (_,j,paramsno))) :: tl) as t ->
982 let ty_t = typeof status ~subst ~metasenv context t in
983 let dc_ctx, dcl, start, stop =
984 specialize_and_abstract_constrs status ~subst indURI indlen context ty_t in
985 let _, dc = List.nth dcl (j-1) in
987 prerr_endline (status#ppterm ~subst ~metasenv ~context:dc_ctx dc);
988 prerr_endline (status#ppcontext ~subst ~metasenv dc_ctx);
990 let rec_params = recursive_args status ~subst ~metasenv dc_ctx start stop dc in
991 let rec analyse_instantiated_type rec_spec args =
992 match rec_spec, args with
993 | h::rec_spec, he::args ->
994 aux context n nn h he && analyse_instantiated_type rec_spec args
996 | _ -> raise (AssertFailure (lazy
997 ("Too many args for constructor: " ^ String.concat " "
998 (List.map (fun x-> status#ppterm ~subst ~metasenv ~context x) args))))
1000 let _, args = HExtlib.split_nth paramsno tl in
1001 analyse_instantiated_type rec_params args
1002 | C.Appl ((C.Match (_,out,te,pl))::_)
1003 | C.Match (_,out,te,pl) as t ->
1004 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
1005 List.for_all (does_not_occur status ~subst context n nn) tl &&
1006 does_not_occur status ~subst context n nn out &&
1007 does_not_occur status ~subst context n nn te &&
1008 List.for_all (aux context n nn h) pl
1009 (* IMPOSSIBLE unsless we allow to pass cofix to other fix/cofix as we do for
1010 higher order fix in g_b_destructors.
1012 | C.Const (Ref.Ref (u,(Ref.Fix _| Ref.CoFix _)) as ref)
1013 | C.Appl(C.Const (Ref.Ref(u,(Ref.Fix _| Ref.CoFix _)) as ref) :: _) as t ->
1014 let tl = match t with C.Appl (_::tl) -> tl | _ -> [] in
1015 let fl,_,_ = E.get_checked_fixes_or_cofixes ref in
1016 let len = List.length fl in
1017 let tys = List.map (fun (_,n,_,ty,_) -> n, C.Decl ty) fl in
1018 List.for_all (does_not_occur status ~subst context n nn) tl &&
1020 (fun (_,_,_,_,bo) ->
1021 aux (context@tys) n nn h (debruijn status u len context bo))
1025 | C.Appl _ as t -> does_not_occur status ~subst context n nn t
1027 aux context 0 nn false t
1029 and recursive_args status ~subst ~metasenv context n nn te =
1030 match R.whd status ~subst context te with
1031 | C.Rel _ | C.Appl _ | C.Const _ -> []
1032 | C.Prod (name,so,de) ->
1033 (not (does_not_occur status ~subst context n nn so)) ::
1034 (recursive_args status ~subst ~metasenv
1035 ((name,(C.Decl so))::context) (n+1) (nn + 1) de)
1037 raise (AssertFailure (lazy ("recursive_args:" ^ status#ppterm ~subst
1038 ~metasenv ~context:[] t)))
1040 and get_new_safes status ~subst (context, recfuns, x as k) p rl =
1041 match R.whd status ~subst context p, rl with
1042 | C.Lambda (name,so,ta), b::tl ->
1043 let recfuns = (if b then [0,Safe] else []) @ recfuns in
1044 get_new_safes status ~subst
1045 (shift_k (name,(C.Decl so)) (context, recfuns, x)) ta tl
1046 | C.Meta _ as e, _ | e, [] -> e, k
1047 | _ -> raise (AssertFailure (lazy "Ill formed pattern"))
1049 and is_really_smaller status
1050 r_uri r_len ~subst ~metasenv (context, recfuns, x as k) te
1052 match R.whd status ~subst context te with
1053 | C.Rel m when is_safe m recfuns -> true
1054 | C.Lambda (name, s, t) ->
1055 is_really_smaller status r_uri r_len ~subst ~metasenv (shift_k (name,C.Decl s) k) t
1057 is_really_smaller status r_uri r_len ~subst ~metasenv k he
1058 | C.Appl [] | C.Implicit _ -> assert false
1060 | C.Match (Ref.Ref (_,Ref.Ind (isinductive,_,_)),_,term,pl) ->
1062 | C.Rel m | C.Appl (C.Rel m :: _ ) when is_safe m recfuns || m = x ->
1063 if not isinductive then
1064 List.for_all (is_really_smaller status r_uri r_len ~subst ~metasenv k) pl
1066 let ty = typeof status ~subst ~metasenv context term in
1067 let dc_ctx, dcl, start, stop =
1068 specialize_and_abstract_constrs status ~subst r_uri r_len context ty in
1071 let rl = recursive_args status ~subst ~metasenv dc_ctx start stop dc in
1072 let e, k = get_new_safes status ~subst k p rl in
1073 is_really_smaller status r_uri r_len ~subst ~metasenv k e)
1075 | _ -> List.for_all (is_really_smaller status r_uri r_len ~subst ~metasenv k) pl)
1078 and returns_a_coinductive status ~subst context ty =
1079 match R.whd status ~subst context ty with
1080 | C.Const (Ref.Ref (uri,Ref.Ind (false,_,_)) as ref)
1081 | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind (false,_,_)) as ref)::_) ->
1082 let _, _, itl, _, _ = E.get_checked_indtys status ref in
1083 Some (uri,List.length itl)
1084 | C.Prod (n,so,de) ->
1085 returns_a_coinductive status ~subst ((n,C.Decl so)::context) de
1088 and type_of_constant status ((Ref.Ref (uri,_)) as ref) =
1090 raise (TypeCheckerFailure (lazy "Inconsistent cached infos in reference"))
1092 match E.get_checked_obj status uri, ref with
1093 | (_,_,_,_,C.Inductive(isind1,lno1,tl,_)),Ref.Ref(_,Ref.Ind (isind2,i,lno2))->
1094 if isind1 <> isind2 || lno1 <> lno2 then error ();
1095 let _,_,arity,_ = List.nth tl i in arity
1096 | (_,_,_,_,C.Inductive (_,lno1,tl,_)), Ref.Ref (_,Ref.Con (i,j,lno2)) ->
1097 if lno1 <> lno2 then error ();
1098 let _,_,_,cl = List.nth tl i in
1099 let _,_,arity = List.nth cl (j-1) in
1101 | (_,_,_,_,C.Fixpoint (false,fl,_)), Ref.Ref (_,Ref.CoFix i) ->
1102 let _,_,_,arity,_ = List.nth fl i in
1104 | (_,h1,_,_,C.Fixpoint (true,fl,_)), Ref.Ref (_,Ref.Fix (i,recno2,h2)) ->
1105 let _,_,recno1,arity,_ = List.nth fl i in
1106 if h1 <> h2 || recno1 <> recno2 then error ();
1108 | (_,_,_,_,C.Constant (_,_,None,ty,_)), Ref.Ref (_,Ref.Decl) -> ty
1109 | (_,h1,_,_,C.Constant (_,_,Some _,ty,_)), Ref.Ref (_,Ref.Def h2) ->
1110 if h1 <> h2 then error ();
1113 raise (AssertFailure
1114 (lazy ("type_of_constant: environment/reference: " ^
1115 Ref.string_of_reference ref)))
1117 and get_relevance (status:#NCicEnvironment.status) ~metasenv ~subst context t args =
1118 let ty = typeof status ~subst ~metasenv context t in
1119 let rec aux context ty = function
1121 | arg::tl -> match R.whd status ~subst context ty with
1122 | C.Prod (_,so,de) ->
1123 let sort = typeof status ~subst ~metasenv context so in
1124 let new_ty = S.subst status ~avoid_beta_redexes:true arg de in
1125 (*prerr_endline ("so: " ^ status#ppterm ~subst ~metasenv:[]
1127 prerr_endline ("sort: " ^ status#ppterm ~subst ~metasenv:[]
1129 (match R.whd status ~subst context sort with
1131 false::(aux context new_ty tl)
1133 | C.Meta _ -> true::(aux context new_ty tl)
1134 | _ -> raise (TypeCheckerFailure (lazy (Printf.sprintf
1135 "Prod: the type %s of the source of %s is not a sort"
1136 (status#ppterm ~subst ~metasenv ~context sort)
1137 (status#ppterm ~subst ~metasenv ~context so)))))
1141 (lazy (Printf.sprintf
1142 "Appl: %s is not a function, it cannot be applied"
1143 (status#ppterm ~subst ~metasenv ~context
1144 (let res = List.length tl in
1145 let eaten = List.length args - res in
1148 (HExtlib.split_nth eaten args))))))))
1149 in aux context ty args
1152 let typecheck_context status ~metasenv ~subst context =
1158 _,C.Decl t -> ignore (typeof status ~metasenv ~subst:[] context t)
1159 | name,C.Def (te,ty) ->
1160 ignore (typeof status ~metasenv ~subst:[] context ty);
1161 let ty' = typeof status ~metasenv ~subst:[] context te in
1162 if not (R.are_convertible status ~metasenv ~subst context ty' ty) then
1163 raise (AssertFailure (lazy (Printf.sprintf (
1164 "the type of the definiens for %s in the context is not "^^
1165 "convertible with the declared one.\n"^^
1166 "inferred type:\n%s\nexpected type:\n%s")
1167 name (status#ppterm ~subst ~metasenv ~context ty')
1168 (status#ppterm ~subst ~metasenv ~context ty))))
1174 let typecheck_metasenv status metasenv =
1177 (fun metasenv (i,(_,context,ty) as conj) ->
1178 if List.mem_assoc i metasenv then
1179 raise (TypeCheckerFailure (lazy ("duplicate meta " ^ string_of_int i ^
1181 typecheck_context status ~metasenv ~subst:[] context;
1182 ignore (typeof status ~metasenv ~subst:[] context ty);
1187 let typecheck_subst status ~metasenv subst =
1190 (fun subst (i,(_,context,ty,bo) as conj) ->
1191 if List.mem_assoc i subst then
1192 raise (AssertFailure (lazy ("duplicate meta " ^ string_of_int i ^
1193 " in substitution")));
1194 if List.mem_assoc i metasenv then
1195 raise (AssertFailure (lazy ("meta " ^ string_of_int i ^
1196 " is both in the metasenv and in the substitution")));
1197 typecheck_context status ~metasenv ~subst context;
1198 ignore (typeof status ~metasenv ~subst context ty);
1199 let ty' = typeof status ~metasenv ~subst context bo in
1200 if not (R.are_convertible status ~metasenv ~subst context ty' ty) then
1201 raise (AssertFailure (lazy (Printf.sprintf (
1202 "the type of the definiens for %d in the substitution is not "^^
1203 "convertible with the declared one.\n"^^
1204 "inferred type:\n%s\nexpected type:\n%s")
1206 (status#ppterm ~subst ~metasenv ~context ty')
1207 (status#ppterm ~subst ~metasenv ~context ty))));
1212 let height_of_term status tl =
1214 let get_height (NReference.Ref (uri,_)) =
1215 let _,height,_,_,_ = NCicEnvironment.get_checked_obj status uri in
1219 NCic.Meta (_,(_,NCic.Ctx l)) -> List.iter aux l
1223 | NCic.Implicit _ -> assert false
1224 | NCic.Const nref -> h := max !h (get_height nref)
1225 | NCic.Prod (_,t1,t2)
1226 | NCic.Lambda (_,t1,t2) -> aux t1; aux t2
1227 | NCic.LetIn (_,s,ty,t) -> aux s; aux ty; aux t
1228 | NCic.Appl l -> List.iter aux l
1229 | NCic.Match (_,outty,t,pl) -> aux outty; aux t; List.iter aux pl
1235 let height_of_obj_kind status uri ~subst =
1238 | NCic.Constant (_,_,None,_,_)
1239 | NCic.Fixpoint (false,_,_) -> 0
1240 | NCic.Fixpoint (true,ifl,_) ->
1241 let iflno = List.length ifl in
1242 height_of_term status
1244 (fun l (_,_,_,ty,bo) ->
1245 let bo = debruijn status uri iflno [] ~subst bo in
1248 | NCic.Constant (_,_,Some bo,ty,_) -> height_of_term status [bo;ty]
1251 let typecheck_obj status (uri,height,metasenv,subst,kind) =
1252 (*height must be checked since it is not only an optimization during reduction*)
1253 let iheight = height_of_obj_kind status uri ~subst kind in
1254 if height <> iheight then
1255 raise (TypeCheckerFailure (lazy (Printf.sprintf
1256 "the declared object height (%d) is not the inferred one (%d)"
1258 typecheck_metasenv status metasenv;
1259 typecheck_subst status ~metasenv subst;
1261 | C.Constant (relevance,_,Some te,ty,_) ->
1262 let _ = typeof status ~subst ~metasenv [] ty in
1263 let ty_te = typeof status ~subst ~metasenv [] te in
1264 if not (R.are_convertible status ~metasenv ~subst [] ty_te ty) then
1265 raise (TypeCheckerFailure (lazy (Printf.sprintf (
1266 "the type of the body is not convertible with the declared one.\n"^^
1267 "inferred type:\n%s\nexpected type:\n%s")
1268 (status#ppterm ~subst ~metasenv ~context:[] ty_te)
1269 (status#ppterm ~subst ~metasenv ~context:[] ty))));
1270 check_relevance status ~subst ~metasenv [] relevance ty
1271 (*check_relevance status ~in_type:false ~subst ~metasenv relevance te*)
1272 | C.Constant (relevance,_,None,ty,_) ->
1273 ignore (typeof status ~subst ~metasenv [] ty);
1274 check_relevance status ~subst ~metasenv [] relevance ty
1275 | C.Inductive (_, leftno, tyl, _) ->
1276 check_mutual_inductive_defs status uri ~metasenv ~subst leftno tyl
1277 | C.Fixpoint (inductive,fl,_) ->
1280 (fun (types,kl) (relevance,name,k,ty,_) ->
1281 let _ = typeof status ~subst ~metasenv [] ty in
1282 check_relevance status ~subst ~metasenv [] relevance ty;
1283 ((name,C.Decl ty)::types, k::kl)
1286 let len = List.length types in
1288 List.split (List.map2
1289 (fun (_,_,_,_,bo) rno ->
1290 let dbo = debruijn status uri len [] ~subst bo in
1294 List.iter2 (fun (_,_,x,ty,_) bo ->
1295 let ty_bo = typeof status ~subst ~metasenv types bo in
1296 if not (R.are_convertible status ~metasenv ~subst types ty_bo ty)
1297 then raise (TypeCheckerFailure (lazy ("(Co)Fix: ill-typed bodies")))
1299 if inductive then begin
1300 let m, context = eat_lambdas status ~subst ~metasenv types (x + 1) bo in
1303 match List.hd context with _,C.Decl t -> t | _ -> assert false
1305 match R.whd status ~subst (List.tl context) he with
1306 | C.Const (Ref.Ref (uri,Ref.Ind _) as ref)
1307 | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind _) as ref) :: _) ->
1308 let _,_,itl,_,_ = E.get_checked_indtys status ref in
1309 uri, List.length itl
1311 raise (TypeCheckerFailure
1312 (lazy "Fix: the recursive argument is not inductive"))
1314 (* guarded by destructors conditions D{f,k,x,M} *)
1315 let rec enum_from k =
1316 function [] -> [] | v::tl -> (k,v)::enum_from (k+1) tl
1318 guarded_by_destructors status r_uri r_len
1319 ~subst ~metasenv context (enum_from (x+2) kl) m
1321 match returns_a_coinductive status ~subst [] ty with
1323 raise (TypeCheckerFailure
1324 (lazy "CoFix: does not return a coinductive type"))
1325 | Some (r_uri, r_len) ->
1326 (* guarded by constructors conditions C{f,M} *)
1328 (guarded_by_constructors status ~subst ~metasenv types bo r_uri r_len len)
1330 raise (TypeCheckerFailure
1331 (lazy "CoFix: not guarded by constructors"))
1337 let trust = ref (fun _ -> false);;
1338 let set_trust f = trust := f
1339 let trust_obj obj = !trust obj
1342 (* web interface stuff *)
1345 ref (function (`Start_type_checking _|`Type_checking_completed _|`Type_checking_interrupted _|`Type_checking_failed _|`Trust_obj _) -> ())
1348 let set_logger f = logger := f;;
1350 let typecheck_obj status obj =
1351 let u,_,_,_,_ = obj in
1353 !logger (`Start_type_checking u);
1354 typecheck_obj status obj;
1355 !logger (`Type_checking_completed u)
1358 !logger (`Type_checking_interrupted u);
1361 !logger (`Type_checking_failed u);
1367 if trust_obj obj then
1368 let u,_,_,_,_ = obj in
1369 !logger (`Trust_obj u)
1371 typecheck_obj status obj)
1374 let _ = NCicReduction.set_get_relevance get_relevance;;
1376 let indent = ref 0;;
1379 let do_indent () = String.make !indent ' ' in
1381 | `Start_type_checking s ->
1383 prerr_endline (do_indent () ^ "Start: " ^ NUri.string_of_uri s);
1385 | `Type_checking_completed s ->
1388 prerr_endline (do_indent () ^ "End: " ^ NUri.string_of_uri s)
1389 | `Type_checking_interrupted s ->
1392 prerr_endline (do_indent () ^ "Break: " ^ NUri.string_of_uri s)
1393 | `Type_checking_failed s ->
1396 prerr_endline (do_indent () ^ "Fail: " ^ NUri.string_of_uri s)
1399 prerr_endline (do_indent () ^ "Trust: " ^ NUri.string_of_uri s))
1401 (* let _ = set_logger logger ;; *)