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, splitSigmaTy, unUsgTy, boxedTypeKind
32 import TypeRep ( Type(..), TyNote(..) ) -- toHsType sees the representation
33 import TyCon ( isTupleTyCon, tupleTyConBoxity, tyConArity, tyConClass_maybe )
34 import PrelInfo ( mkTupConRdrName )
35 import RdrName ( RdrName )
36 import Name ( toRdrName )
37 import OccName ( NameSpace )
38 import Var ( TyVar, tyVarKind )
39 import PprType ( {- instance Outputable Kind -}, pprParendKind )
40 import BasicTypes ( Arity, Boxity(..), tupleParens )
41 import Unique ( hasKey, listTyConKey, Uniquable(..) )
42 import Maybes ( maybeToBool )
47 This is the syntax for types as seen in type signatures.
50 type HsContext name = [HsPred name]
52 data HsPred name = HsPClass name [HsType name]
53 | HsPIParam name (HsType name)
56 = HsForAllTy (Maybe [HsTyVarBndr name]) -- Nothing for implicitly quantified signatures
60 | HsTyVar name -- Type variable
62 | HsAppTy (HsType name)
65 | HsFunTy (HsType name) -- function type
68 | HsListTy (HsType name) -- Element type
70 | HsTupleTy (HsTupCon name)
71 [HsType name] -- Element types (length gives arity)
73 -- these next two are only used in interfaces
74 | HsPredTy (HsPred name)
76 | HsUsgTy (HsUsageAnn name)
88 -----------------------
89 data HsTupCon name = HsTupCon name Boxity
91 instance Eq name => Eq (HsTupCon name) where
92 (HsTupCon _ b1) == (HsTupCon _ b2) = b1==b2
94 mkHsTupCon :: NameSpace -> Boxity -> [a] -> HsTupCon RdrName
95 mkHsTupCon space boxity args = HsTupCon (mkTupConRdrName space boxity (length args)) boxity
97 hsTupParens :: HsTupCon name -> SDoc -> SDoc
98 hsTupParens (HsTupCon _ b) p = tupleParens b p
100 -----------------------
101 -- Combine adjacent for-alls.
102 -- The following awkward situation can happen otherwise:
103 -- f :: forall a. ((Num a) => Int)
104 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
105 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
106 -- but the export list abstracts f wrt [a]. Disaster.
108 -- A valid type must have one for-all at the top of the type, or of the fn arg types
110 mkHsForAllTy (Just []) [] ty = ty -- Explicit for-all with no tyvars
111 mkHsForAllTy mtvs1 [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
113 mtvs1 `plus` Nothing = mtvs1
114 Nothing `plus` mtvs2 = mtvs2
115 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
116 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
118 mkHsUsForAllTy uvs ty = foldr (\ uv ty -> HsUsgForAllTy uv ty)
121 mkHsDictTy cls tys = HsPredTy (HsPClass cls tys)
122 mkHsIParamTy v ty = HsPredTy (HsPIParam v ty)
124 data HsTyVarBndr name
126 | IfaceTyVar name Kind
127 -- *** NOTA BENE *** A "monotype" in a pragma can have
128 -- for-alls in it, (mostly to do with dictionaries). These
129 -- must be explicitly Kinded.
131 getTyVarName (UserTyVar n) = n
132 getTyVarName (IfaceTyVar n _) = n
134 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
135 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
136 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
140 %************************************************************************
142 \subsection{Pretty printing}
144 %************************************************************************
146 NB: these types get printed into interface files, so
147 don't change the printing format lightly
150 instance (Outputable name) => Outputable (HsType name) where
151 ppr ty = pprHsType ty
153 instance (Outputable name) => Outputable (HsTyVarBndr name) where
154 ppr (UserTyVar name) = ppr name
155 ppr (IfaceTyVar name kind) = pprHsTyVarBndr name kind
157 instance Outputable name => Outputable (HsPred name) where
158 ppr (HsPClass clas tys) = ppr clas <+> hsep (map pprParendHsType tys)
159 ppr (HsPIParam n ty) = hsep [{- char '?' <> -} ppr n, text "::", ppr ty]
161 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
162 pprHsTyVarBndr name kind | kind == boxedTypeKind = ppr name
163 | otherwise = hsep [ppr name, dcolon, pprParendKind kind]
165 pprHsForAll [] [] = empty
167 -- This printer is used for both interface files and
168 -- printing user types in error messages; and alas the
169 -- two use slightly different syntax. Ah well.
170 = getPprStyle $ \ sty ->
171 if userStyle sty then
172 ptext SLIT("forall") <+> interppSP tvs <> dot <+>
176 ppr_context cxt <+> ptext SLIT("=>")
178 else -- Used in interfaces
179 ptext SLIT("__forall") <+> interppSP tvs <+>
180 ppr_context cxt <+> ptext SLIT("=>")
182 pprHsContext :: (Outputable name) => HsContext name -> SDoc
183 pprHsContext [] = empty
184 pprHsContext cxt = ppr_context cxt <+> ptext SLIT("=>")
186 ppr_context [] = empty
187 ppr_context cxt = parens (interpp'SP cxt)
191 pREC_TOP = (0 :: Int)
192 pREC_FUN = (1 :: Int)
193 pREC_CON = (2 :: Int)
195 maybeParen :: Bool -> SDoc -> SDoc
196 maybeParen True p = parens p
197 maybeParen False p = p
199 -- printing works more-or-less as for Types
201 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
203 pprHsType ty = ppr_mono_ty pREC_TOP ty
204 pprParendHsType ty = ppr_mono_ty pREC_CON ty
206 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
207 = maybeParen (ctxt_prec >= pREC_FUN) $
208 sep [pp_header, pprHsType ty]
210 pp_header = case maybe_tvs of
211 Just tvs -> pprHsForAll tvs ctxt
212 Nothing -> pprHsContext ctxt
214 ppr_mono_ty ctxt_prec (HsTyVar name)
217 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)
218 = let p1 = ppr_mono_ty pREC_FUN ty1
219 p2 = ppr_mono_ty pREC_TOP ty2
221 maybeParen (ctxt_prec >= pREC_FUN)
222 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
224 ppr_mono_ty ctxt_prec (HsTupleTy con tys) = hsTupParens con (interpp'SP tys)
225 ppr_mono_ty ctxt_prec (HsListTy ty) = brackets (ppr_mono_ty pREC_TOP ty)
227 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
228 = maybeParen (ctxt_prec >= pREC_CON)
229 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
231 ppr_mono_ty ctxt_prec (HsPredTy pred)
232 = maybeParen (ctxt_prec >= pREC_FUN) $
235 ppr_mono_ty ctxt_prec ty@(HsUsgForAllTy _ _)
237 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
238 ppr_mono_ty pREC_TOP sigma
241 (uvars,sigma) = split [] ty
242 pp_uvars = interppSP uvars
244 split uvs (HsUsgForAllTy uv ty') = split (uv:uvs) ty'
245 split uvs ty' = (reverse uvs,ty')
247 ppr_mono_ty ctxt_prec (HsUsgTy u ty)
248 = maybeParen (ctxt_prec >= pREC_CON) $
249 ptext SLIT("__u") <+> pp_ua <+> ppr_mono_ty pREC_CON ty
252 HsUsOnce -> ptext SLIT("-")
253 HsUsMany -> ptext SLIT("!")
258 %************************************************************************
260 \subsection{Converting from Type to HsType}
262 %************************************************************************
264 @toHsType@ converts from a Type to a HsType, making the latter look as
265 user-friendly as possible. Notably, it uses synonyms where possible, and
266 expresses overloaded functions using the '=>' context part of a HsForAllTy.
269 toHsTyVar :: TyVar -> HsTyVarBndr RdrName
270 toHsTyVar tv = IfaceTyVar (toRdrName tv) (tyVarKind tv)
272 toHsTyVars tvs = map toHsTyVar tvs
274 toHsType :: Type -> HsType RdrName
275 toHsType ty = toHsType' (unUsgTy ty)
276 -- For now we just discard the usage
277 -- = case splitUsgTy ty of
278 -- (usg, tau) -> HsUsgTy (toHsUsg usg) (toHsType' tau)
280 toHsType' :: Type -> HsType RdrName
281 -- Called after the usage is stripped off
282 -- This function knows the representation of types
283 toHsType' (TyVarTy tv) = HsTyVar (toRdrName tv)
284 toHsType' (FunTy arg res) = HsFunTy (toHsType arg) (toHsType res)
285 toHsType' (AppTy fun arg) = HsAppTy (toHsType fun) (toHsType arg)
287 toHsType' (NoteTy (SynNote ty) _) = toHsType ty -- Use synonyms if possible!!
288 toHsType' (NoteTy _ ty) = toHsType ty
290 toHsType' ty@(TyConApp tc tys) -- Must be saturated because toHsType's arg is of kind *
291 | not saturated = generic_case
292 | isTupleTyCon tc = HsTupleTy (HsTupCon (toRdrName tc) (tupleTyConBoxity tc)) tys'
293 | tc `hasKey` listTyConKey = HsListTy (head tys')
294 | maybeToBool maybe_class = HsPredTy (HsPClass (toRdrName clas) tys')
295 | otherwise = generic_case
297 generic_case = foldl HsAppTy (HsTyVar (toRdrName tc)) tys'
298 maybe_class = tyConClass_maybe tc
299 Just clas = maybe_class
300 tys' = map toHsType tys
301 saturated = length tys == tyConArity tc
303 toHsType' ty@(ForAllTy _ _) = case splitSigmaTy ty of
304 (tvs, preds, tau) -> HsForAllTy (Just (map toHsTyVar tvs))
309 toHsPred (Class cls tys) = HsPClass (toRdrName cls) (map toHsType tys)
310 toHsPred (IParam n ty) = HsPIParam (toRdrName n) (toHsType ty)
312 toHsContext :: ClassContext -> HsContext RdrName
313 toHsContext cxt = [HsPClass (toRdrName cls) (map toHsType tys) | (cls,tys) <- cxt]
315 toHsUsg UsOnce = HsUsOnce
316 toHsUsg UsMany = HsUsMany
317 toHsUsg (UsVar v) = HsUsVar (toRdrName v)
319 toHsFDs :: [FunDep TyVar] -> [FunDep RdrName]
320 toHsFDs fds = [(map toRdrName ns, map toRdrName ms) | (ns,ms) <- fds]
324 %************************************************************************
326 \subsection{Comparison}
328 %************************************************************************
331 instance Ord a => Eq (HsType a) where
332 -- The Ord is needed because we keep a
333 -- finite map of variables to variables
334 (==) a b = eq_hsType emptyEqHsEnv a b
336 instance Ord a => Eq (HsPred a) where
337 (==) a b = eq_hsPred emptyEqHsEnv a b
339 eqWithHsTyVars :: Ord name =>
340 [HsTyVarBndr name] -> [HsTyVarBndr name]
341 -> (EqHsEnv name -> Bool) -> Bool
342 eqWithHsTyVars = eq_hsTyVars emptyEqHsEnv
346 type EqHsEnv n = FiniteMap n n
347 -- Tracks the mapping from L-variables to R-variables
349 eq_hsVar :: Ord n => EqHsEnv n -> n -> n -> Bool
350 eq_hsVar env n1 n2 = case lookupFM env n1 of
354 extendEqHsEnv env n1 n2
356 | otherwise = addToFM env n1 n2
358 emptyEqHsEnv :: EqHsEnv n
359 emptyEqHsEnv = emptyFM
362 We do define a specialised equality for these \tr{*Type} types; used
363 in checking interfaces.
367 eq_hsTyVars env [] [] k = k env
368 eq_hsTyVars env (tv1:tvs1) (tv2:tvs2) k = eq_hsTyVar env tv1 tv2 $ \ env ->
369 eq_hsTyVars env tvs1 tvs2 k
370 eq_hsTyVars env _ _ _ = False
372 eq_hsTyVar env (UserTyVar v1) (UserTyVar v2) k = k (extendEqHsEnv env v1 v2)
373 eq_hsTyVar env (IfaceTyVar v1 k1) (IfaceTyVar v2 k2) k = k1 == k2 && k (extendEqHsEnv env v1 v2)
374 eq_hsTyVar env _ _ _ = False
376 eq_hsVars env [] [] k = k env
377 eq_hsVars env (v1:bs1) (v2:bs2) k = eq_hsVars (extendEqHsEnv env v1 v2) bs1 bs2 k
378 eq_hsVars env _ _ _ = False
383 eq_hsTypes env = eqListBy (eq_hsType env)
386 eq_hsType env (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
387 = eq_tvs tvs1 tvs2 $ \env ->
388 eq_hsContext env c1 c2 &&
391 eq_tvs Nothing (Just _) k = False
392 eq_tvs Nothing Nothing k = k env
393 eq_tvs (Just _) Nothing k = False
394 eq_tvs (Just tvs1) (Just tvs2) k = eq_hsTyVars env tvs1 tvs2 k
396 eq_hsType env (HsTyVar n1) (HsTyVar n2)
399 eq_hsType env (HsTupleTy c1 tys1) (HsTupleTy c2 tys2)
400 = (c1 == c2) && eq_hsTypes env tys1 tys2
402 eq_hsType env (HsListTy ty1) (HsListTy ty2)
403 = eq_hsType env ty1 ty2
405 eq_hsType env (HsAppTy fun_ty1 arg_ty1) (HsAppTy fun_ty2 arg_ty2)
406 = eq_hsType env fun_ty1 fun_ty2 && eq_hsType env arg_ty1 arg_ty2
408 eq_hsType env (HsFunTy a1 b1) (HsFunTy a2 b2)
409 = eq_hsType env a1 a2 && eq_hsType env b1 b2
411 eq_hsType env (HsPredTy p1) (HsPredTy p2)
412 = eq_hsPred env p1 p2
414 eq_hsType env (HsUsgTy u1 ty1) (HsUsgTy u2 ty2)
415 = eqUsg u1 u2 && eq_hsType env ty1 ty2
417 eq_hsType env ty1 ty2 = False
421 eq_hsContext env a b = eqListBy (eq_hsPred env) a b
424 eq_hsPred env (HsPClass c1 tys1) (HsPClass c2 tys2)
425 = c1 == c2 && eq_hsTypes env tys1 tys2
426 eq_hsPred env (HsPIParam n1 ty1) (HsPIParam n2 ty2)
427 = n1 == n2 && eq_hsType env ty1 ty2
428 eq_hsPred env _ _ = False
431 eqUsg HsUsOnce HsUsOnce = True
432 eqUsg HsUsMany HsUsMany = True
433 eqUsg (HsUsVar u1) (HsUsVar u2) = u1 == u2
437 eqListBy :: (a->a->Bool) -> [a] -> [a] -> Bool
438 eqListBy eq [] [] = True
439 eqListBy eq (x:xs) (y:ys) = eq x y && eqListBy eq xs ys
440 eqListBy eq xs ys = False