+ kc_app (HsAppTy f a) as = kc_app f (a:as)
+ kc_app f as = kcHsType f `thenM` \ fk ->
+ kcApps (ppr f) fk as
+
+kcHsType (HsForAllTy (Just tv_names) context ty)
+ = kcHsTyVars tv_names `thenM` \ kind_env ->
+ tcExtendKindEnv kind_env $
+ kcHsContext context `thenM_`
+ kcLiftedType ty
+ -- The body of a forall must be of kind *
+ -- In principle, I suppose, we could allow unlifted types,
+ -- but it seems simpler to stick to lifted types for now.
+
+---------------------------
+kcApps :: SDoc -- The function
+ -> TcKind -- Function kind
+ -> [RenamedHsType] -- Arg types
+ -> TcM TcKind -- Result kind
+kcApps pp_fun fun_kind args
+ = go fun_kind args
+ where
+ go fk [] = returnM fk
+ go fk (ty:tys) = unifyFunKind fk `thenM` \ mb_fk ->
+ case mb_fk of {
+ Nothing -> failWithTc too_few_args ;
+ Just (ak',fk') ->
+ kcHsType ty `thenM` \ ak ->
+ checkExpectedKind (ppr ty) ak ak' `thenM_`
+ go fk' tys }
+
+ too_few_args = ptext SLIT("Kind error:") <+> quotes pp_fun <+>
+ ptext SLIT("is applied to too many type arguments")
+
+---------------------------
+-- We would like to get a decent error message from
+-- (a) Under-applied type constructors
+-- f :: (Maybe, Maybe)
+-- (b) Over-applied type constructors
+-- f :: Int x -> Int x
+--
+
+checkExpectedKind :: SDoc -> TcKind -> TcKind -> TcM TcKind
+-- A fancy wrapper for 'unifyKind', which tries to give
+-- decent error messages.
+-- Returns the same kind that it is passed, exp_kind
+checkExpectedKind pp_ty act_kind exp_kind
+ | act_kind `eqKind` exp_kind -- Short cut for a very common case
+ = returnM exp_kind
+ | otherwise
+ = tryTc (unifyKind exp_kind act_kind) `thenM` \ (errs, mb_r) ->
+ case mb_r of {
+ Just _ -> returnM exp_kind ; -- Unification succeeded
+ Nothing ->
+
+ -- So there's definitely an error
+ -- Now to find out what sort
+ zonkTcType exp_kind `thenM` \ exp_kind ->
+ zonkTcType act_kind `thenM` \ act_kind ->
+
+ let (exp_as, _) = Type.splitFunTys exp_kind
+ (act_as, _) = Type.splitFunTys act_kind
+ -- Use the Type versions for kinds
+ n_exp_as = length exp_as
+ n_act_as = length act_as
+
+ err | n_exp_as < n_act_as -- E.g. [Maybe]
+ = quotes pp_ty <+> ptext SLIT("is not applied to enough type arguments")
+
+ -- Now n_exp_as >= n_act_as. In the next two cases,
+ -- n_exp_as == 0, and hence so is n_act_as
+ | exp_kind `eqKind` liftedTypeKind && act_kind `eqKind` unliftedTypeKind
+ = ptext SLIT("Expecting a lifted type, but") <+> quotes pp_ty
+ <+> ptext SLIT("is unlifted")
+
+ | exp_kind `eqKind` unliftedTypeKind && act_kind `eqKind` liftedTypeKind
+ = ptext SLIT("Expecting an unlifted type, but") <+> quotes pp_ty
+ <+> ptext SLIT("is lifted")
+
+ | otherwise -- E.g. Monad [Int]
+ = sep [ ptext SLIT("Expecting kind") <+> quotes (ppr exp_kind) <> comma,
+ ptext SLIT("but") <+> quotes pp_ty <+>
+ ptext SLIT("has kind") <+> quotes (ppr act_kind)]
+ in
+ failWithTc (ptext SLIT("Kind error:") <+> err)
+ }
+
+---------------------------
+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 `thenM` \ kind ->
+ kcApps (ppr cls) kind tys
+
+---------------------------
+kcHsContext ctxt = mappM_ kcHsPred ctxt
+
+kcHsPred pred -- Checks that the result is of kind liftedType
+ = addErrCtxt (appKindCtxt (ppr pred)) $
+ kc_pred pred `thenM` \ kind ->
+ checkExpectedKind (ppr pred) kind liftedTypeKind
+
+
+ ---------------------------
+kcTyVar name -- Could be a tyvar or a tycon
+ = tcLookup name `thenM` \ thing ->
+ case thing of
+ AThing kind -> returnM kind
+ ATyVar tv -> returnM (tyVarKind tv)
+ AGlobal (ATyCon tc) -> returnM (tyConKind tc)
+ other -> failWithTc (wrongThingErr "type" thing name)
+
+kcClass cls -- Must be a class
+ = tcLookup cls `thenM` \ thing ->
+ case thing of
+ AThing kind -> returnM kind
+ AGlobal (AClass cls) -> returnM (tyConKind (classTyCon cls))
+ other -> failWithTc (wrongThingErr "class" thing cls)
+\end{code}
+
+%************************************************************************
+%* *
+\subsection{tc_type}
+%* *
+%************************************************************************
+
+tc_type, the main work horse
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ -------------------
+ *** BIG WARNING ***
+ -------------------
+
+tc_type is used to typecheck the types in the RHS of data
+constructors. In the case of recursive data types, that means that
+the type constructors themselves are (partly) black holes. e.g.
+
+ data T a = MkT a [T a]
+
+While typechecking the [T a] on the RHS, T itself is not yet fully
+defined. That in turn places restrictions on what you can check in
+tcHsType; if you poke on too much you get a black hole. I keep
+forgetting this, hence this warning!
+
+So tc_type does no validity-checking. Instead that's all done
+by TcMType.checkValidType
+
+ --------------------------
+ *** END OF BIG WARNING ***
+ --------------------------
+
+
+\begin{code}
+tc_type :: RenamedHsType -> TcM Type
+
+tc_type ty@(HsTyVar name)
+ = tc_app ty []
+
+tc_type (HsKindSig ty k)
+ = tc_type ty -- Kind checking done already
+
+tc_type (HsListTy ty)
+ = tc_type ty `thenM` \ tau_ty ->
+ returnM (mkListTy tau_ty)
+
+tc_type (HsPArrTy ty)
+ = tc_type ty `thenM` \ tau_ty ->
+ returnM (mkPArrTy tau_ty)
+
+tc_type (HsTupleTy (HsTupCon boxity arity) tys)
+ = ASSERT( tys `lengthIs` arity )
+ tc_types tys `thenM` \ tau_tys ->
+ returnM (mkTupleTy boxity arity tau_tys)
+
+tc_type (HsFunTy ty1 ty2)
+ = tc_type ty1 `thenM` \ tau_ty1 ->
+ tc_type ty2 `thenM` \ tau_ty2 ->
+ returnM (mkFunTy tau_ty1 tau_ty2)