Matthew Brecknell

Pattern-matching dependent types in Coq

| Comments

Pattern matching with dependent types in Coq can be awkward, while equivalent programs in Agda might be straightforward and elegant. Yet despite the awkwardness, there may still be reasons to choose Coq for your next dependently-typed development, for example if you want a tactic language to develop domain-specific proof-search procedures.

In this talk, we show how to match in Coq, using techniques described by Adam Chlipala in his book, Certified Programming with Dependent Types.

We first review what it means to pattern-match on inductive families, contrasting Coq with Agda, and examine what it is about Coq that complicates pattern matching. Using a simple running example, we’ll show how to use Coq match annotations to eliminate nonsense cases, and the convoy pattern for refining the types of things already in scope. Finally, we’ll show that by equipping an inductive family with some well-chosen combinators, it is often possible to regain some semblance of elegance.

This is a recording of a practice run for a talk at YOW! Lambda Jam. You can download the video as MP4 or WebM, and slides as PDF. There is some code on GitHub. Also on YouTube.

Difference lists

| Comments

This post is an extended version of a lightning talk I gave at BFPG. It introduces the difference list, a simple trick for improving the performance of certain kinds of list-building functions in Haskell, and goes on to explore the connections to monoids and folds.

The first half is aimed at Haskell novices, while the latter parts might be interesting to intermediates.

C++11 universal reference pop-quiz

| Comments

Here’s a little exercise for anyone who, like me, recently came across Scott Meyerswork on universal references in C++11: Is the following program well formed? Does it have well-defined behaviour? If not, why not? If so, what is the value returned by main()? Why? References, please!

template <typename R>
struct wat;

template <typename T>
struct wat <T&> {
  static int value(T & t) { return t * 1; }

template <typename T>
struct wat <T&&> {
  static int value(T && t) { return t * 3; }

template <typename T>
struct ref {
  ref(T && t) : t(static_cast<T&&>(t)) {}
  int value() { return wat<T&&>::value(static_cast<T&&>(t)); }
  T && t;

template <typename T>
ref<T> uni(T && t) {
  return ref<T>(static_cast<T&&>(t));

int main() {
  int i(3);
  return uni(i).value() + uni(13).value();

I have written the program so that it needs no #include directives, and therefore you can be sure there is not a single typedef, decltype or auto specifier anywhere in or out of sight. That means there’s only one way that so-called universal references can arise.

However, you might find one or two other little surprises. Oh, and Clang 3.3 and GCC 4.8.1 don’t even agree on this program, so there’s not much point in cheating! I know where I’m putting my money, though…

B-trees with GADTs

| Comments

I interactively build a simple B-tree data structure in Haskell, implementing insertion and deletion, using a GADT to ensure that I maintain the structural invariant.

You can download the video as MP4 or WebM, as well as the slides I used for my YLJ13 talk, in PDF or Keynote. There’s some code on GitHub. Sorry, no subtitles yet, but you can read along with this script. Also on YouTube.

Hole-driven Haskell

| Comments

A demonstration of a technique for using types to guide the construction of Haskell programs, based on natural deduction. Includes some tricks for getting help from GHC.

Thanks to Tony Morris, Greg Davis and Clinton Freeman for giving me the idea. Thanks to everyone else for not giving me too much shit about my noisy hole.

You can download the video as MP4 with embedded subtitles, WebM with separate subtitles, or find it on YouTube.