Setting Up Haskell Development Environment – A 10 Minute Guide

Starting with a right development environment can save you from a lot of future headaches. So here is a short write up on how to quickly setup your own haskell development environment.

Stack

Stack is a cross platform build tool. It can be used for setting up your project, installing compiler and dependencies, building and deploying the generated binary. It  You can follow the instructions on this page to install stack.
To setup a new project with stack you can run stack new command.

stack new <project-name>

Once the setup is done, you can cd to your project and build it

cd <project-name>
stack build

Stack gives you a file-watch option which watches for file changes in your project and build the project automatically after every file change.

stack build --file-watch

If you look at the end of the output from the build command, you’ll find the path where stack saved the executable after build. stack provides you with an exec option to run that binary without typing the whole executable path.

stack exec <your-project-exe>

There are a lot more features which are given by the stack. You can read more about it in the stack doc.

Setting up an IDE

There is a lack of good out of the box IDE in the haskell world. If you are brave enough, you can get on your haskell journey with a plain text editor but having IDE like features are always a nice to have.
Though there is no one stop solution for it, there have been a lot of plugins like hlint, stylish-haskell, ghc-mod, haskell-ide-engine which one can integrate with their favorite text editor.

Misc

Documentation

You can use stack to access documentation of packages locally

stack haddock --open text

This will open up documentation of text package locally in your browser. Hayoo and hoogle are some other useful sites which can help you find docs easily.

Build System

If you are a fan of something like a make file, you can use shake to write a build system for your project.

ghci

Haskell gives you a nice repl environment with ghci.  You can fire up a ghci session using stack

stack ghci

You can interactively run functions, evaluate expressions, find their types, test your code etc in it. If you want to know more, read about it here.

You can even add dynamic breakpoints in your code using ghci. More on it in haskell wiki.

ghcid

ghci can also be used to setup a fast incremental development setup using ghcid. ghcid uses ghci behind the scenes to build the code and can be set to reload on file change. You can also run a command after each build using the --test flag. Read this beautiful blog-post by Matt Parsons to know more about it.

Further Reading

http://seanhess.github.io/2015/08/04/practical-haskell-getting-started.html

https://docs.haskellstack.org/en/stable/GUIDE/

http://haroldcarr.com/posts/2017-10-24-emacs-haskell-dev-env.html

https://github.com/parsonsmatt/intero-neovim

https://lexi-lambda.github.io/blog/2018/02/10/an-opinionated-guide-to-haskell-in-2018/