Jan 27 2011
Archive for January, 2011
Jan 26 2011
Both hands on the ground, both feet in the air?
For about six months now I (and then Helen) have been going to Capoeira Angola classes run by Mão no Chão group. Suffice it to say we are neither of the people in this photograph in fact, it’s just one I found on Flickr.
Jan 24 2011
Books: Incoming, outgoing and in a holding pattern
Right now I’m reading The Three Musketeers by Alexandre Dumas. It’s really enjoyable so far — whimsical and witty like a 19th-century The Princess Bride (not inconceivable). I’ve got a big ol’ pile of things to get through after that. I still have a book from my birthday in June and a bunch from Christmas too. I came away from last night’s book group with two more — Pathfinders: The Golden Age of Arabic Science by Jim Al-Khalili. I’d been swithering over this one until I noticed the author. He has produced some great science television so I thought his book might be worth it. And Under Milk Wood, a play I associate strongly with my father though I’ve never heard or read it. But I’ve been quoted it a lot!
I took along Seamus Heaney’s translation of Beowulf but no-one was interested. I think a lot of people had book overload and weren’t taking new ones to read. We’re not having our next meeting until March so there will be plenty of time for people to finish the books they’ve got. Hopefully I can deplete my to-read pile slightly by then.
Jan 23 2011
A look at Lightweight Static Capabilities (part 3)
This is the third part of my slow examination of Kiselyov and Shan’s Lightweight Static Capabilities paper. Start at the the beginning and then read part two to put this part into context.
Section 3 of the paper uses the same technique from Section 2 and adapts it to tackle a larger and more interesting problem: array bound checking. To motivate the problem they follow in the footsteps of the dependent type literature by implementing binary search with statically-checked “safe” array lookups.
To explain the approach they use we’ll start with an unsafe implementation that uses runtime bounds checking on each index.
bsearch :: (Ord a) => (a -> a -> Ordering) -> a -> Array a -> Maybe (Int,a) bsearch cmp key arr = go 0 (length arr - 1) where go lo hi = if hi >= lo then let midpoint = lo + ((hi - lo)`div`2) curvalue = arr ! midpoint in case cmp key curvalue of LT -> look lo (m - 1) EQ -> Just (m, curvalue) GT -> look (m + 1) hi else Nothing
The index operation (!) checks at runtime to see if the supplied index is within the bounds defined by the array (we still have to catch an exception, which I haven’t shown here, but at least it won’t blow everything up). This check is not necessary if we can prove that the index will always be in range.
The general approach outlined in Section 2 was to brand “safe” operations and types which go together. Once a list had been branded non-empty (ie had type FullList) it could be freely passed around and later safely destructured into head and tail without worry.
In the case of lists the values were branded “safe” and the head/tail operations could only work on “safe” values. The head operation is analogous to indexing the 0th element in an array. If we were to extend the list approach to arrays we would need a deref0 for the 0th element, deref1 for the 1st element and so on. This is both unwieldy and needlessly restricts the type of indexing possible.
The solution proposed by the paper is a set of “safe” indexing operations which only work when both the array and the index are branded as being compatible. An index is compatible with an array if it is within that array’s bounds.
get :: BrandedArray b a -> BrandedInt b -> a
The type variable b here is a phantom type which brands arrays and indices. The get operation can only be used if the same brand is applied to both array and the index.
Initially the only “safe” array indices are the offsets of the upper and lower element. The brands for an array are created alongside the upper and lower index in one operation, providing the assurance to the type system that whatever type b represents it is identical for these three values:
brand :: Array -> (BrandedArray b a, BrandedInt b, BrandedInt b)
(In fact this is not the proper type signature. The proper signature is reproduced below, with existential quantification over the branding type b and continuations for passing/failing conditions. This signature is just a gentle introduction to get the idea across)
Further operations can be created with transformations on the provided indices: increments, decrements and midpoints.
Whatever b is here it’s the same type, but next time brand is called b may well be a different type. The type system can’t unify them since it doesn’t know the type — it’s never specified. So only indices created alongside the branded array can be used with it, since only in that situation can the type checker be sure that the type of b matches.
This seems a powerful use of phantom types and it’s one I’d never seen before. The paper uses the phrase “type eigenvariable” at this point, which I’m not familiar with but it’s such a nice phrase that I’ll have to keep an eye out for it.
As before the type constructors to enforce this safety are hidden and only accessible under specific and well-guarded conditions.
-- Not exported! newtype BrandedArray b a = BrandArray a newtype BrandedInt b i = BrandInt i brand :: Array -> (forall b. (BrandedArray b, BrandedInt b, BrandedInt b) -> w) -> w -> w brand arr cont stop = if lo <= hi then cont (BrandArray, BrandInt lo, BrandInt hi) else stop
The user provides functions to deal with the two cases that the bounds are valid and invalid. This same pattern follows through to other parts of the implementation: condition checking is done internally and the user provides functions to deal with various cases. This keeps some of the constraints (like the existential quantification over b, the brand type) wrapped up. If the user tries to subvert this the type checker objects:
Inferred type is less polymorphic than expected Quantified type variable `b' escapes In the second argument of `brand', namely `Just' In the expression: brand testarray Just Nothing In the definition of `it': it = brand testarray Just Nothing
In order to implement the rest of binary search over arrays some arithmetic functions on indices are introduced. These functions exist inside the trusted kernel so some effort is made to ensure they are correct. They also require passing/failing functions for operations that may fail (such as finding the successor of a valid index: the successor may be out of bounds):
-- If i1 and i2 are within bounds then their midpoint -- is within bounds. bmiddle :: BrandedInt b -> BrandedInt b -> BrandedInt b bmiddle i1 i2 = BrandInt (i1 + (i2 - i1) `div` 2) bsucc :: BrandedInt b -> BrandedInt b -> (BrandedInt b -> w) -> w -> w -- If i is less than the upper bound we use continue calculation -- with the inside else we fail with outside. bsucc (BrandInt upper) (BrandInt i) inside outside = let i' = i + 1 in if i' <= upper then inside (BrandInt i') else outside
There is also a predecessor function which works analogously. First we need to rework the binary search for this style of programming:
bsearch :: (Ord a) => (a -> a -> Ordering) -> a -> Array a -> Maybe (Int,a) bsearch cmp key arr = brand arr find Nothing where find (barr,lower,upper) = go lower upper where go lo hi = let m = bmiddle lo hi x = unsafeGet barr m in case cmp key x of LT -> bpred lo m (\i -> go lo i) Nothing EQ -> Just (unbrand m, x) GT -> bsucc hi m (\i -> go i hi) Nothing
On each iteration of the algorithm finding the value at the midpoint is always a safe operation provided the two arguments are safe. Incrementing or decrementing the upper/lower bound might fail if the index goes out of range, but this check can be considered part of the end condition, ie the search finishes unsuccessfully anyway if the index goes off the end because it’s an indication that the value we’re looking for is not stored.
The call to unsafeGet is, in fact, safe, since the index m is guaranteed to be in range for this array. No runtime checks are required over those needed to indicate the end condition. As before this problem is always dealt with formally using a similar mechanism for the empty-list problem, using Strict and Lax languages and mappings from one to the other. I’ll look at that next time.
Comments Off
Jan 22 2011
ComicBake with comic publishing capabilities!
Version 0.2 of ComicBake has just been pushed to the online repository. The changes since 0.1 were mostly internal reorganisation though there were some useful additions:
- Rounded corners for speech bubbles
- Use estimated box size for layout to prevent accidental overlaps and increase accuracy of inter-bubble spacing.
- Publish to Flickr
The last one is the most important new addition. Now, once you’re happy with the look of the comic you can push it to Flickr. The client remembers logins between invocations so once you’ve authorised it to upload everything will work smoothly.
To this end I’ve split apart the function of the comicbake tool into build and publish sub-commands. Build works as before and is the default behaviour if you don’t specify a command.
To make and upload a previously scripted comic you now do this (making use of the default comicstrip.png file name):
$ comicbake build -i scripts/mycomic.script $ comicbake publish --title="My great comic"
There are still plenty of issues to work out with this interface, and further work needed elsewhere, but ultimately I’m pretty happy with how things are working out.
Comments Off
Jan 19 2011
Dorian Gray and enjoyment of reading
I finished The Picture of Dorian Gray at least a week ago and I’ve been struggling to put into words what I thought about it ever since. It’s a pretty slim novel but it took me a few weeks to get through so it obviously wasn’t enthralling.
My main problem, I think, was that it had a plot but no story. I felt no desire to read on other than to find out how the plot resolved. The characters were bland at best, and often both hateful and boring. Dorian Gray wishes that his portrait would get older instead of him, which seems to stop him maturing at all. The book was originally much shorter, and it shows — I felt there was a lot of filler which expanded it from a short story to a novel.
I got a lot more enjoyment from the introduction which placed the book in a historical context and recounted some of the reactions to its publication. It’s strange how some books are more fun to read about than they are to read.
Incidentally, I object to the inclusion of an introduction which serves to give away large segments of the plot. So much so that they required a pre-introduction to tell you not to read the introduction until you’d read the book. Wouldn’t it just be more appropriate to put the spoilers at the end and call it something else?
Comments Off
Jan 18 2011
A look at Lightweight Static Capabilities (part 2)
This is part 2 of a series of posts looking at the paper Lightweight Static Capabilities by Kiselyov and Shan. Part 1 can be found here.
The next section of the paper is more theoretical. The authors introduce two notional languages, Strict and Lax, and their typing mechanisms. Strict has a “fancy” type system which specifies two list types (List and List+) and only the latter type can be deconstructed with head and tail functions. The List+ type can only be created by a special nonempty conversion, which cannot be applied to empty lists.
The Lax language has no such advanced typing facility and will happily accept nonempty(nil) as a legal construct. This language is more like a traditional functional language with a possibly-empty list type. In terms of typable operations Strict defines a smaller set than Lax, and every legal Strict program is a legal Lax program too. The authors define a mapping from Strict to Lax programs to allow embedding a statically safe program in Haskell, ML, etc.
This embedding is ultimately what we always attempt to do when programming. The set of things which the compiler will accept is necessarily larger than the set of things which will get us the right answer. Type systems aim to constrain the “acceptable” set slightly though there is still plenty of scope for bugs. :-) Some programming languages make it easier to write programs which are dangerously but subtly wrong. Using the full capabilities of a language like C all the time is incredibly foolhardy: not every situation demands pointer arithmetic. If we can use the type system to wall off “dangerous” code then all the better. Another example is the use of IO actions or unsafe* operations in Haskell. These are often used only where necessary in the former case, or walled off in an opaque module in the latter case. This walling-off allows us to concentrate our verification efforts to areas which can cause the most problems.
The resulting “embedded” code is the trusted kernel mentioned in the previous post. The reduction rules defined for Strict and Lax are contained in this kernel. The reduction rules for the non-empty list case are:
head (nonempty (cons E1 E2)) -> E1 tail (nonempty (cons E1 E2)) -> E2 indeed nil E1 E2 -> E1 indeed (cons E E') E1 E2 -> E2 (nonempty (cons E E'))
The first two rules define the operation of head and tail on a non-empty list. These operations aren’t defined for empty lists. The second two rules show how the non-empty type is constructed if the list is truly non-empty. Notice the similarity between indeed and Haskell’s maybe function (with arguments rearranged):
The maybe function forces the user to deal with all cases in the option type by requiring functions and default values for each case. The indeed function goes one step further by not allowing any other way to take apart the list type — in order to use head and tail you have to convert the List to a FullList and only indeed can do that.
In the spirit of the maybe function for Maybe types and the either function for Either types it would seem sensible to have a list function for List types, which would take a function for each case suggested by a list. After a bit of reflection I think this is the fold, or rather maybe and either are specific folds.
Which all suggests that the toy example provided (reversing a linked list) can be safely recreated using a fold. If we look again at the safe version defined in the previous post we see two cases we have to deal with:
reverse :: List a -> List a -> List a reverse xs acc = case full xs of Nothing -> acc Just ls -> reverse (tail ls) (cons (head ls) acc)
Stripping away all the plumbing stuff that’s going on here, we have two vital bits:
- The return of the accumulator when the list is no longer non-empty, ie is empty.
- Consing the head of the list onto the accumulator at each stage that the list is non-empty.
Step 1 is done automatically by the fold: it lets you iterate over values without being concerned about them running out. Step 2 is just the cons operation we know, with the order of arguments reversed. To actually reverse a list we supply an empty accumulator.
We know we can define this simple function as a one-liner but not all functions are as simple. The next section looks at array indexing operations. The motivating example this time is binary search over arrays — checking that all indexing operations remain in-bounds and that these constraints are ensured by the type system. By contrast with list reversal a binary search deals with several separate elements (the array, the lower and upper index) which cannot be hidden completely if we are to allow general array operations.
Jan 17 2011
Greatest CD purchase never made
Several years ago I bought some stuff in HMV and was given a free sampler CD to promote a number of artists’ new releases. This CD has turned out to be a fantastic resource and I’ve not yet mined all of the possibilities.
The disc included Death Cab for Cutie, Skin and Bell X1 — I now have at least one album by all of these artists. OK Go were also there, and they’ve since become heavily played on YouTube for their great music videos. I intend to look more into their music too.
More things to look into: Ladyfuzz, Nightmare of You, Stellastar, Josh Rouse. The lesson learned is that if HMV give you a sampler CD you really should take it. It will probably be incredibly well curated and highlight a whole bunch of interesting artists. It worked for me.
Jan 16 2011
A look at Lightweight Static Capabilities (part 1)
I started to look at the Lightweight Static Capabilities paper over lunch on Friday. This is far too big an paper to consume in half an hour and I’m a long way from understanding it all. I thought the best way to increase my understanding was to write down my thoughts on and understanding of each section as I come to it. (The examples here are given in Haskell-like syntax, with fewer obscure operators. Conseuqently I’ve used the type name List a instead of [a] and cons instead of infix :.)
The area the paper looks at is labeled “lightweight dependent typing” — dependent typing without dependent types, essentially. Dependent type systems allow the programmer to specify types based on the values, so the programmer can ensure that lists will necessarily be non-empty and denominators necessarily non-zero, among other useful examples. The paper looks at ways of creating type safe “capabilities” without dependent types.
I will take the paper a little bit at a time, and as I read on I might have to correct previous misreadings/misinterpretations. The first motivating example given on page 2 provides a program to reverse a list, building up the result in an accumulator.
reverse :: List a -> List a -> List a reverse ls acc = if nil ls then acc else reverse (tail ls) (cons (head ls) acc)
The standard list destructors head and tail are partial functions. They are not defined for the empty list and will produce a runtime error if accidentally applied to the empty list. For this reverse function we can be reasonably certain that it won’t blow up at runtime, but it’s a very simple function and we can’t get the same assurances in all situations. Let’s call these standard head and tail functions by more explicit names:
unsafeHead :: List a -> a unsafeTail :: List a -> List a
In order to provide static guarantees that all uses of head and tail are safe a new type called FullList is introduced which is a rebranded standard list type. The key element is that head and tail can only operate on FullList types, and a guarantee is provided that values in this type are non-empty lists. With this static assurance we can actually use our unsafe functions from earlier:
-- unfull converts a FullList to an ordinary List type head :: FullList a -> a head = unsafeHead . unfull tail :: FullList a -> List a tail = unsafeTail . unfull
The FullList type becomes a one-use token which the value has when it moves through the program. This token can only be created in one place (which we can lock inside a separate module). We can easily control under what conditions this token is assigned:
module (FullList, unFull, full) where newtype FullList a = Full { unFull :: List a } full :: List a -> Maybe (FullList a) full ls = if null ls then Nothing else Just (Full ls)
To verify that head and tail are always “safe” we must only verify that a FullList is always non-empty. The paper goes deeper into the formal verification than I’m comfortable talking about (at least at the moment) but it should be reasonable to say that there is only one line of code, the definition of full, which needs examined.
This is the kernel of trust concept that the paper revolves around. Shrinking down this kernel means it’s easier to examine and verify; and from there the type system extends our trust to the rest of the program.
From the user perspective, the type system forces us to check that lists are not empty before invoking head or other unsafe list operations:
reverse :: List a -> List a -> List a reverse xs acc = case full xs of Nothing -> acc Just ls -> reverse (tail ls) (cons (head ls) acc)
Ideally we’d be doing this anyway so there isn’t a greater burden — but it does provide us with compilation errors if we forget to do it. (From personal experience the use of an option type like Maybe makes it much harder to forget these things, even before getting the compiler error. And calling fromJust or other unsafe extraction method feels immoral.)
This takes us up to Section 2.1, with only one omission. The paper at this point switches from explicit wrapping and unwrapping of option types to continuation-passing style. The option types are straightforward but slow things down. Depending on how things go I might follow suit and use continuation-passing from here on, but it might prove a better learning experience to convert to option types. We’ll see when I come to write up the next section.
Jan 15 2011
The tricky art of supermarket price comparison
Last year some time, when the weather was still hot and people still wore t-shirts and skimpy dresses during the day (obviously people still wear them in the evening, yo), I did some research to see if the Village Store was much more expensive (or cheaper) than the local supermarkets.
I don’t have the data to hand — it’s buried somewhere in my computer — but I think we were “about average”. Some things were very good value and others less so.
The important point was the level of work required to get the prices. Of the local shops (Tesco, Co-op, Lidl) none of them allow price checks online, without at least setting up an account and logging in (Tesco). This makes week-by-week comparisons much harder because you have to pound the streets each time to keep current.
The other factor was the level of obfuscation used by the supermarkets. The Co-op was particularly helpful: all their price labels have a standard unit price somewhere on them. Even a packet of tomatoes sold as a priced unit will give its weight and cost per weight. On the other end of the scale Tesco go out of their way to avoid comparison. To buy a packet of tomatoes, for example, you buy a packet of six. So you have to guess how much six tomatoes might weigh, assuming a spherical tomato of uniform density and blandness.
In short, even with a small number of shops comparing prices is extremely difficult without a great deal of legwork and calculation. Clearly they don’t want you or me or anyone else setting up supermarket-price-comparison.co.uk and informing people of the real cost of their weekly shop.

