Personal tools

GHC/GHCi

From HaskellWiki

< GHC(Difference between revisions)
Jump to: navigation, search
(Using GHCi)
(+cat)
Line 1: Line 1:
  +
[[Category:GHC|GHCi]]
 
== Using GHCi ==
 
== Using GHCi ==
   

Revision as of 11:46, 13 January 2007

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 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
  ^D
  Compiling Main             ( foo.hs, interpreted )
  Ok, modules loaded: Main.
  *Main> x
  42
  *Main>

1.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:

  rlwrap ghcii.sh

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