Once I was playing with and working on a Haskell clone of the old Gradius arcade games, Monadius. Most of my changes were not particularly interesting (cleaning up, Cabalizing, fixing warnings, switching all Integer to Int and so on), but in its Demo.hs, I found an interesting solution to a problem, and it seems like a good small example of Haskell abstraction.

Examples

One of the problems with language advocacy is that it’s hard to have good examples, since someone who is writing an essay probably will only come up with trivial ones, and someone dealing with meaty problems isn’t thinking about advocacy but how to solve their problem. I hope this example will be a little more substantial than the one-liners examples of closures or partial application.

Level data in Monadius

Suppose we have levels which are specified by a pair of numbers and then a long list of numbers, often very repetitious. Perhaps a particular level might be represented this way:

But this representation, while certainly more understandable, is still very repetitious. It will take even more space to write down.

We could auto-derive the Read and Show typeclasses for Geometry and Enemy datatypes, and then we could store level1, level2 etc. as some sort of “levelinformation.dat” file, and read the level information in at runtime.

But that makes installation and running more difficult1 still quite possible, , and it doesn’t address the core issue: the information is written in a way that is so ungainly that it’s next to impossible to write or even modify!

The solution

We need some way of expressing this more concisely, of, in short, compressing it. In this vein of thought, our first observation should be that we do not need to resort to fancy compression libraries or anything; there is an obvious way to compress it - the entire thing is a series of repeated numbers.

We should be able to replace the lengthy enumeration of fragments like [0,0,0,0,0,0,0,0,0,0,0,0] with something simpler. For example, the number of repetitions, and what is to be repeated: (12,0).

This run-length encoding is shorter and easier to modify. It is possible that there may be a performance benefit here, as we’ve gotten rid of a large constant that would have to be defined in the program itself and instead replaced it with a shorter function which evaluates to the same thing. (If we’re only on level 1, we don’t need to carry around the expanded version of the other levels, and when we go to level 2, level 1 will be garbage-collected.)

Writing it

So, what is the type of our decompressing function? Well, we need to turn a (Int,Int) into a [Int]; even better, we want to turn a whole list of (Int,Int)s into a single list of Ints. Thus, our end goal is going to be a function of this type:

rleDecode :: [(Int,Int)] -> [Int]

Let us tackle the single tuple example first. The second entry defines what we need, and the first entry defines how many we need, and that’s our type right there:

rleRecursiveDecode :: (Int, a) -> [a]

We could write a primitive recursive function that takes the parameter, decreases the counter by 1, and cons on a copy of the second entry. It could look like this:

But this really is not the best way to go. It is not necessarily easy to follow, and if there is one thing I have learned about Haskell programming, it is that the most obvious approach (in this case, primitive recursion) may not be the best way to go.

This is code that could have as well been written in Scheme or something. It is complicated because we are trying to ensure that we generate an item for the list only at the exact moment we need to add it into the list; we are programming as if our language is strict, in other words. So, what is the lazy way of doing things? Why do we like laziness to begin with?

One of the key rationale for lazy evaluation is that it promotes separation of concerns - one function doesn’t need to know when or how much has been done by another function. In this case, what concerns ought to be separated? Well, our desired list has 2 properties: its length, and the single repeated item.

Since we can create infinite lists, there is no need for the length to be set at the same time as the repeated-item is being generated. We can create an infinite list containing the item, and we demand as many as we need. Lazy evaluation means our infinite list doesn’t blow up. Simple enough!

The generation is too easy for words:

-- Define an infinite list of our item.duplicate :: a -> [a]
duplicate a = a : duplicate a

But what’s our demand function? A little thinking and we know that we have a list, a number, and we get back a list. Int -> [a] -> [a] describes a few functions, the second of which turns out to be what we want: take.

Now, duplicate is a simple enough function to define, but another type-search will show that a -> [a] is already defined in the standard libraries - repeat.

So we scrap duplicate:

rleLazyDecode (n,b) = take n (repeat b)

Might as well remove the parentheses:

rleLazyDecode (n,b) = take n $ repeat b

We can do better. What is the type of \n b -> take n $ repeat b? It is Int -> a -> [a], and a Hoogle type-search turns up an exact match: replicate. So we can improve it further:

rleLazyDecode (n,b) = replicate n b

Are we quite done? Well, one could ask: rleLazyDecade is now basically nothing but an alias or new name for replicate, except for how it converts from a tuple (n,b) to normal uncurried arguments n b. Is there any function which will do this conversion for us? Well, we have a function replicate :: a -> b -> c, and data (a,b) passed into said function to produce a c, so we want a function with the type signature (a -> b -> c) -> (a,b) -> c, which turns out to be something called uncurry. Having reasoned our way this far, the implementation is much clearer than the type:

uncurry :: (a -> b -> c) -> (a, b) -> c
uncurry f (a,b) = f a b

That’s what we were just doing manually. So now the next version is clear:

rleLazyDecode = uncurry replicate

A satisfying, short, functional, and lazy one-liner. From here the definition of rleDecode is almost trivial: we extend it to a list of tuples by throwing in a map, and we turn the resulting list of lists into a single list by way of concat:

rleDecode ns = concat $ map rleLazyDecode ns

We can tweak this further, as concat . map is a common enough idiom that there is a shortcut:

We can actually go even further into the realms of incomprehensibility. It turns out that lists are a monad. This means we can use bind and all the rest of the operations defined by the Monad typeclass to operate on lists and other things. So we can write this bizarre (but short and effective) version of rleDecode: