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 mkHsForAllTy [] [] ty = ty
70 mkHsForAllTy tvs ctxt ty = HsForAllTy (Just tvs) ctxt ty
72 mkHsUsForAllTy uvs ty = foldr (\ uv ty -> MonoUsgForAllTy uv ty)
77 | IfaceTyVar name Kind
78 -- *** NOTA BENE *** A "monotype" in a pragma can have
79 -- for-alls in it, (mostly to do with dictionaries). These
80 -- must be explicitly Kinded.
82 getTyVarName (UserTyVar n) = n
83 getTyVarName (IfaceTyVar n _) = n
85 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
86 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
87 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
91 %************************************************************************
93 \subsection{Pretty printing}
95 %************************************************************************
99 instance (Outputable name) => Outputable (HsType name) where
100 ppr ty = pprHsType ty
102 instance (Outputable name) => Outputable (HsTyVar name) where
103 ppr (UserTyVar name) = ppr name
104 ppr (IfaceTyVar name kind) = hsep [ppr name, dcolon, ppr kind]
107 pprForAll tvs = ptext SLIT("forall") <+> interppSP tvs <> ptext SLIT(".")
109 pprContext :: (Outputable name) => Context name -> SDoc
110 pprContext [] = empty
111 pprContext context = parens (hsep (punctuate comma (map pprClassAssertion context))) <+> ptext SLIT("=>")
113 pprClassAssertion :: (Outputable name) => ClassAssertion name -> SDoc
114 pprClassAssertion (clas, tys)
115 = ppr clas <+> hsep (map pprParendHsType tys)
119 pREC_TOP = (0 :: Int)
120 pREC_FUN = (1 :: Int)
121 pREC_CON = (2 :: Int)
123 maybeParen :: Bool -> SDoc -> SDoc
124 maybeParen True p = parens p
125 maybeParen False p = p
127 -- printing works more-or-less as for Types
129 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
131 pprHsType ty = ppr_mono_ty pREC_TOP ty
132 pprParendHsType ty = ppr_mono_ty pREC_CON ty
134 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
135 = maybeParen (ctxt_prec >= pREC_FUN) $
136 sep [pprForAll tvs, pprContext ctxt, pprHsType ty]
138 tvs = case maybe_tvs of
142 ppr_mono_ty ctxt_prec (MonoTyVar name)
145 ppr_mono_ty ctxt_prec (MonoFunTy ty1 ty2)
146 = let p1 = ppr_mono_ty pREC_FUN ty1
147 p2 = ppr_mono_ty pREC_TOP ty2
149 maybeParen (ctxt_prec >= pREC_FUN)
150 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
152 ppr_mono_ty ctxt_prec (MonoTupleTy tys True)
153 = parens (sep (punctuate comma (map ppr tys)))
154 ppr_mono_ty ctxt_prec (MonoTupleTy tys False)
155 = ptext SLIT("(#") <> sep (punctuate comma (map ppr tys)) <> ptext SLIT("#)")
157 ppr_mono_ty ctxt_prec (MonoListTy ty)
158 = brackets (ppr_mono_ty pREC_TOP ty)
160 ppr_mono_ty ctxt_prec (MonoTyApp fun_ty arg_ty)
161 = maybeParen (ctxt_prec >= pREC_CON)
162 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
164 ppr_mono_ty ctxt_prec (MonoDictTy clas tys)
165 = ppr clas <+> hsep (map (ppr_mono_ty pREC_CON) tys)
167 ppr_mono_ty ctxt_prec ty@(MonoUsgForAllTy _ _)
168 = maybeParen (ctxt_prec >= pREC_FUN) $
169 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
170 ppr_mono_ty pREC_TOP sigma
173 (uvars,sigma) = split [] ty
174 pp_uvars = interppSP uvars
176 split uvs (MonoUsgForAllTy uv ty') = split (uv:uvs) ty'
177 split uvs ty' = (reverse uvs,ty')
179 ppr_mono_ty ctxt_prec (MonoUsgTy u ty)
180 = maybeParen (ctxt_prec >= pREC_CON) $
181 ptext SLIT("__u") <+> pp_ua <+> ppr_mono_ty pREC_CON ty
184 MonoUsOnce -> ptext SLIT("-")
185 MonoUsMany -> ptext SLIT("!")
186 MonoUsVar uv -> ppr uv
190 %************************************************************************
192 \subsection{Comparison}
194 %************************************************************************
196 We do define a specialised equality for these \tr{*Type} types; used
197 in checking interfaces. Most any other use is likely to be {\em
198 wrong}, so be careful!
201 cmpHsTyVar :: (a -> a -> Ordering) -> HsTyVar a -> HsTyVar a -> Ordering
202 cmpHsType :: (a -> a -> Ordering) -> HsType a -> HsType a -> Ordering
203 cmpHsTypes :: (a -> a -> Ordering) -> [HsType a] -> [HsType a] -> Ordering
204 cmpContext :: (a -> a -> Ordering) -> Context a -> Context a -> Ordering
206 cmpHsTyVar cmp (UserTyVar v1) (UserTyVar v2) = v1 `cmp` v2
207 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
208 cmpHsTyVar cmp (UserTyVar _) other = LT
209 cmpHsTyVar cmp other1 other2 = GT
212 cmpHsTypes cmp [] [] = EQ
213 cmpHsTypes cmp [] tys2 = LT
214 cmpHsTypes cmp tys1 [] = GT
215 cmpHsTypes cmp (ty1:tys1) (ty2:tys2) = cmpHsType cmp ty1 ty2 `thenCmp` cmpHsTypes cmp tys1 tys2
217 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
218 = cmpMaybe (cmpList (cmpHsTyVar cmp)) tvs1 tvs2 `thenCmp`
219 cmpContext cmp c1 c2 `thenCmp`
222 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
225 cmpHsType cmp (MonoTupleTy tys1 b1) (MonoTupleTy tys2 b2)
226 = (b1 `compare` b2) `thenCmp` cmpHsTypes cmp tys1 tys2
228 cmpHsType cmp (MonoListTy ty1) (MonoListTy ty2)
229 = cmpHsType cmp ty1 ty2
231 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
232 = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
234 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
235 = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
237 cmpHsType cmp (MonoDictTy c1 tys1) (MonoDictTy c2 tys2)
238 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
240 cmpHsType cmp (MonoUsgTy u1 ty1) (MonoUsgTy u2 ty2)
241 = cmpUsg cmp u1 u2 `thenCmp` cmpHsType cmp ty1 ty2
243 cmpHsType cmp ty1 ty2 -- tags must be different
247 if tag1 _LT_ tag2 then LT else GT
249 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
250 tag (MonoTupleTy tys1 _) = ILIT(2)
251 tag (MonoListTy ty1) = ILIT(3)
252 tag (MonoTyApp tc1 tys1) = ILIT(4)
253 tag (MonoFunTy a1 b1) = ILIT(5)
254 tag (MonoDictTy c1 tys1) = ILIT(6)
255 tag (MonoUsgTy c1 ty1) = ILIT(7)
256 tag (MonoUsgForAllTy uv1 ty1) = ILIT(8)
257 tag (HsForAllTy _ _ _) = ILIT(9)
261 = cmpList cmp_ctxt a b
263 cmp_ctxt (c1, tys1) (c2, tys2)
264 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
266 cmpUsg cmp MonoUsOnce MonoUsOnce = EQ
267 cmpUsg cmp MonoUsMany MonoUsMany = EQ
268 cmpUsg cmp (MonoUsVar u1) (MonoUsVar u2) = cmp u1 u2
270 cmpUsg cmp ua1 ua2 -- tags must be different
274 if tag1 _LT_ tag2 then LT else GT
276 tag MonoUsOnce = (ILIT(1) :: FAST_INT)
277 tag MonoUsMany = ILIT(2)
278 tag (MonoUsVar _) = ILIT(3)
280 -- Should be in Maybes, I guess
281 cmpMaybe cmp Nothing Nothing = EQ
282 cmpMaybe cmp Nothing (Just x) = LT
283 cmpMaybe cmp (Just x) Nothing = GT
284 cmpMaybe cmp (Just x) (Just y) = x `cmp` y