Tuesday, February 7, 2012

Open library files from /w ghci

If you want to get some information about a function in GHC just do
ghci> :t sum
sum :: Num a => [a] -> a -- Defined in Data.List
What do you do if you want to look at the source of that file just from /w the interpreter ghci? You could do following.

Download the source from ghci an put it wherever you want. For all Debianer out there just do following:
apt-get source ghc
After that you have to define a macro in ghci. You can also define this macro in the configuration file for ghci in $HOME/.ghc/ghci.conf.
:def source \s -> return $ ":! openGHCLib.sh " ++ s
Afterwards you have to write the script openGHCLib.sh and put it into your PATH. The script openGHCLib.sh look like:
#!/bin/bash

# path where you put the source files of ghc
base_path=$HOME/tmp/ghc-7.0.4/libraries/base
file=`echo $1 | tr '.' '/'`
hfile="${base_path}/${file}"

if [ -e "${hfile}.hs" ]; then
  ${EDITOR} "${hfile}.hs"
elif [ -e "${hfile}.lhs" ]; then
  ${EDITOR} "${hfile}.lhs"
else
  echo "file: ${hfile}.hs or ${hfile}.lhs does not exist"
  exit 1
fi
After that you can open the library file /w ghci.
ghci> :source Data.List

No comments:

Post a Comment