+ let ty = typeof ~subst ~metasenv context term in
+ let dc_ctx, dcl, start, stop =
+ specialize_and_abstract_constrs ~subst r_uri r_len context ty in
+ List.for_all2
+ (fun p (_,dc) ->
+ let rl = recursive_args ~subst ~metasenv dc_ctx start stop dc in
+ let e, k = get_new_safes ~subst k p rl in
+ is_really_smaller r_uri r_len ~subst ~metasenv k e)
+ pl dcl
+ | _ -> List.for_all (is_really_smaller r_uri r_len ~subst ~metasenv k) pl)
+ | _ -> assert false
+
+and returns_a_coinductive ~subst context ty =
+ match R.whd ~subst context ty with
+ | C.Const (Ref.Ref (uri,Ref.Ind (false,_,_)) as ref)
+ | C.Appl (C.Const (Ref.Ref (uri,Ref.Ind (false,_,_)) as ref)::_) ->
+ let _, _, itl, _, _ = E.get_checked_indtys ref in
+ Some (uri,List.length itl)
+ | C.Prod (n,so,de) ->
+ returns_a_coinductive ~subst ((n,C.Decl so)::context) de
+ | _ -> None
+
+and type_of_constant ((Ref.Ref (uri,_)) as ref) =
+ let error () =
+ raise (TypeCheckerFailure (lazy "Inconsistent cached infos in reference"))
+ in
+ match E.get_checked_obj uri, ref with
+ | (_,_,_,_,C.Inductive(isind1,lno1,tl,_)),Ref.Ref(_,Ref.Ind (isind2,i,lno2))->
+ if isind1 <> isind2 || lno1 <> lno2 then error ();
+ let _,_,arity,_ = List.nth tl i in arity
+ | (_,_,_,_,C.Inductive (_,lno1,tl,_)), Ref.Ref (_,Ref.Con (i,j,lno2)) ->
+ if lno1 <> lno2 then error ();
+ let _,_,_,cl = List.nth tl i in
+ let _,_,arity = List.nth cl (j-1) in
+ arity
+ | (_,_,_,_,C.Fixpoint (false,fl,_)), Ref.Ref (_,Ref.CoFix i) ->
+ let _,_,_,arity,_ = List.nth fl i in
+ arity
+ | (_,h1,_,_,C.Fixpoint (true,fl,_)), Ref.Ref (_,Ref.Fix (i,recno2,h2)) ->
+ let _,_,recno1,arity,_ = List.nth fl i in
+ if h1 <> h2 || recno1 <> recno2 then error ();
+ arity
+ | (_,_,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Decl) -> ty
+ | (_,h1,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Def h2) ->
+ if h1 <> h2 then error ();
+ ty
+ | _ -> raise (AssertFailure (lazy "type_of_constant: environment/reference"))
+
+and get_relevance ~subst context t args =
+ let ty = typeof ~subst ~metasenv:[] context t in
+ let rec aux context ty = function
+ | [] -> []
+ | arg::tl -> match R.whd ~subst context ty with
+ | C.Prod (_,so,de) ->
+ let sort = typeof ~subst ~metasenv:[] context so in
+ let new_ty = S.subst ~avoid_beta_redexes:true arg de in
+ (*prerr_endline ("so: " ^ PP.ppterm ~subst ~metasenv:[]
+ ~context so);
+ prerr_endline ("sort: " ^ PP.ppterm ~subst ~metasenv:[]
+ ~context sort);*)
+ (match R.whd ~subst context sort with
+ | C.Sort C.Prop ->
+ false::(aux context new_ty tl)
+ | C.Sort _
+ | C.Meta _ -> true::(aux context new_ty tl)
+ | _ -> raise (TypeCheckerFailure (lazy (Printf.sprintf
+ "Prod: the type %s of the source of %s is not a sort"
+ (PP.ppterm ~subst ~metasenv:[] ~context sort)
+ (PP.ppterm ~subst ~metasenv:[] ~context so)))))
+ | _ ->
+ raise
+ (TypeCheckerFailure
+ (lazy (Printf.sprintf
+ "Appl: %s is not a function, it cannot be applied"
+ (PP.ppterm ~subst ~metasenv:[] ~context
+ (let res = List.length tl in
+ let eaten = List.length args - res in
+ (C.Appl
+ (t::fst
+ (HExtlib.split_nth eaten args))))))))
+ in aux context ty args
+;;
+
+let typecheck_context ~metasenv ~subst context =
+ ignore
+ (List.fold_right
+ (fun d context ->
+ begin
+ match d with
+ _,C.Decl t -> ignore (typeof ~metasenv ~subst:[] context t)
+ | name,C.Def (te,ty) ->
+ ignore (typeof ~metasenv ~subst:[] context ty);
+ let ty' = typeof ~metasenv ~subst:[] context te in
+ if not (R.are_convertible ~subst get_relevance context ty' ty) then
+ raise (AssertFailure (lazy (Printf.sprintf (
+ "the type of the definiens for %s in the context is not "^^
+ "convertible with the declared one.\n"^^
+ "inferred type:\n%s\nexpected type:\n%s")
+ name (PP.ppterm ~subst ~metasenv ~context ty')
+ (PP.ppterm ~subst ~metasenv ~context ty))))
+ end;
+ d::context
+ ) context [])
+;;
+
+let typecheck_metasenv metasenv =
+ ignore
+ (List.fold_left
+ (fun metasenv (i,(_,context,ty) as conj) ->
+ if List.mem_assoc i metasenv then
+ raise (TypeCheckerFailure (lazy ("duplicate meta " ^ string_of_int i ^
+ " in metasenv")));
+ typecheck_context ~metasenv ~subst:[] context;
+ ignore (typeof ~metasenv ~subst:[] context ty);
+ metasenv @ [conj]
+ ) [] metasenv)
+;;
+
+let typecheck_subst ~metasenv subst =
+ ignore
+ (List.fold_left
+ (fun subst (i,(_,context,ty,bo) as conj) ->
+ if List.mem_assoc i subst then
+ raise (AssertFailure (lazy ("duplicate meta " ^ string_of_int i ^
+ " in substitution")));
+ if List.mem_assoc i metasenv then
+ raise (AssertFailure (lazy ("meta " ^ string_of_int i ^
+ " is both in the metasenv and in the substitution")));
+ typecheck_context ~metasenv ~subst context;
+ ignore (typeof ~metasenv ~subst context ty);
+ let ty' = typeof ~metasenv ~subst context bo in
+ if not (R.are_convertible ~subst get_relevance context ty' ty) then
+ raise (AssertFailure (lazy (Printf.sprintf (
+ "the type of the definiens for %d in the substitution is not "^^
+ "convertible with the declared one.\n"^^
+ "inferred type:\n%s\nexpected type:\n%s")
+ i
+ (PP.ppterm ~subst ~metasenv ~context ty')
+ (PP.ppterm ~subst ~metasenv ~context ty))));
+ subst @ [conj]
+ ) [] subst)
+;;
+
+
+let typecheck_obj (uri,_height,metasenv,subst,kind) =
+ (* height is not checked since it is only used to implement an optimization *)
+ typecheck_metasenv metasenv;
+ typecheck_subst ~metasenv subst;
+ match kind with
+ | C.Constant (relevance,_,Some te,ty,_) ->
+ let _ = typeof ~subst ~metasenv [] ty in
+ let ty_te = typeof ~subst ~metasenv [] te in
+ if not (R.are_convertible ~subst get_relevance [] ty_te ty) then
+ raise (TypeCheckerFailure (lazy (Printf.sprintf (
+ "the type of the body is not convertible with the declared one.\n"^^
+ "inferred type:\n%s\nexpected type:\n%s")
+ (PP.ppterm ~subst ~metasenv ~context:[] ty_te)
+ (PP.ppterm ~subst ~metasenv ~context:[] ty))));
+ check_relevance ~subst ~metasenv [] relevance ty
+ (*check_relevance ~in_type:false ~subst ~metasenv relevance te*)
+ | C.Constant (relevance,_,None,ty,_) ->
+ ignore (typeof ~subst ~metasenv [] ty);
+ check_relevance ~subst ~metasenv [] relevance ty
+ | C.Inductive (_, leftno, tyl, _) ->
+ check_mutual_inductive_defs uri ~metasenv ~subst leftno tyl
+ | C.Fixpoint (inductive,fl,_) ->
+ let types, kl =