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 Util ( thenCmp, cmpList, panic )
27 import GlaExts ( Int#, (<#) )
30 This is the syntax for types as seen in type signatures.
33 type Context name = [ClassAssertion name]
35 type ClassAssertion name = (name, [HsType name])
36 -- The type is usually a type variable, but it
37 -- doesn't have to be when reading interface files
40 = HsPreForAllTy (Context name)
43 -- The renamer turns HsPreForAllTys into HsForAllTys when they
44 -- occur in signatures, to make the binding of variables
45 -- explicit. This distinction is made visible for
46 -- non-COMPILING_GHC code, because you probably want to do the
49 | HsForAllTy [HsTyVar name]
53 | MonoTyVar name -- Type variable
55 | MonoTyApp (HsType name)
58 | MonoFunTy (HsType name) -- function type
61 | MonoListTy name -- The list TyCon name
62 (HsType name) -- Element type
64 | MonoTupleTy name -- The tuple TyCon name
65 [HsType name] -- Element types (length gives arity)
67 -- these next two are only used in unfoldings in interfaces
68 | MonoDictTy name -- Class
71 mkHsForAllTy [] [] ty = ty
72 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
76 | IfaceTyVar name Kind
77 -- *** NOTA BENE *** A "monotype" in a pragma can have
78 -- for-alls in it, (mostly to do with dictionaries). These
79 -- must be explicitly Kinded.
81 getTyVarName (UserTyVar n) = n
82 getTyVarName (IfaceTyVar n _) = n
84 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
85 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
86 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
90 %************************************************************************
92 \subsection{Pretty printing}
94 %************************************************************************
98 instance (Outputable name) => Outputable (HsType name) where
101 instance (Outputable name) => Outputable (HsTyVar name) where
102 ppr (UserTyVar name) = ppr name
103 ppr (IfaceTyVar name kind) = hsep [ppr name, ptext SLIT("::"), ppr kind]
105 ppr_forall ctxt_prec [] [] ty
106 = ppr_mono_ty ctxt_prec ty
107 ppr_forall ctxt_prec tvs ctxt ty
108 = maybeParen (ctxt_prec >= pREC_FUN) $
109 sep [ptext SLIT("_forall_"), brackets (interppSP tvs),
110 pprContext ctxt, ptext SLIT("=>"),
113 pprContext :: (Outputable name) => Context name -> SDoc
114 pprContext [] = empty
115 pprContext context = parens (hsep (punctuate comma (map pprClassAssertion context)))
117 pprClassAssertion :: (Outputable name) => ClassAssertion name -> SDoc
118 pprClassAssertion (clas, tys)
119 = ppr clas <+> hsep (map ppr tys)
123 pREC_TOP = (0 :: Int)
124 pREC_FUN = (1 :: Int)
125 pREC_CON = (2 :: Int)
127 maybeParen :: Bool -> SDoc -> SDoc
128 maybeParen True p = parens p
129 maybeParen False p = p
131 -- printing works more-or-less as for Types
133 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
135 pprHsType ty = ppr_mono_ty pREC_TOP ty
136 pprParendHsType ty = ppr_mono_ty pREC_CON ty
138 ppr_mono_ty ctxt_prec (HsPreForAllTy ctxt ty) = ppr_forall ctxt_prec [] ctxt ty
139 ppr_mono_ty ctxt_prec (HsForAllTy tvs ctxt ty) = ppr_forall ctxt_prec tvs ctxt ty
141 ppr_mono_ty ctxt_prec (MonoTyVar name) = ppr name
143 ppr_mono_ty ctxt_prec (MonoFunTy ty1 ty2)
144 = let p1 = ppr_mono_ty pREC_FUN ty1
145 p2 = ppr_mono_ty pREC_TOP ty2
147 maybeParen (ctxt_prec >= pREC_FUN)
148 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
150 ppr_mono_ty ctxt_prec (MonoTupleTy _ tys)
151 = parens (sep (punctuate comma (map ppr tys)))
153 ppr_mono_ty ctxt_prec (MonoListTy _ ty)
154 = brackets (ppr_mono_ty pREC_TOP ty)
156 ppr_mono_ty ctxt_prec (MonoTyApp fun_ty arg_ty)
157 = maybeParen (ctxt_prec >= pREC_CON)
158 (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
160 ppr_mono_ty ctxt_prec (MonoDictTy clas tys)
161 = ppr clas <+> hsep (map (ppr_mono_ty pREC_CON) tys)
165 %************************************************************************
167 \subsection{Comparison}
169 %************************************************************************
171 We do define a specialised equality for these \tr{*Type} types; used
172 in checking interfaces. Most any other use is likely to be {\em
173 wrong}, so be careful!
176 cmpHsTyVar :: (a -> a -> Ordering) -> HsTyVar a -> HsTyVar a -> Ordering
177 cmpHsType :: (a -> a -> Ordering) -> HsType a -> HsType a -> Ordering
178 cmpHsTypes :: (a -> a -> Ordering) -> [HsType a] -> [HsType a] -> Ordering
179 cmpContext :: (a -> a -> Ordering) -> Context a -> Context a -> Ordering
181 cmpHsTyVar cmp (UserTyVar v1) (UserTyVar v2) = v1 `cmp` v2
182 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
183 cmpHsTyVar cmp (UserTyVar _) other = LT
184 cmpHsTyVar cmp other1 other2 = GT
187 cmpHsTypes cmp [] [] = EQ
188 cmpHsTypes cmp [] tys2 = LT
189 cmpHsTypes cmp tys1 [] = GT
190 cmpHsTypes cmp (ty1:tys1) (ty2:tys2) = cmpHsType cmp ty1 ty2 `thenCmp` cmpHsTypes cmp tys1 tys2
192 -- We assume that HsPreForAllTys have been smashed by now.
194 cmpHsType _ (HsPreForAllTy _ _) _ = panic "cmpHsType:HsPreForAllTy:1st arg"
195 cmpHsType _ _ (HsPreForAllTy _ _) = panic "cmpHsType:HsPreForAllTy:2nd arg"
198 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
199 = cmpList (cmpHsTyVar cmp) tvs1 tvs2 `thenCmp`
200 cmpContext cmp c1 c2 `thenCmp`
203 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
206 cmpHsType cmp (MonoTupleTy _ tys1) (MonoTupleTy _ tys2)
207 = cmpList (cmpHsType cmp) tys1 tys2
208 cmpHsType cmp (MonoListTy _ ty1) (MonoListTy _ ty2)
209 = cmpHsType cmp ty1 ty2
211 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
212 = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
214 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
215 = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
217 cmpHsType cmp (MonoDictTy c1 tys1) (MonoDictTy c2 tys2)
218 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
220 cmpHsType cmp ty1 ty2 -- tags must be different
224 if tag1 _LT_ tag2 then LT else GT
226 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
227 tag (MonoTupleTy _ tys1) = ILIT(2)
228 tag (MonoListTy _ ty1) = ILIT(3)
229 tag (MonoTyApp tc1 tys1) = ILIT(4)
230 tag (MonoFunTy a1 b1) = ILIT(5)
231 tag (MonoDictTy c1 tys1) = ILIT(7)
232 tag (HsForAllTy _ _ _) = ILIT(8)
233 tag (HsPreForAllTy _ _) = ILIT(9)
237 = cmpList cmp_ctxt a b
239 cmp_ctxt (c1, tys1) (c2, tys2)
240 = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2