UP | HOME

Notes on Writing a Haskell Program (or Service)

Table of Contents

1 Overview

Now that I've finally finished Learn a Haskell, I'm ready to write some real functionality in Haskell, which requires all sort of setup and configuration. These are the notes I'll take as I go along (I hope).

The software I hope to write is a function that will return the Delaunay triangulation of a set of 2-D points. It currently lives at https://github.com/JohnL4/Delaunay.

The "or Service" part of this document refers to the fact that functionality written in Haskell might not be an entire standalone program, but may instead be a "service" provided to software written in other languages (e.g., Java), or might use other software to provide services. How that might be implemented is unknown at the moment, but probably requires some sort of FFI from/to Haskell.

See also:

2 Build System

Decided to use Stack, not Cabal. Stack seems to be based on Cabal and is supposed to guarantee reproducible builds, with respect to package versions. (Supposedly, Cabal can, too, but that functionality seems to be more of a bolt-on.)

See:

2.1 Mac install

stack --version
stack upgrade
brew update
brew upgrade haskell-stack

2.2 Linux install

Just follow the docs, they worked for me. :)

3 New Project

Run:

stack new project-name

In the directory that will contain the directory that contains your project (i.e., in the directory above the (not-yet-existing) directory that will correspond to your project).

You can probably either accept the defaults or figure out what the right values are.

Consider creating your project with a lower-case name and then (if you want) capitalizing the directory name (and git repo?) afterward. Lower-case project name will generate a nicer package.yaml.

3.2 Category

For category, pick from one of the top-level Hackage categories, which you can see at https://hackage.haskell.org/packages/.

(For Delaunay, I'm picking "Graphics".)

3.3 Library Structure

stack, by default, creates one library, named "Lib". If you want to give it a more creative name (e.g., "Graphics.Delaunay"), don't forget that module names with dots are expected to correspond to directory structures in which each dot-separated component is a directory (except the last, which is a file). So, the code defining module Graphics.Delaunay must reside in Graphics/Delaunay.hs.

4 Testing

There seems to be a fair amount of discussion on this, but I like hspec right now.

You can pop over to Unit testing with Hspec in beginners-guide-to-haskell.org to steal the syntax, but here's how to integrate with the stack build system.

4.1 Add hspec to stack config

(There's probably something similar for cabal, if you chose that.)

Under "tests", add "hspec" to the dependencies:

dependencies:
- delaunay
- hspec

Created: 2023-08-06 Sun 14:23

Emacs 27.2 (Org mode 9.4.4)

Validate