X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fghci.sgml;h=d87c1db93dcd5e5a61323aed00622fc2afb400b2;hb=3092faa7bbee99ca6cd56425d8ea67f3dc9555b2;hp=f8f5bbd0ad021e55f33b07324afb55deecdb621a;hpb=0cce925f92d4bdd742073445e26da5378da4bf49;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ghci.sgml b/ghc/docs/users_guide/ghci.sgml index f8f5bbd..d87c1db 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 foreign export, at the moment - the language extensions that GHC provides. + 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 @@ -158,12 +191,12 @@ Main> fac 17 or it can be set using the :set command from within GHCi (see )Note that in - GHCi, and mode, the + 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 . + linkend="search-path">. One consequence of the way that GHCi follows dependencies to find modules to load is that every module must have a source @@ -174,10 +207,6 @@ Main> fac 17 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. - - One final note: if you load a module called Main, it must - contain a main function, just like in - GHC. @@ -202,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 @@ -234,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, @@ -244,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 @@ -268,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 ) @@ -284,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. @@ -294,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 ) @@ -305,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 @@ -357,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: - 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. + +:module +|- *mod1 ... *modn + + + 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. + @@ -464,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 @@ -478,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: @@ -538,15 +637,42 @@ 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. One or more modules or + 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 @@ -569,29 +695,33 @@ $ ghci Main.hs Packages packageswith GHCi - GHCi can make use of all the packages that come with GHC, - 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> - - - 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. + The following command works to load new packages into a running GHCi: @@ -602,7 +732,7 @@ 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. + Prelude. @@ -611,7 +741,10 @@ Prelude> :set -package name 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 @@ -673,6 +806,28 @@ $ ghci -lm + :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 @@ -682,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. @@ -728,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") @@ -779,7 +940,10 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") All previously loaded modules, except package modules, are forgotten. The new set of modules is known as the - target set. + 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: @@ -802,15 +966,12 @@ Prelude> :def make (\_ -> return ":! ghc --make Main") - :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. @@ -847,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 @@ -899,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 @@ -1055,30 +1261,6 @@ Prelude> :set -fno-glasgow-exts - GHCi complains about main not being - in scope when I load a module. - mainwith GHCi - - - You probably omitted the module - declaration at the top of the module, which causes the - module name to default to Main. In - Haskell, the Main module must define a - function called main. Admittedly this - doesn't make a great deal of sense for an interpreter, but - the rule was kept for compatibility with GHC. - - - - - System.getArgs returns GHCi's command - line arguments! - - Yes, it does. - - - - The interpreter can't load modules with foreign export declarations!