root/compiler/basicTypes/Var.lhs

Revision c676a15e9b0ead9d9322d036d9cf2f4df734bb01, 14.6 KB (checked in by Simon Peyton Jones <simonpj@…>, 3 months ago)

Define TypeVar? (like KindVar?), isTypeVar, isKindVar, and use them

This is the start of more global renaming

  • Property mode set to 100644
Line 
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\begin{code}
8{-# OPTIONS -fno-warn-tabs #-}
9-- The above warning supression flag is a temporary kludge.
10-- While working on this module you are encouraged to remove it and
11-- detab the module (please do the detabbing in a separate patch). See
12--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
13-- for details
14
15-- |
16-- #name_types#
17-- GHC uses several kinds of name internally:
18--
19-- * 'OccName.OccName': see "OccName#name_types"
20--
21-- * 'RdrName.RdrName': see "RdrName#name_types"
22--
23-- * 'Name.Name': see "Name#name_types"
24--
25-- * 'Id.Id': see "Id#name_types"
26--
27-- * 'Var.Var' is a synonym for the 'Id.Id' type but it may additionally
28--   potentially contain type variables, which have a 'TypeRep.Kind'
29--   rather than a 'TypeRep.Type' and only contain some extra
30--   details during typechecking.
31--
32--   These 'Var.Var' names may either be global or local, see "Var#globalvslocal"
33--
34-- #globalvslocal#
35-- Global 'Id's and 'Var's are those that are imported or correspond
36--    to a data constructor, primitive operation, or record selectors.
37-- Local 'Id's and 'Var's are those bound within an expression
38--    (e.g. by a lambda) or at the top level of the module being compiled.
39
40module Var (
41        -- * The main data type and synonyms
42        Var, CoVar, Id, DictId, DFunId, EvVar, EqVar, EvId, IpId,
43        TyVar, TypeVar, KindVar, TKVar,
44
45        -- ** Taking 'Var's apart
46        varName, varUnique, varType, 
47
48        -- ** Modifying 'Var's
49        setVarName, setVarUnique, setVarType,
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, isTKVar, isTyVar, isTcTyVar,
59        isLocalVar, isLocalId,
60        isGlobalId, isExportedId,
61        mustHaveLocalBinding,
62
63        -- ** Constructing 'TyVar's
64        mkTyVar, mkTcTyVar, mkKindVar,
65
66        -- ** Taking 'TyVar's apart
67        tyVarName, tyVarKind, tcTyVarDetails, setTcTyVarDetails,
68
69        -- ** Modifying 'TyVar's
70        setTyVarName, setTyVarUnique, setTyVarKind, updateTyVarKind,
71        updateTyVarKindM
72
73    ) where
74
75#include "HsVersions.h"
76#include "Typeable.h"
77
78import {-# SOURCE #-}   TypeRep( Type, Kind, SuperKind )
79import {-# SOURCE #-}   TcType( TcTyVarDetails, pprTcTyVarDetails )
80import {-# SOURCE #-}   IdInfo( IdDetails, IdInfo, coVarDetails, vanillaIdInfo, pprIdDetails )
81
82import Name hiding (varName)
83import Unique
84import Util
85import FastTypes
86import FastString
87import Outputable
88
89-- import StaticFlags ( opt_SuppressVarKinds )
90
91import Data.Data
92\end{code}
93
94
95%************************************************************************
96%*                                                                      *
97                     Synonyms                                                                   
98%*                                                                      *
99%************************************************************************
100-- These synonyms are here and not in Id because otherwise we need a very
101-- large number of SOURCE imports of Id.hs :-(
102
103\begin{code}
104type Id    = Var       -- A term-level identifier
105
106type TyVar   = Var     -- Type *or* kind variable (historical)
107
108type TKVar   = Var     -- Type *or* kind variable (historical)
109type TypeVar = Var     -- Definitely a type variable
110type KindVar = Var     -- Definitely a kind variable
111                       -- See Note [Kind and type variables]
112
113-- See Note [Evidence: EvIds and CoVars]
114type EvId   = Id        -- Term-level evidence: DictId, IpId, or EqVar
115type EvVar  = EvId      -- ...historical name for EvId
116type DFunId = Id        -- A dictionary function
117type DictId = EvId      -- A dictionary variable
118type IpId   = EvId      -- A term-level implicit parameter
119type EqVar  = EvId      -- Boxed equality evidence
120
121type CoVar = Id         -- See Note [Evidence: EvIds and CoVars]
122\end{code}
123
124Note [Evidence: EvIds and CoVars]
125~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126* An EvId (evidence Id) is a *boxed*, term-level evidence variable
127  (dictionary, implicit parameter, or equality).
128
129* A CoVar (coercion variable) is an *unboxed* term-level evidence variable
130  of type (t1 ~# t2).  So it's the unboxed version of an EqVar.
131
132* Only CoVars can occur in Coercions, EqVars appear in TcCoercions.
133
134* DictId, IpId, and EqVar are synonyms when we know what kind of
135  evidence we are talking about.  For example, an EqVar has type (t1 ~ t2).
136
137Note [Kind and type variables]
138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139Before kind polymorphism, TyVar were used to mean type variables. Now
140they are use to mean kind *or* type variables. KindVar is used when we
141know for sure that it is a kind variable. In future, we might want to
142go over the whole compiler code to use:
143   - TKVar   to mean kind or type variables
144   - TypeVar to mean         type variables only
145   - KindVar to mean kind         variables
146
147
148%************************************************************************
149%*                                                                      *
150\subsection{The main data type declarations}
151%*                                                                      *
152%************************************************************************
153
154
155Every @Var@ has a @Unique@, to uniquify it and for fast comparison, a
156@Type@, and an @IdInfo@ (non-essential info about it, e.g.,
157strictness).  The essential info about different kinds of @Vars@ is
158in its @VarDetails@.
159
160\begin{code}
161-- | Essentially a typed 'Name', that may also contain some additional information
162-- about the 'Var' and it's use sites.
163data Var
164  = TyVar {  -- Type and kind variables
165             -- see Note [Kind and type variables]
166        varName    :: !Name,
167        realUnique :: FastInt,       -- Key for fast comparison
168                                     -- Identical to the Unique in the name,
169                                     -- cached here for speed
170        varType    :: Kind           -- ^ The type or kind of the 'Var' in question
171 }
172
173  | TcTyVar {                           -- Used only during type inference
174                                        -- Used for kind variables during
175                                        -- inference, as well
176        varName        :: !Name,
177        realUnique     :: FastInt,
178        varType        :: Kind,
179        tc_tv_details  :: TcTyVarDetails }
180
181  | Id {
182        varName    :: !Name,
183        realUnique :: FastInt,
184        varType    :: Type,
185        idScope    :: IdScope,
186        id_details :: IdDetails,        -- Stable, doesn't change
187        id_info    :: IdInfo }          -- Unstable, updated by simplifier
188    deriving Typeable
189
190data IdScope    -- See Note [GlobalId/LocalId]
191  = GlobalId 
192  | LocalId ExportFlag
193
194data ExportFlag 
195  = NotExported -- ^ Not exported: may be discarded as dead code.
196  | Exported    -- ^ Exported: kept alive
197\end{code}
198
199Note [GlobalId/LocalId]
200~~~~~~~~~~~~~~~~~~~~~~~
201A GlobalId is
202  * always a constant (top-level)
203  * imported, or data constructor, or primop, or record selector
204  * has a Unique that is globally unique across the whole
205    GHC invocation (a single invocation may compile multiple modules)
206  * never treated as a candidate by the free-variable finder;
207        it's a constant!
208
209A LocalId is
210  * bound within an expression (lambda, case, local let(rec))
211  * or defined at top level in the module being compiled
212  * always treated as a candidate by the free-variable finder
213
214After CoreTidy, top-level LocalIds are turned into GlobalIds
215
216\begin{code}
217instance Outputable Var where
218  ppr var = ppr (varName var) <+> ifPprDebug (brackets (ppr_debug var))
219-- Printing the type on every occurrence is too much!
220--            <+> if (not opt_SuppressVarKinds)
221--                then ifPprDebug (text "::" <+> ppr (tyVarKind var) <+> text ")")
222--                else empty
223
224ppr_debug :: Var -> SDoc
225ppr_debug (TyVar {})                           = ptext (sLit "tv")
226ppr_debug (TcTyVar {tc_tv_details = d})        = pprTcTyVarDetails d
227ppr_debug (Id { idScope = s, id_details = d }) = ppr_id_scope s <> pprIdDetails d
228
229ppr_id_scope :: IdScope -> SDoc
230ppr_id_scope GlobalId              = ptext (sLit "gid")
231ppr_id_scope (LocalId Exported)    = ptext (sLit "lidx")
232ppr_id_scope (LocalId NotExported) = ptext (sLit "lid")
233
234instance Show Var where
235  showsPrec p var = showsPrecSDoc p (ppr var)
236
237instance NamedThing Var where
238  getName = varName
239
240instance Uniquable Var where
241  getUnique = varUnique
242
243instance Eq Var where
244    a == b = realUnique a ==# realUnique b
245
246instance Ord Var where
247    a <= b = realUnique a <=# realUnique b
248    a <  b = realUnique a <#  realUnique b
249    a >= b = realUnique a >=# realUnique b
250    a >  b = realUnique a >#  realUnique b
251    a `compare` b = varUnique a `compare` varUnique b
252
253instance Data Var where
254  -- don't traverse?
255  toConstr _   = abstractConstr "Var"
256  gunfold _ _  = error "gunfold"
257  dataTypeOf _ = mkNoRepType "Var"
258\end{code}
259
260
261\begin{code}
262varUnique :: Var -> Unique
263varUnique var = mkUniqueGrimily (iBox (realUnique var))
264
265setVarUnique :: Var -> Unique -> Var
266setVarUnique var uniq
267  = var { realUnique = getKeyFastInt uniq, 
268          varName = setNameUnique (varName var) uniq }
269
270setVarName :: Var -> Name -> Var
271setVarName var new_name
272  = var { realUnique = getKeyFastInt (getUnique new_name), 
273          varName = new_name }
274
275setVarType :: Id -> Type -> Id
276setVarType id ty = id { varType = ty }
277\end{code}
278
279
280%************************************************************************
281%*                                                                      *
282\subsection{Type and kind variables}
283%*                                                                      *
284%************************************************************************
285
286\begin{code}
287tyVarName :: TyVar -> Name
288tyVarName = varName
289
290tyVarKind :: TyVar -> Kind
291tyVarKind = varType
292
293setTyVarUnique :: TyVar -> Unique -> TyVar
294setTyVarUnique = setVarUnique
295
296setTyVarName :: TyVar -> Name -> TyVar
297setTyVarName   = setVarName
298
299setTyVarKind :: TyVar -> Kind -> TyVar
300setTyVarKind tv k = tv {varType = k}
301
302updateTyVarKind :: (Kind -> Kind) -> TyVar -> TyVar
303updateTyVarKind update tv = tv {varType = update (tyVarKind tv)}
304
305updateTyVarKindM :: (Monad m) => (Kind -> m Kind) -> TyVar -> m TyVar
306updateTyVarKindM update tv
307  = do { k' <- update (tyVarKind tv)
308       ; return $ tv {varType = k'} }
309\end{code}
310
311\begin{code}
312mkTyVar :: Name -> Kind -> TyVar
313mkTyVar name kind = TyVar { varName    = name
314                          , realUnique = getKeyFastInt (nameUnique name)
315                          , varType  = kind
316                        }
317
318mkTcTyVar :: Name -> Kind -> TcTyVarDetails -> TyVar
319mkTcTyVar name kind details
320  = -- NB: 'kind' may be a coercion kind; cf, 'TcMType.newMetaCoVar'
321    TcTyVar {   varName    = name,
322                realUnique = getKeyFastInt (nameUnique name),
323                varType  = kind,
324                tc_tv_details = details
325        }
326
327tcTyVarDetails :: TyVar -> TcTyVarDetails
328tcTyVarDetails (TcTyVar { tc_tv_details = details }) = details
329tcTyVarDetails var = pprPanic "tcTyVarDetails" (ppr var)
330
331setTcTyVarDetails :: TyVar -> TcTyVarDetails -> TyVar
332setTcTyVarDetails tv details = tv { tc_tv_details = details }
333
334mkKindVar :: Name -> SuperKind -> KindVar
335-- mkKindVar take a SuperKind as argument because we don't have access
336-- to superKind here.
337mkKindVar name kind = TyVar
338  { varName    = name
339  , realUnique = getKeyFastInt (nameUnique name)
340  , varType    = kind }
341
342\end{code}
343
344%************************************************************************
345%*                                                                      *
346\subsection{Ids}
347%*                                                                      *
348%************************************************************************
349
350\begin{code}
351idInfo :: Id -> IdInfo
352idInfo (Id { id_info = info }) = info
353idInfo other                   = pprPanic "idInfo" (ppr other)
354
355idDetails :: Id -> IdDetails
356idDetails (Id { id_details = details }) = details
357idDetails other                         = pprPanic "idDetails" (ppr other)
358
359-- The next three have a 'Var' suffix even though they always build
360-- Ids, becuase Id.lhs uses 'mkGlobalId' etc with different types
361mkGlobalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
362mkGlobalVar details name ty info
363  = mk_id name ty GlobalId details info
364
365mkLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
366mkLocalVar details name ty info
367  = mk_id name ty (LocalId NotExported) details  info
368
369mkCoVar :: Name -> Type -> CoVar
370-- Coercion variables have no IdInfo
371mkCoVar name ty = mk_id name ty (LocalId NotExported) coVarDetails vanillaIdInfo
372
373-- | Exported 'Var's will not be removed as dead code
374mkExportedLocalVar :: IdDetails -> Name -> Type -> IdInfo -> Id
375mkExportedLocalVar details name ty info
376  = mk_id name ty (LocalId Exported) details info
377
378mk_id :: Name -> Type -> IdScope -> IdDetails -> IdInfo -> Id
379mk_id name ty scope details info
380  = Id { varName    = name, 
381         realUnique = getKeyFastInt (nameUnique name),
382         varType    = ty,       
383         idScope    = scope,
384         id_details = details,
385         id_info    = info }
386
387-------------------
388lazySetIdInfo :: Id -> IdInfo -> Var
389lazySetIdInfo id info = id { id_info = info }
390
391setIdDetails :: Id -> IdDetails -> Id
392setIdDetails id details = id { id_details = details }
393
394globaliseId :: Id -> Id
395-- ^ If it's a local, make it global
396globaliseId id = id { idScope = GlobalId }
397
398setIdExported :: Id -> Id
399-- ^ Exports the given local 'Id'. Can also be called on global 'Id's, such as data constructors
400-- and class operations, which are born as global 'Id's and automatically exported
401setIdExported id@(Id { idScope = LocalId {} }) = id { idScope = LocalId Exported }
402setIdExported id@(Id { idScope = GlobalId })   = id
403setIdExported tv                               = pprPanic "setIdExported" (ppr tv)
404
405setIdNotExported :: Id -> Id
406-- ^ We can only do this to LocalIds
407setIdNotExported id = ASSERT( isLocalId id ) 
408                      id { idScope = LocalId NotExported }
409\end{code}
410
411%************************************************************************
412%*                                                                      *
413\subsection{Predicates over variables}
414%*                                                                      *
415%************************************************************************
416
417\begin{code}
418isTyVar :: Var -> Bool
419isTyVar = isTKVar     -- Historical
420
421isTKVar :: Var -> Bool  -- True of both type and kind variables
422isTKVar (TyVar {})   = True
423isTKVar (TcTyVar {}) = True
424isTKVar _            = False
425
426isTcTyVar :: Var -> Bool
427isTcTyVar (TcTyVar {}) = True
428isTcTyVar _            = False
429
430isId :: Var -> Bool
431isId (Id {}) = True
432isId _       = False
433
434isLocalId :: Var -> Bool
435isLocalId (Id { idScope = LocalId _ }) = True
436isLocalId _                            = False
437
438-- | 'isLocalVar' returns @True@ for type variables as well as local 'Id's
439-- These are the variables that we need to pay attention to when finding free
440-- variables, or doing dependency analysis.
441isLocalVar :: Var -> Bool
442isLocalVar v = not (isGlobalId v)
443
444isGlobalId :: Var -> Bool
445isGlobalId (Id { idScope = GlobalId }) = True
446isGlobalId _                           = False
447
448-- | 'mustHaveLocalBinding' returns @True@ of 'Id's and 'TyVar's
449-- that must have a binding in this module.  The converse
450-- is not quite right: there are some global 'Id's that must have
451-- bindings, such as record selectors.  But that doesn't matter,
452-- because it's only used for assertions
453mustHaveLocalBinding        :: Var -> Bool
454mustHaveLocalBinding var = isLocalVar var
455
456-- | 'isExportedIdVar' means \"don't throw this away\"
457isExportedId :: Var -> Bool
458isExportedId (Id { idScope = GlobalId })        = True
459isExportedId (Id { idScope = LocalId Exported}) = True
460isExportedId _ = False
461\end{code}
Note: See TracBrowser for help on using the browser.