e558d4dd46534f26bc97b8aa155619d724af18e4
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsTypes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[HsTypes]{Abstract syntax: user-defined types}
5
6 If compiled without \tr{#define COMPILING_GHC}, you get
7 (part of) a Haskell-abstract-syntax library.  With it,
8 you get part of GHC.
9
10 \begin{code}
11 #include "HsVersions.h"
12
13 module HsTypes (
14         HsType(..), HsTyVar(..),
15         SYN_IE(Context), SYN_IE(ClassAssertion)
16
17         , mkHsForAllTy
18         , getTyVarName, replaceTyVarName
19         , pprParendHsType
20         , pprContext
21         , cmpHsType, cmpContext
22     ) where
23
24 IMP_Ubiq()
25
26 import Outputable       ( interppSP, ifnotPprForUser )
27 import Kind             ( Kind {- instance Outputable -} )
28 import Pretty
29 import Util             ( thenCmp, cmpList, isIn, panic# )
30 \end{code}
31
32 This is the syntax for types as seen in type signatures.
33
34 \begin{code}
35 type Context name = [ClassAssertion name]
36
37 type ClassAssertion name = (name, HsType name)
38         -- The type is usually a type variable, but it
39         -- doesn't have to be when reading interface files
40
41 data HsType name
42   = HsPreForAllTy       (Context name)
43                         (HsType name)
44
45         -- The renamer turns HsPreForAllTys into HsForAllTys when they
46         -- occur in signatures, to make the binding of variables
47         -- explicit.  This distinction is made visible for
48         -- non-COMPILING_GHC code, because you probably want to do the
49         -- same thing.
50
51   | HsForAllTy          [HsTyVar name]
52                         (Context name)
53                         (HsType name)
54
55   | MonoTyVar           name            -- Type variable
56
57   | MonoTyApp           name            -- Type constructor or variable
58                         [HsType name]
59
60     -- We *could* have a "MonoTyCon name" equiv to "MonoTyApp name []"
61     -- (for efficiency, what?)  WDP 96/02/18
62
63   | MonoFunTy           (HsType name) -- function type
64                         (HsType name)
65
66   | MonoListTy          name            -- The list TyCon name
67                         (HsType name)   -- Element type
68
69   | MonoTupleTy         name            -- The tuple TyCon name
70                         [HsType name]   -- Element types (length gives arity)
71
72   -- these next two are only used in unfoldings in interfaces
73   | MonoDictTy          name    -- Class
74                         (HsType name)
75
76 mkHsForAllTy []  []   ty = ty
77 mkHsForAllTy tvs ctxt ty = HsForAllTy tvs ctxt ty
78
79 data HsTyVar name
80   = UserTyVar name
81   | IfaceTyVar name Kind
82         -- *** NOTA BENE *** A "monotype" in a pragma can have
83         -- for-alls in it, (mostly to do with dictionaries).  These
84         -- must be explicitly Kinded.
85
86 getTyVarName (UserTyVar n)    = n
87 getTyVarName (IfaceTyVar n _) = n
88
89 replaceTyVarName :: HsTyVar name1 -> name2 -> HsTyVar name2
90 replaceTyVarName (UserTyVar n)    n' = UserTyVar n'
91 replaceTyVarName (IfaceTyVar n k) n' = IfaceTyVar n' k
92 \end{code}
93
94
95 %************************************************************************
96 %*                                                                      *
97 \subsection{Pretty printing}
98 %*                                                                      *
99 %************************************************************************
100
101 \begin{code}
102
103 instance (Outputable name) => Outputable (HsType name) where
104     ppr = pprHsType
105
106 instance (Outputable name) => Outputable (HsTyVar name) where
107     ppr sty (UserTyVar name) = ppr sty name
108     ppr sty (IfaceTyVar name kind) = ppCat [ppr sty name, ppStr "::", ppr sty kind]
109
110
111 ppr_forall sty ctxt_prec [] [] ty
112    = ppr_mono_ty sty ctxt_prec ty
113 ppr_forall sty ctxt_prec tvs ctxt ty
114    = ppSep [ppStr "_forall_", ppBracket (interppSP sty tvs),
115             pprContext sty ctxt,  ppStr "=>",
116             pprHsType sty ty]
117
118 pprContext :: (Outputable name) => PprStyle -> (Context name) -> Pretty
119 pprContext sty []               = ppNil
120 pprContext sty context
121   = ppCat [ppCurlies (ppIntersperse pp'SP (map ppr_assert context))]
122   where
123     ppr_assert (clas, ty) = ppCat [ppr sty clas, ppr sty ty]
124 \end{code}
125
126 \begin{code}
127 pREC_TOP = (0 :: Int)
128 pREC_FUN = (1 :: Int)
129 pREC_CON = (2 :: Int)
130
131 maybeParen :: Bool -> Pretty -> Pretty
132 maybeParen True  p = ppParens p
133 maybeParen False p = p
134         
135 -- printing works more-or-less as for Types
136
137 pprHsType, pprParendHsType :: (Outputable name) => PprStyle -> HsType name -> Pretty
138
139 pprHsType sty ty       = ppr_mono_ty sty pREC_TOP ty
140 pprParendHsType sty ty = ppr_mono_ty sty pREC_CON ty
141
142 ppr_mono_ty sty ctxt_prec (HsPreForAllTy ctxt ty)     = ppr_forall sty ctxt_prec [] ctxt ty
143 ppr_mono_ty sty ctxt_prec (HsForAllTy tvs ctxt ty)    = ppr_forall sty ctxt_prec tvs ctxt ty
144
145 ppr_mono_ty sty ctxt_prec (MonoTyVar name) = ppr sty name
146
147 ppr_mono_ty sty ctxt_prec (MonoFunTy ty1 ty2)
148   = let p1 = ppr_mono_ty sty pREC_FUN ty1
149         p2 = ppr_mono_ty sty pREC_TOP ty2
150     in
151     maybeParen (ctxt_prec >= pREC_FUN)
152                (ppSep [p1, ppBeside (ppStr "-> ") p2])
153
154 ppr_mono_ty sty ctxt_prec (MonoTupleTy _ tys)
155  = ppParens (ppInterleave ppComma (map (ppr sty) tys))
156
157 ppr_mono_ty sty ctxt_prec (MonoListTy _ ty)
158  = ppBesides [ppLbrack, ppr_mono_ty sty pREC_TOP ty, ppRbrack]
159
160 ppr_mono_ty sty ctxt_prec (MonoTyApp tycon tys)
161   = let pp_tycon = ppr sty tycon in
162     if null tys then
163         pp_tycon
164     else 
165         maybeParen (ctxt_prec >= pREC_CON)
166                    (ppCat [pp_tycon, ppInterleave ppNil (map (ppr_mono_ty sty pREC_CON) tys)])
167
168 ppr_mono_ty sty ctxt_prec (MonoDictTy clas ty)
169   = ppCurlies (ppCat [ppr sty clas, ppr_mono_ty sty pREC_CON ty])
170         -- Curlies are temporary
171 \end{code}
172
173
174 %************************************************************************
175 %*                                                                      *
176 \subsection{Comparison}
177 %*                                                                      *
178 %************************************************************************
179
180 We do define a specialised equality for these \tr{*Type} types; used
181 in checking interfaces.  Most any other use is likely to be {\em
182 wrong}, so be careful!
183
184 \begin{code}
185 cmpHsTyVar :: (a -> a -> TAG_) -> HsTyVar a -> HsTyVar a -> TAG_
186 cmpHsType :: (a -> a -> TAG_) -> HsType a -> HsType a -> TAG_
187 cmpContext  :: (a -> a -> TAG_) -> Context  a -> Context  a -> TAG_
188
189 cmpHsTyVar cmp (UserTyVar v1)    (UserTyVar v2)    = v1 `cmp` v2
190 cmpHsTyVar cmp (IfaceTyVar v1 _) (IfaceTyVar v2 _) = v1 `cmp` v2
191 cmpHsTyVar cmp (UserTyVar _)     other             = LT_
192 cmpHsTyVar cmp other1            other2            = GT_
193
194
195 -- We assume that HsPreForAllTys have been smashed by now.
196 # ifdef DEBUG
197 cmpHsType _ (HsPreForAllTy _ _) _ = panic# "cmpHsType:HsPreForAllTy:1st arg"
198 cmpHsType _ _ (HsPreForAllTy _ _) = panic# "cmpHsType:HsPreForAllTy:2nd arg"
199 # endif
200
201 cmpHsType cmp (HsForAllTy tvs1 c1 t1) (HsForAllTy tvs2 c2 t2)
202   = cmpList (cmpHsTyVar cmp) tvs1 tvs2  `thenCmp`
203     cmpContext cmp c1 c2                `thenCmp`
204     cmpHsType cmp t1 t2
205
206 cmpHsType cmp (MonoTyVar n1) (MonoTyVar n2)
207   = cmp n1 n2
208
209 cmpHsType cmp (MonoTupleTy _ tys1) (MonoTupleTy _ tys2)
210   = cmpList (cmpHsType cmp) tys1 tys2
211 cmpHsType cmp (MonoListTy _ ty1) (MonoListTy _ ty2)
212   = cmpHsType cmp ty1 ty2
213
214 cmpHsType cmp (MonoTyApp tc1 tys1) (MonoTyApp tc2 tys2)
215   = cmp tc1 tc2 `thenCmp`
216     cmpList (cmpHsType cmp) tys1 tys2
217
218 cmpHsType cmp (MonoFunTy a1 b1) (MonoFunTy a2 b2)
219   = cmpHsType cmp a1 a2 `thenCmp` cmpHsType cmp b1 b2
220
221 cmpHsType cmp (MonoDictTy c1 ty1)   (MonoDictTy c2 ty2)
222   = cmp c1 c2 `thenCmp` cmpHsType cmp ty1 ty2
223
224 cmpHsType cmp ty1 ty2 -- tags must be different
225   = let tag1 = tag ty1
226         tag2 = tag ty2
227     in
228     if tag1 _LT_ tag2 then LT_ else GT_
229   where
230     tag (MonoTyVar n1)          = (ILIT(1) :: FAST_INT)
231     tag (MonoTupleTy _ tys1)    = ILIT(2)
232     tag (MonoListTy _ ty1)      = ILIT(3)
233     tag (MonoTyApp tc1 tys1)    = ILIT(4)
234     tag (MonoFunTy a1 b1)       = ILIT(5)
235     tag (MonoDictTy c1 ty1)     = ILIT(7)
236     tag (HsForAllTy _ _ _)      = ILIT(8)
237     tag (HsPreForAllTy _ _)     = ILIT(9)
238
239 -------------------
240 cmpContext cmp a b
241   = cmpList cmp_ctxt a b
242   where
243     cmp_ctxt (c1, ty1) (c2, ty2)
244       = cmp c1 c2 `thenCmp` cmpHsType cmp ty1 ty2
245 \end{code}