Merge branch 'master' of http://darcs.haskell.org/ghc
[ghc-hetmet.git] / compiler / types / TypeRep.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 %
5 \section[TypeRep]{Type - friends' interface}
6
7 \begin{code}
8 -- We expose the relevant stuff from this module via the Type module
9 {-# OPTIONS_HADDOCK hide #-}
10 {-# LANGUAGE DeriveDataTypeable #-}
11
12 module TypeRep (
13         TyThing(..), 
14         Type(..),
15         PredType(..),                   -- to friends
16         
17         Kind, ThetaType,                -- Synonyms
18
19         funTyCon, funTyConName,
20
21         -- Pretty-printing
22         pprType, pprParendType, pprTypeApp,
23         pprTyThing, pprTyThingCategory, 
24         pprPred, pprEqPred, pprTheta, pprForAll, pprThetaArrow, pprClassPred,
25
26         -- Kinds
27         liftedTypeKind, unliftedTypeKind, openTypeKind,
28         argTypeKind, ubxTupleKind,
29         isLiftedTypeKindCon, isLiftedTypeKind,
30         mkArrowKind, mkArrowKinds, isCoercionKind,
31         coVarPred,
32
33         -- Kind constructors...
34         liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
35         argTypeKindTyCon, ubxTupleKindTyCon,
36
37         -- And their names
38         unliftedTypeKindTyConName, openTypeKindTyConName,
39         ubxTupleKindTyConName, argTypeKindTyConName,
40         liftedTypeKindTyConName,
41
42         -- Super Kinds
43         tySuperKind, coSuperKind,
44         isTySuperKind, isCoSuperKind,
45         tySuperKindTyCon, coSuperKindTyCon,
46         
47         pprKind, pprParendKind
48     ) where
49
50 #include "HsVersions.h"
51
52 import {-# SOURCE #-} DataCon( DataCon, dataConName )
53
54 -- friends:
55 import Var
56 import Name
57 import BasicTypes
58 import TyCon
59 import Class
60
61 -- others
62 import PrelNames
63 import Outputable
64 import FastString
65
66 -- libraries
67 import Data.Data hiding ( TyCon )
68 \end{code}
69
70         ----------------------
71         A note about newtypes
72         ----------------------
73
74 Consider
75         newtype N = MkN Int
76
77 Then we want N to be represented as an Int, and that's what we arrange.
78 The front end of the compiler [TcType.lhs] treats N as opaque, 
79 the back end treats it as transparent [Type.lhs].
80
81 There's a bit of a problem with recursive newtypes
82         newtype P = MkP P
83         newtype Q = MkQ (Q->Q)
84
85 Here the 'implicit expansion' we get from treating P and Q as transparent
86 would give rise to infinite types, which in turn makes eqType diverge.
87 Similarly splitForAllTys and splitFunTys can get into a loop.  
88
89 Solution: 
90
91 * Newtypes are always represented using TyConApp.
92
93 * For non-recursive newtypes, P, treat P just like a type synonym after 
94   type-checking is done; i.e. it's opaque during type checking (functions
95   from TcType) but transparent afterwards (functions from Type).  
96   "Treat P as a type synonym" means "all functions expand NewTcApps 
97   on the fly".
98
99   Applications of the data constructor P simply vanish:
100         P x = x
101   
102
103 * For recursive newtypes Q, treat the Q and its representation as 
104   distinct right through the compiler.  Applications of the data consructor
105   use a coerce:
106         Q = \(x::Q->Q). coerce Q x
107   They are rare, so who cares if they are a tiny bit less efficient.
108
109 The typechecker (TcTyDecls) identifies enough type construtors as 'recursive'
110 to cut all loops.  The other members of the loop may be marked 'non-recursive'.
111
112
113 %************************************************************************
114 %*                                                                      *
115 \subsection{The data type}
116 %*                                                                      *
117 %************************************************************************
118
119
120 \begin{code}
121 -- | The key representation of types within the compiler
122 data Type
123   = TyVarTy TyVar       -- ^ Vanilla type variable
124
125   | AppTy
126         Type
127         Type            -- ^ Type application to something other than a 'TyCon'. Parameters:
128                         --
129                         --  1) Function: must /not/ be a 'TyConApp', must be another 'AppTy', or 'TyVarTy'
130                         --
131                         --  2) Argument type
132
133   | TyConApp
134         TyCon
135         [Type]          -- ^ Application of a 'TyCon', including newtypes /and/ synonyms.
136                         -- Invariant: saturated appliations of 'FunTyCon' must
137                         -- use 'FunTy' and saturated synonyms must use their own
138                         -- constructors. However, /unsaturated/ 'FunTyCon's do appear as 'TyConApp's.
139                         -- Parameters:
140                         --
141                         -- 1) Type constructor being applied to.
142                         --
143                         -- 2) Type arguments. Might not have enough type arguments here to saturate the constructor.
144                         -- Even type synonyms are not necessarily saturated; for example unsaturated type synonyms
145                         -- can appear as the right hand side of a type synonym.
146
147   | FunTy
148         Type
149         Type            -- ^ Special case of 'TyConApp': @TyConApp FunTyCon [t1, t2]@
150
151   | ForAllTy
152         TyVar
153         Type            -- ^ A polymorphic type
154
155   | PredTy
156         PredType        -- ^ The type of evidence for a type predictate.
157                         -- Note that a @PredTy (EqPred _ _)@ can appear only as the kind
158                         -- of a coercion variable; never as the argument or result
159                         -- of a 'FunTy' (unlike the 'PredType' constructors 'ClassP' or 'IParam')
160                         
161                         -- See Note [PredTy], and Note [Equality predicates]
162   deriving (Data, Typeable)
163
164 -- | The key type representing kinds in the compiler.
165 -- Invariant: a kind is always in one of these forms:
166 --
167 -- > FunTy k1 k2
168 -- > TyConApp PrimTyCon [...]
169 -- > TyVar kv   -- (during inference only)
170 -- > ForAll ... -- (for top-level coercions)
171 type Kind = Type
172
173 -- | "Super kinds", used to help encode 'Kind's as types.
174 -- Invariant: a super kind is always of this form:
175 --
176 -- > TyConApp SuperKindTyCon ...
177 type SuperKind = Type
178 \end{code}
179
180 -------------------------------------
181                 Note [PredTy]
182
183 \begin{code}
184 -- | A type of the form @PredTy p@ represents a value whose type is
185 -- the Haskell predicate @p@, where a predicate is what occurs before 
186 -- the @=>@ in a Haskell type.
187 -- It can be expanded into its representation, but: 
188 --
189 -- * The type checker must treat it as opaque
190 --
191 -- * The rest of the compiler treats it as transparent
192 --
193 -- Consider these examples:
194 --
195 -- > f :: (Eq a) => a -> Int
196 -- > g :: (?x :: Int -> Int) => a -> Int
197 -- > h :: (r\l) => {r} => {l::Int | r}
198 --
199 -- Here the @Eq a@ and @?x :: Int -> Int@ and @r\l@ are all called \"predicates\"
200 data PredType 
201   = ClassP Class [Type]         -- ^ Class predicate e.g. @Eq a@
202   | IParam (IPName Name) Type   -- ^ Implicit parameter e.g. @?x :: Int@
203   | EqPred Type Type            -- ^ Equality predicate e.g @ty1 ~ ty2@
204   deriving (Data, Typeable)
205
206 -- | A collection of 'PredType's
207 type ThetaType = [PredType]
208 \end{code}
209
210 (We don't support TREX records yet, but the setup is designed
211 to expand to allow them.)
212
213 A Haskell qualified type, such as that for f,g,h above, is
214 represented using 
215         * a FunTy for the double arrow
216         * with a PredTy as the function argument
217
218 The predicate really does turn into a real extra argument to the
219 function.  If the argument has type (PredTy p) then the predicate p is
220 represented by evidence (a dictionary, for example, of type (predRepTy p).
221
222 Note [Equality predicates]
223 ~~~~~~~~~~~~~~~~~~~~~~~~~~
224         forall a b. (a ~ S b) => a -> b
225 could be represented by
226         ForAllTy a (ForAllTy b (FunTy (PredTy (EqPred a (S b))) ...))
227 OR
228         ForAllTy a (ForAllTy b (ForAllTy (c::PredTy (EqPred a (S b))) ...))
229
230 The latter is what we do.  (Unlike for class and implicit parameter
231 constraints, which do use FunTy.)
232
233 Reason:
234         * FunTy is always a *value* function
235         * ForAllTy is discarded at runtime
236
237 We often need to make a "wildcard" (c::PredTy..).  We always use the same
238 name (wildCoVarName), since it's not mentioned.
239
240
241 %************************************************************************
242 %*                                                                      *
243                         TyThing
244 %*                                                                      *
245 %************************************************************************
246
247 Despite the fact that DataCon has to be imported via a hi-boot route, 
248 this module seems the right place for TyThing, because it's needed for
249 funTyCon and all the types in TysPrim.
250
251 \begin{code}
252 -- | A typecheckable-thing, essentially anything that has a name
253 data TyThing = AnId     Id
254              | ADataCon DataCon
255              | ATyCon   TyCon
256              | AClass   Class
257
258 instance Outputable TyThing where 
259   ppr = pprTyThing
260
261 pprTyThing :: TyThing -> SDoc
262 pprTyThing thing = pprTyThingCategory thing <+> quotes (ppr (getName thing))
263
264 pprTyThingCategory :: TyThing -> SDoc
265 pprTyThingCategory (ATyCon _)   = ptext (sLit "Type constructor")
266 pprTyThingCategory (AClass _)   = ptext (sLit "Class")
267 pprTyThingCategory (AnId   _)   = ptext (sLit "Identifier")
268 pprTyThingCategory (ADataCon _) = ptext (sLit "Data constructor")
269
270 instance NamedThing TyThing where       -- Can't put this with the type
271   getName (AnId id)     = getName id    -- decl, because the DataCon instance
272   getName (ATyCon tc)   = getName tc    -- isn't visible there
273   getName (AClass cl)   = getName cl
274   getName (ADataCon dc) = dataConName dc
275 \end{code}
276
277
278 %************************************************************************
279 %*                                                                      *
280                 Wired-in type constructors
281 %*                                                                      *
282 %************************************************************************
283
284 We define a few wired-in type constructors here to avoid module knots
285
286 \begin{code}
287 --------------------------
288 -- First the TyCons...
289
290 -- | See "Type#kind_subtyping" for details of the distinction between the 'Kind' 'TyCon's
291 funTyCon, tySuperKindTyCon, coSuperKindTyCon, liftedTypeKindTyCon,
292       openTypeKindTyCon, unliftedTypeKindTyCon,
293       ubxTupleKindTyCon, argTypeKindTyCon
294    :: TyCon
295 funTyConName, tySuperKindTyConName, coSuperKindTyConName, liftedTypeKindTyConName,
296       openTypeKindTyConName, unliftedTypeKindTyConName,
297       ubxTupleKindTyConName, argTypeKindTyConName
298    :: Name
299
300 funTyCon = mkFunTyCon funTyConName (mkArrowKinds [argTypeKind, openTypeKind] liftedTypeKind)
301         -- You might think that (->) should have type (?? -> ? -> *), and you'd be right
302         -- But if we do that we get kind errors when saying
303         --      instance Control.Arrow (->)
304         -- becuase the expected kind is (*->*->*).  The trouble is that the
305         -- expected/actual stuff in the unifier does not go contra-variant, whereas
306         -- the kind sub-typing does.  Sigh.  It really only matters if you use (->) in
307         -- a prefix way, thus:  (->) Int# Int#.  And this is unusual.
308
309
310 tySuperKindTyCon     = mkSuperKindTyCon tySuperKindTyConName
311 coSuperKindTyCon     = mkSuperKindTyCon coSuperKindTyConName
312
313 liftedTypeKindTyCon   = mkKindTyCon liftedTypeKindTyConName   tySuperKind
314 openTypeKindTyCon     = mkKindTyCon openTypeKindTyConName     tySuperKind
315 unliftedTypeKindTyCon = mkKindTyCon unliftedTypeKindTyConName tySuperKind
316 ubxTupleKindTyCon     = mkKindTyCon ubxTupleKindTyConName     tySuperKind
317 argTypeKindTyCon      = mkKindTyCon argTypeKindTyConName      tySuperKind
318
319 --------------------------
320 -- ... and now their names
321
322 tySuperKindTyConName      = mkPrimTyConName (fsLit "BOX") tySuperKindTyConKey tySuperKindTyCon
323 coSuperKindTyConName      = mkPrimTyConName (fsLit "COERCION") coSuperKindTyConKey coSuperKindTyCon
324 liftedTypeKindTyConName   = mkPrimTyConName (fsLit "*") liftedTypeKindTyConKey liftedTypeKindTyCon
325 openTypeKindTyConName     = mkPrimTyConName (fsLit "?") openTypeKindTyConKey openTypeKindTyCon
326 unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon
327 ubxTupleKindTyConName     = mkPrimTyConName (fsLit "(#)") ubxTupleKindTyConKey ubxTupleKindTyCon
328 argTypeKindTyConName      = mkPrimTyConName (fsLit "??") argTypeKindTyConKey argTypeKindTyCon
329 funTyConName              = mkPrimTyConName (fsLit "(->)") funTyConKey funTyCon
330
331 mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
332 mkPrimTyConName occ key tycon = mkWiredInName gHC_PRIM (mkTcOccFS occ) 
333                                               key 
334                                               (ATyCon tycon)
335                                               BuiltInSyntax
336         -- All of the super kinds and kinds are defined in Prim and use BuiltInSyntax,
337         -- because they are never in scope in the source
338
339 ------------------
340 -- We also need Kinds and SuperKinds, locally and in TyCon
341
342 kindTyConType :: TyCon -> Type
343 kindTyConType kind = TyConApp kind []
344
345 -- | See "Type#kind_subtyping" for details of the distinction between these 'Kind's
346 liftedTypeKind, unliftedTypeKind, openTypeKind, argTypeKind, ubxTupleKind :: Kind
347
348 liftedTypeKind   = kindTyConType liftedTypeKindTyCon
349 unliftedTypeKind = kindTyConType unliftedTypeKindTyCon
350 openTypeKind     = kindTyConType openTypeKindTyCon
351 argTypeKind      = kindTyConType argTypeKindTyCon
352 ubxTupleKind     = kindTyConType ubxTupleKindTyCon
353
354 -- | Given two kinds @k1@ and @k2@, creates the 'Kind' @k1 -> k2@
355 mkArrowKind :: Kind -> Kind -> Kind
356 mkArrowKind k1 k2 = FunTy k1 k2
357
358 -- | Iterated application of 'mkArrowKind'
359 mkArrowKinds :: [Kind] -> Kind -> Kind
360 mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds
361
362 tySuperKind, coSuperKind :: SuperKind
363 tySuperKind = kindTyConType tySuperKindTyCon 
364 coSuperKind = kindTyConType coSuperKindTyCon 
365
366 isTySuperKind :: SuperKind -> Bool
367 isTySuperKind (TyConApp kc []) = kc `hasKey` tySuperKindTyConKey
368 isTySuperKind _                = False
369
370 isCoSuperKind :: SuperKind -> Bool
371 isCoSuperKind (TyConApp kc []) = kc `hasKey` coSuperKindTyConKey
372 isCoSuperKind _                = False
373
374 -------------------
375 -- Lastly we need a few functions on Kinds
376
377 isLiftedTypeKindCon :: TyCon -> Bool
378 isLiftedTypeKindCon tc    = tc `hasKey` liftedTypeKindTyConKey
379
380 isLiftedTypeKind :: Kind -> Bool
381 isLiftedTypeKind (TyConApp tc []) = isLiftedTypeKindCon tc
382 isLiftedTypeKind _                = False
383
384 isCoercionKind :: Kind -> Bool
385 -- All coercions are of form (ty1 ~ ty2)
386 -- This function is here rather than in Coercion, 
387 -- because it's used in a knot-tied way to enforce invariants in Var
388 isCoercionKind (PredTy (EqPred {})) = True
389 isCoercionKind _                    = False
390
391 coVarPred :: CoVar -> PredType
392 coVarPred tv
393   = ASSERT( isCoVar tv )
394     case tyVarKind tv of
395         PredTy eq -> eq
396         other     -> pprPanic "coVarPred" (ppr tv $$ ppr other)
397 \end{code}
398
399
400
401 %************************************************************************
402 %*                                                                      *
403 \subsection{The external interface}
404 %*                                                                      *
405 %************************************************************************
406
407 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
408 defined to use this.  @pprParendType@ is the same, except it puts
409 parens around the type, except for the atomic cases.  @pprParendType@
410 works just by setting the initial context precedence very high.
411
412 \begin{code}
413 data Prec = TopPrec     -- No parens
414           | FunPrec     -- Function args; no parens for tycon apps
415           | TyConPrec   -- Tycon args; no parens for atomic
416           deriving( Eq, Ord )
417
418 maybeParen :: Prec -> Prec -> SDoc -> SDoc
419 maybeParen ctxt_prec inner_prec pretty
420   | ctxt_prec < inner_prec = pretty
421   | otherwise              = parens pretty
422
423 ------------------
424 pprType, pprParendType :: Type -> SDoc
425 pprType       ty = ppr_type TopPrec   ty
426 pprParendType ty = ppr_type TyConPrec ty
427
428 pprTypeApp :: NamedThing a => a -> [Type] -> SDoc
429 -- The first arg is the tycon, or sometimes class
430 -- Print infix if the tycon/class looks like an operator
431 pprTypeApp tc tys = ppr_type_app TopPrec (getName tc) tys
432
433 ------------------
434 pprPred :: PredType -> SDoc
435 pprPred (ClassP cls tys) = pprClassPred cls tys
436 pprPred (IParam ip ty)   = ppr ip <> dcolon <> pprType ty
437 pprPred (EqPred ty1 ty2) = pprEqPred (ty1,ty2)
438
439 pprEqPred :: (Type,Type) -> SDoc
440 pprEqPred (ty1,ty2) = sep [ ppr_type FunPrec ty1
441                           , nest 2 (ptext (sLit "~"))
442                           , ppr_type FunPrec ty2]
443                                -- Precedence looks like (->) so that we get
444                                --    Maybe a ~ Bool
445                                --    (a->a) ~ Bool
446                                -- Note parens on the latter!
447
448 pprClassPred :: Class -> [Type] -> SDoc
449 pprClassPred clas tys = ppr_type_app TopPrec (getName clas) tys
450
451 pprTheta :: ThetaType -> SDoc
452 -- pprTheta [pred] = pprPred pred        -- I'm in two minds about this
453 pprTheta theta  = parens (sep (punctuate comma (map pprPred theta)))
454
455 pprThetaArrow :: ThetaType -> SDoc
456 pprThetaArrow []     = empty
457 pprThetaArrow [pred] 
458   | noParenPred pred = pprPred pred <+> darrow
459 pprThetaArrow preds  = parens (sep (punctuate comma (map pprPred preds))) <+> darrow
460
461 noParenPred :: PredType -> Bool
462 -- A predicate that can appear without parens before a "=>"
463 --       C a => a -> a
464 --       a~b => a -> b
465 -- But   (?x::Int) => Int -> Int
466 noParenPred (ClassP {}) = True
467 noParenPred (EqPred {}) = True
468 noParenPred (IParam {}) = False
469
470 ------------------
471 instance Outputable Type where
472     ppr ty = pprType ty
473
474 instance Outputable PredType where
475     ppr = pprPred
476
477 instance Outputable name => OutputableBndr (IPName name) where
478     pprBndr _ n = ppr n -- Simple for now
479
480 ------------------
481         -- OK, here's the main printer
482
483 pprKind, pprParendKind :: Kind -> SDoc
484 pprKind = pprType
485 pprParendKind = pprParendType
486
487 ppr_type :: Prec -> Type -> SDoc
488 ppr_type _ (TyVarTy tv)       = ppr_tvar tv
489 ppr_type p (PredTy pred)      = maybeParen p TyConPrec $
490                                 ifPprDebug (ptext (sLit "<pred>")) <> (ppr pred)
491 ppr_type p (TyConApp tc tys)  = ppr_tc_app p tc tys
492
493 ppr_type p (AppTy t1 t2) = maybeParen p TyConPrec $
494                            pprType t1 <+> ppr_type TyConPrec t2
495
496 ppr_type p ty@(ForAllTy _ _)       = ppr_forall_type p ty
497 ppr_type p ty@(FunTy (PredTy _) _) = ppr_forall_type p ty
498
499 ppr_type p (FunTy ty1 ty2)
500   = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
501     maybeParen p FunPrec $
502     sep (ppr_type FunPrec ty1 : ppr_fun_tail ty2)
503   where
504     ppr_fun_tail (FunTy ty1 ty2) 
505       | not (is_pred ty1) = (arrow <+> ppr_type FunPrec ty1) : ppr_fun_tail ty2
506     ppr_fun_tail other_ty = [arrow <+> pprType other_ty]
507     is_pred (PredTy {}) = True
508     is_pred _           = False
509
510 ppr_forall_type :: Prec -> Type -> SDoc
511 ppr_forall_type p ty
512   = maybeParen p FunPrec $
513     sep [pprForAll tvs, pprThetaArrow ctxt, pprType tau]
514   where
515     (tvs,  rho) = split1 [] ty
516     (ctxt, tau) = split2 [] rho
517
518     -- We need to be extra careful here as equality constraints will occur as
519     -- type variables with an equality kind.  So, while collecting quantified
520     -- variables, we separate the coercion variables out and turn them into
521     -- equality predicates.
522     split1 tvs (ForAllTy tv ty) 
523       | not (isCoVar tv)     = split1 (tv:tvs) ty
524     split1 tvs ty            = (reverse tvs, ty)
525  
526     split2 ps (PredTy p `FunTy` ty) = split2 (p:ps) ty
527     split2 ps (ForAllTy tv ty) 
528         | isCoVar tv                = split2 (coVarPred tv : ps) ty
529     split2 ps ty                    = (reverse ps, ty)
530
531 ppr_tc_app :: Prec -> TyCon -> [Type] -> SDoc
532 ppr_tc_app _ tc []
533   = ppr_tc tc
534 ppr_tc_app _ tc [ty]
535   | tc `hasKey` listTyConKey = brackets (pprType ty)
536   | tc `hasKey` parrTyConKey = ptext (sLit "[:") <> pprType ty <> ptext (sLit ":]")
537   | tc `hasKey` liftedTypeKindTyConKey   = ptext (sLit "*")
538   | tc `hasKey` unliftedTypeKindTyConKey = ptext (sLit "#")
539   | tc `hasKey` openTypeKindTyConKey     = ptext (sLit "(?)")
540   | tc `hasKey` ubxTupleKindTyConKey     = ptext (sLit "(#)")
541   | tc `hasKey` argTypeKindTyConKey      = ptext (sLit "??")
542
543 ppr_tc_app p tc tys
544   | isTupleTyCon tc && tyConArity tc == length tys
545   = tupleParens (tupleTyConBoxity tc) (sep (punctuate comma (map pprType tys)))
546   | otherwise
547   = ppr_type_app p (getName tc) tys
548
549 ppr_type_app :: Prec -> Name -> [Type] -> SDoc
550 -- Used for classes as well as types; that's why it's separate from ppr_tc_app
551 ppr_type_app p tc tys
552   | is_sym_occ          -- Print infix if possible
553   , [ty1,ty2] <- tys    -- We know nothing of precedence though
554   = maybeParen p FunPrec (sep [ppr_type FunPrec ty1, 
555                                pprInfixVar True (ppr tc) <+> ppr_type FunPrec ty2])
556   | otherwise
557   = maybeParen p TyConPrec (hang (pprPrefixVar is_sym_occ (ppr tc))
558                                2 (sep (map pprParendType tys)))
559   where
560     is_sym_occ = isSymOcc (getOccName tc)
561
562 ppr_tc :: TyCon -> SDoc -- No brackets for SymOcc
563 ppr_tc tc 
564   = pp_nt_debug <> ppr tc
565   where
566    pp_nt_debug | isNewTyCon tc = ifPprDebug (if isRecursiveTyCon tc 
567                                              then ptext (sLit "<recnt>")
568                                              else ptext (sLit "<nt>"))
569                | otherwise     = empty
570
571 ppr_tvar :: TyVar -> SDoc
572 ppr_tvar tv  -- Note [Infix type variables]
573   | isSymOcc (getOccName tv)  = parens (ppr tv)
574   | otherwise                 = ppr tv
575
576 -------------------
577 pprForAll :: [TyVar] -> SDoc
578 pprForAll []  = empty
579 pprForAll tvs = ptext (sLit "forall") <+> sep (map pprTvBndr tvs) <> dot
580
581 pprTvBndr :: TyVar -> SDoc
582 pprTvBndr tv | isLiftedTypeKind kind = ppr_tvar tv
583              | otherwise             = parens (ppr_tvar tv <+> dcolon <+> pprKind kind)
584              where
585                kind = tyVarKind tv
586 \end{code}
587
588 Note [Infix type variables]
589 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
590 With TypeOperators you can say
591
592    f :: (a ~> b) -> b
593
594 and the (~>) is considered a type variable.  However, the type
595 pretty-printer in this module will just see (a ~> b) as
596
597    App (App (TyVarTy "~>") (TyVarTy "a")) (TyVarTy "b")
598
599 So it'll print the type in prefix form.  To avoid confusion we must
600 remember to parenthesise the operator, thus
601
602    (~>) a b -> b
603
604 See Trac #2766.
605
606
607
608