a3060bf9c02b8f30921e9c6c1d72ba0115c42f8b
[ghc-hetmet.git] / ghc / docs / comm / the-beast / vars.html
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2 <html>
3   <head>
4     <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
5     <title>The GHC Commentary - The Real Story about Variables, Ids, TyVars, and the like</title>
6   </head>
7
8   <body BGCOLOR="FFFFFF">
9     <h1>The GHC Commentary - The Real Story about Variables, Ids, TyVars, and the like</h1>
10     <p>
11
12
13 <h2>Variables</h2>
14
15 The <code>Var</code> type, defined in <code>basicTypes/Var.lhs</code>,
16 represents variables, both term variables and type variables:
17 <pre>
18     data Var
19       = Var {
20             varName    :: Name,
21             realUnique :: FastInt,
22             varType    :: Type,
23             varDetails :: VarDetails,
24             varInfo    :: IdInfo                
25         }
26 </pre>
27 <ul>
28 <li> The <code>varName</code> field contains the identity of the variable:
29 its unique number, and its print-name.  The unique number is cached in the
30 <code>realUnique</code> field, just to make comparison of <code>Var</code>s a little faster.
31
32 <p><li> The <code>Type</code> field gives the type of a term variable, or the kind of a
33 type variable.  (Types and kinds are both represented by a <code>Type</code>.)
34
35 <p><li> The <code>varDetails</code> field distinguishes term variables from type variables,
36 and makes some further distinctions (see below).
37
38 <p><li> For term variables (only) the <code>varInfo</code> field contains lots of useful
39 information: strictness, unfolding, etc.  However, this information is all optional;
40 you can always throw away the <code>IdInfo</code>.  In contrast, you can't safely throw away
41 the <code>VarDetails</code> of a <code>Var</code>
42 </ul>
43 <p>
44 It's often fantastically convenient to have term variables and type variables
45 share a single data type.  For example, 
46 <pre>
47   exprFreeVars :: CoreExpr -> VarSet
48 </pre>
49 If there were two types, we'd need to return two sets.  Simiarly, big lambdas and
50 little lambdas use the same constructor in Core, which is extremely convenient.
51 <p>
52 We define a couple of type synonyms:
53 <pre>
54   type Id    = Var  -- Term variables
55   type TyVar = Var  -- Type variables
56 </pre>
57 just to help us document the occasions when we are expecting only term variables,
58 or only type variables.
59
60 <h2> The <code>VarDetails</code> field </h2>
61
62 The <code>VarDetails</code> field tells what kind of variable this is:
63 <pre>
64 data VarDetails
65   = LocalId             -- Used for locally-defined Ids (see NOTE below)
66         LocalIdDetails
67
68   | GlobalId            -- Used for imported Ids, dict selectors etc
69         GlobalIdDetails
70
71   | TyVar
72   | MutTyVar (IORef (Maybe Type))       -- Used during unification;
73              Bool                       -- True <=> this is a type signature variable, which
74                                         --          should not be unified with a non-tyvar type
75 </pre>
76
77 <h2>Type variables (<code>TyVar</code>)</h2>
78
79 The <code>TyVar</code> case is self-explanatory.  The
80 <code>MutTyVar</code> case is used only during type checking.  Then a
81 type variable can be unified, using an imperative update, with a type,
82 and that is what the <code>IORef</code> is for.  The <code>Bool</code>
83 field records whether the type variable arose from a type signature,
84 in which case it should not be unified with a type (only with another
85 type variable).
86 <p>
87 For a long time I tried to keep mutable Vars statically type-distinct
88 from immutable Vars, but I've finally given up.   It's just too painful.
89 After type checking there are no MutTyVars left, but there's no static check
90 of that fact.
91
92 <h2>Term variables (<code>Id</code>)</h2>
93
94 A term variable (of type <code>Id</code>) is represented either by a
95 <code>LocalId</code> or a <code>GlobalId</code>:
96 <p>
97 A <code>GlobalId</code> is
98 <ul>
99 <li> Always bound at top-level.
100 <li> Always has a <code>GlobalName</code>, and hence has 
101      a <code>Unique</code> that is globally unique across the whole
102      GHC invocation (a single invocation may compile multiple modules).
103 <li> Has <code>IdInfo</code> that is absolutely fixed, forever.
104 </ul>
105
106 <p>
107 A <code>LocalId</code> is:
108 <ul> 
109 <li> Always bound in the module being compiled:
110 <ul>
111 <li> <em>either</em> bound within an expression (lambda, case, local let(rec))
112 <li> <em>or</em> defined at top level in the module being compiled.
113 </ul>
114 <li> Has IdInfo that changes as the simpifier bashes repeatedly on it.
115 </ul>
116 <p>
117 The key thing about <code>LocalId</code>s is that the free-variable finder
118 typically treats them as candidate free variables. That is, it ignores
119 <code>GlobalId</code>s such as imported constants, data contructors, etc.
120 <p>
121 An important invariant is this: <em>All the bindings in the module
122 being compiled (whether top level or not) are <code>LocalId</code>s
123 until the CoreTidy phase.</em> In the CoreTidy phase, all
124 externally-visible top-level bindings are made into GlobalIds.  This
125 is the point when a <code>LocalId</code> becomes "frozen" and becomes
126 a fixed, immutable <code>GlobalId</code>.
127 <p>
128 (A binding is <em>"externally-visible"</em> if it is exported, or
129 mentioned in the unfolding of an externally-visible Id.  An
130 externally-visible Id may not have an unfolding, either because it is
131 too big, or because it is the loop-breaker of a recursive group.)
132
133 <h3>Global Ids and implicit Ids</h3>
134
135 <code>GlobalId</code>s are further categorised by their <code>GlobalIdDetails</code>.
136 This type is defined in <code>basicTypes/IdInfo</code>, because it mentions other
137 structured types like <code>DataCon</code>.  Unfortunately it is *used* in <code>Var.lhs</code>
138 so there's a <code>hi-boot</code> knot to get it there.  Anyway, here's the declaration:
139 <pre>
140 data GlobalIdDetails
141   = NotGlobalId                 -- Used as a convenient extra return value 
142                                 -- from globalIdDetails
143
144   | VanillaGlobal               -- Imported from elsewhere
145
146   | PrimOpId PrimOp             -- The Id for a primitive operator
147   | FCallId ForeignCall         -- The Id for a foreign call
148
149   -- These next ones are all "implicit Ids"
150   | RecordSelId FieldLabel      -- The Id for a record selector
151   | DataConId DataCon           -- The Id for a data constructor *worker*
152   | DataConWrapId DataCon       -- The Id for a data constructor *wrapper*
153                                 -- [the only reasons we need to know is so that
154                                 --  a) we can  suppress printing a definition in the interface file
155                                 --  b) when typechecking a pattern we can get from the
156                                 --     Id back to the data con]
157 </pre>
158 The <code>GlobalIdDetails</code> allows us to go from the <code>Id</code> for 
159 a record selector, say, to its field name; or the <code>Id</code> for a primitive
160 operator to the <code>PrimOp</code> itself.
161 <p>
162 Certain <code>GlobalId</code>s are called <em>"implicit"</em> Ids.  An implicit
163 Id is derived by implication from some other declaration.  So a record selector is
164 derived from its data type declaration, for example.  An implicit Ids is always 
165 a <code>GlobalId</code>.  For most of the compilation, the implicit Ids are just
166 that: implicit.  If you do -ddump-simpl you won't see their definition.  (That's
167 why it's true to say that until CoreTidy all Ids in this compilation unit are
168 LocalIds.)  But at CorePrep, a binding is added for each implicit Id defined in
169 this module, so that the code generator will generate code for the (curried) function.
170 <p>
171 Implicit Ids carry their unfolding inside them, of course, so they may well have
172 been inlined much earlier; but we generate the curried top-level defn just in
173 case its ever needed.
174
175 <h3>LocalIds</h3>
176
177 The <code>LocalIdDetails</code> gives more info about a <code>LocalId</code>:
178 <pre>
179 data LocalIdDetails 
180   = NotExported -- Not exported
181   | Exported    -- Exported
182   | SpecPragma  -- Not exported, but not to be discarded either
183                 -- It's unclean that this is so deeply built in
184 </pre>
185 From this we can tell whether the <code>LocalId</code> is exported, and that
186 tells us whether we can drop an unused binding as dead code. 
187 <p>
188 The <code>SpecPragma</code> thing is a HACK.  Suppose you write a SPECIALIZE pragma:
189 <pre>
190    foo :: Num a => a -> a
191    {-# SPECIALIZE foo :: Int -> Int #-}
192    foo = ...
193 </pre>
194 The type checker generates a dummy call to <code>foo</code> at the right types:
195 <pre>
196    $dummy = foo Int dNumInt
197 </pre>
198 The Id <code>$dummy</code> is marked <code>SpecPragma</code>.  Its role is to hang
199 onto that call to <code>foo</code> so that the specialiser can see it, but there
200 are no calls to <code>$dummy</code>.
201 The simplifier is careful not to discard <code>SpecPragma</code> Ids, so that it
202 reaches the specialiser.  The specialiser processes the right hand side of a <code>SpecPragma</code> Id
203 to find calls to overloaded functions, <em>and then discards the <code>SpecPragma</code> Id</em>.
204 So <code>SpecPragma</code> behaves a like <code>Exported</code>, at least until the specialiser.
205
206
207 <h3>Global and Local <code>Name</code>s</h3>
208
209 Notice that whether an Id is a <code>LocalId</code> or <code>GlobalId</code> is 
210 not the same as whether the Id has a <code>Local</code> or <code>Global</code> <code>Name</code>:
211 <ul>
212 <li> Every <code>GlobalId</code> has a <code>Global</code> <code>Name</code>.
213 <li> A <code>LocalId</code> might have either kind of <code>Name</code>.
214 </ul>
215 The significance of Global vs Local names is this:
216 <ul>
217 <li> A <code>Global</code> Name has a module and occurrence name; a <code>Local</code> 
218 has only an occurrence name.
219 <p> <li> A <code>Global</code> Name has a unique that never changes.  It is never
220 cloned.  This is important, because the simplifier invents new names pretty freely,
221 but we don't want to lose the connnection with the type environment (constructed earlier).
222 A <code>Local</code> name can be cloned freely.
223 </ul>
224
225
226 <!-- hhmts start -->
227 Last modified: Wed Aug  8 19:23:01 EST 2001
228 <!-- hhmts end -->
229     </small>
230   </body>
231 </html>