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