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:.
Prelude> "hello" "hello" Prelude> putStrLn "hello" hello |
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 |
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 |
The error message contains some clues as to the transformation happening internally.
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.
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.
Exactly which entities are in scope in a given context depends on whether the context module is compiled or interpreted:
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.
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 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.
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 (see Section 4.12 in Haskell Libraries for more information on GHC's Exception support).
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: if you turn on the +t option, GHCi will show the type of each variable bound by a statement. For example:
Prelude> :set +t Prelude> let (x:xs) = [1..] x :: Integer xs :: [Integer] |
Whenever an expression (or a non-binding statement, to be precise) is typed at the prompt, GHCi implicitly binds its value to the variable it. For example:
Prelude> 1+2 3 Prelude> it * 2 6 |
This is a result of the translation mentioned earlier, namely that an expression e is translated to
let it = e; print it |
If the expression was 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.:
Prelude> Time.getClockTime Prelude> print it Wed Mar 14 12:23:13 GMT 2001 |
The corresponding translation for an IO-typed e is
it <- e |
Note that it is shadowed by the new value each time you evaluate a new expression, and the old value of it is lost.