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
18 , PostTcType, placeHolderType,
21 , pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context, pprHsTyVarBndr
23 -- Equality over Hs things
24 , EqHsEnv, emptyEqHsEnv, extendEqHsEnv,
25 , eqWithHsTyVars, eq_hsVar, eq_hsVars, eq_hsTyVars, eq_hsType, eq_hsContext, eqListBy
27 -- Converting from Type to HsType
28 , toHsType, toHsTyVar, toHsTyVars, toHsContext, toHsFDs
31 #include "HsVersions.h"
33 import Class ( FunDep )
34 import TcType ( Type, Kind, ThetaType, SourceType(..),
35 tcSplitSigmaTy, liftedTypeKind, eqKind, tcEqType
37 import TypeRep ( Type(..), TyNote(..) ) -- toHsType sees the representation
38 import TyCon ( isTupleTyCon, tupleTyConBoxity, tyConArity, isNewTyCon, getSynTyConDefn )
39 import RdrName ( RdrName, mkUnqual )
40 import Name ( Name, getName )
41 import OccName ( NameSpace, tvName )
42 import Var ( TyVar, tyVarKind )
43 import Subst ( substTyWith )
44 import PprType ( {- instance Outputable Kind -}, pprParendKind )
45 import BasicTypes ( Boxity(..), Arity, IPName, tupleParens )
46 import PrelNames ( mkTupConRdrName, listTyConKey, parrTyConKey,
47 usOnceTyConKey, usManyTyConKey, hasKey,
48 usOnceTyConName, usManyTyConName )
50 import Util ( eqListBy, lengthIs )
55 %************************************************************************
57 \subsection{Annotating the syntax}
59 %************************************************************************
62 type PostTcType = Type -- Used for slots in the abstract syntax
63 -- where we want to keep slot for a type
64 -- to be added by the type checker...but
65 -- before typechecking it's just bogus
67 placeHolderType :: PostTcType -- Used before typechecking
68 placeHolderType = panic "Evaluated the place holder for a PostTcType"
72 %************************************************************************
74 \subsection{Data types}
76 %************************************************************************
78 This is the syntax for types as seen in type signatures.
81 type HsContext name = [HsPred name]
83 data HsPred name = HsClassP name [HsType name]
84 | HsIParam (IPName name) (HsType name)
87 = HsForAllTy (Maybe [HsTyVarBndr name]) -- Nothing for implicitly quantified signatures
91 | HsTyVar name -- Type variable or type constructor
93 | HsAppTy (HsType name)
96 | HsFunTy (HsType name) -- function type
99 | HsListTy (HsType name) -- Element type
101 | HsPArrTy (HsType name) -- Elem. type of parallel array: [:t:]
103 | HsTupleTy (HsTupCon name)
104 [HsType name] -- Element types (length gives arity)
106 | HsOpTy (HsType name) name (HsType name)
109 -- these next two are only used in interfaces
110 | HsPredTy (HsPred name)
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
176 getHsInstHead :: HsType name -> ([HsTyVarBndr name], (name, [HsType name]))
177 -- Split up an instance decl type, returning the 'head' part
179 -- In interface fiels, the type of the decl is held like this:
180 -- forall a. Foo a -> Baz (T a)
181 -- so we have to strip off function argument types,
182 -- as well as the bit before the '=>' (which is always
183 -- empty in interface files)
185 -- The parser ensures the type will have the right shape.
186 -- (e.g. see ParseUtil.checkInstType)
188 getHsInstHead (HsForAllTy (Just tvs) _ tau) = (tvs, get_head1 tau)
189 getHsInstHead tau = ([], get_head1 tau)
191 get_head1 (HsFunTy _ ty) = get_head1 ty
192 get_head1 (HsPredTy (HsClassP cls tys)) = (cls,tys)
196 %************************************************************************
198 \subsection{Pretty printing}
200 %************************************************************************
202 NB: these types get printed into interface files, so
203 don't change the printing format lightly
206 instance (Outputable name) => Outputable (HsType name) where
207 ppr ty = pprHsType ty
209 instance (Outputable name) => Outputable (HsTyVarBndr name) where
210 ppr (UserTyVar name) = ppr name
211 ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
213 instance Outputable name => Outputable (HsPred name) where
214 ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
215 ppr (HsIParam n ty) = hsep [ppr n, dcolon, ppr ty]
217 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
218 pprHsTyVarBndr name kind | kind `eqKind` liftedTypeKind = ppr name
219 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
221 pprHsForAll [] [] = empty
223 -- This printer is used for both interface files and
224 -- printing user types in error messages; and alas the
225 -- two use slightly different syntax. Ah well.
226 = getPprStyle $ \ sty ->
227 if userStyle sty then
228 ptext SLIT("forall") <+> interppSP tvs <> dot <+>
229 -- **! ToDo: want to hide uvars from user, but not enough info
230 -- in a HsTyVarBndr name (see PprType). KSW 2000-10.
232 else -- Used in interfaces
233 ptext SLIT("__forall") <+> interppSP tvs <+>
234 ppr_hs_context cxt <+> ptext SLIT("=>")
236 pprHsContext :: (Outputable name) => HsContext name -> SDoc
237 pprHsContext [] = empty
238 pprHsContext cxt = ppr_hs_context cxt <+> ptext SLIT("=>")
240 ppr_hs_context [] = empty
241 ppr_hs_context cxt = parens (interpp'SP cxt)
245 pREC_TOP = (0 :: Int) -- type in ParseIface.y
246 pREC_FUN = (1 :: Int) -- btype in ParseIface.y
247 pREC_CON = (2 :: Int) -- atype in ParseIface.y
249 maybeParen :: Bool -> SDoc -> SDoc
250 maybeParen True p = parens p
251 maybeParen False p = p
253 -- printing works more-or-less as for Types
255 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
257 pprHsType ty = ppr_mono_ty pREC_TOP ty
258 pprParendHsType ty = ppr_mono_ty pREC_CON ty
260 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
261 = maybeParen (ctxt_prec >= pREC_FUN) $
262 sep [pp_header, pprHsType ty]
264 pp_header = case maybe_tvs of
265 Just tvs -> pprHsForAll tvs ctxt
266 Nothing -> pprHsContext ctxt
268 ppr_mono_ty ctxt_prec (HsTyVar name)
271 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)
272 = let p1 = ppr_mono_ty pREC_FUN ty1
273 p2 = ppr_mono_ty pREC_TOP ty2
275 maybeParen (ctxt_prec >= pREC_FUN)
276 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
278 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = hsTupParens con (interpp'SP tys)
279 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
280 ppr_mono_ty ctxt_prec (HsPArrTy ty) = pabrackets (ppr_mono_ty pREC_TOP ty)
282 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
284 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
285 = maybeParen (ctxt_prec >= pREC_CON)
286 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
288 ppr_mono_ty ctxt_prec (HsPredTy pred)
292 ppr_mono_ty ctxt_prec (HsNumTy n) = integer n
293 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2) = ppr ty1 <+> ppr op <+> ppr ty2
297 %************************************************************************
299 \subsection{Converting from Type to HsType}
301 %************************************************************************
303 @toHsType@ converts from a Type to a HsType, making the latter look as
304 user-friendly as possible. Notably, it uses synonyms where possible, and
305 expresses overloaded functions using the '=>' context part of a HsForAllTy.
308 toHsTyVar :: TyVar -> HsTyVarBndr Name
309 toHsTyVar tv = IfaceTyVar (getName tv) (tyVarKind tv)
311 toHsTyVars tvs = map toHsTyVar tvs
313 toHsType :: Type -> HsType Name
314 -- This function knows the representation of types
315 toHsType (TyVarTy tv) = HsTyVar (getName tv)
316 toHsType (FunTy arg res) = HsFunTy (toHsType arg) (toHsType res)
317 toHsType (AppTy fun arg) = HsAppTy (toHsType fun) (toHsType arg)
319 toHsType (NoteTy (SynNote ty@(TyConApp tycon tyargs)) real_ty)
320 | isNewTyCon tycon = toHsType ty
321 | syn_matches = toHsType ty -- Use synonyms if possible!!
324 pprTrace "WARNING: synonym info lost in .hi file for " (ppr syn_ty) $
326 toHsType real_ty -- but drop it if not.
328 syn_matches = ty_from_syn `tcEqType` real_ty
329 (tyvars,syn_ty) = getSynTyConDefn tycon
330 ty_from_syn = substTyWith tyvars tyargs syn_ty
332 -- We only use the type synonym in the file if this doesn't cause
333 -- us to lose important information. This matters for usage
334 -- annotations. It's an issue if some of the args to the synonym
335 -- have arrows in them, or if the synonym's RHS has an arrow; for
336 -- example, with nofib/real/ebnf2ps/ in Parsers.using.
338 -- **! It would be nice if when this test fails we could still
339 -- write the synonym in as a Note, so we don't lose the info for
340 -- error messages, but it's too much work for right now.
343 toHsType (NoteTy _ ty) = toHsType ty
345 toHsType (SourceTy (NType tc tys)) = foldl HsAppTy (HsTyVar (getName tc)) (map toHsType tys)
346 toHsType (SourceTy pred) = HsPredTy (toHsPred pred)
348 toHsType ty@(TyConApp tc tys) -- Must be saturated because toHsType's arg is of kind *
349 | not saturated = generic_case
350 | isTupleTyCon tc = HsTupleTy (HsTupCon (getName tc) (tupleTyConBoxity tc) (tyConArity tc)) tys'
351 | tc `hasKey` listTyConKey = HsListTy (head tys')
352 | tc `hasKey` parrTyConKey = HsPArrTy (head tys')
353 | tc `hasKey` usOnceTyConKey = hsUsOnce_Name -- must print !, . unqualified
354 | tc `hasKey` usManyTyConKey = hsUsMany_Name -- must print !, . unqualified
355 | otherwise = generic_case
357 generic_case = foldl HsAppTy (HsTyVar (getName tc)) tys'
358 tys' = map toHsType tys
359 saturated = tys `lengthIs` tyConArity tc
361 toHsType ty@(ForAllTy _ _) = case tcSplitSigmaTy ty of
362 (tvs, preds, tau) -> HsForAllTy (Just (map toHsTyVar tvs))
366 toHsPred (ClassP cls tys) = HsClassP (getName cls) (map toHsType tys)
367 toHsPred (IParam n ty) = HsIParam n (toHsType ty)
369 toHsContext :: ThetaType -> HsContext Name
370 toHsContext theta = map toHsPred theta
372 toHsFDs :: [FunDep TyVar] -> [FunDep Name]
373 toHsFDs fds = [(map getName ns, map getName ms) | (ns,ms) <- fds]
377 %************************************************************************
379 \subsection{Comparison}
381 %************************************************************************
384 instance Ord a => Eq (HsType a) where
385 -- The Ord is needed because we keep a
386 -- finite map of variables to variables
387 (==) a b = eq_hsType emptyEqHsEnv a b
389 instance Ord a => Eq (HsPred a) where
390 (==) a b = eq_hsPred emptyEqHsEnv a b
392 eqWithHsTyVars :: Ord name =>
393 [HsTyVarBndr name] -> [HsTyVarBndr name]
394 -> (EqHsEnv name -> Bool) -> Bool
395 eqWithHsTyVars = eq_hsTyVars emptyEqHsEnv
399 type EqHsEnv n = FiniteMap n n
400 -- Tracks the mapping from L-variables to R-variables
402 eq_hsVar :: Ord n => EqHsEnv n -> n -> n -> Bool
403 eq_hsVar env n1 n2 = case lookupFM env n1 of
407 extendEqHsEnv env n1 n2
409 | otherwise = addToFM env n1 n2
411 emptyEqHsEnv :: EqHsEnv n
412 emptyEqHsEnv = emptyFM
415 We do define a specialised equality for these \tr{*Type} types; used
416 in checking interfaces.
420 eq_hsTyVars env [] [] k = k env
421 eq_hsTyVars env (tv1:tvs1) (tv2:tvs2) k = eq_hsTyVar env tv1 tv2 $ \ env ->
422 eq_hsTyVars env tvs1 tvs2 k
423 eq_hsTyVars env _ _ _ = False
425 eq_hsTyVar env (UserTyVar v1) (UserTyVar v2) k = k (extendEqHsEnv env v1 v2)
426 eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 `eqKind` k2 && k (extendEqHsEnv env v1 v2)
427 eq_hsTyVar env _ _ _ = False
429 eq_hsVars env [] [] k = k env
430 eq_hsVars env (v1:bs1) (v2:bs2) k = eq_hsVars (extendEqHsEnv env v1 v2) bs1 bs2 k
431 eq_hsVars env _ _ _ = False
436 eq_hsTypes env = eqListBy (eq_hsType env)
439 eq_hsType env (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
440 = eq_tvs tvs1 tvs2 $ \env ->
441 eq_hsContext env c1 c2 &&
444 eq_tvs Nothing (Just _) k = False
445 eq_tvs Nothing Nothing k = k env
446 eq_tvs (Just _) Nothing k = False
447 eq_tvs (Just tvs1) (Just tvs2) k = eq_hsTyVars env tvs1 tvs2 k
449 eq_hsType env (HsTyVar n1) (HsTyVar n2)
452 eq_hsType env (HsTupleTy c1 tys1) (HsTupleTy c2 tys2)
453 = (c1 == c2) && eq_hsTypes env tys1 tys2
455 eq_hsType env (HsListTy ty1) (HsListTy ty2)
456 = eq_hsType env ty1 ty2
458 eq_hsType env (HsPArrTy ty1) (HsPArrTy ty2)
459 = eq_hsType env ty1 ty2
461 eq_hsType env (HsAppTy fun_ty1 arg_ty1) (HsAppTy fun_ty2 arg_ty2)
462 = eq_hsType env fun_ty1 fun_ty2 && eq_hsType env arg_ty1 arg_ty2
464 eq_hsType env (HsFunTy a1 b1) (HsFunTy a2 b2)
465 = eq_hsType env a1 a2 && eq_hsType env b1 b2
467 eq_hsType env (HsPredTy p1) (HsPredTy p2)
468 = eq_hsPred env p1 p2
470 eq_hsType env (HsOpTy lty1 op1 rty1) (HsOpTy lty2 op2 rty2)
471 = eq_hsVar env op1 op2 && eq_hsType env lty1 lty2 && eq_hsType env rty1 rty2
473 eq_hsType env ty1 ty2 = False
477 eq_hsContext env a b = eqListBy (eq_hsPred env) a b
480 eq_hsPred env (HsClassP c1 tys1) (HsClassP c2 tys2)
481 = c1 == c2 && eq_hsTypes env tys1 tys2
482 eq_hsPred env (HsIParam n1 ty1) (HsIParam n2 ty2)
483 = n1 == n2 && eq_hsType env ty1 ty2
484 eq_hsPred env _ _ = False