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