X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fghci.sgml;h=5743d89510a83c497c0c3a5d7dc5056ebd8513f9;hb=a41b1c9d82459da16aa0d66dae41488658e7e936;hp=87f294447500a7c3b19dd5fcdc15028810efcb57;hpb=950c1ecaffcb6e8d7cefb20b8372691182c6c304;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ghci.sgml b/ghc/docs/users_guide/ghci.sgml index 87f2944..5743d89 100644 --- a/ghc/docs/users_guide/ghci.sgml +++ b/ghc/docs/users_guide/ghci.sgml @@ -105,18 +105,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 @@ -234,7 +235,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 +245,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 +282,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 +298,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 +308,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 ) @@ -357,60 +371,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: - 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. + +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 + + + 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. + @@ -478,6 +540,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: @@ -673,6 +743,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 @@ -802,15 +894,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 +936,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 @@ -1071,14 +1199,6 @@ Prelude> :set -fno-glasgow-exts - System.getArgs returns GHCi's command - line arguments! - - Yes, it does. - - - - The interpreter can't load modules with foreign export declarations!