1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
5 <title>The GHC Commentary - The truth about names: OccNames, and Names</title>
8 <body BGCOLOR="FFFFFF">
9 <h1>The GHC Commentary - The truth about names: OccNames, and Names</h1>
11 Every entity (type constructor, class, identifier, type variable) has a
12 <code>Name</code>. The <code>Name</code> type is pervasive in GHC, and
13 is defined in <code>basicTypes/Name.lhs</code>. Here is what a Name
14 looks like, though it is private to the Name module.
19 n_sort :: NameSort, -- What sort of name it is
20 n_occ :: !OccName, -- Its occurrence name
21 n_uniq :: Unique, -- Its identity
22 n_loc :: !SrcLoc -- Definition site
26 <li> The <code>n_sort</code> field says what sort of name this is: see
27 <a href="#sort">NameSort below</a>.
28 <li> The <code>n_occ</code> field gives the "occurrence name" of the
30 <a href="#occname">OccName below</a>.
31 <li> The <code>n_uniq</code> field allows fast tests for equality of
33 <li> The <code>n_loc</code> field gives some indication of where the
37 <h2><a name="sort">The <code>NameSort</code> of a <code>Name</code></a></h2>
39 There are four flavours of <code>Name</code>:
44 = External Module (Maybe Name)
45 -- (Just parent) => this Name is a subordinate name of 'parent'
46 -- e.g. data constructor of a data type, method of a class
47 -- Nothing => not a subordinate
49 | WiredIn Module (Maybe Name) TyThing BuiltInSyntax
50 -- A variant of External, for wired-in things
52 | Internal -- A user-defined Id or TyVar
53 -- defined in the module being compiled
55 | System -- A system-defined Id or TyVar. Typically the
56 -- OccName is very uninformative (like 's')</pre>
59 <li>Here are the sorts of Name an entity can have:
61 <li> Class, TyCon: External.
62 <li> Id: External, Internal, or System.
63 <li> TyVar: Internal, or System.
66 <li>An <code>External</code> name has a globally-unique
67 (module name, occurrence name) pair, namely the
68 <em>original name</em> of the entity,
69 describing where the thing was originally defined. So for example,
78 import qualified M as Q
83 then the RdrNames for "a", "Q.f" and "g" get replaced (by the
84 Renamer) by the Names "A.a", "M.f", and "M.g" respectively.
87 <li>An <code>InternalName</code>
88 has only an occurrence name. Distinct InternalNames may have the same
89 occurrence name; use the Unique to distinguish them.
91 <li>An <code>ExternalName</code> has a unique that never changes. It
92 is never cloned. This is important, because the simplifier invents
93 new names pretty freely, but we don't want to lose the connnection
94 with the type environment (constructed earlier). An
95 <code>InternalName</code> name can be cloned freely.
97 <li><strong>Before CoreTidy</strong>: the Ids that were defined at top
98 level in the original source program get <code>ExternalNames</code>,
99 whereas extra top-level bindings generated (say) by the type checker
100 get <code>InternalNames</code>. q This distinction is occasionally
101 useful for filtering diagnostic output; e.g. for -ddump-types.
103 <li><strong>After CoreTidy</strong>: An Id with an
104 <code>ExternalName</code> will generate symbols that
105 appear as external symbols in the object file. An Id with an
106 <code>InternalName</code> cannot be referenced from outside the
107 module, and so generates a local symbol in the object file. The
108 CoreTidy pass makes the decision about which names should be External
111 <li>A <code>System</code> name is for the most part the same as an
112 <code>Internal</code>. Indeed, the differences are purely cosmetic:
114 <li>Internal names usually come from some name the
115 user wrote, whereas a System name has an OccName like "a", or "t".
116 Usually there are masses of System names with the same OccName but
117 different uniques, whereas typically there are only a handful of
118 distince Internal names with the same OccName.
120 <li>Another difference is that when unifying the type checker tries
121 to unify away type variables with System names, leaving ones with
122 Internal names (to improve error messages).
128 <h2><a name="occname">Occurrence names: <code>OccName</code></a></h2>
130 An <code>OccName</code> is more-or-less just a string, like "foo" or
131 "Tree", giving the (unqualified) name of an entity.
134 Well, not quite just a string, because in Haskell a name like "C" could
135 mean a type constructor or data constructor, depending on context. So
136 GHC defines a type <tt>OccName</tt> (defined in
137 <tt>basicTypes/OccName.lhs</tt>) that is a pair of a <tt>FastString</tt>
138 and a <tt>NameSpace</tt> indicating which name space the name is drawn
142 data OccName = OccName NameSpace EncodedFS</pre>
145 The <tt>EncodedFS</tt> is a synonym for <tt>FastString</tt> indicating
146 that the string is Z-encoded. (Details in <tt>OccName.lhs</tt>.)
147 Z-encoding encodes funny characters like '%' and '$' into alphabetic
148 characters, like "zp" and "zd", so that they can be used in object-file
149 symbol tables without confusing linkers and suchlike.
155 <li> <tt>VarName</tt>: ordinary variables</li>
156 <li> <tt>TvName</tt>: type variables</li>
157 <li> <tt>DataName</tt>: data constructors</li>
158 <li> <tt>TcClsName</tt>: type constructors and classes (in Haskell they
159 share a name space) </li>
164 Last modified: Wed May 4 14:57:55 EST 2005