Haskell CPP macros

Posted on September 13, 2014

Listing these here mostly because whenever I need to use them I seem to be unable to find them and have to dig for them in previous code I wrote.

Checking for minimal package versions

This relies on macros generated by Cabal. Example:

#if MIN_VERSION_process(1,2,0)
    -- something
#else
    -- something else
#endif

Read more

Dealing with Asynchronous Exceptions during Resource Acquisition

Posted on August 28, 2014

Consider the following code: we open a socket, compute with it, and finally close the socket again. The computation happens inside an exception handler (try), so even when an exception happens we still close the socket:

example1 :: (Socket -> IO a) -> IO a 
example1 compute = do -- WRONG
  s <- openSocket 
  r <- try $ compute s
  closeSocket s
  case r of
    Left ex -> throwIO (ex :: SomeException)
    Right a -> return a

Although this code correctly deals with synchronous exceptions–exceptions that are the direct result of the execution of the program–it does not deal correctly with asynchronous exceptions–exceptions that are raised as the result of an external event, such as a signal from another thread.

Read more at well-typed.com

Debugging Haskell at assembly level
by scripting lldb in Python

Posted on August 1, 2014

Haskell programmers tend to spend far less time with debuggers than programmers in other languages. Partly this is because for pure code debuggers are of limited value anyway, and Haskell’s type system is so strong that a lot of bugs are caught at compile time rather than at runtime. Moreover, Haskell is a managed language – like Java, say – and errors are turned into exceptions. Unlike in unmanaged languages such as C “true” runtime errors such as segmentation faults almost never happen.

Read more at well-typed.com

Understanding the RealWorld

Posted on June 12, 2014

In my previous blog post Understanding the Stack I mentioned that “RealWorld tokens disappear” in the generated code. This is not entirely accurate. Simon Marlow gives a very brief explanation of this on the old glasgow-haskell-users mailing list. In this blog post we will explore this in more depth and hopefully come to a better understanding of the RealWorld, though perhaps not of the real world.

Read more at well-typed.com

Understanding the Stack

Posted on May 21, 2014

One of our clients sent us a bug report, which consisted of a 700 line Haskell program with a complaint that “it deadlocks”. After studying the code I concluded that, for the sake of the bug report, it could be summarized as

print a bunch of stuff, then crash with a stack overflow

So I wanted to replace the original code with code that did just that: print a bunch of stuff, then crash with a stack overflow. No big deal:

Read more at well-typed.com

Haskell (including GTK) on Mavericks

Posted on April 27, 2014

Installing GTK

Although the GTK libraries can be installed through fink, macports, or brew, I’ve never had much luck with any of them. I recently tried installing them through brew and although they appeared to install ok, as soon as I tried to run a Haskell GTK application it segfaulted with a null pointer exception somewhere in libcairo. I therefore prefer to install from source. This is a fairly painless process; some hints are in my Comprehensive Haskell Sandboxes post. You can follow those instructions or just follow the instructions on the GTK+ website. The only important thing is that you need set set LDFLAGS so that it can find -lpython:

export LDFLAGS=-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config

Read more

Pointwise Lenses

Posted on April 24, 2014

Lenses are a current hot topic in the Haskell community, with a bunch of packages providing implementations (data-accessor, fclabels, lens, amongst others). Although we will recall definitions, this post is not meant as an introduction to lenses. If you have not worked with lenses before, the talk from Simon Peyton Jones or the blog post by Sebastiaan Visser about fclabels are good starting points.

In this blog post we will propose a generalization to the lens representation used in fclabels and in many other packages (with various minor variations); we will consider the relation to the representation used in lens in a separate section.

Read more at well-typed.com

Performance profiling with ghc-events-analyze

Posted on February 12, 2014

ghc-events-analyze is a new simple Haskell profiling tool that uses GHC’s eventlog system. It helps with some profiling use cases that are not covered by the existing GHC profiling modes or tools. It has two major features:

Read more at well-typed.com

sendmail (postfix) on OSX

Posted on October 9, 2013

Scenario: you want to be able to run sendmail from the command line and have it deliver mail to mail servers other than localhost. It’s not particularly difficult once you know what to do but it was a bit of a pain to figure out. So, mostly for my own benefit:

  1. Fix configuration and permissions
  2. Set up SASL authentication for your favourite SMTP server
  3. Tell postfix which users are local (none)

The darker corners of throwTo

Posted on June 11, 2013

We will assume

import System.IO.Unsafe (unsafePerformIO, unsafeInterleaveIO)
import System.IO.Error (catchIOError)
import Control.Concurrent (throwTo, myThreadId, threadDelay)
import Control.Concurrent.Async (async, cancel, wait)
import qualified Control.Exception as Ex

in the remainder of this post.

Whatever work the target thread was doing when the exception was raised is not lost: the computation is suspended until required by another thread.

We can observe this directly when using unsafePerformIO :

Read more

Brief Intro to Quasi-Quotation (Show me the types already)

Posted on May 9, 2013

This posts serves as a brief introduction to quasi-quotation in Haskell, because I couldn’t make much sense of the existing documentation, which gets bogged down in unnecessary details about writing parsers, the use of SYB, etc. Also, although quasi-quotation borrows syntax from Template Haskell, the TH expression [|e|] and the QQ [qq|e|] are false friends at best.

Read more

Comprehensive Haskell Sandboxes

Posted on February 10, 2013

Serious Haskell developers will want to test their packages against different versions of ghc, different versions of libraries, etc. It is therefore useful to have multiple Hackage development environments, or sandboxes. A comprensive sandbox contains:

Moreover, good sandboxes are

  1. isolated from each other: only the active sandbox can be modified
  2. relocatable: it should be possible to copy the sandbox ~/env/ghc762 to ~/env/client1 to use as the basis for a new sandbox, or to copy the entire set of sandboxes (~/env) from one machine to another (suitably similar) machine (this means that paths in config files or baked into executables should never mention the name of the sandbox)
  3. self-contained: there should be no need for a system wide install of ghc
  4. transparent: we should be able to use the standard tools (ghc, cabal) as they are

There are existing tools for Haskell sandboxes (such as cabal-dev or hsenv), but none satisfy all of the above requirements (to the best of my knowledge). However, it is easy enough to set up our own.

Read more

Performance problems with -threaded?

Posted on February 6, 2013

Consider the following two implementations of a very simple ping/pong server and client, one written in C and one written in Haskell:

Neither of them has been particularly optimized, that’s not the point of this post. The point of this post will become evident when we measure the latency for the following three programs:

  1. The C program compiled with -O2
  2. The Haskell program compiled with -O2
  3. The Haskell program compiled with -O2 and -threaded

Read more

ThreadScope 0.2.2 binary for Mac OS X 10.8.2

Posted on January 24, 2013

This is an application bundle which should hopefully Just Work without any additional libraries (in particular, you should not have to install any GTK libraries).

threadscope-0.2.2.dmg

Communication Patterns in Cloud Haskell (Part 4)

Posted on October 15, 2012

K-Means

In Part 3 of this series we showed how to write a simple distributed implementation of Map-Reduce using Cloud Haskell. In this final part of the series we will explain the K-Means algorithm and show how it can be implemented in terms of Map-Reduce.

K-Means is an algorithm to partition a set of points into n clusters. The algorithm iterates the following two steps for a fixed number of times (or until convergence):

Read more at well-typed.com

Communication Patterns in Cloud Haskell (Part 3)

Posted on October 12, 2012

Map-Reduce

In Part 1 and Part 2 of this series we described a number of ways in which we might compute the number of prime factors of a set of numbers in a distributed manner. Abstractly, these examples can be described as

a problem (computing the number of prime factors of 100 natural numbers) is split into subproblems (factorizing each number), those subproblems are solved by slave nodes, and the partial results are then combined (summing the number of factors)

Read more at well-typed.com

Communication Patterns in Cloud Haskell (Part 2)

Posted on October 8, 2012

Performance

In Part 1 of this series we introduced a number of basic Cloud Haskell communication patterns. All these patterns will work fine for modest numbers of work items; in this blog post we’re going to look at the performance and memory use for much larger numbers of work items, so we can see how each pattern scales up. We will see that scalability improves as we move from master-slave to work-pushing to work-stealing, and that many further improvements can still be made. More importantly, we hope to help you to predict runtime behaviour from your code, so that you can write more scalable and high-performance code.

Read more at well-typed.com

Communication Patterns in Cloud Haskell (Part 1)

Posted on October 5, 2012

Master-Slave, Work-Stealing and Work-Pushing

In this series (2, 3, 4) of blog posts we will describe a number of basic communication patterns in Cloud Haskell. We don’t assume much familiarity with Cloud Haskell, but it will probably be useful to be familiar with the basics; Towards Haskell in the Cloud (Epstein, Black and Peyton Jones, Haskell Symposium 2011) is a good starting point. We will start simple but we will finish with some more advanced techniques.

Read more at well-typed.com

DLX/MIPS tutorial

Posted on May 19, 2005

TODO