Skip to content

GHC, GHCi & Stack

Haskell is a compiled language, just as C, C++, Rust and many others, and unlike Python, Ruby or Scheme. This means that once compiled, your code no longer needs the Haskell compiler installed to run. It also means that your program usually runs orders of magnitude faster than when interpreted. It has a major downside though. In Python or Scheme, you can load your program into the interpreter and then inspect the behaviour of individual functions interactively, which is a great way to explore why some part of your program misbehaves. Trying out pieces of code in a compiled language usually requires instrumentation and is thus more difficult. The Haskell compiler system, GHC, comes with two programs: the compiler proper, GHC, and an interactive environment, GHCi, where you can explore pieces of code interactively just as you would in Python or Scheme. You get the best of both worlds.

When using Stack to interact with your Haskell compiler, as we do in this course, you will rarely call GHC directly. Instead, we will later use commands such as stack build and stack run to compile and run our programs. To start the interactive environment, GHCi, you run stack ghci from your shell prompt.

So how do you start working with the Haskell Stack? You have two options:

  • You can install it on your personal computer. Follow the instructions at docs.haskellstack.org to do this. The installation is easy, but if you go this route and you run into trouble, you are on your own: neither I nor the TAs or the Faculty's tech support team have the manpower to help you troubleshoot the installation process on your own computer.

  • You can use the installation available on timberlea.cs.dal.ca. To use it, you have to log into timberlea via ssh (from Linux or MacOS) or PuTTY (from Windows). Again, installation of PuTTY on your Windows laptop is easy, and there are plenty of tutorials available online on how to use PuTTY to connect to some remote host.

    Warning

    Before you start using Stack on timberlea, you must read the following section.

Stack on Timberlea

Stack's package management means that normally, every user has their own local copy of the Haskell compiler and accompanying packages according to the Stack snapshot they use. Different programs you develop may use different snapshots of the compiler and packages. In that case, Stack makes sure that you have local copies of the compiler and packages needed for each of your programs to compile. The downside of this approach is that in a shared environment such as timberlea.cs.dal.ca, it is far from optimal if each of you in this fairly large class downloads about 1GB worth of compiler and packages. Thus, there is a way to instruct Stack to use the compiler version installed in a system-wide location. I urge you to do this now, to save yourself and our system administrators some serious grief.

You need to perform two configuration steps for things to work correctly:

  1. Instruct Stack to use the preinstalled compiler:

    Shell
    $ stack config set system-ghc --global true
    

    This command needs to be run from the shell prompt on timberlea, so you need to connect to timberlea first, via ssh or PuTTY.

  2. If you do the previous step but not this second step, then Stack will still download your own copy of GHC the first time you try to use it. The reason is that the most recent Stack snapshot, which is used by default if you don't select a different snapshot, uses a more recent version of GHC than is installed on Timberlea. Since the preinstalled compiler doesn't match, Stack goes ahead and downloads your own copy of the newest version of GHC. To prevent this, you need to pin Stack to a snapshot that works with the GHC version installed on Timberlea. Here's how you do this (again, from the shell prompt on timberlea):

    Shell
    $ mkdir -p ~/.stack/global-project
    

    Now use your favourite editor to create the following file:

    ~/.stack/global-project/stack.yaml
    resolver: lts-18.28
    packages: []
    

With these two simple steps out of the way, you can use GHCi without any massive downloads. Towards the end of this book, we will start building standalone executables in Haskell. For those, you also need to ensure that your project using the correct Stack snapshot. I'll tell you how to that when we get there.

GHCi

GHCi is Haskell's interactive shell, similar to what you would get in Python if you were to run python without any arguments. When using the Haskell Stack, the shell command to start GHCi is stack ghci:

Shell
$ stack ghci
[Lots of startup messages]
Prelude> █

Prelude> is GHCi's command prompt. I'll explain in the next section what Prelude means and how to customize your prompt if you desire.

To leave GHCi, you enter the command :quit:

GHCi
Prelude> :quit
Leaving GHCi.

You can also abbreviate the command to :q or just press Ctrl+D.

Most of what you enter at the GHCi prompt is standard Haskell code. When you enter a command that starts with :, such as :quit, then that's not Haskell code but a command to control the behaviour of GHCi itself. Here, you cause GHCi to exit.