2536e8d07ca8f2f694c30880a22e64476aa7d21f
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsTypes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[HsTypes]{Abstract syntax: user-defined types}
5
6 \begin{code}
7 module HsTypes (
8         HsType(..), MonoUsageAnn(..), HsTyVar(..),
9         HsContext, HsClassAssertion, HsPred(..)
10
11         , mkHsForAllTy, mkHsUsForAllTy
12         , getTyVarName, replaceTyVarName
13         , pprParendHsType
14         , pprForAll, pprHsContext, pprHsClassAssertion, pprHsPred
15         , cmpHsType, cmpHsTypes, cmpHsContext, cmpHsPred
16     ) where
17
18 #include "HsVersions.h"
19
20 import Type             ( Kind, UsageAnn(..) )
21 import PprType          ( {- instance Outputable Kind -} )
22 import Outputable
23 import Util             ( thenCmp, cmpList )
24 \end{code}
25
26 This is the syntax for types as seen in type signatures.
27
28 \begin{code}
29 type HsContext name = [HsPred name]
30 type HsClassAssertion name = (name, [HsType name])
31 -- The type is usually a type variable, but it
32 -- doesn't have to be when reading interface files
33 data HsPred name =
34     HsPClass name [HsType name]
35   | HsPIParam name (HsType name)
36
37 data HsType name
38   = HsForAllTy          (Maybe [HsTyVar name])  -- Nothing for implicitly quantified signatures
39                         (HsContext name)
40                         (HsType name)
41
42   | MonoTyVar           name            -- Type variable
43
44   | MonoTyApp           (HsType name)
45                         (HsType name)
46
47   | MonoFunTy           (HsType name) -- function type
48                         (HsType name)
49
50   | MonoListTy          (HsType name)   -- Element type
51
52   | MonoTupleTy         [HsType name]   -- Element types (length gives arity)
53                         Bool            -- boxed?
54
55   | MonoIParamTy        name (HsType name)
56
57   -- these next two are only used in interfaces
58   | MonoDictTy          name    -- Class
59                         [HsType name]
60
61   | MonoUsgTy           (MonoUsageAnn name)
62                         (HsType name)
63
64   | MonoUsgForAllTy     name
65                         (HsType name)
66
67 data MonoUsageAnn name
68   = MonoUsOnce
69   | MonoUsMany
70   | MonoUsVar name
71   
72
73 -- Combine adjacent for-alls. 
74 -- The following awkward situation can happen otherwise:
75 --      f :: forall a. ((Num a) => Int)
76 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
77 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
78 -- but the export list abstracts f wrt [a].  Disaster.
79 --
80 -- A valid type must have one for-all at the top of the type, or of the fn arg types
81
82 mkHsForAllTy (Just []) [] ty = ty       -- Explicit for-all with no tyvars
83 mkHsForAllTy mtvs1     [] (HsForAllTy mtvs2 ctxt ty) = mkHsForAllTy (mtvs1 `plus` mtvs2) ctxt ty
84                                                      where
85                                                        mtvs1       `plus` Nothing     = mtvs1
86                                                        Nothing     `plus` mtvs2       = mtvs2 
87                                                        (Just tvs1) `plus` (Just tvs2) = Just (tvs1 ++ tvs2)
88 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
89
90 mkHsUsForAllTy uvs ty = foldr (\ uv ty -> MonoUsgForAllTy uv ty)
91                               ty uvs
92
93 data HsTyVar name
94   = UserTyVar name
95   | IfaceTyVar name Kind
96         -- *** NOTA BENE *** A "monotype" in a pragma can have
97         -- for-alls in it, (mostly to do with dictionaries).  These
98         -- must be explicitly Kinded.
99
100 getTyVarName (UserTyVar n)    = n
101 getTyVarName (IfaceTyVar n _) = n
102
103 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
104 replaceTyVarName (UserTyVar n)    n' = UserTyVar n'
105 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
106 \end{code}
107
108
109 %************************************************************************
110 %*                                                                      *
111 \subsection{Pretty printing}
112 %*                                                                      *
113 %************************************************************************
114
115 \begin{code}
116
117 instance (Outputable name) => Outputable (HsType name) where
118     ppr ty = pprHsType ty
119
120 instance (Outputable name) => Outputable (HsTyVar name) where
121     ppr (UserTyVar name)       = ppr name
122     ppr (IfaceTyVar name kind) = hsep [ppr name, dcolon, ppr kind]
123
124 -- Better to see those for-alls
125 -- pprForAll []  = empty
126 pprForAll tvs = ptext SLIT("forall") <+> interppSP tvs <> ptext SLIT(".")
127
128 pprHsContext :: (Outputable name) => HsContext name -> SDoc
129 pprHsContext []    = empty
130 pprHsContext context = parens (hsep (punctuate comma (map pprHsPred context))) <+> ptext SLIT("=>")
131
132 pprHsClassAssertion :: (Outputable name) => HsClassAssertion name -> SDoc
133 pprHsClassAssertion (clas, tys)
134   = ppr clas <+> hsep (map pprParendHsType tys)
135
136 pprHsPred :: (Outputable name) => HsPred name -> SDoc
137 pprHsPred (HsPClass clas tys)
138   = ppr clas <+> hsep (map pprParendHsType tys)
139 pprHsPred (HsPIParam n ty)
140   = hsep [{- char '?' <> -} ppr n, text "::", ppr ty]
141 \end{code}
142
143 \begin{code}
144 pREC_TOP = (0 :: Int)
145 pREC_FUN = (1 :: Int)
146 pREC_CON = (2 :: Int)
147
148 maybeParen :: Bool -> SDoc -> SDoc
149 maybeParen True  p = parens p
150 maybeParen False p = p
151         
152 -- printing works more-or-less as for Types
153
154 pprHsType, pprParendHsType :: (Outputable name) => HsType name -> SDoc
155
156 pprHsType ty       = ppr_mono_ty pREC_TOP ty
157 pprParendHsType ty = ppr_mono_ty pREC_CON ty
158
159 ppr_mono_ty ctxt_prec (HsForAllTy maybe_tvs ctxt ty)
160   = maybeParen (ctxt_prec >= pREC_FUN) $
161     sep [pp_tvs, pprHsContext ctxt, pprHsType ty]
162   where
163     pp_tvs = case maybe_tvs of
164                 Just tvs -> pprForAll tvs
165                 Nothing  -> text "{- implicit forall -}"
166
167 ppr_mono_ty ctxt_prec (MonoTyVar name)
168   = ppr name
169
170 ppr_mono_ty ctxt_prec (MonoFunTy ty1 ty2)
171   = let p1 = ppr_mono_ty pREC_FUN ty1
172         p2 = ppr_mono_ty pREC_TOP ty2
173     in
174     maybeParen (ctxt_prec >= pREC_FUN)
175                (sep [p1, (<>) (ptext SLIT("-> ")) p2])
176
177 ppr_mono_ty ctxt_prec (MonoTupleTy tys True)
178  = parens (sep (punctuate comma (map ppr tys)))
179 ppr_mono_ty ctxt_prec (MonoTupleTy tys False)
180  = ptext SLIT("(#") <> sep (punctuate comma (map ppr tys)) <> ptext SLIT("#)")
181
182 ppr_mono_ty ctxt_prec (MonoListTy ty)
183  = brackets (ppr_mono_ty pREC_TOP ty)
184
185 ppr_mono_ty ctxt_prec (MonoTyApp fun_ty arg_ty)
186   = maybeParen (ctxt_prec >= pREC_CON)
187                (hsep [ppr_mono_ty pREC_FUN fun_ty, ppr_mono_ty pREC_CON arg_ty])
188
189 ppr_mono_ty ctxt_prec (MonoIParamTy n ty)
190   = hsep [{- char '?' <> -} ppr n, text "::", ppr_mono_ty pREC_TOP ty]
191
192 ppr_mono_ty ctxt_prec (MonoDictTy clas tys)
193   = ppr clas <+> hsep (map (ppr_mono_ty pREC_CON) tys)
194
195 ppr_mono_ty ctxt_prec ty@(MonoUsgForAllTy _ _)
196   = maybeParen (ctxt_prec >= pREC_FUN) $
197     sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
198           ppr_mono_ty pREC_TOP sigma
199         ]
200   where
201     (uvars,sigma) = split [] ty
202     pp_uvars      = interppSP uvars
203
204     split uvs (MonoUsgForAllTy uv ty') = split (uv:uvs) ty'
205     split uvs ty'                      = (reverse uvs,ty')
206
207 ppr_mono_ty ctxt_prec (MonoUsgTy u ty)
208   = maybeParen (ctxt_prec >= pREC_CON) $
209     ptext SLIT("__u") <+> pp_ua <+> ppr_mono_ty pREC_CON ty
210   where
211     pp_ua = case u of
212               MonoUsOnce   -> ptext SLIT("-")
213               MonoUsMany   -> ptext SLIT("!")
214               MonoUsVar uv -> ppr uv
215 \end{code}
216
217
218 %************************************************************************
219 %*                                                                      *
220 \subsection{Comparison}
221 %*                                                                      *
222 %************************************************************************
223
224 We do define a specialised equality for these \tr{*Type} types; used
225 in checking interfaces.  Most any other use is likely to be {\em
226 wrong}, so be careful!
227
228 \begin{code}
229 cmpHsTyVar   :: (a -> a -> Ordering) -> HsTyVar a   -> HsTyVar a   -> Ordering
230 cmpHsType    :: (a -> a -> Ordering) -> HsType a    -> HsType a    -> Ordering
231 cmpHsTypes   :: (a -> a -> Ordering) -> [HsType a]  -> [HsType a]  -> Ordering
232 cmpHsContext :: (a -> a -> Ordering) -> HsContext a -> HsContext a -> Ordering
233 cmpHsPred    :: (a -> a -> Ordering) -> HsPred a    -> HsPred a    -> Ordering
234
235 cmpHsTyVar cmp (UserTyVar v1)    (UserTyVar v2)    = v1 `cmp` v2
236 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
237 cmpHsTyVar cmp (UserTyVar _)     other             = LT
238 cmpHsTyVar cmp other1            other2            = GT
239
240 cmpHsTypes cmp [] []   = EQ
241 cmpHsTypes cmp [] tys2 = LT
242 cmpHsTypes cmp tys1 [] = GT
243 cmpHsTypes cmp (ty1:tys1) (ty2:tys2) = cmpHsType cmp ty1 ty2 `thenCmp` cmpHsTypes cmp tys1 tys2
244
245 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
246   = cmpMaybe (cmpList (cmpHsTyVar cmp)) tvs1 tvs2       `thenCmp`
247     cmpHsContext cmp c1 c2                              `thenCmp`
248     cmpHsType cmp t1 t2
249
250 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
251   = cmp n1 n2
252
253 cmpHsType cmp (MonoTupleTy tys1 b1) (MonoTupleTy tys2 b2)
254   = (b1 `compare` b2) `thenCmp` cmpHsTypes cmp tys1 tys2
255
256 cmpHsType cmp (MonoListTy ty1) (MonoListTy ty2)
257   = cmpHsType cmp ty1 ty2
258
259 cmpHsType cmp (MonoTyApp fun_ty1 arg_ty1) (MonoTyApp fun_ty2 arg_ty2)
260   = cmpHsType cmp fun_ty1 fun_ty2 `thenCmp` cmpHsType cmp arg_ty1 arg_ty2
261
262 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
263   = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
264
265 cmpHsType cmp (MonoDictTy c1 tys1)   (MonoDictTy c2 tys2)
266   = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
267
268 cmpHsType cmp (MonoUsgTy u1 ty1) (MonoUsgTy u2 ty2)
269   = cmpUsg cmp u1 u2 `thenCmp` cmpHsType cmp ty1 ty2
270
271 cmpHsType cmp ty1 ty2 -- tags must be different
272   = let tag1 = tag ty1
273         tag2 = tag ty2
274     in
275     if tag1 _LT_ tag2 then LT else GT
276   where
277     tag (MonoTyVar n1)                  = (ILIT(1) :: FAST_INT)
278     tag (MonoTupleTy tys1 _)            = ILIT(2)
279     tag (MonoListTy ty1)                = ILIT(3)
280     tag (MonoTyApp tc1 tys1)            = ILIT(4)
281     tag (MonoFunTy a1 b1)               = ILIT(5)
282     tag (MonoDictTy c1 tys1)            = ILIT(6)
283     tag (MonoUsgTy c1 ty1)              = ILIT(7)
284     tag (MonoUsgForAllTy uv1 ty1)       = ILIT(8)
285     tag (HsForAllTy _ _ _)              = ILIT(9)
286
287 -------------------
288 cmpHsContext cmp a b
289   = cmpList (cmpHsPred cmp) a b
290
291 cmpHsPred cmp (HsPClass c1 tys1) (HsPClass c2 tys2)
292   = cmp c1 c2 `thenCmp` cmpHsTypes cmp tys1 tys2
293 cmpHsPred cmp (HsPIParam n1 ty1) (HsPIParam n2 ty2)
294   = cmp n1 n2 `thenCmp` cmpHsType cmp ty1 ty2
295 cmpHsPred cmp (HsPClass _ _) (HsPIParam _ _) = LT
296 cmpHsPred cmp _              _               = GT
297
298 cmpUsg cmp  MonoUsOnce     MonoUsOnce    = EQ
299 cmpUsg cmp  MonoUsMany     MonoUsMany    = EQ
300 cmpUsg cmp (MonoUsVar u1) (MonoUsVar u2) = cmp u1 u2
301
302 cmpUsg cmp ua1 ua2  -- tags must be different
303   = let tag1 = tag ua1
304         tag2 = tag ua2
305     in
306         if tag1 _LT_ tag2 then LT else GT
307   where
308     tag MonoUsOnce       = (ILIT(1) :: FAST_INT)
309     tag MonoUsMany       = ILIT(2)
310     tag (MonoUsVar    _) = ILIT(3)
311
312 -- Should be in Maybes, I guess
313 cmpMaybe cmp Nothing  Nothing  = EQ
314 cmpMaybe cmp Nothing  (Just x) = LT
315 cmpMaybe cmp (Just x)  Nothing = GT
316 cmpMaybe cmp (Just x) (Just y) = x `cmp` y
317 \end{code}