hwp-book/2_Install.org
Yann Esposito (Yogsototh) 44e1b7805c
initial commit
2018-06-20 22:11:04 +02:00

6.4 KiB

Haskell for the working programmer

TO-CLEAN Install a dev environment (about 30 minutes)

Installing a dev environment should hopefully be the most boring part of this book. But this is a necessary price to pay to really get why Haskell is considered so great by people using it.

TO-CLEAN Working environment

A thing to note is the distinction between learning a language for personal interrest for some personal project and learning with the goal to achieve a "product" with some hard deadline.

So for example, it can be nice to understand the language by playing inside a REPL. We won't use that much in this book as the goal is not to really gain a deeper knowledge but perhaps to be able to use the language.

The problem I try to solve in this book is to make you a professional user of Haskell more than a contributor to Haskell. While I encourage everybody to gain deeper understanding on the internals of Haskell this is not the primary goal of this book.

What I mean by professional user is that you should have the following features at your disposal:

  • DCVS
  • Generated documentation
  • Tests (Unit tests, Generative tests, Integration tests, etc…)
  • Benchmark
  • Continuous Integration
  • Continuous Deployment

Choices:

  • Raw: get GHC and cabal exectuable and work with that. Too long and manual
  • Nix: this is really great because it's like a super make that can deal with external dependencies. Certainly the best tool in the long term.
  • Stack: fast to install focused on being user friendly. Has a lot of easy to use features like:

    • integration with docker that will make it easy to cross-compile, deploy.
    • integration with nix
    • easy to deal with many private repositories
    • good professional starting templates

TO-CLEAN Stack

I recommend stack. But there are many different method to install Haskell. Stack should be simple and straight to the point.

If thing haven't changed since the book was written it could be installed with:

curl -sSL https://get.haskellstack.org/ | sh

TO-CLEAN git

You should have git installed.

TO-CLEAN Stack template

Before starting to write your first line of code. Let's create a project with a sane and modern file organisation.

I made a stack templates largely inspired by tasty-travis template. It will provide a bootstrap for organizing your application with tests, benchmarks and continuous integration.

This template provide a file organisation for your projects.

Mainly do jump into programmin you could theoretically just download the binary of the main Haskell compiler GHC to your compiler and compile each file with ghc myfile.hs. But let's face it. It's not suitable for real project which need more informations about it.

So let's start with a sane professional organisation for your files.

stack new myproject https://git.io/vbpej

After that, this should generate a new `myproject` directory with the following files:

> tree
.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── Setup.hs
├── myproject.cabal
├── package.yaml
├── src
│   └── Lib.hs
├── src-benchmark
│   └── Main.hs
├── src-doctest
│   └── Main.hs
├── src-exe
│   └── Main.hs
├── src-test
│   └── Main.hs
├── stack.yaml
└── tutorial.md

5 directories, 13 files

Most of your source code should be in the src directory. Generally src-exe should be a minimal code that could handle the main function to start your application. We'll talk about other part later in the book but most other file should be quite straightforward.

TO-CLEAN Editor

You should check any of the supported editor here:

https://github.com/rainbyte/haskell-ide-chart#the-chart-with-a-link-to-each-plug-in

I personnaly use spacemacs with the haskell layer because it comes with battery included. If you're not used to vim keybindings I believe it is easy to switch to more classical editor keybindings easily.

Even if I don't have a strong opinion on the editor you should choose. It should at least be easy to support the Haskell tooling, like intero or ghc-mod. Because it's one of the best part of Haskell.

For example without any configuration I have the following features:

  • I see errors, warn and even code hints while I'm typing my code.
  • very good code completion
  • auto styling of my source code and be able to change the style of my entire buffer
  • be able to get the type of the expression under my cursor
  • be able to add the type of a top level declaration
  • be able to launch a repl easily loading the current code of the file I'm currently editing

And many other nice features.

Note that in the past I had some problem with ghc-mod during upgrades while intero was mostly a no problem story.

It is also useful to have hoogle and hayoo, which are search engine focused on Haskell.

TO-CLEAN Spacemacs

So if you want to choose spacemacs:

  1. Install a recent emacs
  2. git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d
  3. Launch emacs
  4. Edit your ~/.spacemacs file to add to the layer list:
 haskell
 (auto-completion :variables
                     auto-completion-enable-help-tooltip t
                     auto-completion-enable-short-usage t)

If you're not used to vim keybinding and it is too much to handle for you. I think you can change the value of dotspacemacs-editing-style from 'vim to 'hybrid or 'emacs in the .spacemacs file.

It should be good now.

TO-CLEAN Conclusion

First you can congratulate yourself to have installed all the prerequiste to have a great working development environment.

I know it was already a lot of boring tasks to perform before being able to write any line of code. But I promise it will be worth it.

By starting with this template, you won't use the classic prelude. It's quite a strong opinionated move. Because many classic function will be overwritten by safer/more generic one.

So be prepared that the actual learning route is jumping other classical learning steps you can find in other learning resources. Don't worry I'll do my best to make the jump as natural as possible.