[project @ 2002-02-05 15:37:01 by simonpj]
[ghc-hetmet.git] / ghc / docs / comm / the-beast / renamer.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 Glorious Renamer</h1>
10     <p>
11
12 (This section is, like most of the Commentary, rather incomplete.)
13 <p>
14 The <em>renamer</em> sits between the parser and the typechecker.
15 Roughly speaking, It has the type:
16 <pre>
17    HsModule RdrName -> HsModule Name
18 </pre>
19 That is, it converts all the <tt>RdrNames</tt> to <tt>Names</tt>.
20
21 <h2> OccNames, RdrNames, and Names </h2>
22
23 A <tt>RdrNames</tt> is pretty much just a string (for an unqualified name
24 like "<tt>f</tt>") or a pair of strings (for a qualified name like "<tt>M.f</tt>").
25 Well, not quite just strings, because in Haskell a name like "C" could mean a type 
26 constructor or data constructor, depending on context.  So GHC defines a type 
27 <tt>OccName</tt> (defined in <tt>basicTypes/OccName.lhs</tt>) that is a pair of
28 a <tt>FastString</tt> and a <tt>NameSpace</tt> indicating which name space the
29 name is drawn from:
30 <pre>
31     data OccName = OccName NameSpace EncodedFS
32 </pre>
33 The <tt>EncodedFS</tt> is a synonym for <tt>FastString</tt> indicating that the
34 string is Z-encoded.  (Details in <tt>OccName.lhs</tt>.)
35 <p>
36 The name spaces are:
37 <ul>
38 <li> <tt>VarName</tt>: ordinary variables
39 <li> <tt>TvName</tt>: type variables
40 <li> <tt>DataName</tt>: data constructors
41 <li> <tt>TcClsName</tt>: type constructors and classes (in Haskell they share a name space)
42 </ul>
43 So a <tt>RdrName</tt> is defined thus:
44 <pre>
45     data RdrName = RdrName Qual OccName
46     
47     data Qual = Unqual
48     
49               | Qual ModuleName     -- A qualified name written by the user in source code
50                                     -- The module isn't necessarily the module where
51                                     -- the thing is defined; just the one from which it
52                                     -- is imported
53     
54               | Orig ModuleName     -- This is an *original* name; the module is the place
55                                     -- where the thing was defined
56 </pre>
57 The <tt>OrigName</tt> variant is used internally; it allows GHC to speak of <tt>RdrNames</tt>
58 that refer to the original name of the thing.
59
60 <p>
61 On the other hand, a <tt>Name</tt>:
62 <ul>
63 <li> Contains the <em>original name</em> for the thing.   
64 <li> Contains a <tt>Unique</tt> that makes it easy to compare names for equality quickly.
65 <li> Contains a <tt>SrcLoc</tt> saying where the name was bound.
66 </ul>
67 The <em>original name</em> of an entity (type constructor, class, function etc) is
68 the (module,name) pair describing where the thing was originally defined.  So for example,
69 if we have
70 <pre>
71   module M where
72     f = e1
73     g = e2
74
75   module A where
76     import qualified M as Q
77     import M
78     a = Q.f + g
79 </pre>
80 then the RdrNames for "a", "Q.f" and "g" get replaced by the Names
81 "A.a", "M.f", and "M.g" respectively.
82 <p>
83 <tt>Names</tt> come in two flavours: Local and Global.  The Global kind contain
84 both a <tt>Module</tt> and an <tt>OccName</tt>
85 Not all Names are qualifed.  Local (e.g. lambda-bound) names are given Local Names
86
87 <h2> Rebindable syntax </h2>
88
89 In Haskell when one writes "3" one gets "fromInteger 3", where
90 "fromInteger" comes from the Prelude (regardless of whether the
91 Prelude is in scope).  If you want to completely redefine numbers,
92 that becomes inconvenient.  So GHC lets you say
93 "-fno-implicit-prelude"; in that case, the "fromInteger" comes from
94 whatever is in scope.  (This is documented in the User Guide.)
95 <p>
96 This feature is implemented as follows (I always forget).
97 <ul>
98 <li> Four HsSyn constructs (NegApp, NPlusKPat, HsIntegral, HsFractional) 
99 contain a <tt>Name</tt> (i.e. it is not parameterised).
100 <li> When the parser builds these constructs, it puts in the built-in Prelude
101 Name (e.g. PrelNum.fromInteger).
102 <li> When the renamer encounters these constructs, it calls <tt>RnEnv.lookupSyntaxName</tt>.
103 This checks for <tt>-fno-implicit-prelude</tt>; if not, it just returns the same Name;
104 otherwise it takes the occurrence name of the Name, turns it into an unqualified RdrName, and looks
105 it up in the environment.  The returned name is plugged back into the construct.
106 <li> The typechecker uses the Name to generate the appropriate typing constraints.
107 </ul>
108
109 <!-- hhmts start -->
110 Last modified: Tue Nov 13 14:11:35 EST 2001
111 <!-- hhmts end -->
112     </small>
113   </body>
114 </html>