2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsTypes]{Abstract syntax: user-defined types}
8 HsType(..), HsUsageAnn(..), HsTyVarBndr(..),
9 , HsContext, HsPred(..)
10 , HsTupCon(..), hsTupParens, mkHsTupCon,
12 , mkHsForAllTy, mkHsUsForAllTy, mkHsDictTy, mkHsIParamTy
13 , getTyVarName, replaceTyVarName
16 , pprParendHsType, pprHsForAll, pprHsContext, pprHsTyVarBndr
18 -- Equality over Hs things
19 , EqHsEnv, emptyEqHsEnv, extendEqHsEnv,
20 , eqWithHsTyVars, eq_hsVar, eq_hsVars, eq_hsType, eq_hsContext, eqListBy
22 -- Converting from Type to HsType
23 , toHsType, toHsTyVar, toHsTyVars, toHsContext, toHsFDs
26 #include "HsVersions.h"
28 import Class ( FunDep )
29 import Type ( Type, Kind, PredType(..), UsageAnn(..), ClassContext,
30 getTyVar_maybe, splitFunTy_maybe, splitAppTy_maybe,
31 splitTyConApp_maybe, splitPredTy_maybe,
32 splitUsgTy, splitSigmaTy, unUsgTy, boxedTypeKind
34 import TypeRep ( Type(..), TyNote(..) ) -- toHsType sees the representation
35 import TyCon ( isTupleTyCon, tupleTyConBoxity, tyConArity, tyConClass_maybe )
36 import PrelInfo ( mkTupConRdrName )
37 import RdrName ( RdrName )
38 import Name ( toRdrName )
39 import OccName ( NameSpace )
40 import Var ( TyVar, tyVarKind )
41 import PprType ( {- instance Outputable Kind -}, pprParendKind )
42 import BasicTypes ( Arity, Boxity(..), tupleParens )
43 import Unique ( hasKey, listTyConKey, Uniquable(..) )
44 import Maybes ( maybeToBool )
49 This is the syntax for types as seen in type signatures.
52 type HsContext name = [HsPred name]
54 data HsPred name = HsPClass name [HsType name]
55 | HsPIParam name (HsType name)
58 = HsForAllTy (Maybe [HsTyVarBndr name]) -- Nothing for implicitly quantified signatures
62 | HsTyVar name -- Type variable
64 | HsAppTy (HsType name)
67 | HsFunTy (HsType name) -- function type
70 | HsListTy (HsType name) -- Element type
72 | HsTupleTy (HsTupCon name)
73 [HsType name] -- Element types (length gives arity)
75 -- these next two are only used in interfaces
76 | HsPredTy (HsPred name)
78 | HsUsgTy (HsUsageAnn name)
90 -----------------------
91 data HsTupCon name = HsTupCon name Boxity
93 instance Eq name => Eq (HsTupCon name) where
94 (HsTupCon _ b1) == (HsTupCon _ b2) = b1==b2
96 mkHsTupCon :: NameSpace -> Boxity -> [a] -> HsTupCon RdrName
97 mkHsTupCon space boxity args = HsTupCon (mkTupConRdrName space boxity (length args)) boxity
99 hsTupParens :: HsTupCon name -> SDoc -> SDoc
100 hsTupParens (HsTupCon _ b) p = tupleParens b p
102 -----------------------
103 -- Combine adjacent for-alls.
104 -- The following awkward situation can happen otherwise:
105 -- f :: forall a. ((Num a) => Int)
106 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
107 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
108 -- but the export list abstracts f wrt [a]. Disaster.
110 -- A valid type must have one for-all at the top of the type, or of the fn arg types
112 mkHsForAllTy (Just []) [] ty = ty -- Explicit for-all with no tyvars
113 mkHsForAllTy mtvs1 [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
115 mtvs1 `plus` Nothing = mtvs1
116 Nothing `plus` mtvs2 = mtvs2
117 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
118 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
120 mkHsUsForAllTy uvs ty = foldr (\ uv ty -> HsUsgForAllTy uv ty)
123 mkHsDictTy cls tys = HsPredTy (HsPClass cls tys)
124 mkHsIParamTy v ty = HsPredTy (HsPIParam v ty)
126 data HsTyVarBndr name
128 | IfaceTyVar name Kind
129 -- *** NOTA BENE *** A "monotype" in a pragma can have
130 -- for-alls in it, (mostly to do with dictionaries). These
131 -- must be explicitly Kinded.
133 getTyVarName (UserTyVar n) = n
134 getTyVarName (IfaceTyVar n _) = n
136 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
137 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
138 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
142 %************************************************************************
144 \subsection{Pretty printing}
146 %************************************************************************
149 instance (Outputable name) => Outputable (HsType name) where
150 ppr ty = pprHsType ty
152 instance (Outputable name) => Outputable (HsTyVarBndr name) where
153 ppr (UserTyVar name) = ppr name
154 ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
156 instance Outputable name => Outputable (HsPred name) where
157 ppr (HsPClass clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
158 ppr (HsPIParam n ty) = hsep [{- char '?' <> -} ppr n, text "::", ppr ty]
160 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
161 pprHsTyVarBndr name kind | kind == boxedTypeKind = ppr name
162 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
164 pprHsForAll [] [] = empty
165 pprHsForAll tvs cxt = ptext SLIT("__forall") <+> interppSP tvs <+> ppr_context cxt <+> ptext SLIT("=>")
167 pprHsContext :: (Outputable name) => HsContext name -> SDoc
168 pprHsContext [] = empty
169 pprHsContext cxt = ppr_context cxt <+> ptext SLIT("=>")
171 ppr_context [] = empty
172 ppr_context cxt = parens (interpp'SP cxt)
176 pREC_TOP = (0 :: Int)
177 pREC_FUN = (1 :: Int)
178 pREC_CON = (2 :: Int)
180 maybeParen :: Bool -> SDoc -> SDoc
181 maybeParen True p = parens p
182 maybeParen False p = p
184 -- printing works more-or-less as for Types
186 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
188 pprHsType ty = ppr_mono_ty pREC_TOP ty
189 pprParendHsType ty = ppr_mono_ty pREC_CON ty
191 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
192 = maybeParen (ctxt_prec >= pREC_FUN) $
193 sep [pp_header, pprHsType ty]
195 pp_header = case maybe_tvs of
196 Just tvs -> pprHsForAll tvs ctxt
197 Nothing -> pprHsContext ctxt
199 ppr_mono_ty ctxt_prec (HsTyVar name)
202 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)
203 = let p1 = ppr_mono_ty pREC_FUN ty1
204 p2 = ppr_mono_ty pREC_TOP ty2
206 maybeParen (ctxt_prec >= pREC_FUN)
207 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
209 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = hsTupParens con (interpp'SP tys)
210 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
212 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
213 = maybeParen (ctxt_prec >= pREC_CON)
214 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
216 ppr_mono_ty ctxt_prec (HsPredTy pred)
217 = maybeParen (ctxt_prec >= pREC_FUN) $
220 ppr_mono_ty ctxt_prec ty@(HsUsgForAllTy _ _)
222 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
223 ppr_mono_ty pREC_TOP sigma
226 (uvars,sigma) = split [] ty
227 pp_uvars = interppSP uvars
229 split uvs (HsUsgForAllTy uv ty') = split (uv:uvs) ty'
230 split uvs ty' = (reverse uvs,ty')
232 ppr_mono_ty ctxt_prec (HsUsgTy u ty)
233 = maybeParen (ctxt_prec >= pREC_CON) $
234 ptext SLIT("__u") <+> pp_ua <+> ppr_mono_ty pREC_CON ty
237 HsUsOnce -> ptext SLIT("-")
238 HsUsMany -> ptext SLIT("!")
243 %************************************************************************
245 \subsection{Converting from Type to HsType}
247 %************************************************************************
249 @toHsType@ converts from a Type to a HsType, making the latter look as
250 user-friendly as possible. Notably, it uses synonyms where possible, and
251 expresses overloaded functions using the '=>' context part of a HsForAllTy.
254 toHsTyVar :: TyVar -> HsTyVarBndr RdrName
255 toHsTyVar tv = IfaceTyVar (toRdrName tv) (tyVarKind tv)
257 toHsTyVars tvs = map toHsTyVar tvs
259 toHsType :: Type -> HsType RdrName
260 toHsType ty = toHsType' (unUsgTy ty)
261 -- For now we just discard the usage
262 -- = case splitUsgTy ty of
263 -- (usg, tau) -> HsUsgTy (toHsUsg usg) (toHsType' tau)
265 toHsType' :: Type -> HsType RdrName
266 -- Called after the usage is stripped off
267 -- This function knows the representation of types
268 toHsType' (TyVarTy tv) = HsTyVar (toRdrName tv)
269 toHsType' (FunTy arg res) = HsFunTy (toHsType arg) (toHsType res)
270 toHsType' (AppTy fun arg) = HsAppTy (toHsType fun) (toHsType arg)
272 toHsType' (NoteTy (SynNote ty) _) = toHsType ty -- Use synonyms if possible!!
273 toHsType' (NoteTy _ ty) = toHsType ty
275 toHsType' ty@(TyConApp tc tys) -- Must be saturated because toHsType's arg is of kind *
276 | not saturated = generic_case
277 | isTupleTyCon tc = HsTupleTy (HsTupCon (toRdrName tc) (tupleTyConBoxity tc)) tys'
278 | tc `hasKey` listTyConKey = HsListTy (head tys')
279 | maybeToBool maybe_class = HsPredTy (HsPClass (toRdrName clas) tys')
280 | otherwise = generic_case
282 generic_case = foldl HsAppTy (HsTyVar (toRdrName tc)) tys'
283 maybe_class = tyConClass_maybe tc
284 Just clas = maybe_class
285 tys' = map toHsType tys
286 saturated = length tys == tyConArity tc
288 toHsType' ty@(ForAllTy _ _) = case splitSigmaTy ty of
289 (tvs, preds, tau) -> HsForAllTy (Just (map toHsTyVar tvs))
294 toHsPred (Class cls tys) = HsPClass (toRdrName cls) (map toHsType tys)
295 toHsPred (IParam n ty) = HsPIParam (toRdrName n) (toHsType ty)
297 toHsContext :: ClassContext -> HsContext RdrName
298 toHsContext cxt = [HsPClass (toRdrName cls) (map toHsType tys) | (cls,tys) <- cxt]
300 toHsUsg UsOnce = HsUsOnce
301 toHsUsg UsMany = HsUsMany
302 toHsUsg (UsVar v) = HsUsVar (toRdrName v)
304 toHsFDs :: [FunDep TyVar] -> [FunDep RdrName]
305 toHsFDs fds = [(map toRdrName ns, map toRdrName ms) | (ns,ms) <- fds]
309 %************************************************************************
311 \subsection{Comparison}
313 %************************************************************************
316 instance Ord a => Eq (HsType a) where
317 -- The Ord is needed because we keep a
318 -- finite map of variables to variables
319 (==) a b = eq_hsType emptyEqHsEnv a b
321 instance Ord a => Eq (HsPred a) where
322 (==) a b = eq_hsPred emptyEqHsEnv a b
324 eqWithHsTyVars :: Ord name =>
325 [HsTyVarBndr name] -> [HsTyVarBndr name]
326 -> (EqHsEnv name -> Bool) -> Bool
327 eqWithHsTyVars = eq_hsTyVars emptyEqHsEnv
331 type EqHsEnv n = FiniteMap n n
332 -- Tracks the mapping from L-variables to R-variables
334 eq_hsVar :: Ord n => EqHsEnv n -> n -> n -> Bool
335 eq_hsVar env n1 n2 = case lookupFM env n1 of
339 extendEqHsEnv env n1 n2
341 | otherwise = addToFM env n1 n2
343 emptyEqHsEnv :: EqHsEnv n
344 emptyEqHsEnv = emptyFM
347 We do define a specialised equality for these \tr{*Type} types; used
348 in checking interfaces.
352 eq_hsTyVars env [] [] k = k env
353 eq_hsTyVars env (tv1:tvs1) (tv2:tvs2) k = eq_hsTyVar env tv1 tv2 $ \ env ->
354 eq_hsTyVars env tvs1 tvs2 k
355 eq_hsTyVars env _ _ _ = False
357 eq_hsTyVar env (UserTyVar v1) (UserTyVar v2) k = k (extendEqHsEnv env v1 v2)
358 eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 == k2 && k (extendEqHsEnv env v1 v2)
359 eq_hsTyVar env _ _ _ = False
361 eq_hsVars env [] [] k = k env
362 eq_hsVars env (v1:bs1) (v2:bs2) k = eq_hsVars (extendEqHsEnv env v1 v2) bs1 bs2 k
363 eq_hsVars env _ _ _ = False
368 eq_hsTypes env = eqListBy (eq_hsType env)
371 eq_hsType env (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
372 = eq_tvs tvs1 tvs2 $ \env ->
373 eq_hsContext env c1 c2 &&
376 eq_tvs Nothing (Just _) k = False
377 eq_tvs Nothing Nothing k = k env
378 eq_tvs (Just _) Nothing k = False
379 eq_tvs (Just tvs1) (Just tvs2) k = eq_hsTyVars env tvs1 tvs2 k
381 eq_hsType env (HsTyVar n1) (HsTyVar n2)
384 eq_hsType env (HsTupleTy c1 tys1) (HsTupleTy c2 tys2)
385 = (c1 == c2) && eq_hsTypes env tys1 tys2
387 eq_hsType env (HsListTy ty1) (HsListTy ty2)
388 = eq_hsType env ty1 ty2
390 eq_hsType env (HsAppTy fun_ty1 arg_ty1) (HsAppTy fun_ty2 arg_ty2)
391 = eq_hsType env fun_ty1 fun_ty2 && eq_hsType env arg_ty1 arg_ty2
393 eq_hsType env (HsFunTy a1 b1) (HsFunTy a2 b2)
394 = eq_hsType env a1 a2 && eq_hsType env b1 b2
396 eq_hsType env (HsPredTy p1) (HsPredTy p2)
397 = eq_hsPred env p1 p2
399 eq_hsType env (HsUsgTy u1 ty1) (HsUsgTy u2 ty2)
400 = eqUsg u1 u2 && eq_hsType env ty1 ty2
402 eq_hsType env ty1 ty2 = False
406 eq_hsContext env a b = eqListBy (eq_hsPred env) a b
409 eq_hsPred env (HsPClass c1 tys1) (HsPClass c2 tys2)
410 = c1 == c2 && eq_hsTypes env tys1 tys2
411 eq_hsPred env (HsPIParam n1 ty1) (HsPIParam n2 ty2)
412 = n1 == n2 && eq_hsType env ty1 ty2
413 eq_hsPred env _ _ = False
416 eqUsg HsUsOnce HsUsOnce = True
417 eqUsg HsUsMany HsUsMany = True
418 eqUsg (HsUsVar u1) (HsUsVar u2) = u1 == u2
422 eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
423 eqListBy eq [] [] = True
424 eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
425 eqListBy eq xs ys = False