Allow TH quoting of assoc type defaults.
[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 LHsTyVarBndrs(..),
24 HsWithBndrs(..),
25 HsTupleSort(..), HsExplicitFlag(..),
26 HsContext, LHsContext,
27 HsTyWrapper(..),
28 HsTyLit(..),
29 HsIPName(..), hsIPNameFS,
30
31 LBangType, BangType,
32 HsSrcBang(..), HsImplBang(..),
33 SrcStrictness(..), SrcUnpackedness(..),
34 getBangType, getBangStrictness,
35
36 ConDeclField(..), LConDeclField, pprConDeclFields,
37
38 HsWildCardInfo(..), mkAnonWildCardTy, mkNamedWildCardTy,
39 wildCardName, sameWildCard, sameNamedWildCard,
40 isAnonWildCard, isNamedWildCard,
41
42 mkHsQTvs, hsQTvBndrs, isHsKindedTyVar, hsTvbAllKinded,
43 mkExplicitHsForAllTy, mkImplicitHsForAllTy, mkQualifiedHsForAllTy,
44 mkHsForAllTy,
45 flattenTopLevelLHsForAllTy,flattenTopLevelHsForAllTy,
46 flattenHsForAllTyKeepAnns,
47 hsExplicitTvs,
48 hsTyVarName, mkHsWithBndrs, hsLKiTyVarNames,
49 hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
50 hsLTyVarBndrsToTypes,
51 splitLHsInstDeclTy_maybe,
52 splitHsClassTy_maybe, splitLHsClassTy_maybe,
53 splitHsFunType,
54 splitHsAppTys, hsTyGetAppHead_maybe, mkHsAppTys, mkHsOpTy,
55 ignoreParens,
56
57 -- Printing
58 pprParendHsType, pprHsForAll, pprHsForAllExtra,
59 pprHsContext, pprHsContextNoArrow, pprHsContextMaybe
60 ) where
61
62 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
63
64 import PlaceHolder ( PostTc,PostRn,DataId,PlaceHolder(..) )
65
66 import Name( Name )
67 import RdrName( RdrName )
68 import DataCon( HsSrcBang(..), HsImplBang(..),
69 SrcStrictness(..), SrcUnpackedness(..) )
70 import TysPrim( funTyConName )
71 import Type
72 import HsDoc
73 import BasicTypes
74 import SrcLoc
75 import StaticFlags
76 import Outputable
77 import FastString
78 import Lexer ( AddAnn, mkParensApiAnn )
79 import Maybes( isJust )
80
81 import Data.Data hiding ( Fixity )
82 import Data.Maybe ( fromMaybe )
83 #if __GLASGOW_HASKELL__ < 709
84 import Data.Monoid hiding ((<>))
85 #endif
86
87 {-
88 ************************************************************************
89 * *
90 \subsection{Bang annotations}
91 * *
92 ************************************************************************
93 -}
94
95 type LBangType name = Located (BangType name)
96 type BangType name = HsType name -- Bangs are in the HsType data type
97
98 getBangType :: LHsType a -> LHsType a
99 getBangType (L _ (HsBangTy _ ty)) = ty
100 getBangType ty = ty
101
102 getBangStrictness :: LHsType a -> HsSrcBang
103 getBangStrictness (L _ (HsBangTy s _)) = s
104 getBangStrictness _ = (HsSrcBang Nothing NoSrcUnpack NoSrcStrict)
105
106 {-
107 ************************************************************************
108 * *
109 \subsection{Data types}
110 * *
111 ************************************************************************
112
113 This is the syntax for types as seen in type signatures.
114
115 Note [HsBSig binder lists]
116 ~~~~~~~~~~~~~~~~~~~~~~~~~~
117 Consider a binder (or pattern) decoarated with a type or kind,
118 \ (x :: a -> a). blah
119 forall (a :: k -> *) (b :: k). blah
120 Then we use a LHsBndrSig on the binder, so that the
121 renamer can decorate it with the variables bound
122 by the pattern ('a' in the first example, 'k' in the second),
123 assuming that neither of them is in scope already
124 See also Note [Kind and type-variable binders] in RnTypes
125 -}
126
127 type LHsContext name = Located (HsContext name)
128 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
129
130 -- For details on above see note [Api annotations] in ApiAnnotation
131
132 type HsContext name = [LHsType name]
133
134 type LHsType name = Located (HsType name)
135 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
136 -- in a list
137
138 -- For details on above see note [Api annotations] in ApiAnnotation
139 type HsKind name = HsType name
140 type LHsKind name = Located (HsKind name)
141 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
142
143 -- For details on above see note [Api annotations] in ApiAnnotation
144
145 --------------------------------------------------
146 -- LHsTyVarBndrs
147 -- The quantified binders in a HsForallTy
148
149 type LHsTyVarBndr name = Located (HsTyVarBndr name)
150
151 data LHsTyVarBndrs name
152 = HsQTvs { hsq_kvs :: [Name] -- Kind variables
153 , hsq_tvs :: [LHsTyVarBndr name] -- Type variables
154 -- See Note [HsForAllTy tyvar binders]
155 }
156 deriving( Typeable )
157 deriving instance (DataId name) => Data (LHsTyVarBndrs name)
158
159 mkHsQTvs :: [LHsTyVarBndr RdrName] -> LHsTyVarBndrs RdrName
160 -- Just at RdrName because in the Name variant we should know just
161 -- what the kind-variable binders are; and we don't
162 -- We put an empty list (rather than a panic) for the kind vars so
163 -- that the pretty printer works ok on them.
164 mkHsQTvs tvs = HsQTvs { hsq_kvs = [], hsq_tvs = tvs }
165
166 emptyHsQTvs :: LHsTyVarBndrs name -- Use only when you know there are no kind binders
167 emptyHsQTvs = HsQTvs { hsq_kvs = [], hsq_tvs = [] }
168
169 hsQTvBndrs :: LHsTyVarBndrs name -> [LHsTyVarBndr name]
170 hsQTvBndrs = hsq_tvs
171
172 instance Monoid (LHsTyVarBndrs name) where
173 mempty = emptyHsQTvs
174 mappend (HsQTvs kvs1 tvs1) (HsQTvs kvs2 tvs2)
175 = HsQTvs (kvs1 ++ kvs2) (tvs1 ++ tvs2)
176
177 ------------------------------------------------
178 -- HsWithBndrs
179 -- Used to quantify the binders of a type in cases
180 -- when a HsForAll isn't appropriate:
181 -- * Patterns in a type/data family instance (HsTyPats)
182 -- * Type of a rule binder (RuleBndr)
183 -- * Pattern type signatures (SigPatIn)
184 -- In the last of these, wildcards can happen, so we must accommodate them
185
186 data HsWithBndrs name thing
187 = HsWB { hswb_cts :: thing -- Main payload (type or list of types)
188 , hswb_kvs :: PostRn name [Name] -- Kind vars
189 , hswb_tvs :: PostRn name [Name] -- Type vars
190 , hswb_wcs :: PostRn name [Name] -- Wild cards
191 }
192 deriving (Typeable)
193 deriving instance (Data name, Data thing, Data (PostRn name [Name]))
194 => Data (HsWithBndrs name thing)
195
196 mkHsWithBndrs :: thing -> HsWithBndrs RdrName thing
197 mkHsWithBndrs x = HsWB { hswb_cts = x, hswb_kvs = PlaceHolder
198 , hswb_tvs = PlaceHolder
199 , hswb_wcs = PlaceHolder }
200
201
202 --------------------------------------------------
203 -- | These names are used early on to store the names of implicit
204 -- parameters. They completely disappear after type-checking.
205 newtype HsIPName = HsIPName FastString
206 deriving( Eq, Data, Typeable )
207
208 hsIPNameFS :: HsIPName -> FastString
209 hsIPNameFS (HsIPName n) = n
210
211 instance Outputable HsIPName where
212 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
213
214 instance OutputableBndr HsIPName where
215 pprBndr _ n = ppr n -- Simple for now
216 pprInfixOcc n = ppr n
217 pprPrefixOcc n = ppr n
218
219 --------------------------------------------------
220 data HsTyVarBndr name
221 = UserTyVar -- no explicit kinding
222 name
223
224 | KindedTyVar
225 (Located name)
226 (LHsKind name) -- The user-supplied kind signature
227 -- ^
228 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
229 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
230
231 -- For details on above see note [Api annotations] in ApiAnnotation
232 deriving (Typeable)
233 deriving instance (DataId name) => Data (HsTyVarBndr name)
234
235 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
236 isHsKindedTyVar :: HsTyVarBndr name -> Bool
237 isHsKindedTyVar (UserTyVar {}) = False
238 isHsKindedTyVar (KindedTyVar {}) = True
239
240 -- | Do all type variables in this 'LHsTyVarBndr' come with kind annotations?
241 hsTvbAllKinded :: LHsTyVarBndrs name -> Bool
242 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvBndrs
243
244 data HsType name
245 = HsForAllTy HsExplicitFlag -- Renamer leaves this flag unchanged, to record the way
246 -- the user wrote it originally, so that the printer can
247 -- print it as the user wrote it
248 (Maybe SrcSpan) -- Indicates whether extra constraints may be inferred.
249 -- When Nothing, no, otherwise the location of the extra-
250 -- constraints wildcard is stored. For instance, for the
251 -- signature (Eq a, _) => a -> a -> Bool, this field would
252 -- be something like (Just 1:8), with 1:8 being line 1,
253 -- column 8.
254 (LHsTyVarBndrs name)
255 (LHsContext name)
256 (LHsType name)
257 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
258 -- 'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
259
260 -- For details on above see note [Api annotations] in ApiAnnotation
261
262 | HsTyVar name -- Type variable, type constructor, or data constructor
263 -- see Note [Promotions (HsTyVar)]
264 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
265
266 -- For details on above see note [Api annotations] in ApiAnnotation
267
268 | HsAppTy (LHsType name)
269 (LHsType name)
270 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
271
272 -- For details on above see note [Api annotations] in ApiAnnotation
273
274 | HsFunTy (LHsType name) -- function type
275 (LHsType name)
276 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
277
278 -- For details on above see note [Api annotations] in ApiAnnotation
279
280 | HsListTy (LHsType name) -- Element type
281 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
282 -- 'ApiAnnotation.AnnClose' @']'@
283
284 -- For details on above see note [Api annotations] in ApiAnnotation
285
286 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
287 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
288 -- 'ApiAnnotation.AnnClose' @':]'@
289
290 -- For details on above see note [Api annotations] in ApiAnnotation
291
292 | HsTupleTy HsTupleSort
293 [LHsType name] -- Element types (length gives arity)
294 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
295 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
296
297 -- For details on above see note [Api annotations] in ApiAnnotation
298
299 | HsOpTy (LHsType name) (LHsTyOp name) (LHsType name)
300 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
301
302 -- For details on above see note [Api annotations] in ApiAnnotation
303
304 | HsParTy (LHsType name) -- See Note [Parens in HsSyn] in HsExpr
305 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
306 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
307 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
308 -- 'ApiAnnotation.AnnClose' @')'@
309
310 -- For details on above see note [Api annotations] in ApiAnnotation
311
312 | HsIParamTy HsIPName -- (?x :: ty)
313 (LHsType name) -- Implicit parameters as they occur in contexts
314 -- ^
315 -- > (?x :: ty)
316 --
317 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
318
319 -- For details on above see note [Api annotations] in ApiAnnotation
320
321 | HsEqTy (LHsType name) -- ty1 ~ ty2
322 (LHsType name) -- Always allowed even without TypeOperators, and has special kinding rule
323 -- ^
324 -- > ty1 ~ ty2
325 --
326 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
327
328 -- For details on above see note [Api annotations] in ApiAnnotation
329
330 | HsKindSig (LHsType name) -- (ty :: kind)
331 (LHsKind name) -- A type with a kind signature
332 -- ^
333 -- > (ty :: kind)
334 --
335 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
336 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
337
338 -- For details on above see note [Api annotations] in ApiAnnotation
339
340 | HsSpliceTy (HsSplice name) -- Includes quasi-quotes
341 (PostTc name Kind)
342 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
343 -- 'ApiAnnotation.AnnClose' @')'@
344
345 -- For details on above see note [Api annotations] in ApiAnnotation
346
347 | HsDocTy (LHsType name) LHsDocString -- A documented type
348 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
349
350 -- For details on above see note [Api annotations] in ApiAnnotation
351
352 | HsBangTy HsSrcBang (LHsType name) -- Bang-style type annotations
353 -- ^ - 'ApiAnnotation.AnnKeywordId' :
354 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
355 -- 'ApiAnnotation.AnnClose' @'#-}'@
356 -- 'ApiAnnotation.AnnBang' @\'!\'@
357
358 -- For details on above see note [Api annotations] in ApiAnnotation
359
360 | HsRecTy [LConDeclField name] -- Only in data type declarations
361 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
362 -- 'ApiAnnotation.AnnClose' @'}'@
363
364 -- For details on above see note [Api annotations] in ApiAnnotation
365
366 | HsCoreTy Type -- An escape hatch for tunnelling a *closed*
367 -- Core Type through HsSyn.
368 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
369
370 -- For details on above see note [Api annotations] in ApiAnnotation
371
372 | HsExplicitListTy -- A promoted explicit list
373 (PostTc name Kind) -- See Note [Promoted lists and tuples]
374 [LHsType name]
375 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
376 -- 'ApiAnnotation.AnnClose' @']'@
377
378 -- For details on above see note [Api annotations] in ApiAnnotation
379
380 | HsExplicitTupleTy -- A promoted explicit tuple
381 [PostTc name Kind] -- See Note [Promoted lists and tuples]
382 [LHsType name]
383 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
384 -- 'ApiAnnotation.AnnClose' @')'@
385
386 -- For details on above see note [Api annotations] in ApiAnnotation
387
388 | HsTyLit HsTyLit -- A promoted numeric literal.
389 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
390
391 -- For details on above see note [Api annotations] in ApiAnnotation
392
393 | HsWrapTy HsTyWrapper (HsType name) -- only in typechecker output
394 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
395
396 -- For details on above see note [Api annotations] in ApiAnnotation
397
398 | HsWildCardTy (HsWildCardInfo name) -- A type wildcard
399 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
400
401 -- For details on above see note [Api annotations] in ApiAnnotation
402 deriving (Typeable)
403 deriving instance (DataId name) => Data (HsType name)
404
405 -- Note [Literal source text] in BasicTypes for SourceText fields in
406 -- the following
407 data HsTyLit
408 = HsNumTy SourceText Integer
409 | HsStrTy SourceText FastString
410 deriving (Data, Typeable)
411
412 data HsTyWrapper
413 = WpKiApps [Kind] -- kind instantiation: [] k1 k2 .. kn
414 deriving (Data, Typeable)
415
416 type LHsTyOp name = HsTyOp (Located name)
417 type HsTyOp name = (HsTyWrapper, name)
418
419 mkHsOpTy :: LHsType name -> Located name -> LHsType name -> HsType name
420 mkHsOpTy ty1 op ty2 = HsOpTy ty1 (WpKiApps [], op) ty2
421
422 data HsWildCardInfo name
423 = AnonWildCard (PostRn name Name)
424 -- A anonymous wild card ('_'). A name is generated during renaming.
425 | NamedWildCard name
426 -- A named wild card ('_a').
427 deriving (Typeable)
428 deriving instance (DataId name) => Data (HsWildCardInfo name)
429
430 {-
431 Note [HsForAllTy tyvar binders]
432 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
433 After parsing:
434 * Implicit => empty
435 Explicit => the variables the user wrote
436
437 After renaming
438 * Implicit => the *type* variables free in the type
439 Explicit => the variables the user wrote (renamed)
440
441 Qualified currently behaves exactly as Implicit,
442 but it is deprecated to use it for implicit quantification.
443 In this case, GHC 7.10 gives a warning; see
444 Note [Context quantification] in RnTypes, and Trac #4426.
445 In GHC 7.12, Qualified will no longer bind variables
446 and this will become an error.
447
448 The kind variables bound in the hsq_kvs field come both
449 a) from the kind signatures on the kind vars (eg k1)
450 b) from the scope of the forall (eg k2)
451 Example: f :: forall (a::k1) b. T a (b::k2)
452
453
454 Note [Unit tuples]
455 ~~~~~~~~~~~~~~~~~~
456 Consider the type
457 type instance F Int = ()
458 We want to parse that "()"
459 as HsTupleTy HsBoxedOrConstraintTuple [],
460 NOT as HsTyVar unitTyCon
461
462 Why? Because F might have kind (* -> Constraint), so we when parsing we
463 don't know if that tuple is going to be a constraint tuple or an ordinary
464 unit tuple. The HsTupleSort flag is specifically designed to deal with
465 that, but it has to work for unit tuples too.
466
467 Note [Promotions (HsTyVar)]
468 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
469 HsTyVar: A name in a type or kind.
470 Here are the allowed namespaces for the name.
471 In a type:
472 Var: not allowed
473 Data: promoted data constructor
474 Tv: type variable
475 TcCls before renamer: type constructor, class constructor, or promoted data constructor
476 TcCls after renamer: type constructor or class constructor
477 In a kind:
478 Var, Data: not allowed
479 Tv: kind variable
480 TcCls: kind constructor or promoted type constructor
481
482
483 Note [Promoted lists and tuples]
484 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
485 Notice the difference between
486 HsListTy HsExplicitListTy
487 HsTupleTy HsExplicitListTupleTy
488
489 E.g. f :: [Int] HsListTy
490
491 g3 :: T '[] All these use
492 g2 :: T '[True] HsExplicitListTy
493 g1 :: T '[True,False]
494 g1a :: T [True,False] (can omit ' where unambiguous)
495
496 kind of T :: [Bool] -> * This kind uses HsListTy!
497
498 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
499 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
500 a type-level pair of booleans
501 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
502
503 Note [Distinguishing tuple kinds]
504 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
505
506 Apart from promotion, tuples can have one of three different kinds:
507
508 x :: (Int, Bool) -- Regular boxed tuples
509 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
510 g :: (Eq a, Ord a) => a -- Constraint tuples
511
512 For convenience, internally we use a single constructor for all of these,
513 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
514 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
515 because of the #. However, with -XConstraintKinds we can only distinguish
516 between constraint and boxed tuples during type checking, in general. Hence the
517 four constructors of HsTupleSort:
518
519 HsUnboxedTuple -> Produced by the parser
520 HsBoxedTuple -> Certainly a boxed tuple
521 HsConstraintTuple -> Certainly a constraint tuple
522 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
523 tuple. Produced by the parser only,
524 disappears after type checking
525 -}
526
527 data HsTupleSort = HsUnboxedTuple
528 | HsBoxedTuple
529 | HsConstraintTuple
530 | HsBoxedOrConstraintTuple
531 deriving (Data, Typeable)
532
533 data HsExplicitFlag
534 = Explicit -- An explicit forall, eg f :: forall a. a-> a
535 | Implicit -- No explicit forall, eg f :: a -> a, or f :: Eq a => a -> a
536 | Qualified -- A *nested* occurrences of (ctxt => ty), with no explicit forall
537 -- e.g. f :: (Eq a => a -> a) -> Int
538 deriving (Data, Typeable)
539
540 type LConDeclField name = Located (ConDeclField name)
541 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
542 -- in a list
543
544 -- For details on above see note [Api annotations] in ApiAnnotation
545 data ConDeclField name -- Record fields have Haddoc docs on them
546 = ConDeclField { cd_fld_names :: [Located name],
547 cd_fld_type :: LBangType name,
548 cd_fld_doc :: Maybe LHsDocString }
549 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
550
551 -- For details on above see note [Api annotations] in ApiAnnotation
552 deriving (Typeable)
553 deriving instance (DataId name) => Data (ConDeclField name)
554
555 -----------------------
556 -- A valid type must have a for-all at the top of the type, or of the fn arg
557 -- types
558
559 mkImplicitHsForAllTy :: LHsType RdrName -> HsType RdrName
560 mkExplicitHsForAllTy :: [LHsTyVarBndr RdrName] -> LHsContext RdrName -> LHsType RdrName -> HsType RdrName
561 mkQualifiedHsForAllTy :: LHsContext RdrName -> LHsType RdrName -> HsType RdrName
562
563 -- | mkImplicitHsForAllTy is called when we encounter
564 -- f :: type
565 -- Wrap around a HsForallTy if one is not there already.
566 mkImplicitHsForAllTy (L _ (HsForAllTy exp extra tvs cxt ty))
567 = HsForAllTy exp' extra tvs cxt ty
568 where
569 exp' = case exp of
570 Qualified -> Implicit
571 -- Qualified is used only for a nested forall,
572 -- this is now top level
573 _ -> exp
574 mkImplicitHsForAllTy ty = mkHsForAllTy Implicit [] (noLoc []) ty
575
576 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
577 mkQualifiedHsForAllTy ctxt ty = mkHsForAllTy Qualified [] ctxt ty
578
579 -- |Smart constructor for HsForAllTy, which populates the extra-constraints
580 -- field if a wildcard is present in the context.
581 mkHsForAllTy :: HsExplicitFlag -> [LHsTyVarBndr RdrName] -> LHsContext RdrName -> LHsType RdrName -> HsType RdrName
582 mkHsForAllTy exp tvs ctxt ty
583 = HsForAllTy exp Nothing (mkHsQTvs tvs) ctxt ty
584
585 -- |When a sigtype is parsed, the type found is wrapped in an Implicit
586 -- HsForAllTy via mkImplicitHsForAllTy, to ensure that a signature always has a
587 -- forall at the outer level. For Api Annotations this nested structure is
588 -- important to ensure that all `forall` and `.` locations are retained. From
589 -- the renamer onwards this structure is flattened, to ease the renaming and
590 -- type checking process.
591 flattenTopLevelLHsForAllTy :: LHsType name -> LHsType name
592 flattenTopLevelLHsForAllTy (L l ty) = L l (flattenTopLevelHsForAllTy ty)
593
594 flattenTopLevelHsForAllTy :: HsType name -> HsType name
595 flattenTopLevelHsForAllTy (HsForAllTy exp extra tvs (L l []) ty)
596 = snd $ mk_forall_ty [] l exp extra tvs ty
597 flattenTopLevelHsForAllTy ty = ty
598
599 flattenHsForAllTyKeepAnns :: HsType name -> ([AddAnn],HsType name)
600 flattenHsForAllTyKeepAnns (HsForAllTy exp extra tvs (L l []) ty)
601 = mk_forall_ty [] l exp extra tvs ty
602 flattenHsForAllTyKeepAnns ty = ([],ty)
603
604 -- mk_forall_ty makes a pure for-all type (no context)
605 mk_forall_ty :: [AddAnn] -> SrcSpan -> HsExplicitFlag -> Maybe SrcSpan
606 -> LHsTyVarBndrs name
607 -> LHsType name -> ([AddAnn],HsType name)
608 mk_forall_ty ann _ exp1 extra1 tvs1 (L _ (HsForAllTy exp2 extra qtvs2 ctxt ty))
609 = (ann,HsForAllTy (exp1 `plus` exp2) (mergeExtra extra1 extra)
610 (tvs1 `mappend` qtvs2) ctxt ty)
611 where
612 -- Bias the merging of extra's to the top level, so that a single
613 -- wildcard context will prevail
614 mergeExtra (Just s) _ = Just s
615 mergeExtra _ e = e
616 mk_forall_ty ann l exp extra tvs (L lp (HsParTy ty))
617 = mk_forall_ty (ann ++ mkParensApiAnn lp) l exp extra tvs ty
618 mk_forall_ty ann l exp extra tvs ty
619 = (ann,HsForAllTy exp extra tvs (L l []) ty)
620 -- Even if tvs is empty, we still make a HsForAll!
621 -- In the Implicit case, this signals the place to do implicit quantification
622 -- In the Explicit case, it prevents implicit quantification
623 -- (see the sigtype production in Parser.y)
624 -- so that (forall. ty) isn't implicitly quantified
625
626 plus :: HsExplicitFlag -> HsExplicitFlag -> HsExplicitFlag
627 Qualified `plus` Qualified = Qualified
628 Explicit `plus` _ = Explicit
629 _ `plus` Explicit = Explicit
630 _ `plus` _ = Implicit
631 -- NB: Implicit `plus` Qualified = Implicit
632 -- so that f :: Eq a => a -> a ends up Implicit
633
634 ---------------------
635 hsExplicitTvs :: LHsType Name -> [Name]
636 -- The explicitly-given forall'd type variables of a HsType
637 hsExplicitTvs (L _ (HsForAllTy Explicit _ tvs _ _)) = hsLKiTyVarNames tvs
638 hsExplicitTvs _ = []
639
640 ---------------------
641 hsTyVarName :: HsTyVarBndr name -> name
642 hsTyVarName (UserTyVar n) = n
643 hsTyVarName (KindedTyVar (L _ n) _) = n
644
645 hsLTyVarName :: LHsTyVarBndr name -> name
646 hsLTyVarName = hsTyVarName . unLoc
647
648 hsLTyVarNames :: LHsTyVarBndrs name -> [name]
649 -- Type variables only
650 hsLTyVarNames qtvs = map hsLTyVarName (hsQTvBndrs qtvs)
651
652 hsLKiTyVarNames :: LHsTyVarBndrs Name -> [Name]
653 -- Kind and type variables
654 hsLKiTyVarNames (HsQTvs { hsq_kvs = kvs, hsq_tvs = tvs })
655 = kvs ++ map hsLTyVarName tvs
656
657 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
658 hsLTyVarLocName = fmap hsTyVarName
659
660 hsLTyVarLocNames :: LHsTyVarBndrs name -> [Located name]
661 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvBndrs qtvs)
662
663 -- | Convert a LHsTyVarBndr to an equivalent LHsType. Used in Template Haskell
664 -- quoting for type family equations.
665 hsLTyVarBndrToType :: LHsTyVarBndr name -> LHsType name
666 hsLTyVarBndrToType = fmap cvt
667 where cvt (UserTyVar n) = HsTyVar n
668 cvt (KindedTyVar (L name_loc n) kind) = HsKindSig (L name_loc (HsTyVar n))
669 kind
670
671 -- | Convert a LHsTyVarBndrs to a list of types. Used in Template Haskell
672 -- quoting for type family equations. Works on *type* variable only, no kind
673 -- vars.
674 hsLTyVarBndrsToTypes :: LHsTyVarBndrs name -> [LHsType name]
675 hsLTyVarBndrsToTypes (HsQTvs { hsq_tvs = tvbs }) = map hsLTyVarBndrToType tvbs
676
677 ---------------------
678 mkAnonWildCardTy :: HsType RdrName
679 mkAnonWildCardTy = HsWildCardTy (AnonWildCard PlaceHolder)
680
681 mkNamedWildCardTy :: n -> HsType n
682 mkNamedWildCardTy = HsWildCardTy . NamedWildCard
683
684 isAnonWildCard :: HsWildCardInfo name -> Bool
685 isAnonWildCard (AnonWildCard _) = True
686 isAnonWildCard _ = False
687
688 isNamedWildCard :: HsWildCardInfo name -> Bool
689 isNamedWildCard = not . isAnonWildCard
690
691 wildCardName :: HsWildCardInfo Name -> Name
692 wildCardName (NamedWildCard n) = n
693 wildCardName (AnonWildCard n) = n
694
695 -- Two wild cards are the same when: they're both named and have the same
696 -- name, or they're both anonymous and have the same location.
697 sameWildCard :: Eq name
698 => Located (HsWildCardInfo name)
699 -> Located (HsWildCardInfo name) -> Bool
700 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
701 sameWildCard (L _ (NamedWildCard n1)) (L _ (NamedWildCard n2)) = n1 == n2
702 sameWildCard _ _ = False
703
704 sameNamedWildCard :: Eq name
705 => Located (HsWildCardInfo name)
706 -> Located (HsWildCardInfo name) -> Bool
707 sameNamedWildCard (L _ (NamedWildCard n1)) (L _ (NamedWildCard n2)) = n1 == n2
708 sameNamedWildCard _ _ = False
709
710 splitHsAppTys :: LHsType n -> [LHsType n] -> (LHsType n, [LHsType n])
711 splitHsAppTys (L _ (HsAppTy f a)) as = splitHsAppTys f (a:as)
712 splitHsAppTys (L _ (HsParTy f)) as = splitHsAppTys f as
713 splitHsAppTys f as = (f,as)
714
715 -- retrieve the name of the "head" of a nested type application
716 -- somewhat like splitHsAppTys, but a little more thorough
717 -- used to examine the result of a GADT-like datacon, so it doesn't handle
718 -- *all* cases (like lists, tuples, (~), etc.)
719 hsTyGetAppHead_maybe :: LHsType n -> Maybe (n, [LHsType n])
720 hsTyGetAppHead_maybe = go []
721 where
722 go tys (L _ (HsTyVar n)) = Just (n, tys)
723 go tys (L _ (HsAppTy l r)) = go (r : tys) l
724 go tys (L _ (HsOpTy l (_, L _ n) r)) = Just (n, l : r : tys)
725 go tys (L _ (HsParTy t)) = go tys t
726 go tys (L _ (HsKindSig t _)) = go tys t
727 go _ _ = Nothing
728
729 mkHsAppTys :: OutputableBndr n => LHsType n -> [LHsType n] -> HsType n
730 mkHsAppTys fun_ty [] = pprPanic "mkHsAppTys" (ppr fun_ty)
731 mkHsAppTys fun_ty (arg_ty:arg_tys)
732 = foldl mk_app (HsAppTy fun_ty arg_ty) arg_tys
733 where
734 mk_app fun arg = HsAppTy (noLoc fun) arg
735 -- Add noLocs for inner nodes of the application;
736 -- they are never used
737
738 splitLHsInstDeclTy_maybe
739 :: LHsType name
740 -> Maybe (LHsTyVarBndrs name, HsContext name, Located name, [LHsType name])
741 -- Split up an instance decl type, returning the pieces
742 splitLHsInstDeclTy_maybe inst_ty = do
743 let (tvs, cxt, ty) = splitLHsForAllTy inst_ty
744 (cls, tys) <- splitLHsClassTy_maybe ty
745 return (tvs, cxt, cls, tys)
746
747 splitLHsForAllTy
748 :: LHsType name
749 -> (LHsTyVarBndrs name, HsContext name, LHsType name)
750 splitLHsForAllTy poly_ty
751 = case unLoc poly_ty of
752 HsParTy ty -> splitLHsForAllTy ty
753 HsForAllTy _ _ tvs cxt ty -> (tvs, unLoc cxt, ty)
754 _ -> (emptyHsQTvs, [], poly_ty)
755 -- The type vars should have been computed by now, even if they were implicit
756
757 splitHsClassTy_maybe :: HsType name -> Maybe (name, [LHsType name])
758 splitHsClassTy_maybe ty = fmap (\(L _ n, tys) -> (n, tys)) $ splitLHsClassTy_maybe (noLoc ty)
759
760 splitLHsClassTy_maybe :: LHsType name -> Maybe (Located name, [LHsType name])
761 --- Watch out.. in ...deriving( Show )... we use this on
762 --- the list of partially applied predicates in the deriving,
763 --- so there can be zero args.
764
765 -- In TcDeriv we also use this to figure out what data type is being
766 -- mentioned in a deriving (Generic (Foo bar baz)) declaration (i.e. "Foo").
767 splitLHsClassTy_maybe ty
768 = checkl ty []
769 where
770 checkl (L l ty) args = case ty of
771 HsTyVar t -> Just (L l t, args)
772 HsAppTy l r -> checkl l (r:args)
773 HsOpTy l (_, tc) r -> checkl (fmap HsTyVar tc) (l:r:args)
774 HsParTy t -> checkl t args
775 HsKindSig ty _ -> checkl ty args
776 _ -> Nothing
777
778 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
779 -- Breaks up any parens in the result type:
780 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
781 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
782 -- (see Trac #9096)
783 splitHsFunType :: LHsType Name -> ([LHsType Name], LHsType Name)
784 splitHsFunType (L _ (HsParTy ty))
785 = splitHsFunType ty
786
787 splitHsFunType (L _ (HsFunTy x y))
788 | (args, res) <- splitHsFunType y
789 = (x:args, res)
790
791 splitHsFunType orig_ty@(L _ (HsAppTy t1 t2))
792 = go t1 [t2]
793 where -- Look for (->) t1 t2, possibly with parenthesisation
794 go (L _ (HsTyVar fn)) tys | fn == funTyConName
795 , [t1,t2] <- tys
796 , (args, res) <- splitHsFunType t2
797 = (t1:args, res)
798 go (L _ (HsAppTy t1 t2)) tys = go t1 (t2:tys)
799 go (L _ (HsParTy ty)) tys = go ty tys
800 go _ _ = ([], orig_ty) -- Failure to match
801
802 splitHsFunType other = ([], other)
803
804 ignoreParens :: LHsType name -> LHsType name
805 ignoreParens (L _ (HsParTy ty)) = ignoreParens ty
806 ignoreParens ty = ty
807
808 {-
809 ************************************************************************
810 * *
811 \subsection{Pretty printing}
812 * *
813 ************************************************************************
814 -}
815
816 instance (OutputableBndr name) => Outputable (HsType name) where
817 ppr ty = pprHsType ty
818
819 instance Outputable HsTyLit where
820 ppr = ppr_tylit
821
822 instance (OutputableBndr name) => Outputable (LHsTyVarBndrs name) where
823 ppr (HsQTvs { hsq_kvs = kvs, hsq_tvs = tvs })
824 = sep [ ifPprDebug $ braces (interppSP kvs), interppSP tvs ]
825
826 instance (OutputableBndr name) => Outputable (HsTyVarBndr name) where
827 ppr (UserTyVar n) = ppr n
828 ppr (KindedTyVar n k) = parens $ hsep [ppr n, dcolon, ppr k]
829
830 instance (Outputable thing) => Outputable (HsWithBndrs name thing) where
831 ppr (HsWB { hswb_cts = ty }) = ppr ty
832
833 instance (Outputable name) => Outputable (HsWildCardInfo name) where
834 ppr (AnonWildCard _) = char '_'
835 ppr (NamedWildCard n) = ppr n
836
837 pprHsForAll :: OutputableBndr name => HsExplicitFlag -> LHsTyVarBndrs name -> LHsContext name -> SDoc
838 pprHsForAll exp = pprHsForAllExtra exp Nothing
839
840 -- | Version of 'pprHsForAll' that can also print an extra-constraints
841 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
842 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
843 -- containing the location of the extra-constraints wildcard. A special
844 -- function for this is needed, as the extra-constraints wildcard is removed
845 -- from the actual context and type, and stored in a separate field, thus just
846 -- printing the type will not print the extra-constraints wildcard.
847 pprHsForAllExtra :: OutputableBndr name => HsExplicitFlag -> Maybe SrcSpan -> LHsTyVarBndrs name -> LHsContext name -> SDoc
848 pprHsForAllExtra exp extra qtvs cxt
849 | show_forall = forall_part <+> pprHsContextExtra show_extra (unLoc cxt)
850 | otherwise = pprHsContextExtra show_extra (unLoc cxt)
851 where
852 show_extra = isJust extra
853 show_forall = opt_PprStyle_Debug
854 || (not (null (hsQTvBndrs qtvs)) && is_explicit)
855 is_explicit = case exp of {Explicit -> True; Implicit -> False; Qualified -> False}
856 forall_part = forAllLit <+> ppr qtvs <> dot
857
858 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
859 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
860
861 pprHsContextNoArrow :: (OutputableBndr name) => HsContext name -> SDoc
862 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
863
864 pprHsContextMaybe :: (OutputableBndr name) => HsContext name -> Maybe SDoc
865 pprHsContextMaybe [] = Nothing
866 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty FunPrec pred
867 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
868
869 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
870 pprHsContextExtra :: (OutputableBndr name) => Bool -> HsContext name -> SDoc
871 pprHsContextExtra False = pprHsContext
872 pprHsContextExtra True
873 = \ctxt -> case ctxt of
874 [] -> char '_' <+> darrow
875 _ -> parens (sep (punctuate comma ctxt')) <+> darrow
876 where ctxt' = map ppr ctxt ++ [char '_']
877
878 pprConDeclFields :: OutputableBndr name => [LConDeclField name] -> SDoc
879 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
880 where
881 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
882 cd_fld_doc = doc }))
883 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
884 ppr_names [n] = ppr n
885 ppr_names ns = sep (punctuate comma (map ppr ns))
886
887 {-
888 Note [Printing KindedTyVars]
889 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
890 Trac #3830 reminded me that we should really only print the kind
891 signature on a KindedTyVar if the kind signature was put there by the
892 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
893 rather than converting to KindedTyVars as before.
894
895 (As it happens, the message in #3830 comes out a different way now,
896 and the problem doesn't show up; but having the flag on a KindedTyVar
897 seems like the Right Thing anyway.)
898 -}
899
900 -- Printing works more-or-less as for Types
901
902 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
903
904 pprHsType ty = getPprStyle $ \sty -> ppr_mono_ty TopPrec (prepare sty ty)
905 pprParendHsType ty = ppr_mono_ty TyConPrec ty
906
907 -- Before printing a type
908 -- (a) Remove outermost HsParTy parens
909 -- (b) Drop top-level for-all type variables in user style
910 -- since they are implicit in Haskell
911 prepare :: PprStyle -> HsType name -> HsType name
912 prepare sty (HsParTy ty) = prepare sty (unLoc ty)
913 prepare _ ty = ty
914
915 ppr_mono_lty :: (OutputableBndr name) => TyPrec -> LHsType name -> SDoc
916 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
917
918 ppr_mono_ty :: (OutputableBndr name) => TyPrec -> HsType name -> SDoc
919 ppr_mono_ty ctxt_prec (HsForAllTy exp extra tvs ctxt ty)
920 = maybeParen ctxt_prec FunPrec $
921 sep [pprHsForAllExtra exp extra tvs ctxt, ppr_mono_lty TopPrec ty]
922
923 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr_mono_lty TyConPrec ty
924 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
925 ppr_mono_ty _ (HsTyVar name) = pprPrefixOcc name
926 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
927 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens std_con (pprWithCommas ppr tys)
928 where std_con = case con of
929 HsUnboxedTuple -> UnboxedTuple
930 _ -> BoxedTuple
931 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty TopPrec ty <+> dcolon <+> ppr kind)
932 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty TopPrec ty)
933 ppr_mono_ty _ (HsPArrTy ty) = paBrackets (ppr_mono_lty TopPrec ty)
934 ppr_mono_ty prec (HsIParamTy n ty) = maybeParen prec FunPrec (ppr n <+> dcolon <+> ppr_mono_lty TopPrec ty)
935 ppr_mono_ty _ (HsSpliceTy s _) = pprSplice s
936 ppr_mono_ty _ (HsCoreTy ty) = ppr ty
937 ppr_mono_ty _ (HsExplicitListTy _ tys) = quote $ brackets (interpp'SP tys)
938 ppr_mono_ty _ (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
939 ppr_mono_ty _ (HsTyLit t) = ppr_tylit t
940 ppr_mono_ty _ (HsWildCardTy (AnonWildCard _)) = char '_'
941 ppr_mono_ty _ (HsWildCardTy (NamedWildCard name)) = ppr name
942
943 ppr_mono_ty ctxt_prec (HsWrapTy (WpKiApps _kis) ty)
944 = ppr_mono_ty ctxt_prec ty
945 -- We are not printing kind applications. If we wanted to do so, we should do
946 -- something like this:
947 {-
948 = go ctxt_prec kis ty
949 where
950 go ctxt_prec [] ty = ppr_mono_ty ctxt_prec ty
951 go ctxt_prec (ki:kis) ty
952 = maybeParen ctxt_prec TyConPrec $
953 hsep [ go FunPrec kis ty
954 , ptext (sLit "@") <> pprParendKind ki ]
955 -}
956
957 ppr_mono_ty ctxt_prec (HsEqTy ty1 ty2)
958 = maybeParen ctxt_prec TyOpPrec $
959 ppr_mono_lty TyOpPrec ty1 <+> char '~' <+> ppr_mono_lty TyOpPrec ty2
960
961 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
962 = maybeParen ctxt_prec TyConPrec $
963 hsep [ppr_mono_lty FunPrec fun_ty, ppr_mono_lty TyConPrec arg_ty]
964
965 ppr_mono_ty ctxt_prec (HsOpTy ty1 (_wrapper, L _ op) ty2)
966 = maybeParen ctxt_prec TyOpPrec $
967 sep [ ppr_mono_lty TyOpPrec ty1
968 , sep [pprInfixOcc op, ppr_mono_lty TyOpPrec ty2 ] ]
969 -- Don't print the wrapper (= kind applications)
970 -- c.f. HsWrapTy
971
972 ppr_mono_ty _ (HsParTy ty)
973 = parens (ppr_mono_lty TopPrec ty)
974 -- Put the parens in where the user did
975 -- But we still use the precedence stuff to add parens because
976 -- toHsType doesn't put in any HsParTys, so we may still need them
977
978 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
979 = maybeParen ctxt_prec TyOpPrec $
980 ppr_mono_lty TyOpPrec ty <+> ppr (unLoc doc)
981 -- we pretty print Haddock comments on types as if they were
982 -- postfix operators
983
984 --------------------------
985 ppr_fun_ty :: (OutputableBndr name) => TyPrec -> LHsType name -> LHsType name -> SDoc
986 ppr_fun_ty ctxt_prec ty1 ty2
987 = let p1 = ppr_mono_lty FunPrec ty1
988 p2 = ppr_mono_lty TopPrec ty2
989 in
990 maybeParen ctxt_prec FunPrec $
991 sep [p1, ptext (sLit "->") <+> p2]
992
993 --------------------------
994 ppr_tylit :: HsTyLit -> SDoc
995 ppr_tylit (HsNumTy _ i) = integer i
996 ppr_tylit (HsStrTy _ s) = text (show s)