07.01.2014 Views

The Glorious Glasgow Haskell Compilation System User's Guide ...

The Glorious Glasgow Haskell Compilation System User's Guide ...

The Glorious Glasgow Haskell Compilation System User's Guide ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong> <strong>Glorious</strong> <strong>Glasgow</strong> <strong>Haskell</strong><br />

<strong>Compilation</strong> <strong>System</strong> User’s <strong>Guide</strong>,<br />

Version 6.10.4 13 / 224<br />

You can save Main.hs anywhere you like, but if you save it somewhere other than the current directory 3 then we will need to<br />

change to the right directory in GHCi:<br />

Prelude> :cd dir<br />

where dir is the directory (or folder) in which you saved Main.hs.<br />

To load a <strong>Haskell</strong> source file into GHCi, use the :load command:<br />

Prelude> :load Main<br />

Compiling Main ( Main.hs, interpreted )<br />

Ok, modules loaded: Main.<br />

*Main><br />

GHCi has loaded the Main module, and the prompt has changed to “*Main>” to indicate that the current context for expressions<br />

typed at the prompt is the Main module we just loaded (we’ll explain what the * means later in Section 2.4.3). So we can now<br />

type expressions involving the functions from Main.hs:<br />

*Main> fac 17<br />

355687428096000<br />

Loading a multi-module program is just as straightforward; just give the name of the “topmost” module to the :load command<br />

(hint: :load can be abbreviated to :l). <strong>The</strong> topmost module will normally be Main, but it doesn’t have to be. GHCi will<br />

discover which modules are required, directly or indirectly, by the topmost module, and load them all in dependency order.<br />

2.2.1 Modules vs. filenames<br />

Question: How does GHC find the filename which contains module M? Answer: it looks for the file M.hs, or M.lhs. This<br />

means that for most modules, the module name must match the filename. If it doesn’t, GHCi won’t be able to find it.<br />

<strong>The</strong>re is one exception to this general rule: when you load a program with :load, or specify it when you invoke ghci, you can<br />

give a filename rather than a module name. This filename is loaded if it exists, and it may contain any module you like. This is<br />

particularly convenient if you have several Main modules in the same directory and you can’t call them all Main.hs.<br />

<strong>The</strong> search path for finding source files is specified with the -i option on the GHCi command line, like so:<br />

ghci -idir1:...:dirn<br />

or it can be set using the :set command from within GHCi (see Section 2.8.2) 4<br />

One consequence of the way that GHCi follows dependencies to find modules to load is that every module must have a source<br />

file. <strong>The</strong> only exception to the rule is modules that come from a package, including the Prelude and standard libraries such as<br />

IO and Complex. If you attempt to load a module for which GHCi can’t find a source file, even if there are object and interface<br />

files for the module, you’ll get an error message.<br />

2.2.2 Making changes and recompilation<br />

If you make some changes to the source code and want GHCi to recompile the program, give the :reload command. <strong>The</strong><br />

program will be recompiled as necessary, with GHCi doing its best to avoid actually recompiling modules if their external<br />

dependencies haven’t changed. This is the same mechanism we use to avoid re-compiling modules in the batch compilation<br />

setting (see Section 4.6.8).<br />

3 If you started up GHCi from the command line then GHCi’s current directory is the same as the current directory of the shell from which it was started. If<br />

you started GHCi from the “Start” menu in Windows, then the current directory is probably something like C:\DocumentsandSettings\username.<br />

4 Note that in GHCi, and --make mode, the -i option is used to specify the search path for source files, whereas in standard batch-compilation mode the<br />

-i option is used to specify the search path for interface files, see Section 4.6.3.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!