1c2d383dbf6bff1dd580d37226a523351ce10f15
[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 :: Located RdrName
679 -- ^ See Note [Located RdrNames] in HsExpr
680 , selectorFieldOcc :: PostRn name name
681 }
682 deriving Typeable
683 deriving instance Eq (PostRn name name) => Eq (FieldOcc name)
684 deriving instance Ord (PostRn name name) => Ord (FieldOcc name)
685 deriving instance (Data name, Data (PostRn name name)) => Data (FieldOcc name)
686
687 instance Outputable (FieldOcc name) where
688 ppr = ppr . rdrNameFieldOcc
689
690 mkFieldOcc :: Located RdrName -> FieldOcc RdrName
691 mkFieldOcc rdr = FieldOcc rdr PlaceHolder
692
693
694 -- | Represents an *occurrence* of a field that is potentially
695 -- ambiguous after the renamer, with the ambiguity resolved by the
696 -- typechecker. We always store the 'RdrName' that the user
697 -- originally wrote, and store the selector function after the renamer
698 -- (for unambiguous occurrences) or the typechecker (for ambiguous
699 -- occurrences).
700 --
701 -- See Note [HsRecField and HsRecUpdField] in HsPat and
702 -- Note [Disambiguating record fields] in TcExpr.
703 -- See Note [Located RdrNames] in HsExpr
704 data AmbiguousFieldOcc name
705 = Unambiguous (Located RdrName) (PostRn name name)
706 | Ambiguous (Located RdrName) (PostTc name name)
707 deriving (Typeable)
708 deriving instance ( Data name
709 , Data (PostRn name name)
710 , Data (PostTc name name))
711 => Data (AmbiguousFieldOcc name)
712
713 instance Outputable (AmbiguousFieldOcc name) where
714 ppr = ppr . rdrNameAmbiguousFieldOcc
715
716 instance OutputableBndr (AmbiguousFieldOcc name) where
717 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
718 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
719
720 mkAmbiguousFieldOcc :: Located RdrName -> AmbiguousFieldOcc RdrName
721 mkAmbiguousFieldOcc rdr = Unambiguous rdr PlaceHolder
722
723 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc name -> RdrName
724 rdrNameAmbiguousFieldOcc (Unambiguous (L _ rdr) _) = rdr
725 rdrNameAmbiguousFieldOcc (Ambiguous (L _ rdr) _) = rdr
726
727 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc Id -> Id
728 selectorAmbiguousFieldOcc (Unambiguous _ sel) = sel
729 selectorAmbiguousFieldOcc (Ambiguous _ sel) = sel
730
731 unambiguousFieldOcc :: AmbiguousFieldOcc Id -> FieldOcc Id
732 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
733 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
734
735 ambiguousFieldOcc :: FieldOcc name -> AmbiguousFieldOcc name
736 ambiguousFieldOcc (FieldOcc rdr sel) = Unambiguous rdr sel
737
738 {-
739 Note [ConDeclField names]
740 ~~~~~~~~~~~~~~~~~~~~~~~~~
741
742 A ConDeclField contains a list of field occurrences: these always
743 include the field label as the user wrote it. After the renamer, it
744 will additionally contain the identity of the selector function in the
745 second component.
746
747 Due to DuplicateRecordFields, the OccName of the selector function
748 may have been mangled, which is why we keep the original field label
749 separately. For example, when DuplicateRecordFields is enabled
750
751 data T = MkT { x :: Int }
752
753 gives
754
755 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
756 -}
757
758 -----------------------
759 -- A valid type must have a for-all at the top of the type, or of the fn arg
760 -- types
761
762 ---------------------
763 hsWcScopedTvs :: LHsSigWcType Name -> [Name]
764 -- Get the lexically-scoped type variables of a HsSigType
765 -- - the explicitly-given forall'd type variables
766 -- - the implicitly-bound kind variables
767 -- - the named wildcars; see Note [Scoping of named wildcards]
768 -- because they scope in the same way
769 hsWcScopedTvs sig_ty
770 | HsIB { hsib_vars = vars, hsib_body = sig_ty1 } <- sig_ty
771 , HsWC { hswc_wcs = nwcs, hswc_body = sig_ty2 } <- sig_ty1
772 = case sig_ty2 of
773 L _ (HsForAllTy { hst_bndrs = tvs }) -> vars ++ nwcs ++
774 map hsLTyVarName tvs
775 -- include kind variables only if the type is headed by forall
776 -- (this is consistent with GHC 7 behaviour)
777 _ -> nwcs
778
779 hsScopedTvs :: LHsSigType Name -> [Name]
780 -- Same as hsWcScopedTvs, but for a LHsSigType
781 hsScopedTvs sig_ty
782 | HsIB { hsib_vars = vars, hsib_body = sig_ty2 } <- sig_ty
783 , L _ (HsForAllTy { hst_bndrs = tvs }) <- sig_ty2
784 = vars ++ map hsLTyVarName tvs
785 | otherwise
786 = []
787
788 {- Note [Scoping of named wildcards]
789 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
790 Consider
791 f :: _a -> _a
792 f x = let g :: _a -> _a
793 g = ...
794 in ...
795
796 Currently, for better or worse, the "_a" variables are all the same. So
797 although there is no explicit forall, the "_a" scopes over the definition.
798 I don't know if this is a good idea, but there it is.
799 -}
800
801 ---------------------
802 hsTyVarName :: HsTyVarBndr name -> name
803 hsTyVarName (UserTyVar (L _ n)) = n
804 hsTyVarName (KindedTyVar (L _ n) _) = n
805
806 hsLTyVarName :: LHsTyVarBndr name -> name
807 hsLTyVarName = hsTyVarName . unLoc
808
809 hsExplicitLTyVarNames :: LHsQTyVars name -> [name]
810 -- Explicit variables only
811 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
812
813 hsAllLTyVarNames :: LHsQTyVars Name -> [Name]
814 -- All variables
815 hsAllLTyVarNames (HsQTvs { hsq_implicit = kvs, hsq_explicit = tvs })
816 = kvs ++ map hsLTyVarName tvs
817
818 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
819 hsLTyVarLocName = fmap hsTyVarName
820
821 hsLTyVarLocNames :: LHsQTyVars name -> [Located name]
822 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
823
824 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
825 hsLTyVarBndrToType :: LHsTyVarBndr name -> LHsType name
826 hsLTyVarBndrToType = fmap cvt
827 where cvt (UserTyVar n) = HsTyVar n
828 cvt (KindedTyVar (L name_loc n) kind)
829 = HsKindSig (L name_loc (HsTyVar (L name_loc n))) kind
830
831 -- | Convert a LHsTyVarBndrs to a list of types.
832 -- Works on *type* variable only, no kind vars.
833 hsLTyVarBndrsToTypes :: LHsQTyVars name -> [LHsType name]
834 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
835
836 ---------------------
837 mkAnonWildCardTy :: HsType RdrName
838 mkAnonWildCardTy = HsWildCardTy (AnonWildCard PlaceHolder)
839
840 mkNamedWildCardTy :: Located n -> HsType n
841 mkNamedWildCardTy = HsWildCardTy . NamedWildCard
842
843 isAnonWildCard :: HsWildCardInfo name -> Bool
844 isAnonWildCard (AnonWildCard _) = True
845 isAnonWildCard _ = False
846
847 isNamedWildCard :: HsWildCardInfo name -> Bool
848 isNamedWildCard = not . isAnonWildCard
849
850 wildCardName :: HsWildCardInfo Name -> Name
851 wildCardName (NamedWildCard (L _ n)) = n
852 wildCardName (AnonWildCard (L _ n)) = n
853
854 -- Two wild cards are the same when: they're both named and have the same
855 -- name, or they're both anonymous and have the same location.
856 sameWildCard :: Eq name
857 => Located (HsWildCardInfo name)
858 -> Located (HsWildCardInfo name) -> Bool
859 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
860 sameWildCard (L _ (NamedWildCard (L _ n1)))
861 (L _ (NamedWildCard (L _ n2))) = n1 == n2
862 sameWildCard _ _ = False
863
864 sameNamedWildCard :: Eq name
865 => Located (HsWildCardInfo name)
866 -> Located (HsWildCardInfo name) -> Bool
867 sameNamedWildCard (L _ (NamedWildCard (L _ n1)))
868 (L _ (NamedWildCard (L _ n2))) = n1 == n2
869 sameNamedWildCard _ _ = False
870
871 splitHsAppTys :: LHsType Name -> [LHsType Name] -> (LHsType Name, [LHsType Name])
872 -- no need to worry about HsAppsTy here
873 splitHsAppTys (L _ (HsAppTy f a)) as = splitHsAppTys f (a:as)
874 splitHsAppTys (L _ (HsParTy f)) as = splitHsAppTys f as
875 splitHsAppTys f as = (f,as)
876
877 splitLHsPatSynTy :: LHsType name
878 -> ( [LHsTyVarBndr name]
879 , LHsContext name -- Required
880 , LHsContext name -- Provided
881 , LHsType name) -- Body
882 splitLHsPatSynTy ty
883 | L _ (HsQualTy { hst_ctxt = req, hst_body = ty2 }) <- ty1
884 , L _ (HsQualTy { hst_ctxt = prov, hst_body = ty3 }) <- ty2
885 = (tvs, req, prov, ty3)
886
887 | L _ (HsQualTy { hst_ctxt = req, hst_body = ty2 }) <- ty1
888 = (tvs, req, noLoc [], ty2)
889
890 | otherwise
891 = (tvs, noLoc [], noLoc [], ty1)
892 where
893 (tvs, ty1) = splitLHsForAllTy ty
894
895 splitLHsSigmaTy :: LHsType name -> ([LHsTyVarBndr name], LHsContext name, LHsType name)
896 splitLHsSigmaTy ty
897 | (tvs, ty1) <- splitLHsForAllTy ty
898 , (ctxt, ty2) <- splitLHsQualTy ty1
899 = (tvs, ctxt, ty2)
900
901 splitLHsForAllTy :: LHsType name -> ([LHsTyVarBndr name], LHsType name)
902 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
903 splitLHsForAllTy body = ([], body)
904
905 splitLHsQualTy :: LHsType name -> (LHsContext name, LHsType name)
906 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
907 splitLHsQualTy body = (noLoc [], body)
908
909 splitLHsInstDeclTy
910 :: LHsSigType Name
911 -> ([Name], LHsContext Name, LHsType Name)
912 -- Split up an instance decl type, returning the pieces
913 splitLHsInstDeclTy (HsIB { hsib_vars = itkvs
914 , hsib_body = inst_ty })
915 = (itkvs, cxt, body_ty)
916 -- Return implicitly bound type and kind vars
917 -- For an instance decl, all of them are in scope
918 where
919 (cxt, body_ty) = splitLHsQualTy inst_ty
920
921 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
922 -- Breaks up any parens in the result type:
923 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
924 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
925 -- (see Trac #9096)
926 splitHsFunType :: LHsType Name -> ([LHsType Name], LHsType Name)
927 splitHsFunType (L _ (HsParTy ty))
928 = splitHsFunType ty
929
930 splitHsFunType (L _ (HsFunTy x y))
931 | (args, res) <- splitHsFunType y
932 = (x:args, res)
933
934 splitHsFunType orig_ty@(L _ (HsAppTy t1 t2))
935 = go t1 [t2]
936 where -- Look for (->) t1 t2, possibly with parenthesisation
937 go (L _ (HsTyVar (L _ fn))) tys | fn == funTyConName
938 , [t1,t2] <- tys
939 , (args, res) <- splitHsFunType t2
940 = (t1:args, res)
941 go (L _ (HsAppTy t1 t2)) tys = go t1 (t2:tys)
942 go (L _ (HsParTy ty)) tys = go ty tys
943 go _ _ = ([], orig_ty) -- Failure to match
944
945 splitHsFunType other = ([], other)
946
947 ignoreParens :: LHsType name -> LHsType name
948 ignoreParens (L _ (HsParTy ty)) = ignoreParens ty
949 ignoreParens (L _ (HsAppsTy [HsAppPrefix ty])) = ignoreParens ty
950 ignoreParens ty = ty
951
952 {-
953 ************************************************************************
954 * *
955 \subsection{Pretty printing}
956 * *
957 ************************************************************************
958 -}
959
960 instance (OutputableBndr name) => Outputable (HsType name) where
961 ppr ty = pprHsType ty
962
963 instance Outputable HsTyLit where
964 ppr = ppr_tylit
965
966 instance (OutputableBndr name) => Outputable (LHsQTyVars name) where
967 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
968
969 instance (OutputableBndr name) => Outputable (HsTyVarBndr name) where
970 ppr (UserTyVar n) = ppr n
971 ppr (KindedTyVar n k) = parens $ hsep [ppr n, dcolon, ppr k]
972
973 instance (Outputable thing) => Outputable (HsImplicitBndrs name thing) where
974 ppr (HsIB { hsib_body = ty }) = ppr ty
975
976 instance (Outputable thing) => Outputable (HsWildCardBndrs name thing) where
977 ppr (HsWC { hswc_body = ty }) = ppr ty
978
979 instance (Outputable name) => Outputable (HsWildCardInfo name) where
980 ppr (AnonWildCard _) = char '_'
981 ppr (NamedWildCard n) = ppr n
982
983 pprHsForAll :: OutputableBndr name => [LHsTyVarBndr name] -> LHsContext name -> SDoc
984 pprHsForAll = pprHsForAllExtra Nothing
985
986 -- | Version of 'pprHsForAll' that can also print an extra-constraints
987 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
988 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
989 -- containing the location of the extra-constraints wildcard. A special
990 -- function for this is needed, as the extra-constraints wildcard is removed
991 -- from the actual context and type, and stored in a separate field, thus just
992 -- printing the type will not print the extra-constraints wildcard.
993 pprHsForAllExtra :: OutputableBndr name => Maybe SrcSpan -> [LHsTyVarBndr name] -> LHsContext name -> SDoc
994 pprHsForAllExtra extra qtvs cxt
995 = pprHsForAllTvs qtvs <+> pprHsContextExtra show_extra (unLoc cxt)
996 where
997 show_extra = isJust extra
998
999 pprHsForAllTvs :: OutputableBndr name => [LHsTyVarBndr name] -> SDoc
1000 pprHsForAllTvs qtvs
1001 | show_forall = forAllLit <+> interppSP qtvs <> dot
1002 | otherwise = empty
1003 where
1004 show_forall = opt_PprStyle_Debug || not (null qtvs)
1005
1006 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
1007 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
1008
1009 pprHsContextNoArrow :: (OutputableBndr name) => HsContext name -> SDoc
1010 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
1011
1012 pprHsContextMaybe :: (OutputableBndr name) => HsContext name -> Maybe SDoc
1013 pprHsContextMaybe [] = Nothing
1014 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty FunPrec pred
1015 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
1016
1017 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1018 pprHsContextExtra :: (OutputableBndr name) => Bool -> HsContext name -> SDoc
1019 pprHsContextExtra show_extra ctxt
1020 | not show_extra
1021 = pprHsContext ctxt
1022 | null ctxt
1023 = char '_' <+> darrow
1024 | otherwise
1025 = parens (sep (punctuate comma ctxt')) <+> darrow
1026 where
1027 ctxt' = map ppr ctxt ++ [char '_']
1028
1029 pprConDeclFields :: OutputableBndr name => [LConDeclField name] -> SDoc
1030 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
1031 where
1032 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
1033 cd_fld_doc = doc }))
1034 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
1035 ppr_names [n] = ppr n
1036 ppr_names ns = sep (punctuate comma (map ppr ns))
1037
1038 {-
1039 Note [Printing KindedTyVars]
1040 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1041 Trac #3830 reminded me that we should really only print the kind
1042 signature on a KindedTyVar if the kind signature was put there by the
1043 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
1044 rather than converting to KindedTyVars as before.
1045
1046 (As it happens, the message in #3830 comes out a different way now,
1047 and the problem doesn't show up; but having the flag on a KindedTyVar
1048 seems like the Right Thing anyway.)
1049 -}
1050
1051 -- Printing works more-or-less as for Types
1052
1053 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
1054
1055 pprHsType ty = ppr_mono_ty TopPrec (prepare ty)
1056 pprParendHsType ty = ppr_mono_ty TyConPrec ty
1057
1058 -- Before printing a type, remove outermost HsParTy parens
1059 prepare :: HsType name -> HsType name
1060 prepare (HsParTy ty) = prepare (unLoc ty)
1061 prepare (HsAppsTy [HsAppPrefix (L _ ty)]) = prepare ty
1062 prepare ty = ty
1063
1064 ppr_mono_lty :: (OutputableBndr name) => TyPrec -> LHsType name -> SDoc
1065 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
1066
1067 ppr_mono_ty :: (OutputableBndr name) => TyPrec -> HsType name -> SDoc
1068 ppr_mono_ty ctxt_prec (HsForAllTy { hst_bndrs = tvs, hst_body = ty })
1069 = maybeParen ctxt_prec FunPrec $
1070 sep [pprHsForAllTvs tvs, ppr_mono_lty TopPrec ty]
1071
1072 ppr_mono_ty ctxt_prec (HsQualTy { hst_ctxt = L _ ctxt, hst_body = ty })
1073 = maybeParen ctxt_prec FunPrec $
1074 sep [pprHsContext ctxt, ppr_mono_lty TopPrec ty]
1075
1076 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr_mono_lty TyConPrec ty
1077 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
1078 ppr_mono_ty _ (HsTyVar (L _ name))= pprPrefixOcc name
1079 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
1080 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens std_con (pprWithCommas ppr tys)
1081 where std_con = case con of
1082 HsUnboxedTuple -> UnboxedTuple
1083 _ -> BoxedTuple
1084 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty TopPrec ty <+> dcolon <+> ppr kind)
1085 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty TopPrec ty)
1086 ppr_mono_ty _ (HsPArrTy ty) = paBrackets (ppr_mono_lty TopPrec ty)
1087 ppr_mono_ty prec (HsIParamTy n ty) = maybeParen prec FunPrec (ppr n <+> dcolon <+> ppr_mono_lty TopPrec ty)
1088 ppr_mono_ty _ (HsSpliceTy s _) = pprSplice s
1089 ppr_mono_ty _ (HsCoreTy ty) = ppr ty
1090 ppr_mono_ty _ (HsExplicitListTy _ tys) = quote $ brackets (interpp'SP tys)
1091 ppr_mono_ty _ (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
1092 ppr_mono_ty _ (HsTyLit t) = ppr_tylit t
1093 ppr_mono_ty _ (HsWildCardTy (AnonWildCard _)) = char '_'
1094 ppr_mono_ty _ (HsWildCardTy (NamedWildCard name)) = ppr name
1095
1096 ppr_mono_ty ctxt_prec (HsEqTy ty1 ty2)
1097 = maybeParen ctxt_prec TyOpPrec $
1098 ppr_mono_lty TyOpPrec ty1 <+> char '~' <+> ppr_mono_lty TyOpPrec ty2
1099
1100 ppr_mono_ty ctxt_prec (HsAppsTy tys)
1101 = maybeParen ctxt_prec TyConPrec $
1102 hsep (map (ppr_app_ty TopPrec) tys)
1103
1104 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
1105 = maybeParen ctxt_prec TyConPrec $
1106 hsep [ppr_mono_lty FunPrec fun_ty, ppr_mono_lty TyConPrec arg_ty]
1107
1108 ppr_mono_ty ctxt_prec (HsOpTy ty1 (L _ op) ty2)
1109 = maybeParen ctxt_prec TyOpPrec $
1110 sep [ ppr_mono_lty TyOpPrec ty1
1111 , sep [pprInfixOcc op, ppr_mono_lty TyOpPrec ty2 ] ]
1112
1113 ppr_mono_ty _ (HsParTy ty)
1114 = parens (ppr_mono_lty TopPrec ty)
1115 -- Put the parens in where the user did
1116 -- But we still use the precedence stuff to add parens because
1117 -- toHsType doesn't put in any HsParTys, so we may still need them
1118
1119 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
1120 = maybeParen ctxt_prec TyOpPrec $
1121 ppr_mono_lty TyOpPrec ty <+> ppr (unLoc doc)
1122 -- we pretty print Haddock comments on types as if they were
1123 -- postfix operators
1124
1125 --------------------------
1126 ppr_fun_ty :: (OutputableBndr name) => TyPrec -> LHsType name -> LHsType name -> SDoc
1127 ppr_fun_ty ctxt_prec ty1 ty2
1128 = let p1 = ppr_mono_lty FunPrec ty1
1129 p2 = ppr_mono_lty TopPrec ty2
1130 in
1131 maybeParen ctxt_prec FunPrec $
1132 sep [p1, ptext (sLit "->") <+> p2]
1133
1134 --------------------------
1135 ppr_app_ty :: OutputableBndr name => TyPrec -> HsAppType name -> SDoc
1136 ppr_app_ty _ (HsAppInfix (L _ n)) = pprInfixOcc n
1137 ppr_app_ty _ (HsAppPrefix (L _ (HsTyVar (L _ n)))) = pprPrefixOcc n
1138 ppr_app_ty ctxt (HsAppPrefix ty) = ppr_mono_lty ctxt ty
1139
1140 --------------------------
1141 ppr_tylit :: HsTyLit -> SDoc
1142 ppr_tylit (HsNumTy _ i) = integer i
1143 ppr_tylit (HsStrTy _ s) = text (show s)