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 PostTcType, placeHolderType,
20 , pprParendHsType, pprHsForAll, pprHsContext, pprHsTyVarBndr
22 -- Equality over Hs things
23 , EqHsEnv, emptyEqHsEnv, extendEqHsEnv,
24 , eqWithHsTyVars, eq_hsVar, eq_hsVars, eq_hsTyVars, eq_hsType, eq_hsContext, eqListBy
26 -- Converting from Type to HsType
27 , toHsType, toHsTyVar, toHsTyVars, toHsContext, toHsFDs
30 #include "HsVersions.h"
32 import Class ( FunDep )
33 import TcType ( Type, Kind, ThetaType, SourceType(..),
34 tcSplitSigmaTy, liftedTypeKind, eqKind, tcEqType
36 import TypeRep ( Type(..), TyNote(..) ) -- toHsType sees the representation
37 import TyCon ( isTupleTyCon, tupleTyConBoxity, tyConArity, isNewTyCon, getSynTyConDefn )
38 import RdrName ( RdrName, mkUnqual )
39 import Name ( Name, getName )
40 import OccName ( NameSpace, tvName )
41 import Var ( TyVar, tyVarKind )
42 import Subst ( mkTyVarSubst, substTy )
43 import PprType ( {- instance Outputable Kind -}, pprParendKind )
44 import BasicTypes ( Boxity(..), Arity, tupleParens )
45 import PrelNames ( mkTupConRdrName, listTyConKey, usOnceTyConKey, usManyTyConKey, hasKey,
46 usOnceTyConName, usManyTyConName
49 import Util ( eqListBy )
54 %************************************************************************
56 \subsection{Annotating the syntax}
58 %************************************************************************
61 type PostTcType = Type -- Used for slots in the abstract syntax
62 -- where we want to keep slot for a type
63 -- to be added by the type checker...but
64 -- before typechecking it's just bogus
66 placeHolderType :: PostTcType -- Used before typechecking
67 placeHolderType = panic "Evaluated the place holder for a PostTcType"
71 %************************************************************************
73 \subsection{Data types}
75 %************************************************************************
77 This is the syntax for types as seen in type signatures.
80 type HsContext name = [HsPred name]
82 data HsPred name = HsClassP name [HsType name]
83 | HsIParam name (HsType name)
86 = HsForAllTy (Maybe [HsTyVarBndr name]) -- Nothing for implicitly quantified signatures
90 | HsTyVar name -- Type variable or type constructor
92 | HsAppTy (HsType name)
95 | HsFunTy (HsType name) -- function type
98 | HsListTy (HsType name) -- Element type
100 | HsTupleTy (HsTupCon name)
101 [HsType name] -- Element types (length gives arity)
103 | HsOpTy (HsType name) name (HsType name)
106 -- these next two are only used in interfaces
107 | HsPredTy (HsPred name)
109 | HsUsageTy (HsType name) -- Usage annotation
110 (HsType name) -- Annotated type
113 -----------------------
114 hsUsOnce, hsUsMany :: HsType RdrName
115 hsUsOnce = HsTyVar (mkUnqual tvName SLIT(".")) -- deep magic
116 hsUsMany = HsTyVar (mkUnqual tvName SLIT("!")) -- deep magic
118 hsUsOnce_Name, hsUsMany_Name :: HsType Name
119 hsUsOnce_Name = HsTyVar usOnceTyConName
120 hsUsMany_Name = HsTyVar usManyTyConName
122 -----------------------
123 data HsTupCon name = HsTupCon name Boxity Arity
125 instance Eq name => Eq (HsTupCon name) where
126 (HsTupCon _ b1 a1) == (HsTupCon _ b2 a2) = b1==b2 && a1==a2
128 mkHsTupCon :: NameSpace -> Boxity -> [a] -> HsTupCon RdrName
129 mkHsTupCon space boxity args = HsTupCon (mkTupConRdrName space boxity arity) boxity arity
133 hsTupParens :: HsTupCon name -> SDoc -> SDoc
134 hsTupParens (HsTupCon _ b _) p = tupleParens b p
136 -----------------------
137 -- Combine adjacent for-alls.
138 -- The following awkward situation can happen otherwise:
139 -- f :: forall a. ((Num a) => Int)
140 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
141 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
142 -- but the export list abstracts f wrt [a]. Disaster.
144 -- A valid type must have one for-all at the top of the type, or of the fn arg types
146 mkHsForAllTy (Just []) [] ty = ty -- Explicit for-all with no tyvars
147 mkHsForAllTy mtvs1 [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
149 mtvs1 `plus` Nothing = mtvs1
150 Nothing `plus` mtvs2 = mtvs2
151 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
152 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
154 mkHsDictTy cls tys = HsPredTy (HsClassP cls tys)
155 mkHsIParamTy v ty = HsPredTy (HsIParam v ty)
157 data HsTyVarBndr name
159 | IfaceTyVar name Kind
160 -- *** NOTA BENE *** A "monotype" in a pragma can have
161 -- for-alls in it, (mostly to do with dictionaries). These
162 -- must be explicitly Kinded.
164 hsTyVarName (UserTyVar n) = n
165 hsTyVarName (IfaceTyVar n _) = n
167 hsTyVarNames tvs = map hsTyVarName tvs
169 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
170 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
171 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
175 %************************************************************************
177 \subsection{Pretty printing}
179 %************************************************************************
181 NB: these types get printed into interface files, so
182 don't change the printing format lightly
185 instance (Outputable name) => Outputable (HsType name) where
186 ppr ty = pprHsType ty
188 instance (Outputable name) => Outputable (HsTyVarBndr name) where
189 ppr (UserTyVar name) = ppr name
190 ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
192 instance Outputable name => Outputable (HsPred name) where
193 ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
194 ppr (HsIParam n ty) = hsep [char '?' <> ppr n, text "::", ppr ty]
196 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
197 pprHsTyVarBndr name kind | kind `eqKind` liftedTypeKind = ppr name
198 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
200 pprHsForAll [] [] = empty
202 -- This printer is used for both interface files and
203 -- printing user types in error messages; and alas the
204 -- two use slightly different syntax. Ah well.
205 = getPprStyle $ \ sty ->
206 if userStyle sty then
207 ptext SLIT("forall") <+> interppSP tvs <> dot <+>
208 -- **! ToDo: want to hide uvars from user, but not enough info
209 -- in a HsTyVarBndr name (see PprType). KSW 2000-10.
213 ppr_context cxt <+> ptext SLIT("=>")
215 else -- Used in interfaces
216 ptext SLIT("__forall") <+> interppSP tvs <+>
217 ppr_context cxt <+> ptext SLIT("=>")
219 pprHsContext :: (Outputable name) => HsContext name -> SDoc
220 pprHsContext [] = empty
221 pprHsContext cxt = ppr_context cxt <+> ptext SLIT("=>")
223 ppr_context [] = empty
224 ppr_context cxt = parens (interpp'SP cxt)
228 pREC_TOP = (0 :: Int) -- type in ParseIface.y
229 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
230 pREC_CON = (2 :: Int) -- atype in ParseIface.y
232 maybeParen :: Bool -> SDoc -> SDoc
233 maybeParen True p = parens p
234 maybeParen False p = p
236 -- printing works more-or-less as for Types
238 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
240 pprHsType ty = ppr_mono_ty pREC_TOP ty
241 pprParendHsType ty = ppr_mono_ty pREC_CON ty
243 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
244 = maybeParen (ctxt_prec >= pREC_FUN) $
245 sep [pp_header, pprHsType ty]
247 pp_header = case maybe_tvs of
248 Just tvs -> pprHsForAll tvs ctxt
249 Nothing -> pprHsContext ctxt
251 ppr_mono_ty ctxt_prec (HsTyVar name)
254 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)
255 = let p1 = ppr_mono_ty pREC_FUN ty1
256 p2 = ppr_mono_ty pREC_TOP ty2
258 maybeParen (ctxt_prec >= pREC_FUN)
259 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
261 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = hsTupParens con (interpp'SP tys)
262 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
264 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
265 = maybeParen (ctxt_prec >= pREC_CON)
266 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
268 ppr_mono_ty ctxt_prec (HsPredTy pred)
271 ppr_mono_ty ctxt_prec (HsUsageTy u ty)
272 = maybeParen (ctxt_prec >= pREC_CON)
273 (sep [ptext SLIT("__u") <+> ppr_mono_ty pREC_CON u,
274 ppr_mono_ty pREC_CON ty])
275 -- pREC_FUN would be logical for u, but it yields a reduce/reduce conflict with AppTy
278 ppr_mono_ty ctxt_prec (HsNumTy n) = integer n
279 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2) = ppr ty1 <+> ppr op <+> ppr ty2
283 %************************************************************************
285 \subsection{Converting from Type to HsType}
287 %************************************************************************
289 @toHsType@ converts from a Type to a HsType, making the latter look as
290 user-friendly as possible. Notably, it uses synonyms where possible, and
291 expresses overloaded functions using the '=>' context part of a HsForAllTy.
294 toHsTyVar :: TyVar -> HsTyVarBndr Name
295 toHsTyVar tv = IfaceTyVar (getName tv) (tyVarKind tv)
297 toHsTyVars tvs = map toHsTyVar tvs
299 toHsType :: Type -> HsType Name
300 -- This function knows the representation of types
301 toHsType (TyVarTy tv) = HsTyVar (getName tv)
302 toHsType (FunTy arg res) = HsFunTy (toHsType arg) (toHsType res)
303 toHsType (AppTy fun arg) = HsAppTy (toHsType fun) (toHsType arg)
305 toHsType (NoteTy (SynNote ty@(TyConApp tycon tyargs)) real_ty)
306 | isNewTyCon tycon = toHsType ty
307 | syn_matches = toHsType ty -- Use synonyms if possible!!
310 pprTrace "WARNING: synonym info lost in .hi file for " (ppr syn_ty) $
312 toHsType real_ty -- but drop it if not.
314 syn_matches = ty_from_syn `tcEqType` real_ty
315 (tyvars,syn_ty) = getSynTyConDefn tycon
316 ty_from_syn = substTy (mkTyVarSubst tyvars tyargs) syn_ty
318 -- We only use the type synonym in the file if this doesn't cause
319 -- us to lose important information. This matters for usage
320 -- annotations. It's an issue if some of the args to the synonym
321 -- have arrows in them, or if the synonym's RHS has an arrow; for
322 -- example, with nofib/real/ebnf2ps/ in Parsers.using.
324 -- **! It would be nice if when this test fails we could still
325 -- write the synonym in as a Note, so we don't lose the info for
326 -- error messages, but it's too much work for right now.
329 toHsType (NoteTy _ ty) = toHsType ty
331 toHsType (SourceTy (NType tc tys)) = foldl HsAppTy (HsTyVar (getName tc)) (map toHsType tys)
332 toHsType (SourceTy pred) = HsPredTy (toHsPred pred)
334 toHsType ty@(TyConApp tc tys) -- Must be saturated because toHsType's arg is of kind *
335 | not saturated = generic_case
336 | isTupleTyCon tc = HsTupleTy (HsTupCon (getName tc) (tupleTyConBoxity tc) (tyConArity tc)) tys'
337 | tc `hasKey` listTyConKey = HsListTy (head tys')
338 | tc `hasKey` usOnceTyConKey = hsUsOnce_Name -- must print !, . unqualified
339 | tc `hasKey` usManyTyConKey = hsUsMany_Name -- must print !, . unqualified
340 | otherwise = generic_case
342 generic_case = foldl HsAppTy (HsTyVar (getName tc)) tys'
343 tys' = map toHsType tys
344 saturated = length tys == tyConArity tc
346 toHsType ty@(ForAllTy _ _) = case tcSplitSigmaTy ty of
347 (tvs, preds, tau) -> HsForAllTy (Just (map toHsTyVar tvs))
351 toHsType (UsageTy u ty) = HsUsageTy (toHsType u) (toHsType ty)
352 -- **! consider dropping usMany annotations ToDo KSW 2000-10
355 toHsPred (ClassP cls tys) = HsClassP (getName cls) (map toHsType tys)
356 toHsPred (IParam n ty) = HsIParam (getName n) (toHsType ty)
358 toHsContext :: ThetaType -> HsContext Name
359 toHsContext theta = map toHsPred theta
361 toHsFDs :: [FunDep TyVar] -> [FunDep Name]
362 toHsFDs fds = [(map getName ns, map getName ms) | (ns,ms) <- fds]
366 %************************************************************************
368 \subsection{Comparison}
370 %************************************************************************
373 instance Ord a => Eq (HsType a) where
374 -- The Ord is needed because we keep a
375 -- finite map of variables to variables
376 (==) a b = eq_hsType emptyEqHsEnv a b
378 instance Ord a => Eq (HsPred a) where
379 (==) a b = eq_hsPred emptyEqHsEnv a b
381 eqWithHsTyVars :: Ord name =>
382 [HsTyVarBndr name] -> [HsTyVarBndr name]
383 -> (EqHsEnv name -> Bool) -> Bool
384 eqWithHsTyVars = eq_hsTyVars emptyEqHsEnv
388 type EqHsEnv n = FiniteMap n n
389 -- Tracks the mapping from L-variables to R-variables
391 eq_hsVar :: Ord n => EqHsEnv n -> n -> n -> Bool
392 eq_hsVar env n1 n2 = case lookupFM env n1 of
396 extendEqHsEnv env n1 n2
398 | otherwise = addToFM env n1 n2
400 emptyEqHsEnv :: EqHsEnv n
401 emptyEqHsEnv = emptyFM
404 We do define a specialised equality for these \tr{*Type} types; used
405 in checking interfaces.
409 eq_hsTyVars env [] [] k = k env
410 eq_hsTyVars env (tv1:tvs1) (tv2:tvs2) k = eq_hsTyVar env tv1 tv2 $ \ env ->
411 eq_hsTyVars env tvs1 tvs2 k
412 eq_hsTyVars env _ _ _ = False
414 eq_hsTyVar env (UserTyVar v1) (UserTyVar v2) k = k (extendEqHsEnv env v1 v2)
415 eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 `eqKind` k2 && k (extendEqHsEnv env v1 v2)
416 eq_hsTyVar env _ _ _ = False
418 eq_hsVars env [] [] k = k env
419 eq_hsVars env (v1:bs1) (v2:bs2) k = eq_hsVars (extendEqHsEnv env v1 v2) bs1 bs2 k
420 eq_hsVars env _ _ _ = False
425 eq_hsTypes env = eqListBy (eq_hsType env)
428 eq_hsType env (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
429 = eq_tvs tvs1 tvs2 $ \env ->
430 eq_hsContext env c1 c2 &&
433 eq_tvs Nothing (Just _) k = False
434 eq_tvs Nothing Nothing k = k env
435 eq_tvs (Just _) Nothing k = False
436 eq_tvs (Just tvs1) (Just tvs2) k = eq_hsTyVars env tvs1 tvs2 k
438 eq_hsType env (HsTyVar n1) (HsTyVar n2)
441 eq_hsType env (HsTupleTy c1 tys1) (HsTupleTy c2 tys2)
442 = (c1 == c2) && eq_hsTypes env tys1 tys2
444 eq_hsType env (HsListTy ty1) (HsListTy ty2)
445 = eq_hsType env ty1 ty2
447 eq_hsType env (HsAppTy fun_ty1 arg_ty1) (HsAppTy fun_ty2 arg_ty2)
448 = eq_hsType env fun_ty1 fun_ty2 && eq_hsType env arg_ty1 arg_ty2
450 eq_hsType env (HsFunTy a1 b1) (HsFunTy a2 b2)
451 = eq_hsType env a1 a2 && eq_hsType env b1 b2
453 eq_hsType env (HsPredTy p1) (HsPredTy p2)
454 = eq_hsPred env p1 p2
456 eq_hsType env (HsUsageTy u1 ty1) (HsUsageTy u2 ty2)
457 = eq_hsType env u1 u2 && eq_hsType env ty1 ty2
459 eq_hsType env (HsOpTy lty1 op1 rty1) (HsOpTy lty2 op2 rty2)
460 = eq_hsVar env op1 op2 && eq_hsType env lty1 lty2 && eq_hsType env rty1 rty2
462 eq_hsType env ty1 ty2 = False
466 eq_hsContext env a b = eqListBy (eq_hsPred env) a b
469 eq_hsPred env (HsClassP c1 tys1) (HsClassP c2 tys2)
470 = c1 == c2 && eq_hsTypes env tys1 tys2
471 eq_hsPred env (HsIParam n1 ty1) (HsIParam n2 ty2)
472 = n1 == n2 && eq_hsType env ty1 ty2
473 eq_hsPred env _ _ = False