{"id":12698,"date":"2015-10-14T22:05:00","date_gmt":"2015-10-14T21:05:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=4f192f07f0d62a0077402bfcb51874ac"},"modified":"2023-04-27T11:32:30","modified_gmt":"2023-04-27T09:32:30","slug":"getting-started-with-haskell-stack-and-spacemacs","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/10\/14\/getting-started-with-haskell-stack-and-spacemacs\/","title":{"rendered":"Getting started with Haskell, stack and spacemacs"},"content":{"rendered":"<div style=\"text-align: justify\">\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a style=\"margin-left: 1em;margin-right: 1em\" href=\"http:\/\/3.bp.blogspot.com\/-WPSbxnWpNxg\/Vh7C1AHvgsI\/AAAAAAAAAS0\/2FJX1FKJ0iI\/s1600\/HaskellLogoStyPreview-1.png\"><img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/-WPSbxnWpNxg\/Vh7C1AHvgsI\/AAAAAAAAAS0\/2FJX1FKJ0iI\/s1600\/HaskellLogoStyPreview-1.png\" border=\"0\" \/><\/a><\/div>\n<p>It has been very long time since my last blog post. During this period I have become big enthusiast of functional programming, especially using Haskell language. In this and next posts I am going to show that Haskell can be very pleasant to use and with proper tools we are able to develop applications without unnecessary burden.<\/p>\n<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\">Recently, many useful tools and editors emerged and they are really easy and convenient to use. In this post I intend to present toolchain that I am using in my everyday Haskell programming.<\/span><\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">This post is not an introduction to Haskell language. It is meant to describe how to setup Haskell with stack build tool and spacemacs as an editor. I am also planning to write a post about Haskell basics and its usage in my little project in series of the next posts.<\/span><br \/>\n<b style=\"font-family: inherit\"><br \/>\n<\/b><b style=\"font-family: inherit\">The only necessary prerequisite is having the most recent version of Emacs installed on your system.<\/b><\/div>\n<h4 id=\"new-project-build-management-tool-stack\"><span style=\"font-family: inherit\">New project build\/management tool &#8211; stack<\/span><\/h4>\n<div style=\"text-align: justify\">Managing dependencies and build process is always a gruesome task and there are many tools to ease this work. In Haskell most popular dependency management tool is <b>cabal<\/b>. It is based on Hackage repository (<a href=\"https:\/\/hackage.haskell.org\/\">https:\/\/hackage.haskell.org<\/a>).<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">One of the desired features of build tools are <b>reproducible builds<\/b>. We would like to build project in new environment or on the new developer\u2019s machine and have the same outcome in every situation. This would require same compiler version, same libraries etc.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Lately, new tool came out &#8211; <b>stack<\/b> (<a href=\"https:\/\/github.com\/commercialhaskell\/stack\">https:\/\/github.com\/commercialhaskell\/stack<\/a>). It is aimed at reproducible builds and simple project management. Stack takes care of proper configuration of your project environment.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Stack achieves reproducible builds by using curated snapshot packages managed by special versioned <b>resolvers<\/b>. It uses cabal as a package manager. Packages are grouped into resolvers. There are two types of resolvers: LTS (long term support) and nightly. The latter contains packages in fresh version but there is also a drawback &#8211; potential instability. On the other hand, LTS resolvers contain fixed version of packages which are tested and should not cause any problems. If you are not in need of using latest packages version, LTS resolver should entirely satisfy your project needs.<br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">What is more, stack can also download and setup locally Haskell compiler in version required by your project.<\/span><\/div>\n<h4 id=\"stack-in-action\"><span style=\"font-family: inherit\">stack in action<\/span><\/h4>\n<div>\n<div style=\"text-align: justify\"><span style=\"font-family: inherit\">Using stack to create new project is really easy. After installing it on our machine (description of installation is included in documentation on project&#8217;s GitHub page I linked above), all we need to do to create new project is execute below steps in our terminal:<\/span><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">stack new hello-haskell\r\ncd hello-haskell\r\nstack setup\r\nstack build\r\nstack exec hello-haskell\r\n<\/pre>\n<\/div>\n<\/div>\n<div style=\"text-align: justify\">These commands create new project with name hello-haskell. <i>stack setup<\/i> initialises environment, install compiler (if it will be required) and necessary libraries for project. <i>stack build<\/i> builds and compiles project. <i>stack exec<\/i> \u2026 executes executable program built earlier.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">\n<p><span style=\"font-family: inherit\"><span style=\"font-family: inherit\">If you would like to play around with your project&#8217;s code you should type <i>stack ghci<\/i> in your terminal &#8211; this will launch Haskell interactive console &#8211; ghci &#8211; in version specified in project configuration.<\/span><\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">Another stack command worth mentioning is <i>stack test<\/i> which executes test suites declared in test\/ directory.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">Dependencies and project settings are placed in hello-haskell.cabal file. It is standard cabal configuration file where we can add desired libraries, set project version, licence, link to the repository and so on. I suggest reading some cabal documentation if you have any doubts but in my opinion this file is very easy and straightforward to edit.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">Settings specific for stack are placed in <b>stack.yaml<\/b>\u00a0file. Most important option is resolver &#8211; which influences version of GHC compiler and libraries your project will be using.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">There is one thing you might encounter while setting up project dependencies. What if you need library that is not present in any of stack resolvers? Well, then we must go to <\/span><b style=\"font-family: inherit\">stack.yaml <\/b><span style=\"font-family: inherit\">file and edit or add section:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">extra-deps:\r\n- Vec-1.0.5\r\n<\/pre>\n<p><span style=\"font-family: inherit\">With this information stack will download and build desired package from hackage repositories. In my case I needed Vec library so I added it on a list with full name containing version number.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\">All details and gotchas are described in stack\u2019s Wiki on GitHub. Be sure to check it out frequently as stack is still very young tool and it can change quite often. Documentation is very strong point of stack as it describes very well many aspects of its usages.<\/span><\/span><\/div>\n<p><span style=\"font-family: inherit\">\u00a0<\/span><\/p>\n<h4 id=\"powerful-editor-in-new-edition-spacemacs\"><span style=\"font-family: inherit\">Powerful editor in new edition &#8211; spacemacs<\/span><\/h4>\n<div style=\"text-align: justify\">I have spent a lot of time searching for editor that is easy to use with Haskell and that integrates well with its tools like REPL. I\u2019ve been working with Sublime Text for some time as it is integrated quite well with Haskell when using SublimeHaskell package. However, recently I\u2019ve discovered spacemacs project.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\"><span style=\"font-family: inherit\"><b style=\"font-family: inherit;font-weight: bold\">spacemacs<\/b><span style=\"font-family: inherit;font-weight: bold\"> (<\/span><a style=\"font-family: inherit;font-weight: bold\" href=\"https:\/\/github.com\/syl20bnr\/spacemacs\">https:\/\/github.com\/syl20bnr\/spacemacs<\/a><span style=\"font-family: inherit\"><b>)<\/b> is a easy-to-use kit for Emacs focused on ergonomics. What is great about it is that it embraces Evil mode of Emacs which mimics Vim-style editing and document navigation. With this feature spacemacs is really straightforward for users which know Vim. It is also possible to mix Vim and Emacs style in the same time.<\/span><\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">In my opinion, it is really great feature as we can use this editor in the way we like more or is more convenient to us. Whether we are Vim-lovers or Emacs-fans or we want to mix them both &#8211; spacemacs allows to work in whatever style we like. I personally use mostly Vim-like mode with only few of original Emacs commands and with spacemacs shortcuts for many actions.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">spacemacs is based on <\/span><b style=\"font-family: inherit\">layers<\/b><span style=\"font-family: inherit\"> which add additional functionalities to editor. It can enrich our development environment with syntax completion, git integration, code completion and integration with build tools for many languages.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">One of these layers is haskell layer. It supports this language quite well with syntax checking, code suggestions, built-in REPL and code templates for common patterns.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">I refer to the official documentation for detailed installation instruction on various platform. After we are ready and spacemacs is on our disk, we can proceed.<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">Entire spacemacs configuration is placed in .spacemacs file in your home directory. This file is written in Lisp-like language and contains many options to change or add. Here is my current .spacemacs file on what this post section is based:\u00a0<\/span><br \/>\n<a style=\"font-family: inherit\" href=\"https:\/\/gist.github.com\/rafalnowak\/202aba0ee7986515345b\">https:\/\/gist.github.com\/rafalnowak\/202aba0ee7986515345b<\/a><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">In dotspacemacs-configuration-layers we need to add haskell layer (I also recommend setting auto-completion and syntax-checking layers as well). In order to get layer to work properly, we need to install some additional packages:<\/span><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">stack install stylish-haskell hlint hasktags<\/pre>\n<\/div>\n<div style=\"text-align: justify\"><span style=\"font-family: inherit\">Next step is adding these two settings to .spacemacs just after text <i>;; User initialization goes here<\/i>:<\/span><\/div>\n<div style=\"text-align: justify\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">(add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)\r\n(add-to-list 'exec-path \"~\/.local\/bin\/\")\r\n<\/pre>\n<\/div>\n<div style=\"font-family: inherit;text-align: justify\">\n<p><span style=\"font-family: inherit\">It makes spacemacs aware of Haskell indentation style and adds binaries installed by stack to path. It is important as we want to make our editor able to run Haskell tools.\u00a0<\/span><\/p>\n<p><span style=\"font-family: inherit\">Full description, as well as platform specific problems, are listed in Haskell layer documentation: <\/span><a style=\"font-family: inherit\" href=\"https:\/\/github.com\/syl20bnr\/spacemacs\/tree\/master\/layers\/%2Blang\/haskell\">https:\/\/github.com\/syl20bnr\/spacemacs\/tree\/master\/layers\/%2Blang\/haskell<\/a><span style=\"font-family: inherit\"> There is also a list of useful shortcuts used by this layer.<\/span><\/p>\n<div style=\"text-align: justify\">\n<p><span style=\"font-family: inherit\"><b>One essential note: if you wish to use spacemacs with ghc-mod integration, you will need to install ghc-mod at least in version 5.4.0.0. Previous versions do not work properly with Haskell layer a<span style=\"font-family: inherit\">nd stack<\/span>.<\/b><span style=\"font-family: inherit\"> T<span style=\"font-family: inherit\">o install <b>ghc-mod<\/b> in this version<span style=\"font-family: inherit\"> you must<span style=\"font-family: inherit\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\"> add <i>cabal-helper-0.6.1.0<\/i> to you<span style=\"font-family: inherit\">r extra-deps section in <b>stack.yaml<\/b><\/span> and run\u00a0<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">stack install ghc-mod\r\n<\/pre>\n<\/div>\n<div style=\"text-align: justify\">\n<p><span style=\"font-family: inherit\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\"><span style=\"font-family: inherit\">Which should proceed now wi<span style=\"font-family: inherit\">thou<span style=\"font-family: inherit\">t problems<\/span>.<\/span> <\/span><\/span><\/span><\/span><\/span><\/span>\u00a0<\/span><\/p>\n<p><span style=\"font-family: inherit\">After this configuration we are ready to use all power of Haskell and stack in our projects. We will also have solid support from editor. If you have followed steps above, you will see that spacemacs is colouring Haskell syntax, checking its correctness and giving you code completion tips. There is also interactive console for Haskell available under SPC m s s keys combination which makes quick testing of new functions possible.\u00a0<\/span><br \/>\n<span style=\"font-family: inherit\"><br \/>\n<\/span><span style=\"font-family: inherit\">Unfortunately, there are some disadvantages of spacemacs. For me, the biggest drawdown is its responsivity. Sometimes during code completion or syntax checking it can hang application for a second or less.<\/span><\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h4 id=\"summary\"><span style=\"font-family: inherit\">Summary<\/span><\/h4>\n<div style=\"text-align: justify\">\n<p>As we could see, Haskell with stack and spacemacs is really powerful yet still simple to use. With <b>stack<\/b> we can achieve reproducible builds with specific compiler and libraries versions as well as easy project management. <b>spacemacs<\/b> allows us to create code quickly with support for Haskell syntax, build tools and code completion.<\/p>\n<p>In my next post I am going to describe my experiences with my first bigger Haskell project &#8211; functional ray tracer I have been working on recently &#8211;<a href=\"https:\/\/github.com\/rafalnowak\/RaytracaH\"> https:\/\/github.com\/rafalnowak\/RaytracaH<\/a><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"It has been very long time since my last blog post. During this period I have become big&hellip;\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[114,553],"class_list":{"0":"post-12698","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-functional-programming","8":"tag-haskell"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12698"}],"version-history":[{"count":8,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12698\/revisions"}],"predecessor-version":[{"id":15674,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12698\/revisions\/15674"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}