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 PolyType(..), MonoType(..),
15 Context(..), ClassAssertion(..)
18 , cmpPolyType, cmpMonoType
19 , pprParendMonoType, pprContext
20 , extractMonoTyNames, extractCtxtTyNames
27 import Outputable ( interppSP, ifnotPprForUser )
29 import ProtoName ( cmpProtoName, ProtoName )
31 import Util ( cmpList, panic# )
33 #endif {- COMPILING_GHC -}
36 This is the syntax for types as seen in type signatures.
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
53 type Context name = [ClassAssertion name]
55 type ClassAssertion name = (name, name)
58 = MonoTyVar name -- Type variable
60 | MonoTyApp name -- Type constructor or variable
63 -- We *could* have a "MonoTyCon name" equiv to "MonoTyApp name []"
64 -- (for efficiency, what?) WDP 96/02/18
66 | MonoFunTy (MonoType name) -- function type
69 | MonoListTy (MonoType name) -- list type
70 | MonoTupleTy [MonoType name] -- tuple type (length gives arity)
73 -- these next two are only used in unfoldings in interfaces
74 | MonoDictTy name -- Class
77 | MonoForAllTy [(name, Kind)]
79 -- *** NOTA BENE *** A "monotype" in a pragma can have
80 -- for-alls in it, (mostly to do with dictionaries). These
81 -- must be explicitly Kinded.
83 #endif {- COMPILING_GHC -}
86 We do define a specialised equality for these \tr{*Type} types; used
87 in checking interfaces. Most any other use is likely to be {\em
88 wrong}, so be careful!
92 cmpPolyType :: (a -> a -> TAG_) -> PolyType a -> PolyType a -> TAG_
93 cmpMonoType :: (a -> a -> TAG_) -> MonoType a -> MonoType a -> TAG_
94 cmpContext :: (a -> a -> TAG_) -> Context a -> Context a -> TAG_
96 -- We assume that HsPreForAllTys have been smashed by now.
98 cmpPolyType _ (HsPreForAllTy _ _) _ = panic# "cmpPolyType:HsPreForAllTy:1st arg"
99 cmpPolyType _ _ (HsPreForAllTy _ _) = panic# "cmpPolyType:HsPreForAllTy:2nd arg"
102 cmpPolyType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
103 = case (cmp_tvs tvs1 tvs2) of
104 EQ_ -> case (cmpContext cmp c1 c2) of
105 EQ_ -> cmpMonoType cmp t1 t2
112 cmp_tvs (a:as) (b:bs)
113 = case cmp a b of { EQ_ -> cmp_tvs as bs; xxx -> xxx }
114 cmp_tvs _ _ = panic# "cmp_tvs"
117 cmpMonoType cmp (MonoTyVar n1) (MonoTyVar n2)
120 cmpMonoType cmp (MonoTupleTy tys1) (MonoTupleTy tys2)
121 = cmpList (cmpMonoType cmp) tys1 tys2
122 cmpMonoType cmp (MonoListTy ty1) (MonoListTy ty2)
123 = cmpMonoType cmp ty1 ty2
125 cmpMonoType cmp (MonoTyApp tc1 tys1) (MonoTyApp tc2 tys2)
126 = case cmp tc1 tc2 of { EQ_ -> cmpList (cmpMonoType cmp) tys1 tys2; xxx -> xxx }
128 cmpMonoType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
129 = case cmpMonoType cmp a1 a2 of { EQ_ -> cmpMonoType cmp b1 b2; xxx -> xxx }
131 cmpMonoType cmp (MonoDictTy c1 ty1) (MonoDictTy c2 ty2)
132 = case cmp c1 c2 of { EQ_ -> cmpMonoType cmp ty1 ty2; xxx -> xxx }
134 cmpMonoType cmp ty1 ty2 -- tags must be different
138 if tag1 _LT_ tag2 then LT_ else GT_
140 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
141 tag (MonoTupleTy tys1) = ILIT(2)
142 tag (MonoListTy ty1) = ILIT(3)
143 tag (MonoTyApp tc1 tys1) = ILIT(4)
144 tag (MonoFunTy a1 b1) = ILIT(5)
145 tag (MonoDictTy c1 ty1) = ILIT(7)
149 = cmpList cmp_ctxt a b
151 cmp_ctxt (c1, tv1) (c2, tv2)
152 = case cmp c1 c2 of { EQ_ -> cmp tv1 tv2; xxx -> xxx }
157 This is used in various places:
159 pprContext :: (Outputable name) => PprStyle -> (Context name) -> Pretty
161 pprContext sty [] = ppNil
162 pprContext sty [(clas, ty)] = ppCat [ppr sty clas, ppr sty ty, ppStr "=>"]
163 pprContext sty context
164 = ppBesides [ppLparen,
165 ppInterleave ppComma (map pp_assert context),
166 ppRparen, ppStr " =>"]
169 = ppCat [ppr sty clas, ppr sty ty]
173 instance (Outputable name) => Outputable (PolyType name) where
174 ppr sty (HsPreForAllTy ctxt ty)
175 = print_it sty ppNil ctxt ty
176 ppr sty (HsForAllTy tvs ctxt ty)
178 (ppBesides [ppStr "_forall_ ", interppSP sty tvs, ppStr " => "])
181 print_it sty pp_forall ctxt ty
182 = ppCat [ifnotPprForUser sty pp_forall, -- print foralls unless PprForUser
183 pprContext sty ctxt, ppr sty ty]
185 instance (Outputable name) => Outputable (MonoType name) where
188 pREC_TOP = (0 :: Int)
189 pREC_FUN = (1 :: Int)
190 pREC_CON = (2 :: Int)
192 -- printing works more-or-less as for Types
194 pprMonoType, pprParendMonoType :: (Outputable name) => PprStyle -> MonoType name -> Pretty
196 pprMonoType sty ty = ppr_mono_ty sty pREC_TOP ty
197 pprParendMonoType sty ty = ppr_mono_ty sty pREC_CON ty
199 ppr_mono_ty sty ctxt_prec (MonoTyVar name) = ppr sty name
201 ppr_mono_ty sty ctxt_prec (MonoFunTy ty1 ty2)
202 = let p1 = ppr_mono_ty sty pREC_FUN ty1
203 p2 = ppr_mono_ty sty pREC_TOP ty2
205 if ctxt_prec < pREC_FUN then -- no parens needed
206 ppSep [p1, ppBeside (ppStr "-> ") p2]
208 ppSep [ppBeside ppLparen p1, ppBesides [ppStr "-> ", p2, ppRparen]]
210 ppr_mono_ty sty ctxt_prec (MonoTupleTy tys)
211 = ppBesides [ppLparen, ppInterleave ppComma (map (ppr sty) tys), ppRparen]
213 ppr_mono_ty sty ctxt_prec (MonoListTy ty)
214 = ppBesides [ppLbrack, ppr_mono_ty sty pREC_TOP ty, ppRbrack]
216 ppr_mono_ty sty ctxt_prec (MonoTyApp tycon tys)
217 = let pp_tycon = ppr sty tycon in
220 else if ctxt_prec < pREC_CON then -- no parens needed
221 ppCat [pp_tycon, ppInterleave ppNil (map (ppr_mono_ty sty pREC_CON) tys)]
223 ppBesides [ ppLparen, pp_tycon, ppSP,
224 ppInterleave ppNil (map (ppr_mono_ty sty pREC_CON) tys), ppRparen ]
227 ppr_mono_ty sty ctxt_prec (MonoDictTy clas ty)
228 = ppBesides [ppStr "{{", ppr sty clas, ppSP, ppr_mono_ty sty ctxt_prec ty, ppStr "}}"]
230 #endif {- COMPILING_GHC -}
233 Get the type variable names from a @MonoType@. Don't use class @Eq@
234 because @ProtoNames@ aren't in it.
239 extractCtxtTyNames :: (name -> name -> Bool) -> Context name -> [name]
240 extractMonoTyNames :: (name -> name -> Bool) -> MonoType name -> [name]
242 extractCtxtTyNames eq ctxt
246 | is_elem eq tv acc = acc
247 | otherwise = tv : acc
249 extractMonoTyNames eq ty
252 get (MonoTyApp con tys) acc = foldr get acc tys
253 get (MonoListTy ty) acc = get ty acc
254 get (MonoFunTy ty1 ty2) acc = get ty1 (get ty2 acc)
255 get (MonoDictTy _ ty) acc = get ty acc
256 get (MonoTupleTy tys) acc = foldr get acc tys
257 get (MonoTyVar name) acc
258 | is_elem eq name acc = acc
259 | otherwise = name : acc
261 is_elem eq n [] = False
262 is_elem eq n (x:xs) = n `eq` x || is_elem eq n xs
264 #endif {- COMPILING_GHC -}