Add kind equalities to GHC.
[ghc.git] / compiler / hsSyn / HsTypes.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 HsTypes: Abstract syntax: user-defined types
7 -}
8
9 {-# LANGUAGE DeriveDataTypeable #-}
10 {-# LANGUAGE FlexibleContexts #-}
11 {-# LANGUAGE FlexibleInstances #-}
12 {-# LANGUAGE StandaloneDeriving #-}
13 {-# LANGUAGE TypeSynonymInstances #-}
14 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
15 -- in module PlaceHolder
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE CPP #-}
18
19 module HsTypes (
20 HsType(..), LHsType, HsKind, LHsKind,
21 HsTyVarBndr(..), LHsTyVarBndr,
22 LHsQTyVars(..),
23 HsImplicitBndrs(..),
24 HsWildCardBndrs(..),
25 LHsSigType, LHsSigWcType, LHsWcType,
26 HsTupleSort(..),
27 HsContext, LHsContext,
28 HsTyLit(..),
29 HsIPName(..), hsIPNameFS,
30 HsAppType(..),
31
32 LBangType, BangType,
33 HsSrcBang(..), HsImplBang(..),
34 SrcStrictness(..), SrcUnpackedness(..),
35 getBangType, getBangStrictness,
36
37 ConDeclField(..), LConDeclField, pprConDeclFields,
38
39 FieldOcc(..), LFieldOcc, mkFieldOcc,
40 AmbiguousFieldOcc(..), mkAmbiguousFieldOcc,
41 rdrNameAmbiguousFieldOcc, selectorAmbiguousFieldOcc,
42 unambiguousFieldOcc, ambiguousFieldOcc,
43
44 HsWildCardInfo(..), mkAnonWildCardTy, mkNamedWildCardTy,
45 wildCardName, sameWildCard, sameNamedWildCard,
46 isAnonWildCard, isNamedWildCard,
47
48 mkHsImplicitBndrs, mkHsWildCardBndrs, hsImplicitBody,
49 mkEmptyImplicitBndrs, mkEmptyWildCardBndrs,
50 mkHsQTvs, hsQTvExplicit, isHsKindedTyVar, hsTvbAllKinded,
51 hsScopedTvs, hsWcScopedTvs, dropWildCards,
52 hsTyVarName, hsAllLTyVarNames, hsLTyVarLocNames,
53 hsLTyVarName, hsLTyVarLocName, hsExplicitLTyVarNames,
54 splitLHsInstDeclTy,
55 splitLHsPatSynTy,
56 splitLHsForAllTy, splitLHsQualTy, splitLHsSigmaTy,
57 splitHsFunType, splitHsAppTys,
58 mkHsOpTy,
59 ignoreParens, hsSigType, hsSigWcType,
60 hsLTyVarBndrToType, hsLTyVarBndrsToTypes,
61
62 -- Printing
63 pprParendHsType, pprHsForAll, pprHsForAllTvs, pprHsForAllExtra,
64 pprHsContext, pprHsContextNoArrow, pprHsContextMaybe
65 ) where
66
67 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
68
69 import PlaceHolder ( PostTc,PostRn,DataId,PlaceHolder(..) )
70
71 import Id ( Id )
72 import Name( Name )
73 import RdrName ( RdrName )
74 import DataCon( HsSrcBang(..), HsImplBang(..),
75 SrcStrictness(..), SrcUnpackedness(..) )
76 import TysPrim( funTyConName )
77 import Type
78 import HsDoc
79 import BasicTypes
80 import SrcLoc
81 import StaticFlags
82 import Outputable
83 import FastString
84 import Maybes( isJust )
85
86 import Data.Data hiding ( Fixity )
87 import Data.Maybe ( fromMaybe )
88 #if __GLASGOW_HASKELL > 710
89 import Data.Semigroup ( Semigroup )
90 import qualified Data.Semigroup as Semigroup
91 #endif
92
93 {-
94 ************************************************************************
95 * *
96 \subsection{Bang annotations}
97 * *
98 ************************************************************************
99 -}
100
101 type LBangType name = Located (BangType name)
102 type BangType name = HsType name -- Bangs are in the HsType data type
103
104 getBangType :: LHsType a -> LHsType a
105 getBangType (L _ (HsBangTy _ ty)) = ty
106 getBangType ty = ty
107
108 getBangStrictness :: LHsType a -> HsSrcBang
109 getBangStrictness (L _ (HsBangTy s _)) = s
110 getBangStrictness _ = (HsSrcBang Nothing NoSrcUnpack NoSrcStrict)
111
112 {-
113 ************************************************************************
114 * *
115 \subsection{Data types}
116 * *
117 ************************************************************************
118
119 This is the syntax for types as seen in type signatures.
120
121 Note [HsBSig binder lists]
122 ~~~~~~~~~~~~~~~~~~~~~~~~~~
123 Consider a binder (or pattern) decoarated with a type or kind,
124 \ (x :: a -> a). blah
125 forall (a :: k -> *) (b :: k). blah
126 Then we use a LHsBndrSig on the binder, so that the
127 renamer can decorate it with the variables bound
128 by the pattern ('a' in the first example, 'k' in the second),
129 assuming that neither of them is in scope already
130 See also Note [Kind and type-variable binders] in RnTypes
131
132 Note [HsType binders]
133 ~~~~~~~~~~~~~~~~~~~~~
134 The system for recording type and kind-variable binders in HsTypes
135 is a bit complicated. Here's how it works.
136
137 * In a HsType,
138 HsForAllTy represents an /explicit, user-written/ 'forall'
139 e.g. forall a b. ...
140 HsQualTy reprsents an /explicit, user-written/ context
141 e.g. (Eq a, Show a) => ...
142 The context can be empty if that's what the user wrote
143 These constructors represent what the user wrote, no more
144 and no less.
145
146 * HsTyVarBndr describes a quantified type variable written by the
147 user. For example
148 f :: forall a (b :: *). blah
149 here 'a' and '(b::*)' are each a HsTyVarBndr. A HsForAllTy has
150 a list of LHsTyVarBndrs.
151
152 * HsImplicitBndrs is a wrapper that gives the implicitly-quantified
153 kind and type variables of the wrapped thing. It is filled in by
154 the renamer. For example, if the
155 user writes
156 f :: a -> a
157 the HsImplicitBinders binds the 'a' (not a HsForAllTy!).
158 NB: this implicit quantification is purely lexical: we bind any
159 type or kind variables that are not in scope. The type checker
160 may subsequently quantify over further kind variables.
161
162 * HsWildCardBndrs is a wrapper that binds the wildcard variables
163 of the wrapped thing. It is filled in by the renamer
164 f :: _a -> _
165 The enclosing HsWildCardBndrs binds the wildcards _a and _.
166
167 * The explicit presence of these wrappers specifies, in the HsSyn,
168 exactly where implicit quantification is allowed, and where
169 wildcards are allowed.
170
171 * LHsQTyVars is used in data/class declarations, where the user gives
172 explicit *type* variable bindings, but we need to implicitly bind
173 *kind* variables. For example
174 class C (a :: k -> *) where ...
175 The 'k' is implicitly bound in the hsq_tvs field of LHsQTyVars
176
177 -}
178
179 type LHsContext name = Located (HsContext name)
180 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
181
182 -- For details on above see note [Api annotations] in ApiAnnotation
183
184 type HsContext name = [LHsType name]
185
186 type LHsType name = Located (HsType name)
187 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
188 -- in a list
189
190 -- For details on above see note [Api annotations] in ApiAnnotation
191 type HsKind name = HsType name
192 type LHsKind name = Located (HsKind name)
193 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
194
195 -- For details on above see note [Api annotations] in ApiAnnotation
196
197 --------------------------------------------------
198 -- LHsQTyVars
199 -- The explicitly-quantified binders in a data/type declaration
200
201 type LHsTyVarBndr name = Located (HsTyVarBndr name)
202 -- See Note [HsType binders]
203
204 data LHsQTyVars name -- See Note [HsType binders]
205 = HsQTvs { hsq_implicit :: PostRn name [Name] -- implicit (dependent) variables
206 , hsq_explicit :: [LHsTyVarBndr name] -- explicit variables
207 -- See Note [HsForAllTy tyvar binders]
208 }
209 deriving( Typeable )
210
211 deriving instance (DataId name) => Data (LHsQTyVars name)
212
213 mkHsQTvs :: [LHsTyVarBndr RdrName] -> LHsQTyVars RdrName
214 mkHsQTvs tvs = HsQTvs { hsq_implicit = PlaceHolder, hsq_explicit = tvs }
215
216 hsQTvExplicit :: LHsQTyVars name -> [LHsTyVarBndr name]
217 hsQTvExplicit = hsq_explicit
218
219 ------------------------------------------------
220 -- HsImplicitBndrs
221 -- Used to quantify the binders of a type in cases
222 -- when a HsForAll isn't appropriate:
223 -- * Patterns in a type/data family instance (HsTyPats)
224 -- * Type of a rule binder (RuleBndr)
225 -- * Pattern type signatures (SigPatIn)
226 -- In the last of these, wildcards can happen, so we must accommodate them
227
228 data HsImplicitBndrs name thing -- See Note [HsType binders]
229 = HsIB { hsib_vars :: PostRn name [Name] -- Implicitly-bound kind & type vars
230 , hsib_body :: thing -- Main payload (type or list of types)
231 }
232 deriving (Typeable)
233
234 data HsWildCardBndrs name thing -- See Note [HsType binders]
235 = HsWC { hswc_wcs :: PostRn name [Name]
236 -- Wild cards, both named and anonymous
237
238 , hswc_ctx :: Maybe SrcSpan
239 -- Indicates whether hswc_body has an
240 -- extra-constraint wildcard, and if so where
241 -- e.g. (Eq a, _) => a -> a
242 -- NB: the wildcard stays in HsQualTy inside the type!
243 -- So for pretty printing purposes you can ignore
244 -- hswc_ctx
245
246 , hswc_body :: thing -- Main payload (type or list of types)
247 }
248 deriving( Typeable )
249
250 deriving instance (Data name, Data thing, Data (PostRn name [Name]))
251 => Data (HsImplicitBndrs name thing)
252
253 deriving instance (Data name, Data thing, Data (PostRn name [Name]))
254 => Data (HsWildCardBndrs name thing)
255
256 type LHsSigType name = HsImplicitBndrs name (LHsType name) -- Implicit only
257 type LHsWcType name = HsWildCardBndrs name (LHsType name) -- Wildcard only
258 type LHsSigWcType name = HsImplicitBndrs name (LHsWcType name) -- Both
259
260 -- See Note [Representing type signatures]
261
262 hsImplicitBody :: HsImplicitBndrs name thing -> thing
263 hsImplicitBody (HsIB { hsib_body = body }) = body
264
265 hsSigType :: LHsSigType name -> LHsType name
266 hsSigType = hsImplicitBody
267
268 hsSigWcType :: LHsSigWcType name -> LHsType name
269 hsSigWcType sig_ty = hswc_body (hsib_body sig_ty)
270
271 dropWildCards :: LHsSigWcType name -> LHsSigType name
272 -- Drop the wildcard part of a LHsSigWcType
273 dropWildCards sig_ty = sig_ty { hsib_body = hsSigWcType sig_ty }
274
275 {- Note [Representing type signatures]
276 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277 HsSigType is used to represent an explicit user type signature
278 such as f :: a -> a
279 or g (x :: a -> a) = x
280
281 A HsSigType is just a HsImplicitBndrs wrapping a LHsType.
282 * The HsImplicitBndrs binds the /implicitly/ quantified tyvars
283 * The LHsType binds the /explictly/ quantified tyvars
284
285 E.g. For a signature like
286 f :: forall (a::k). blah
287 we get
288 HsIB { hsib_vars = [k]
289 , hsib_body = HsForAllTy { hst_bndrs = [(a::*)]
290 , hst_body = blah }
291 The implicit kind variable 'k' is bound by the HsIB;
292 the explictly forall'd tyvar 'a' is bounnd by the HsForAllTy
293 -}
294
295 mkHsImplicitBndrs :: thing -> HsImplicitBndrs RdrName thing
296 mkHsImplicitBndrs x = HsIB { hsib_body = x
297 , hsib_vars = PlaceHolder }
298
299 mkHsWildCardBndrs :: thing -> HsWildCardBndrs RdrName thing
300 mkHsWildCardBndrs x = HsWC { hswc_body = x
301 , hswc_wcs = PlaceHolder
302 , hswc_ctx = Nothing }
303
304 -- Add empty binders. This is a bit suspicious; what if
305 -- the wrapped thing had free type variables?
306 mkEmptyImplicitBndrs :: thing -> HsImplicitBndrs Name thing
307 mkEmptyImplicitBndrs x = HsIB { hsib_body = x
308 , hsib_vars = [] }
309
310 mkEmptyWildCardBndrs :: thing -> HsWildCardBndrs Name thing
311 mkEmptyWildCardBndrs x = HsWC { hswc_body = x
312 , hswc_wcs = []
313 , hswc_ctx = Nothing }
314
315
316 --------------------------------------------------
317 -- | These names are used early on to store the names of implicit
318 -- parameters. They completely disappear after type-checking.
319 newtype HsIPName = HsIPName FastString
320 deriving( Eq, Data, Typeable )
321
322 hsIPNameFS :: HsIPName -> FastString
323 hsIPNameFS (HsIPName n) = n
324
325 instance Outputable HsIPName where
326 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
327
328 instance OutputableBndr HsIPName where
329 pprBndr _ n = ppr n -- Simple for now
330 pprInfixOcc n = ppr n
331 pprPrefixOcc n = ppr n
332
333 --------------------------------------------------
334 data HsTyVarBndr name
335 = UserTyVar -- no explicit kinding
336 (Located name)
337 -- See Note [Located RdrNames] in HsExpr
338 | KindedTyVar
339 (Located name)
340 (LHsKind name) -- The user-supplied kind signature
341 -- ^
342 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
343 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
344
345 -- For details on above see note [Api annotations] in ApiAnnotation
346 deriving (Typeable)
347 deriving instance (DataId name) => Data (HsTyVarBndr name)
348
349 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
350 isHsKindedTyVar :: HsTyVarBndr name -> Bool
351 isHsKindedTyVar (UserTyVar {}) = False
352 isHsKindedTyVar (KindedTyVar {}) = True
353
354 -- | Do all type variables in this 'LHsQTyVars' come with kind annotations?
355 hsTvbAllKinded :: LHsQTyVars name -> Bool
356 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvExplicit
357
358 data HsType name
359 = HsForAllTy -- See Note [HsType binders]
360 { hst_bndrs :: [LHsTyVarBndr name] -- Explicit, user-supplied 'forall a b c'
361 , hst_body :: LHsType name -- body type
362 }
363 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
364 -- 'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
365 -- For details on above see note [Api annotations] in ApiAnnotation
366
367 | HsQualTy -- See Note [HsType binders]
368 { hst_ctxt :: LHsContext name -- Context C => blah
369 , hst_body :: LHsType name }
370
371 | HsTyVar (Located name)
372 -- Type variable, type constructor, or data constructor
373 -- see Note [Promotions (HsTyVar)]
374 -- See Note [Located RdrNames] in HsExpr
375 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
376
377 -- For details on above see note [Api annotations] in ApiAnnotation
378
379 | HsAppsTy [HsAppType name] -- Used only before renaming,
380 -- Note [HsAppsTy]
381 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
382
383 | HsAppTy (LHsType name)
384 (LHsType name)
385 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
386
387 -- For details on above see note [Api annotations] in ApiAnnotation
388
389 | HsFunTy (LHsType name) -- function type
390 (LHsType name)
391 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
392
393 -- For details on above see note [Api annotations] in ApiAnnotation
394
395 | HsListTy (LHsType name) -- Element type
396 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
397 -- 'ApiAnnotation.AnnClose' @']'@
398
399 -- For details on above see note [Api annotations] in ApiAnnotation
400
401 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
402 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
403 -- 'ApiAnnotation.AnnClose' @':]'@
404
405 -- For details on above see note [Api annotations] in ApiAnnotation
406
407 | HsTupleTy HsTupleSort
408 [LHsType name] -- Element types (length gives arity)
409 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
410 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
411
412 -- For details on above see note [Api annotations] in ApiAnnotation
413
414 | HsOpTy (LHsType name) (Located name) (LHsType name)
415 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
416
417 -- For details on above see note [Api annotations] in ApiAnnotation
418
419 | HsParTy (LHsType name) -- See Note [Parens in HsSyn] in HsExpr
420 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
421 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
422 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
423 -- 'ApiAnnotation.AnnClose' @')'@
424
425 -- For details on above see note [Api annotations] in ApiAnnotation
426
427 | HsIParamTy HsIPName -- (?x :: ty)
428 (LHsType name) -- Implicit parameters as they occur in contexts
429 -- ^
430 -- > (?x :: ty)
431 --
432 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
433
434 -- For details on above see note [Api annotations] in ApiAnnotation
435
436 | HsEqTy (LHsType name) -- ty1 ~ ty2
437 (LHsType name) -- Always allowed even without TypeOperators, and has special kinding rule
438 -- ^
439 -- > ty1 ~ ty2
440 --
441 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
442
443 -- For details on above see note [Api annotations] in ApiAnnotation
444
445 | HsKindSig (LHsType name) -- (ty :: kind)
446 (LHsKind name) -- A type with a kind signature
447 -- ^
448 -- > (ty :: kind)
449 --
450 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
451 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
452
453 -- For details on above see note [Api annotations] in ApiAnnotation
454
455 | HsSpliceTy (HsSplice name) -- Includes quasi-quotes
456 (PostTc name Kind)
457 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
458 -- 'ApiAnnotation.AnnClose' @')'@
459
460 -- For details on above see note [Api annotations] in ApiAnnotation
461
462 | HsDocTy (LHsType name) LHsDocString -- A documented type
463 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
464
465 -- For details on above see note [Api annotations] in ApiAnnotation
466
467 | HsBangTy HsSrcBang (LHsType name) -- Bang-style type annotations
468 -- ^ - 'ApiAnnotation.AnnKeywordId' :
469 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
470 -- 'ApiAnnotation.AnnClose' @'#-}'@
471 -- 'ApiAnnotation.AnnBang' @\'!\'@
472
473 -- For details on above see note [Api annotations] in ApiAnnotation
474
475 | HsRecTy [LConDeclField name] -- Only in data type declarations
476 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
477 -- 'ApiAnnotation.AnnClose' @'}'@
478
479 -- For details on above see note [Api annotations] in ApiAnnotation
480
481 | HsCoreTy Type -- An escape hatch for tunnelling a *closed*
482 -- Core Type through HsSyn.
483 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
484
485 -- For details on above see note [Api annotations] in ApiAnnotation
486
487 | HsExplicitListTy -- A promoted explicit list
488 (PostTc name Kind) -- See Note [Promoted lists and tuples]
489 [LHsType name]
490 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
491 -- 'ApiAnnotation.AnnClose' @']'@
492
493 -- For details on above see note [Api annotations] in ApiAnnotation
494
495 | HsExplicitTupleTy -- A promoted explicit tuple
496 [PostTc name Kind] -- See Note [Promoted lists and tuples]
497 [LHsType name]
498 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
499 -- 'ApiAnnotation.AnnClose' @')'@
500
501 -- For details on above see note [Api annotations] in ApiAnnotation
502
503 | HsTyLit HsTyLit -- A promoted numeric literal.
504 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
505
506 -- For details on above see note [Api annotations] in ApiAnnotation
507
508 | HsWildCardTy (HsWildCardInfo name) -- A type wildcard
509 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
510
511 -- For details on above see note [Api annotations] in ApiAnnotation
512 deriving (Typeable)
513 deriving instance (DataId name) => Data (HsType name)
514
515 -- Note [Literal source text] in BasicTypes for SourceText fields in
516 -- the following
517 data HsTyLit
518 = HsNumTy SourceText Integer
519 | HsStrTy SourceText FastString
520 deriving (Data, Typeable)
521
522 mkHsOpTy :: LHsType name -> Located name -> LHsType name -> HsType name
523 mkHsOpTy ty1 op ty2 = HsOpTy ty1 op ty2
524
525 data HsWildCardInfo name
526 = AnonWildCard (PostRn name (Located Name))
527 -- A anonymous wild card ('_'). A fresh Name is generated for
528 -- each individual anonymous wildcard during renaming
529 | NamedWildCard (Located name)
530 -- A named wild card ('_a').
531 deriving (Typeable)
532 deriving instance (DataId name) => Data (HsWildCardInfo name)
533
534 data HsAppType name
535 = HsAppInfix (Located name) -- either a symbol or an id in backticks
536 | HsAppPrefix (LHsType name) -- anything else, including things like (+)
537 deriving (Typeable)
538 deriving instance (DataId name) => Data (HsAppType name)
539
540 instance OutputableBndr name => Outputable (HsAppType name) where
541 ppr = ppr_app_ty TopPrec
542
543 {-
544 Note [HsForAllTy tyvar binders]
545 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
546 After parsing:
547 * Implicit => empty
548 Explicit => the variables the user wrote
549
550 After renaming
551 * Implicit => the *type* variables free in the type
552 Explicit => the variables the user wrote (renamed)
553
554 Qualified currently behaves exactly as Implicit,
555 but it is deprecated to use it for implicit quantification.
556 In this case, GHC 7.10 gives a warning; see
557 Note [Context quantification] in RnTypes, and Trac #4426.
558 In GHC 7.12, Qualified will no longer bind variables
559 and this will become an error.
560
561 The kind variables bound in the hsq_implicit field come both
562 a) from the kind signatures on the kind vars (eg k1)
563 b) from the scope of the forall (eg k2)
564 Example: f :: forall (a::k1) b. T a (b::k2)
565
566
567 Note [Unit tuples]
568 ~~~~~~~~~~~~~~~~~~
569 Consider the type
570 type instance F Int = ()
571 We want to parse that "()"
572 as HsTupleTy HsBoxedOrConstraintTuple [],
573 NOT as HsTyVar unitTyCon
574
575 Why? Because F might have kind (* -> Constraint), so we when parsing we
576 don't know if that tuple is going to be a constraint tuple or an ordinary
577 unit tuple. The HsTupleSort flag is specifically designed to deal with
578 that, but it has to work for unit tuples too.
579
580 Note [Promotions (HsTyVar)]
581 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
582 HsTyVar: A name in a type or kind.
583 Here are the allowed namespaces for the name.
584 In a type:
585 Var: not allowed
586 Data: promoted data constructor
587 Tv: type variable
588 TcCls before renamer: type constructor, class constructor, or promoted data constructor
589 TcCls after renamer: type constructor or class constructor
590 In a kind:
591 Var, Data: not allowed
592 Tv: kind variable
593 TcCls: kind constructor or promoted type constructor
594
595 Note [HsAppsTy]
596 ~~~~~~~~~~~~~~~
597 How to parse
598
599 Foo * Int
600
601 ? Is it `(*) Foo Int` or `Foo GHC.Types.* Int`? There's no way to know until renaming.
602 So we just take type expressions like this and put each component in a list, so be
603 sorted out in the renamer. The sorting out is done by RnTypes.mkHsOpTyRn. This means
604 that the parser should never produce HsAppTy or HsOpTy.
605
606 Note [Promoted lists and tuples]
607 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
608 Notice the difference between
609 HsListTy HsExplicitListTy
610 HsTupleTy HsExplicitListTupleTy
611
612 E.g. f :: [Int] HsListTy
613
614 g3 :: T '[] All these use
615 g2 :: T '[True] HsExplicitListTy
616 g1 :: T '[True,False]
617 g1a :: T [True,False] (can omit ' where unambiguous)
618
619 kind of T :: [Bool] -> * This kind uses HsListTy!
620
621 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
622 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
623 a type-level pair of booleans
624 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
625
626 Note [Distinguishing tuple kinds]
627 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
628
629 Apart from promotion, tuples can have one of three different kinds:
630
631 x :: (Int, Bool) -- Regular boxed tuples
632 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
633 g :: (Eq a, Ord a) => a -- Constraint tuples
634
635 For convenience, internally we use a single constructor for all of these,
636 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
637 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
638 because of the #. However, with -XConstraintKinds we can only distinguish
639 between constraint and boxed tuples during type checking, in general. Hence the
640 four constructors of HsTupleSort:
641
642 HsUnboxedTuple -> Produced by the parser
643 HsBoxedTuple -> Certainly a boxed tuple
644 HsConstraintTuple -> Certainly a constraint tuple
645 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
646 tuple. Produced by the parser only,
647 disappears after type checking
648 -}
649
650 data HsTupleSort = HsUnboxedTuple
651 | HsBoxedTuple
652 | HsConstraintTuple
653 | HsBoxedOrConstraintTuple
654 deriving (Data, Typeable)
655
656 type LConDeclField name = Located (ConDeclField name)
657 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
658 -- in a list
659
660 -- For details on above see note [Api annotations] in ApiAnnotation
661 data ConDeclField name -- Record fields have Haddoc docs on them
662 = ConDeclField { cd_fld_names :: [LFieldOcc name],
663 -- ^ See Note [ConDeclField names]
664 cd_fld_type :: LBangType name,
665 cd_fld_doc :: Maybe LHsDocString }
666 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
667
668 -- For details on above see note [Api annotations] in ApiAnnotation
669 deriving (Typeable)
670 deriving instance (DataId name) => Data (ConDeclField name)
671
672
673 type LFieldOcc name = Located (FieldOcc name)
674
675 -- | Represents an *occurrence* of an unambiguous field. We store
676 -- both the 'RdrName' the user originally wrote, and after the
677 -- renamer, the selector function.
678 data FieldOcc name = FieldOcc { rdrNameFieldOcc :: RdrName
679 , selectorFieldOcc :: PostRn name name
680 }
681 deriving Typeable
682 deriving instance Eq (PostRn name name) => Eq (FieldOcc name)
683 deriving instance Ord (PostRn name name) => Ord (FieldOcc name)
684 deriving instance (Data name, Data (PostRn name name)) => Data (FieldOcc name)
685
686 instance Outputable (FieldOcc name) where
687 ppr = ppr . rdrNameFieldOcc
688
689 mkFieldOcc :: RdrName -> FieldOcc RdrName
690 mkFieldOcc rdr = FieldOcc rdr PlaceHolder
691
692
693 -- | Represents an *occurrence* of a field that is potentially
694 -- ambiguous after the renamer, with the ambiguity resolved by the
695 -- typechecker. We always store the 'RdrName' that the user
696 -- originally wrote, and store the selector function after the renamer
697 -- (for unambiguous occurrences) or the typechecker (for ambiguous
698 -- occurrences).
699 --
700 -- See Note [HsRecField and HsRecUpdField] in HsPat and
701 -- Note [Disambiguating record fields] in TcExpr.
702 data AmbiguousFieldOcc name
703 = Unambiguous RdrName (PostRn name name)
704 | Ambiguous RdrName (PostTc name name)
705 deriving (Typeable)
706 deriving instance ( Data name
707 , Data (PostRn name name)
708 , Data (PostTc name name))
709 => Data (AmbiguousFieldOcc name)
710
711 instance Outputable (AmbiguousFieldOcc name) where
712 ppr = ppr . rdrNameAmbiguousFieldOcc
713
714 instance OutputableBndr (AmbiguousFieldOcc name) where
715 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
716 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
717
718 mkAmbiguousFieldOcc :: RdrName -> AmbiguousFieldOcc RdrName
719 mkAmbiguousFieldOcc rdr = Unambiguous rdr PlaceHolder
720
721 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc name -> RdrName
722 rdrNameAmbiguousFieldOcc (Unambiguous rdr _) = rdr
723 rdrNameAmbiguousFieldOcc (Ambiguous rdr _) = rdr
724
725 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc Id -> Id
726 selectorAmbiguousFieldOcc (Unambiguous _ sel) = sel
727 selectorAmbiguousFieldOcc (Ambiguous _ sel) = sel
728
729 unambiguousFieldOcc :: AmbiguousFieldOcc Id -> FieldOcc Id
730 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
731 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
732
733 ambiguousFieldOcc :: FieldOcc name -> AmbiguousFieldOcc name
734 ambiguousFieldOcc (FieldOcc rdr sel) = Unambiguous rdr sel
735
736 {-
737 Note [ConDeclField names]
738 ~~~~~~~~~~~~~~~~~~~~~~~~~
739
740 A ConDeclField contains a list of field occurrences: these always
741 include the field label as the user wrote it. After the renamer, it
742 will additionally contain the identity of the selector function in the
743 second component.
744
745 Due to DuplicateRecordFields, the OccName of the selector function
746 may have been mangled, which is why we keep the original field label
747 separately. For example, when DuplicateRecordFields is enabled
748
749 data T = MkT { x :: Int }
750
751 gives
752
753 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
754 -}
755
756 -----------------------
757 -- A valid type must have a for-all at the top of the type, or of the fn arg
758 -- types
759
760 ---------------------
761 hsWcScopedTvs :: LHsSigWcType Name -> [Name]
762 -- Get the lexically-scoped type variables of a HsSigType
763 -- - the explicitly-given forall'd type variables
764 -- - the implicitly-bound kind variables
765 -- - the named wildcars; see Note [Scoping of named wildcards]
766 -- because they scope in the same way
767 hsWcScopedTvs sig_ty
768 | HsIB { hsib_vars = vars, hsib_body = sig_ty1 } <- sig_ty
769 , HsWC { hswc_wcs = nwcs, hswc_body = sig_ty2 } <- sig_ty1
770 = case sig_ty2 of
771 L _ (HsForAllTy { hst_bndrs = tvs }) -> vars ++ nwcs ++
772 map hsLTyVarName tvs
773 -- include kind variables only if the type is headed by forall
774 -- (this is consistent with GHC 7 behaviour)
775 _ -> nwcs
776
777 hsScopedTvs :: LHsSigType Name -> [Name]
778 -- Same as hsWcScopedTvs, but for a LHsSigType
779 hsScopedTvs sig_ty
780 | HsIB { hsib_vars = vars, hsib_body = sig_ty2 } <- sig_ty
781 , L _ (HsForAllTy { hst_bndrs = tvs }) <- sig_ty2
782 = vars ++ map hsLTyVarName tvs
783 | otherwise
784 = []
785
786 {- Note [Scoping of named wildcards]
787 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
788 Consider
789 f :: _a -> _a
790 f x = let g :: _a -> _a
791 g = ...
792 in ...
793
794 Currently, for better or worse, the "_a" variables are all the same. So
795 although there is no explicit forall, the "_a" scopes over the definition.
796 I don't know if this is a good idea, but there it is.
797 -}
798
799 ---------------------
800 hsTyVarName :: HsTyVarBndr name -> name
801 hsTyVarName (UserTyVar (L _ n)) = n
802 hsTyVarName (KindedTyVar (L _ n) _) = n
803
804 hsLTyVarName :: LHsTyVarBndr name -> name
805 hsLTyVarName = hsTyVarName . unLoc
806
807 hsExplicitLTyVarNames :: LHsQTyVars name -> [name]
808 -- Explicit variables only
809 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
810
811 hsAllLTyVarNames :: LHsQTyVars Name -> [Name]
812 -- All variables
813 hsAllLTyVarNames (HsQTvs { hsq_implicit = kvs, hsq_explicit = tvs })
814 = kvs ++ map hsLTyVarName tvs
815
816 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
817 hsLTyVarLocName = fmap hsTyVarName
818
819 hsLTyVarLocNames :: LHsQTyVars name -> [Located name]
820 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
821
822 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
823 hsLTyVarBndrToType :: LHsTyVarBndr name -> LHsType name
824 hsLTyVarBndrToType = fmap cvt
825 where cvt (UserTyVar n) = HsTyVar n
826 cvt (KindedTyVar (L name_loc n) kind)
827 = HsKindSig (L name_loc (HsTyVar (L name_loc n))) kind
828
829 -- | Convert a LHsTyVarBndrs to a list of types.
830 -- Works on *type* variable only, no kind vars.
831 hsLTyVarBndrsToTypes :: LHsQTyVars name -> [LHsType name]
832 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
833
834 ---------------------
835 mkAnonWildCardTy :: HsType RdrName
836 mkAnonWildCardTy = HsWildCardTy (AnonWildCard PlaceHolder)
837
838 mkNamedWildCardTy :: Located n -> HsType n
839 mkNamedWildCardTy = HsWildCardTy . NamedWildCard
840
841 isAnonWildCard :: HsWildCardInfo name -> Bool
842 isAnonWildCard (AnonWildCard _) = True
843 isAnonWildCard _ = False
844
845 isNamedWildCard :: HsWildCardInfo name -> Bool
846 isNamedWildCard = not . isAnonWildCard
847
848 wildCardName :: HsWildCardInfo Name -> Name
849 wildCardName (NamedWildCard (L _ n)) = n
850 wildCardName (AnonWildCard (L _ n)) = n
851
852 -- Two wild cards are the same when: they're both named and have the same
853 -- name, or they're both anonymous and have the same location.
854 sameWildCard :: Eq name
855 => Located (HsWildCardInfo name)
856 -> Located (HsWildCardInfo name) -> Bool
857 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
858 sameWildCard (L _ (NamedWildCard (L _ n1)))
859 (L _ (NamedWildCard (L _ n2))) = n1 == n2
860 sameWildCard _ _ = False
861
862 sameNamedWildCard :: Eq name
863 => Located (HsWildCardInfo name)
864 -> Located (HsWildCardInfo name) -> Bool
865 sameNamedWildCard (L _ (NamedWildCard (L _ n1)))
866 (L _ (NamedWildCard (L _ n2))) = n1 == n2
867 sameNamedWildCard _ _ = False
868
869 splitHsAppTys :: LHsType Name -> [LHsType Name] -> (LHsType Name, [LHsType Name])
870 -- no need to worry about HsAppsTy here
871 splitHsAppTys (L _ (HsAppTy f a)) as = splitHsAppTys f (a:as)
872 splitHsAppTys (L _ (HsParTy f)) as = splitHsAppTys f as
873 splitHsAppTys f as = (f,as)
874
875 splitLHsPatSynTy :: LHsType name
876 -> ( [LHsTyVarBndr name]
877 , LHsContext name -- Required
878 , LHsContext name -- Provided
879 , LHsType name) -- Body
880 splitLHsPatSynTy ty
881 | L _ (HsQualTy { hst_ctxt = req, hst_body = ty2 }) <- ty1
882 , L _ (HsQualTy { hst_ctxt = prov, hst_body = ty3 }) <- ty2
883 = (tvs, req, prov, ty3)
884
885 | L _ (HsQualTy { hst_ctxt = req, hst_body = ty2 }) <- ty1
886 = (tvs, req, noLoc [], ty2)
887
888 | otherwise
889 = (tvs, noLoc [], noLoc [], ty1)
890 where
891 (tvs, ty1) = splitLHsForAllTy ty
892
893 splitLHsSigmaTy :: LHsType name -> ([LHsTyVarBndr name], LHsContext name, LHsType name)
894 splitLHsSigmaTy ty
895 | (tvs, ty1) <- splitLHsForAllTy ty
896 , (ctxt, ty2) <- splitLHsQualTy ty1
897 = (tvs, ctxt, ty2)
898
899 splitLHsForAllTy :: LHsType name -> ([LHsTyVarBndr name], LHsType name)
900 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
901 splitLHsForAllTy body = ([], body)
902
903 splitLHsQualTy :: LHsType name -> (LHsContext name, LHsType name)
904 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
905 splitLHsQualTy body = (noLoc [], body)
906
907 splitLHsInstDeclTy
908 :: LHsSigType Name
909 -> ([Name], LHsContext Name, LHsType Name)
910 -- Split up an instance decl type, returning the pieces
911 splitLHsInstDeclTy (HsIB { hsib_vars = itkvs
912 , hsib_body = inst_ty })
913 = (itkvs, cxt, body_ty)
914 -- Return implicitly bound type and kind vars
915 -- For an instance decl, all of them are in scope
916 where
917 (cxt, body_ty) = splitLHsQualTy inst_ty
918
919 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
920 -- Breaks up any parens in the result type:
921 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
922 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
923 -- (see Trac #9096)
924 splitHsFunType :: LHsType Name -> ([LHsType Name], LHsType Name)
925 splitHsFunType (L _ (HsParTy ty))
926 = splitHsFunType ty
927
928 splitHsFunType (L _ (HsFunTy x y))
929 | (args, res) <- splitHsFunType y
930 = (x:args, res)
931
932 splitHsFunType orig_ty@(L _ (HsAppTy t1 t2))
933 = go t1 [t2]
934 where -- Look for (->) t1 t2, possibly with parenthesisation
935 go (L _ (HsTyVar (L _ fn))) tys | fn == funTyConName
936 , [t1,t2] <- tys
937 , (args, res) <- splitHsFunType t2
938 = (t1:args, res)
939 go (L _ (HsAppTy t1 t2)) tys = go t1 (t2:tys)
940 go (L _ (HsParTy ty)) tys = go ty tys
941 go _ _ = ([], orig_ty) -- Failure to match
942
943 splitHsFunType other = ([], other)
944
945 ignoreParens :: LHsType name -> LHsType name
946 ignoreParens (L _ (HsParTy ty)) = ignoreParens ty
947 ignoreParens (L _ (HsAppsTy [HsAppPrefix ty])) = ignoreParens ty
948 ignoreParens ty = ty
949
950 {-
951 ************************************************************************
952 * *
953 \subsection{Pretty printing}
954 * *
955 ************************************************************************
956 -}
957
958 instance (OutputableBndr name) => Outputable (HsType name) where
959 ppr ty = pprHsType ty
960
961 instance Outputable HsTyLit where
962 ppr = ppr_tylit
963
964 instance (OutputableBndr name) => Outputable (LHsQTyVars name) where
965 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
966
967 instance (OutputableBndr name) => Outputable (HsTyVarBndr name) where
968 ppr (UserTyVar n) = ppr n
969 ppr (KindedTyVar n k) = parens $ hsep [ppr n, dcolon, ppr k]
970
971 instance (Outputable thing) => Outputable (HsImplicitBndrs name thing) where
972 ppr (HsIB { hsib_body = ty }) = ppr ty
973
974 instance (Outputable thing) => Outputable (HsWildCardBndrs name thing) where
975 ppr (HsWC { hswc_body = ty }) = ppr ty
976
977 instance (Outputable name) => Outputable (HsWildCardInfo name) where
978 ppr (AnonWildCard _) = char '_'
979 ppr (NamedWildCard n) = ppr n
980
981 pprHsForAll :: OutputableBndr name => [LHsTyVarBndr name] -> LHsContext name -> SDoc
982 pprHsForAll = pprHsForAllExtra Nothing
983
984 -- | Version of 'pprHsForAll' that can also print an extra-constraints
985 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
986 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
987 -- containing the location of the extra-constraints wildcard. A special
988 -- function for this is needed, as the extra-constraints wildcard is removed
989 -- from the actual context and type, and stored in a separate field, thus just
990 -- printing the type will not print the extra-constraints wildcard.
991 pprHsForAllExtra :: OutputableBndr name => Maybe SrcSpan -> [LHsTyVarBndr name] -> LHsContext name -> SDoc
992 pprHsForAllExtra extra qtvs cxt
993 = pprHsForAllTvs qtvs <+> pprHsContextExtra show_extra (unLoc cxt)
994 where
995 show_extra = isJust extra
996
997 pprHsForAllTvs :: OutputableBndr name => [LHsTyVarBndr name] -> SDoc
998 pprHsForAllTvs qtvs
999 | show_forall = forAllLit <+> interppSP qtvs <> dot
1000 | otherwise = empty
1001 where
1002 show_forall = opt_PprStyle_Debug || not (null qtvs)
1003
1004 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
1005 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
1006
1007 pprHsContextNoArrow :: (OutputableBndr name) => HsContext name -> SDoc
1008 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
1009
1010 pprHsContextMaybe :: (OutputableBndr name) => HsContext name -> Maybe SDoc
1011 pprHsContextMaybe [] = Nothing
1012 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty FunPrec pred
1013 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
1014
1015 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1016 pprHsContextExtra :: (OutputableBndr name) => Bool -> HsContext name -> SDoc
1017 pprHsContextExtra show_extra ctxt
1018 | not show_extra
1019 = pprHsContext ctxt
1020 | null ctxt
1021 = char '_' <+> darrow
1022 | otherwise
1023 = parens (sep (punctuate comma ctxt')) <+> darrow
1024 where
1025 ctxt' = map ppr ctxt ++ [char '_']
1026
1027 pprConDeclFields :: OutputableBndr name => [LConDeclField name] -> SDoc
1028 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
1029 where
1030 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
1031 cd_fld_doc = doc }))
1032 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
1033 ppr_names [n] = ppr n
1034 ppr_names ns = sep (punctuate comma (map ppr ns))
1035
1036 {-
1037 Note [Printing KindedTyVars]
1038 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1039 Trac #3830 reminded me that we should really only print the kind
1040 signature on a KindedTyVar if the kind signature was put there by the
1041 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
1042 rather than converting to KindedTyVars as before.
1043
1044 (As it happens, the message in #3830 comes out a different way now,
1045 and the problem doesn't show up; but having the flag on a KindedTyVar
1046 seems like the Right Thing anyway.)
1047 -}
1048
1049 -- Printing works more-or-less as for Types
1050
1051 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
1052
1053 pprHsType ty = ppr_mono_ty TopPrec (prepare ty)
1054 pprParendHsType ty = ppr_mono_ty TyConPrec ty
1055
1056 -- Before printing a type, remove outermost HsParTy parens
1057 prepare :: HsType name -> HsType name
1058 prepare (HsParTy ty) = prepare (unLoc ty)
1059 prepare (HsAppsTy [HsAppPrefix (L _ ty)]) = prepare ty
1060 prepare ty = ty
1061
1062 ppr_mono_lty :: (OutputableBndr name) => TyPrec -> LHsType name -> SDoc
1063 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
1064
1065 ppr_mono_ty :: (OutputableBndr name) => TyPrec -> HsType name -> SDoc
1066 ppr_mono_ty ctxt_prec (HsForAllTy { hst_bndrs = tvs, hst_body = ty })
1067 = maybeParen ctxt_prec FunPrec $
1068 sep [pprHsForAllTvs tvs, ppr_mono_lty TopPrec ty]
1069
1070 ppr_mono_ty ctxt_prec (HsQualTy { hst_ctxt = L _ ctxt, hst_body = ty })
1071 = maybeParen ctxt_prec FunPrec $
1072 sep [pprHsContext ctxt, ppr_mono_lty TopPrec ty]
1073
1074 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr_mono_lty TyConPrec ty
1075 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
1076 ppr_mono_ty _ (HsTyVar (L _ name))= pprPrefixOcc name
1077 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
1078 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens std_con (pprWithCommas ppr tys)
1079 where std_con = case con of
1080 HsUnboxedTuple -> UnboxedTuple
1081 _ -> BoxedTuple
1082 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty TopPrec ty <+> dcolon <+> ppr kind)
1083 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty TopPrec ty)
1084 ppr_mono_ty _ (HsPArrTy ty) = paBrackets (ppr_mono_lty TopPrec ty)
1085 ppr_mono_ty prec (HsIParamTy n ty) = maybeParen prec FunPrec (ppr n <+> dcolon <+> ppr_mono_lty TopPrec ty)
1086 ppr_mono_ty _ (HsSpliceTy s _) = pprSplice s
1087 ppr_mono_ty _ (HsCoreTy ty) = ppr ty
1088 ppr_mono_ty _ (HsExplicitListTy _ tys) = quote $ brackets (interpp'SP tys)
1089 ppr_mono_ty _ (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
1090 ppr_mono_ty _ (HsTyLit t) = ppr_tylit t
1091 ppr_mono_ty _ (HsWildCardTy (AnonWildCard _)) = char '_'
1092 ppr_mono_ty _ (HsWildCardTy (NamedWildCard name)) = ppr name
1093
1094 ppr_mono_ty ctxt_prec (HsEqTy ty1 ty2)
1095 = maybeParen ctxt_prec TyOpPrec $
1096 ppr_mono_lty TyOpPrec ty1 <+> char '~' <+> ppr_mono_lty TyOpPrec ty2
1097
1098 ppr_mono_ty ctxt_prec (HsAppsTy tys)
1099 = maybeParen ctxt_prec TyConPrec $
1100 hsep (map (ppr_app_ty TopPrec) tys)
1101
1102 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
1103 = maybeParen ctxt_prec TyConPrec $
1104 hsep [ppr_mono_lty FunPrec fun_ty, ppr_mono_lty TyConPrec arg_ty]
1105
1106 ppr_mono_ty ctxt_prec (HsOpTy ty1 (L _ op) ty2)
1107 = maybeParen ctxt_prec TyOpPrec $
1108 sep [ ppr_mono_lty TyOpPrec ty1
1109 , sep [pprInfixOcc op, ppr_mono_lty TyOpPrec ty2 ] ]
1110
1111 ppr_mono_ty _ (HsParTy ty)
1112 = parens (ppr_mono_lty TopPrec ty)
1113 -- Put the parens in where the user did
1114 -- But we still use the precedence stuff to add parens because
1115 -- toHsType doesn't put in any HsParTys, so we may still need them
1116
1117 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
1118 = maybeParen ctxt_prec TyOpPrec $
1119 ppr_mono_lty TyOpPrec ty <+> ppr (unLoc doc)
1120 -- we pretty print Haddock comments on types as if they were
1121 -- postfix operators
1122
1123 --------------------------
1124 ppr_fun_ty :: (OutputableBndr name) => TyPrec -> LHsType name -> LHsType name -> SDoc
1125 ppr_fun_ty ctxt_prec ty1 ty2
1126 = let p1 = ppr_mono_lty FunPrec ty1
1127 p2 = ppr_mono_lty TopPrec ty2
1128 in
1129 maybeParen ctxt_prec FunPrec $
1130 sep [p1, ptext (sLit "->") <+> p2]
1131
1132 --------------------------
1133 ppr_app_ty :: OutputableBndr name => TyPrec -> HsAppType name -> SDoc
1134 ppr_app_ty _ (HsAppInfix (L _ n)) = pprInfixOcc n
1135 ppr_app_ty _ (HsAppPrefix (L _ (HsTyVar (L _ n)))) = pprPrefixOcc n
1136 ppr_app_ty ctxt (HsAppPrefix ty) = ppr_mono_lty ctxt ty
1137
1138 --------------------------
1139 ppr_tylit :: HsTyLit -> SDoc
1140 ppr_tylit (HsNumTy _ i) = integer i
1141 ppr_tylit (HsStrTy _ s) = text (show s)