1{-# LANGUAGE CPP #-} 2{-# LANGUAGE ConstraintKinds #-} 3{-# LANGUAGE DeriveDataTypeable #-} 4{-# LANGUAGE FlexibleContexts #-} 5{-# LANGUAGE FlexibleInstances #-} 6{-# LANGUAGE ScopedTypeVariables #-} 7{-# LANGUAGE StandaloneDeriving #-} 8{-# LANGUAGE TypeApplications #-} 9{-# LANGUAGE TypeFamilies #-} 10{-# LANGUAGE ViewPatterns #-} 11{-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow] 12 -- in module Language.Haskell.Syntax.Extension 13{- 14(c) The University of Glasgow 2006 15(c) The GRASP/AQUA Project, Glasgow University, 1992-1998 16 17 18GHC.Hs.Type: Abstract syntax: user-defined types 19-} 20 21-- See Note [Language.Haskell.Syntax.* Hierarchy] for why not GHC.Hs.* 22module Language.Haskell.Syntax.Type ( 23 Mult, HsScaled(..), 24 hsMult, hsScaledThing, 25 HsArrow(..), 26 hsLinear, hsUnrestricted, 27 28 HsType(..), HsCoreTy, LHsType, HsKind, LHsKind, 29 HsForAllTelescope(..), HsTyVarBndr(..), LHsTyVarBndr, 30 LHsQTyVars(..), 31 HsOuterTyVarBndrs(..), HsOuterFamEqnTyVarBndrs, HsOuterSigTyVarBndrs, 32 HsWildCardBndrs(..), 33 HsPatSigType(..), HsPSRn(..), 34 HsSigType(..), LHsSigType, LHsSigWcType, LHsWcType, 35 HsTupleSort(..), 36 HsContext, LHsContext, 37 HsTyLit(..), 38 HsIPName(..), hsIPNameFS, 39 HsArg(..), numVisibleArgs, 40 LHsTypeArg, 41 42 LBangType, BangType, 43 HsSrcBang(..), HsImplBang(..), 44 SrcStrictness(..), SrcUnpackedness(..), 45 46 ConDeclField(..), LConDeclField, 47 48 HsConDetails(..), noTypeArgs, 49 50 FieldOcc(..), LFieldOcc, 51 AmbiguousFieldOcc(..), 52 53 mapHsOuterImplicit, 54 hsQTvExplicit, 55 isHsKindedTyVar, 56 hsPatSigType, 57 ) where 58 59#include "GhclibHsVersions.h" 60 61import GHC.Prelude 62 63import {-# SOURCE #-} Language.Haskell.Syntax.Expr ( HsSplice ) 64 65import Language.Haskell.Syntax.Extension 66 67import GHC.Types.SourceText 68import GHC.Types.Name( Name ) 69import GHC.Types.Name.Reader ( RdrName ) 70import GHC.Core.DataCon( HsSrcBang(..), HsImplBang(..), 71 SrcStrictness(..), SrcUnpackedness(..) ) 72import GHC.Core.Type 73import GHC.Hs.Doc 74import GHC.Types.Basic 75import GHC.Types.SrcLoc 76import GHC.Utils.Outputable 77import GHC.Data.FastString 78import GHC.Utils.Misc ( count ) 79import GHC.Parser.Annotation 80 81import Data.Data hiding ( Fixity, Prefix, Infix ) 82import Data.Void 83 84{- 85************************************************************************ 86* * 87\subsection{Bang annotations} 88* * 89************************************************************************ 90-} 91 92-- | Located Bang Type 93type LBangType pass = XRec pass (BangType pass) 94 95-- | Bang Type 96-- 97-- In the parser, strictness and packedness annotations bind more tightly 98-- than docstrings. This means that when consuming a 'BangType' (and looking 99-- for 'HsBangTy') we must be ready to peer behind a potential layer of 100-- 'HsDocTy'. See #15206 for motivation and 'getBangType' for an example. 101type BangType pass = HsType pass -- Bangs are in the HsType data type 102 103{- 104************************************************************************ 105* * 106\subsection{Data types} 107* * 108************************************************************************ 109 110This is the syntax for types as seen in type signatures. 111 112Note [HsBSig binder lists] 113~~~~~~~~~~~~~~~~~~~~~~~~~~ 114Consider a binder (or pattern) decorated with a type or kind, 115 \ (x :: a -> a). blah 116 forall (a :: k -> *) (b :: k). blah 117Then we use a LHsBndrSig on the binder, so that the 118renamer can decorate it with the variables bound 119by the pattern ('a' in the first example, 'k' in the second), 120assuming that neither of them is in scope already 121See also Note [Kind and type-variable binders] in GHC.Rename.HsType 122 123Note [HsType binders] 124~~~~~~~~~~~~~~~~~~~~~ 125The system for recording type and kind-variable binders in HsTypes 126is a bit complicated. Here's how it works. 127 128* In a HsType, 129 HsForAllTy represents an /explicit, user-written/ 'forall' that 130 is nested within another HsType 131 e.g. forall a b. {...} or 132 forall a b -> {...} 133 134 Note that top-level 'forall's are represented with a 135 different AST form. See the description of HsOuterTyVarBndrs 136 below. 137 HsQualTy represents an /explicit, user-written/ context 138 e.g. (Eq a, Show a) => ... 139 The context can be empty if that's what the user wrote 140 These constructors represent what the user wrote, no more 141 and no less. 142 143* The ForAllTelescope field of HsForAllTy represents whether a forall is 144 invisible (e.g., forall a b. {...}, with a dot) or visible 145 (e.g., forall a b -> {...}, with an arrow). 146 147* HsTyVarBndr describes a quantified type variable written by the 148 user. For example 149 f :: forall a (b :: *). blah 150 here 'a' and '(b::*)' are each a HsTyVarBndr. A HsForAllTy has 151 a list of LHsTyVarBndrs. 152 153* HsOuterTyVarBndrs is used to represent the outermost quantified type 154 variables in a type that obeys the forall-or-nothing rule. An 155 HsOuterTyVarBndrs can be one of the following: 156 157 HsOuterImplicit (implicit quantification, added by renamer) 158 f :: a -> a -- Desugars to f :: forall {a}. a -> a 159 HsOuterExplicit (explicit user quantifiation): 160 f :: forall a. a -> a 161 162 See Note [forall-or-nothing rule]. 163 164* An HsSigType is an LHsType with an accompanying HsOuterTyVarBndrs that 165 represents the presence (or absence) of its outermost 'forall'. 166 See Note [Representing type signatures]. 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* HsSigPatType describes types that appear in pattern signatures and 174 the signatures of term-level binders in RULES. Like 175 HsWildCardBndrs/HsOuterTyVarBndrs, they track the names of wildcard 176 variables and implicitly bound type variables. Unlike 177 HsOuterTyVarBndrs, however, HsSigPatTypes do not obey the 178 forall-or-nothing rule. See Note [Pattern signature binders and scoping]. 179 180* The explicit presence of these wrappers specifies, in the HsSyn, 181 exactly where implicit quantification is allowed, and where 182 wildcards are allowed. 183 184* LHsQTyVars is used in data/class declarations, where the user gives 185 explicit *type* variable bindings, but we need to implicitly bind 186 *kind* variables. For example 187 class C (a :: k -> *) where ... 188 The 'k' is implicitly bound in the hsq_tvs field of LHsQTyVars 189 190Note [The wildcard story for types] 191~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 192Types can have wildcards in them, to support partial type signatures, 193like f :: Int -> (_ , _a) -> _a 194 195A wildcard in a type can be 196 197 * An anonymous wildcard, 198 written '_' 199 In HsType this is represented by HsWildCardTy. 200 The renamer leaves it untouched, and it is later given a fresh 201 meta tyvar in the typechecker. 202 203 * A named wildcard, 204 written '_a', '_foo', etc 205 In HsType this is represented by (HsTyVar "_a") 206 i.e. a perfectly ordinary type variable that happens 207 to start with an underscore 208 209Note carefully: 210 211* When NamedWildCards is off, type variables that start with an 212 underscore really /are/ ordinary type variables. And indeed, even 213 when NamedWildCards is on you can bind _a explicitly as an ordinary 214 type variable: 215 data T _a _b = MkT _b _a 216 Or even: 217 f :: forall _a. _a -> _b 218 Here _a is an ordinary forall'd binder, but (With NamedWildCards) 219 _b is a named wildcard. (See the comments in #10982) 220 221* Named wildcards are bound by the HsWildCardBndrs (for types that obey the 222 forall-or-nothing rule) and HsPatSigType (for type signatures in patterns 223 and term-level binders in RULES), which wrap types that are allowed to have 224 wildcards. Unnamed wildcards, however are left unchanged until typechecking, 225 where we give them fresh wild tyvars and determine whether or not to emit 226 hole constraints on each wildcard (we don't if it's a visible type/kind 227 argument or a type family pattern). See related notes 228 Note [Wildcards in visible kind application] and 229 Note [Wildcards in visible type application] in GHC.Tc.Gen.HsType. 230 231* After type checking is done, we report what types the wildcards 232 got unified with. 233 234Note [Ordering of implicit variables] 235~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 236Since the advent of -XTypeApplications, GHC makes promises about the ordering 237of implicit variable quantification. Specifically, we offer that implicitly 238quantified variables (such as those in const :: a -> b -> a, without a `forall`) 239will occur in left-to-right order of first occurrence. Here are a few examples: 240 241 const :: a -> b -> a -- forall a b. ... 242 f :: Eq a => b -> a -> a -- forall a b. ... contexts are included 243 244 type a <-< b = b -> a 245 g :: a <-< b -- forall a b. ... type synonyms matter 246 247 class Functor f where 248 fmap :: (a -> b) -> f a -> f b -- forall f a b. ... 249 -- The f is quantified by the class, so only a and b are considered in fmap 250 251This simple story is complicated by the possibility of dependency: all variables 252must come after any variables mentioned in their kinds. 253 254 typeRep :: Typeable a => TypeRep (a :: k) -- forall k a. ... 255 256The k comes first because a depends on k, even though the k appears later than 257the a in the code. Thus, GHC does a *stable topological sort* on the variables. 258By "stable", we mean that any two variables who do not depend on each other 259preserve their existing left-to-right ordering. 260 261Implicitly bound variables are collected by the extract- family of functions 262(extractHsTysRdrTyVars, extractHsTyVarBndrsKVs, etc.) in GHC.Rename.HsType. 263These functions thus promise to keep left-to-right ordering. 264Look for pointers to this note to see the places where the action happens. 265 266Note that we also maintain this ordering in kind signatures. Even though 267there's no visible kind application (yet), having implicit variables be 268quantified in left-to-right order in kind signatures is nice since: 269 270* It's consistent with the treatment for type signatures. 271* It can affect how types are displayed with -fprint-explicit-kinds (see 272 #15568 for an example), which is a situation where knowing the order in 273 which implicit variables are quantified can be useful. 274* In the event that visible kind application is implemented, the order in 275 which we would expect implicit variables to be ordered in kinds will have 276 already been established. 277-} 278 279-- | Located Haskell Context 280type LHsContext pass = XRec pass (HsContext pass) 281 -- ^ 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnUnit' 282 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 283 284-- | Haskell Context 285type HsContext pass = [LHsType pass] 286 287-- | Located Haskell Type 288type LHsType pass = XRec pass (HsType pass) 289 -- ^ May have 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnComma' when 290 -- in a list 291 292 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 293 294-- | Haskell Kind 295type HsKind pass = HsType pass 296 297-- | Located Haskell Kind 298type LHsKind pass = XRec pass (HsKind pass) 299 -- ^ 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnDcolon' 300 301 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 302 303-------------------------------------------------- 304-- LHsQTyVars 305-- The explicitly-quantified binders in a data/type declaration 306 307-- | The type variable binders in an 'HsForAllTy'. 308-- See also @Note [Variable Specificity and Forall Visibility]@ in 309-- "GHC.Tc.Gen.HsType". 310data HsForAllTelescope pass 311 = HsForAllVis -- ^ A visible @forall@ (e.g., @forall a -> {...}@). 312 -- These do not have any notion of specificity, so we use 313 -- '()' as a placeholder value. 314 { hsf_xvis :: XHsForAllVis pass 315 , hsf_vis_bndrs :: [LHsTyVarBndr () pass] 316 } 317 | HsForAllInvis -- ^ An invisible @forall@ (e.g., @forall a {b} c. {...}@), 318 -- where each binder has a 'Specificity'. 319 { hsf_xinvis :: XHsForAllInvis pass 320 , hsf_invis_bndrs :: [LHsTyVarBndr Specificity pass] 321 } 322 | XHsForAllTelescope !(XXHsForAllTelescope pass) 323 324-- | Located Haskell Type Variable Binder 325type LHsTyVarBndr flag pass = XRec pass (HsTyVarBndr flag pass) 326 -- See Note [HsType binders] 327 328-- | Located Haskell Quantified Type Variables 329data LHsQTyVars pass -- See Note [HsType binders] 330 = HsQTvs { hsq_ext :: XHsQTvs pass 331 332 , hsq_explicit :: [LHsTyVarBndr () pass] 333 -- Explicit variables, written by the user 334 } 335 | XLHsQTyVars !(XXLHsQTyVars pass) 336 337hsQTvExplicit :: LHsQTyVars pass -> [LHsTyVarBndr () pass] 338hsQTvExplicit = hsq_explicit 339 340------------------------------------------------ 341-- HsOuterTyVarBndrs 342-- Used to quantify the outermost type variable binders of a type that obeys 343-- the forall-or-nothing rule. These are used to represent the outermost 344-- quantification in: 345-- * Type signatures (LHsSigType/LHsSigWcType) 346-- * Patterns in a type/data family instance (HsTyPats) 347-- 348-- We support two forms: 349-- HsOuterImplicit (implicit quantification, added by renamer) 350-- f :: a -> a -- Desugars to f :: forall {a}. a -> a 351-- type instance F (a,b) = a->b 352-- HsOuterExplicit (explicit user quantifiation): 353-- f :: forall a. a -> a 354-- type instance forall a b. F (a,b) = a->b 355-- 356-- In constrast, when the user writes /visible/ quanitification 357-- T :: forall k -> k -> Type 358-- we use use HsOuterImplicit, wrapped around a HsForAllTy 359-- for the visible quantification 360-- 361-- See Note [forall-or-nothing rule] 362 363-- | The outermost type variables in a type that obeys the @forall@-or-nothing 364-- rule. See @Note [forall-or-nothing rule]@. 365data HsOuterTyVarBndrs flag pass 366 = HsOuterImplicit -- ^ Implicit forall, e.g., 367 -- @f :: a -> b -> b@ 368 { hso_ximplicit :: XHsOuterImplicit pass 369 } 370 | HsOuterExplicit -- ^ Explicit forall, e.g., 371 -- @f :: forall a b. a -> b -> b@ 372 { hso_xexplicit :: XHsOuterExplicit pass flag 373 , hso_bndrs :: [LHsTyVarBndr flag (NoGhcTc pass)] 374 } 375 | XHsOuterTyVarBndrs !(XXHsOuterTyVarBndrs pass) 376 377-- | Used for signatures, e.g., 378-- 379-- @ 380-- f :: forall a {b}. blah 381-- @ 382-- 383-- We use 'Specificity' for the 'HsOuterTyVarBndrs' @flag@ to allow 384-- distinguishing between specified and inferred type variables. 385type HsOuterSigTyVarBndrs = HsOuterTyVarBndrs Specificity 386 387-- | Used for type-family instance equations, e.g., 388-- 389-- @ 390-- type instance forall a. F [a] = Tree a 391-- @ 392-- 393-- The notion of specificity is irrelevant in type family equations, so we use 394-- @()@ for the 'HsOuterTyVarBndrs' @flag@. 395type HsOuterFamEqnTyVarBndrs = HsOuterTyVarBndrs () 396 397-- | Haskell Wildcard Binders 398data HsWildCardBndrs pass thing 399 -- See Note [HsType binders] 400 -- See Note [The wildcard story for types] 401 = HsWC { hswc_ext :: XHsWC pass thing 402 -- after the renamer 403 -- Wild cards, only named 404 -- See Note [Wildcards in visible kind application] 405 406 , hswc_body :: thing 407 -- Main payload (type or list of types) 408 -- If there is an extra-constraints wildcard, 409 -- it's still there in the hsc_body. 410 } 411 | XHsWildCardBndrs !(XXHsWildCardBndrs pass thing) 412 413-- | Types that can appear in pattern signatures, as well as the signatures for 414-- term-level binders in RULES. 415-- See @Note [Pattern signature binders and scoping]@. 416-- 417-- This is very similar to 'HsSigWcType', but with 418-- slightly different semantics: see @Note [HsType binders]@. 419-- See also @Note [The wildcard story for types]@. 420data HsPatSigType pass 421 = HsPS { hsps_ext :: XHsPS pass -- ^ After renamer: 'HsPSRn' 422 , hsps_body :: LHsType pass -- ^ Main payload (the type itself) 423 } 424 | XHsPatSigType !(XXHsPatSigType pass) 425 426-- | The extension field for 'HsPatSigType', which is only used in the 427-- renamer onwards. See @Note [Pattern signature binders and scoping]@. 428data HsPSRn = HsPSRn 429 { hsps_nwcs :: [Name] -- ^ Wildcard names 430 , hsps_imp_tvs :: [Name] -- ^ Implicitly bound variable names 431 } 432 deriving Data 433 434-- | Located Haskell Signature Type 435type LHsSigType pass = XRec pass (HsSigType pass) -- Implicit only 436 437-- | Located Haskell Wildcard Type 438type LHsWcType pass = HsWildCardBndrs pass (LHsType pass) -- Wildcard only 439 440-- | Located Haskell Signature Wildcard Type 441type LHsSigWcType pass = HsWildCardBndrs pass (LHsSigType pass) -- Both 442 443-- | A type signature that obeys the @forall@-or-nothing rule. In other 444-- words, an 'LHsType' that uses an 'HsOuterSigTyVarBndrs' to represent its 445-- outermost type variable quantification. 446-- See @Note [Representing type signatures]@. 447data HsSigType pass 448 = HsSig { sig_ext :: XHsSig pass 449 , sig_bndrs :: HsOuterSigTyVarBndrs pass 450 , sig_body :: LHsType pass 451 } 452 | XHsSigType !(XXHsSigType pass) 453 454hsPatSigType :: HsPatSigType pass -> LHsType pass 455hsPatSigType = hsps_body 456 457{- 458Note [forall-or-nothing rule] 459~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 460Free variables in signatures are usually bound in an implicit 'forall' at the 461beginning of user-written signatures. However, if the signature has an 462explicit, invisible forall at the beginning, this is disabled. This is referred 463to as the forall-or-nothing rule. 464 465The idea is nested foralls express something which is only expressible 466explicitly, while a top level forall could (usually) be replaced with an 467implicit binding. Top-level foralls alone ("forall.") are therefore an 468indication that the user is trying to be fastidious, so we don't implicitly 469bind any variables. 470 471Note that this rule only applies to outermost /in/visible 'forall's, and not 472outermost visible 'forall's. See #18660 for more on this point. 473 474Here are some concrete examples to demonstrate the forall-or-nothing rule in 475action: 476 477 type F1 :: a -> b -> b -- Legal; a,b are implicitly quantified. 478 -- Equivalently: forall a b. a -> b -> b 479 480 type F2 :: forall a b. a -> b -> b -- Legal; explicitly quantified 481 482 type F3 :: forall a. a -> b -> b -- Illegal; the forall-or-nothing rule says that 483 -- if you quantify a, you must also quantify b 484 485 type F4 :: forall a -> b -> b -- Legal; the top quantifier (forall a) is a /visible/ 486 -- quantifer, so the "nothing" part of the forall-or-nothing 487 -- rule applies, and b is therefore implicitly quantified. 488 -- Equivalently: forall b. forall a -> b -> b 489 490 type F5 :: forall b. forall a -> b -> c -- Illegal; the forall-or-nothing rule says that 491 -- if you quantify b, you must also quantify c 492 493 type F6 :: forall a -> forall b. b -> c -- Legal: just like F4. 494 495For a complete list of all places where the forall-or-nothing rule applies, see 496"The `forall`-or-nothing rule" section of the GHC User's Guide. 497 498Any type that obeys the forall-or-nothing rule is represented in the AST with 499an HsOuterTyVarBndrs: 500 501* If the type has an outermost, invisible 'forall', it uses HsOuterExplicit, 502 which contains a list of the explicitly quantified type variable binders in 503 `hso_bndrs`. After typechecking, HsOuterExplicit also stores a list of the 504 explicitly quantified `InvisTVBinder`s in 505 `hso_xexplicit :: XHsOuterExplicit GhcTc`. 506 507* Otherwise, it uses HsOuterImplicit. HsOuterImplicit is used for different 508 things depending on the phase: 509 510 * After parsing, it does not store anything in particular. 511 * After renaming, it stores the implicitly bound type variable `Name`s in 512 `hso_ximplicit :: XHsOuterImplicit GhcRn`. 513 * After typechecking, it stores the implicitly bound `TyVar`s in 514 `hso_ximplicit :: XHsOuterImplicit GhcTc`. 515 516 NB: this implicit quantification is purely lexical: we bind any 517 type or kind variables that are not in scope. The type checker 518 may subsequently quantify over further kind variables. 519 See Note [Binding scoped type variables] in GHC.Tc.Gen.Sig. 520 521HsOuterTyVarBndrs GhcTc is used in the typechecker as an intermediate data type 522for storing the outermost TyVars/InvisTVBinders in a type. 523See GHC.Tc.Gen.HsType.bindOuterTKBndrsX for an example of this. 524 525Note [Representing type signatures] 526~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 527HsSigType is used to represent an explicit user type signature. These are 528used in a variety of places. Some examples include: 529 530* Type signatures (e.g., f :: a -> a) 531* Standalone kind signatures (e.g., type G :: a -> a) 532* GADT constructor types (e.g., data T where MkT :: a -> T) 533 534A HsSigType is the combination of an HsOuterSigTyVarBndrs and an LHsType: 535 536* The HsOuterSigTyVarBndrs binds the /explicitly/ quantified type variables 537 when the type signature has an outermost, user-written 'forall' (i.e, 538 the HsOuterExplicit constructor is used). If there is no outermost 'forall', 539 then it binds the /implicitly/ quantified type variables instead (i.e., 540 the HsOuterImplicit constructor is used). 541* The LHsType represents the rest of the type. 542 543E.g. For a signature like 544 f :: forall k (a::k). blah 545we get 546 HsSig { sig_bndrs = HsOuterExplicit { hso_bndrs = [k, (a :: k)] } 547 , sig_body = blah } 548 549Note [Pattern signature binders and scoping] 550~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 551Consider the pattern signatures like those on `t` and `g` in: 552 553 f = let h = \(t :: (b, b) -> 554 \(g :: forall a. a -> b) -> 555 ...(t :: (Int,Int))... 556 in woggle 557 558* The `b` in t's pattern signature is implicitly bound and scopes over 559 the signature and the body of the lambda. It stands for a type (any type); 560 indeed we subsequently discover that b=Int. 561 (See Note [TyVarTv] in GHC.Tc.Utils.TcMType for more on this point.) 562* The `b` in g's pattern signature is an /occurrence/ of the `b` bound by 563 t's pattern signature. 564* The `a` in `forall a` scopes only over the type `a -> b`, not over the body 565 of the lambda. 566* There is no forall-or-nothing rule for pattern signatures, which is why the 567 type `forall a. a -> b` is permitted in `g`'s pattern signature, even though 568 `b` is not explicitly bound. See Note [forall-or-nothing rule]. 569 570Similar scoping rules apply to term variable binders in RULES, like in the 571following example: 572 573 {-# RULES "h" forall (t :: (b, b)) (g :: forall a. a -> b). h t g = ... #-} 574 575Just like in pattern signatures, the `b` in t's signature is implicitly bound 576and scopes over the remainder of the RULE. As a result, the `b` in g's 577signature is an occurrence. Moreover, the `a` in `forall a` scopes only over 578the type `a -> b`, and the forall-or-nothing rule does not apply. 579 580While quite similar, RULE term binder signatures behave slightly differently 581from pattern signatures in two ways: 582 5831. Unlike in pattern signatures, where type variables can stand for any type, 584 type variables in RULE term binder signatures are skolems. 585 See Note [Typechecking pattern signature binders] in GHC.Tc.Gen.HsType for 586 more on this point. 587 588 In this sense, type variables in pattern signatures are quite similar to 589 named wildcards, as both can refer to arbitrary types. The main difference 590 lies in error reporting: if a named wildcard `_a` in a pattern signature 591 stands for Int, then by default GHC will emit a warning stating as much. 592 Changing `_a` to `a`, on the other hand, will cause it not to be reported. 5932. In the `h` RULE above, only term variables are explicitly bound, so any free 594 type variables in the term variables' signatures are implicitly bound. 595 This is just like how the free type variables in pattern signatures are 596 implicitly bound. If a RULE explicitly binds both term and type variables, 597 however, then free type variables in term signatures are /not/ implicitly 598 bound. For example, this RULE would be ill scoped: 599 600 {-# RULES "h2" forall b. forall (t :: (b, c)) (g :: forall a. a -> b). 601 h2 t g = ... #-} 602 603 This is because `b` and `c` occur free in the signature for `t`, but only 604 `b` was explicitly bound, leaving `c` out of scope. If the RULE had started 605 with `forall b c.`, then it would have been accepted. 606 607The types in pattern signatures and RULE term binder signatures are represented 608in the AST by HsSigPatType. From the renamer onward, the hsps_ext field (of 609type HsPSRn) tracks the names of named wildcards and implicitly bound type 610variables so that they can be brought into scope during renaming and 611typechecking. 612 613Note [Lexically scoped type variables] 614~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 615The ScopedTypeVariables extension does two things: 616 617* It allows the use of type signatures in patterns 618 (e.g., `f (x :: a -> a) = ...`). See 619 Note [Pattern signature binders and scoping] for more on this point. 620* It brings lexically scoped type variables into scope for certain type 621 signatures with outermost invisible 'forall's. 622 623This Note concerns the latter bullet point. Per the 624"Lexically scoped type variables" section of the GHC User's Guide, the 625following forms of type signatures can have lexically scoped type variables: 626 627* In declarations with type signatures, e.g., 628 629 f :: forall a. a -> a 630 f x = e @a 631 632 Here, the 'forall a' brings 'a' into scope over the body of 'f'. 633 634 Note that ScopedTypeVariables does /not/ interact with standalone kind 635 signatures, only type signatures. 636 637* In explicit type annotations in expressions, e.g., 638 639 id @a :: forall a. a -> a 640 641* In instance declarations, e.g., 642 643 instance forall a. C [a] where 644 m = e @a 645 646 Note that unlike the examples above, the use of an outermost 'forall' isn't 647 required to bring 'a' into scope. That is, the following would also work: 648 649 instance forall a. C [a] where 650 m = e @a 651 652Note that all of the types above obey the forall-or-nothing rule. As a result, 653the places in the AST that can have lexically scoped type variables are a 654subset of the places that use HsOuterTyVarBndrs 655(See Note [forall-or-nothing rule].) 656 657Some other observations about lexically scoped type variables: 658 659* Only type variables bound by an /invisible/ forall can be lexically scoped. 660 See Note [hsScopedTvs and visible foralls]. 661* The lexically scoped type variables may be a strict subset of the type 662 variables brought into scope by a type signature. 663 See Note [Binding scoped type variables] in GHC.Tc.Gen.Sig. 664-} 665 666mapHsOuterImplicit :: (XHsOuterImplicit pass -> XHsOuterImplicit pass) 667 -> HsOuterTyVarBndrs flag pass 668 -> HsOuterTyVarBndrs flag pass 669mapHsOuterImplicit f (HsOuterImplicit{hso_ximplicit = imp}) = 670 HsOuterImplicit{hso_ximplicit = f imp} 671mapHsOuterImplicit _ hso@(HsOuterExplicit{}) = hso 672mapHsOuterImplicit _ hso@(XHsOuterTyVarBndrs{}) = hso 673 674 675-------------------------------------------------- 676-- | These names are used early on to store the names of implicit 677-- parameters. They completely disappear after type-checking. 678newtype HsIPName = HsIPName FastString 679 deriving( Eq, Data ) 680 681hsIPNameFS :: HsIPName -> FastString 682hsIPNameFS (HsIPName n) = n 683 684instance Outputable HsIPName where 685 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters 686 687instance OutputableBndr HsIPName where 688 pprBndr _ n = ppr n -- Simple for now 689 pprInfixOcc n = ppr n 690 pprPrefixOcc n = ppr n 691 692-------------------------------------------------- 693 694-- | Haskell Type Variable Binder 695-- The flag annotates the binder. It is 'Specificity' in places where 696-- explicit specificity is allowed (e.g. x :: forall {a} b. ...) or 697-- '()' in other places. 698data HsTyVarBndr flag pass 699 = UserTyVar -- no explicit kinding 700 (XUserTyVar pass) 701 flag 702 (LIdP pass) 703 -- See Note [Located RdrNames] in GHC.Hs.Expr 704 705 | KindedTyVar 706 (XKindedTyVar pass) 707 flag 708 (LIdP pass) 709 (LHsKind pass) -- The user-supplied kind signature 710 -- ^ 711 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen', 712 -- 'GHC.Parser.Annotation.AnnDcolon', 'GHC.Parser.Annotation.AnnClose' 713 714 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 715 716 | XTyVarBndr 717 !(XXTyVarBndr pass) 718 719-- | Does this 'HsTyVarBndr' come with an explicit kind annotation? 720isHsKindedTyVar :: HsTyVarBndr flag pass -> Bool 721isHsKindedTyVar (UserTyVar {}) = False 722isHsKindedTyVar (KindedTyVar {}) = True 723isHsKindedTyVar (XTyVarBndr {}) = False 724 725-- | Haskell Type 726data HsType pass 727 = HsForAllTy -- See Note [HsType binders] 728 { hst_xforall :: XForAllTy pass 729 , hst_tele :: HsForAllTelescope pass 730 -- Explicit, user-supplied 'forall a {b} c' 731 , hst_body :: LHsType pass -- body type 732 } 733 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnForall', 734 -- 'GHC.Parser.Annotation.AnnDot','GHC.Parser.Annotation.AnnDarrow' 735 -- For details on above see note [exact print annotations] in "GHC.Parser.Annotation" 736 737 | HsQualTy -- See Note [HsType binders] 738 { hst_xqual :: XQualTy pass 739 , hst_ctxt :: Maybe (LHsContext pass) -- Context C => blah 740 , hst_body :: LHsType pass } 741 742 | HsTyVar (XTyVar pass) 743 PromotionFlag -- Whether explicitly promoted, 744 -- for the pretty printer 745 (LIdP pass) 746 -- Type variable, type constructor, or data constructor 747 -- see Note [Promotions (HsTyVar)] 748 -- See Note [Located RdrNames] in GHC.Hs.Expr 749 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 750 751 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 752 753 | HsAppTy (XAppTy pass) 754 (LHsType pass) 755 (LHsType pass) 756 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 757 758 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 759 760 | HsAppKindTy (XAppKindTy pass) -- type level type app 761 (LHsType pass) 762 (LHsKind pass) 763 764 | HsFunTy (XFunTy pass) 765 (HsArrow pass) 766 (LHsType pass) -- function type 767 (LHsType pass) 768 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnRarrow', 769 770 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 771 772 | HsListTy (XListTy pass) 773 (LHsType pass) -- Element type 774 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'['@, 775 -- 'GHC.Parser.Annotation.AnnClose' @']'@ 776 777 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 778 779 | HsTupleTy (XTupleTy pass) 780 HsTupleSort 781 [LHsType pass] -- Element types (length gives arity) 782 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'(' or '(#'@, 783 -- 'GHC.Parser.Annotation.AnnClose' @')' or '#)'@ 784 785 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 786 787 | HsSumTy (XSumTy pass) 788 [LHsType pass] -- Element types (length gives arity) 789 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'(#'@, 790 -- 'GHC.Parser.Annotation.AnnClose' '#)'@ 791 792 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 793 794 | HsOpTy (XOpTy pass) 795 (LHsType pass) (LIdP pass) (LHsType pass) 796 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 797 798 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 799 800 | HsParTy (XParTy pass) 801 (LHsType pass) -- See Note [Parens in HsSyn] in GHC.Hs.Expr 802 -- Parenthesis preserved for the precedence re-arrangement in 803 -- GHC.Rename.HsType 804 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c! 805 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'('@, 806 -- 'GHC.Parser.Annotation.AnnClose' @')'@ 807 808 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 809 810 | HsIParamTy (XIParamTy pass) 811 (XRec pass HsIPName) -- (?x :: ty) 812 (LHsType pass) -- Implicit parameters as they occur in 813 -- contexts 814 -- ^ 815 -- > (?x :: ty) 816 -- 817 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnDcolon' 818 819 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 820 821 | HsStarTy (XStarTy pass) 822 Bool -- Is this the Unicode variant? 823 -- Note [HsStarTy] 824 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 825 826 | HsKindSig (XKindSig pass) 827 (LHsType pass) -- (ty :: kind) 828 (LHsKind pass) -- A type with a kind signature 829 -- ^ 830 -- > (ty :: kind) 831 -- 832 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'('@, 833 -- 'GHC.Parser.Annotation.AnnDcolon','GHC.Parser.Annotation.AnnClose' @')'@ 834 835 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 836 837 | HsSpliceTy (XSpliceTy pass) 838 (HsSplice pass) -- Includes quasi-quotes 839 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'$('@, 840 -- 'GHC.Parser.Annotation.AnnClose' @')'@ 841 842 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 843 844 | HsDocTy (XDocTy pass) 845 (LHsType pass) LHsDocString -- A documented type 846 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 847 848 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 849 850 | HsBangTy (XBangTy pass) 851 HsSrcBang (LHsType pass) -- Bang-style type annotations 852 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 853 -- 'GHC.Parser.Annotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@, 854 -- 'GHC.Parser.Annotation.AnnClose' @'#-}'@ 855 -- 'GHC.Parser.Annotation.AnnBang' @\'!\'@ 856 857 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 858 859 | HsRecTy (XRecTy pass) 860 [LConDeclField pass] -- Only in data type declarations 861 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @'{'@, 862 -- 'GHC.Parser.Annotation.AnnClose' @'}'@ 863 864 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 865 866 | HsExplicitListTy -- A promoted explicit list 867 (XExplicitListTy pass) 868 PromotionFlag -- whether explicitly promoted, for pretty printer 869 [LHsType pass] 870 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @"'["@, 871 -- 'GHC.Parser.Annotation.AnnClose' @']'@ 872 873 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 874 875 | HsExplicitTupleTy -- A promoted explicit tuple 876 (XExplicitTupleTy pass) 877 [LHsType pass] 878 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen' @"'("@, 879 -- 'GHC.Parser.Annotation.AnnClose' @')'@ 880 881 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 882 883 | HsTyLit (XTyLit pass) HsTyLit -- A promoted numeric literal. 884 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 885 886 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 887 888 | HsWildCardTy (XWildCardTy pass) -- A type wildcard 889 -- See Note [The wildcard story for types] 890 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None 891 892 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 893 894 -- For adding new constructors via Trees that Grow 895 | XHsType 896 !(XXType pass) 897 898-- An escape hatch for tunnelling a Core 'Type' through 'HsType'. 899-- For more details on how this works, see: 900-- 901-- * @Note [Renaming HsCoreTys]@ in "GHC.Rename.HsType" 902-- 903-- * @Note [Typechecking HsCoreTys]@ in "GHC.Tc.Gen.HsType" 904type HsCoreTy = Type 905 906 907-- Note [Literal source text] in GHC.Types.Basic for SourceText fields in 908-- the following 909-- | Haskell Type Literal 910data HsTyLit 911 = HsNumTy SourceText Integer 912 | HsStrTy SourceText FastString 913 | HsCharTy SourceText Char 914 deriving Data 915 916-- | Denotes the type of arrows in the surface language 917data HsArrow pass 918 = HsUnrestrictedArrow IsUnicodeSyntax 919 -- ^ a -> b or a → b 920 | HsLinearArrow IsUnicodeSyntax (Maybe AddEpAnn) 921 -- ^ a %1 -> b or a %1 → b, or a ⊸ b 922 | HsExplicitMult IsUnicodeSyntax (Maybe AddEpAnn) (LHsType pass) 923 -- ^ a %m -> b or a %m → b (very much including `a %Many -> b`! 924 -- This is how the programmer wrote it). It is stored as an 925 -- `HsType` so as to preserve the syntax as written in the 926 -- program. 927 928-- | This is used in the syntax. In constructor declaration. It must keep the 929-- arrow representation. 930data HsScaled pass a = HsScaled (HsArrow pass) a 931 932hsMult :: HsScaled pass a -> HsArrow pass 933hsMult (HsScaled m _) = m 934 935hsScaledThing :: HsScaled pass a -> a 936hsScaledThing (HsScaled _ t) = t 937 938-- | When creating syntax we use the shorthands. It's better for printing, also, 939-- the shorthands work trivially at each pass. 940hsUnrestricted, hsLinear :: a -> HsScaled pass a 941hsUnrestricted = HsScaled (HsUnrestrictedArrow NormalSyntax) 942hsLinear = HsScaled (HsLinearArrow NormalSyntax Nothing) 943 944instance Outputable a => Outputable (HsScaled pass a) where 945 ppr (HsScaled _cnt t) = -- ppr cnt <> ppr t 946 ppr t 947 948{- 949Note [Unit tuples] 950~~~~~~~~~~~~~~~~~~ 951Consider the type 952 type instance F Int = () 953We want to parse that "()" 954 as HsTupleTy HsBoxedOrConstraintTuple [], 955NOT as HsTyVar unitTyCon 956 957Why? Because F might have kind (* -> Constraint), so we when parsing we 958don't know if that tuple is going to be a constraint tuple or an ordinary 959unit tuple. The HsTupleSort flag is specifically designed to deal with 960that, but it has to work for unit tuples too. 961 962Note [Promotions (HsTyVar)] 963~~~~~~~~~~~~~~~~~~~~~~~~~~~ 964HsTyVar: A name in a type or kind. 965 Here are the allowed namespaces for the name. 966 In a type: 967 Var: not allowed 968 Data: promoted data constructor 969 Tv: type variable 970 TcCls before renamer: type constructor, class constructor, or promoted data constructor 971 TcCls after renamer: type constructor or class constructor 972 In a kind: 973 Var, Data: not allowed 974 Tv: kind variable 975 TcCls: kind constructor or promoted type constructor 976 977 The 'Promoted' field in an HsTyVar captures whether the type was promoted in 978 the source code by prefixing an apostrophe. 979 980Note [HsStarTy] 981~~~~~~~~~~~~~~~ 982When the StarIsType extension is enabled, we want to treat '*' and its Unicode 983variant identically to 'Data.Kind.Type'. Unfortunately, doing so in the parser 984would mean that when we pretty-print it back, we don't know whether the user 985wrote '*' or 'Type', and lose the parse/ppr roundtrip property. 986 987As a workaround, we parse '*' as HsStarTy (if it stands for 'Data.Kind.Type') 988and then desugar it to 'Data.Kind.Type' in the typechecker (see tc_hs_type). 989When '*' is a regular type operator (StarIsType is disabled), HsStarTy is not 990involved. 991 992 993Note [Promoted lists and tuples] 994~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 995Notice the difference between 996 HsListTy HsExplicitListTy 997 HsTupleTy HsExplicitListTupleTy 998 999E.g. f :: [Int] HsListTy 1000 1001 g3 :: T '[] All these use 1002 g2 :: T '[True] HsExplicitListTy 1003 g1 :: T '[True,False] 1004 g1a :: T [True,False] (can omit ' where unambiguous) 1005 1006 kind of T :: [Bool] -> * This kind uses HsListTy! 1007 1008E.g. h :: (Int,Bool) HsTupleTy; f is a pair 1009 k :: S '(True,False) HsExplicitTypleTy; S is indexed by 1010 a type-level pair of booleans 1011 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy 1012 1013Note [Distinguishing tuple kinds] 1014~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1015 1016Apart from promotion, tuples can have one of three different kinds: 1017 1018 x :: (Int, Bool) -- Regular boxed tuples 1019 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples 1020 g :: (Eq a, Ord a) => a -- Constraint tuples 1021 1022For convenience, internally we use a single constructor for all of these, 1023namely HsTupleTy, but keep track of the tuple kind (in the first argument to 1024HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing, 1025because of the #. However, with -XConstraintKinds we can only distinguish 1026between constraint and boxed tuples during type checking, in general. Hence the 1027two constructors of HsTupleSort: 1028 1029 HsUnboxedTuple -> Produced by the parser 1030 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint 1031 tuple. Produced by the parser only, 1032 disappears after type checking 1033 1034After typechecking, we use TupleSort (which clearly distinguishes between 1035constraint tuples and boxed tuples) rather than HsTupleSort. 1036-} 1037 1038-- | Haskell Tuple Sort 1039data HsTupleSort = HsUnboxedTuple 1040 | HsBoxedOrConstraintTuple 1041 deriving Data 1042 1043-- | Located Constructor Declaration Field 1044type LConDeclField pass = XRec pass (ConDeclField pass) 1045 -- ^ May have 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnComma' when 1046 -- in a list 1047 1048 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 1049 1050-- | Constructor Declaration Field 1051data ConDeclField pass -- Record fields have Haddock docs on them 1052 = ConDeclField { cd_fld_ext :: XConDeclField pass, 1053 cd_fld_names :: [LFieldOcc pass], 1054 -- ^ See Note [ConDeclField passs] 1055 cd_fld_type :: LBangType pass, 1056 cd_fld_doc :: Maybe LHsDocString } 1057 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnDcolon' 1058 1059 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation 1060 | XConDeclField !(XXConDeclField pass) 1061 1062-- | Describes the arguments to a data constructor. This is a common 1063-- representation for several constructor-related concepts, including: 1064-- 1065-- * The arguments in a Haskell98-style constructor declaration 1066-- (see 'HsConDeclH98Details' in "GHC.Hs.Decls"). 1067-- 1068-- * The arguments in constructor patterns in @case@/function definitions 1069-- (see 'HsConPatDetails' in "GHC.Hs.Pat"). 1070-- 1071-- * The left-hand side arguments in a pattern synonym binding 1072-- (see 'HsPatSynDetails' in "GHC.Hs.Binds"). 1073-- 1074-- One notable exception is the arguments in a GADT constructor, which uses 1075-- a separate data type entirely (see 'HsConDeclGADTDetails' in 1076-- "GHC.Hs.Decls"). This is because GADT constructors cannot be declared with 1077-- infix syntax, unlike the concepts above (#18844). 1078data HsConDetails tyarg arg rec 1079 = PrefixCon [tyarg] [arg] -- C @t1 @t2 p1 p2 p3 1080 | RecCon rec -- C { x = p1, y = p2 } 1081 | InfixCon arg arg -- p1 `C` p2 1082 deriving Data 1083 1084-- | An empty list that can be used to indicate that there are no 1085-- type arguments allowed in cases where HsConDetails is applied to Void. 1086noTypeArgs :: [Void] 1087noTypeArgs = [] 1088 1089instance (Outputable tyarg, Outputable arg, Outputable rec) 1090 => Outputable (HsConDetails tyarg arg rec) where 1091 ppr (PrefixCon tyargs args) = text "PrefixCon:" <+> hsep (map (\t -> text "@" <> ppr t) tyargs) <+> ppr args 1092 ppr (RecCon rec) = text "RecCon:" <+> ppr rec 1093 ppr (InfixCon l r) = text "InfixCon:" <+> ppr [l, r] 1094 1095{- 1096Note [ConDeclField passs] 1097~~~~~~~~~~~~~~~~~~~~~~~~~ 1098 1099A ConDeclField contains a list of field occurrences: these always 1100include the field label as the user wrote it. After the renamer, it 1101will additionally contain the identity of the selector function in the 1102second component. 1103 1104Due to DuplicateRecordFields, the OccName of the selector function 1105may have been mangled, which is why we keep the original field label 1106separately. For example, when DuplicateRecordFields is enabled 1107 1108 data T = MkT { x :: Int } 1109 1110gives 1111 1112 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }. 1113-} 1114 1115----------------------- 1116-- A valid type must have a for-all at the top of the type, or of the fn arg 1117-- types 1118 1119--------------------- 1120 1121{- Note [Scoping of named wildcards] 1122~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1123Consider 1124 f :: _a -> _a 1125 f x = let g :: _a -> _a 1126 g = ... 1127 in ... 1128 1129Currently, for better or worse, the "_a" variables are all the same. So 1130although there is no explicit forall, the "_a" scopes over the definition. 1131I don't know if this is a good idea, but there it is. 1132-} 1133 1134{- Note [hsScopedTvs and visible foralls] 1135~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1136-XScopedTypeVariables can be defined in terms of a desugaring to 1137-XTypeAbstractions (GHC Proposal #50): 1138 1139 fn :: forall a b c. tau(a,b,c) fn :: forall a b c. tau(a,b,c) 1140 fn = defn(a,b,c) ==> fn @x @y @z = defn(x,y,z) 1141 1142That is, for every type variable of the leading 'forall' in the type signature, 1143we add an invisible binder at term level. 1144 1145This model does not extend to visible forall, as discussed here: 1146 1147* https://gitlab.haskell.org/ghc/ghc/issues/16734#note_203412 1148* https://github.com/ghc-proposals/ghc-proposals/pull/238 1149 1150The conclusion of these discussions can be summarized as follows: 1151 1152 > Assuming support for visible 'forall' in terms, consider this example: 1153 > 1154 > vfn :: forall x y -> tau(x,y) 1155 > vfn = \a b -> ... 1156 > 1157 > The user has written their own binders 'a' and 'b' to stand for 'x' and 1158 > 'y', and we definitely should not desugar this into: 1159 > 1160 > vfn :: forall x y -> tau(x,y) 1161 > vfn x y = \a b -> ... -- bad! 1162 1163This design choice is reflected in the design of HsOuterSigTyVarBndrs, which are 1164used in every place that ScopedTypeVariables takes effect: 1165 1166 data HsOuterTyVarBndrs flag pass 1167 = HsOuterImplicit { ... } 1168 | HsOuterExplicit { ..., hso_bndrs :: [LHsTyVarBndr flag pass] } 1169 | ... 1170 type HsOuterSigTyVarBndrs = HsOuterTyVarBndrs Specificity 1171 1172The HsOuterExplicit constructor is only used in type signatures with outermost, 1173/invisible/ 'forall's. Any other type—including those with outermost, 1174/visible/ 'forall's—will use HsOuterImplicit. Therefore, when we determine 1175which type variables to bring into scope over the body of a function 1176(in hsScopedTvs), we /only/ bring the type variables bound by the hso_bndrs in 1177an HsOuterExplicit into scope. If we have an HsOuterImplicit instead, then we 1178do not bring any type variables into scope over the body of a function at all. 1179 1180At the moment, GHC does not support visible 'forall' in terms. Nevertheless, 1181it is still possible to write erroneous programs that use visible 'forall's in 1182terms, such as this example: 1183 1184 x :: forall a -> a -> a 1185 x = x 1186 1187Previous versions of GHC would bring `a` into scope over the body of `x` in the 1188hopes that the typechecker would error out later 1189(see `GHC.Tc.Validity.vdqAllowed`). However, this can wreak havoc in the 1190renamer before GHC gets to that point (see #17687 for an example of this). 1191Bottom line: nip problems in the bud by refraining from bringing any type 1192variables in an HsOuterImplicit into scope over the body of a function, even 1193if they correspond to a visible 'forall'. 1194-} 1195 1196{- 1197************************************************************************ 1198* * 1199 Decomposing HsTypes 1200* * 1201************************************************************************ 1202-} 1203 1204-- Arguments in an expression/type after splitting 1205data HsArg tm ty 1206 = HsValArg tm -- Argument is an ordinary expression (f arg) 1207 | HsTypeArg SrcSpan ty -- Argument is a visible type application (f @ty) 1208 -- SrcSpan is location of the `@` 1209 | HsArgPar SrcSpan -- See Note [HsArgPar] 1210 1211numVisibleArgs :: [HsArg tm ty] -> Arity 1212numVisibleArgs = count is_vis 1213 where is_vis (HsValArg _) = True 1214 is_vis _ = False 1215 1216-- type level equivalent 1217type LHsTypeArg p = HsArg (LHsType p) (LHsKind p) 1218 1219instance (Outputable tm, Outputable ty) => Outputable (HsArg tm ty) where 1220 ppr (HsValArg tm) = ppr tm 1221 ppr (HsTypeArg _ ty) = char '@' <> ppr ty 1222 ppr (HsArgPar sp) = text "HsArgPar" <+> ppr sp 1223{- 1224Note [HsArgPar] 1225A HsArgPar indicates that everything to the left of this in the argument list is 1226enclosed in parentheses together with the function itself. It is necessary so 1227that we can recreate the parenthesis structure in the original source after 1228typechecking the arguments. 1229 1230The SrcSpan is the span of the original HsPar 1231 1232((f arg1) arg2 arg3) results in an input argument list of 1233[HsValArg arg1, HsArgPar span1, HsValArg arg2, HsValArg arg3, HsArgPar span2] 1234 1235-} 1236 1237-------------------------------- 1238 1239 1240{- 1241************************************************************************ 1242* * 1243 FieldOcc 1244* * 1245************************************************************************ 1246-} 1247 1248-- | Located Field Occurrence 1249type LFieldOcc pass = XRec pass (FieldOcc pass) 1250 1251-- | Field Occurrence 1252-- 1253-- Represents an *occurrence* of an unambiguous field. This may or may not be a 1254-- binding occurrence (e.g. this type is used in 'ConDeclField' and 1255-- 'RecordPatSynField' which bind their fields, but also in 'HsRecField' for 1256-- record construction and patterns, which do not). 1257-- 1258-- We store both the 'RdrName' the user originally wrote, and after the renamer, 1259-- the selector function. 1260data FieldOcc pass = FieldOcc { extFieldOcc :: XCFieldOcc pass 1261 , rdrNameFieldOcc :: LocatedN RdrName 1262 -- ^ See Note [Located RdrNames] in "GHC.Hs.Expr" 1263 } 1264 1265 | XFieldOcc 1266 !(XXFieldOcc pass) 1267 1268deriving instance (Eq (XCFieldOcc pass), Eq (XXFieldOcc pass)) => Eq (FieldOcc pass) 1269 1270instance Outputable (FieldOcc pass) where 1271 ppr = ppr . rdrNameFieldOcc 1272 1273instance OutputableBndr (FieldOcc pass) where 1274 pprInfixOcc = pprInfixOcc . unLoc . rdrNameFieldOcc 1275 pprPrefixOcc = pprPrefixOcc . unLoc . rdrNameFieldOcc 1276 1277instance OutputableBndr (GenLocated SrcSpan (FieldOcc pass)) where 1278 pprInfixOcc = pprInfixOcc . unLoc 1279 pprPrefixOcc = pprPrefixOcc . unLoc 1280 1281-- | Ambiguous Field Occurrence 1282-- 1283-- Represents an *occurrence* of a field that is potentially 1284-- ambiguous after the renamer, with the ambiguity resolved by the 1285-- typechecker. We always store the 'RdrName' that the user 1286-- originally wrote, and store the selector function after the renamer 1287-- (for unambiguous occurrences) or the typechecker (for ambiguous 1288-- occurrences). 1289-- 1290-- See Note [HsRecField and HsRecUpdField] in "GHC.Hs.Pat" and 1291-- Note [Disambiguating record fields] in "GHC.Tc.Gen.Head". 1292-- See Note [Located RdrNames] in "GHC.Hs.Expr" 1293data AmbiguousFieldOcc pass 1294 = Unambiguous (XUnambiguous pass) (LocatedN RdrName) 1295 | Ambiguous (XAmbiguous pass) (LocatedN RdrName) 1296 | XAmbiguousFieldOcc !(XXAmbiguousFieldOcc pass) 1297 1298 1299{- 1300************************************************************************ 1301* * 1302\subsection{Pretty printing} 1303* * 1304************************************************************************ 1305-} 1306 1307instance Outputable HsTyLit where 1308 ppr = ppr_tylit 1309-------------------------- 1310ppr_tylit :: HsTyLit -> SDoc 1311ppr_tylit (HsNumTy source i) = pprWithSourceText source (integer i) 1312ppr_tylit (HsStrTy source s) = pprWithSourceText source (text (show s)) 1313ppr_tylit (HsCharTy source c) = pprWithSourceText source (text (show c)) 1314