2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[HsTypes]{Abstract syntax: user-defined types}
6 If compiled without \tr{#define COMPILING_GHC}, you get
7 (part of) a Haskell-abstract-syntax library. With it,
12 HsType(..), HsTyVar(..),
13 Context, ClassAssertion
16 , getTyVarName, replaceTyVarName
18 , pprContext, pprClassAssertion
19 , cmpHsType, cmpHsTypes, cmpContext
22 #include "HsVersions.h"
25 import Kind ( Kind {- instance Outputable -} )
26 import Name ( nameOccName )
27 import Util ( thenCmp, cmpList, isIn, panic )
28 import GlaExts ( Int#, (<#) )
31 This is the syntax for types as seen in type signatures.
34 type Context name = [ClassAssertion name]
36 type ClassAssertion name = (name, [HsType name])
37 -- The type is usually a type variable, but it
38 -- doesn't have to be when reading interface files
41 = HsPreForAllTy (Context name)
44 -- The renamer turns HsPreForAllTys into HsForAllTys when they
45 -- occur in signatures, to make the binding of variables
46 -- explicit. This distinction is made visible for
47 -- non-COMPILING_GHC code, because you probably want to do the
50 | HsForAllTy [HsTyVar name]
54 | MonoTyVar name -- Type variable
56 | MonoTyApp (HsType name)
59 | MonoFunTy (HsType name) -- function type
62 | MonoListTy name -- The list TyCon name
63 (HsType name) -- Element type
65 | MonoTupleTy name -- The tuple TyCon name
66 [HsType name] -- Element types (length gives arity)
68 -- these next two are only used in unfoldings in interfaces
69 | MonoDictTy name -- Class
72 mkHsForAllTy [] [] ty = ty
73 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt 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, ptext SLIT("::"), ppr kind]
106 ppr_forall ctxt_prec [] [] ty
107 = ppr_mono_ty ctxt_prec ty
108 ppr_forall ctxt_prec tvs ctxt ty
109 = maybeParen (ctxt_prec >= pREC_FUN) $
110 sep [ptext SLIT("_forall_"), brackets (interppSP tvs),
111 pprContext ctxt, ptext SLIT("=>"),
114 pprContext :: (Outputable name) => Context name -> SDoc
115 pprContext [] = empty
116 pprContext context = parens (hsep (punctuate comma (map pprClassAssertion context)))
118 pprClassAssertion :: (Outputable name) => ClassAssertion name -> SDoc
119 pprClassAssertion (clas, tys)
120 = ppr clas <+> hsep (map ppr tys)
124 pREC_TOP = (0 :: Int)
125 pREC_FUN = (1 :: Int)
126 pREC_CON = (2 :: Int)
128 maybeParen :: Bool -> SDoc -> SDoc
129 maybeParen True p = parens p
130 maybeParen False p = p
132 -- printing works more-or-less as for Types
134 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
136 pprHsType ty = ppr_mono_ty pREC_TOP ty
137 pprParendHsType ty = ppr_mono_ty pREC_CON ty
139 ppr_mono_ty ctxt_prec (HsPreForAllTy ctxt ty) = ppr_forall ctxt_prec [] ctxt ty
140 ppr_mono_ty ctxt_prec (HsForAllTy tvs ctxt ty) = ppr_forall ctxt_prec tvs ctxt ty
142 ppr_mono_ty ctxt_prec (MonoTyVar name) = ppr name
144 ppr_mono_ty ctxt_prec (MonoFunTy ty1 ty2)
145 = let p1 = ppr_mono_ty pREC_FUN ty1
146 p2 = ppr_mono_ty pREC_TOP ty2
148 maybeParen (ctxt_prec >= pREC_FUN)
149 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
151 ppr_mono_ty ctxt_prec (MonoTupleTy _ tys)
152 = parens (sep (punctuate comma (map ppr tys)))
154 ppr_mono_ty ctxt_prec (MonoListTy _ ty)
155 = brackets (ppr_mono_ty pREC_TOP ty)
157 ppr_mono_ty ctxt_prec (MonoTyApp fun_ty arg_ty)
158 = maybeParen (ctxt_prec >= pREC_CON)
159 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
161 ppr_mono_ty ctxt_prec (MonoDictTy clas tys)
162 = ppr clas <+> hsep (map (ppr_mono_ty pREC_CON) tys)
166 %************************************************************************
168 \subsection{Comparison}
170 %************************************************************************
172 We do define a specialised equality for these \tr{*Type} types; used
173 in checking interfaces. Most any other use is likely to be {\em
174 wrong}, so be careful!
177 cmpHsTyVar :: (a -> a -> Ordering) -> HsTyVar a -> HsTyVar a -> Ordering
178 cmpHsType :: (a -> a -> Ordering) -> HsType a -> HsType a -> Ordering
179 cmpHsTypes :: (a -> a -> Ordering) -> [HsType a] -> [HsType a] -> Ordering
180 cmpContext :: (a -> a -> Ordering) -> Context a -> Context a -> Ordering
182 cmpHsTyVar cmp (UserTyVar v1) (UserTyVar v2) = v1 `cmp` v2
183 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
184 cmpHsTyVar cmp (UserTyVar _) other = LT
185 cmpHsTyVar cmp other1 other2 = GT
188 cmpHsTypes cmp [] [] = EQ
189 cmpHsTypes cmp [] tys2 = LT
190 cmpHsTypes cmp tys1 [] = GT
191 cmpHsTypes cmp (ty1:tys1) (ty2:tys2) = cmpHsType cmp ty1 ty2 `thenCmp` cmpHsTypes cmp tys1 tys2
193 -- We assume that HsPreForAllTys have been smashed by now.
195 cmpHsType _ (HsPreForAllTy _ _) _ = panic "cmpHsType:HsPreForAllTy:1st arg"
196 cmpHsType _ _ (HsPreForAllTy _ _) = panic "cmpHsType:HsPreForAllTy:2nd arg"
199 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
200 = cmpList (cmpHsTyVar cmp) tvs1 tvs2 `thenCmp`
201 cmpContext cmp c1 c2 `thenCmp`
204 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
207 cmpHsType cmp (MonoTupleTy _ tys1) (MonoTupleTy _ tys2)
208 = cmpList (cmpHsType cmp) tys1 tys2
209 cmpHsType cmp (MonoListTy _ ty1) (MonoListTy _ ty2)
210 = cmpHsType cmp ty1 ty2
212 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
213 = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
215 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
216 = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
218 cmpHsType cmp (MonoDictTy c1 tys1) (MonoDictTy c2 tys2)
219 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
221 cmpHsType cmp ty1 ty2 -- tags must be different
225 if tag1 _LT_ tag2 then LT else GT
227 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
228 tag (MonoTupleTy _ tys1) = ILIT(2)
229 tag (MonoListTy _ ty1) = ILIT(3)
230 tag (MonoTyApp tc1 tys1) = ILIT(4)
231 tag (MonoFunTy a1 b1) = ILIT(5)
232 tag (MonoDictTy c1 tys1) = ILIT(7)
233 tag (HsForAllTy _ _ _) = ILIT(8)
234 tag (HsPreForAllTy _ _) = ILIT(9)
238 = cmpList cmp_ctxt a b
240 cmp_ctxt (c1, tys1) (c2, tys2)
241 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2