[project @ 2005-03-05 11:58:41 by chak]
[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 <a href="names.html"><tt>Names</tt></a>.
20
21 <h2> RdrNames </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 <pre>
26     data RdrName = RdrName Qual OccName
27     
28     data Qual = Unqual
29     
30               | Qual ModuleName     -- A qualified name written by the user in source code
31                                     -- The module isn't necessarily the module where
32                                     -- the thing is defined; just the one from which it
33                                     -- is imported
34     
35               | Orig ModuleName     -- This is an *original* name; the module is the place
36                                     -- where the thing was defined
37 </pre>
38 The OccName type is described in <a href="names.html#occname">"The truth about names"</a>.
39 <p>
40 The <tt>OrigName</tt> variant is used internally; it allows GHC to speak of <tt>RdrNames</tt>
41 that refer to the original name of the thing.
42
43
44 <h2> Rebindable syntax </h2>
45
46 In Haskell when one writes "3" one gets "fromInteger 3", where
47 "fromInteger" comes from the Prelude (regardless of whether the
48 Prelude is in scope).  If you want to completely redefine numbers,
49 that becomes inconvenient.  So GHC lets you say
50 "-fno-implicit-prelude"; in that case, the "fromInteger" comes from
51 whatever is in scope.  (This is documented in the User Guide.)
52 <p>
53 This feature is implemented as follows (I always forget).
54 <ul>
55 <li> Four HsSyn constructs (NegApp, NPlusKPat, HsIntegral, HsFractional) 
56 contain a <tt>Name</tt> (i.e. it is not parameterised).
57 <li> When the parser builds these constructs, it puts in the built-in Prelude
58 Name (e.g. PrelNum.fromInteger).
59 <li> When the renamer encounters these constructs, it calls <tt>RnEnv.lookupSyntaxName</tt>.
60 This checks for <tt>-fno-implicit-prelude</tt>; if not, it just returns the same Name;
61 otherwise it takes the occurrence name of the Name, turns it into an unqualified RdrName, and looks
62 it up in the environment.  The returned name is plugged back into the construct.
63 <li> The typechecker uses the Name to generate the appropriate typing constraints.
64 </ul>
65
66 <!-- hhmts start -->
67 Last modified: Tue Nov 13 14:11:35 EST 2001
68 <!-- hhmts end -->
69     </small>
70   </body>
71 </html>