Fix scoped type variables for expression type signatures
[ghc-hetmet.git] / 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, IfaceCoercion,
11
12         IfaceExtName(..), mkIfaceExtName, isLocalIfaceExtName,
13         ifaceTyConName,
14
15         -- Conversion from Type -> IfaceType
16         toIfaceType, toIfacePred, toIfaceContext, 
17         toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs, 
18         toIfaceTyCon, toIfaceTyCon_name,
19
20         -- Printing
21         pprIfaceType, pprParendIfaceType, pprIfaceContext, 
22         pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
23         tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
24
25     ) where
26
27 #include "HsVersions.h"
28
29 import TypeRep          ( TyThing(..), Type(..), PredType(..), ThetaType,
30                           unliftedTypeKindTyConName, openTypeKindTyConName,
31                           ubxTupleKindTyConName, argTypeKindTyConName,
32                           liftedTypeKindTyConName )
33 import TyCon            ( TyCon, isTupleTyCon, tyConArity, tupleTyConBoxity, tyConName )
34 import Var              ( isId, tyVarKind, idType )
35 import TysWiredIn       ( listTyConName, parrTyConName, tupleTyCon, intTyConName, charTyConName, boolTyConName )
36 import OccName          ( OccName, parenSymOcc, occNameFS )
37 import Name             ( Name, getName, getOccName, nameModule, nameOccName,
38                           wiredInNameTyThing_maybe )
39 import Module           ( Module, ModuleName )
40 import BasicTypes       ( IPName(..), Arity, Version, mapIPName, tupleParens, Boxity )
41 import Outputable
42 import FastString
43 \end{code}
44
45         
46 %************************************************************************
47 %*                                                                      *
48                 IfaceExtName
49 %*                                                                      *
50 %************************************************************************
51
52 \begin{code}
53 data IfaceExtName
54   = ExtPkg Module OccName
55         -- From an external package; no version # Also used for
56         -- wired-in things regardless of whether they are home-pkg or
57         -- not
58
59   | HomePkg ModuleName OccName Version
60         -- From another module in home package; has version #; in all
61         -- other respects, HomePkg and ExtPkg are the same. Since this
62         -- is a home package name, we use ModuleName rather than Module
63
64   | LocalTop OccName                    -- Top-level from the same module as 
65                                         -- the enclosing IfaceDecl
66
67   | LocalTopSub         -- Same as LocalTop, but for a class method or constr
68         OccName         -- Class-meth/constr name
69         OccName         -- Parent class/datatype name
70         -- LocalTopSub is written into iface files as LocalTop; the parent 
71         -- info is only used when computing version information in MkIface
72
73 isLocalIfaceExtName :: IfaceExtName -> Bool
74 isLocalIfaceExtName (LocalTop _)      = True
75 isLocalIfaceExtName (LocalTopSub _ _) = True
76 isLocalIfaceExtName other             = False
77
78 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
79         -- Local helper for wired-in names
80
81 ifaceExtOcc :: IfaceExtName -> OccName
82 ifaceExtOcc (ExtPkg _ occ)      = occ
83 ifaceExtOcc (HomePkg _ occ _)   = occ
84 ifaceExtOcc (LocalTop occ)      = occ
85 ifaceExtOcc (LocalTopSub occ _) = occ
86 \end{code}
87
88
89 %************************************************************************
90 %*                                                                      *
91                 Local (nested) binders
92 %*                                                                      *
93 %************************************************************************
94
95 \begin{code}
96 data IfaceBndr          -- Local (non-top-level) binders
97   = IfaceIdBndr {-# UNPACK #-} !IfaceIdBndr
98   | IfaceTvBndr {-# UNPACK #-} !IfaceTvBndr
99
100 type IfaceIdBndr  = (FastString, IfaceType)
101 type IfaceTvBndr  = (FastString, IfaceKind)
102
103 -------------------------------
104 type IfaceKind = IfaceType                      -- Re-use the Kind type, but no KindVars in it
105
106 type IfaceCoercion = IfaceType
107
108 data IfaceType
109   = IfaceTyVar    FastString                    -- Type variable only, not tycon
110   | IfaceAppTy    IfaceType IfaceType
111   | IfaceForAllTy IfaceTvBndr IfaceType
112   | IfacePredTy   IfacePredType
113   | IfaceTyConApp IfaceTyCon [IfaceType]        -- Not necessarily saturated
114                                                 -- Includes newtypes, synonyms, tuples
115   | IfaceFunTy  IfaceType IfaceType
116
117 data IfacePredType      -- NewTypes are handled as ordinary TyConApps
118   = IfaceClassP IfaceExtName [IfaceType]
119   | IfaceIParam (IPName OccName) IfaceType
120   | IfaceEqPred IfaceType IfaceType
121
122 type IfaceContext = [IfacePredType]
123
124 -- NB: If you add a data constructor, remember to add a case to
125 --     IfaceSyn.eqIfTc!
126 data IfaceTyCon         -- Abbreviations for common tycons with known names
127   = IfaceTc IfaceExtName        -- The common case
128   | IfaceIntTc | IfaceBoolTc | IfaceCharTc
129   | IfaceListTc | IfacePArrTc
130   | IfaceTupTc Boxity Arity 
131   | IfaceLiftedTypeKindTc | IfaceOpenTypeKindTc | IfaceUnliftedTypeKindTc
132   | IfaceUbxTupleKindTc | IfaceArgTypeKindTc 
133
134 ifaceTyConName :: IfaceTyCon -> Name    -- Works for all except IfaceTc
135 ifaceTyConName IfaceIntTc         = intTyConName
136 ifaceTyConName IfaceBoolTc        = boolTyConName
137 ifaceTyConName IfaceCharTc        = charTyConName
138 ifaceTyConName IfaceListTc        = listTyConName
139 ifaceTyConName IfacePArrTc        = parrTyConName
140 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
141 ifaceTyConName IfaceLiftedTypeKindTc   = liftedTypeKindTyConName
142 ifaceTyConName IfaceOpenTypeKindTc     = openTypeKindTyConName
143 ifaceTyConName IfaceUnliftedTypeKindTc = unliftedTypeKindTyConName
144 ifaceTyConName IfaceUbxTupleKindTc     = ubxTupleKindTyConName
145 ifaceTyConName IfaceArgTypeKindTc      = argTypeKindTyConName
146 ifaceTyConName (IfaceTc ext)      = pprPanic "ifaceTyConName" (ppr ext)
147
148
149 \end{code}
150
151
152 %************************************************************************
153 %*                                                                      *
154                 Functions over IFaceTypes
155 %*                                                                      *
156 %************************************************************************
157
158
159 \begin{code}
160 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
161 -- Mainly for printing purposes
162 splitIfaceSigmaTy ty
163   = (tvs,theta,tau)
164   where
165     (tvs, rho)   = split_foralls ty
166     (theta, tau) = split_rho rho
167
168     split_foralls (IfaceForAllTy tv ty) 
169         = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
170     split_foralls rho = ([], rho)
171
172     split_rho (IfaceFunTy (IfacePredTy st) ty) 
173         = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
174     split_rho tau = ([], tau)
175 \end{code}
176
177 %************************************************************************
178 %*                                                                      *
179                 Pretty-printing
180 %*                                                                      *
181 %************************************************************************
182
183 Precedence
184 ~~~~~~~~~~
185 @ppr_ty@ takes an @Int@ that is the precedence of the context.
186 The precedence levels are:
187 \begin{description}
188 \item[tOP_PREC]   No parens required.
189 \item[fUN_PREC]   Left hand argument of a function arrow.
190 \item[tYCON_PREC] Argument of a type constructor.
191 \end{description}
192
193 \begin{code}
194 tOP_PREC    = (0 :: Int)  -- type   in ParseIface.y
195 fUN_PREC    = (1 :: Int)  -- btype  in ParseIface.y
196 tYCON_PREC  = (2 :: Int)  -- atype  in ParseIface.y
197
198 noParens :: SDoc -> SDoc
199 noParens pp = pp
200
201 maybeParen ctxt_prec inner_prec pretty
202   | ctxt_prec < inner_prec = pretty
203   | otherwise              = parens pretty
204 \end{code}
205
206
207 ----------------------------- Printing binders ------------------------------------
208
209 \begin{code}
210 -- These instances are used only when printing for the user, either when
211 -- debugging, or in GHCi when printing the results of a :info command
212 instance Outputable IfaceExtName where
213     ppr (ExtPkg mod occ)       = ppr mod <> dot <> ppr occ
214     ppr (HomePkg mod occ vers) = ppr mod <> dot <> ppr occ <> braces (ppr vers)
215     ppr (LocalTop occ)         = ppr occ        -- Do we want to distinguish these 
216     ppr (LocalTopSub occ _)    = ppr occ        -- from an ordinary occurrence?
217 -- No need to worry about printing unqualified becuase that was handled
218 -- in the transiation to IfaceSyn 
219
220 instance Outputable IfaceBndr where
221     ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
222     ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
223
224 pprIfaceBndrs :: [IfaceBndr] -> SDoc
225 pprIfaceBndrs bs = sep (map ppr bs)
226
227 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
228
229 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
230 pprIfaceTvBndr (tv, IfaceTyConApp IfaceLiftedTypeKindTc []) 
231   = ppr tv
232 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
233 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
234 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
235 \end{code}
236
237 ----------------------------- Printing IfaceType ------------------------------------
238
239 \begin{code}
240 ---------------------------------
241 instance Outputable IfaceType where
242   ppr ty = pprIfaceTypeForUser ty
243
244 pprIfaceTypeForUser ::IfaceType -> SDoc
245 -- Drop top-level for-alls; if that's not what you want, use pprIfaceType dire
246 pprIfaceTypeForUser ty
247   = pprIfaceForAllPart [] theta (pprIfaceType tau)
248  where          
249     (_tvs, theta, tau) = splitIfaceSigmaTy ty
250
251 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
252 pprIfaceType       = ppr_ty tOP_PREC
253 pprParendIfaceType = ppr_ty tYCON_PREC
254
255
256 ppr_ty :: Int -> IfaceType -> SDoc
257 ppr_ty ctxt_prec (IfaceTyVar tyvar)     = ppr tyvar
258 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
259 ppr_ty ctxt_prec (IfacePredTy st)       = ppr st
260
261         -- Function types
262 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
263   = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
264     maybeParen ctxt_prec fUN_PREC $
265     sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
266   where
267     ppr_fun_tail (IfaceFunTy ty1 ty2) 
268       = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
269     ppr_fun_tail other_ty
270       = [arrow <+> pprIfaceType other_ty]
271
272 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
273   = maybeParen ctxt_prec tYCON_PREC $
274     ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
275
276 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
277   = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
278  where          
279     (tvs, theta, tau) = splitIfaceSigmaTy ty
280     
281 -------------------
282 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
283 pprIfaceForAllPart tvs ctxt doc 
284   = sep [ppr_tvs, pprIfaceContext ctxt, doc]
285   where
286     ppr_tvs | null tvs  = empty
287             | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
288
289 -------------------
290 ppr_tc_app ctxt_prec tc          []   = ppr_tc tc
291 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets   (pprIfaceType ty)
292 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
293 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
294   | arity == length tys 
295   = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
296 ppr_tc_app ctxt_prec tc tys 
297   = maybeParen ctxt_prec tYCON_PREC 
298                (sep [ppr_tc tc, nest 4 (sep (map pprParendIfaceType tys))])
299
300 ppr_tc :: IfaceTyCon -> SDoc
301 -- Wrap infix type constructors in parens
302 ppr_tc tc@(IfaceTc ext_nm) = parenSymOcc (ifaceExtOcc ext_nm) (ppr tc)
303 ppr_tc tc                  = ppr tc
304
305 -------------------
306 instance Outputable IfacePredType where
307         -- Print without parens
308   ppr (IfaceEqPred ty1 ty2)= hsep [ppr ty1, ptext SLIT(":=:"), ppr ty2]
309   ppr (IfaceIParam ip ty)  = hsep [ppr ip, dcolon, ppr ty]
310   ppr (IfaceClassP cls ts) = parenSymOcc (ifaceExtOcc cls) (ppr cls)
311                              <+> sep (map pprParendIfaceType ts)
312
313 instance Outputable IfaceTyCon where
314   ppr (IfaceTc ext) = ppr ext
315   ppr other_tc      = ppr (ifaceTyConName other_tc)
316
317 -------------------
318 pprIfaceContext :: IfaceContext -> SDoc
319 -- Prints "(C a, D b) =>", including the arrow
320 pprIfaceContext []     = empty
321 pprIfaceContext theta = ppr_preds theta <+> ptext SLIT("=>")
322
323 ppr_preds [pred] = ppr pred     -- No parens
324 ppr_preds preds  = parens (sep (punctuate comma (map ppr preds))) 
325                          
326 -------------------
327 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
328 \end{code}
329
330 %************************************************************************
331 %*                                                                      *
332         Conversion from Type to IfaceType
333 %*                                                                      *
334 %************************************************************************
335
336 \begin{code}
337 ----------------
338 toIfaceTvBndr tyvar   = (occNameFS (getOccName tyvar), toIfaceKind (tyVarKind tyvar))
339 toIfaceIdBndr ext id  = (occNameFS (getOccName id),    toIfaceType ext (idType id))
340 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
341
342 toIfaceBndr ext var
343   | isId var  = IfaceIdBndr (toIfaceIdBndr ext var)
344   | otherwise = IfaceTvBndr (toIfaceTvBndr var)
345
346 -- we had better not have to use ext for kinds
347 toIfaceKind = toIfaceType (\name -> pprPanic "toIfaceKind ext used on:" (ppr name))
348
349 ---------------------
350 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
351 -- Synonyms are retained in the interface type
352 toIfaceType ext (TyVarTy tv)                 = IfaceTyVar (occNameFS (getOccName tv))
353 toIfaceType ext (AppTy t1 t2)                = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
354 toIfaceType ext (FunTy t1 t2)                = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
355 toIfaceType ext (TyConApp tc tys)            = IfaceTyConApp (toIfaceTyCon ext tc) (toIfaceTypes ext tys)
356 toIfaceType ext (ForAllTy tv t)              = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
357 toIfaceType ext (PredTy st)                  = IfacePredTy (toIfacePred ext st)
358 toIfaceType ext (NoteTy other_note ty)       = toIfaceType ext ty
359
360 ----------------
361 -- A little bit of (perhaps optional) trickiness here.  When
362 -- compiling Data.Tuple, the tycons are not TupleTyCons, although
363 -- they have a wired-in name.  But we'd like to dump them into the Iface
364 -- as a tuple tycon, to save lookups when reading the interface
365 -- Hence a tuple tycon may 'miss' in toIfaceTyCon, but then
366 -- toIfaceTyCon_name will still catch it.
367
368 toIfaceTyCon :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
369 toIfaceTyCon ext tc 
370   | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
371   | otherwise       = toIfaceTyCon_name ext (tyConName tc)
372
373 toIfaceTyCon_name :: (Name -> IfaceExtName) -> Name -> IfaceTyCon
374 toIfaceTyCon_name ext nm
375   | Just (ATyCon tc) <- wiredInNameTyThing_maybe nm
376   = toIfaceWiredInTyCon ext tc nm
377   | otherwise
378   = IfaceTc (ext nm)
379
380 toIfaceWiredInTyCon :: (Name -> IfaceExtName) -> TyCon -> Name -> IfaceTyCon
381 toIfaceWiredInTyCon ext tc nm
382   | isTupleTyCon tc                 =  IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
383   | nm == intTyConName              = IfaceIntTc
384   | nm == boolTyConName             = IfaceBoolTc 
385   | nm == charTyConName             = IfaceCharTc 
386   | nm == listTyConName             = IfaceListTc 
387   | nm == parrTyConName             = IfacePArrTc 
388   | nm == liftedTypeKindTyConName   = IfaceLiftedTypeKindTc
389   | nm == unliftedTypeKindTyConName = IfaceUnliftedTypeKindTc
390   | nm == openTypeKindTyConName     = IfaceOpenTypeKindTc
391   | nm == argTypeKindTyConName      = IfaceArgTypeKindTc
392   | nm == ubxTupleKindTyConName     = IfaceUbxTupleKindTc
393   | otherwise                       = IfaceTc (ext nm)
394
395 ----------------
396 toIfaceTypes ext ts = map (toIfaceType ext) ts
397
398 ----------------
399 toIfacePred ext (ClassP cls ts)  = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
400 toIfacePred ext (IParam ip t)    = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
401 toIfacePred ext (EqPred ty1 ty2) = IfaceEqPred (toIfaceType ext ty1) (toIfaceType ext ty2)
402
403 ----------------
404 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
405 toIfaceContext ext cs = map (toIfacePred ext) cs
406 \end{code}
407