+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 (HsKindSig ty k)
+ = kcHsType ty `thenTc` \ k' ->
+ unifyKind k k' `thenTc_`
+ returnTc k
+
+kcHsType (HsListTy ty)
+ = kcLiftedType ty `thenTc` \ tau_ty ->
+ returnTc liftedTypeKind
+
+kcHsType (HsPArrTy 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 (HsOpTy ty1 HsArrow ty2)
+ = kcTypeType ty1 `thenTc_`
+ kcTypeType ty2 `thenTc_`
+ returnTc liftedTypeKind
+
+kcHsType ty@(HsOpTy ty1 (HsTyOp 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 (HsParTy ty) -- Skip parentheses markers
+ = kcHsType ty
+
+kcHsType (HsNumTy _) -- The unit type for generics
+ = returnTc liftedTypeKind
+
+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
+
+
+---------------------------
+kc_pred :: RenamedHsPred -> TcM TcKind -- Does *not* check for a saturated
+ -- application (reason: used from TcDeriv)
+kc_pred pred@(HsIParam name ty)
+ = kcHsType ty
+
+kc_pred pred@(HsClassP cls tys)
+ = kcClass cls `thenTc` \ kind ->
+ mapTc kcHsType tys `thenTc` \ arg_kinds ->
+ newKindVar `thenNF_Tc` \ kv ->
+ unifyKind kind (mkArrowKinds arg_kinds kv) `thenTc_`
+ returnTc kv
+
+---------------------------
+kcHsContext ctxt = mapTc_ kcHsPred ctxt
+
+kcHsPred pred -- Checks that the result is of kind liftedType
+ = tcAddErrCtxt (appKindCtxt (ppr pred)) $
+ kc_pred pred `thenTc` \ kind ->
+ unifyKind liftedTypeKind kind `thenTc_`
+ returnTc ()