Setting up Emacs and Haskell on Linux

02.09.2014 Permalink

I'm curious about how functional programming feels with Haskell, so I recently invested a few hours to install it and prepare my Emacs configuration to support it. To spare your precious time I recorded the steps I took. It should work without hassle on Debian based distros. We'll start with the Haskell platform:
  1. To install it issue in a terminal sudo apt-get install haskell-platform.
  2. This will also include Cabal, the widely used build system. Since it uses a local package list you'll first have to update it before you can install anything. Use this command: cabal update
  3. I learned that Emacs support for Haskell often consists of two parts: Emacs Elisp packages stored somewhere in ~/.emacs.d which are backed by some executables stored in ~/.cabal/bin. To begin with, we need structured-haskell-mode and ghc-mod, and we can install those using Cabal: cabal install structured-haskell-mode ghc-mod. This takes some minutes, so you might want to read another section of the friendly introduction Learn You a Haskell for Great Good.
  4. Finally, you should edit your ~/.profile to add the ~/.cabal/bin path. You can a add a line like this: PATH="$PATH:$HOME/.cabal/bin". Make sure the path is in effect before you try to work on Haskell in Emacs. For this, you can do a source ~/.profile and start Emacs from the same terminal.
Now, let's turn to Emacs. In case you don't have Emacs you can install the latest Emacs like this:
  1. Add the snapshot PPA by issuing sudo apt-add-repository ppa:ubuntu-elisp/ppa in a terminal window.
  2. To actually install Emacs type sudo apt-get install emacs-snapshot emacs-snapshot-el
Please note that there exist comprehensive and well maintained Emacs configurations out there, some of which include Haskell support, one example is Emacs Prelude.

If you like to start with my settings you'll find my ~/.emacs.d contents on GitHub, to get it use git clone https://github.com/friemen/emacsd.git ~/.emacs.d. Please note that this will overwrite stuff in any existing ~/.emacs.d folder. Afterwards start Emacs, wait until install is complete and restart Emacs. You'll find my favorite keyboard shortcuts in the README on GitHub.

We start with a project around our first lines of Haskell code:
  1. In a terminal create a new directory and cd into it: mkdir helloworld && cd helloworld.
  2. Create a .cabal file: cabal init. Use defaults (hit Enter), except where you are asked for Library or Executable. Choose Executable.
  3. Open helloworld.cabal and add Main.hs to the line that starts with -- main-is:. Remove -- to uncomment the line.
  4. Create a new file helloworld/Main.hs with contents like below.
  5. Start the REPL (which is an interactive GHC) using C-c C-l. Answer y and hit Enter twice. Now your code is compiled and ready to be executed. You can call main now.
  6. In order to create an executable you have to first issue cabal configure on the command line.
  7. Hitting C-c C-c in Emacs will execute cabal build which creates an executable file somewhere in the dist tree.
  8. If you changed some code you can always hit C-c C-l to recompile it and make it available for the REPL.
That's it. Oh, and here's the example code I used:
module Main where

main :: IO ()
main = putStrLn $ hello "World"

hello :: String -> String
hello x = "Hello " ++ x
Now you're ready to dive in. Have fun!