+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 te args =
+ match te with
+ | C.Const r when List.length (E.get_relevance r) >= List.length args ->
+ let relevance = E.get_relevance r in
+ (match r with
+ | Ref.Ref (_,Ref.Con (_,_,lno)) ->
+ let _,relevance = HExtlib.split_nth lno relevance in
+ HExtlib.mk_list false lno @ relevance
+ | _ -> relevance)
+ | t ->
+ 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 (name,so,de) ->
+ let sort = typeof ~subst ~metasenv:[] context so in
+ let new_ty = S.subst ~avoid_beta_redexes:true arg de in
+ (match R.whd ~subst context sort with
+ | C.Sort C.Prop ->
+ false::(aux ((name,(C.Decl so))::context) new_ty tl)
+ | C.Sort _
+ | C.Meta _ -> true::(aux ((name,(C.Decl so))::context) de 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 check_rel1_irrelevant ~metasenv ~subst context = fun _ -> ();;
+(* let shift e (k, context) = k+1,e::context in
+ let rec aux (evil, context as k) () t =
+ match R.whd ~subst context t with
+ | C.Rel i when i = evil -> (*
+ raise (TypeCheckerFailure (lazy (Printf.sprintf
+ "Argument %s declared as irrelevante is used in a relevant position"
+ (PP.ppterm ~subst ~metasenv ~context (C.Rel i))))) *) ()
+ | C.Meta _ -> ()
+ | C.Lambda (name,so,tgt) ->
+ (* checking so is not needed since the implicit version of CC
+ * has untyped lambdas (curry style), see Barras and Bernardo *)
+ aux (shift (name,C.Decl so) k) () tgt
+ | C.Appl (C.Const ref::args) ->
+ let relevance = NCicEnvironment.get_relevance ref in
+ HExtlib.list_iter_default2
+ (fun t -> function false -> () | _ -> aux k () t)
+ args true relevance
+ | C.Match (_, _, _, []) -> ()
+ | C.Match (ref, _, t, [p]) ->
+ aux k () p;
+ let _,lno,itl,_,_ = E.get_checked_indtys ref in
+ let _,_,_,cl = List.hd itl in
+ let _,_,c = List.hd cl in
+ if not (is_non_informative lno c) then aux k () t
+ | C.Match (_, _, t, pl) -> List.iter (aux k ()) (t::pl)
+ | t -> U.fold shift k aux () t
+ in
+ aux (1, context) () *)
+
+let check_relevance ~subst ~metasenv relevance ty =
+ let error () =
+ raise (TypeCheckerFailure
+ (lazy ("Wrong relevance declaration: " ^
+ String.concat "," (List.map string_of_bool relevance)^
+ "\nfor type: "^PP.ppterm ~metasenv ~subst ~context:[] ty)))