never executed always true always false
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section{@Vars@: Variables}
6 -}
7
8 {-# LANGUAGE FlexibleContexts, MultiWayIf, FlexibleInstances, DeriveDataTypeable,
9 PatternSynonyms, BangPatterns #-}
10 {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
11 {-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
12
13 -- |
14 -- #name_types#
15 -- GHC uses several kinds of name internally:
16 --
17 -- * 'GHC.Types.Name.Occurrence.OccName': see "GHC.Types.Name.Occurrence#name_types"
18 --
19 -- * 'GHC.Types.Name.Reader.RdrName': see "GHC.Types.Name.Reader#name_types"
20 --
21 -- * 'GHC.Types.Name.Name': see "GHC.Types.Name#name_types"
22 --
23 -- * 'GHC.Types.Id.Id': see "GHC.Types.Id#name_types"
24 --
25 -- * 'GHC.Types.Var.Var' is a synonym for the 'GHC.Types.Id.Id' type but it may additionally
26 -- potentially contain type variables, which have a 'GHC.Core.TyCo.Rep.Kind'
27 -- rather than a 'GHC.Core.TyCo.Rep.Type' and only contain some extra
28 -- details during typechecking.
29 --
30 -- These 'Var' names may either be global or local, see "GHC.Types.Var#globalvslocal"
31 --
32 -- #globalvslocal#
33 -- Global 'Id's and 'Var's are those that are imported or correspond
34 -- to a data constructor, primitive operation, or record selectors.
35 -- Local 'Id's and 'Var's are those bound within an expression
36 -- (e.g. by a lambda) or at the top level of the module being compiled.
37
38 module GHC.Types.Var (
39 -- * The main data type and synonyms
40 Var, CoVar, Id, NcId, DictId, DFunId, EvVar, EqVar, EvId, IpId, JoinId,
41 TyVar, TcTyVar, TypeVar, KindVar, TKVar, TyCoVar,
42
43 -- * In and Out variants
44 InVar, InCoVar, InId, InTyVar,
45 OutVar, OutCoVar, OutId, OutTyVar,
46
47 -- ** Taking 'Var's apart
48 varName, varUnique, varType,
49 varMult, varMultMaybe,
50
51 -- ** Modifying 'Var's
52 setVarName, setVarUnique, setVarType,
53 updateVarType, updateVarTypeM,
54
55 -- ** Constructing, taking apart, modifying 'Id's
56 mkGlobalVar, mkLocalVar, mkExportedLocalVar, mkCoVar,
57 idInfo, idDetails,
58 lazySetIdInfo, setIdDetails, globaliseId,
59 setIdExported, setIdNotExported, setIdMult,
60 updateIdTypeButNotMult,
61 updateIdTypeAndMult, updateIdTypeAndMultM,
62
63 -- ** Predicates
64 isId, isTyVar, isTcTyVar,
65 isLocalVar, isLocalId, isCoVar, isNonCoVarId, isTyCoVar,
66 isGlobalId, isExportedId,
67 mustHaveLocalBinding,
68
69 -- * ArgFlags
70 ArgFlag(Invisible,Required,Specified,Inferred),
71 AnonArgFlag(..), Specificity(..),
72 isVisibleArgFlag, isInvisibleArgFlag, isInferredArgFlag,
73 sameVis,
74
75 -- * TyVar's
76 VarBndr(..), TyCoVarBinder, TyVarBinder, InvisTVBinder, ReqTVBinder,
77 binderVar, binderVars, binderArgFlag, binderType,
78 mkTyCoVarBinder, mkTyCoVarBinders,
79 mkTyVarBinder, mkTyVarBinders,
80 isTyVarBinder,
81 tyVarSpecToBinder, tyVarSpecToBinders, tyVarReqToBinder, tyVarReqToBinders,
82 mapVarBndr, mapVarBndrs, lookupVarBndr,
83
84 -- ** Constructing TyVar's
85 mkTyVar, mkTcTyVar,
86
87 -- ** Taking 'TyVar's apart
88 tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
89
90 -- ** Modifying 'TyVar's
91 setTyVarName, setTyVarUnique, setTyVarKind, updateTyVarKind,
92 updateTyVarKindM,
93
94 nonDetCmpVar
95
96 ) where
97
98 import GHC.Prelude
99
100 import {-# SOURCE #-} GHC.Core.TyCo.Rep( Type, Kind, Mult )
101 import {-# SOURCE #-} GHC.Core.TyCo.Ppr( pprKind )
102 import {-# SOURCE #-} GHC.Tc.Utils.TcType( TcTyVarDetails, pprTcTyVarDetails, vanillaSkolemTv )
103 import {-# SOURCE #-} GHC.Types.Id.Info( IdDetails, IdInfo, coVarDetails, isCoVarDetails,
104 vanillaIdInfo, pprIdDetails )
105 import {-# SOURCE #-} GHC.Builtin.Types ( manyDataConTy )
106 import {-# SOURCE #-} GHC.Types.Name
107 import GHC.Types.Unique ( Uniquable, Unique, getKey, getUnique
108 , mkUniqueGrimily, nonDetCmpUnique )
109 import GHC.Utils.Misc
110 import GHC.Utils.Binary
111 import GHC.Utils.Outputable
112 import GHC.Utils.Panic
113 import GHC.Utils.Panic.Plain
114
115 import Data.Data
116
117 {-
118 ************************************************************************
119 * *
120 Synonyms
121 * *
122 ************************************************************************
123 -- These synonyms are here and not in Id because otherwise we need a very
124 -- large number of SOURCE imports of "GHC.Types.Id" :-(
125 -}
126
127 -- | Identifier
128 type Id = Var -- A term-level identifier
129 -- predicate: isId
130
131 -- | Coercion Variable
132 type CoVar = Id -- See Note [Evidence: EvIds and CoVars]
133 -- predicate: isCoVar
134
135 -- |
136 type NcId = Id -- A term-level (value) variable that is
137 -- /not/ an (unlifted) coercion
138 -- predicate: isNonCoVarId
139
140 -- | Type or kind Variable
141 type TyVar = Var -- Type *or* kind variable (historical)
142
143 -- | Type or Kind Variable
144 type TKVar = Var -- Type *or* kind variable (historical)
145
146 -- | Type variable that might be a metavariable
147 type TcTyVar = Var
148
149 -- | Type Variable
150 type TypeVar = Var -- Definitely a type variable
151
152 -- | Kind Variable
153 type KindVar = Var -- Definitely a kind variable
154 -- See Note [Kind and type variables]
155
156 -- See Note [Evidence: EvIds and CoVars]
157 -- | Evidence Identifier
158 type EvId = Id -- Term-level evidence: DictId, IpId, or EqVar
159
160 -- | Evidence Variable
161 type EvVar = EvId -- ...historical name for EvId
162
163 -- | Dictionary Function Identifier
164 type DFunId = Id -- A dictionary function
165
166 -- | Dictionary Identifier
167 type DictId = EvId -- A dictionary variable
168
169 -- | Implicit parameter Identifier
170 type IpId = EvId -- A term-level implicit parameter
171
172 -- | Equality Variable
173 type EqVar = EvId -- Boxed equality evidence
174 type JoinId = Id -- A join variable
175
176 -- | Type or Coercion Variable
177 type TyCoVar = Id -- Type, *or* coercion variable
178 -- predicate: isTyCoVar
179
180
181 {- Many passes apply a substitution, and it's very handy to have type
182 synonyms to remind us whether or not the substitution has been applied -}
183
184 type InVar = Var
185 type InTyVar = TyVar
186 type InCoVar = CoVar
187 type InId = Id
188 type OutVar = Var
189 type OutTyVar = TyVar
190 type OutCoVar = CoVar
191 type OutId = Id
192
193
194
195 {- Note [Evidence: EvIds and CoVars]
196 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197 * An EvId (evidence Id) is a term-level evidence variable
198 (dictionary, implicit parameter, or equality). Could be boxed or unboxed.
199
200 * DictId, IpId, and EqVar are synonyms when we know what kind of
201 evidence we are talking about. For example, an EqVar has type (t1 ~ t2).
202
203 * A CoVar is always an un-lifted coercion, of type (t1 ~# t2) or (t1 ~R# t2)
204
205 Note [Kind and type variables]
206 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207 Before kind polymorphism, TyVar were used to mean type variables. Now
208 they are used to mean kind *or* type variables. KindVar is used when we
209 know for sure that it is a kind variable. In future, we might want to
210 go over the whole compiler code to use:
211 - TKVar to mean kind or type variables
212 - TypeVar to mean type variables only
213 - KindVar to mean kind variables
214
215
216 ************************************************************************
217 * *
218 \subsection{The main data type declarations}
219 * *
220 ************************************************************************
221
222
223 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
224 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
225 strictness). The essential info about different kinds of @Vars@ is
226 in its @VarDetails@.
227 -}
228
229 -- | Variable
230 --
231 -- Essentially a typed 'Name', that may also contain some additional information
232 -- about the 'Var' and its use sites.
233 data Var
234 = TyVar { -- Type and kind variables
235 -- see Note [Kind and type variables]
236 varName :: !Name,
237 realUnique :: {-# UNPACK #-} !Int,
238 -- ^ Key for fast comparison
239 -- Identical to the Unique in the name,
240 -- cached here for speed
241 varType :: Kind -- ^ The type or kind of the 'Var' in question
242 }
243
244 | TcTyVar { -- Used only during type inference
245 -- Used for kind variables during
246 -- inference, as well
247 varName :: !Name,
248 realUnique :: {-# UNPACK #-} !Int,
249 varType :: Kind,
250 tc_tv_details :: TcTyVarDetails
251 }
252
253 | Id {
254 varName :: !Name,
255 realUnique :: {-# UNPACK #-} !Int,
256 varType :: Type,
257 varMult :: Mult, -- See Note [Multiplicity of let binders]
258 idScope :: IdScope,
259 id_details :: IdDetails, -- Stable, doesn't change
260 id_info :: IdInfo } -- Unstable, updated by simplifier
261
262 -- | Identifier Scope
263 data IdScope -- See Note [GlobalId/LocalId]
264 = GlobalId
265 | LocalId ExportFlag
266
267 data ExportFlag -- See Note [ExportFlag on binders]
268 = NotExported -- ^ Not exported: may be discarded as dead code.
269 | Exported -- ^ Exported: kept alive
270
271 {- Note [ExportFlag on binders]
272 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273 An ExportFlag of "Exported" on a top-level binder says "keep this
274 binding alive; do not drop it as dead code". This transitively
275 keeps alive all the other top-level bindings that this binding refers
276 to. This property is persisted all the way down the pipeline, so that
277 the binding will be compiled all the way to object code, and its
278 symbols will appear in the linker symbol table.
279
280 However, note that this use of "exported" is quite different to the
281 export list on a Haskell module. Setting the ExportFlag on an Id does
282 /not/ mean that if you import the module (in Haskell source code) you
283 will see this Id. Of course, things that appear in the export list
284 of the source Haskell module do indeed have their ExportFlag set.
285 But many other things, such as dictionary functions, are kept alive
286 by having their ExportFlag set, even though they are not exported
287 in the source-code sense.
288
289 We should probably use a different term for ExportFlag, like
290 KeepAlive.
291
292 Note [GlobalId/LocalId]
293 ~~~~~~~~~~~~~~~~~~~~~~~
294 A GlobalId is
295 * always a constant (top-level)
296 * imported, or data constructor, or primop, or record selector
297 * has a Unique that is globally unique across the whole
298 GHC invocation (a single invocation may compile multiple modules)
299 * never treated as a candidate by the free-variable finder;
300 it's a constant!
301
302 A LocalId is
303 * bound within an expression (lambda, case, local let(rec))
304 * or defined at top level in the module being compiled
305 * always treated as a candidate by the free-variable finder
306
307 After CoreTidy, top-level LocalIds are turned into GlobalIds
308
309 Note [Multiplicity of let binders]
310 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
311 In Core, let-binders' multiplicity is always completely determined by syntax:
312 a recursive let will always have multiplicity Many (it's a prerequisite for
313 being recursive), and non-recursive let doesn't have a conventional multiplicity,
314 instead they act, for the purpose of multiplicity, as an alias for their
315 right-hand side.
316
317 Therefore, the `varMult` field of identifier is only used by binders in lambda
318 and case expressions. In a let expression the `varMult` field holds an
319 arbitrary value which will (and must!) be ignored.
320 -}
321
322 instance Outputable Var where
323 ppr var = sdocOption sdocSuppressVarKinds $ \supp_var_kinds ->
324 getPprDebug $ \debug ->
325 getPprStyle $ \sty ->
326 let
327 ppr_var = case var of
328 (TyVar {})
329 | debug
330 -> brackets (text "tv")
331
332 (TcTyVar {tc_tv_details = d})
333 | dumpStyle sty || debug
334 -> brackets (pprTcTyVarDetails d)
335
336 (Id { idScope = s, id_details = d })
337 | debug
338 -> brackets (ppr_id_scope s <> pprIdDetails d)
339
340 _ -> empty
341 in if
342 | debug && (not supp_var_kinds)
343 -> parens (ppr (varName var) <+> ppr (varMultMaybe var)
344 <+> ppr_var <+>
345 dcolon <+> pprKind (tyVarKind var))
346 | otherwise
347 -> ppr (varName var) <> ppr_var
348
349 ppr_id_scope :: IdScope -> SDoc
350 ppr_id_scope GlobalId = text "gid"
351 ppr_id_scope (LocalId Exported) = text "lidx"
352 ppr_id_scope (LocalId NotExported) = text "lid"
353
354 instance NamedThing Var where
355 getName = varName
356
357 instance Uniquable Var where
358 getUnique = varUnique
359
360 instance Eq Var where
361 a == b = realUnique a == realUnique b
362
363 instance Ord Var where
364 a <= b = realUnique a <= realUnique b
365 a < b = realUnique a < realUnique b
366 a >= b = realUnique a >= realUnique b
367 a > b = realUnique a > realUnique b
368 a `compare` b = a `nonDetCmpVar` b
369
370 -- | Compare Vars by their Uniques.
371 -- This is what Ord Var does, provided here to make it explicit at the
372 -- call-site that it can introduce non-determinism.
373 -- See Note [Unique Determinism]
374 nonDetCmpVar :: Var -> Var -> Ordering
375 nonDetCmpVar a b = varUnique a `nonDetCmpUnique` varUnique b
376
377 instance Data Var where
378 -- don't traverse?
379 toConstr _ = abstractConstr "Var"
380 gunfold _ _ = error "gunfold"
381 dataTypeOf _ = mkNoRepType "Var"
382
383 instance HasOccName Var where
384 occName = nameOccName . varName
385
386 varUnique :: Var -> Unique
387 varUnique var = mkUniqueGrimily (realUnique var)
388
389 varMultMaybe :: Id -> Maybe Mult
390 varMultMaybe (Id { varMult = mult }) = Just mult
391 varMultMaybe _ = Nothing
392
393 setVarUnique :: Var -> Unique -> Var
394 setVarUnique var uniq
395 = var { realUnique = getKey uniq,
396 varName = setNameUnique (varName var) uniq }
397
398 setVarName :: Var -> Name -> Var
399 setVarName var new_name
400 = var { realUnique = getKey (getUnique new_name),
401 varName = new_name }
402
403 setVarType :: Var -> Type -> Var
404 setVarType id ty = id { varType = ty }
405
406 -- | Update a 'Var's type. Does not update the /multiplicity/
407 -- stored in an 'Id', if any. Because of the possibility for
408 -- abuse, ASSERTs that there is no multiplicity to update.
409 updateVarType :: (Type -> Type) -> Var -> Var
410 updateVarType upd var
411 = case var of
412 Id { id_details = details } -> assert (isCoVarDetails details) $
413 result
414 _ -> result
415 where
416 result = var { varType = upd (varType var) }
417
418 -- | Update a 'Var's type monadically. Does not update the /multiplicity/
419 -- stored in an 'Id', if any. Because of the possibility for
420 -- abuse, ASSERTs that there is no multiplicity to update.
421 updateVarTypeM :: Monad m => (Type -> m Type) -> Var -> m Var
422 updateVarTypeM upd var
423 = case var of
424 Id { id_details = details } -> assert (isCoVarDetails details) $
425 result
426 _ -> result
427 where
428 result = do { ty' <- upd (varType var)
429 ; return (var { varType = ty' }) }
430
431 {- *********************************************************************
432 * *
433 * ArgFlag
434 * *
435 ********************************************************************* -}
436
437 -- | Argument Flag
438 --
439 -- Is something required to appear in source Haskell ('Required'),
440 -- permitted by request ('Specified') (visible type application), or
441 -- prohibited entirely from appearing in source Haskell ('Inferred')?
442 -- See Note [VarBndrs, TyCoVarBinders, TyConBinders, and visibility] in "GHC.Core.TyCo.Rep"
443 data ArgFlag = Invisible Specificity
444 | Required
445 deriving (Eq, Ord, Data)
446 -- (<) on ArgFlag means "is less visible than"
447
448 -- | Whether an 'Invisible' argument may appear in source Haskell.
449 data Specificity = InferredSpec
450 -- ^ the argument may not appear in source Haskell, it is
451 -- only inferred.
452 | SpecifiedSpec
453 -- ^ the argument may appear in source Haskell, but isn't
454 -- required.
455 deriving (Eq, Ord, Data)
456
457 pattern Inferred, Specified :: ArgFlag
458 pattern Inferred = Invisible InferredSpec
459 pattern Specified = Invisible SpecifiedSpec
460
461 {-# COMPLETE Required, Specified, Inferred #-}
462
463 -- | Does this 'ArgFlag' classify an argument that is written in Haskell?
464 isVisibleArgFlag :: ArgFlag -> Bool
465 isVisibleArgFlag af = not (isInvisibleArgFlag af)
466
467 -- | Does this 'ArgFlag' classify an argument that is not written in Haskell?
468 isInvisibleArgFlag :: ArgFlag -> Bool
469 isInvisibleArgFlag (Invisible {}) = True
470 isInvisibleArgFlag Required = False
471
472 isInferredArgFlag :: ArgFlag -> Bool
473 -- More restrictive than isInvisibleArgFlag
474 isInferredArgFlag (Invisible InferredSpec) = True
475 isInferredArgFlag _ = False
476
477 -- | Do these denote the same level of visibility? 'Required'
478 -- arguments are visible, others are not. So this function
479 -- equates 'Specified' and 'Inferred'. Used for printing.
480 sameVis :: ArgFlag -> ArgFlag -> Bool
481 sameVis Required Required = True
482 sameVis (Invisible _) (Invisible _) = True
483 sameVis _ _ = False
484
485 instance Outputable ArgFlag where
486 ppr Required = text "[req]"
487 ppr Specified = text "[spec]"
488 ppr Inferred = text "[infrd]"
489
490 instance Binary Specificity where
491 put_ bh SpecifiedSpec = putByte bh 0
492 put_ bh InferredSpec = putByte bh 1
493
494 get bh = do
495 h <- getByte bh
496 case h of
497 0 -> return SpecifiedSpec
498 _ -> return InferredSpec
499
500 instance Binary ArgFlag where
501 put_ bh Required = putByte bh 0
502 put_ bh Specified = putByte bh 1
503 put_ bh Inferred = putByte bh 2
504
505 get bh = do
506 h <- getByte bh
507 case h of
508 0 -> return Required
509 1 -> return Specified
510 _ -> return Inferred
511
512 -- | The non-dependent version of 'ArgFlag'.
513 -- See Note [AnonArgFlag]
514 -- Appears here partly so that it's together with its friends ArgFlag
515 -- and ForallVisFlag, but also because it is used in IfaceType, rather
516 -- early in the compilation chain
517 data AnonArgFlag
518 = VisArg -- ^ Used for @(->)@: an ordinary non-dependent arrow.
519 -- The argument is visible in source code.
520 | InvisArg -- ^ Used for @(=>)@: a non-dependent predicate arrow.
521 -- The argument is invisible in source code.
522 deriving (Eq, Ord, Data)
523
524 instance Outputable AnonArgFlag where
525 ppr VisArg = text "[vis]"
526 ppr InvisArg = text "[invis]"
527
528 instance Binary AnonArgFlag where
529 put_ bh VisArg = putByte bh 0
530 put_ bh InvisArg = putByte bh 1
531
532 get bh = do
533 h <- getByte bh
534 case h of
535 0 -> return VisArg
536 _ -> return InvisArg
537
538 {- Note [AnonArgFlag]
539 ~~~~~~~~~~~~~~~~~~~~~
540 AnonArgFlag is used principally in the FunTy constructor of Type.
541 FunTy VisArg t1 t2 means t1 -> t2
542 FunTy InvisArg t1 t2 means t1 => t2
543
544 However, the AnonArgFlag in a FunTy is just redundant, cached
545 information. In (FunTy { ft_af = af, ft_arg = t1, ft_res = t2 })
546 * if (isPredTy t1 = True) then af = InvisArg
547 * if (isPredTy t1 = False) then af = VisArg
548 where isPredTy is defined in GHC.Core.Type, and sees if t1's
549 kind is Constraint. See GHC.Core.TyCo.Rep
550 Note [Types for coercions, predicates, and evidence]
551
552 GHC.Core.Utils.mkFunctionType :: Mult -> Type -> Type -> Type
553 uses isPredTy to decide the AnonArgFlag for the FunTy.
554
555 The term (Lam b e), and coercion (FunCo co1 co2) don't carry
556 AnonArgFlags; instead they use mkFunctionType when we want to
557 get their types; see mkLamType and coercionLKind/RKind resp.
558 This is just an engineering choice; we could cache here too
559 if we wanted.
560
561 Why bother with all this? After all, we are in Core, where (=>) and
562 (->) behave the same. We maintain this distinction throughout Core so
563 that we can cheaply and conveniently determine
564 * How to print a type
565 * How to split up a type: tcSplitSigmaTy
566 * How to specialise it (over type classes; GHC.Core.Opt.Specialise)
567
568 For the specialisation point, consider
569 (\ (d :: Ord a). blah). We want to give it type
570 (Ord a => blah_ty)
571 with a fat arrow; that is, using mkInvisFunTy, not mkVisFunTy.
572 Why? Because the /specialiser/ treats dictionary arguments specially.
573 Suppose we do w/w on 'foo', thus (#11272, #6056)
574 foo :: Ord a => Int -> blah
575 foo a d x = case x of I# x' -> $wfoo @a d x'
576
577 $wfoo :: Ord a => Int# -> blah
578
579 Now, at a call we see (foo @Int dOrdInt). The specialiser will
580 specialise this to $sfoo, where
581 $sfoo :: Int -> blah
582 $sfoo x = case x of I# x' -> $wfoo @Int dOrdInt x'
583
584 Now we /must/ also specialise $wfoo! But it wasn't user-written,
585 and has a type built with mkLamTypes.
586
587 Conclusion: the easiest thing is to make mkLamType build
588 (c => ty)
589 when the argument is a predicate type. See GHC.Core.TyCo.Rep
590 Note [Types for coercions, predicates, and evidence]
591 -}
592
593 {- *********************************************************************
594 * *
595 * VarBndr, TyCoVarBinder
596 * *
597 ********************************************************************* -}
598
599 {- Note [The VarBndr type and its uses]
600 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
601 VarBndr is polymorphic in both var and visibility fields.
602 Currently there are nine different uses of 'VarBndr':
603
604 * Var.TyCoVarBinder = VarBndr TyCoVar ArgFlag
605 Binder of a forall-type; see ForAllTy in GHC.Core.TyCo.Rep
606
607 * Var.TyVarBinder = VarBndr TyVar ArgFlag
608 Subset of TyCoVarBinder when we are sure the binder is a TyVar
609
610 * Var.InvisTVBinder = VarBndr TyVar Specificity
611 Specialised form of TyVarBinder, when ArgFlag = Invisible s
612 See GHC.Core.Type.splitForAllInvisTVBinders
613
614 * Var.ReqTVBinder = VarBndr TyVar ()
615 Specialised form of TyVarBinder, when ArgFlag = Required
616 See GHC.Core.Type.splitForAllReqTVBinders
617 This one is barely used
618
619 * TyCon.TyConBinder = VarBndr TyVar TyConBndrVis
620 Binders of a TyCon; see TyCon in GHC.Core.TyCon
621
622 * TyCon.TyConTyCoBinder = VarBndr TyCoVar TyConBndrVis
623 Binders of a PromotedDataCon
624 See Note [Promoted GADT data construtors] in GHC.Core.TyCon
625
626 * IfaceType.IfaceForAllBndr = VarBndr IfaceBndr ArgFlag
627 * IfaceType.IfaceForAllSpecBndr = VarBndr IfaceBndr Specificity
628 * IfaceType.IfaceTyConBinder = VarBndr IfaceBndr TyConBndrVis
629 -}
630
631 data VarBndr var argf = Bndr var argf
632 -- See Note [The VarBndr type and its uses]
633 deriving( Data )
634
635 -- | Variable Binder
636 --
637 -- A 'TyCoVarBinder' is the binder of a ForAllTy
638 -- It's convenient to define this synonym here rather its natural
639 -- home in "GHC.Core.TyCo.Rep", because it's used in GHC.Core.DataCon.hs-boot
640 --
641 -- A 'TyVarBinder' is a binder with only TyVar
642 type TyCoVarBinder = VarBndr TyCoVar ArgFlag
643 type TyVarBinder = VarBndr TyVar ArgFlag
644 type InvisTVBinder = VarBndr TyVar Specificity
645 type ReqTVBinder = VarBndr TyVar ()
646
647 tyVarSpecToBinders :: [VarBndr a Specificity] -> [VarBndr a ArgFlag]
648 tyVarSpecToBinders = map tyVarSpecToBinder
649
650 tyVarSpecToBinder :: VarBndr a Specificity -> VarBndr a ArgFlag
651 tyVarSpecToBinder (Bndr tv vis) = Bndr tv (Invisible vis)
652
653 tyVarReqToBinders :: [VarBndr a ()] -> [VarBndr a ArgFlag]
654 tyVarReqToBinders = map tyVarReqToBinder
655
656 tyVarReqToBinder :: VarBndr a () -> VarBndr a ArgFlag
657 tyVarReqToBinder (Bndr tv _) = Bndr tv Required
658
659 binderVar :: VarBndr tv argf -> tv
660 binderVar (Bndr v _) = v
661
662 binderVars :: [VarBndr tv argf] -> [tv]
663 binderVars tvbs = map binderVar tvbs
664
665 binderArgFlag :: VarBndr tv argf -> argf
666 binderArgFlag (Bndr _ argf) = argf
667
668 binderType :: VarBndr TyCoVar argf -> Type
669 binderType (Bndr tv _) = varType tv
670
671 -- | Make a named binder
672 mkTyCoVarBinder :: vis -> TyCoVar -> VarBndr TyCoVar vis
673 mkTyCoVarBinder vis var = Bndr var vis
674
675 -- | Make a named binder
676 -- 'var' should be a type variable
677 mkTyVarBinder :: vis -> TyVar -> VarBndr TyVar vis
678 mkTyVarBinder vis var
679 = assert (isTyVar var) $
680 Bndr var vis
681
682 -- | Make many named binders
683 mkTyCoVarBinders :: vis -> [TyCoVar] -> [VarBndr TyCoVar vis]
684 mkTyCoVarBinders vis = map (mkTyCoVarBinder vis)
685
686 -- | Make many named binders
687 -- Input vars should be type variables
688 mkTyVarBinders :: vis -> [TyVar] -> [VarBndr TyVar vis]
689 mkTyVarBinders vis = map (mkTyVarBinder vis)
690
691 isTyVarBinder :: TyCoVarBinder -> Bool
692 isTyVarBinder (Bndr v _) = isTyVar v
693
694 mapVarBndr :: (var -> var') -> (VarBndr var flag) -> (VarBndr var' flag)
695 mapVarBndr f (Bndr v fl) = Bndr (f v) fl
696
697 mapVarBndrs :: (var -> var') -> [VarBndr var flag] -> [VarBndr var' flag]
698 mapVarBndrs f = map (mapVarBndr f)
699
700 lookupVarBndr :: Eq var => var -> [VarBndr var flag] -> Maybe flag
701 lookupVarBndr var bndrs = lookup var zipped_bndrs
702 where
703 zipped_bndrs = map (\(Bndr v f) -> (v,f)) bndrs
704
705 instance Outputable tv => Outputable (VarBndr tv ArgFlag) where
706 ppr (Bndr v Required) = ppr v
707 ppr (Bndr v Specified) = char '@' <> ppr v
708 ppr (Bndr v Inferred) = braces (ppr v)
709
710 instance Outputable tv => Outputable (VarBndr tv Specificity) where
711 ppr = ppr . tyVarSpecToBinder
712
713 instance (Binary tv, Binary vis) => Binary (VarBndr tv vis) where
714 put_ bh (Bndr tv vis) = do { put_ bh tv; put_ bh vis }
715
716 get bh = do { tv <- get bh; vis <- get bh; return (Bndr tv vis) }
717
718 instance NamedThing tv => NamedThing (VarBndr tv flag) where
719 getName (Bndr tv _) = getName tv
720
721 {-
722 ************************************************************************
723 * *
724 * Type and kind variables *
725 * *
726 ************************************************************************
727 -}
728
729 tyVarName :: TyVar -> Name
730 tyVarName = varName
731
732 tyVarKind :: TyVar -> Kind
733 tyVarKind = varType
734
735 setTyVarUnique :: TyVar -> Unique -> TyVar
736 setTyVarUnique = setVarUnique
737
738 setTyVarName :: TyVar -> Name -> TyVar
739 setTyVarName = setVarName
740
741 setTyVarKind :: TyVar -> Kind -> TyVar
742 setTyVarKind tv k = tv {varType = k}
743
744 updateTyVarKind :: (Kind -> Kind) -> TyVar -> TyVar
745 updateTyVarKind update tv = tv {varType = update (tyVarKind tv)}
746
747 updateTyVarKindM :: (Monad m) => (Kind -> m Kind) -> TyVar -> m TyVar
748 updateTyVarKindM update tv
749 = do { k' <- update (tyVarKind tv)
750 ; return $ tv {varType = k'} }
751
752 mkTyVar :: Name -> Kind -> TyVar
753 mkTyVar name kind = TyVar { varName = name
754 , realUnique = getKey (nameUnique name)
755 , varType = kind
756 }
757
758 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
759 mkTcTyVar name kind details
760 = -- NB: 'kind' may be a coercion kind; cf, 'GHC.Tc.Utils.TcMType.newMetaCoVar'
761 TcTyVar { varName = name,
762 realUnique = getKey (nameUnique name),
763 varType = kind,
764 tc_tv_details = details
765 }
766
767 tcTyVarDetails :: TyVar -> TcTyVarDetails
768 -- See Note [TcTyVars in the typechecker] in GHC.Tc.Utils.TcType
769 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
770 tcTyVarDetails (TyVar {}) = vanillaSkolemTv
771 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var <+> dcolon <+> pprKind (tyVarKind var))
772
773 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
774 setTcTyVarDetails tv details = tv { tc_tv_details = details }
775
776 {-
777 %************************************************************************
778 %* *
779 \subsection{Ids}
780 * *
781 ************************************************************************
782 -}
783
784 idInfo :: HasDebugCallStack => Id -> IdInfo
785 idInfo (Id { id_info = info }) = info
786 idInfo other = pprPanic "idInfo" (ppr other)
787
788 idDetails :: Id -> IdDetails
789 idDetails (Id { id_details = details }) = details
790 idDetails other = pprPanic "idDetails" (ppr other)
791
792 -- The next three have a 'Var' suffix even though they always build
793 -- Ids, because "GHC.Types.Id" uses 'mkGlobalId' etc with different types
794 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
795 mkGlobalVar details name ty info
796 = mk_id name manyDataConTy ty GlobalId details info
797 -- There is no support for linear global variables yet. They would require
798 -- being checked at link-time, which can be useful, but is not a priority.
799
800 mkLocalVar :: IdDetails -> Name -> Mult -> Type -> IdInfo -> Id
801 mkLocalVar details name w ty info
802 = mk_id name w ty (LocalId NotExported) details info
803
804 mkCoVar :: Name -> Type -> CoVar
805 -- Coercion variables have no IdInfo
806 mkCoVar name ty = mk_id name manyDataConTy ty (LocalId NotExported) coVarDetails vanillaIdInfo
807
808 -- | Exported 'Var's will not be removed as dead code
809 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
810 mkExportedLocalVar details name ty info
811 = mk_id name manyDataConTy ty (LocalId Exported) details info
812 -- There is no support for exporting linear variables. See also [mkGlobalVar]
813
814 mk_id :: Name -> Mult -> Type -> IdScope -> IdDetails -> IdInfo -> Id
815 mk_id name !w ty scope details info
816 = Id { varName = name,
817 realUnique = getKey (nameUnique name),
818 varMult = w,
819 varType = ty,
820 idScope = scope,
821 id_details = details,
822 id_info = info }
823
824 -------------------
825 lazySetIdInfo :: Id -> IdInfo -> Var
826 lazySetIdInfo id info = id { id_info = info }
827
828 setIdDetails :: Id -> IdDetails -> Id
829 setIdDetails id details = id { id_details = details }
830
831 globaliseId :: Id -> Id
832 -- ^ If it's a local, make it global
833 globaliseId id = id { idScope = GlobalId }
834
835 setIdExported :: Id -> Id
836 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
837 -- and class operations, which are born as global 'Id's and automatically exported
838 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
839 setIdExported id@(Id { idScope = GlobalId }) = id
840 setIdExported tv = pprPanic "setIdExported" (ppr tv)
841
842 setIdNotExported :: Id -> Id
843 -- ^ We can only do this to LocalIds
844 setIdNotExported id = assert (isLocalId id) $
845 id { idScope = LocalId NotExported }
846
847 -----------------------
848 updateIdTypeButNotMult :: (Type -> Type) -> Id -> Id
849 updateIdTypeButNotMult f id = id { varType = f (varType id) }
850
851
852 updateIdTypeAndMult :: (Type -> Type) -> Id -> Id
853 updateIdTypeAndMult f id@(Id { varType = ty
854 , varMult = mult })
855 = id { varType = ty'
856 , varMult = mult' }
857 where
858 !ty' = f ty
859 !mult' = f mult
860 updateIdTypeAndMult _ other = pprPanic "updateIdTypeAndMult" (ppr other)
861
862 updateIdTypeAndMultM :: Monad m => (Type -> m Type) -> Id -> m Id
863 updateIdTypeAndMultM f id@(Id { varType = ty
864 , varMult = mult })
865 = do { !ty' <- f ty
866 ; !mult' <- f mult
867 ; return (id { varType = ty', varMult = mult' }) }
868 updateIdTypeAndMultM _ other = pprPanic "updateIdTypeAndMultM" (ppr other)
869
870 setIdMult :: Id -> Mult -> Id
871 setIdMult id !r | isId id = id { varMult = r }
872 | otherwise = pprPanic "setIdMult" (ppr id <+> ppr r)
873
874 {-
875 ************************************************************************
876 * *
877 \subsection{Predicates over variables}
878 * *
879 ************************************************************************
880 -}
881
882 -- | Is this a type-level (i.e., computationally irrelevant, thus erasable)
883 -- variable? Satisfies @isTyVar = not . isId@.
884 isTyVar :: Var -> Bool -- True of both TyVar and TcTyVar
885 isTyVar (TyVar {}) = True
886 isTyVar (TcTyVar {}) = True
887 isTyVar _ = False
888
889 isTcTyVar :: Var -> Bool -- True of TcTyVar only
890 isTcTyVar (TcTyVar {}) = True
891 isTcTyVar _ = False
892
893 isTyCoVar :: Var -> Bool
894 isTyCoVar v = isTyVar v || isCoVar v
895
896 -- | Is this a value-level (i.e., computationally relevant) 'Id'entifier?
897 -- Satisfies @isId = not . isTyVar@.
898 isId :: Var -> Bool
899 isId (Id {}) = True
900 isId _ = False
901
902 -- | Is this a coercion variable?
903 -- Satisfies @'isId' v ==> 'isCoVar' v == not ('isNonCoVarId' v)@.
904 isCoVar :: Var -> Bool
905 isCoVar (Id { id_details = details }) = isCoVarDetails details
906 isCoVar _ = False
907
908 -- | Is this a term variable ('Id') that is /not/ a coercion variable?
909 -- Satisfies @'isId' v ==> 'isCoVar' v == not ('isNonCoVarId' v)@.
910 isNonCoVarId :: Var -> Bool
911 isNonCoVarId (Id { id_details = details }) = not (isCoVarDetails details)
912 isNonCoVarId _ = False
913
914 isLocalId :: Var -> Bool
915 isLocalId (Id { idScope = LocalId _ }) = True
916 isLocalId _ = False
917
918 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
919 -- These are the variables that we need to pay attention to when finding free
920 -- variables, or doing dependency analysis.
921 isLocalVar :: Var -> Bool
922 isLocalVar v = not (isGlobalId v)
923
924 isGlobalId :: Var -> Bool
925 isGlobalId (Id { idScope = GlobalId }) = True
926 isGlobalId _ = False
927
928 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
929 -- that must have a binding in this module. The converse
930 -- is not quite right: there are some global 'Id's that must have
931 -- bindings, such as record selectors. But that doesn't matter,
932 -- because it's only used for assertions
933 mustHaveLocalBinding :: Var -> Bool
934 mustHaveLocalBinding var = isLocalVar var
935
936 -- | 'isExportedIdVar' means \"don't throw this away\"
937 isExportedId :: Var -> Bool
938 isExportedId (Id { idScope = GlobalId }) = True
939 isExportedId (Id { idScope = LocalId Exported}) = True
940 isExportedId _ = False