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