Getting started with Haskell tooling
This article helps the Haskell neophyte get started with an IDE for Haskell. It assumes that you have a predeliction for IntelliJ.
Install stack
IntelliJ requires use of the Stack build tool. Install it by following these instructions.
Enable IntelliJ
In IntelliJ, install the IntelliJ-Haskell plugin. In File / Project Structure / SDKs
(⌘-;
), add an entry for Stack.
You can find the location of stack via which stack
, on OS X typically at /usr/local/bin/stack
.
Create a project
In the parent directory where you want to create a Haskell project, run stack new someproject
. The output should look like:
Downloading template "new-template" to create project "someproject" in someproject/ ...
:
Looking for .cabal or package.yaml files to use to init the project.
Using cabal packages:
- someproject/
Selecting the best among 16 snapshots...
* Matches lts-14.4
Selected resolver: lts-14.4
Initialising configuration using resolver: lts-14.4
Total number of user packages considered: 1
Writing configuration to file: someproject/stack.yaml
All done.
To build the project:
Stack project templates
You are not limited to the default template for new projects. The are more specialised versions available.
Open project in IntelliJ
To open the project, select File / New / Project from existing sources
. Choose Haskell Stack.
Navigate to and select the someproject
directory created above.
IntelliJ should open the project. This might take a while, even a long while, to download dependencies and look busy generally.
IDE capabilities
IntelliJ can provide some basic IDE capabilities, but if you are used to its capabilities in Java, Kotlin or Scala, you might find the range of capabilities surprisingly minor. Some things it can do:
- reformat code (
⌘-L
) – this is equivalent to running thehindent
tool - organise imports (
⌘-O
) - auto-import (via
⌥-Enter
popup menu) - apply suggestion (via
⌥-Enter
popup menu) – suggestions are equivalent to those generated by thehlint
tool - go to definitions, find usages (
⌘-B
)
Development workflow
Compilation in IntelliJ is a bit hit and miss, so I keep a terminal window open with the following tools running.
ghcid
The simplest way to run this is to issue the command ghcid
in the project directory. ghcid
sees the Stack project and uses it to compile the project on file save, which happens pretty continuously in IntelliJ. See Neil Mitchell’s ghcid documentation.
Running tests
To execute your unit tests, run stack test
. You can auto-run tests on file save using stack test --fast --file-watch
, but it goes somewhat beserk because of the frequency of IntelliJ auto-save.