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