2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsTypes]{Abstract syntax: user-defined types}
8 HsType(..), MonoUsageAnn(..), HsTyVar(..),
9 Context, ClassAssertion
11 , mkHsForAllTy, mkHsUsForAllTy
12 , getTyVarName, replaceTyVarName
14 , pprForAll, pprContext, pprClassAssertion
15 , cmpHsType, cmpHsTypes, cmpContext
18 #include "HsVersions.h"
20 import Type ( Kind, UsageAnn(..) )
21 import PprType ( {- instance Outputable Kind -} )
23 import Util ( thenCmp, cmpList )
26 This is the syntax for types as seen in type signatures.
29 type Context name = [ClassAssertion name]
31 type ClassAssertion name = (name, [HsType name])
32 -- The type is usually a type variable, but it
33 -- doesn't have to be when reading interface files
36 = HsForAllTy (Maybe [HsTyVar name]) -- Nothing for implicitly quantified signatures
40 | MonoTyVar name -- Type variable
42 | MonoTyApp (HsType name)
45 | MonoFunTy (HsType name) -- function type
48 | MonoListTy (HsType name) -- Element type
50 | MonoTupleTy [HsType name] -- Element types (length gives arity)
53 -- these next two are only used in interfaces
54 | MonoDictTy name -- Class
57 | MonoUsgTy (MonoUsageAnn name)
60 | MonoUsgForAllTy name
63 data MonoUsageAnn name
69 -- Combine adjacent for-alls.
70 -- The following awkward situation can happen otherwise:
71 -- f :: forall a. ((Num a) => Int)
72 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
73 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
74 -- but the export list abstracts f wrt [a]. Disaster.
76 -- A valid type must have one for-all at the top of the type, or of the fn arg types
78 mkHsForAllTy (Just []) [] ty = ty -- Explicit for-all with no tyvars
79 mkHsForAllTy mtvs1 [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
81 mtvs1 `plus` Nothing = mtvs1
82 Nothing `plus` mtvs2 = mtvs2
83 (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
84 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
86 mkHsUsForAllTy uvs ty = foldr (\ uv ty -> MonoUsgForAllTy uv ty)
91 | IfaceTyVar name Kind
92 -- *** NOTA BENE *** A "monotype" in a pragma can have
93 -- for-alls in it, (mostly to do with dictionaries). These
94 -- must be explicitly Kinded.
96 getTyVarName (UserTyVar n) = n
97 getTyVarName (IfaceTyVar n _) = n
99 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
100 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
101 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
105 %************************************************************************
107 \subsection{Pretty printing}
109 %************************************************************************
113 instance (Outputable name) => Outputable (HsType name) where
114 ppr ty = pprHsType ty
116 instance (Outputable name) => Outputable (HsTyVar name) where
117 ppr (UserTyVar name) = ppr name
118 ppr (IfaceTyVar name kind) = hsep [ppr name, dcolon, ppr kind]
120 -- Better to see those for-alls
121 -- pprForAll [] = empty
122 pprForAll tvs = ptext SLIT("forall") <+> interppSP tvs <> ptext SLIT(".")
124 pprContext :: (Outputable name) => Context name -> SDoc
125 pprContext [] = empty
126 pprContext context = parens (hsep (punctuate comma (map pprClassAssertion context))) <+> ptext SLIT("=>")
128 pprClassAssertion :: (Outputable name) => ClassAssertion name -> SDoc
129 pprClassAssertion (clas, tys)
130 = ppr clas <+> hsep (map pprParendHsType tys)
134 pREC_TOP = (0 :: Int)
135 pREC_FUN = (1 :: Int)
136 pREC_CON = (2 :: Int)
138 maybeParen :: Bool -> SDoc -> SDoc
139 maybeParen True p = parens p
140 maybeParen False p = p
142 -- printing works more-or-less as for Types
144 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
146 pprHsType ty = ppr_mono_ty pREC_TOP ty
147 pprParendHsType ty = ppr_mono_ty pREC_CON ty
149 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
150 = maybeParen (ctxt_prec >= pREC_FUN) $
151 sep [pp_tvs, pprContext ctxt, pprHsType ty]
153 pp_tvs = case maybe_tvs of
154 Just tvs -> pprForAll tvs
155 Nothing -> text "{- implicit forall -}"
157 ppr_mono_ty ctxt_prec (MonoTyVar name)
160 ppr_mono_ty ctxt_prec (MonoFunTy ty1 ty2)
161 = let p1 = ppr_mono_ty pREC_FUN ty1
162 p2 = ppr_mono_ty pREC_TOP ty2
164 maybeParen (ctxt_prec >= pREC_FUN)
165 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
167 ppr_mono_ty ctxt_prec (MonoTupleTy tys True)
168 = parens (sep (punctuate comma (map ppr tys)))
169 ppr_mono_ty ctxt_prec (MonoTupleTy tys False)
170 = ptext SLIT("(#") <> sep (punctuate comma (map ppr tys)) <> ptext SLIT("#)")
172 ppr_mono_ty ctxt_prec (MonoListTy ty)
173 = brackets (ppr_mono_ty pREC_TOP ty)
175 ppr_mono_ty ctxt_prec (MonoTyApp fun_ty arg_ty)
176 = maybeParen (ctxt_prec >= pREC_CON)
177 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
179 ppr_mono_ty ctxt_prec (MonoDictTy clas tys)
180 = ppr clas <+> hsep (map (ppr_mono_ty pREC_CON) tys)
182 ppr_mono_ty ctxt_prec ty@(MonoUsgForAllTy _ _)
183 = maybeParen (ctxt_prec >= pREC_FUN) $
184 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
185 ppr_mono_ty pREC_TOP sigma
188 (uvars,sigma) = split [] ty
189 pp_uvars = interppSP uvars
191 split uvs (MonoUsgForAllTy uv ty') = split (uv:uvs) ty'
192 split uvs ty' = (reverse uvs,ty')
194 ppr_mono_ty ctxt_prec (MonoUsgTy u ty)
195 = maybeParen (ctxt_prec >= pREC_CON) $
196 ptext SLIT("__u") <+> pp_ua <+> ppr_mono_ty pREC_CON ty
199 MonoUsOnce -> ptext SLIT("-")
200 MonoUsMany -> ptext SLIT("!")
201 MonoUsVar uv -> ppr uv
205 %************************************************************************
207 \subsection{Comparison}
209 %************************************************************************
211 We do define a specialised equality for these \tr{*Type} types; used
212 in checking interfaces. Most any other use is likely to be {\em
213 wrong}, so be careful!
216 cmpHsTyVar :: (a -> a -> Ordering) -> HsTyVar a -> HsTyVar a -> Ordering
217 cmpHsType :: (a -> a -> Ordering) -> HsType a -> HsType a -> Ordering
218 cmpHsTypes :: (a -> a -> Ordering) -> [HsType a] -> [HsType a] -> Ordering
219 cmpContext :: (a -> a -> Ordering) -> Context a -> Context a -> Ordering
221 cmpHsTyVar cmp (UserTyVar v1) (UserTyVar v2) = v1 `cmp` v2
222 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
223 cmpHsTyVar cmp (UserTyVar _) other = LT
224 cmpHsTyVar cmp other1 other2 = GT
227 cmpHsTypes cmp [] [] = EQ
228 cmpHsTypes cmp [] tys2 = LT
229 cmpHsTypes cmp tys1 [] = GT
230 cmpHsTypes cmp (ty1:tys1) (ty2:tys2) = cmpHsType cmp ty1 ty2 `thenCmp` cmpHsTypes cmp tys1 tys2
232 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
233 = cmpMaybe (cmpList (cmpHsTyVar cmp)) tvs1 tvs2 `thenCmp`
234 cmpContext cmp c1 c2 `thenCmp`
237 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
240 cmpHsType cmp (MonoTupleTy tys1 b1) (MonoTupleTy tys2 b2)
241 = (b1 `compare` b2) `thenCmp` cmpHsTypes cmp tys1 tys2
243 cmpHsType cmp (MonoListTy ty1) (MonoListTy ty2)
244 = cmpHsType cmp ty1 ty2
246 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
247 = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
249 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
250 = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
252 cmpHsType cmp (MonoDictTy c1 tys1) (MonoDictTy c2 tys2)
253 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
255 cmpHsType cmp (MonoUsgTy u1 ty1) (MonoUsgTy u2 ty2)
256 = cmpUsg cmp u1 u2 `thenCmp` cmpHsType cmp ty1 ty2
258 cmpHsType cmp ty1 ty2 -- tags must be different
262 if tag1 _LT_ tag2 then LT else GT
264 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
265 tag (MonoTupleTy tys1 _) = ILIT(2)
266 tag (MonoListTy ty1) = ILIT(3)
267 tag (MonoTyApp tc1 tys1) = ILIT(4)
268 tag (MonoFunTy a1 b1) = ILIT(5)
269 tag (MonoDictTy c1 tys1) = ILIT(6)
270 tag (MonoUsgTy c1 ty1) = ILIT(7)
271 tag (MonoUsgForAllTy uv1 ty1) = ILIT(8)
272 tag (HsForAllTy _ _ _) = ILIT(9)
276 = cmpList cmp_ctxt a b
278 cmp_ctxt (c1, tys1) (c2, tys2)
279 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
281 cmpUsg cmp MonoUsOnce MonoUsOnce = EQ
282 cmpUsg cmp MonoUsMany MonoUsMany = EQ
283 cmpUsg cmp (MonoUsVar u1) (MonoUsVar u2) = cmp u1 u2
285 cmpUsg cmp ua1 ua2 -- tags must be different
289 if tag1 _LT_ tag2 then LT else GT
291 tag MonoUsOnce = (ILIT(1) :: FAST_INT)
292 tag MonoUsMany = ILIT(2)
293 tag (MonoUsVar _) = ILIT(3)
295 -- Should be in Maybes, I guess
296 cmpMaybe cmp Nothing Nothing = EQ
297 cmpMaybe cmp Nothing (Just x) = LT
298 cmpMaybe cmp (Just x) Nothing = GT
299 cmpMaybe cmp (Just x) (Just y) = x `cmp` y