X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcHsType.lhs;h=e158763e78ae73de4d65c5050c41ad57e53f58ff;hb=678086d0d26108713d1c361ab07d5ae12e24f363;hp=66102a64dfd36cd5a9d89db9349cca099d5bd4a3;hpb=f59d6c9d6ead47a61681b1086b313c2fad225912;p=ghc-hetmet.git diff --git a/compiler/typecheck/TcHsType.lhs b/compiler/typecheck/TcHsType.lhs index 66102a6..e158763 100644 --- a/compiler/typecheck/TcHsType.lhs +++ b/compiler/typecheck/TcHsType.lhs @@ -5,13 +5,6 @@ \section[TcMonoType]{Typechecking user-specified @MonoTypes@} \begin{code} -{-# OPTIONS -w #-} --- The above warning supression flag is a temporary kludge. --- While working on this module you are encouraged to remove it and fix --- any warnings in the module. See --- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings --- for details - module TcHsType ( tcHsSigType, tcHsDeriv, tcHsInstHead, tcHsQuantifiedType, @@ -42,6 +35,7 @@ import TcIface import TcType import {- Kind parts of -} Type import Var +import Coercion import TyCon import Class import Name @@ -53,6 +47,7 @@ import BasicTypes import SrcLoc import UniqSupply import Outputable +import FastString import Control.Monad \end{code} @@ -172,13 +167,15 @@ tcHsQuantifiedType tv_names hs_ty ; return (tvs, ty) } } -- Used for the deriving(...) items -tcHsDeriv :: LHsType Name -> TcM ([TyVar], Class, [Type]) -tcHsDeriv = addLocM (tc_hs_deriv []) +tcHsDeriv :: HsType Name -> TcM ([TyVar], Class, [Type]) +tcHsDeriv = tc_hs_deriv [] +tc_hs_deriv :: [LHsTyVarBndr Name] -> HsType Name + -> TcM ([TyVar], Class, [Type]) tc_hs_deriv tv_names (HsPredTy (HsClassP cls_name hs_tys)) = kcHsTyVars tv_names $ \ tv_names' -> do { cls_kind <- kcClass cls_name - ; (tys, res_kind) <- kcApps cls_kind (ppr cls_name) hs_tys + ; (tys, _res_kind) <- kcApps cls_kind (ppr cls_name) hs_tys ; tcTyVarBndrs tv_names' $ \ tyvars -> do { arg_tys <- dsHsTypes tys ; cls <- tcLookupClass cls_name @@ -191,7 +188,7 @@ tc_hs_deriv tv_names1 (HsForAllTy _ tv_names2 (L _ []) (L _ ty)) tc_hs_deriv (tv_names1 ++ tv_names2) ty tc_hs_deriv _ other - = failWithTc (ptext SLIT("Illegal deriving item") <+> ppr other) + = failWithTc (ptext (sLit "Illegal deriving item") <+> ppr other) \end{code} These functions are used during knot-tying in @@ -213,8 +210,8 @@ tcHsKindedType hs_ty = dsHsType hs_ty tcHsBangType :: LHsType Name -> TcM Type -- Permit a bang, but discard it -tcHsBangType (L span (HsBangTy b ty)) = tcHsKindedType ty -tcHsBangType ty = tcHsKindedType ty +tcHsBangType (L _ (HsBangTy _ ty)) = tcHsKindedType ty +tcHsBangType ty = tcHsKindedType ty tcHsKindedContext :: LHsContext Name -> TcM ThetaType -- Used when we are expecting a ClassContext (i.e. no implicit params) @@ -259,7 +256,7 @@ kcCheckHsType (L span ty) exp_kind ; return (L span ty') } where -- Wrap a context around only if we want to show that contexts. - add_ctxt (HsPredTy p) thing = thing + add_ctxt (HsPredTy _) thing = thing -- Omit invisble ones and ones user's won't grok (HsPred p). add_ctxt (HsForAllTy _ _ (L _ []) _) thing = thing -- Omit wrapping if the theta-part is empty @@ -293,6 +290,7 @@ kcHsType ty = wrapLocFstM kc_hs_type ty -- -- The translated type has explicitly-kinded type-variable binders +kc_hs_type :: HsType Name -> TcM (HsType Name, TcKind) kc_hs_type (HsParTy ty) = do (ty', kind) <- kcHsType ty return (HsParTy ty', kind) @@ -329,12 +327,12 @@ kc_hs_type (HsFunTy ty1 ty2) = do ty2' <- kcTypeType ty2 return (HsFunTy ty1' ty2', liftedTypeKind) -kc_hs_type ty@(HsOpTy ty1 op ty2) = do +kc_hs_type (HsOpTy ty1 op ty2) = do op_kind <- addLocM kcTyVar op ([ty1',ty2'], res_kind) <- kcApps op_kind (ppr op) [ty1,ty2] return (HsOpTy ty1' op ty2', res_kind) -kc_hs_type ty@(HsAppTy ty1 ty2) = do +kc_hs_type (HsAppTy ty1 ty2) = do (fun_ty', fun_kind) <- kcHsType fun_ty ((arg_ty':arg_tys'), res_kind) <- kcApps fun_kind (ppr fun_ty) arg_tys return (foldl mk_app (HsAppTy fun_ty' arg_ty') arg_tys', res_kind) @@ -346,7 +344,7 @@ kc_hs_type ty@(HsAppTy ty1 ty2) = do -- the application; they are -- never used -kc_hs_type ty@(HsPredTy (HsEqualP _ _)) +kc_hs_type (HsPredTy (HsEqualP _ _)) = wrongEqualityErr kc_hs_type (HsPredTy pred) = do @@ -373,7 +371,7 @@ kc_hs_type (HsBangTy b ty) = do return (HsBangTy b ty', kind) kc_hs_type ty@(HsSpliceTy _) - = failWithTc (ptext SLIT("Unexpected type splice:") <+> ppr ty) + = failWithTc (ptext (sLit "Unexpected type splice:") <+> ppr ty) -- remove the doc nodes here, no need to worry about the location since -- its the same for a doc node and it's child type node @@ -399,8 +397,8 @@ kcApps fun_kind ppr_fun args = do kc_arg arg arg_kind = kcCheckHsType arg arg_kind - too_many_args = ptext SLIT("Kind error:") <+> quotes ppr_fun <+> - ptext SLIT("is applied to too many type arguments") + too_many_args = ptext (sLit "Kind error:") <+> quotes ppr_fun <+> + ptext (sLit "is applied to too many type arguments") --------------------------- kcHsContext :: LHsContext Name -> TcM (LHsContext Name) @@ -419,16 +417,16 @@ kcHsPred pred = do -- Checks that the result is of kind liftedType kc_pred :: HsPred Name -> TcM (HsPred Name, TcKind) -- Does *not* check for a saturated -- application (reason: used from TcDeriv) -kc_pred pred@(HsIParam name ty) +kc_pred (HsIParam name ty) = do { (ty', kind) <- kcHsType ty ; return (HsIParam name ty', kind) } -kc_pred pred@(HsClassP cls tys) +kc_pred (HsClassP cls tys) = do { kind <- kcClass cls ; (tys', res_kind) <- kcApps kind (ppr cls) tys ; return (HsClassP cls tys', res_kind) } -kc_pred pred@(HsEqualP ty1 ty2) +kc_pred (HsEqualP ty1 ty2) = do { (ty1', kind1) <- kcHsType ty1 -- ; checkExpectedKind ty1 kind1 liftedTypeKind ; (ty2', kind2) <- kcHsType ty2 @@ -447,7 +445,7 @@ kcTyVar name = do -- Could be a tyvar or a tycon ATyVar _ ty -> return (typeKind ty) AThing kind -> return kind AGlobal (ATyCon tc) -> return (tyConKind tc) - other -> wrongThingErr "type" thing name + _ -> wrongThingErr "type" thing name kcClass :: Name -> TcM TcKind kcClass cls = do -- Must be a class @@ -455,7 +453,7 @@ kcClass cls = do -- Must be a class case thing of AThing kind -> return kind AGlobal (AClass cls) -> return (tyConKind (classTyCon cls)) - other -> wrongThingErr "class" thing cls + _ -> wrongThingErr "class" thing cls \end{code} @@ -480,16 +478,17 @@ dsHsType :: LHsType Name -> TcM Type -- All HsTyVarBndrs in the intput type are kind-annotated dsHsType ty = ds_type (unLoc ty) -ds_type ty@(HsTyVar name) +ds_type :: HsType Name -> TcM Type +ds_type ty@(HsTyVar _) = ds_app ty [] ds_type (HsParTy ty) -- Remove the parentheses markers = dsHsType ty ds_type ty@(HsBangTy _ _) -- No bangs should be here - = failWithTc (ptext SLIT("Unexpected strictness annotation:") <+> ppr ty) + = failWithTc (ptext (sLit "Unexpected strictness annotation:") <+> ppr ty) -ds_type (HsKindSig ty k) +ds_type (HsKindSig ty _) = dsHsType ty -- Kind checking done already ds_type (HsListTy ty) = do @@ -531,7 +530,7 @@ ds_type (HsPredTy pred) = do pred' <- dsHsPred pred return (mkPredTy pred') -ds_type full_ty@(HsForAllTy exp tv_names ctxt ty) +ds_type (HsForAllTy _ tv_names ctxt ty) = tcTyVarBndrs tv_names $ \ tyvars -> do theta <- mapM dsHsLPred (unLoc ctxt) tau <- dsHsType ty @@ -542,6 +541,7 @@ ds_type (HsSpliceTy {}) = panic "ds_type: HsSpliceTy" ds_type (HsDocTy ty _) -- Remove the doc comment = dsHsType ty +dsHsTypes :: [LHsType Name] -> TcM [Type] dsHsTypes arg_tys = mapM dsHsType arg_tys \end{code} @@ -557,16 +557,16 @@ ds_app ty tys = do arg_tys <- dsHsTypes tys case ty of HsTyVar fun -> ds_var_app fun arg_tys - other -> do fun_ty <- ds_type ty + _ -> do fun_ty <- ds_type ty return (mkAppTys fun_ty arg_tys) ds_var_app :: Name -> [Type] -> TcM Type ds_var_app name arg_tys = do thing <- tcLookup name case thing of - ATyVar _ ty -> return (mkAppTys ty arg_tys) + ATyVar _ ty -> return (mkAppTys ty arg_tys) AGlobal (ATyCon tc) -> return (mkTyConApp tc arg_tys) - other -> wrongThingErr "type" thing name + _ -> wrongThingErr "type" thing name \end{code} @@ -577,12 +577,13 @@ Contexts dsHsLPred :: LHsPred Name -> TcM PredType dsHsLPred pred = dsHsPred (unLoc pred) -dsHsPred pred@(HsClassP class_name tys) +dsHsPred :: HsPred Name -> TcM PredType +dsHsPred (HsClassP class_name tys) = do { arg_tys <- dsHsTypes tys ; clas <- tcLookupClass class_name ; return (ClassP clas arg_tys) } -dsHsPred pred@(HsEqualP ty1 ty2) +dsHsPred (HsEqualP ty1 ty2) = do { arg_ty1 <- dsHsType ty1 ; arg_ty2 <- dsHsType ty2 ; return (EqPred arg_ty1 arg_ty2) @@ -605,23 +606,25 @@ tcLHsConResTy (L span res_ty) ; thing <- tcLookup tc_name ; case thing of AGlobal (ATyCon tc) -> return (tc, args') - other -> failWithTc (badGadtDecl res_ty) } - other -> failWithTc (badGadtDecl res_ty) + _ -> failWithTc (badGadtDecl res_ty) } + _ -> failWithTc (badGadtDecl res_ty) where -- We can't call dsHsType on res_ty, and then do tcSplitTyConApp_maybe -- because that causes a black hole, and for good reason. Building -- the type means expanding type synonyms, and we can't do that -- inside the "knot". So we have to work by steam. - get_args (HsAppTy (L _ fun) arg) args = get_args fun (arg:args) - get_args (HsParTy (L _ ty)) args = get_args ty args - get_args (HsOpTy ty1 (L span tc) ty2) args = (HsTyVar tc, ty1:ty2:args) - get_args ty args = (ty, args) + get_args (HsAppTy (L _ fun) arg) args = get_args fun (arg:args) + get_args (HsParTy (L _ ty)) args = get_args ty args + get_args (HsOpTy ty1 (L _ tc) ty2) args = (HsTyVar tc, ty1:ty2:args) + get_args ty args = (ty, args) +badGadtDecl :: HsType Name -> SDoc badGadtDecl ty - = hang (ptext SLIT("Malformed constructor result type:")) + = hang (ptext (sLit "Malformed constructor result type:")) 2 (ppr ty) -typeCtxt ty = ptext SLIT("In the type") <+> quotes (ppr ty) +typeCtxt :: HsType Name -> SDoc +typeCtxt ty = ptext (sLit "In the type") <+> quotes (ppr ty) \end{code} %************************************************************************ @@ -657,7 +660,7 @@ tcTyVarBndrs bndrs thing_inside = do where zonk (KindedTyVar name kind) = do { kind' <- zonkTcKindToKind kind ; return (mkTyVar name kind') } - zonk (UserTyVar name) = WARN( True, ptext SLIT("Un-kinded tyvar") <+> ppr name ) + zonk (UserTyVar name) = WARN( True, ptext (sLit "Un-kinded tyvar") <+> ppr name ) return (mkTyVar name liftedTypeKind) ----------------------------------- @@ -674,23 +677,43 @@ tcDataKindSig (Just kind) ; us <- newUniqueSupply ; let uniqs = uniqsFromSupply us ; return [ mk_tv span uniq str kind - | ((kind, str), uniq) <- arg_kinds `zip` names `zip` uniqs ] } + | ((kind, str), uniq) <- arg_kinds `zip` dnames `zip` uniqs ] } where (arg_kinds, res_kind) = splitKindFunTys kind mk_tv loc uniq str kind = mkTyVar name kind where name = mkInternalName uniq occ loc occ = mkOccName tvName str + + dnames = map ('$' :) names -- Note [Avoid name clashes for associated data types] - names :: [String] -- a,b,c...aa,ab,ac etc + names :: [String] names = [ c:cs | cs <- "" : names, c <- ['a'..'z'] ] badKindSig :: Kind -> SDoc badKindSig kind - = hang (ptext SLIT("Kind signature on data type declaration has non-* return kind")) + = hang (ptext (sLit "Kind signature on data type declaration has non-* return kind")) 2 (ppr kind) \end{code} +Note [Avoid name clashes for associated data types] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Consider class C a b where + data D b :: * -> * +When typechecking the decl for D, we'll invent an extra type variable for D, +to fill out its kind. We *don't* want this type variable to be 'a', because +in an .hi file we'd get + class C a b where + data D b a +which makes it look as if there are *two* type indices. But there aren't! +So we use $a instead, which cannot clash with a user-written type variable. +Remember that type variable binders in interface files are just FastStrings, +not proper Names. + +(The tidying phase can't help here because we don't tidy TyCons. Another +alternative would be to record the number of indexing parameters in the +interface file.) + %************************************************************************ %* * @@ -761,16 +784,17 @@ tcPatSig :: UserTypeCtxt -> LHsType Name -> BoxySigmaType -> TcM (TcType, -- The type to use for "inside" the signature - [(Name,TcType)]) -- The new bit of type environment, binding + [(Name, TcType)], -- The new bit of type environment, binding -- the scoped type variables + CoercionI) -- Coercion due to unification with actual ty tcPatSig ctxt sig res_ty = do { (sig_tvs, sig_ty) <- tcHsPatSigType ctxt sig ; if null sig_tvs then do { -- The type signature binds no type variables, -- and hence is rigid, so use it to zap the res_ty - boxyUnify sig_ty res_ty - ; return (sig_ty, []) + coi <- boxyUnify sig_ty res_ty + ; return (sig_ty, [], coi) } else do { -- Type signature binds at least one scoped type variable @@ -781,7 +805,7 @@ tcPatSig ctxt sig res_ty -- So we just have an ASSERT here ; let in_pat_bind = case ctxt of BindPatSigCtxt -> True - other -> False + _ -> False ; ASSERT( not in_pat_bind || null sig_tvs ) return () -- Check that pat_ty is rigid @@ -793,7 +817,8 @@ tcPatSig ctxt sig res_ty -- unifying, and reading out the results. -- This is a strictly local operation. ; box_tvs <- mapM tcInstBoxyTyVar sig_tvs - ; boxyUnify (substTyWith sig_tvs (mkTyVarTys box_tvs) sig_ty) res_ty + ; coi <- boxyUnify (substTyWith sig_tvs (mkTyVarTys box_tvs) sig_ty) + res_ty ; sig_tv_tys <- mapM readFilledBox box_tvs -- Check that each is bound to a distinct type variable, @@ -803,10 +828,10 @@ tcPatSig ctxt sig res_ty ; check binds_in_scope tv_binds -- Phew! - ; return (res_ty, tv_binds) + ; return (res_ty, tv_binds, coi) } } where - check in_scope [] = return () + check _ [] = return () check in_scope ((n,ty):rest) = do { check_one in_scope n ty ; check ((n,ty):in_scope) rest } @@ -832,32 +857,35 @@ tcPatSig ctxt sig res_ty \begin{code} pprHsSigCtxt :: UserTypeCtxt -> LHsType Name -> SDoc -pprHsSigCtxt ctxt hs_ty = vcat [ ptext SLIT("In") <+> pprUserTypeCtxt ctxt <> colon, +pprHsSigCtxt ctxt hs_ty = vcat [ ptext (sLit "In") <+> pprUserTypeCtxt ctxt <> colon, nest 2 (pp_sig ctxt) ] where pp_sig (FunSigCtxt n) = pp_n_colon n pp_sig (ConArgCtxt n) = pp_n_colon n pp_sig (ForSigCtxt n) = pp_n_colon n - pp_sig other = ppr (unLoc hs_ty) + pp_sig _ = ppr (unLoc hs_ty) pp_n_colon n = ppr n <+> dcolon <+> ppr (unLoc hs_ty) - +wobblyPatSig :: [Var] -> SDoc wobblyPatSig sig_tvs - = hang (ptext SLIT("A pattern type signature cannot bind scoped type variables") + = hang (ptext (sLit "A pattern type signature cannot bind scoped type variables") <+> pprQuotedList sig_tvs) - 2 (ptext SLIT("unless the pattern has a rigid type context")) + 2 (ptext (sLit "unless the pattern has a rigid type context")) +scopedNonVar :: Name -> Type -> SDoc scopedNonVar n ty - = vcat [sep [ptext SLIT("The scoped type variable") <+> quotes (ppr n), - nest 2 (ptext SLIT("is bound to the type") <+> quotes (ppr ty))], - nest 2 (ptext SLIT("You can only bind scoped type variables to type variables"))] + = vcat [sep [ptext (sLit "The scoped type variable") <+> quotes (ppr n), + nest 2 (ptext (sLit "is bound to the type") <+> quotes (ppr ty))], + nest 2 (ptext (sLit "You can only bind scoped type variables to type variables"))] -dupInScope n n' ty - = hang (ptext SLIT("The scoped type variables") <+> quotes (ppr n) <+> ptext SLIT("and") <+> quotes (ppr n')) - 2 (vcat [ptext SLIT("are bound to the same type (variable)"), - ptext SLIT("Distinct scoped type variables must be distinct")]) +dupInScope :: Name -> Name -> Type -> SDoc +dupInScope n n' _ + = hang (ptext (sLit "The scoped type variables") <+> quotes (ppr n) <+> ptext (sLit "and") <+> quotes (ppr n')) + 2 (vcat [ptext (sLit "are bound to the same type (variable)"), + ptext (sLit "Distinct scoped type variables must be distinct")]) +wrongEqualityErr :: TcM (HsType Name, TcKind) wrongEqualityErr = failWithTc (text "Equality predicate used as a type") \end{code}