2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[HsTypes]{Abstract syntax: user-defined types}
7 #include "HsVersions.h"
10 PolyType(..), MonoType(..),
11 ClassAssertion(..), Context(..),
14 ProtoNameMonoType(..),
15 ProtoNamePolyType(..),
20 cmpPolyType, cmpMonoType, cmpList,
23 pprContext, pprParendMonoType
29 import Unique ( Unique )
35 This is the syntax for types as seen in type signatures.
39 = UnoverloadedTy (MonoType name) -- equiv to having a [] context
41 | OverloadedTy (Context name) -- not supposed to be []
44 -- this next one is only used in unfoldings in interfaces
48 type Context name = [ClassAssertion name]
50 type ClassAssertion name = (name, name)
52 type ProtoNamePolyType = PolyType ProtoName
53 type RenamedPolyType = PolyType Name
55 type ProtoNameContext = Context ProtoName
56 type RenamedContext = Context Name
59 = MonoTyVar name -- Type variable
60 | MonoTyCon name -- Type constructor
62 | FunMonoTy (MonoType name) -- function type
64 | ListMonoTy (MonoType name) -- list type
65 | TupleMonoTy [PolyType name] -- tuple type (length gives arity)
66 -- *** NOTA BENE *** The tuple type takes *Poly*Type
67 -- arguments, because these *do* arise in pragmatic info
68 -- in interfaces (mostly to do with dictionaries). It just
69 -- so happens that this won't happen for lists, etc.,
70 -- (as far as I know).
71 -- We might want to be less hacky about this in future. (ToDo)
74 -- these next two are only used in unfoldings in interfaces
75 | MonoTyVarTemplate name
76 | MonoDict name -- Class
80 | MonoTyProc [MonoType name]
81 (MonoType name) -- Processor
82 | MonoTyPod (MonoType name) -- Pod
83 #endif {- Data Parallel Haskell -}
85 type ProtoNameMonoType = MonoType ProtoName
86 type RenamedMonoType = MonoType Name
89 We do define a specialised equality for these \tr{*Type} types; used
90 in checking interfaces. Most any other use is likely to be {\em
91 wrong}, so be careful!
93 cmpPolyType :: (a -> a -> TAG_) -> PolyType a -> PolyType a -> TAG_
94 cmpMonoType :: (a -> a -> TAG_) -> MonoType a -> MonoType a -> TAG_
95 cmpContext :: (a -> a -> TAG_) -> Context a -> Context a -> TAG_
96 cmpList :: (a -> a -> TAG_) -> [a] -> [a] -> TAG_
98 cmpPolyType cmp (UnoverloadedTy t1) (UnoverloadedTy t2)
99 = cmpMonoType cmp t1 t2
100 cmpPolyType cmp (OverloadedTy c1 t1) (OverloadedTy c2 t2)
101 = case cmpContext cmp c1 c2 of { EQ_ -> cmpMonoType cmp t1 t2; xxx -> xxx }
103 cmpPolyType cmp (ForAllTy tvs1 t1) (ForAllTy tvs2 t2)
104 = case cmp_tvs tvs1 tvs2 of { EQ_ -> cmpMonoType cmp t1 t2; xxx -> xxx }
109 cmp_tvs (a:as) (b:bs)
110 = case cmp a b of { EQ_ -> cmp_tvs as bs; xxx -> xxx }
111 cmp_tvs _ _ = case (panic "cmp_tvs") of { v -> cmp_tvs v v } -- BUG avoidance
113 cmpPolyType cmp ty1 ty2 -- tags must be different
117 if tag1 _LT_ tag2 then LT_ else GT_
119 tag (UnoverloadedTy _) = (ILIT(1) :: FAST_INT)
120 tag (OverloadedTy _ _) = ILIT(2)
121 tag (ForAllTy _ _) = ILIT(3)
124 cmpMonoType cmp (MonoTyVar n1) (MonoTyVar n2)
127 cmpMonoType cmp (TupleMonoTy tys1) (TupleMonoTy tys2)
128 = cmpList (cmpPolyType cmp) tys1 tys2
129 cmpMonoType cmp (ListMonoTy ty1) (ListMonoTy ty2)
130 = cmpMonoType cmp ty1 ty2
132 cmpMonoType cmp (MonoTyCon tc1 tys1) (MonoTyCon tc2 tys2)
133 = case cmp tc1 tc2 of { EQ_ -> cmpList (cmpMonoType cmp) tys1 tys2; xxx -> xxx }
135 cmpMonoType cmp (FunMonoTy a1 b1) (FunMonoTy a2 b2)
136 = case cmpMonoType cmp a1 a2 of { EQ_ -> cmpMonoType cmp b1 b2; xxx -> xxx }
138 cmpMonoType cmp (MonoTyVarTemplate n1) (MonoTyVarTemplate n2)
140 cmpMonoType cmp (MonoDict c1 ty1) (MonoDict c2 ty2)
141 = case cmp c1 c2 of { EQ_ -> cmpMonoType cmp ty1 ty2; xxx -> xxx }
144 cmpMonoType cmp (MonoTyProc tys1 ty1) (MonoTyProc tys2 ty2)
145 = case cmpList (cmpMonoType cmp) tys1 tys2 of { EQ_ -> cmpMonoType cmp ty1 ty2; xxx -> xxx }
146 cmpMonoType cmp (MonoTyPod ty1) (MonoTyPod ty2) = cmpMonoType cmp ty1 ty2
147 #endif {- Data Parallel Haskell -}
149 cmpMonoType cmp ty1 ty2 -- tags must be different
153 if tag1 _LT_ tag2 then LT_ else GT_
155 tag (MonoTyVar n1) = (ILIT(1) :: FAST_INT)
156 tag (TupleMonoTy tys1) = ILIT(2)
157 tag (ListMonoTy ty1) = ILIT(3)
158 tag (MonoTyCon tc1 tys1) = ILIT(4)
159 tag (FunMonoTy a1 b1) = ILIT(5)
160 tag (MonoTyVarTemplate n1) = ILIT(6)
161 tag (MonoDict c1 ty1) = ILIT(7)
163 tag (MonoTyProc tys1 ty1) = ILIT(8)
164 tag (MonoTyPod ty1) = ILIT(9)
165 #endif {- Data Parallel Haskell -}
169 = cmpList cmp_ctxt a b
171 cmp_ctxt (c1, tv1) (c2, tv2)
172 = case cmp c1 c2 of { EQ_ -> cmp tv1 tv2; xxx -> xxx }
175 cmpList cmp [] [] = EQ_
176 cmpList cmp [] _ = LT_
177 cmpList cmp _ [] = GT_
178 cmpList cmp (a:as) (b:bs)
179 = case cmp a b of { EQ_ -> cmpList cmp as bs; xxx -> xxx }
182 = case (panic "cmpList (HsTypes)") of { l -> cmpList cmp l l } -- BUG avoidance
186 eqMonoType :: ProtoNameMonoType -> ProtoNameMonoType -> Bool
188 eqMonoType a b = case (cmpMonoType cmpProtoName a b) of { EQ_ -> True; _ -> False }
191 This is used in various places:
193 pprContext :: (Outputable name) => PprStyle -> (Context name) -> Pretty
195 pprContext sty [] = ppNil
196 pprContext sty [(clas, ty)] = ppCat [ppr sty clas, ppr sty ty, ppStr "=>"]
197 pprContext sty context
198 = ppBesides [ppLparen,
199 ppInterleave ppComma (map pp_assert context),
200 ppRparen, ppStr " =>"]
203 = ppCat [ppr sty clas, ppr sty ty]
207 instance (Outputable name) => Outputable (PolyType name) where
208 ppr sty (UnoverloadedTy ty) = ppr sty ty
209 ppr sty (OverloadedTy ctxt ty)
210 = ppCat [pprContext sty ctxt, ppr sty ty]
211 ppr sty (ForAllTy tvs ty)
212 = ppBesides [ppStr "_forall_ ", interppSP sty tvs, ppStr " => ", ppr sty ty]
214 instance (Outputable name) => Outputable (MonoType name) where
217 pREC_TOP = (0 :: Int)
218 pREC_FUN = (1 :: Int)
219 pREC_CON = (2 :: Int)
221 -- printing works more-or-less as for UniTypes (in UniTyFuns)
223 pprMonoType, pprParendMonoType :: (Outputable name) => PprStyle -> MonoType name -> Pretty
225 pprMonoType sty ty = ppr_mono_ty sty pREC_TOP ty
226 pprParendMonoType sty ty = ppr_mono_ty sty pREC_CON ty
228 ppr_mono_ty sty ctxt_prec (MonoTyVar name) = ppr sty name
230 ppr_mono_ty sty ctxt_prec (FunMonoTy ty1 ty2)
231 = let p1 = ppr_mono_ty sty pREC_FUN ty1
232 p2 = ppr_mono_ty sty pREC_TOP ty2
234 if ctxt_prec < pREC_FUN then -- no parens needed
235 ppSep [p1, ppBeside (ppStr "-> ") p2]
237 ppSep [ppBeside ppLparen p1, ppBesides [ppStr "-> ", p2, ppRparen]]
239 ppr_mono_ty sty ctxt_prec (TupleMonoTy tys)
240 = ppBesides [ppLparen, ppInterleave ppComma (map (ppr sty) tys), ppRparen]
242 ppr_mono_ty sty ctxt_prec (ListMonoTy ty)
243 = ppBesides [ppLbrack, ppr_mono_ty sty pREC_TOP ty, ppRbrack]
245 ppr_mono_ty sty ctxt_prec (MonoTyCon tycon tys)
246 = let pp_tycon = ppr sty tycon in
249 else if ctxt_prec < pREC_CON then -- no parens needed
250 ppCat [pp_tycon, ppInterleave ppNil (map (ppr_mono_ty sty pREC_CON) tys)]
252 ppBesides [ ppLparen, pp_tycon, ppSP,
253 ppInterleave ppNil (map (ppr_mono_ty sty pREC_CON) tys), ppRparen ]
256 ppr_mono_ty sty ctxt_prec (MonoTyVarTemplate tv) = ppr sty tv
258 ppr_mono_ty sty ctxt_prec (MonoDict clas ty)
259 = ppBesides [ppStr "{{", ppr sty clas, ppSP, ppr_mono_ty sty ctxt_prec ty, ppStr "}}"]
262 ppr_mono_ty sty ctxt_prec (MonoTyProc tys ty)
263 = ppBesides [ppStr "(|",
264 ppInterleave ppComma (map (ppr_mono_ty sty pREC_TOP) tys),
266 ppr_mono_ty sty pREC_TOP ty,
269 ppr_mono_ty sty ctxt_prec (MonoTyPod ty)
270 = ppBesides [ppStr "<<", ppr_mono_ty sty pREC_TOP ty ,ppStr ">>"]
272 #endif {- Data Parallel Haskell -}