Coercion Quantification
[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 {- *********************************************************************
409 * *
410 * VarBndr, TyCoVarBinder
411 * *
412 ********************************************************************* -}
413
414 -- Variable Binder
415 --
416 -- VarBndr is polymorphic in both var and visibility fields.
417 -- Currently there are six different uses of 'VarBndr':
418 -- * Var.TyVarBinder = VarBndr TyVar ArgFlag
419 -- * Var.TyCoVarBinder = VarBndr TyCoVar ArgFlag
420 -- * TyCon.TyConBinder = VarBndr TyVar TyConBndrVis
421 -- * TyCon.TyConTyCoBinder = VarBndr TyCoVar TyConBndrVis
422 -- * IfaceType.IfaceForAllBndr = VarBndr IfaceBndr ArgFlag
423 -- * IfaceType.IfaceTyConBinder = VarBndr IfaceBndr TyConBndrVis
424 data VarBndr var argf = Bndr var argf
425 deriving( Data )
426
427 -- | Variable Binder
428 --
429 -- A 'TyCoVarBinder' is the binder of a ForAllTy
430 -- It's convenient to define this synonym here rather its natural
431 -- home in TyCoRep, because it's used in DataCon.hs-boot
432 --
433 -- A 'TyVarBinder' is a binder with only TyVar
434 type TyCoVarBinder = VarBndr TyCoVar ArgFlag
435 type TyVarBinder = VarBndr TyVar ArgFlag
436
437 binderVar :: VarBndr tv argf -> tv
438 binderVar (Bndr v _) = v
439
440 binderVars :: [VarBndr tv argf] -> [tv]
441 binderVars tvbs = map binderVar tvbs
442
443 binderArgFlag :: VarBndr tv argf -> argf
444 binderArgFlag (Bndr _ argf) = argf
445
446 binderType :: VarBndr TyCoVar argf -> Type
447 binderType (Bndr tv _) = varType tv
448
449 -- | Make a named binder
450 mkTyCoVarBinder :: ArgFlag -> TyCoVar -> TyCoVarBinder
451 mkTyCoVarBinder vis var = Bndr var vis
452
453 -- | Make a named binder
454 -- 'var' should be a type variable
455 mkTyVarBinder :: ArgFlag -> TyVar -> TyVarBinder
456 mkTyVarBinder vis var
457 = ASSERT( isTyVar var )
458 Bndr var vis
459
460 -- | Make many named binders
461 mkTyCoVarBinders :: ArgFlag -> [TyCoVar] -> [TyCoVarBinder]
462 mkTyCoVarBinders vis = map (mkTyCoVarBinder vis)
463
464 -- | Make many named binders
465 -- Input vars should be type variables
466 mkTyVarBinders :: ArgFlag -> [TyVar] -> [TyVarBinder]
467 mkTyVarBinders vis = map (mkTyVarBinder vis)
468
469 isTyVarBinder :: TyCoVarBinder -> Bool
470 isTyVarBinder (Bndr v _) = isTyVar v
471
472 {-
473 ************************************************************************
474 * *
475 * Type and kind variables *
476 * *
477 ************************************************************************
478 -}
479
480 tyVarName :: TyVar -> Name
481 tyVarName = varName
482
483 tyVarKind :: TyVar -> Kind
484 tyVarKind = varType
485
486 setTyVarUnique :: TyVar -> Unique -> TyVar
487 setTyVarUnique = setVarUnique
488
489 setTyVarName :: TyVar -> Name -> TyVar
490 setTyVarName = setVarName
491
492 setTyVarKind :: TyVar -> Kind -> TyVar
493 setTyVarKind tv k = tv {varType = k}
494
495 updateTyVarKind :: (Kind -> Kind) -> TyVar -> TyVar
496 updateTyVarKind update tv = tv {varType = update (tyVarKind tv)}
497
498 updateTyVarKindM :: (Monad m) => (Kind -> m Kind) -> TyVar -> m TyVar
499 updateTyVarKindM update tv
500 = do { k' <- update (tyVarKind tv)
501 ; return $ tv {varType = k'} }
502
503 mkTyVar :: Name -> Kind -> TyVar
504 mkTyVar name kind = TyVar { varName = name
505 , realUnique = getKey (nameUnique name)
506 , varType = kind
507 }
508
509 mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
510 mkTcTyVar name kind details
511 = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
512 TcTyVar { varName = name,
513 realUnique = getKey (nameUnique name),
514 varType = kind,
515 tc_tv_details = details
516 }
517
518 tcTyVarDetails :: TyVar -> TcTyVarDetails
519 -- See Note [TcTyVars in the typechecker] in TcType
520 tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
521 tcTyVarDetails (TyVar {}) = vanillaSkolemTv
522 tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var <+> dcolon <+> pprKind (tyVarKind var))
523
524 setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
525 setTcTyVarDetails tv details = tv { tc_tv_details = details }
526
527 -------------------------------------
528 instance Outputable tv => Outputable (VarBndr tv ArgFlag) where
529 ppr (Bndr v Required) = ppr v
530 ppr (Bndr v Specified) = char '@' <> ppr v
531 ppr (Bndr v Inferred) = braces (ppr v)
532
533 instance Outputable ArgFlag where
534 ppr Required = text "[req]"
535 ppr Specified = text "[spec]"
536 ppr Inferred = text "[infrd]"
537
538 instance (Binary tv, Binary vis) => Binary (VarBndr tv vis) where
539 put_ bh (Bndr tv vis) = do { put_ bh tv; put_ bh vis }
540
541 get bh = do { tv <- get bh; vis <- get bh; return (Bndr tv vis) }
542
543
544 instance Binary ArgFlag where
545 put_ bh Required = putByte bh 0
546 put_ bh Specified = putByte bh 1
547 put_ bh Inferred = putByte bh 2
548
549 get bh = do
550 h <- getByte bh
551 case h of
552 0 -> return Required
553 1 -> return Specified
554 _ -> return Inferred
555
556 {-
557 %************************************************************************
558 %* *
559 \subsection{Ids}
560 * *
561 ************************************************************************
562 -}
563
564 idInfo :: HasDebugCallStack => Id -> IdInfo
565 idInfo (Id { id_info = info }) = info
566 idInfo other = pprPanic "idInfo" (ppr other)
567
568 idDetails :: Id -> IdDetails
569 idDetails (Id { id_details = details }) = details
570 idDetails other = pprPanic "idDetails" (ppr other)
571
572 -- The next three have a 'Var' suffix even though they always build
573 -- Ids, because Id.hs uses 'mkGlobalId' etc with different types
574 mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
575 mkGlobalVar details name ty info
576 = mk_id name ty GlobalId details info
577
578 mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
579 mkLocalVar details name ty info
580 = mk_id name ty (LocalId NotExported) details info
581
582 mkCoVar :: Name -> Type -> CoVar
583 -- Coercion variables have no IdInfo
584 mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
585
586 -- | Exported 'Var's will not be removed as dead code
587 mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
588 mkExportedLocalVar details name ty info
589 = mk_id name ty (LocalId Exported) details info
590
591 mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
592 mk_id name ty scope details info
593 = Id { varName = name,
594 realUnique = getKey (nameUnique name),
595 varType = ty,
596 idScope = scope,
597 id_details = details,
598 id_info = info }
599
600 -------------------
601 lazySetIdInfo :: Id -> IdInfo -> Var
602 lazySetIdInfo id info = id { id_info = info }
603
604 setIdDetails :: Id -> IdDetails -> Id
605 setIdDetails id details = id { id_details = details }
606
607 globaliseId :: Id -> Id
608 -- ^ If it's a local, make it global
609 globaliseId id = id { idScope = GlobalId }
610
611 setIdExported :: Id -> Id
612 -- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
613 -- and class operations, which are born as global 'Id's and automatically exported
614 setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
615 setIdExported id@(Id { idScope = GlobalId }) = id
616 setIdExported tv = pprPanic "setIdExported" (ppr tv)
617
618 setIdNotExported :: Id -> Id
619 -- ^ We can only do this to LocalIds
620 setIdNotExported id = ASSERT( isLocalId id )
621 id { idScope = LocalId NotExported }
622
623 {-
624 ************************************************************************
625 * *
626 \subsection{Predicates over variables}
627 * *
628 ************************************************************************
629 -}
630
631 isTyVar :: Var -> Bool -- True of both TyVar and TcTyVar
632 isTyVar (TyVar {}) = True
633 isTyVar (TcTyVar {}) = True
634 isTyVar _ = False
635
636 isTcTyVar :: Var -> Bool -- True of TcTyVar only
637 isTcTyVar (TcTyVar {}) = True
638 isTcTyVar _ = False
639
640 isTyCoVar :: Var -> Bool
641 isTyCoVar v = isTyVar v || isCoVar v
642
643 isId :: Var -> Bool
644 isId (Id {}) = True
645 isId _ = False
646
647 isCoVar :: Var -> Bool
648 -- A coercion variable
649 isCoVar (Id { id_details = details }) = isCoVarDetails details
650 isCoVar _ = False
651
652 isNonCoVarId :: Var -> Bool
653 -- A term variable (Id) that is /not/ a coercion variable
654 isNonCoVarId (Id { id_details = details }) = not (isCoVarDetails details)
655 isNonCoVarId _ = False
656
657 isLocalId :: Var -> Bool
658 isLocalId (Id { idScope = LocalId _ }) = True
659 isLocalId _ = False
660
661 -- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
662 -- These are the variables that we need to pay attention to when finding free
663 -- variables, or doing dependency analysis.
664 isLocalVar :: Var -> Bool
665 isLocalVar v = not (isGlobalId v)
666
667 isGlobalId :: Var -> Bool
668 isGlobalId (Id { idScope = GlobalId }) = True
669 isGlobalId _ = False
670
671 -- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
672 -- that must have a binding in this module. The converse
673 -- is not quite right: there are some global 'Id's that must have
674 -- bindings, such as record selectors. But that doesn't matter,
675 -- because it's only used for assertions
676 mustHaveLocalBinding :: Var -> Bool
677 mustHaveLocalBinding var = isLocalVar var
678
679 -- | 'isExportedIdVar' means \"don't throw this away\"
680 isExportedId :: Var -> Bool
681 isExportedId (Id { idScope = GlobalId }) = True
682 isExportedId (Id { idScope = LocalId Exported}) = True
683 isExportedId _ = False