X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fghci.sgml;h=d87c1db93dcd5e5a61323aed00622fc2afb400b2;hb=3092faa7bbee99ca6cd56425d8ea67f3dc9555b2;hp=0919b20394222e60081c9b632bfc40ca98d86e68;hpb=7bcdfdba58be3bd3159a87ca8413c28e3c64d147;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ghci.sgml b/ghc/docs/users_guide/ghci.sgml index 0919b20..d87c1db 100644 --- a/ghc/docs/users_guide/ghci.sgml +++ b/ghc/docs/users_guide/ghci.sgml @@ -44,36 +44,36 @@ Prelude> Commands available from the prompt: - %lt;stmt> evaluate/run %lt;stmt> - :add %lt;filename> ... add module(s) to the current target set - :browse [*]%lt;module> display the names defined by %lt;module> - :cd %lt;dir> change directory to %lt;dir> - :def %lt;cmd> %lt;expr> define a command :%lt;cmd> + <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 [%lt;name> ...] display information about the given names - :load %lt;filename> ... load module(s) and their dependents - :module [+/-] [*]%lt;mod> ... set the context for expression evaluation + :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 %lt;option> ... set options - :set args %lt;arg> ... set the arguments returned by System.getArgs - :set prog %lt;progname> set the value returned by System.getProgName + :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 %lt;expr> show the type of %lt;expr> - :undef %lt;cmd> undefine user-defined command :%lt;cmd> - :unset %lt;option> ... unset options + :type <expr> show the type of <expr> + :undef <cmd> undefine user-defined command :<cmd> + :unset <option> ... unset options :quit exit GHCi - :!%lt;command> run the shell command %lt;command> + :!<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 - -%lt;flags> 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.) @@ -102,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) @@ -112,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 @@ -145,7 +162,7 @@ Ok, modules loaded: Main. indirectly, by the topmost module, and load them all in dependency order. - + Modules vs. filenames modulesand filenames filenamesof modules @@ -179,7 +196,7 @@ Ok, modules loaded: Main. 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 @@ -190,10 +207,6 @@ Ok, modules loaded: Main. 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. @@ -624,6 +637,33 @@ 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. + + @@ -655,26 +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 network - 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 network +$ ghci -package data ___ ___ _ / _ \ /\ /\/ __(_) - / /_\// /_/ / / | | GHC Interactive, version 5.04, for Haskell 98. + / /_\// /_/ / / | | GHC Interactive, version 5.05, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base ... linking ... done. Loading package haskell98 ... linking ... done. -Loading package network ... linking ... done. +Loading package lang ... 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 will also automatically load any packages - on which the requested package depends. + The following command works to load new packages into a running GHCi: @@ -685,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. @@ -694,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 @@ -787,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. @@ -1205,22 +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. - - - - The interpreter can't load modules with foreign export declarations!