+kcHsTyVars tvs = mapNF_Tc kcHsTyVar tvs
+
+newNamedKindVar name = newKindVar `thenNF_Tc` \ kind ->
+ returnNF_Tc (name, kind)
+
+---------------------------
+kcLiftedType :: RenamedHsType -> TcM ()
+ -- The type ty must be a *lifted* *type*
+kcLiftedType ty
+ = kcHsType ty `thenTc` \ kind ->
+ tcAddErrCtxt (typeKindCtxt ty) $
+ unifyKind liftedTypeKind kind
+
+---------------------------
+kcTypeType :: RenamedHsType -> TcM ()
+ -- The type ty must be a *type*, but it can be lifted or unlifted.
+kcTypeType ty
+ = kcHsType ty `thenTc` \ kind ->
+ tcAddErrCtxt (typeKindCtxt ty) $
+ unifyOpenTypeKind kind
+
+---------------------------
+kcHsSigType, kcHsLiftedSigType :: RenamedHsType -> TcM ()
+ -- Used for type signatures
+kcHsSigType = kcTypeType
+kcHsSigTypes tys = mapTc_ kcHsSigType tys
+kcHsLiftedSigType = kcLiftedType
+
+---------------------------
+kcHsType :: RenamedHsType -> TcM TcKind
+kcHsType (HsTyVar name) = kcTyVar name
+
+kcHsType (HsListTy ty)
+ = kcLiftedType ty `thenTc` \ tau_ty ->
+ returnTc liftedTypeKind
+
+kcHsType (HsTupleTy (HsTupCon _ boxity _) tys)
+ = mapTc kcTypeType tys `thenTc_`
+ returnTc (case boxity of
+ Boxed -> liftedTypeKind
+ Unboxed -> unliftedTypeKind)
+
+kcHsType (HsFunTy ty1 ty2)
+ = kcTypeType ty1 `thenTc_`
+ kcTypeType ty2 `thenTc_`
+ returnTc liftedTypeKind
+
+kcHsType ty@(HsOpTy ty1 op ty2)
+ = kcTyVar op `thenTc` \ op_kind ->
+ kcHsType ty1 `thenTc` \ ty1_kind ->
+ kcHsType ty2 `thenTc` \ ty2_kind ->
+ tcAddErrCtxt (appKindCtxt (ppr ty)) $
+ kcAppKind op_kind ty1_kind `thenTc` \ op_kind' ->
+ kcAppKind op_kind' ty2_kind
+
+kcHsType (HsPredTy pred)
+ = kcHsPred pred `thenTc_`
+ returnTc liftedTypeKind
+
+kcHsType ty@(HsAppTy ty1 ty2)
+ = kcHsType ty1 `thenTc` \ tc_kind ->
+ kcHsType ty2 `thenTc` \ arg_kind ->
+ tcAddErrCtxt (appKindCtxt (ppr ty)) $
+ kcAppKind tc_kind arg_kind
+
+kcHsType (HsForAllTy (Just tv_names) context ty)
+ = kcHsTyVars tv_names `thenNF_Tc` \ kind_env ->
+ tcExtendKindEnv kind_env $
+ kcHsContext context `thenTc_`
+ kcHsType ty `thenTc_`
+ returnTc liftedTypeKind
+
+---------------------------
+kcAppKind fun_kind arg_kind
+ = case tcSplitFunTy_maybe fun_kind of
+ Just (arg_kind', res_kind)
+ -> unifyKind arg_kind arg_kind' `thenTc_`
+ returnTc res_kind
+
+ Nothing -> newKindVar `thenNF_Tc` \ res_kind ->
+ unifyKind fun_kind (mkArrowKind arg_kind res_kind) `thenTc_`
+ returnTc res_kind
+
+
+---------------------------
+kcHsContext ctxt = mapTc_ kcHsPred ctxt
+
+kcHsPred :: RenamedHsPred -> TcM ()
+kcHsPred pred@(HsIParam name ty)
+ = tcAddErrCtxt (appKindCtxt (ppr pred)) $
+ kcLiftedType ty
+
+kcHsPred pred@(HsClassP cls tys)
+ = tcAddErrCtxt (appKindCtxt (ppr pred)) $
+ kcClass cls `thenTc` \ kind ->
+ mapTc kcHsType tys `thenTc` \ arg_kinds ->
+ unifyKind kind (mkArrowKinds arg_kinds liftedTypeKind)
+
+ ---------------------------
+kcTyVar name -- Could be a tyvar or a tycon
+ = tcLookup name `thenTc` \ thing ->
+ case thing of
+ AThing kind -> returnTc kind
+ ATyVar tv -> returnTc (tyVarKind tv)
+ AGlobal (ATyCon tc) -> returnTc (tyConKind tc)
+ other -> failWithTc (wrongThingErr "type" thing name)
+
+kcClass cls -- Must be a class
+ = tcLookup cls `thenNF_Tc` \ thing ->
+ case thing of
+ AThing kind -> returnTc kind
+ AGlobal (AClass cls) -> returnTc (tyConKind (classTyCon cls))
+ other -> failWithTc (wrongThingErr "class" thing cls)
+\end{code}