X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fghci.sgml;h=405f7b22e2c00336396dfef8cc2dbed52c5a220c;hb=0d3f3debcf01ef11d43f7cc6f81ecc3eb2d38ee6;hp=55e09bb3d555284e62fd3a48b55da384389c20b9;hpb=62cfe85aeb765dc11503b26daadf42de54298850;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ghci.sgml b/ghc/docs/users_guide/ghci.sgml index 55e09bb..405f7b2 100644 --- a/ghc/docs/users_guide/ghci.sgml +++ b/ghc/docs/users_guide/ghci.sgml @@ -9,11 +9,11 @@ is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted. If - you're famililar with HugsHugs + you're famililar with HugsHugs , then you'll be right at home with GHCi. However, GHCi also has support for interactively loading compiled code, as well as - supporting allexcept the FFI, at the moment - the language extensions that GHC provides. + supporting allexcept foreign export, at the moment + the language extensions that GHC provides. FFIGHCi support Foreign Function InterfaceGHCi support @@ -27,11 +27,12 @@ $ ghci ___ ___ _ / _ \ /\ /\/ __(_) - / /_\// /_/ / / | | GHC Interactive, version 5.00, For Haskell 98. + / /_\// /_/ / / | | GHC Interactive, version 5.04, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. -Loading package std ... linking ... done. +Loading package base ... linking ... done. +Loading package haskell98 ... linking ... done. Prelude> @@ -42,23 +43,37 @@ Prelude> Commands available from the prompt: - <stmt> evaluate/run <stmt> - :cd <dir> change directory to <dir> - :def <cmd> <expr> define a macro :<cmd> - :help, :? display this list of commands - :load <filename> load a module (and it dependents) - :module <mod> set the context for expression evaluation to <mod> - :reload reload the current module set - :set <option> ... set options - :type <expr> show the type of <expr> - :unset <option> ... unset options - :quit exit GHCi - :!<command> run the shell command <command> + + <stmt> evaluate/run <stmt> + :add <filename> ... add module(s) to the current target set + :browse [*]<module> display the names defined by <module> + :cd <dir> change directory to <dir> + :def <cmd> <expr> define a command :<cmd> + :help, :? display this list of commands + :info [<name> ...] display information about the given names + :load <filename> ... load module(s) and their dependents + :module [+/-] [*]<mod> ... set the context for expression evaluation + :reload reload the current module set + + :set <option> ... set options + :set args <arg> ... set the arguments returned by System.getArgs + :set prog <progname> set the value returned by System.getProgName + + :show modules show the currently loaded modules + :show bindings show the current bindings made at the prompt + + :type <expr> show the type of <expr> + :undef <cmd> undefine user-defined command :<cmd> + :unset <option> ... unset options + :quit exit GHCi + :!<command> run the shell command <command> + Options for `:set' and `:unset': + +r revert top-level expressions after each evaluation +s print timing/memory stats after each evaluation +t print type after evaluation - -<flag> most GHC command line flags can also be set here + -<flags> most GHC command line flags can also be set here (eg. -v2, -fglasgow-exts, etc.) @@ -87,8 +102,7 @@ Prelude> Loading source files Suppose we have the following Haskell source code, which we - place in a file Main.hs in the current - directory: + place in a file Main.hs: main = print (fac 20) @@ -97,6 +111,24 @@ fac 0 = 1 fac n = n * fac (n-1) + You can save Main.hs anywhere you like, + but if you save it somewhere other than the current + directoryIf 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 you started + GHCi from the “Start” menu in Windows, then the + current directory is probably something like + C:\Documents and Settings\user + name. then we will + need to change to the right directory in GHCi: + + +Prelude> :cd dir + + + where dir is the directory (or + folder) in which you saved Main.hs. + To load a Haskell source file into GHCi, use the :load command: :load @@ -105,18 +137,19 @@ fac n = n * fac (n-1) Prelude> :load Main Compiling Main ( Main.hs, interpreted ) Ok, modules loaded: Main. -Main> +*Main> GHCi has loaded the Main module, and the - prompt has changed to “Main>” to + prompt has changed to “*Main>” to indicate that the current context for expressions typed at the - prompt is the Main module we just - loaded. So we can now type expressions involving the functions - from Main.hs: + prompt is the Main module we just loaded (we'll + explain what the * means later in ). So we can now type expressions involving + the functions from Main.hs: -Main> fac 17 +*Main> fac 17 355687428096000 @@ -129,7 +162,7 @@ Main> fac 17 indirectly, by the topmost module, and load them all in dependency order. - + Modules vs. filenames modulesand filenames filenamesof modules @@ -150,9 +183,30 @@ Main> fac 17 in the same directory and you can't call them all Main.hs. - One final note: if you load a module called Main, it must - contain a main function, just like in - GHC. + The search path for finding source files is specified with + the option on the GHCi command line, like + so: +ghci -idir1:...:dirn + + or it can be set using the :set command + from within GHCi (see )Note that in + GHCi, and mode, the + option is used to specify the search path for + source files, whereas in standard + batch-compilation mode the option is used to + specify the search path for interface files, see . + + One consequence of the way that GHCi follows dependencies + to find modules to load is that every module must have a source + file. The only exception to the rule is modules that come from + a package, including the Prelude and standard + libraries such as 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 files for the module, you'll get an error + message. @@ -177,8 +231,8 @@ Main> fac 17 normally converted to byte-code and run using the interpreter. However, interpreted code can also run alongside compiled code in GHCi; indeed, normally when GHCi starts, it loads up a compiled - copy of package std, which contains the Prelude - and standard libraries. + copy of the base package, which contains the + Prelude. Why should we want to run compiled code? Well, compiled code is roughly 10x faster than interpreted code, but takes about @@ -209,7 +263,7 @@ Compiling C ( C.hs, interpreted ) Compiling B ( B.hs, interpreted ) Compiling A ( A.hs, interpreted ) Ok, modules loaded: A, B, C, D. -Main> +*Main> In the messages from the compiler, we see that it skipped D, @@ -219,20 +273,33 @@ Main> isn't necessary, because the source and everything it depends on is unchanged since the last compilation. + At any time you can use the command + :show modules + to get a list of the modules currently loaded + into GHCi: + + +*Main> :show modules +D ( D.hs, D.o ) +C ( C.hs, interpreted ) +B ( B.hs, interpreted ) +A ( A.hs, interpreted ) +*Main> + If we now modify the source of D (or pretend to: using Unix command touch on the source file is handy for this), the compiler will no longer be able to use the object file, because it might be out of date: -Main> :! touch D.hs -Main> :reload +*Main> :! touch D.hs +*Main> :reload Compiling D ( D.hs, interpreted ) Skipping C ( C.hs, interpreted ) Skipping B ( B.hs, interpreted ) Skipping A ( A.hs, interpreted ) Ok, modules loaded: A, B, C, D. -Main> +*Main> Note that module D was compiled, but in this instance @@ -243,8 +310,8 @@ Main> So let's try compiling one of the other modules: -Main> :! ghc -c C.hs -Main> :load A +*Main> :! ghc -c C.hs +*Main> :load A Compiling D ( D.hs, interpreted ) Compiling C ( C.hs, interpreted ) Compiling B ( B.hs, interpreted ) @@ -259,8 +326,8 @@ Ok, modules loaded: A, B, C, D. also compile D: -Main> :! ghc -c D.hs -Main> :reload +*Main> :! ghc -c D.hs +*Main> :reload Ok, modules loaded: A, B, C, D. @@ -269,7 +336,7 @@ Ok, modules loaded: A, B, C, D. :load: -Main> :load A +*Main> :load A Skipping D ( D.hs, D.o ) Skipping C ( C.hs, C.o ) Compiling B ( B.hs, interpreted ) @@ -280,7 +347,7 @@ Ok, modules loaded: A, B, C, D. HINT: since GHCi will only use a compiled object file if it can sure that the compiled version is up-to-date, a good technique when working on a large program is to occasionally run - ghc --make to compile the whole project (say + ghc ––make to compile the whole project (say before you go for lunch :-), then continue working in the interpreter. As you modify code, the new modules will be interpreted, but the rest of the project will remain @@ -332,60 +399,108 @@ in a `do' expression pattern binding: print it What's really in scope at the prompt? - When you type an expression at the prompt, what - identifiers and types are in scope? GHCi has a concept of a - context module, which can be set using - the :module command. + When you type an expression at the prompt, what + identifiers and types are in scope? GHCi provides a flexible + way to control exactly how the context for an expression is + constructed. Let's start with the simple cases; when you start + GHCi the prompt looks like this: - The context module is shown in the prompt: for example, - the prompt Prelude> indicates that the - current context for evaluating expressions is the Haskell - Prelude module. The Prelude is the default - context when you start up GHCi. - Prelude +Prelude> - Exactly which entities are in scope in a given context - depends on whether the context module is compiled or - interpreted: + Which indicates that everything from the module + Prelude is currently in scope. If we now + load a file into GHCi, the prompt will change: - - - If the context module is interpreted, then everything - that was in scope during compilation of that module is also - in scope at the prompt, i.e. all the imports and any - top-level functions, types and classes defined in that - module. - + +Prelude> :load Main.hs +Compiling Main ( Main.hs, interpreted ) +*Main> + - - If the context module comes from a package, or is - otherwise compiled, then only the exports of that module are - in scope at the prompt. So for example, when the current - context module is Prelude, everything the - Prelude exports is in scope, but if we - switch context to eg. Time, then - everything from the Prelude is now - invisible. - - + The new prompt is *Main, which + indicates that we are typing expressions in the context of the + top-level of the Main module. Everything + that is in scope at the top-level in the module + Main we just loaded is also in scope at the + prompt (probably including Prelude, as long + as Main doesn't explicitly hide it). + + The syntax + *module indicates + that it is the full top-level scope of + module that is contributing to the + scope for expressions typed at the prompt. Without the + *, just the exports of the module are + visible. + + We're not limited to a single module: GHCi can combine + scopes from multiple modules, in any mixture of + * and non-* forms. GHCi + combines the scopes from all of these modules to form the scope + that is in effect at the prompt. For technical reasons, GHCi + can only support the *-form for modules which + are interpreted, so compiled modules and package modules can + only contribute their exports to the current scope. + + The scope is manipulated using the + :module command. For example, if the current + scope is Prelude, then we can bring into + scope the exports from the module IO like + so: + + +Prelude> :module +IO +Prelude,IO> hPutStrLn stdout "hello\n" +hello +Prelude,IO> + + + (Note: :module can be shortened to + :m). The full syntax of the + :module command is: + + +:module +|- *mod1 ... *modn + - The reason for this unfortunate distinction is boring: for - a compiled module when the source isn't available, the compiler - has no way of knowing what was in scope when the module was - compiled (and we don't store this information in the interface - file). However, in practice it shouldn't be a problem: if you - want both Time and Prelude - in scope at the same time, just create a file containing the - line import Time and load it into - GHCi. - - To make life slightly easier, the GHCi prompt also behaves - as if there is an implicit import qualified - declaration for every module in every package, and every module - currently loaded into GHCi. So in the above example where the - Prelude was invisible, we can always get at - Prelude identifiers by qualifying them, eg. - Prelude.map. + Using the + form of the + module commands adds modules to the current + scope, and - removes them. Without either + + or -, the current scope + is replaced by the set of modules specified. Note that if you + use this form and leave out Prelude, GHCi + will assume that you really wanted the + Prelude and add it in for you (if you don't + want the Prelude, then ask to remove it with + :m -Prelude). + + The scope is automatically set after a + :load command, to the most recently loaded + "target" module, in a *-form if possible. + For example, if you say :load foo.hs bar.hs + and bar.hs contains module + Bar, then the scope will be set to + *Bar if Bar is + interpreted, or if Bar is compiled it will be + set to Prelude,Bar (GHCi automatically adds + Prelude if it isn't present and there aren't + any *-form modules). + + With multiple modules in scope, especially multiple + *-form modules, it is likely that name + clashes will occur. Haskell specifies that name clashes are + only reported when an ambiguous identifier is used, and GHCi + behaves in the same way for expressions typed at the + prompt. + + + Qualified names + + To make life slightly easier, the GHCi prompt also + behaves as if there is an implicit import + qualified declaration for every module in every + package, and every module currently loaded into GHCi. + @@ -439,8 +554,9 @@ Prelude> Any exceptions raised during the evaluation or execution of the statement are caught and printed by the GHCi command line - interface (see for more - information on GHC's Exception support). + interface (for more information on exceptions, see the module + Control.Exception in the libraries + documentation). Every new binding shadows any existing bindings of the same name, including entities that are in scope in the current @@ -453,6 +569,14 @@ Prelude> :module: the temporary bindings just move to the new location. + HINT: To get a list of the bindings currently in scope, use the + :show bindings command: + + +Prelude> :show bindings +x :: Int +Prelude> + HINT: if you turn on the +t option, GHCi will show the type of each variable bound by a statement. For example: @@ -513,30 +637,54 @@ Wed Mar 14 12:23:13 GMT 2001 of it is lost. + + + Type defaulting in GHCi + Type default + Show class + + Consider this GHCi session: + + ghci> reverse [] + + What should GHCi do? Strictly speaking, the program is ambiguous. show (reverse []) + (which is what GHCi computes here) has type Show a => a and how that displays depends + on the type a. For example: + + ghci> (reverse []) :: String + "" + ghci> (reverse []) :: [Int] + [] + + However, it is tiresome for the user to have to specify the type, so GHCi extends Haskell's type-defaulting + rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows. If the expression yields a set of + type constraints that are all from standard classes (Num, Eq etc.), + and at least one is either a numeric class or the Show, + Eq, or Ord class, + GHCi will try to use one of the default types, just as described in the Report. + + - + Invoking GHCi invokingGHCi - + GHCi is invoked with the command ghci or - ghc --interactive. A module or filename can - also be specified on the command line; this instructs GHCi to load - the that module or filename (and all the modules it depends on), - just as if you had said :load - module at the GHCi prompt - (see ). For example, to start GHCi - and load the program whose topmost module is in the file - Main.hs, we could say: + ghc ––interactive. One or more modules or + filenames can also be specified on the command line; this + instructs GHCi to load the specified modules or filenames (and all + the modules they depend on), just as if you had said + :load modules at the + GHCi prompt (see ). For example, to + start GHCi and load the program whose topmost module is in the + file Main.hs, we could say: $ ghci Main.hs - Note: only one module name or filename - may be given on the command line. - Most of the command-line options accepted by GHC (see ) also make sense in interactive mode. The ones that don't make sense are mostly obvious; for example, GHCi @@ -547,31 +695,44 @@ $ ghci Main.hs Packages packageswith GHCi - GHCi can make use of all the packages that come with GHC, - but note: packages must be specified on the - GHCi command line, you can't add extra packages after GHCi has - started up. For example, to start up GHCi with the - text package loaded: + Most packages (see ) are + available without needing to specify any extra flags at all: + they will be automatically loaded the first time they are + needed. + + For non-auto packages, however, you need to request the + package be loaded by using the -package flag: -$ ghci -package text +$ ghci -package data ___ ___ _ / _ \ /\ /\/ __(_) - / /_\// /_/ / / | | GHC Interactive, version 5.00, For Haskell 98. + / /_\// /_/ / / | | GHC Interactive, version 5.05, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. -Loading package std ... linking ... done. +Loading package base ... linking ... done. +Loading package haskell98 ... linking ... done. Loading package lang ... linking ... done. -Loading package text ... linking ... done. +Loading package concurrent ... linking ... done. +Loading package readline ... linking ... done. +Loading package unix ... linking ... done. +Loading package posix ... linking ... done. +Loading package util ... linking ... done. +Loading package data ... linking ... done. Prelude> - + + + The following command works to load new packages into a + running GHCi: - Note that GHCi also loaded the lang - package even though we didn't ask for it: that's because the - text package makes use of one or more of the - modules in lang, and therefore has a - dependency on it. + +Prelude> :set -package name + + + But note that doing this will cause all currently loaded + modules to be unloaded, and you'll be dumped back into the + Prelude. @@ -580,7 +741,10 @@ Prelude> Extra libraries may be specified on the command line using the normal -llib - option. For example, to load the “m” library: + option. (The term library here refers to + libraries of foreign object code; for using libraries of Haskell + source code, see .) For + example, to load the “m” library: $ ghci -lm @@ -588,16 +752,32 @@ $ ghci -lm On systems with .so-style shared libraries, the actual library loaded will the - liblib.so. If - no such library exists on the standard library search path, - including paths given using - -Lpath, then - ghci will signal an error. + liblib.so. GHCi + searches the following places for libraries, in this order: + + + + Paths specified using the + -Lpath + command-line option, + + + the standard library search path for your system, + which on some systems may be overriden by setting the + LD_LIBRARY_PATH environment + variable. + + On systems with .dll-style shared libraries, the actual library loaded will be lib.dll. Again, GHCi will signal an error if it can't find the library. + + GHCi can also load plain object files + (.o or .obj depending on + your platform) from the command-line. Just add the name the + object file to the command line. @@ -615,6 +795,39 @@ $ ghci -lm + :add + module ... + :add + + Add module(s) to the + current target set, and perform a + reload. + + + + + :browse + *module + ... + :browse + + + Displays the identifiers defined by the module + module, which must be either + loaded into GHCi or be a member of a package. If the + * symbol is placed before the module + name, then all the identifiers defined + in module are shown; otherwise + the list is limited to the exports of + module. The + *-form is only available for modules + which are interpreted; for compiled modules (including + modules from packages) only the non-* + form of :browse is available. + + + + :cd dir :cd @@ -624,6 +837,12 @@ $ ghci -lm beginning of dir will be replaced by the contents of the environment variable HOME. + + NOTE: changing directories causes all currently loaded + modules to be unloaded. This is because the search path is + usually expressed using relative directories, and changing + the search path in the middle of a session is not + supported. @@ -670,11 +889,11 @@ Prelude> :mycd .. Or I could define a simple way to invoke - “ghc --make Main” in the + “ghc ––make Main” in the current directory: -Prelude> :def make (\_ -> return ":! ghc --make Main") +Prelude> :def make (\_ -> return ":! ghc ––make Main") @@ -691,27 +910,68 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") - :load module + :info name + ... + :info + + + Displays information about the given name(s). For + example, if name is a class, then + the class methods and their types will be printed; if + name is a type constructor, then + its definition will be printed; if + name is a function, then its type + will be printed. If name has + been loaded from a source file, then GHCi will also display + the location of its definition in the source. + + + + + :load + module ... :load - Recursively loads module - (which may be a module name or filename), and all the - modules it depends on. All previously loaded modules are - forgotten. The module module is - known as the target. + Recursively loads the specified + modules, and all the modules they + depend on. Here, each module + must be a module name or filename, but may not be the name + of a module in a package. + + All previously loaded modules, except package modules, + are forgotten. The new set of modules is known as the + target set. Note that + :load can be used without any arguments + to unload all the currently loaded modules and + bindings. + + After a :load command, the current + context is set to: + + + + module, if it was loaded + successfully, or + + + the most recently successfully loaded module, if + any other modules were loaded as a result of the current + :load, or + + + Prelude otherwise. + + - :module module + :module +|- *mod1 ... *modn :module - Sets the current context for statements typed at the - prompt to module, which must be a - module name which is already loaded or in a package. See - for more information on what - effect the context has on what entities are in scope at the - prompt. + Sets or modifies the current context for statements + typed at the prompt. See for + more details. @@ -728,11 +988,11 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") :reload :reload - Attempts to reload the current target (see - :load) if it, or any module it depends - on, has changed. Note that this may entail loading new - modules, or even dropping modules which are no longer - indirectly required by the target. + Attempts to reload the current target set (see + :load) if any of the modules in the set, + or any dependent module, has changed. Note that this may + entail loading new modules, or dropping modules which are no + longer indirectly required by the target. @@ -748,6 +1008,45 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") + :set args + arg ... + :set + + Sets the list of arguments which are returned when the + program calls System.getArgsgetArgs + . + + + + + :set prog + prog + :set + + Sets the string to be returned when the program calls + System.getProgNamegetProgName + . + + + + + :show bindings + :show bindings + + Show the bindings made at the prompt and their + types. + + + + + :show modules + :show modules + + Show the list of modules currently load. + + + + :type expression :type @@ -800,6 +1099,12 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") ‘+” and “command-line” options, which begin with ‘-’. + NOTE: at the moment, the :set command + doesn't support any kind of quoting in its arguments: quotes will + not be removed and cannot be used to group words together. For + example, :set -DFOO='BAR BAZ' will not do what + you expect. + GHCi options optionsGHCi @@ -858,7 +1163,7 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") - + Setting GHC command-line options in GHCi Normal GHC command-line options may also be set using @@ -883,15 +1188,16 @@ Prelude> :set -fno-glasgow-exts lists the reverse for each option where applicable. - Certain static options (, - , and in particular) will - also work, but may not take effect until the next reload. + Certain static options (, + , , and + in particular) will also work, but some may + not take effect until the next reload. staticoptions - - The <filename>.ghci</filename> file + + The <filename>.ghci</filename> file .ghcifile startupfiles, GHCi @@ -920,31 +1226,42 @@ Prelude> :set -fno-glasgow-exts a static one, but in fact it works to set it using :set like this. The changes won't take effect until the next :load, though.) - - - FAQ and Things To Watch Out For - + Two command-line options control whether the + .ghci files are read: + - System.exit causes GHCi to exit! - System.exitin - GHCi + + + - Yes, it does. + Don't read either ./.ghci or + $HOME/.ghci when starting up. - - System.getArgs returns GHCi's command - line arguments! + + + - Yes, it does. + Read .ghci and + $HOME/.ghci. This is normally the + default, but the option may + be used to override a previous + option. + + + + + FAQ and Things To Watch Out For + + - The interpreter can't load modules with FFI + The interpreter can't load modules with foreign export declarations! Unfortunately not. We haven't implemented it yet. @@ -954,15 +1271,6 @@ Prelude> :set -fno-glasgow-exts - Hugs has a :add command for adding - modules without throwing away any that are already loaded. - Why doesn't this work in GHCi? - - We haven't implemented it yet. Sorry about that. - - - - -O doesn't work with GHCi! @@ -998,8 +1306,31 @@ Prelude> :set -fno-glasgow-exts properly with GHC's concurrency model. - + + After using getContents, I can't use + stdin again until I do + :load or :reload. + + + This is the defined behaviour of + getContents: it puts the stdin Handle in + a state known as semi-closed, wherein + any further I/O operations on it are forbidden. Because I/O + state is retained between computations, the semi-closed + state persists until the next :load or + :reload command. + + You can make stdin reset itself + after every evaluation by giving GHCi the command + :set +r. This works because + stdin is just a top-level expression that + can be reverted to its unevaluated state in the same way as + any other top-level expression (CAF). + + + +