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 TcType ( Type, Kind, ThetaType, SourceType(..),
31 tcSplitSigmaTy, liftedTypeKind, eqKind, tcEqType
33 import TypeRep ( Type(..), TyNote(..) ) -- toHsType sees the representation
34 import TyCon ( isTupleTyCon, tupleTyConBoxity, tyConArity, isNewTyCon, getSynTyConDefn )
35 import RdrName ( RdrName, mkUnqual )
36 import Name ( Name, getName )
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(..), Arity, 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 = HsClassP name [HsType name]
56 | HsIParam 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)
79 -- these next two are only used in interfaces
80 | HsPredTy (HsPred name)
82 | HsUsageTy (HsType name) -- Usage annotation
83 (HsType name) -- Annotated type
86 -----------------------
87 hsUsOnce, hsUsMany :: HsType RdrName
88 hsUsOnce = HsTyVar (mkUnqual tvName SLIT(".")) -- deep magic
89 hsUsMany = HsTyVar (mkUnqual tvName SLIT("!")) -- deep magic
91 hsUsOnce_Name, hsUsMany_Name :: HsType Name
92 hsUsOnce_Name = HsTyVar usOnceTyConName
93 hsUsMany_Name = HsTyVar usManyTyConName
95 -----------------------
96 data HsTupCon name = HsTupCon name Boxity Arity
98 instance Eq name => Eq (HsTupCon name) where
99 (HsTupCon _ b1 a1) == (HsTupCon _ b2 a2) = b1==b2 && a1==a2
101 mkHsTupCon :: NameSpace -> Boxity -> [a] -> HsTupCon RdrName
102 mkHsTupCon space boxity args = HsTupCon (mkTupConRdrName space boxity arity) boxity arity
106 hsTupParens :: HsTupCon name -> SDoc -> SDoc
107 hsTupParens (HsTupCon _ b _) p = tupleParens b p
109 -----------------------
110 -- Combine adjacent for-alls.
111 -- The following awkward situation can happen otherwise:
112 -- f :: forall a. ((Num a) => Int)
113 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
114 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
115 -- but the export list abstracts f wrt [a]. Disaster.
117 -- A valid type must have one for-all at the top of the type, or of the fn arg types
119 mkHsForAllTy (Just []) [] ty = ty -- Explicit for-all with no tyvars
120 mkHsForAllTy mtvs1 [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
122 mtvs1 `plus` Nothing = mtvs1
123 Nothing `plus` mtvs2 = mtvs2
124 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
125 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
127 mkHsDictTy cls tys = HsPredTy (HsClassP cls tys)
128 mkHsIParamTy v ty = HsPredTy (HsIParam v ty)
130 data HsTyVarBndr name
132 | IfaceTyVar name Kind
133 -- *** NOTA BENE *** A "monotype" in a pragma can have
134 -- for-alls in it, (mostly to do with dictionaries). These
135 -- must be explicitly Kinded.
137 hsTyVarName (UserTyVar n) = n
138 hsTyVarName (IfaceTyVar n _) = n
140 hsTyVarNames tvs = map hsTyVarName tvs
142 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
143 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
144 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
148 %************************************************************************
150 \subsection{Pretty printing}
152 %************************************************************************
154 NB: these types get printed into interface files, so
155 don't change the printing format lightly
158 instance (Outputable name) => Outputable (HsType name) where
159 ppr ty = pprHsType ty
161 instance (Outputable name) => Outputable (HsTyVarBndr name) where
162 ppr (UserTyVar name) = ppr name
163 ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
165 instance Outputable name => Outputable (HsPred name) where
166 ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
167 ppr (HsIParam n ty) = hsep [char '?' <> ppr n, text "::", ppr ty]
169 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
170 pprHsTyVarBndr name kind | kind `eqKind` liftedTypeKind = ppr name
171 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
173 pprHsForAll [] [] = empty
175 -- This printer is used for both interface files and
176 -- printing user types in error messages; and alas the
177 -- two use slightly different syntax. Ah well.
178 = getPprStyle $ \ sty ->
179 if userStyle sty then
180 ptext SLIT("forall") <+> interppSP tvs <> dot <+>
181 -- **! ToDo: want to hide uvars from user, but not enough info
182 -- in a HsTyVarBndr name (see PprType). KSW 2000-10.
186 ppr_context cxt <+> ptext SLIT("=>")
188 else -- Used in interfaces
189 ptext SLIT("__forall") <+> interppSP tvs <+>
190 ppr_context cxt <+> ptext SLIT("=>")
192 pprHsContext :: (Outputable name) => HsContext name -> SDoc
193 pprHsContext [] = empty
194 pprHsContext cxt = ppr_context cxt <+> ptext SLIT("=>")
196 ppr_context [] = empty
197 ppr_context cxt = parens (interpp'SP cxt)
201 pREC_TOP = (0 :: Int) -- type in ParseIface.y
202 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
203 pREC_CON = (2 :: Int) -- atype in ParseIface.y
205 maybeParen :: Bool -> SDoc -> SDoc
206 maybeParen True p = parens p
207 maybeParen False p = p
209 -- printing works more-or-less as for Types
211 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
213 pprHsType ty = ppr_mono_ty pREC_TOP ty
214 pprParendHsType ty = ppr_mono_ty pREC_CON ty
216 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
217 = maybeParen (ctxt_prec >= pREC_FUN) $
218 sep [pp_header, pprHsType ty]
220 pp_header = case maybe_tvs of
221 Just tvs -> pprHsForAll tvs ctxt
222 Nothing -> pprHsContext ctxt
224 ppr_mono_ty ctxt_prec (HsTyVar name)
227 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)
228 = let p1 = ppr_mono_ty pREC_FUN ty1
229 p2 = ppr_mono_ty pREC_TOP ty2
231 maybeParen (ctxt_prec >= pREC_FUN)
232 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
234 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = hsTupParens con (interpp'SP tys)
235 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
237 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
238 = maybeParen (ctxt_prec >= pREC_CON)
239 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
241 ppr_mono_ty ctxt_prec (HsPredTy pred)
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 ty@(TyConApp tycon tyargs)) real_ty)
279 | isNewTyCon tycon = toHsType ty
280 | syn_matches = toHsType ty -- Use synonyms if possible!!
283 pprTrace "WARNING: synonym info lost in .hi file for " (ppr syn_ty) $
285 toHsType real_ty -- but drop it if not.
287 syn_matches = ty_from_syn `tcEqType` real_ty
288 (tyvars,syn_ty) = getSynTyConDefn tycon
289 ty_from_syn = substTy (mkTyVarSubst tyvars tyargs) syn_ty
291 -- We only use the type synonym in the file if this doesn't cause
292 -- us to lose important information. This matters for usage
293 -- annotations. It's an issue if some of the args to the synonym
294 -- have arrows in them, or if the synonym's RHS has an arrow; for
295 -- example, with nofib/real/ebnf2ps/ in Parsers.using.
297 -- **! It would be nice if when this test fails we could still
298 -- write the synonym in as a Note, so we don't lose the info for
299 -- error messages, but it's too much work for right now.
302 toHsType (NoteTy _ ty) = toHsType ty
304 toHsType (SourceTy (NType tc tys)) = foldl HsAppTy (HsTyVar (getName tc)) (map toHsType tys)
305 toHsType (SourceTy pred) = HsPredTy (toHsPred pred)
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) (tyConArity 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 tcSplitSigmaTy 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 (ClassP cls tys) = HsClassP (getName cls) (map toHsType tys)
329 toHsPred (IParam n ty) = HsIParam (getName n) (toHsType ty)
331 toHsContext :: ThetaType -> HsContext Name
332 toHsContext theta = map toHsPred theta
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 `eqKind` 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 (HsClassP c1 tys1) (HsClassP c2 tys2)
443 = c1 == c2 && eq_hsTypes env tys1 tys2
444 eq_hsPred env (HsIParam n1 ty1) (HsIParam 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