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

\begin{code}
module TypeRep (
9
	TyThing(..), 
10
	Type(..),
11
	PredType(..),	 		-- to friends
12
	
13
 	Kind, ThetaType,		-- Synonyms
14

15 16
	funTyCon,

17
	-- Pretty-printing
18
	pprType, pprParendType, pprTypeApp,
19
	pprTyThing, pprTyThingCategory, 
20
	pprPred, pprTheta, pprForAll, pprThetaArrow, pprClassPred,
21

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

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

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

        -- Super Kinds
	tySuperKind, coSuperKind,
        isTySuperKind, isCoSuperKind,
	tySuperKindTyCon, coSuperKindTyCon,
        
43
	pprKind, pprParendKind
44 45 46 47
    ) where

#include "HsVersions.h"

48
import {-# SOURCE #-} DataCon( DataCon, dataConName )
49

50 51 52 53 54 55 56
-- friends:
import Var
import Name
import OccName
import BasicTypes
import TyCon
import Class
57 58

-- others
59
import PrelNames
60
import Outputable
twanvl's avatar
twanvl committed
61
import FastString
62 63 64 65 66 67 68 69 70 71 72
\end{code}

%************************************************************************
%*									*
\subsection{Type Classifications}
%*									*
%************************************************************************

A type is

	*unboxed*	iff its representation is other than a pointer
73
			Unboxed types are also unlifted.
74 75 76 77 78 79

	*lifted*	A type is lifted iff it has bottom as an element.
			Closures always have lifted types:  i.e. any
			let-bound identifier in Core must have a lifted
			type.  Operationally, a lifted object is one that
			can be entered.
80 81

			Only lifted types may be unified with a type variable.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

	*algebraic*	A type with one or more constructors, whether declared
			with "data" or "newtype".   
			An algebraic type is one that can be deconstructed
			with a case expression.  
			*NOT* the same as lifted types,  because we also 
			include unboxed tuples in this classification.

	*data*		A type declared with "data".  Also boxed tuples.

	*primitive*	iff it is a built-in type that can't be expressed
			in Haskell.

Currently, all primitive types are unlifted, but that's not necessarily
the case.  (E.g. Int could be primitive.)

Some primitive types are unboxed, such as Int#, whereas some are boxed
but unlifted (such as ByteArray#).  The only primitive types that we
classify as algebraic are the unboxed tuples.

examples of type classifications:

Type		primitive	boxed		lifted		algebraic    
-----------------------------------------------------------------------------
Int#,		Yes		No		No		No
ByteArray#	Yes		Yes		No		No
(# a, b #)	Yes		No		No		Yes
(  a, b  )	No		Yes		Yes		Yes
[a]		No		Yes		Yes		Yes

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132


	----------------------
	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.  

133 134
Solution: 

135
* Newtypes are always represented using TyConApp.
136

137 138 139 140 141
* 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".
142

143 144 145
  Applications of the data constructor P simply vanish:
	P x = x
  
146

147 148 149 150 151
* 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.
152

153 154
The typechecker (TcTyDecls) identifies enough type construtors as 'recursive'
to cut all loops.  The other members of the loop may be marked 'non-recursive'.
155 156


157 158 159 160 161 162 163 164 165
%************************************************************************
%*									*
\subsection{The data type}
%*									*
%************************************************************************


\begin{code}
data Type
166
  = TyVarTy TyVar	
167 168

  | AppTy
169
	Type		-- Function is *not* a TyConApp
170
	Type		-- It must be another AppTy, or TyVarTy
171

172
  | TyConApp		-- Application of a TyCon, including newtypes *and* synonyms
173
	TyCon		--  *Invariant* saturated appliations of FunTyCon and
174
			-- 	synonyms have their own constructors, below.
175 176
			-- However, *unsaturated* FunTyCons do appear as TyConApps.  
			-- 
177
	[Type]		-- Might not be saturated.
178 179 180
			-- Even type synonyms are not necessarily saturated;
			-- for example unsaturated type synonyms can appear as the 
			-- RHS of a type synonym.
181

182
  | FunTy		-- Special case of TyConApp: TyConApp FunTyCon [t1,t2]
183 184 185
	Type
	Type

186 187 188 189
  | ForAllTy		-- A polymorphic type
	TyVar
	Type	

chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
190
  | PredTy		-- The type of evidence for a type predictate
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
191
	PredType	-- See Note [PredTy], and Note [Equality predicates]
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
192 193 194
	-- NB: A PredTy (EqPred _ _) can appear only as the kind
	--     of a coercion variable; never as the argument or result
	--     of a FunTy (unlike ClassP, IParam)
195

196 197 198 199 200 201 202 203
type Kind = Type 	-- Invariant: a kind is always
			--	FunTy k1 k2
			-- or	TyConApp PrimTyCon [...]
			-- or	TyVar kv (during inference only)
			-- or   ForAll ... (for top-level coercions)

type SuperKind = Type   -- Invariant: a super kind is always 
                        --   TyConApp SuperKindTyCon ...
204 205 206
\end{code}

-------------------------------------
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
207
 		Note [PredTy]
208 209

A type of the form
210 211 212
	PredTy p
represents a value whose type is the Haskell predicate p, 
where a predicate is what occurs before the '=>' in a Haskell type.
213 214 215 216 217
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

218 219 220 221 222 223 224 225 226
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*
Predicates are represented inside GHC by PredType:

\begin{code}
227
data PredType 
228 229
  = ClassP Class [Type]		-- Class predicate
  | IParam (IPName Name) Type	-- Implicit parameter
230
  | EqPred Type Type		-- Equality predicate (ty1 ~ ty2)
231

232
type ThetaType = [PredType]
233 234
\end{code}

235 236 237 238 239 240 241 242 243 244 245 246
(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
247 248
Note [Equality predicates]
~~~~~~~~~~~~~~~~~~~~~~~~~~
249
	forall a b. (a ~ S b) => a -> b
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
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.

265

266 267 268 269 270 271 272 273 274
%************************************************************************
%*									*
			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.
275 276

\begin{code}
277 278 279 280
data TyThing = AnId     Id
	     | ADataCon DataCon
	     | ATyCon   TyCon
	     | AClass   Class
281

282 283 284 285 286
instance Outputable TyThing where 
  ppr = pprTyThing

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

pprTyThingCategory :: TyThing -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
289 290 291 292
pprTyThingCategory (ATyCon _) 	= ptext (sLit "Type constructor")
pprTyThingCategory (AClass _)   = ptext (sLit "Class")
pprTyThingCategory (AnId   _)   = ptext (sLit "Identifier")
pprTyThingCategory (ADataCon _) = ptext (sLit "Data constructor")
293 294 295 296 297 298

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
299
\end{code}
300

301

302 303
%************************************************************************
%*									*
304
		Wired-in type constructors
305 306 307 308 309 310
%*									*
%************************************************************************

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

\begin{code}
311 312 313
--------------------------
-- First the TyCons...

twanvl's avatar
twanvl committed
314 315 316 317 318 319 320 321 322
funTyCon, tySuperKindTyCon, coSuperKindTyCon, liftedTypeKindTyCon,
      openTypeKindTyCon, unliftedTypeKindTyCon,
      ubxTupleKindTyCon, argTypeKindTyCon
   :: TyCon
funTyConName, tySuperKindTyConName, coSuperKindTyConName, liftedTypeKindTyConName,
      openTypeKindTyConName, unliftedTypeKindTyConName,
      ubxTupleKindTyConName, argTypeKindTyConName
   :: Name

323 324
funTyCon = mkFunTyCon funTyConName (mkArrowKinds [argTypeKind, openTypeKind] liftedTypeKind)
	-- You might think that (->) should have type (?? -> ? -> *), and you'd be right
325 326
	-- But if we do that we get kind errors when saying
	--	instance Control.Arrow (->)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
327
	-- becuase the expected kind is (*->*->*).  The trouble is that the
328 329 330
	-- 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.
331

332 333

tySuperKindTyCon     = mkSuperKindTyCon tySuperKindTyConName
334
coSuperKindTyCon     = mkSuperKindTyCon coSuperKindTyConName
335 336 337 338 339 340 341 342

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

mkKindTyCon :: Name -> TyCon
343
mkKindTyCon name = mkVoidPrimTyCon name tySuperKind 0
344 345 346 347

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

Ian Lynagh's avatar
Ian Lynagh committed
348 349 350 351 352 353 354 355
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
356

twanvl's avatar
twanvl committed
357
mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
358 359 360 361 362 363 364 365 366 367 368 369 370
mkPrimTyConName occ key tycon = mkWiredInName gHC_PRIM (mkOccNameFS tcName occ) 
					      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 []

twanvl's avatar
twanvl committed
371 372
liftedTypeKind, unliftedTypeKind, openTypeKind, argTypeKind, ubxTupleKind :: Kind

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
liftedTypeKind   = kindTyConType liftedTypeKindTyCon
unliftedTypeKind = kindTyConType unliftedTypeKindTyCon
openTypeKind     = kindTyConType openTypeKindTyCon
argTypeKind      = kindTyConType argTypeKindTyCon
ubxTupleKind	 = kindTyConType ubxTupleKindTyCon

mkArrowKind :: Kind -> Kind -> Kind
mkArrowKind k1 k2 = FunTy k1 k2

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
389
isTySuperKind :: SuperKind -> Bool
390
isTySuperKind (TyConApp kc []) = kc `hasKey` tySuperKindTyConKey
twanvl's avatar
twanvl committed
391
isTySuperKind _                = False
392

393
isCoSuperKind :: SuperKind -> Bool
394
isCoSuperKind (TyConApp kc []) = kc `hasKey` coSuperKindTyConKey
twanvl's avatar
twanvl committed
395
isCoSuperKind _                = False
396 397

-------------------
398
-- Lastly we need a few functions on Kinds
399

twanvl's avatar
twanvl committed
400
isLiftedTypeKindCon :: TyCon -> Bool
401 402
isLiftedTypeKindCon tc    = tc `hasKey` liftedTypeKindTyConKey

403
isLiftedTypeKind :: Kind -> Bool
404
isLiftedTypeKind (TyConApp tc []) = isLiftedTypeKindCon tc
twanvl's avatar
twanvl committed
405
isLiftedTypeKind _                = False
406

407
isCoercionKind :: Kind -> Bool
408
-- All coercions are of form (ty1 ~ ty2)
409 410 411
-- 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
412
isCoercionKind _                    = False
413 414 415 416 417

coVarPred :: CoVar -> PredType
coVarPred tv
  = ASSERT( isCoVar tv )
    case tyVarKind tv of
418
	PredTy eq -> eq
419
	other	  -> pprPanic "coVarPred" (ppr tv $$ ppr other)
420 421 422
\end{code}


423

424 425
%************************************************************************
%*									*
426
\subsection{The external interface}
427 428 429
%*									*
%************************************************************************

430 431 432 433 434
@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.

435
\begin{code}
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
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

451 452 453 454
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
455

456 457 458 459
------------------
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
460
pprPred (EqPred ty1 ty2) = sep [ppr ty1, nest 2 (ptext (sLit "~")), ppr ty2]
461
pprClassPred :: Class -> [Type] -> SDoc
462
pprClassPred clas tys = ppr_type_app TopPrec (getName clas) tys
463 464 465 466 467 468 469

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
470
  | otherwise  = parens (sep (punctuate comma (map pprPred theta))) <+> ptext (sLit "=>")
471 472 473 474 475 476 477 478 479 480 481 482 483 484

------------------
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
485
pprKind, pprParendKind :: Kind -> SDoc
486 487 488
pprKind = pprType
pprParendKind = pprParendType

489
ppr_type :: Prec -> Type -> SDoc
twanvl's avatar
twanvl committed
490
ppr_type _ (TyVarTy tv)       = ppr tv
Ian Lynagh's avatar
Ian Lynagh committed
491
ppr_type _ (PredTy pred)      = ifPprDebug (ptext (sLit "<pred>")) <> (ppr pred)
492
ppr_type p (TyConApp tc tys)  = ppr_tc_app p tc tys
493 494 495 496

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

497 498 499
ppr_type p ty@(ForAllTy _ _)       = ppr_forall_type p ty
ppr_type p ty@(FunTy (PredTy _) _) = ppr_forall_type p ty

500 501 502 503 504 505 506 507
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]

508 509
ppr_forall_type :: Prec -> Type -> SDoc
ppr_forall_type p ty
510
  = maybeParen p FunPrec $
511
    sep [pprForAll tvs, pprThetaArrow ctxt, pprType tau]
512
  where
513 514
    (tvs,  rho) = split1 [] ty
    (ctxt, tau) = split2 [] rho
515 516 517 518 519

    -- 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.
520 521 522
    split1 tvs (ForAllTy tv ty) 
      | not (isCoVar tv)     = split1 (tv:tvs) ty
    split1 tvs ty	     = (reverse tvs, ty)
523
 
524 525 526 527
    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)
528 529

ppr_tc_app :: Prec -> TyCon -> [Type] -> SDoc
twanvl's avatar
twanvl committed
530
ppr_tc_app _ tc []
531
  = ppr_tc tc
twanvl's avatar
twanvl committed
532
ppr_tc_app _ tc [ty]
533
  | tc `hasKey` listTyConKey = brackets (pprType ty)
Ian Lynagh's avatar
Ian Lynagh committed
534 535 536 537 538 539
  | 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 "??")
540

541 542 543 544
ppr_tc_app p tc tys
  | isTupleTyCon tc && tyConArity tc == length tys
  = tupleParens (tupleTyConBoxity tc) (sep (punctuate comma (map pprType tys)))
  | otherwise
545
  = ppr_type_app p (getName tc) tys
546

547 548 549
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
550 551 552
  | is_sym_occ		-- Print infix if possible
  , [ty1,ty2] <- tys	-- We know nothing of precedence though
  = maybeParen p FunPrec (sep [ppr_type FunPrec ty1, 
553
			       pprInfixVar True (ppr tc) <+> ppr_type FunPrec ty2])
554
  | otherwise
555 556
  = maybeParen p TyConPrec (hang (pprPrefixVar is_sym_occ (ppr tc))
    	       	 	       2 (sep (map pprParendType tys)))
557 558
  where
    is_sym_occ = isSymOcc (getOccName tc)
559

560 561
ppr_tc :: TyCon -> SDoc	-- No brackets for SymOcc
ppr_tc tc 
562
  = pp_nt_debug <> ppr tc
563 564
  where
   pp_nt_debug | isNewTyCon tc = ifPprDebug (if isRecursiveTyCon tc 
Ian Lynagh's avatar
Ian Lynagh committed
565 566
				             then ptext (sLit "<recnt>")
					     else ptext (sLit "<nt>"))
567 568
	       | otherwise     = empty

569
-------------------
twanvl's avatar
twanvl committed
570
pprForAll :: [TyVar] -> SDoc
571
pprForAll []  = empty
Ian Lynagh's avatar
Ian Lynagh committed
572
pprForAll tvs = ptext (sLit "forall") <+> sep (map pprTvBndr tvs) <> dot
573

twanvl's avatar
twanvl committed
574
pprTvBndr :: TyVar -> SDoc
575 576 577 578 579 580
pprTvBndr tv | isLiftedTypeKind kind = ppr tv
	     | otherwise	     = parens (ppr tv <+> dcolon <+> pprKind kind)
	     where
	       kind = tyVarKind tv
\end{code}