Personal tools


From HaskellWiki

< GHC(Difference between revisions)
Jump to: navigation, search
(Customized GHCi interactive environments)

Revision as of 13:14, 9 April 2008


1 Using GHCi

This page is a place to collect advice about how to use GHC's interactive interpreter, GHCi. Please add to it!

1.1 External tool integration

External command-line tools like Hoogle can be integrated in GHCi by adding a line to .ghci similar to

:def hoogle \str -> return $ ":! hoogle -n 15 \"" ++ str ++ "\""

Make sure that the directory containing the executable is in your PATH environment variable or modify the line to point directly to the executable. Invoke the executable with commands like

:hoogle map

1.2 Using :def

The :def command, documented here, allows quite GHCi's commands to be extended in quite a powerful way.

Here is one example.

  Prelude> let loop = do { l <- getLine; if l == "\^D" then return () else do appendFile "foo.hs" (l++"\n"); loop }
  Prelude> :def pasteCode (\_ -> loop >> return ":load foo.hs")

This defines a new command :pasteCode, which allows you to paste Haskell code diretly into GHCi. You type the command :pasteCode, followed by the code you want, followed by ^D, followed (unfortunately) by enter, and your code is executed. Thus:

  Prelude> :pasteCode
  x = 42
  Compiling Main             ( foo.hs, interpreted )
  Ok, modules loaded: Main.
  *Main> x

1.3 Customized GHCi interactive environments

You can create shell commands that start up GHCi and initialize it for use as a specialized interactive computing environment for any purpose that you can imagine.

The idea is that if you put the following lines in your .ghci file, GHCi will load commands at startup from whatever file whose path you specify in the GHCIRC environment variable. You can then easily write shell scripts that exploit this to initialize GHCi in any manner you please.

-- Read GHCI commands from the file whose name is
-- in the GHCIRC environment variable
:def _load const(System.Environment.getEnvironment>>=maybe(return"")readFile.lookup"GHCIRC")
:undef _load

2 A readline-aware GHCi on Windows

Mauricio reports: I've just uploaded a package (rlwrap) to Cygwin that I like to use with ghci. You can use it like this:


and then you will use ghc as if it were readline aware (i.e., you can press up arrow to get last typed lines etc.). rlwrap is very stable and I never had unexpected results while using it.

Since the issue of ghci integration with terminals has been raised here sometimes, I thought some guys here would be interested (actually, I found rlwrap looking for a better way to use ghci).

3 How do I stop GHCi from printing the result of a bind statement?

Sometimes you want to perform an IO action at the prompt that will produce a lot of data (e.g. reading a large file). When you try to do this, GHCi will helpfully spew this data all over your terminal, making the console temporarily unavailable.

To prevent this, use :set -fno-print-bind-result. If you want this option to be permanently set, add it to your .ghci file.

4 Using .ghci, a mini-tutorial

There is a lot more one can do to customize and extend GHCi. Some extended examples can be found in an email posted to haskell-cafe, titled getting more out of ghci. Dating from September 2007, and using GHC 6.6.1, some of the GHCi tickets mentioned in there have since been fixed, but the message should still serve as a useful introduction to writing your own .ghci files. It also provides several useful commands you might want to copy into your own file!-) Newer GHCis support the multiline commands mentioned in the message, allowing for more readable .ghci files (at the time, definitions had to be squashed into single lines, so you have to read the message to understand the `.ghci` file). For those still using older GHCis, a variant file for 6.4.1 is available, too:

"getting more out of ghci", the mini-tutorial

squashed .ghci, for 6.6.1 or later

squashed .ghci, for 6.4.1