Skip to content

Using the Glasgow Haskell Compiler

GHCI stands for “Glasgow Haskell Compiler”. This compiler is the go-to solution for compiling your Haskell code. It can be seen as the pendant to GCC, actually compiling your code to C under the hood. Here is quick tour of running, testing and building your code with GHCI.

Starting it:

Terminal window
ghci

Executing a hello-world file (main.hs):

main :: IO ()
main = putStrLn "Hello World"

When you saved your file, we can open it in GHCI like this:

Terminal window
ghci main.hs

Alternatively, you can start GHCI first, and then import your module main.hs:

Terminal window
ghci
:load main

No, your file won’t execute right now - opening like this kind of loads your file into GHCI, so that you can now play around with it. To execute it, type “main” and press enter, and you should see the output.

If you change something in the file, and want to run it again with GHCI opened, run:

Terminal window
:reload
main

Compiling your Haskell file to an executeable:

Terminal window
ghc main.hs
./main.o

Instead of compiling to run it, you can also use “runhaskell” to run your program instantly.

Terminal window
runhaskell main.hs

Getting the function signature of something:

:type head
---head :: [a] -> a

To quit GHCI:

Terminal window
:quit