TypeRep.lhs 19.2 KB
Newer Older
1
%
2
% (c) The University of Glasgow 2006
3 4 5 6 7
% (c) The GRASP/AQUA Project, Glasgow University, 1998
%
\section[TypeRep]{Type - friends' interface}

\begin{code}
8 9 10
-- We expose the relevant stuff from this module via the Type module
{-# OPTIONS_HADDOCK hide #-}

11
module TypeRep (
12
	TyThing(..), 
13
	Type(..),
14
	PredType(..),	 		-- to friends
15
	
16
 	Kind, ThetaType,		-- Synonyms
17

18
	funTyCon, funTyConName,
19

20
	-- Pretty-printing
21
	pprType, pprParendType, pprTypeApp,
22
	pprTyThing, pprTyThingCategory, 
23
	pprPred, pprTheta, pprForAll, pprThetaArrow, pprClassPred,
24

25
	-- Kinds
26
	liftedTypeKind, unliftedTypeKind, openTypeKind,
27 28
        argTypeKind, ubxTupleKind,
	isLiftedTypeKindCon, isLiftedTypeKind,
29
	mkArrowKind, mkArrowKinds, isCoercionKind,
30
  	coVarPred,
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

        -- Kind constructors...
        liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
        argTypeKindTyCon, ubxTupleKindTyCon,

        -- And their names
        unliftedTypeKindTyConName, openTypeKindTyConName,
        ubxTupleKindTyConName, argTypeKindTyConName,
        liftedTypeKindTyConName,

        -- Super Kinds
	tySuperKind, coSuperKind,
        isTySuperKind, isCoSuperKind,
	tySuperKindTyCon, coSuperKindTyCon,
        
46
	pprKind, pprParendKind
47 48 49 50
    ) where

#include "HsVersions.h"

51
import {-# SOURCE #-} DataCon( DataCon, dataConName )
52

53 54 55 56 57 58 59
-- friends:
import Var
import Name
import OccName
import BasicTypes
import TyCon
import Class
60 61

-- others
62
import PrelNames
63
import Outputable
twanvl's avatar
twanvl committed
64
import FastString
65 66
\end{code}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	----------------------
	A note about newtypes
	----------------------

Consider
	newtype N = MkN Int

Then we want N to be represented as an Int, and that's what we arrange.
The front end of the compiler [TcType.lhs] treats N as opaque, 
the back end treats it as transparent [Type.lhs].

There's a bit of a problem with recursive newtypes
	newtype P = MkP P
	newtype Q = MkQ (Q->Q)

Here the 'implicit expansion' we get from treating P and Q as transparent
would give rise to infinite types, which in turn makes eqType diverge.
Similarly splitForAllTys and splitFunTys can get into a loop.  

86 87
Solution: 

88
* Newtypes are always represented using TyConApp.
89

90 91 92 93 94
* For non-recursive newtypes, P, treat P just like a type synonym after 
  type-checking is done; i.e. it's opaque during type checking (functions
  from TcType) but transparent afterwards (functions from Type).  
  "Treat P as a type synonym" means "all functions expand NewTcApps 
  on the fly".
95

96 97 98
  Applications of the data constructor P simply vanish:
	P x = x
  
99

100 101 102 103 104
* For recursive newtypes Q, treat the Q and its representation as 
  distinct right through the compiler.  Applications of the data consructor
  use a coerce:
	Q = \(x::Q->Q). coerce Q x
  They are rare, so who cares if they are a tiny bit less efficient.
105

106 107
The typechecker (TcTyDecls) identifies enough type construtors as 'recursive'
to cut all loops.  The other members of the loop may be marked 'non-recursive'.
108 109


110 111 112 113 114 115 116 117
%************************************************************************
%*									*
\subsection{The data type}
%*									*
%************************************************************************


\begin{code}
118
-- | The key representation of types within the compiler
119
data Type
120
  = TyVarTy TyVar	-- ^ Vanilla type variable
121 122 123

  | AppTy
	Type
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	Type		-- ^ Type application to something other than a 'TyCon'. Parameters:
	                --
	                --  1) Function: must /not/ be a 'TyConApp', must be another 'AppTy', or 'TyVarTy'
	                --
	                --  2) Argument type

  | TyConApp
	TyCon
	[Type]		-- ^ Application of a 'TyCon', including newtypes /and/ synonyms.
	                -- Invariant: saturated appliations of 'FunTyCon' must
	                -- use 'FunTy' and saturated synonyms must use their own
	                -- constructors. However, /unsaturated/ 'FunTyCon's do appear as 'TyConApp's.
	                -- Parameters:
	                --
	                -- 1) Type constructor being applied to.
	                --
	                -- 2) Type arguments. Might not have enough type arguments here to saturate the constructor.
	                -- Even type synonyms are not necessarily saturated; for example unsaturated type synonyms
	                -- can appear as the right hand side of a type synonym.

  | FunTy
145
	Type
146
	Type		-- ^ Special case of 'TyConApp': @TyConApp FunTyCon [t1, t2]@
147

148
  | ForAllTy
149
	TyVar
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	Type	        -- ^ A polymorphic type

  | PredTy
	PredType	-- ^ The type of evidence for a type predictate.
                        -- Note that a @PredTy (EqPred _ _)@ can appear only as the kind
	                -- of a coercion variable; never as the argument or result
	                -- of a 'FunTy' (unlike the 'PredType' constructors 'ClassP' or 'IParam')
	                
	                -- See Note [PredTy], and Note [Equality predicates]

-- | The key type representing kinds in the compiler.
-- Invariant: a kind is always in one of these forms:
--
-- > FunTy k1 k2
-- > TyConApp PrimTyCon [...]
-- > TyVar kv   -- (during inference only)
-- > ForAll ... -- (for top-level coercions)
type Kind = Type

-- | "Super kinds", used to help encode 'Kind's as types.
-- Invariant: a super kind is always of this form:
--
-- > TyConApp SuperKindTyCon ...
type SuperKind = Type
174 175 176
\end{code}

-------------------------------------
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
177
 		Note [PredTy]
178

179
\begin{code}
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
-- | A type of the form @PredTy p@ represents a value whose type is
-- the Haskell predicate @p@, where a predicate is what occurs before 
-- the @=>@ in a Haskell type.
-- It can be expanded into its representation, but: 
--
-- * The type checker must treat it as opaque
--
-- * The rest of the compiler treats it as transparent
--
-- Consider these examples:
--
-- > f :: (Eq a) => a -> Int
-- > g :: (?x :: Int -> Int) => a -> Int
-- > h :: (r\l) => {r} => {l::Int | r}
--
-- Here the @Eq a@ and @?x :: Int -> Int@ and @r\l@ are all called \"predicates\"
196
data PredType 
197 198 199
  = ClassP Class [Type]		-- ^ Class predicate e.g. @Eq a@
  | IParam (IPName Name) Type	-- ^ Implicit parameter e.g. @?x :: Int@
  | EqPred Type Type		-- ^ Equality predicate e.g @ty1 ~ ty2@
200

201
-- | A collection of 'PredType's
202
type ThetaType = [PredType]
203 204
\end{code}

205 206 207 208 209 210 211 212 213 214 215 216
(We don't support TREX records yet, but the setup is designed
to expand to allow them.)

A Haskell qualified type, such as that for f,g,h above, is
represented using 
	* a FunTy for the double arrow
	* with a PredTy as the function argument

The predicate really does turn into a real extra argument to the
function.  If the argument has type (PredTy p) then the predicate p is
represented by evidence (a dictionary, for example, of type (predRepTy p).

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
217 218
Note [Equality predicates]
~~~~~~~~~~~~~~~~~~~~~~~~~~
219
	forall a b. (a ~ S b) => a -> b
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
could be represented by
	ForAllTy a (ForAllTy b (FunTy (PredTy (EqPred a (S b))) ...))
OR
	ForAllTy a (ForAllTy b (ForAllTy (c::PredTy (EqPred a (S b))) ...))

The latter is what we do.  (Unlike for class and implicit parameter
constraints, which do use FunTy.)

Reason:
	* FunTy is always a *value* function
	* ForAllTy is discarded at runtime

We often need to make a "wildcard" (c::PredTy..).  We always use the same
name (wildCoVarName), since it's not mentioned.

235

236 237 238 239 240 241 242 243 244
%************************************************************************
%*									*
			TyThing
%*									*
%************************************************************************

Despite the fact that DataCon has to be imported via a hi-boot route, 
this module seems the right place for TyThing, because it's needed for
funTyCon and all the types in TysPrim.
245 246

\begin{code}
247
-- | A typecheckable-thing, essentially anything that has a name
248 249 250 251
data TyThing = AnId     Id
	     | ADataCon DataCon
	     | ATyCon   TyCon
	     | AClass   Class
252

253 254 255 256 257
instance Outputable TyThing where 
  ppr = pprTyThing

pprTyThing :: TyThing -> SDoc
pprTyThing thing = pprTyThingCategory thing <+> quotes (ppr (getName thing))
258 259

pprTyThingCategory :: TyThing -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
260 261 262 263
pprTyThingCategory (ATyCon _) 	= ptext (sLit "Type constructor")
pprTyThingCategory (AClass _)   = ptext (sLit "Class")
pprTyThingCategory (AnId   _)   = ptext (sLit "Identifier")
pprTyThingCategory (ADataCon _) = ptext (sLit "Data constructor")
264 265 266 267 268 269

instance NamedThing TyThing where	-- Can't put this with the type
  getName (AnId id)     = getName id	-- decl, because the DataCon instance
  getName (ATyCon tc)   = getName tc	-- isn't visible there
  getName (AClass cl)   = getName cl
  getName (ADataCon dc) = dataConName dc
270
\end{code}
271

272

273 274
%************************************************************************
%*									*
275
		Wired-in type constructors
276 277 278 279 280 281
%*									*
%************************************************************************

We define a few wired-in type constructors here to avoid module knots

\begin{code}
282 283 284
--------------------------
-- First the TyCons...

285
-- | See "Type#kind_subtyping" for details of the distinction between the 'Kind' 'TyCon's
twanvl's avatar
twanvl committed
286 287 288 289 290 291 292 293 294
funTyCon, tySuperKindTyCon, coSuperKindTyCon, liftedTypeKindTyCon,
      openTypeKindTyCon, unliftedTypeKindTyCon,
      ubxTupleKindTyCon, argTypeKindTyCon
   :: TyCon
funTyConName, tySuperKindTyConName, coSuperKindTyConName, liftedTypeKindTyConName,
      openTypeKindTyConName, unliftedTypeKindTyConName,
      ubxTupleKindTyConName, argTypeKindTyConName
   :: Name

295 296
funTyCon = mkFunTyCon funTyConName (mkArrowKinds [argTypeKind, openTypeKind] liftedTypeKind)
	-- You might think that (->) should have type (?? -> ? -> *), and you'd be right
297 298
	-- But if we do that we get kind errors when saying
	--	instance Control.Arrow (->)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
299
	-- becuase the expected kind is (*->*->*).  The trouble is that the
300 301 302
	-- expected/actual stuff in the unifier does not go contra-variant, whereas
	-- the kind sub-typing does.  Sigh.  It really only matters if you use (->) in
	-- a prefix way, thus:  (->) Int# Int#.  And this is unusual.
303

304 305

tySuperKindTyCon     = mkSuperKindTyCon tySuperKindTyConName
306
coSuperKindTyCon     = mkSuperKindTyCon coSuperKindTyConName
307 308 309 310 311 312 313 314

liftedTypeKindTyCon   = mkKindTyCon liftedTypeKindTyConName
openTypeKindTyCon     = mkKindTyCon openTypeKindTyConName
unliftedTypeKindTyCon = mkKindTyCon unliftedTypeKindTyConName
ubxTupleKindTyCon     = mkKindTyCon ubxTupleKindTyConName
argTypeKindTyCon      = mkKindTyCon argTypeKindTyConName

mkKindTyCon :: Name -> TyCon
315
mkKindTyCon name = mkVoidPrimTyCon name tySuperKind 0
316 317 318 319

--------------------------
-- ... and now their names

Ian Lynagh's avatar
Ian Lynagh committed
320 321 322 323 324 325 326 327
tySuperKindTyConName      = mkPrimTyConName (fsLit "BOX") tySuperKindTyConKey tySuperKindTyCon
coSuperKindTyConName      = mkPrimTyConName (fsLit "COERCION") coSuperKindTyConKey coSuperKindTyCon
liftedTypeKindTyConName   = mkPrimTyConName (fsLit "*") liftedTypeKindTyConKey liftedTypeKindTyCon
openTypeKindTyConName     = mkPrimTyConName (fsLit "?") openTypeKindTyConKey openTypeKindTyCon
unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon
ubxTupleKindTyConName     = mkPrimTyConName (fsLit "(#)") ubxTupleKindTyConKey ubxTupleKindTyCon
argTypeKindTyConName      = mkPrimTyConName (fsLit "??") argTypeKindTyConKey argTypeKindTyCon
funTyConName              = mkPrimTyConName (fsLit "(->)") funTyConKey funTyCon
328

twanvl's avatar
twanvl committed
329
mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
330
mkPrimTyConName occ key tycon = mkWiredInName gHC_PRIM (mkTcOccFS occ) 
331 332 333 334 335 336 337 338 339 340 341 342
					      key 
					      (ATyCon tycon)
					      BuiltInSyntax
	-- All of the super kinds and kinds are defined in Prim and use BuiltInSyntax,
	-- because they are never in scope in the source

------------------
-- We also need Kinds and SuperKinds, locally and in TyCon

kindTyConType :: TyCon -> Type
kindTyConType kind = TyConApp kind []

343
-- | See "Type#kind_subtyping" for details of the distinction between these 'Kind's
twanvl's avatar
twanvl committed
344 345
liftedTypeKind, unliftedTypeKind, openTypeKind, argTypeKind, ubxTupleKind :: Kind

346 347 348 349 350 351
liftedTypeKind   = kindTyConType liftedTypeKindTyCon
unliftedTypeKind = kindTyConType unliftedTypeKindTyCon
openTypeKind     = kindTyConType openTypeKindTyCon
argTypeKind      = kindTyConType argTypeKindTyCon
ubxTupleKind	 = kindTyConType ubxTupleKindTyCon

352
-- | Given two kinds @k1@ and @k2@, creates the 'Kind' @k1 -> k2@
353 354 355
mkArrowKind :: Kind -> Kind -> Kind
mkArrowKind k1 k2 = FunTy k1 k2

356
-- | Iterated application of 'mkArrowKind'
357 358 359 360 361 362 363
mkArrowKinds :: [Kind] -> Kind -> Kind
mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds

tySuperKind, coSuperKind :: SuperKind
tySuperKind = kindTyConType tySuperKindTyCon 
coSuperKind = kindTyConType coSuperKindTyCon 

twanvl's avatar
twanvl committed
364
isTySuperKind :: SuperKind -> Bool
365
isTySuperKind (TyConApp kc []) = kc `hasKey` tySuperKindTyConKey
twanvl's avatar
twanvl committed
366
isTySuperKind _                = False
367

368
isCoSuperKind :: SuperKind -> Bool
369
isCoSuperKind (TyConApp kc []) = kc `hasKey` coSuperKindTyConKey
twanvl's avatar
twanvl committed
370
isCoSuperKind _                = False
371 372

-------------------
373
-- Lastly we need a few functions on Kinds
374

twanvl's avatar
twanvl committed
375
isLiftedTypeKindCon :: TyCon -> Bool
376 377
isLiftedTypeKindCon tc    = tc `hasKey` liftedTypeKindTyConKey

378
isLiftedTypeKind :: Kind -> Bool
379
isLiftedTypeKind (TyConApp tc []) = isLiftedTypeKindCon tc
twanvl's avatar
twanvl committed
380
isLiftedTypeKind _                = False
381

382
isCoercionKind :: Kind -> Bool
383
-- All coercions are of form (ty1 ~ ty2)
384 385 386
-- This function is here rather than in Coercion, 
-- because it's used in a knot-tied way to enforce invariants in Var
isCoercionKind (PredTy (EqPred {})) = True
twanvl's avatar
twanvl committed
387
isCoercionKind _                    = False
388 389 390 391 392

coVarPred :: CoVar -> PredType
coVarPred tv
  = ASSERT( isCoVar tv )
    case tyVarKind tv of
393
	PredTy eq -> eq
394
	other	  -> pprPanic "coVarPred" (ppr tv $$ ppr other)
395 396 397
\end{code}


398

399 400
%************************************************************************
%*									*
401
\subsection{The external interface}
402 403 404
%*									*
%************************************************************************

405 406 407 408 409
@pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
defined to use this.  @pprParendType@ is the same, except it puts
parens around the type, except for the atomic cases.  @pprParendType@
works just by setting the initial context precedence very high.

410
\begin{code}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
data Prec = TopPrec 	-- No parens
	  | FunPrec 	-- Function args; no parens for tycon apps
	  | TyConPrec 	-- Tycon args; no parens for atomic
	  deriving( Eq, Ord )

maybeParen :: Prec -> Prec -> SDoc -> SDoc
maybeParen ctxt_prec inner_prec pretty
  | ctxt_prec < inner_prec = pretty
  | otherwise		   = parens pretty

------------------
pprType, pprParendType :: Type -> SDoc
pprType       ty = ppr_type TopPrec   ty
pprParendType ty = ppr_type TyConPrec ty

426 427 428 429
pprTypeApp :: NamedThing a => a -> [Type] -> SDoc
-- The first arg is the tycon, or sometimes class
-- Print infix if the tycon/class looks like an operator
pprTypeApp tc tys = ppr_type_app TopPrec (getName tc) tys
430

431 432 433 434
------------------
pprPred :: PredType -> SDoc
pprPred (ClassP cls tys) = pprClassPred cls tys
pprPred (IParam ip ty)   = ppr ip <> dcolon <> pprType ty
Ian Lynagh's avatar
Ian Lynagh committed
435
pprPred (EqPred ty1 ty2) = sep [ppr ty1, nest 2 (ptext (sLit "~")), ppr ty2]
436
pprClassPred :: Class -> [Type] -> SDoc
437
pprClassPred clas tys = ppr_type_app TopPrec (getName clas) tys
438 439 440 441 442 443 444

pprTheta :: ThetaType -> SDoc
pprTheta theta = parens (sep (punctuate comma (map pprPred theta)))

pprThetaArrow :: ThetaType -> SDoc
pprThetaArrow theta 
  | null theta = empty
Ian Lynagh's avatar
Ian Lynagh committed
445
  | otherwise  = parens (sep (punctuate comma (map pprPred theta))) <+> ptext (sLit "=>")
446 447 448 449 450 451 452 453 454 455 456 457 458 459

------------------
instance Outputable Type where
    ppr ty = pprType ty

instance Outputable PredType where
    ppr = pprPred

instance Outputable name => OutputableBndr (IPName name) where
    pprBndr _ n = ppr n	-- Simple for now

------------------
	-- OK, here's the main printer

twanvl's avatar
twanvl committed
460
pprKind, pprParendKind :: Kind -> SDoc
461 462 463
pprKind = pprType
pprParendKind = pprParendType

464
ppr_type :: Prec -> Type -> SDoc
465 466 467
ppr_type _ (TyVarTy tv)		-- Note [Infix type variables]
  | isSymOcc (getOccName tv)  = parens (ppr tv)
  | otherwise		      = ppr tv
Ian Lynagh's avatar
Ian Lynagh committed
468
ppr_type _ (PredTy pred)      = ifPprDebug (ptext (sLit "<pred>")) <> (ppr pred)
469
ppr_type p (TyConApp tc tys)  = ppr_tc_app p tc tys
470 471 472 473

ppr_type p (AppTy t1 t2) = maybeParen p TyConPrec $
			   pprType t1 <+> ppr_type TyConPrec t2

474 475 476
ppr_type p ty@(ForAllTy _ _)       = ppr_forall_type p ty
ppr_type p ty@(FunTy (PredTy _) _) = ppr_forall_type p ty

477 478 479 480 481 482 483 484
ppr_type p (FunTy ty1 ty2)
  = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
    maybeParen p FunPrec $
    sep (ppr_type FunPrec ty1 : ppr_fun_tail ty2)
  where
    ppr_fun_tail (FunTy ty1 ty2) = (arrow <+> ppr_type FunPrec ty1) : ppr_fun_tail ty2
    ppr_fun_tail other_ty        = [arrow <+> pprType other_ty]

485 486
ppr_forall_type :: Prec -> Type -> SDoc
ppr_forall_type p ty
487
  = maybeParen p FunPrec $
488
    sep [pprForAll tvs, pprThetaArrow ctxt, pprType tau]
489
  where
490 491
    (tvs,  rho) = split1 [] ty
    (ctxt, tau) = split2 [] rho
492 493 494 495 496

    -- We need to be extra careful here as equality constraints will occur as
    -- type variables with an equality kind.  So, while collecting quantified
    -- variables, we separate the coercion variables out and turn them into
    -- equality predicates.
497 498 499
    split1 tvs (ForAllTy tv ty) 
      | not (isCoVar tv)     = split1 (tv:tvs) ty
    split1 tvs ty	     = (reverse tvs, ty)
500
 
501 502 503 504
    split2 ps (PredTy p `FunTy` ty) = split2 (p:ps) ty
    split2 ps (ForAllTy tv ty) 
	| isCoVar tv		    = split2 (coVarPred tv : ps) ty
    split2 ps ty		    = (reverse ps, ty)
505 506

ppr_tc_app :: Prec -> TyCon -> [Type] -> SDoc
twanvl's avatar
twanvl committed
507
ppr_tc_app _ tc []
508
  = ppr_tc tc
twanvl's avatar
twanvl committed
509
ppr_tc_app _ tc [ty]
510
  | tc `hasKey` listTyConKey = brackets (pprType ty)
Ian Lynagh's avatar
Ian Lynagh committed
511 512 513 514 515 516
  | tc `hasKey` parrTyConKey = ptext (sLit "[:") <> pprType ty <> ptext (sLit ":]")
  | tc `hasKey` liftedTypeKindTyConKey   = ptext (sLit "*")
  | tc `hasKey` unliftedTypeKindTyConKey = ptext (sLit "#")
  | tc `hasKey` openTypeKindTyConKey     = ptext (sLit "(?)")
  | tc `hasKey` ubxTupleKindTyConKey     = ptext (sLit "(#)")
  | tc `hasKey` argTypeKindTyConKey      = ptext (sLit "??")
517

518 519 520 521
ppr_tc_app p tc tys
  | isTupleTyCon tc && tyConArity tc == length tys
  = tupleParens (tupleTyConBoxity tc) (sep (punctuate comma (map pprType tys)))
  | otherwise
522
  = ppr_type_app p (getName tc) tys
523

524 525 526
ppr_type_app :: Prec -> Name -> [Type] -> SDoc
-- Used for classes as well as types; that's why it's separate from ppr_tc_app
ppr_type_app p tc tys
527 528 529
  | is_sym_occ		-- Print infix if possible
  , [ty1,ty2] <- tys	-- We know nothing of precedence though
  = maybeParen p FunPrec (sep [ppr_type FunPrec ty1, 
530
			       pprInfixVar True (ppr tc) <+> ppr_type FunPrec ty2])
531
  | otherwise
532 533
  = maybeParen p TyConPrec (hang (pprPrefixVar is_sym_occ (ppr tc))
    	       	 	       2 (sep (map pprParendType tys)))
534 535
  where
    is_sym_occ = isSymOcc (getOccName tc)
536

537 538
ppr_tc :: TyCon -> SDoc	-- No brackets for SymOcc
ppr_tc tc 
539
  = pp_nt_debug <> ppr tc
540 541
  where
   pp_nt_debug | isNewTyCon tc = ifPprDebug (if isRecursiveTyCon tc 
Ian Lynagh's avatar
Ian Lynagh committed
542 543
				             then ptext (sLit "<recnt>")
					     else ptext (sLit "<nt>"))
544 545
	       | otherwise     = empty

546
-------------------
twanvl's avatar
twanvl committed
547
pprForAll :: [TyVar] -> SDoc
548
pprForAll []  = empty
Ian Lynagh's avatar
Ian Lynagh committed
549
pprForAll tvs = ptext (sLit "forall") <+> sep (map pprTvBndr tvs) <> dot
550

twanvl's avatar
twanvl committed
551
pprTvBndr :: TyVar -> SDoc
552 553 554 555 556 557
pprTvBndr tv | isLiftedTypeKind kind = ppr tv
	     | otherwise	     = parens (ppr tv <+> dcolon <+> pprKind kind)
	     where
	       kind = tyVarKind tv
\end{code}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
Note [Infix type variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
In Haskell 98 you can say

   f :: (a ~> b) -> b

and the (~>) is considered a type variable.  However, the type
pretty-printer in this module will just see (a ~> b) as

   App (App (TyVarTy "~>") (TyVarTy "a")) (TyVarTy "b")

So it'll print the type in prefix form.  To avoid confusion we must
remember to parenthesise the operator, thus

   (~>) a b -> b

See Trac #2766.