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