Smarter HsType pretty-print for promoted datacons
[ghc.git] / compiler / basicTypes / Var.hs
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 CPP, FlexibleContexts, MultiWayIf, FlexibleInstances, DeriveDataTypeable #-}
9
10 -- |
11 -- #name_types#
12 -- GHC uses several kinds of name internally:
13 --
14 -- * 'OccName.OccName': see "OccName#name_types"
15 --
16 -- * 'RdrName.RdrName': see "RdrName#name_types"
17 --
18 -- * 'Name.Name': see "Name#name_types"
19 --
20 -- * 'Id.Id': see "Id#name_types"
21 --
22 -- * 'Var.Var' is a synonym for the 'Id.Id' type but it may additionally
23 -- potentially contain type variables, which have a 'TyCoRep.Kind'
24 -- rather than a 'TyCoRep.Type' and only contain some extra
25 -- details during typechecking.
26 --
27 -- These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
28 --
29 -- #globalvslocal#
30 -- Global 'Id's and 'Var's are those that are imported or correspond
31 -- to a data constructor, primitive operation, or record selectors.
32 -- Local 'Id's and 'Var's are those bound within an expression
33 -- (e.g. by a lambda) or at the top level of the module being compiled.
34
35 module Var (
36 -- * The main data type and synonyms
37 Var, CoVar, Id, NcId, DictId, DFunId, EvVar, EqVar, EvId, IpId, JoinId,
38 TyVar, TcTyVar, TypeVar, KindVar, TKVar, TyCoVar,
39
40 -- * In and Out variants
41 InVar, InCoVar, InId, InTyVar,
42 OutVar, OutCoVar, OutId, OutTyVar,
43
44 -- ** Taking 'Var's apart
45 varName, varUnique, varType,
46
47 -- ** Modifying 'Var's
48 setVarName, setVarUnique, setVarType, updateVarType,
49 updateVarTypeM,
50
51 -- ** Constructing, taking apart, modifying 'Id's
52 mkGlobalVar, mkLocalVar, mkExportedLocalVar, mkCoVar,
53 idInfo, idDetails,
54 lazySetIdInfo, setIdDetails, globaliseId,
55 setIdExported, setIdNotExported,
56
57 -- ** Predicates
58 isId, isTyVar, isTcTyVar,
59 isLocalVar, isLocalId, isCoVar, isNonCoVarId, isTyCoVar,
60 isGlobalId, isExportedId,
61 mustHaveLocalBinding,
62
63 -- * TyVar's
64 VarBndr(..), ArgFlag(..), TyCoVarBinder, TyVarBinder,
65 binderVar, binderVars, binderArgFlag, binderType,
66 isVisibleArgFlag, isInvisibleArgFlag, sameVis,
67 mkTyCoVarBinder, mkTyCoVarBinders,
68 mkTyVarBinder, mkTyVarBinders,
69 isTyVarBinder,
70
71 -- ** Constructing TyVar's
72 mkTyVar, mkTcTyVar,
73
74 -- ** Taking 'TyVar's apart
75 tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
76
77 -- ** Modifying 'TyVar's
78 setTyVarName, setTyVarUnique, setTyVarKind, updateTyVarKind,
79 updateTyVarKindM,
80
81 nonDetCmpVar
82
83 ) where
84
85 #include "HsVersions.h"
86
87 import GhcPrelude
88
89 import {-# SOURCE #-} TyCoRep( Type, Kind, pprKind )
90 import {-# SOURCE #-} TcType( TcTyVarDetails, pprTcTyVarDetails, vanillaSkolemTv )
91 import {-# SOURCE #-} IdInfo( IdDetails, IdInfo, coVarDetails, isCoVarDetails,
92 vanillaIdInfo, pprIdDetails )
93
94 import Name hiding (varName)
95 import Unique ( Uniquable, Unique, getKey, getUnique
96 , mkUniqueGrimily, nonDetCmpUnique )
97 import Util
98 import Binary
99 import DynFlags
100 import Outputable
101
102 import Data.Data
103
104 {-
105 ************************************************************************
106 * *
107 Synonyms
108 * *
109 ************************************************************************
110 -- These synonyms are here and not in Id because otherwise we need a very
111 -- large number of SOURCE imports of Id.hs :-(
112 -}
113
114 -- | Identifier
115 type Id = Var -- A term-level identifier
116 -- predicate: isId
117
118 -- | Coercion Variable
119 type CoVar = Id -- See Note [Evidence: EvIds and CoVars]
120 -- predicate: isCoVar
121
122 -- |
123 type NcId = Id -- A term-level (value) variable that is
124 -- /not/ an (unlifted) coercion
125 -- predicate: isNonCoVarId
126
127 -- | Type or kind Variable
128 type TyVar = Var -- Type *or* kind variable (historical)
129
130 -- | Type or Kind Variable
131 type TKVar = Var -- Type *or* kind variable (historical)
132
133 -- | Type variable that might be a metavariable
134 type TcTyVar = Var
135
136 -- | Type Variable
137 type TypeVar = Var -- Definitely a type variable
138
139 -- | Kind Variable
140 type KindVar = Var -- Definitely a kind variable
141 -- See Note [Kind and type variables]
142
143 -- See Note [Evidence: EvIds and CoVars]
144 -- | Evidence Identifier
145 type EvId = Id -- Term-level evidence: DictId, IpId, or EqVar
146
147 -- | Evidence Variable
148 type EvVar = EvId -- ...historical name for EvId
149
150 -- | Dictionary Function Identifier
151 type DFunId = Id -- A dictionary function
152
153 -- | Dictionary Identifier
154 type DictId = EvId -- A dictionary variable
155
156 -- | Implicit parameter Identifier
157 type IpId = EvId -- A term-level implicit parameter
158
159 -- | Equality Variable
160 type EqVar = EvId -- Boxed equality evidence
161 type JoinId = Id -- A join variable
162
163 -- | Type or Coercion Variable
164 type TyCoVar = Id -- Type, *or* coercion variable
165 -- predicate: isTyCoVar
166
167
168 {- Many passes apply a substitution, and it's very handy to have type
169 synonyms to remind us whether or not the substitution has been applied -}
170
171 type InVar = Var
172 type InTyVar = TyVar
173 type InCoVar = CoVar
174 type InId = Id
175 type OutVar = Var
176 type OutTyVar = TyVar
177 type OutCoVar = CoVar
178 type OutId = Id
179
180
181
182 {- Note [Evidence: EvIds and CoVars]
183 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 * An EvId (evidence Id) is a term-level evidence variable
185 (dictionary, implicit parameter, or equality). Could be boxed or unboxed.
186
187 * DictId, IpId, and EqVar are synonyms when we know what kind of
188 evidence we are talking about. For example, an EqVar has type (t1 ~ t2).
189
190 * A CoVar is always an un-lifted coercion, of type (t1 ~# t2) or (t1 ~R# t2)
191
192 Note [Kind and type variables]
193 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 Before kind polymorphism, TyVar were used to mean type variables. Now
195 they are used to mean kind *or* type variables. KindVar is used when we
196 know for sure that it is a kind variable. In future, we might want to
197 go over the whole compiler code to use:
198 - TKVar to mean kind or type variables
199 - TypeVar to mean type variables only
200 - KindVar to mean kind variables
201
202
203 ************************************************************************
204 * *
205 \subsection{The main data type declarations}
206 * *
207 ************************************************************************
208
209
210 Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
211 @Type@, and an @IdInfo@ (non-essential info about it, e.g.,
212 strictness). The essential info about different kinds of @Vars@ is
213 in its @VarDetails@.
214 -}
215
216 -- | Variable
217 --
218 -- Essentially a typed 'Name', that may also contain some additional information
219 -- about the 'Var' and it's use sites.
220 data Var
221 = TyVar { -- Type and kind variables
222 -- see Note [Kind and type variables]
223 varName :: !Name,
224 realUnique :: {-# UNPACK #-} !Int,
225 -- ^ Key for fast comparison
226 -- Identical to the Unique in the name,
227 -- cached here for speed
228 varType :: Kind -- ^ The type or kind of the 'Var' in question
229 }
230
231 | TcTyVar { -- Used only during type inference
232 -- Used for kind variables during
233 -- inference, as well
234 varName :: !Name,
235 realUnique :: {-# UNPACK #-} !Int,
236 varType :: Kind,
237 tc_tv_details :: TcTyVarDetails
238 }
239
240 | Id {
241 varName :: !Name,
242 realUnique :: {-# UNPACK #-} !Int,
243 varType :: Type,
244 idScope :: IdScope,
245 id_details :: IdDetails, -- Stable, doesn't change
246 id_info :: IdInfo } -- Unstable, updated by simplifier
247
248 -- | Identifier Scope
249 data IdScope -- See Note [GlobalId/LocalId]
250 = GlobalId
251 | LocalId ExportFlag
252
253 data ExportFlag -- See Note [ExportFlag on binders]
254 = NotExported -- ^ Not exported: may be discarded as dead code.
255 | Exported -- ^ Exported: kept alive
256
257 {- Note [ExportFlag on binders]
258 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
259 An ExportFlag of "Exported" on a top-level binder says "keep this
260 binding alive; do not drop it as dead code". This transitively
261 keeps alive all the other top-level bindings that this binding refers
262 to. This property is persisted all the way down the pipeline, so that
263 the binding will be compiled all the way to object code, and its
264 symbols will appear in the linker symbol table.
265
266 However, note that this use of "exported" is quite different to the
267 export list on a Haskell module. Setting the ExportFlag on an Id does
268 /not/ mean that if you import the module (in Haskell source code) you
269 will see this Id. Of course, things that appear in the export list
270 of the source Haskell module do indeed have their ExportFlag set.
271 But many other things, such as dictionary functions, are kept alive
272 by having their ExportFlag set, even though they are not exported
273 in the source-code sense.
274
275 We should probably use a different term for ExportFlag, like
276 KeepAlive.
277
278 Note [GlobalId/LocalId]
279 ~~~~~~~~~~~~~~~~~~~~~~~
280 A GlobalId is
281 * always a constant (top-level)
282 * imported, or data constructor, or primop, or record selector
283 * has a Unique that is globally unique across the whole
284 GHC invocation (a single invocation may compile multiple modules)
285 * never treated as a candidate by the free-variable finder;
286 it's a constant!
287
288 A LocalId is
289 * bound within an expression (lambda, case, local let(rec))
290 * or defined at top level in the module being compiled
291 * always treated as a candidate by the free-variable finder
292
293 After CoreTidy, top-level LocalIds are turned into GlobalIds
294 -}
295
296 instance Outputable Var where
297 ppr var = sdocWithDynFlags $ \dflags ->
298 getPprStyle $ \ppr_style ->
299 if | debugStyle ppr_style && (not (gopt Opt_SuppressVarKinds dflags))
300 -> parens (ppr (varName var) <+> ppr_debug var ppr_style <+>
301 dcolon <+> pprKind (tyVarKind var))
302 | otherwise
303 -> ppr (varName var) <> ppr_debug var ppr_style
304
305 ppr_debug :: Var -> PprStyle -> SDoc
306 ppr_debug (TyVar {}) sty
307 | debugStyle sty = brackets (text "tv")
308 ppr_debug (TcTyVar {tc_tv_details = d}) sty
309 | dumpStyle sty || debugStyle sty = brackets (pprTcTyVarDetails d)
310 ppr_debug (Id { idScope = s, id_details = d }) sty
311 | debugStyle sty = brackets (ppr_id_scope s <> pprIdDetails d)
312 ppr_debug _ _ = empty
313
314 ppr_id_scope :: IdScope -> SDoc
315 ppr_id_scope GlobalId = text "gid"
316 ppr_id_scope (LocalId Exported) = text "lidx"
317 ppr_id_scope (LocalId NotExported) = text "lid"
318
319 instance NamedThing Var where
320 getName = varName
321
322 instance Uniquable Var where
323 getUnique = varUnique
324
325 instance Eq Var where
326 a == b = realUnique a == realUnique b
327
328 instance Ord Var where
329 a <= b = realUnique a <= realUnique b
330 a < b = realUnique a < realUnique b
331 a >= b = realUnique a >= realUnique b
332 a > b = realUnique a > realUnique b
333 a `compare` b = a `nonDetCmpVar` b
334
335 -- | Compare Vars by their Uniques.
336 -- This is what Ord Var does, provided here to make it explicit at the
337 -- call-site that it can introduce non-determinism.
338 -- See Note [Unique Determinism]
339 nonDetCmpVar :: Var -> Var -> Ordering
340 nonDetCmpVar a b = varUnique a `nonDetCmpUnique` varUnique b
341
342 instance Data Var where
343 -- don't traverse?
344 toConstr _ = abstractConstr "Var"
345 gunfold _ _ = error "gunfold"
346 dataTypeOf _ = mkNoRepType "Var"
347
348 instance HasOccName Var where
349 occName = nameOccName . varName
350
351 varUnique :: Var -> Unique
352 varUnique var = mkUniqueGrimily (realUnique var)
353
354 setVarUnique :: Var -> Unique -> Var
355 setVarUnique var uniq
356 = var { realUnique = getKey uniq,
357 varName = setNameUnique (varName var) uniq }
358
359 setVarName :: Var -> Name -> Var
360 setVarName var new_name
361 = var { realUnique = getKey (getUnique new_name),
362 varName = new_name }
363
364 setVarType :: Id -> Type -> Id
365 setVarType id ty = id { varType = ty }
366
367 updateVarType :: (Type -> Type) -> Id -> Id
368 updateVarType f id = id { varType = f (varType id) }
369
370 updateVarTypeM :: Monad m => (Type -> m Type) -> Id -> m Id
371 updateVarTypeM f id = do { ty' <- f (varType id)
372 ; return (id { varType = ty' }) }
373
374 {- *********************************************************************
375 * *
376 * ArgFlag
377 * *
378 ********************************************************************* -}
379
380 -- | Argument Flag
381 --
382 -- Is something required to appear in source Haskell ('Required'),
383 -- permitted by request ('Specified') (visible type application), or
384 -- prohibited entirely from appearing in source Haskell ('Inferred')?
385 -- See Note [VarBndrs, TyCoVarBinders, TyConBinders, and visibility] in TyCoRep
386 data ArgFlag = Inferred | Specified | Required
387 deriving (Eq, Ord, Data)
388 -- (<) on ArgFlag meant "is less visible than"
389
390 -- | Does this 'ArgFlag' classify an argument that is written in Haskell?
391 isVisibleArgFlag :: ArgFlag -> Bool
392 isVisibleArgFlag Required = True
393 isVisibleArgFlag _ = False
394
395 -- | Does this 'ArgFlag' classify an argument that is not written in Haskell?
396 isInvisibleArgFlag :: ArgFlag -> Bool
397 isInvisibleArgFlag = not . isVisibleArgFlag
398
399 -- | Do these denote the same level of visibility? 'Required'
400 -- arguments are visible, others are not. So this function
401 -- equates 'Specified' and 'Inferred'. Used for printing.
402 sameVis :: ArgFlag -> ArgFlag -> Bool
403 sameVis Required Required = True
404 sameVis Required _ = False
405 sameVis _ Required = False
406 sameVis _ _ = True
407
408 instance Outputable ArgFlag where
409 ppr Required = text "[req]"
410 ppr Specified = text "[spec]"
411 ppr Inferred = text "[infrd]"
412
413 instance Binary ArgFlag where
414 put_ bh Required = putByte bh 0
415 put_ bh Specified = putByte bh 1
416 put_ bh Inferred = putByte bh 2
417
418 get bh = do
419 h <- getByte bh
420 case h of
421 0 -> return Required
422 1 -> return Specified
423 _ -> return Inferred
424
425 {- *********************************************************************
426 * *
427 * VarBndr, TyCoVarBinder
428 * *
429 ********************************************************************* -}
430
431 -- Variable Binder
432 --
433 -- VarBndr is polymorphic in both var and visibility fields.
434 -- Currently there are six different uses of 'VarBndr':
435 -- * Var.TyVarBinder = VarBndr TyVar ArgFlag
436 -- * Var.TyCoVarBinder = VarBndr TyCoVar ArgFlag
437 -- * TyCon.TyConBinder = VarBndr TyVar TyConBndrVis
438 -- * TyCon.TyConTyCoBinder = VarBndr TyCoVar TyConBndrVis
439 -- * IfaceType.IfaceForAllBndr = VarBndr IfaceBndr ArgFlag
440 -- * IfaceType.IfaceTyConBinder = VarBndr IfaceBndr TyConBndrVis
441 data VarBndr var argf = Bndr var argf
442 deriving( Data )
443
444 -- | Variable Binder
445 --
446 -- A 'TyCoVarBinder' is the binder of a ForAllTy
447 -- It's convenient to define this synonym here rather its natural
448 -- home in TyCoRep, because it's used in DataCon.hs-boot
449 --
450 -- A 'TyVarBinder' is a binder with only TyVar
451 type TyCoVarBinder = VarBndr TyCoVar ArgFlag
452 type TyVarBinder = VarBndr TyVar ArgFlag
453
454 binderVar :: VarBndr tv argf -> tv
455 binderVar (Bndr v _) = v
456
457 binderVars :: [VarBndr tv argf] -> [tv]
458 binderVars tvbs = map binderVar tvbs
459
460 binderArgFlag :: VarBndr tv argf -> argf
461 binderArgFlag (Bndr _ argf) = argf
462
463 binderType :: VarBndr TyCoVar argf -> Type
464 binderType (Bndr tv _) = varType tv
465
466 -- | Make a named binder
467 mkTyCoVarBinder :: ArgFlag -> TyCoVar -> TyCoVarBinder
468 mkTyCoVarBinder vis var = Bndr var vis
469
470 -- | Make a named binder
471 -- 'var' should be a type variable
472 mkTyVarBinder :: ArgFlag -> TyVar -> TyVarBinder
473 mkTyVarBinder vis var
474 = ASSERT( isTyVar var )
475 Bndr var vis
476
477 -- | Make many named binders
478 mkTyCoVarBinders :: ArgFlag -> [TyCoVar] -> [TyCoVarBinder]
479 mkTyCoVarBinders vis = map (mkTyCoVarBinder vis)
480
481 -- | Make many named binders
482 -- Input vars should be type variables
483 mkTyVarBinders :: ArgFlag -> [TyVar] -> [TyVarBinder]
484 mkTyVarBinders vis = map (mkTyVarBinder vis)
485
486 isTyVarBinder :: TyCoVarBinder -> Bool
487 isTyVarBinder (Bndr v _) = isTyVar v
488
489 instance Outputable tv => Outputable (VarBndr tv ArgFlag) where
490 ppr (Bndr v Required) = ppr v
491 ppr (Bndr v Specified) = char '@' <> ppr v
492 ppr (Bndr v Inferred) = braces (ppr v)
493
494 instance (Binary tv, Binary vis) => Binary (VarBndr tv vis) where
495 put_ bh (Bndr tv vis) = do { put_ bh tv; put_ bh vis }
496
497 get bh = do { tv <- get bh; vis <- get bh; return (Bndr tv vis) }
498
499 instance NamedThing tv => NamedThing (VarBndr tv flag) where
500 getName (Bndr tv _) = getName tv
501
502 {-
503 ************************************************************************
504 * *
505 * Type and kind variables *
506 * *
507 ************************************************************************
508 -}
509
510 tyVarName :: TyVar -> Name
511 tyVarName = varName
512
513 tyVarKind :: TyVar -> Kind
514 tyVarKind = varType
515
516 setTyVarUnique :: TyVar -> Unique -> TyVar
517 setTyVarUnique = setVarUnique
518
519 setTyVarName :: TyVar -> Name -> TyVar
520 setTyVarName = setVarName
521
522 setTyVarKind :: TyVar -> Kind -> TyVar
523 setTyVarKind tv k = tv {varType = k}
524
525 updateTyVarKind :: (Kind -> Kind) -> TyVar -> TyVar
526 updateTyVarKind update tv = tv {varType = update (tyVarKind tv)}
527
528 updateTyVarKindM :: (Monad m) => (Kind -> m Kind) -> TyVar -> m TyVar
529 updateTyVarKindM update tv
530 = do { k' <- update (tyVarKind tv)
531 ; return $ tv {varType = k'} }
532
533 mkTyVar :: Name -> Kind -> TyVar
534 mkTyVar name kind = TyVar { varName = name
535 , realUnique = getKey (nameUnique name)
536 , varType = kind
537 }
538
539 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
540 mkTcTyVar name kind details
541 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
542 TcTyVar { varName = name,
543 realUnique = getKey (nameUnique name),
544 varType = kind,
545 tc_tv_details = details
546 }
547
548 tcTyVarDetails :: TyVar -> TcTyVarDetails
549 -- See Note [TcTyVars in the typechecker] in TcType
550 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
551 tcTyVarDetails (TyVar {}) = vanillaSkolemTv
552 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var <+> dcolon <+> pprKind (tyVarKind var))
553
554 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
555 setTcTyVarDetails tv details = tv { tc_tv_details = details }
556
557 {-
558 %************************************************************************
559 %* *
560 \subsection{Ids}
561 * *
562 ************************************************************************
563 -}
564
565 idInfo :: HasDebugCallStack => Id -> IdInfo
566 idInfo (Id { id_info = info }) = info
567 idInfo other = pprPanic "idInfo" (ppr other)
568
569 idDetails :: Id -> IdDetails
570 idDetails (Id { id_details = details }) = details
571 idDetails other = pprPanic "idDetails" (ppr other)
572
573 -- The next three have a 'Var' suffix even though they always build
574 -- Ids, because Id.hs uses 'mkGlobalId' etc with different types
575 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
576 mkGlobalVar details name ty info
577 = mk_id name ty GlobalId details info
578
579 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
580 mkLocalVar details name ty info
581 = mk_id name ty (LocalId NotExported) details info
582
583 mkCoVar :: Name -> Type -> CoVar
584 -- Coercion variables have no IdInfo
585 mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
586
587 -- | Exported 'Var's will not be removed as dead code
588 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
589 mkExportedLocalVar details name ty info
590 = mk_id name ty (LocalId Exported) details info
591
592 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
593 mk_id name ty scope details info
594 = Id { varName = name,
595 realUnique = getKey (nameUnique name),
596 varType = ty,
597 idScope = scope,
598 id_details = details,
599 id_info = info }
600
601 -------------------
602 lazySetIdInfo :: Id -> IdInfo -> Var
603 lazySetIdInfo id info = id { id_info = info }
604
605 setIdDetails :: Id -> IdDetails -> Id
606 setIdDetails id details = id { id_details = details }
607
608 globaliseId :: Id -> Id
609 -- ^ If it's a local, make it global
610 globaliseId id = id { idScope = GlobalId }
611
612 setIdExported :: Id -> Id
613 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
614 -- and class operations, which are born as global 'Id's and automatically exported
615 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
616 setIdExported id@(Id { idScope = GlobalId }) = id
617 setIdExported tv = pprPanic "setIdExported" (ppr tv)
618
619 setIdNotExported :: Id -> Id
620 -- ^ We can only do this to LocalIds
621 setIdNotExported id = ASSERT( isLocalId id )
622 id { idScope = LocalId NotExported }
623
624 {-
625 ************************************************************************
626 * *
627 \subsection{Predicates over variables}
628 * *
629 ************************************************************************
630 -}
631
632 isTyVar :: Var -> Bool -- True of both TyVar and TcTyVar
633 isTyVar (TyVar {}) = True
634 isTyVar (TcTyVar {}) = True
635 isTyVar _ = False
636
637 isTcTyVar :: Var -> Bool -- True of TcTyVar only
638 isTcTyVar (TcTyVar {}) = True
639 isTcTyVar _ = False
640
641 isTyCoVar :: Var -> Bool
642 isTyCoVar v = isTyVar v || isCoVar v
643
644 isId :: Var -> Bool
645 isId (Id {}) = True
646 isId _ = False
647
648 isCoVar :: Var -> Bool
649 -- A coercion variable
650 isCoVar (Id { id_details = details }) = isCoVarDetails details
651 isCoVar _ = False
652
653 isNonCoVarId :: Var -> Bool
654 -- A term variable (Id) that is /not/ a coercion variable
655 isNonCoVarId (Id { id_details = details }) = not (isCoVarDetails details)
656 isNonCoVarId _ = False
657
658 isLocalId :: Var -> Bool
659 isLocalId (Id { idScope = LocalId _ }) = True
660 isLocalId _ = False
661
662 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
663 -- These are the variables that we need to pay attention to when finding free
664 -- variables, or doing dependency analysis.
665 isLocalVar :: Var -> Bool
666 isLocalVar v = not (isGlobalId v)
667
668 isGlobalId :: Var -> Bool
669 isGlobalId (Id { idScope = GlobalId }) = True
670 isGlobalId _ = False
671
672 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
673 -- that must have a binding in this module. The converse
674 -- is not quite right: there are some global 'Id's that must have
675 -- bindings, such as record selectors. But that doesn't matter,
676 -- because it's only used for assertions
677 mustHaveLocalBinding :: Var -> Bool
678 mustHaveLocalBinding var = isLocalVar var
679
680 -- | 'isExportedIdVar' means \"don't throw this away\"
681 isExportedId :: Var -> Bool
682 isExportedId (Id { idScope = GlobalId }) = True
683 isExportedId (Id { idScope = LocalId Exported}) = True
684 isExportedId _ = False