Visual Studio Haskell



Pre-requisities¶

Haskell Language support Support for the Haskell programming language in Visual Studio Code. Hello everyone, In this video we see how to install Haskell in windows and run in visual studio code.Haskell is a general purpose language that can be used. Visual Studio Code, Emacs+haskell-mode, and Emacs are probably your best bets out of the 10 options considered. This page is powered by a knowledgeable community that.

I recently installed the Haskell extension in Visual Studio Code which is powered by the Haskell Language Server. Each time I open a different Haskell file in the editor a new process named 'haskell-language-server-1.0.0-linux-8.6.5' is created. Setting up haskell on visual studio code, macos in december 2020. Hi, I am looking forward to learning haskell and using it for my side projects. But the settup is really making me suffer. Is there a link or something on how to setup Visual Studio Code to be haskells IDE.

Use Linux or Mac. Do not waste time with Haskell on Windows - you may be able tomake some progress initially, but you will run into weird errors later whilebulding certain packages, or while building binaries. You have been warned.

Make sure you have a lot of RAM and an SSD. Serious Haskell projects (nottextbook problems) are memory-guzzlers during development & compilation. Plus,if you’re like me, you will have 50 browser tabs open, as well. So, it won’t belong before your system starts swapping, which is why it’s better to have an SSDalong with a lot of RAM. For serious Haskell development, we recommend atleast 10 GB of RAM and at least 256 GB SSD. For text-book practice, youcan probably get away with 4GB of RAM and an HDD.

Attention

Call for contribution

  • Screen-recording of installing Stack & Spacemacs + Intero along with the first GHCi session
  • Screen-recording of installing Stack & VSCode + HIE along with the first GHCi session

Installing Haskell¶

There are currently four ways to install Haskell! Yes, I know that’s crazy. Here’s what they are:

  • Via your OS’ native package manager, i.e. apt-get, or homebrew, or yum
  • Via a minimal installer orthe Haskell Platform
  • Via Stack(recommended)
  • Via Nix/NixOS

Important

Recommendation

Out of the four methods listed above, only Stack is recommended formaintaining your sanity. So, head over to Stack’s Get Started page and follow only the firststep, titled “Download Haskell Stack” for your OS. The other steps given onthat page are covered in greater detail below.

Quick primer on Stack¶

stack solves the following problems:

Debugger
  • Having different versions of the Haskell compiler (i.e. ghc) available onyour machine without messing things up, and using the right ghc versionfor your project.
  • Taking care of which Haskell libraries are known to compile/build with whichversion of ghc.
  • Taking care of the dependency graph of libraries, so that all the librariesthat your project depends on, compile successfully without you having tomanually specify the version of each library. Basically stack saves youfrom the dependency hell problem.

In a sense stack is similar to the following tools from other ecosystems, whichattempt to solve some, or all, of the same problems (but they may have solvedthem in a different manner):

  • rvm and bundler from the Ruby world
  • virtualenv from the Python world
  • gvm from the Go world
  • nvm or yarn from the NodeJS world

Installing an editor¶

If you are used to tools like Eclipse/IntelliJ from the Java world, or VisualStudio from the Microsoft world, you are in for a rude shock when it comes toIDEs in Haskell. Firstly, there is no industrial-grade [I]ntegrated[D]evelopment [E]nvironment for Haskell - so you can stop looking for it.Secondly, as of Dec 2017, various Haskell editor plugins, don’t work as wellas they should. But, don’t worry - all is not lost - you won’t have to resort towriting Haskell in ed or nano. There is stuff available - just loweryour expectations.

Here are two options that are known to work well:

  • Spacemacs with intero -If you are comfortable with the Emacs editor, this should be your preferred choice.
  • VS Code with HIE: Haskell IDE Engine -If you are comfortable with editors like Sublime, Atom, or VSCode itself, thisshould be your preferred choice. Please follow the instructions atInstalling VSCode & HIE

Installing VSCode & HIE¶

Note

If you choose to use VSCode + HIE here are the installation instructions. Youmay skip this sub-section if you plan on using any other editor. Pleasecomment/annotate if these instructions do not work for you.

  • Install the VSCode editor by downloading it from here

  • Install the hie binary (command-line tool) using the following steps (the last step stackinstall is going to take a lot of time - be patient).

  • Ensure that the hie binary (command-line tool) has installed successfullyby going through the following steps. Ensure that you see output similar to what is shown above after you runthe hie command. You can exit it by pressing Ctr+c

  • Open the VSCode editor and go the the “Extensions” tab, also known as the“plugin marketplace” => search for the haskelllanguageserver plugin (thecorrect plugin is the one authored by AlanZimmerman) => Click theinstall button.

  • Restart your editor.

  • Note down the resolver that HIE is using (instructions given below):

    Warning

    For HIE & VSCode users

    Next, open the haskell-ide-engine directory (the one which you clonedabove) and open the stack.yaml file. There may be multiple files likestack-8.2.1.yml or stack-8.2.2.yaml. Ignore those extra files. Youneed the stack.yaml file. Note down the resolver (very first line ofthis file). You will need to use this in the next section. [1]For example, on my machine, following are the contents of stack.yaml andthe resolver is lts-9.14. It may be different on your machine!

Get used to GHCi before you start¶

GHCi is the interactive coding environment for Haskell (also known as a REPL).You will be spending a lot of time in it. It comes wth a complete user-manualthat you can refer to when you need to do more advanced stuff, but, for now,here’s some basic stuff that you’ll need to know.

Setup your first throw-away project:

Warning

For VSCode & HIE users

In the command given below, make sure you use the correct LTS version as mentioned in hieWarning

The last command stacksetup may take forever, becuase it will probablydownload the GHC compiler and a bunch of core/base libraries. Also, it’s goingto take a shitload of disk-space (about 2GB+). Keep some covfefe (or beer)handy.

Create a new file called Throwaway.hs in your project:

Open first-project/src/Throwaway.hs and make sure it has the following contents:

Important

Visual Studio Code Haskell Debug

Please take care of the case. In Haskell, modules are named withCamelCase. The name of the module and the name of the file sould be thesame, for example, in this case the module is called Throwaway and thefile is called Throwaway.hs

Now, fire-up GHCi:

Now, make some changes to Throwaway.hs, WITHOUT EXITING GHCi:

Note

Do not exit GHCi

Next, reload these changes in GHCI:

That’s the most basic development workflow to follow:

  • Always start ghci with stackghci from within your project directory.This will ensure that the correct version of the compiler is used and that your GHCiis aware of the files in your project and the packages that your project depends on.
  • Load a file in GHCi via :l
  • Run a function
  • Change something in the function
  • Reload the file via :r (There is a difference in the behaviour of :l and :r that you may read about, if you are interested.)
  • Re-run the function

Make sure you are reading the correct docs¶

You will find yourself referring to API documentation very often. However, donot search for the package names on Google and start reading docs from there.You will end-up in a world of pain without even realising it. Google doesn’tknow which version of the package you’re using, and from a first-handexperience, things change a lot between versions.

So, here’s what you should do:

  • Check which LTS/resolver you are on – it’ll be the very first setting in yourproject’s stack.yaml file.
  • Go to the Stackage homepage and find thelisting page for your LTS/resolver. For example, here’s the listing forlts-9.17
  • Search for documentation and packages only from this page

Hackage vs Stackage & Cabal vs Stack¶

Strangely, Haskell has two widely used package repositories. Here is how theyare conceptually different and why both exist:

  • Hackage is the original package repository.This is where authors upload their packages to share them with the world.
  • Stackage pulls specific versions of specificpackages from Hackage and divides them into “sets” known as “snapshots”. Eachsnapshot contains only a single version of any package with a guarantee thatall the packages in a given snapshot will successfully build when included ina project. That is, you will not get a dependency hell when your projectdepends on 5 packages from the same Stackage snapshot. (If you go to asnapshot/LTS’ listing page you can verify that there is only one version ofeach package listed there. On the other hand, if you check the same package onHackage, you will see all versions of the package listed there).
  • Hackage has way more packages than Stackage, because, not every author addstheir package to the Stackage snapshot. This is probably because, every time anew LTS/snapshot is released, package-authors have to do some extra work tomaintain the “no dependeny-hell guaranteee”. However, most popular/importantpackages have already been added to Stackage, so you won’t be missing anypackages till you start doing advanced Haskell stuff.
  • The command-line tool called cabal does not know about Stackage andpulls packages directly from Hackage. Also, cabal+Hackage do not give this “nodependency-hell guarantee” that stack+Stackage works very hard to maintain.
  • The command-line tool called stack pulls packages from Stackage, bydefault. Having said that, it can pull packages from Hackage if aparticular package is not available in its snapshot (but this requires a fewextra lines of config).

Finally, lot of cabal/Hackage lovers hate stack/Stackage and vice-versa. If youare in the mood for some gossip you can search the interwebs and read someflamewars. One hopes, that at some point in the future, the best parts ofstack/Stackage and cabal/Hackage can be merged to build a unified, kickassbuild-tool. Till that day comes, just use Stack.

[1]Strictly speaking, your project and HIE need to be using the same version ofGHC, not necessarily the same LTS. However, for newbies, we recommendsimply using the same LTS because it is simpler and is guaranteed to work.

I've tested out the Haskell support of the top mainstream IDEs. Here'sa rundown of the current state of things.

As a dyed-in-the-wool Emacs hacker I've never used any of the morerecent mainstream IDEs, so I can probably offer an unbiased review ofthe support provided by each.

Note: I tried approaching it as a client would, or prospective Haskelluser, so for any manual intervention I had to do, I've used a tonethat indicates I'm not happy about having to do it, and anything thatdoesn't just work I just discard with little patience, as a realperson would and do today. Even if I know there are probably extramanual investigations that I could do knowing what I do about Haskell,a normal user wouldn't have that advantage.

IntelliJ IDEA

I installed it according to theinstructions on the IntelliJ IDEA web site. Idownloaded it to my Ubuntu laptop and installed it under/opt/intellij.

After installing IntelliJ, running it opens up a splash screen. Ratherthan starting a project, I went straight to the Configure->Pluginsbutton. In the plugins list, I chose IntelliJ-Haskell. After that,it was suggested that I restart, so I hit Restart IDE.

After restarting, on the splash screen I hit Create New Project andchose 'Haskell module'. At this point, it asked me to 'Select thestack binary'. I picked the one at /home/chris/.local/bin/stack, butsomeone else might find it under /usr/local/bin/stack. I hit Next.

Warning: there was a long wait after this step. I entered my projectname and proceeded. Opening the project workspace, it now claims 'busyinstalling hlint', which is a Haskell linting tool. It does this forvarious tools; hlint, hindent, stylish-haskell, hoogle. This tookeasily 15 minutes on my machine. Go make a cup of tea.

Finally, after finishing this process, it's ready to go. Here are somethings that I observed work correctly:

  1. Compile errors when changing code on the fly. Slow, but works. Youcan hit the 'Haskell Problems' tab to see the actual compilermessages.
  2. Hitting Ctrl and mousing over something, which is how you getmetadata in IDEA.
    1. Go to definition of library code.
    2. Go to definition of local code.
    3. Type info at point.
    4. Go to definition of local bindings.

I tested this out by opening the Stack codebase itself. Took about 10seconds on 'Indexing...' and then was ready.

There's a very picky set of steps to opening an existing projectproperly:

  1. You have to go 'Create project from existing source'
  2. Choose 'Create from external model'
  3. Choose the 'Haskell' SDK.

Then it should be good to go. Other ways didn't work for me and I gotstuck.

I've also seen that it's possible to define test and executabletargets quite reasonably.

IntelliJ has support to 'optimize imports' which will remove unneededones, which is very common when refactoring. I'd call that feature amust-have.

Visual Studio Code Haskell Setup

Overall, this IDE experience is not bad. As a Haskeller, I could getby if I had to use this.

Visual Studio Code

Visual Studio Haskell

I followed along with theinstall instructions for Linux. Idownloaded the .deb and ran sudo apt install ./<file>.deb.

I launched Visual Studio Code from the Ubuntu Activities menu. Itdisplays its full UI immediately, which was quite a lot faster thanIntelliJ, which takes about 5 seconds before displaying a UIwindow. Not that I care about start-up times:I use Emacs.

Visual Studio Code: Haskero

I went to the Customize section and then 'Tools and languages'. Uppops a menu for language choices (also quite quickly). I triedinstalling theHaskeroplugin, which, as I understand, is in spirit the same backend andfunctionality of IntelliJ-Haskell. It said 'This extension is enabledglobally'.

Assuming that it was ready to use, I looked for a way to create aproject. I didn't find one, so I opted to try opening an existingHaskell project: stack. I used File -> Open Workspace and chose therepository root directory.

VSC reports 'Unable to watch for file changes in this largeworkspace.' I followedthe link which had a hint to increase the limit. Iedited my sysctl.conf file as instructed to allow VSC to watch allthe files in my project.

Opening, for example, src/main/Main.hs, it opens quickly, butdoesn't appear to be doing any work like IntelliJ was. So I createsome obvious errors in the file to see whether anything works.

After waiting a while, it seems that I have to save the file to seeany kind of reaction from VSC. So I save the file and wait. I timedit:

After a full minute, I got in the Problems tab the problem.

It seems to be recompiling the whole project on every change. Thispretty much makes this plugin unusable. I don't think the author hastested this on a large project.

In its current state, I would not recommend Haskero. I uninstalled itand decided to look at others.

Visual Studio Code: Haskelly

I decided to try the other similar offering calledHaskelly. After a reload and re-opening Stack, I made an intentionalerror in src/main/Main.hs again and found that nothing happened. NoCPU usage by any process.

There weren't any indicators on the screen of anything failing towork. However, I had an intentional type error in my file that was notflagged up anywhere.

Another plugin that I would rate as not usable. I uninstalled it.

Visual Studio Code: Haskell Language Server

I installed the 'Haskell Language Server', which is supposed to be thelatest state of the art in language backends for Haskell.

Enabling it, I see the message:

hie executable missing, please make sure it is installed, seegithub.com/haskell/haskell-ide-engine.

Apparently I have to manually install something. Okay, sure, why not?

There'sa variety of installation methods. I'mnot sure which one will work. But I already have stack installed, soI try the install from source option:

This seems to clone the whole world and takes a while. Definitely aget a cup of tea moment. After that was done, I went to the directoryand ran this as per the instructions:

I am presented with a myriad of options:

I lookup the GHC version that's being used by the stack source code:

Haskell Language Server

Apparently the GHC version in use by stack is too old. At this point Istop and uninstall the plugin.

Visual Studio Code: ghcid

Visual Studio Haskell Stack

Studio

As a last resort, I tried one more plugin. But nothing seemed tohappen with this one either. So I uninstalled it.

SublimeText

Another popular editor is SublimeText. I installed it via the aptrepositorydocumented here. Idecided to try theSublimeHaskellplugin which seems popular.

Installing things in SublimeHaskell is a little arcane: you first haveto install 'Package Control'. I don't remember which menu item thiswas from. However, SublimeText installs this for you. Once that'sdone, you have to use Tools->Command Pallete, which is a kind ofquick-access tool that's apparently common in SublimeText. In thereyou have to literally type 'package control' and then go to 'PackageControl: Install Package' and hit RET. Then you can typeSublimeHaskell and hit RET. As an Emacs user, I'm not afraid of arcaneUIs.

After installing, it pops up a dialog with:

No usable backends (hsdev, ghc-mod) found in PATH. [..] Please checkor update your SublimeHaskell user settings or install hsdev orghc-mod.

It displays a tab with the README from SublimeHaskell and I assumethis is where SublimeText is done helping me.

Okay, let's install hsdev!

I had to create a file hsdev.yaml:

And then run

Visual Studio Code Haskell

That took 5 minutes but succeeded. There isn't a 'next button' onSublimeText, so I just restarted it. I did File->Open Folder andopened the stack directory and the Main.hs file.

I see 'Inspecting stack' which indicates that it's actually doingsomething. However, after that finishes, I still don't see any errormessages for my type error. Finally, I make a new change and save thefile, and a little messages area pops up below.

And so one for pretty much every library module in the project.

At this point I can't find a menu or anything else to help meconfigure packages or anything related.

Visual

Visual Studio Code Haskell Debugger

At this point it seems like SublimeText is almost workable. The consappear to be the manual install process, and the complete lack ofguidance in the user experience. I'm afraid I can't recommend this toclients either at the moment.

Summary

The story for Visual Studio Code is pretty dire. I did not find astraight-forward install or reliably working IDE for Haskell in VisualStudio Code, and therefore, at the moment, cannot recommend it to ourclients. Perhaps a little work done on Haskero could bring it up to par.

SublimeText falls over at the finish line. It seems like with a littlework on the user experience could bring this up to par.

IntelliJ IDEA however worked quite well with little to no interventionrequired. So I would indeed recommend it to clients.

Read more aboutHaskell development, tooling and businesson our blog. Email us to setup a freeconsultation with our engineering team to discuss editor options.

Visual

Do you like this blog post and need help with DevOps, Rust or functional programming? Contact us.