2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsTypes]{Abstract syntax: user-defined types}
8 HsType(..), HsTyVarBndr(..),
9 , HsContext, HsPred(..)
10 , HsTupCon(..), hsTupParens, mkHsTupCon,
13 , mkHsForAllTy, mkHsDictTy, mkHsIParamTy
14 , hsTyVarName, hsTyVarNames, replaceTyVarName
17 , pprParendHsType, pprHsForAll, pprHsContext, pprHsTyVarBndr
19 -- Equality over Hs things
20 , EqHsEnv, emptyEqHsEnv, extendEqHsEnv,
21 , eqWithHsTyVars, eq_hsVar, eq_hsVars, eq_hsTyVars, eq_hsType, eq_hsContext, eqListBy
23 -- Converting from Type to HsType
24 , toHsType, toHsTyVar, toHsTyVars, toHsContext, toHsFDs
27 #include "HsVersions.h"
29 import Class ( FunDep )
30 import Type ( Type, Kind, PredType(..), ClassContext,
31 splitSigmaTy, boxedTypeKind
33 import TypeRep ( Type(..), TyNote(..) ) -- toHsType sees the representation
34 import TyCon ( isTupleTyCon, tupleTyConBoxity, tyConArity, getSynTyConDefn )
35 import RdrName ( RdrName, mkUnqual )
36 import Name ( Name, getName, setLocalNameSort )
37 import OccName ( NameSpace, tvName )
38 import Var ( TyVar, tyVarKind )
39 import Subst ( mkTyVarSubst, substTy )
40 import PprType ( {- instance Outputable Kind -}, pprParendKind )
41 import BasicTypes ( Boxity(..), tupleParens )
42 import PrelNames ( mkTupConRdrName, listTyConKey, usOnceTyConKey, usManyTyConKey, hasKey,
43 usOnceTyConName, usManyTyConName
50 This is the syntax for types as seen in type signatures.
53 type HsContext name = [HsPred name]
55 data HsPred name = HsPClass name [HsType name]
56 | HsPIParam name (HsType name)
59 = HsForAllTy (Maybe [HsTyVarBndr name]) -- Nothing for implicitly quantified signatures
63 | HsTyVar name -- Type variable or type constructor
65 | HsAppTy (HsType name)
68 | HsFunTy (HsType name) -- function type
71 | HsListTy (HsType name) -- Element type
73 | HsTupleTy (HsTupCon name)
74 [HsType name] -- Element types (length gives arity)
76 | HsOpTy (HsType name) name (HsType name)
78 -- these next two are only used in interfaces
79 | HsPredTy (HsPred name)
81 | HsUsageTy (HsType name) -- Usage annotation
82 (HsType name) -- Annotated type
85 -----------------------
86 hsUsOnce, hsUsMany :: HsType RdrName
87 hsUsOnce = HsTyVar (mkUnqual tvName SLIT(".")) -- deep magic
88 hsUsMany = HsTyVar (mkUnqual tvName SLIT("!")) -- deep magic
90 hsUsOnce_Name, hsUsMany_Name :: HsType Name
91 -- Fudge the TyConName so that it prints unqualified
92 -- I hate it! I hate it!
93 hsUsOnce_Name = HsTyVar (setLocalNameSort usOnceTyConName False)
94 hsUsMany_Name = HsTyVar (setLocalNameSort usManyTyConName False)
96 -----------------------
97 data HsTupCon name = HsTupCon name Boxity
99 instance Eq name => Eq (HsTupCon name) where
100 (HsTupCon _ b1) == (HsTupCon _ b2) = b1==b2
102 mkHsTupCon :: NameSpace -> Boxity -> [a] -> HsTupCon RdrName
103 mkHsTupCon space boxity args = HsTupCon (mkTupConRdrName space boxity (length args)) boxity
105 hsTupParens :: HsTupCon name -> SDoc -> SDoc
106 hsTupParens (HsTupCon _ b) p = tupleParens b p
108 -----------------------
109 -- Combine adjacent for-alls.
110 -- The following awkward situation can happen otherwise:
111 -- f :: forall a. ((Num a) => Int)
112 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
113 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
114 -- but the export list abstracts f wrt [a]. Disaster.
116 -- A valid type must have one for-all at the top of the type, or of the fn arg types
118 mkHsForAllTy (Just []) [] ty = ty -- Explicit for-all with no tyvars
119 mkHsForAllTy mtvs1 [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
121 mtvs1 `plus` Nothing = mtvs1
122 Nothing `plus` mtvs2 = mtvs2
123 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
124 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
126 mkHsDictTy cls tys = HsPredTy (HsPClass cls tys)
127 mkHsIParamTy v ty = HsPredTy (HsPIParam v ty)
129 data HsTyVarBndr name
131 | IfaceTyVar name Kind
132 -- *** NOTA BENE *** A "monotype" in a pragma can have
133 -- for-alls in it, (mostly to do with dictionaries). These
134 -- must be explicitly Kinded.
136 hsTyVarName (UserTyVar n) = n
137 hsTyVarName (IfaceTyVar n _) = n
139 hsTyVarNames tvs = map hsTyVarName tvs
141 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
142 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
143 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
147 %************************************************************************
149 \subsection{Pretty printing}
151 %************************************************************************
153 NB: these types get printed into interface files, so
154 don't change the printing format lightly
157 instance (Outputable name) => Outputable (HsType name) where
158 ppr ty = pprHsType ty
160 instance (Outputable name) => Outputable (HsTyVarBndr name) where
161 ppr (UserTyVar name) = ppr name
162 ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
164 instance Outputable name => Outputable (HsPred name) where
165 ppr (HsPClass clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
166 ppr (HsPIParam n ty) = hsep [char '?' <> ppr n, text "::", ppr ty]
168 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
169 pprHsTyVarBndr name kind | kind == boxedTypeKind = ppr name
170 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
172 pprHsForAll [] [] = empty
174 -- This printer is used for both interface files and
175 -- printing user types in error messages; and alas the
176 -- two use slightly different syntax. Ah well.
177 = getPprStyle $ \ sty ->
178 if userStyle sty then
179 ptext SLIT("forall") <+> interppSP tvs <> dot <+>
180 -- **! ToDo: want to hide uvars from user, but not enough info
181 -- in a HsTyVarBndr name (see PprType). KSW 2000-10.
185 ppr_context cxt <+> ptext SLIT("=>")
187 else -- Used in interfaces
188 ptext SLIT("__forall") <+> interppSP tvs <+>
189 ppr_context cxt <+> ptext SLIT("=>")
191 pprHsContext :: (Outputable name) => HsContext name -> SDoc
192 pprHsContext [] = empty
193 pprHsContext cxt = ppr_context cxt <+> ptext SLIT("=>")
195 ppr_context [] = empty
196 ppr_context cxt = parens (interpp'SP cxt)
200 pREC_TOP = (0 :: Int) -- type in ParseIface.y
201 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
202 pREC_CON = (2 :: Int) -- atype in ParseIface.y
204 maybeParen :: Bool -> SDoc -> SDoc
205 maybeParen True p = parens p
206 maybeParen False p = p
208 -- printing works more-or-less as for Types
210 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
212 pprHsType ty = ppr_mono_ty pREC_TOP ty
213 pprParendHsType ty = ppr_mono_ty pREC_CON ty
215 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
216 = maybeParen (ctxt_prec >= pREC_FUN) $
217 sep [pp_header, pprHsType ty]
219 pp_header = case maybe_tvs of
220 Just tvs -> pprHsForAll tvs ctxt
221 Nothing -> pprHsContext ctxt
223 ppr_mono_ty ctxt_prec (HsTyVar name)
226 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)
227 = let p1 = ppr_mono_ty pREC_FUN ty1
228 p2 = ppr_mono_ty pREC_TOP ty2
230 maybeParen (ctxt_prec >= pREC_FUN)
231 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
233 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = hsTupParens con (interpp'SP tys)
234 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
236 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
237 = maybeParen (ctxt_prec >= pREC_CON)
238 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
240 ppr_mono_ty ctxt_prec (HsPredTy pred)
241 = maybeParen (ctxt_prec >= pREC_FUN) $
244 ppr_mono_ty ctxt_prec (HsUsageTy u ty)
245 = maybeParen (ctxt_prec >= pREC_CON)
246 (sep [ptext SLIT("__u") <+> ppr_mono_ty pREC_CON u,
247 ppr_mono_ty pREC_CON ty])
248 -- pREC_FUN would be logical for u, but it yields a reduce/reduce conflict with AppTy
251 ppr_mono_ty ctxt_prec (HsNumTy n) = integer n
252 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2) = ppr ty1 <+> ppr op <+> ppr ty2
256 %************************************************************************
258 \subsection{Converting from Type to HsType}
260 %************************************************************************
262 @toHsType@ converts from a Type to a HsType, making the latter look as
263 user-friendly as possible. Notably, it uses synonyms where possible, and
264 expresses overloaded functions using the '=>' context part of a HsForAllTy.
267 toHsTyVar :: TyVar -> HsTyVarBndr Name
268 toHsTyVar tv = IfaceTyVar (getName tv) (tyVarKind tv)
270 toHsTyVars tvs = map toHsTyVar tvs
272 toHsType :: Type -> HsType Name
273 -- This function knows the representation of types
274 toHsType (TyVarTy tv) = HsTyVar (getName tv)
275 toHsType (FunTy arg res) = HsFunTy (toHsType arg) (toHsType res)
276 toHsType (AppTy fun arg) = HsAppTy (toHsType fun) (toHsType arg)
278 toHsType (NoteTy (SynNote syn_ty) real_ty)
279 | syn_matches = toHsType syn_ty -- Use synonyms if possible!!
282 pprTrace "WARNING: synonym info lost in .hi file for " (ppr syn_ty) $
284 toHsType real_ty -- but drop it if not.
286 syn_matches = ty_from_syn == real_ty
288 TyConApp syn_tycon tyargs = syn_ty
289 (tyvars,ty) = getSynTyConDefn syn_tycon
290 ty_from_syn = substTy (mkTyVarSubst tyvars tyargs) ty
292 -- We only use the type synonym in the file if this doesn't cause
293 -- us to lose important information. This matters for usage
294 -- annotations. It's an issue if some of the args to the synonym
295 -- have arrows in them, or if the synonym's RHS has an arrow; for
296 -- example, with nofib/real/ebnf2ps/ in Parsers.using.
298 -- **! It would be nice if when this test fails we could still
299 -- write the synonym in as a Note, so we don't lose the info for
300 -- error messages, but it's too much work for right now.
303 toHsType (NoteTy _ ty) = toHsType ty
305 toHsType (PredTy p) = HsPredTy (toHsPred p)
307 toHsType ty@(TyConApp tc tys) -- Must be saturated because toHsType's arg is of kind *
308 | not saturated = generic_case
309 | isTupleTyCon tc = HsTupleTy (HsTupCon (getName tc) (tupleTyConBoxity tc)) tys'
310 | tc `hasKey` listTyConKey = HsListTy (head tys')
311 | tc `hasKey` usOnceTyConKey = hsUsOnce_Name -- must print !, . unqualified
312 | tc `hasKey` usManyTyConKey = hsUsMany_Name -- must print !, . unqualified
313 | otherwise = generic_case
315 generic_case = foldl HsAppTy (HsTyVar (getName tc)) tys'
316 tys' = map toHsType tys
317 saturated = length tys == tyConArity tc
319 toHsType ty@(ForAllTy _ _) = case splitSigmaTy ty of
320 (tvs, preds, tau) -> HsForAllTy (Just (map toHsTyVar tvs))
324 toHsType (UsageTy u ty) = HsUsageTy (toHsType u) (toHsType ty)
325 -- **! consider dropping usMany annotations ToDo KSW 2000-10
328 toHsPred (Class cls tys) = HsPClass (getName cls) (map toHsType tys)
329 toHsPred (IParam n ty) = HsPIParam (getName n) (toHsType ty)
331 toHsContext :: ClassContext -> HsContext Name
332 toHsContext cxt = [HsPClass (getName cls) (map toHsType tys) | (cls,tys) <- cxt]
334 toHsFDs :: [FunDep TyVar] -> [FunDep Name]
335 toHsFDs fds = [(map getName ns, map getName ms) | (ns,ms) <- fds]
339 %************************************************************************
341 \subsection{Comparison}
343 %************************************************************************
346 instance Ord a => Eq (HsType a) where
347 -- The Ord is needed because we keep a
348 -- finite map of variables to variables
349 (==) a b = eq_hsType emptyEqHsEnv a b
351 instance Ord a => Eq (HsPred a) where
352 (==) a b = eq_hsPred emptyEqHsEnv a b
354 eqWithHsTyVars :: Ord name =>
355 [HsTyVarBndr name] -> [HsTyVarBndr name]
356 -> (EqHsEnv name -> Bool) -> Bool
357 eqWithHsTyVars = eq_hsTyVars emptyEqHsEnv
361 type EqHsEnv n = FiniteMap n n
362 -- Tracks the mapping from L-variables to R-variables
364 eq_hsVar :: Ord n => EqHsEnv n -> n -> n -> Bool
365 eq_hsVar env n1 n2 = case lookupFM env n1 of
369 extendEqHsEnv env n1 n2
371 | otherwise = addToFM env n1 n2
373 emptyEqHsEnv :: EqHsEnv n
374 emptyEqHsEnv = emptyFM
377 We do define a specialised equality for these \tr{*Type} types; used
378 in checking interfaces.
382 eq_hsTyVars env [] [] k = k env
383 eq_hsTyVars env (tv1:tvs1) (tv2:tvs2) k = eq_hsTyVar env tv1 tv2 $ \ env ->
384 eq_hsTyVars env tvs1 tvs2 k
385 eq_hsTyVars env _ _ _ = False
387 eq_hsTyVar env (UserTyVar v1) (UserTyVar v2) k = k (extendEqHsEnv env v1 v2)
388 eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 == k2 && k (extendEqHsEnv env v1 v2)
389 eq_hsTyVar env _ _ _ = False
391 eq_hsVars env [] [] k = k env
392 eq_hsVars env (v1:bs1) (v2:bs2) k = eq_hsVars (extendEqHsEnv env v1 v2) bs1 bs2 k
393 eq_hsVars env _ _ _ = False
398 eq_hsTypes env = eqListBy (eq_hsType env)
401 eq_hsType env (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
402 = eq_tvs tvs1 tvs2 $ \env ->
403 eq_hsContext env c1 c2 &&
406 eq_tvs Nothing (Just _) k = False
407 eq_tvs Nothing Nothing k = k env
408 eq_tvs (Just _) Nothing k = False
409 eq_tvs (Just tvs1) (Just tvs2) k = eq_hsTyVars env tvs1 tvs2 k
411 eq_hsType env (HsTyVar n1) (HsTyVar n2)
414 eq_hsType env (HsTupleTy c1 tys1) (HsTupleTy c2 tys2)
415 = (c1 == c2) && eq_hsTypes env tys1 tys2
417 eq_hsType env (HsListTy ty1) (HsListTy ty2)
418 = eq_hsType env ty1 ty2
420 eq_hsType env (HsAppTy fun_ty1 arg_ty1) (HsAppTy fun_ty2 arg_ty2)
421 = eq_hsType env fun_ty1 fun_ty2 && eq_hsType env arg_ty1 arg_ty2
423 eq_hsType env (HsFunTy a1 b1) (HsFunTy a2 b2)
424 = eq_hsType env a1 a2 && eq_hsType env b1 b2
426 eq_hsType env (HsPredTy p1) (HsPredTy p2)
427 = eq_hsPred env p1 p2
429 eq_hsType env (HsUsageTy u1 ty1) (HsUsageTy u2 ty2)
430 = eq_hsType env u1 u2 && eq_hsType env ty1 ty2
432 eq_hsType env (HsOpTy lty1 op1 rty1) (HsOpTy lty2 op2 rty2)
433 = eq_hsVar env op1 op2 && eq_hsType env lty1 lty2 && eq_hsType env rty1 rty2
435 eq_hsType env ty1 ty2 = False
439 eq_hsContext env a b = eqListBy (eq_hsPred env) a b
442 eq_hsPred env (HsPClass c1 tys1) (HsPClass c2 tys2)
443 = c1 == c2 && eq_hsTypes env tys1 tys2
444 eq_hsPred env (HsPIParam n1 ty1) (HsPIParam n2 ty2)
445 = n1 == n2 && eq_hsType env ty1 ty2
446 eq_hsPred env _ _ = False
449 eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
450 eqListBy eq [] [] = True
451 eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
452 eqListBy eq xs ys = False