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