3.4. Interactive evaluation at the prompt


Prelude> "hello"
Prelude> putStrLn "hello"

             let it = e;
             print it
which is then run as an IO-action.


Prelude> id
No instance for `Show (a -> a)'
arising from use of `print'
in a `do' expression pattern binding: print it


3.4.1. What's really in scope at the prompt?



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 )



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"

(Note: :module can be shortened to :m). The full syntax of the :module command is:


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).



3.4.2. Using do-notation at the prompt



Here's an example:

Prelude> x <- return 42
Prelude> print x



Prelude> let x = 42
Prelude> print x


Prelude> let x = error "help!"
Prelude> print x
*** Exception: help!





Prelude> :show bindings
x :: Int


Prelude> :set +t
Prelude> let (x:xs) = [1..]
x :: Integer
xs :: [Integer]

3.4.3. The it variable


Prelude> 1+2
Prelude> it * 2

             let it = e;
             print it
before execution, resulting in a binding for it.


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


3.4.4. Type defaulting in GHCi

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.