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,
11 #include "HsVersions.h"
14 HsType(..), HsTyVar(..),
15 SYN_IE(Context), SYN_IE(ClassAssertion)
18 , getTyVarName, replaceTyVarName
21 , cmpHsType, cmpContext
26 import CmdLineOpts ( opt_PprUserLength )
27 import Outputable ( Outputable(..), PprStyle(..), interppSP, ifnotPprForUser )
28 import Kind ( Kind {- instance Outputable -} )
29 import Name ( nameOccName )
31 import Util ( thenCmp, cmpList, isIn, panic# )
34 This is the syntax for types as seen in type signatures.
37 type Context name = [ClassAssertion name]
39 type ClassAssertion name = (name, HsType name)
40 -- The type is usually a type variable, but it
41 -- doesn't have to be when reading interface files
44 = HsPreForAllTy (Context name)
47 -- The renamer turns HsPreForAllTys into HsForAllTys when they
48 -- occur in signatures, to make the binding of variables
49 -- explicit. This distinction is made visible for
50 -- non-COMPILING_GHC code, because you probably want to do the
53 | HsForAllTy [HsTyVar name]
57 | MonoTyVar name -- Type variable
59 | MonoTyApp (HsType name)
62 | MonoFunTy (HsType name) -- function type
65 | MonoListTy name -- The list TyCon name
66 (HsType name) -- Element type
68 | MonoTupleTy name -- The tuple TyCon name
69 [HsType name] -- Element types (length gives arity)
71 -- these next two are only used in unfoldings in interfaces
72 | MonoDictTy name -- Class
75 mkHsForAllTy [] [] ty = ty
76 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
80 | IfaceTyVar name Kind
81 -- *** NOTA BENE *** A "monotype" in a pragma can have
82 -- for-alls in it, (mostly to do with dictionaries). These
83 -- must be explicitly Kinded.
85 getTyVarName (UserTyVar n) = n
86 getTyVarName (IfaceTyVar n _) = n
88 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
89 replaceTyVarName (UserTyVar n) n' = UserTyVar n'
90 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
94 %************************************************************************
96 \subsection{Pretty printing}
98 %************************************************************************
102 instance (Outputable name) => Outputable (HsType name) where
105 instance (Outputable name) => Outputable (HsTyVar name) where
106 ppr sty (UserTyVar name) = ppr_hs_tyname sty name
107 ppr sty (IfaceTyVar name kind) = hsep [ppr_hs_tyname sty name, ptext SLIT("::"), ppr sty kind]
110 -- Here comes a rather gross hack.
111 -- We want to print data and class decls in interface files, from the original source
112 -- When we do, we want the type variables to come out with their original names, not
113 -- some new unique (or else interfaces wobble too much). So when we come to one of
114 -- these type variables we sneakily change the style to PprForUser!
115 ppr_hs_tyname PprInterface tv_name = ppr (PprForUser opt_PprUserLength) tv_name
116 ppr_hs_tyname other_sty tv_name = ppr other_sty tv_name
118 ppr_forall sty ctxt_prec [] [] ty
119 = ppr_mono_ty sty ctxt_prec ty
120 ppr_forall sty ctxt_prec tvs ctxt ty
121 = maybeParen (ctxt_prec >= pREC_FUN) $
122 sep [ptext SLIT("_forall_"), brackets (interppSP sty tvs),
123 pprContext sty ctxt, ptext SLIT("=>"),
126 pprContext :: (Outputable name) => PprStyle -> (Context name) -> Doc
127 pprContext sty [] = empty
128 pprContext sty context
129 = hsep [braces (hsep (punctuate comma (map ppr_assert context)))]
131 ppr_assert (clas, ty) = hsep [ppr sty clas, ppr sty ty]
135 pREC_TOP = (0 :: Int)
136 pREC_FUN = (1 :: Int)
137 pREC_CON = (2 :: Int)
139 maybeParen :: Bool -> Doc -> Doc
140 maybeParen True p = parens p
141 maybeParen False p = p
143 -- printing works more-or-less as for Types
145 pprHsType, pprParendHsType :: (Outputable name) => PprStyle -> HsType name -> Doc
147 pprHsType sty ty = ppr_mono_ty sty pREC_TOP ty
148 pprParendHsType sty ty = ppr_mono_ty sty pREC_CON ty
150 ppr_mono_ty sty ctxt_prec (HsPreForAllTy ctxt ty) = ppr_forall sty ctxt_prec [] ctxt ty
151 ppr_mono_ty sty ctxt_prec (HsForAllTy tvs ctxt ty) = ppr_forall sty ctxt_prec tvs ctxt ty
153 ppr_mono_ty sty ctxt_prec (MonoTyVar name) = ppr_hs_tyname sty name
155 ppr_mono_ty sty ctxt_prec (MonoFunTy ty1 ty2)
156 = let p1 = ppr_mono_ty sty pREC_FUN ty1
157 p2 = ppr_mono_ty sty pREC_TOP ty2
159 maybeParen (ctxt_prec >= pREC_FUN)
160 (sep [p1, (<>) (ptext SLIT("-> ")) p2])
162 ppr_mono_ty sty ctxt_prec (MonoTupleTy _ tys)
163 = parens (sep (punctuate comma (map (ppr sty) tys)))
165 ppr_mono_ty sty ctxt_prec (MonoListTy _ ty)
166 = brackets (ppr_mono_ty sty pREC_TOP ty)
168 ppr_mono_ty sty ctxt_prec (MonoTyApp fun_ty arg_ty)
169 = maybeParen (ctxt_prec >= pREC_CON)
170 (hsep [ppr_mono_ty sty pREC_FUN fun_ty, ppr_mono_ty sty pREC_CON arg_ty])
172 ppr_mono_ty sty ctxt_prec (MonoDictTy clas ty)
173 = braces (hsep [ppr sty clas, ppr_mono_ty sty pREC_CON ty])
174 -- Curlies are temporary
178 %************************************************************************
180 \subsection{Comparison}
182 %************************************************************************
184 We do define a specialised equality for these \tr{*Type} types; used
185 in checking interfaces. Most any other use is likely to be {\em
186 wrong}, so be careful!
189 cmpHsTyVar :: (a -> a -> TAG_) -> HsTyVar a -> HsTyVar a -> TAG_
190 --cmpHsType :: (a -> a -> TAG_) -> HsType a -> HsType a -> TAG_
191 --cmpContext :: (a -> a -> TAG_) -> Context a -> Context a -> TAG_
193 cmpHsTyVar cmp (UserTyVar v1) (UserTyVar v2) = v1 `cmp` v2
194 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
195 cmpHsTyVar cmp (UserTyVar _) other = LT_
196 cmpHsTyVar cmp other1 other2 = GT_
199 -- We assume that HsPreForAllTys have been smashed by now.
201 cmpHsType _ (HsPreForAllTy _ _) _ = panic# "cmpHsType:HsPreForAllTy:1st arg"
202 cmpHsType _ _ (HsPreForAllTy _ _) = panic# "cmpHsType:HsPreForAllTy:2nd arg"
205 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
206 = cmpList (cmpHsTyVar cmp) tvs1 tvs2 `thenCmp`
207 cmpContext cmp c1 c2 `thenCmp`
210 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
213 cmpHsType cmp (MonoTupleTy _ tys1) (MonoTupleTy _ tys2)
214 = cmpList (cmpHsType cmp) tys1 tys2
215 cmpHsType cmp (MonoListTy _ ty1) (MonoListTy _ ty2)
216 = cmpHsType cmp ty1 ty2
218 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
219 = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
221 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
222 = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
224 cmpHsType cmp (MonoDictTy c1 ty1) (MonoDictTy c2 ty2)
225 = cmp c1 c2 `thenCmp` cmpHsType cmp ty1 ty2
227 cmpHsType cmp ty1 ty2 -- tags must be different
231 if tag1 _LT_ tag2 then LT_ else GT_
233 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
234 tag (MonoTupleTy _ tys1) = ILIT(2)
235 tag (MonoListTy _ ty1) = ILIT(3)
236 tag (MonoTyApp tc1 tys1) = ILIT(4)
237 tag (MonoFunTy a1 b1) = ILIT(5)
238 tag (MonoDictTy c1 ty1) = ILIT(7)
239 tag (HsForAllTy _ _ _) = ILIT(8)
240 tag (HsPreForAllTy _ _) = ILIT(9)
244 = cmpList cmp_ctxt a b
246 cmp_ctxt (c1, ty1) (c2, ty2)
247 = cmp c1 c2 `thenCmp` cmpHsType cmp ty1 ty2