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
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
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 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:
Prelude>
Which indicates that everything from the module
Prelude
is currently in scope. If we now
load a file into GHCi, the prompt will change:
Prelude> :load Main.hs Compiling Main ( Main.hs, interpreted ) *Main>
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
*
indicates
that it is the full top-level scope of
module
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:
: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.
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:
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
before execution, resulting in a binding for
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.
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.
The standard defaulting rules require that one of the classes is numeric; the difference here
is that defaulting is also triggered at least one is Show
,
Eq
, or Ord
.