X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fdocs%2Fusers_guide%2Fghci.xml;h=786815d484229e39d695f6907f66963489a1292a;hb=fec3bab822c13cf4fa1bc51170fd7ca1fe0d2111;hp=374b28c5597e9d31438ecf76e2e8ffd4c0ba982c;hpb=b9de29ac3fbf5192049a0bcaf1ab0c8bbbd57a83;p=ghc-hetmet.git diff --git a/ghc/docs/users_guide/ghci.xml b/ghc/docs/users_guide/ghci.xml index 374b28c..786815d 100644 --- a/ghc/docs/users_guide/ghci.xml +++ b/ghc/docs/users_guide/ghci.xml @@ -10,7 +10,7 @@ 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 familiar 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 @@ -59,10 +59,13 @@ Prelude> :set <option> ... set options :set args <arg> ... set the arguments returned by System.getArgs :set prog <progname> set the value returned by System.getProgName - + :set prompt <prompt> set the prompt used in GHCi + :show modules show the currently loaded modules :show bindings show the current bindings made at the prompt + :ctags [<file>] create tags file for Vi (default: "tags") + :etags [<file>] create tags file for Emacs (defauilt: "TAGS") :type <expr> show the type of <expr> :kind <type> show the kind of <type> :undef <cmd> undefine user-defined command :<cmd> @@ -361,42 +364,148 @@ Ok, modules loaded: A, B, C, D. Interactive evaluation at the prompt When you type an expression at the prompt, GHCi immediately - evaluates and prints the result. But that's not the whole story: - if you type something of type IO a for some - a, then GHCi executes it - as an IO-computation, and doesn't attempt to print the - result:. + evaluates and prints the result: + +Prelude> reverse "hello" +"olleh" +Prelude> 5+5 +10 + + +I/O actions at the prompt + +GHCi does more than simple expression evaluation at the prompt. +If you type something of type IO a for some + a, then GHCi executes it + as an IO-computation. Prelude> "hello" "hello" Prelude> putStrLn "hello" hello +Furthermore, GHCi will print the result of the I/O action if (and only +if): + + The result type is an instance of Show. + The result type is not + (). + +For example, remembering that putStrLn :: String -> IO (): + +Prelude> putStrLn "hello" +hello +Prelude> do { putStrLn "hello"; return "yes" } +hello +"yes" + + - What actually happens is that GHCi typechecks the - expression, and if it doesn't have an IO type, - then it transforms it as follows: an expression - e turns into - - let it = e; - print it + + Using <literal>do-</literal>notation at the prompt + do-notationin GHCi + statementsin GHCi + + GHCi actually accepts statements + rather than just expressions at the prompt. This means you can + bind values and functions to names, and use them in future + expressions or statements. + + The syntax of a statement accepted at the GHCi prompt is + exactly the same as the syntax of a statement in a Haskell + do expression. However, there's no monad + overloading here: statements typed at the prompt must be in the + IO monad. + +Prelude> x <- return 42 +42 +Prelude> print x +42 +Prelude> - which is then run as an IO-action. + The statement x <- return 42 means + “execute return 42 in the + IO monad, and bind the result to + x”. We can then use + x in future statements, for example to print + it as we did above. - Hence, the original expression must have a type which is an - instance of the Show class, or GHCi will - complain: + GHCi will print the result of a statement if and only if: + + + The statement is not a binding, or it is a monadic binding + (p <- e) that binds exactly one + variable. + + + The variable's type is not polymorphic, is not + (), and is an instance of + Show + + + + Of course, you can also bind normal non-IO expressions + using the let-statement: -Prelude> id -No instance for `Show (a -> a)' -arising from use of `print' -in a `do' expression pattern binding: print it +Prelude> let x = 42 +Prelude> x +42 +Prelude> + + Another important difference between the two types of binding + is that the monadic bind (p <- e) is + strict (it evaluates e), + whereas with the let form, the expression + isn't evaluated immediately: + +Prelude> let x = error "help!" +Prelude> print x +*** Exception: help! +Prelude> - The error message contains some clues as to the - transformation happening internally. + Note that let bindings do not automatically + print the value bound, unlike monadic bindings. + + Any exceptions raised during the evaluation or execution + of the statement are caught and printed by the GHCi command line + 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 + module context. + + WARNING: temporary bindings introduced at the prompt only + last until the next :load or + :reload command, at which time they will be + simply lost. However, they do survive a change of context with + :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: + +t + +Prelude> :set +t +Prelude> let (x:xs) = [1..] +x :: Integer +xs :: [Integer] + + + What's really in scope at the prompt? @@ -505,92 +614,6 @@ Prelude,IO> - - Using <literal>do-</literal>notation at the prompt - do-notationin GHCi - statementsin GHCi - - GHCi actually accepts statements - rather than just expressions at the prompt. This means you can - bind values and functions to names, and use them in future - expressions or statements. - - The syntax of a statement accepted at the GHCi prompt is - exactly the same as the syntax of a statement in a Haskell - do expression. However, there's no monad - overloading here: statements typed at the prompt must be in the - IO monad. - - Here's an example: - -Prelude> x <- return 42 -Prelude> print x -42 -Prelude> - - The statement x <- return 42 means - “execute return 42 in the - IO monad, and bind the result to - x”. We can then use - x in future statements, for example to print - it as we did above. - - Of course, you can also bind normal non-IO expressions - using the let-statement: - -Prelude> let x = 42 -Prelude> print x -42 -Prelude> - - An important difference between the two types of binding - is that the monadic bind (p <- e) is - strict (it evaluates e), - whereas with the let form, the expression - isn't evaluated immediately: - -Prelude> let x = error "help!" -Prelude> print x -*** Exception: help! -Prelude> - - Any exceptions raised during the evaluation or execution - of the statement are caught and printed by the GHCi command line - 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 - module context. - - WARNING: temporary bindings introduced at the prompt only - last until the next :load or - :reload command, at which time they will be - simply lost. However, they do survive a change of context with - :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: - +t - -Prelude> :set +t -Prelude> let (x:xs) = [1..] -x :: Integer -xs :: [Integer] - - - The <literal>it</literal> variable @@ -606,18 +629,31 @@ Prelude> 1+2 Prelude> it * 2 6 - - This is a result of the translation mentioned earlier, - namely that an expression e is - translated to + What actually happens is that GHCi typechecks the + expression, and if it doesn't have an IO type, + then it transforms it as follows: an expression + e turns into let it = e; print it - before execution, resulting in a binding for - it. + which is then run as an IO-action. + + Hence, the original expression must have a type which is an + instance of the Show class, or GHCi will + complain: + + +Prelude> id +No instance for `Show (a -> a)' +arising from use of `print' +in a `do' expression pattern binding: print it + - If the expression was of type IO a for + The error message contains some clues as to the + transformation happening internally. + + If the expression was instead of type IO a for some a, then it will be bound to the result of the IO computation, which is of type a. eg.: @@ -659,16 +695,31 @@ Wed Mar 14 12:23:13 GMT 2001 [] 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. + rules (Section 4.3.4 of the Haskell 98 Report (Revised)) as follows. The + standard rules take each group of constraints (C1 a, C2 a, ..., Cn + a) for each type variable a, and defaults the + type variable if + + The type variable a + appears in no other constraints + All the classes Ci are standard. + At least one of the classes Ci is + numeric. + + At the GHCi prompt, the second and third rules are relaxed as follows + (differences italicised): + + All of the classes + Ci are single-parameter type classes. + At least one of the classes Ci is + numeric, or is Show, + Eq, or Ord. + - + Invoking GHCi invokingGHCi @@ -765,7 +816,7 @@ $ ghci -lm the standard library search path for your system, - which on some systems may be overriden by setting the + which on some systems may be overridden by setting the LD_LIBRARY_PATH environment variable. @@ -906,6 +957,19 @@ Prelude> :mycd .. Prelude> :def make (\_ -> return ":! ghc ––make Main") + We can define a command that reads GHCi input from a + file. This might be useful for creating a set of bindings + that we want to repeatedly load into the GHCi session: + + +Prelude> :def . readFile +Prelude> :. cmds.ghci + + + Notice that we named the command + :., by analogy with the + ‘.’ Unix shell command that + does the same thing. @@ -1033,7 +1097,7 @@ Prelude> :def make (\_ -> return ":! ghc ––make Main") :set args arg ... - :set + :set args Sets the list of arguments which are returned when the @@ -1045,7 +1109,7 @@ Prelude> :def make (\_ -> return ":! ghc ––make Main") :set prog prog - :set + :set prog Sets the string to be returned when the program calls @@ -1056,6 +1120,19 @@ Prelude> :def make (\_ -> return ":! ghc ––make Main") + :set prompt prompt + + + Sets the string to be used as the prompt in GHCi. + Inside prompt, the sequence + %s is replaced by the names of the + modules currently in scope, and %% is + replaced by %. + + + + + :show bindings :show bindings @@ -1077,6 +1154,27 @@ Prelude> :def make (\_ -> return ":! ghc ––make Main") + :ctags filename + :etags filename + :etags + + :etags + + + + Generates a “tags” file for Vi-style editors + (:ctags) or Emacs-style editors (etags). If + no filename is specified, the defaulit tags or + TAGS is + used, respectively. Tags for all the functions, constructors and + types in the currently loaded modules are created. All modules must + be interpreted for these commands to work. + See also . + + + + + :type expression :type