[project @ 2005-01-18 12:18:11 by simonpj]
[ghc-hetmet.git] / ghc / compiler / iface / IfaceType.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4
5         This module defines interface types and binders
6
7 \begin{code}
8 module IfaceType (
9         IfaceType(..), IfaceKind, IfacePredType(..), IfaceTyCon(..),
10         IfaceContext, IfaceBndr(..), IfaceTvBndr, IfaceIdBndr,
11
12         IfaceExtName(..), mkIfaceExtName, ifaceTyConName, 
13
14         -- Conversion from Type -> IfaceType
15         toIfaceType, toIfacePred, toIfaceContext, 
16         toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs, 
17
18         -- Printing
19         pprIfaceType, pprParendIfaceType, pprIfaceContext, 
20         pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
21         tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
22
23     ) where
24
25 #include "HsVersions.h"
26
27 import Kind             ( Kind(..) )
28 import TypeRep          ( Type(..), TyNote(..), PredType(..), Kind, ThetaType )
29 import TyCon            ( TyCon, isTupleTyCon, tyConArity, tupleTyConBoxity )
30 import Var              ( isId, tyVarKind, idType )
31 import TysWiredIn       ( listTyConName, parrTyConName, tupleTyCon, intTyConName, charTyConName, boolTyConName )
32 import OccName          ( OccName )
33 import Name             ( Name, getName, getOccName, nameModule, nameOccName )
34 import Module           ( Module )
35 import BasicTypes       ( IPName(..), Arity, Version, mapIPName, tupleParens, Boxity )
36 import Outputable
37 import FastString
38 \end{code}
39
40         
41 %************************************************************************
42 %*                                                                      *
43                 IfaceExtName
44 %*                                                                      *
45 %************************************************************************
46
47 \begin{code}
48 data IfaceExtName
49   = ExtPkg Module OccName               -- From an external package; no version #
50                                         -- Also used for wired-in things regardless
51                                         -- of whether they are home-pkg or not
52
53   | HomePkg Module OccName Version      -- From another module in home package;
54                                         -- has version #; in all other respects,
55                                         -- HomePkg and ExtPkg are the same
56
57   | LocalTop OccName                    -- Top-level from the same module as 
58                                         -- the enclosing IfaceDecl
59
60   | LocalTopSub         -- Same as LocalTop, but for a class method or constr
61         OccName         -- Class-meth/constr name
62         OccName         -- Parent class/datatype name
63         -- LocalTopSub is written into iface files as LocalTop; the parent 
64         -- info is only used when computing version information in MkIface
65
66 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
67         -- Local helper for wired-in names
68 \end{code}
69
70
71 %************************************************************************
72 %*                                                                      *
73                 Local (nested) binders
74 %*                                                                      *
75 %************************************************************************
76
77 \begin{code}
78 data IfaceBndr          -- Local (non-top-level) binders
79   = IfaceIdBndr IfaceIdBndr
80   | IfaceTvBndr IfaceTvBndr
81
82 type IfaceIdBndr  = (OccName, IfaceType)        -- OccName, because always local
83 type IfaceTvBndr  = (OccName, IfaceKind)
84
85 -------------------------------
86 type IfaceKind = Kind                   -- Re-use the Kind type, but no KindVars in it
87
88 data IfaceType
89   = IfaceTyVar    OccName                       -- Type variable only, not tycon
90   | IfaceAppTy    IfaceType IfaceType
91   | IfaceForAllTy IfaceTvBndr IfaceType
92   | IfacePredTy   IfacePredType
93   | IfaceTyConApp IfaceTyCon [IfaceType]        -- Not necessarily saturated
94                                                 -- Includes newtypes, synonyms, tuples
95   | IfaceFunTy  IfaceType IfaceType
96
97 data IfacePredType      -- NewTypes are handled as ordinary TyConApps
98   = IfaceClassP IfaceExtName [IfaceType]
99   | IfaceIParam (IPName OccName) IfaceType
100
101 type IfaceContext = [IfacePredType]
102
103 data IfaceTyCon         -- Abbreviations for common tycons with known names
104   = IfaceTc IfaceExtName        -- The common case
105   | IfaceIntTc | IfaceBoolTc | IfaceCharTc
106   | IfaceListTc | IfacePArrTc
107   | IfaceTupTc Boxity Arity 
108
109 ifaceTyConName :: IfaceTyCon -> Name    -- Works for all except IfaceTc
110 ifaceTyConName IfaceIntTc         = intTyConName
111 ifaceTyConName IfaceBoolTc        = boolTyConName
112 ifaceTyConName IfaceCharTc        = charTyConName
113 ifaceTyConName IfaceListTc        = listTyConName
114 ifaceTyConName IfacePArrTc        = parrTyConName
115 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
116 ifaceTyConName (IfaceTc ext)      = pprPanic "ifaceTyConName" (ppr ext)
117 \end{code}
118
119
120 %************************************************************************
121 %*                                                                      *
122                 Functions over IFaceTypes
123 %*                                                                      *
124 %************************************************************************
125
126
127 \begin{code}
128 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
129 -- Mainly for printing purposes
130 splitIfaceSigmaTy ty
131   = (tvs,theta,tau)
132   where
133     (tvs, rho)   = split_foralls ty
134     (theta, tau) = split_rho rho
135
136     split_foralls (IfaceForAllTy tv ty) 
137         = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
138     split_foralls rho = ([], rho)
139
140     split_rho (IfaceFunTy (IfacePredTy st) ty) 
141         = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
142     split_rho tau = ([], tau)
143 \end{code}
144
145 %************************************************************************
146 %*                                                                      *
147                 Pretty-printing
148 %*                                                                      *
149 %************************************************************************
150
151 Precedence
152 ~~~~~~~~~~
153 @ppr_ty@ takes an @Int@ that is the precedence of the context.
154 The precedence levels are:
155 \begin{description}
156 \item[tOP_PREC]   No parens required.
157 \item[fUN_PREC]   Left hand argument of a function arrow.
158 \item[tYCON_PREC] Argument of a type constructor.
159 \end{description}
160
161 \begin{code}
162 tOP_PREC    = (0 :: Int)  -- type   in ParseIface.y
163 fUN_PREC    = (1 :: Int)  -- btype  in ParseIface.y
164 tYCON_PREC  = (2 :: Int)  -- atype  in ParseIface.y
165
166 noParens :: SDoc -> SDoc
167 noParens pp = pp
168
169 maybeParen ctxt_prec inner_prec pretty
170   | ctxt_prec < inner_prec = pretty
171   | otherwise              = parens pretty
172 \end{code}
173
174
175 ----------------------------- Printing binders ------------------------------------
176
177 \begin{code}
178 -- These instances are used only when printing for the user, either when
179 -- debugging, or in GHCi when printing the results of a :info command
180 instance Outputable IfaceExtName where
181     ppr (ExtPkg mod occ)       = pprExt mod occ
182     ppr (HomePkg mod occ vers) = pprExt mod occ <> braces (ppr vers)
183     ppr (LocalTop occ)         = ppr occ        -- Do we want to distinguish these 
184     ppr (LocalTopSub occ _)    = ppr occ        -- from an ordinary occurrence?
185
186 pprExt :: Module -> OccName -> SDoc
187 pprExt mod occ
188   = getPprStyle $ \ sty ->
189     if unqualStyle sty mod occ then
190         ppr occ
191     else 
192         ppr mod <> dot <> ppr occ
193
194 instance Outputable IfaceBndr where
195     ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
196     ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
197
198 pprIfaceBndrs :: [IfaceBndr] -> SDoc
199 pprIfaceBndrs bs = sep (map ppr bs)
200
201 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
202
203 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
204 pprIfaceTvBndr (tv, LiftedTypeKind) = ppr tv
205 pprIfaceTvBndr (tv, kind)           = parens (ppr tv <> dcolon <> ppr kind)
206
207 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
208 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
209 \end{code}
210
211 ----------------------------- Printing IfaceType ------------------------------------
212
213 \begin{code}
214 ---------------------------------
215 instance Outputable IfaceType where
216   ppr ty = pprIfaceTypeForUser ty
217
218 pprIfaceTypeForUser ::IfaceType -> SDoc
219 -- Drop top-level for-alls; if that's not what you want, use pprIfaceType dire
220 pprIfaceTypeForUser ty
221   = pprIfaceForAllPart [] theta (pprIfaceType tau)
222  where          
223     (_tvs, theta, tau) = splitIfaceSigmaTy ty
224
225 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
226 pprIfaceType       = ppr_ty tOP_PREC
227 pprParendIfaceType = ppr_ty tYCON_PREC
228
229
230 ppr_ty :: Int -> IfaceType -> SDoc
231 ppr_ty ctxt_prec (IfaceTyVar tyvar)     = ppr tyvar
232 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
233 ppr_ty ctxt_prec (IfacePredTy st)       = ppr st
234
235         -- Function types
236 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
237   = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
238     maybeParen ctxt_prec fUN_PREC $
239     sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
240   where
241     ppr_fun_tail (IfaceFunTy ty1 ty2) 
242       = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
243     ppr_fun_tail other_ty
244       = [arrow <+> pprIfaceType other_ty]
245
246 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
247   = maybeParen ctxt_prec tYCON_PREC $
248     ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
249
250 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
251   = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
252  where          
253     (tvs, theta, tau) = splitIfaceSigmaTy ty
254     
255 -------------------
256 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
257 pprIfaceForAllPart tvs ctxt doc 
258   = sep [ppr_tvs, pprIfaceContext ctxt, doc]
259   where
260     ppr_tvs | null tvs  = empty
261             | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
262
263 -------------------
264 ppr_tc_app ctxt_prec tc          []   = ppr tc
265 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets   (pprIfaceType ty)
266 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
267 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
268   | arity == length tys 
269   = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
270 ppr_tc_app ctxt_prec tc tys 
271   = maybeParen ctxt_prec tYCON_PREC 
272                (sep [ppr tc, nest 4 (sep (map pprParendIfaceType tys))])
273
274 -------------------
275 instance Outputable IfacePredType where
276         -- Print without parens
277   ppr (IfaceIParam ip ty)  = hsep [ppr ip, dcolon, ppr ty]
278   ppr (IfaceClassP cls ts) = ppr cls <+> sep (map pprParendIfaceType ts)
279
280 instance Outputable IfaceTyCon where
281   ppr (IfaceTc ext) = ppr ext
282   ppr other_tc      = ppr (ifaceTyConName other_tc)
283
284 -------------------
285 pprIfaceContext :: IfaceContext -> SDoc
286 -- Prints "(C a, D b) =>", including the arrow
287 pprIfaceContext []    = empty
288 pprIfaceContext theta = parens (sep (punctuate comma (map ppr theta))) 
289                         <+> ptext SLIT("=>")
290   
291 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
292 \end{code}
293
294 %************************************************************************
295 %*                                                                      *
296         Conversion from Type to IfaceType
297 %*                                                                      *
298 %************************************************************************
299
300 \begin{code}
301 ----------------
302 toIfaceTvBndr tyvar   = (getOccName tyvar, tyVarKind tyvar)
303 toIfaceIdBndr ext id  = (getOccName id,    toIfaceType ext (idType id))
304 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
305
306 toIfaceBndr ext var
307   | isId var  = IfaceIdBndr (toIfaceIdBndr ext var)
308   | otherwise = IfaceTvBndr (toIfaceTvBndr var)
309
310 ---------------------
311 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
312 toIfaceType ext (TyVarTy tv)                 = IfaceTyVar (getOccName tv)
313 toIfaceType ext (AppTy t1 t2)                = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
314 toIfaceType ext (FunTy t1 t2)                = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
315 toIfaceType ext (TyConApp tc tys)            = IfaceTyConApp (mkIfaceTc ext tc) (toIfaceTypes ext tys)
316 toIfaceType ext (ForAllTy tv t)              = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
317 toIfaceType ext (PredTy st)                  = IfacePredTy (toIfacePred ext st)
318 toIfaceType ext (NoteTy (SynNote tc_app) ty) = toIfaceType ext tc_app
319 toIfaceType ext (NoteTy other_note ty)       = toIfaceType ext ty
320
321 ----------------
322 mkIfaceTc :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
323 mkIfaceTc ext tc 
324   | isTupleTyCon tc     = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
325   | nm == intTyConName  = IfaceIntTc
326   | nm == boolTyConName = IfaceBoolTc 
327   | nm == charTyConName = IfaceCharTc 
328   | nm == listTyConName = IfaceListTc 
329   | nm == parrTyConName = IfacePArrTc 
330   | otherwise           = IfaceTc (ext nm)
331   where
332     nm = getName tc
333
334 ----------------
335 toIfaceTypes ext ts = map (toIfaceType ext) ts
336
337 ----------------
338 toIfacePred ext (ClassP cls ts) = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
339 toIfacePred ext (IParam ip t)   = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
340
341 ----------------
342 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
343 toIfaceContext ext cs = map (toIfacePred ext) cs
344 \end{code}
345