Monad Transformers

This isn’t a monad tutorial, although I might do one some day. I’m going to assume a basic understanding of monads and some common functions from Control.Monad for this article.

Monads are rather useful. We can define all softs of computations with them: IO, non-determinism, random, stateful, etc. But what if we want to use multiple monads in a single computation?

Enter monad transformers, a class and framework for defining monads that can contain other monads.

An Example

Say you want to define a computation that consumes random numbers and makes use of some read-only state. We can use the Reader and Random (Rand) monads. We could define a Rand computation inside Reader, but that would require a lot of carting around of StdGen values; in and out of the computation. It could get messy, but what if there was a better way? Both Reader and Rand include transformer versions (ReaderT and RandT) but we will only use the transformer version of Reader.

The ReaderT type constructor takes a state type (r), a monad (m) and a return type (a):

ReaderT r m a

The Rand type constructor takes a RandomGen instance (g) – usually StdGen from System.Random – and a return type (a):

Rand g a

A combination of one or more monad transformers and a base monad is commonly referred to as a “monad stack”. In our case, our monad stack’s type is as follows:

ReaderT Int (Rand StdGen) String

Here, ReaderT is transforming our base monad (Rand StdGen). Computations inside this monad are in ReaderT but we can use lift from the MonadTrans type class to “lift” Rand functions into the ReaderT monad.

To evaluate or “run” the ReaderT monad transformer you must use the runReaderT function:

runReaderT :: r -> m a

All monad transformers provide an equivelant function following the naming convention runMonadNameT.

Running runReaderT on our computation yields a Rand StdGen String value. Essentially “popping” ReaderT off our monad stack. The final String must be obtained by evaluating the Rand monad with evalRand.

In the following example program, we use the same monad stack as before, be we make use of replicateM from Control.Monad to generate multiple String results to demonstrate the random generation.

So, monad transformers are a powerful mechanism to enable multi-monadic computations. Now that I understand them, they’re going to be very useful in my final year engineering project at university.

Awesomium Haskell

One of the problems I’ve encountered when programming with OpenGL is integration of the viewport with some kind of GUI. I’ve looked at GUI libraries offering OpenGL widgets (such as GTK and wxWidgets) and a few libraries implementing a complete GUI system inside OpenGL itself (Crazy Eddie’s GUI System, FLTK and AntTweakBar to name a few). However, none of these really suited my requirements – they were either too clunky, too simple, or just didn’t operate in a way that suited my needs. (Maybe I’m too picky?)

Eventually I found Awesomium – an offscreen web renderer library based on Chromium. At first-glance it just seems like a tool for embedding web pages in native applications, and this would certainly be one use for it (EVE Online uses Awesomium for their in-game browser, for example) but it has also shown promise as a web-based GUI framework in the engine powering Wolfire Games’ Overgrowth. Curious, I set about creating some Haskell bindings to try it out over the summer.

For now I’m going to gloss over why I’m creating the bindings manually instead of using C2hs. :P

Fast-forward a few months and I now have an actual use case! I found myself wanting a quick-and-dirty GUI for my ray tracer project and Awesomium just happened fit the bill. After a couple of days (mainly spent adding-in not-yet-implemented function bindings and smoothing out some creases) I have a nice HTML5/CSS3/JS toolbar in my renderer window:

Icons: Default Icon by interactivemania is licensed under a Creative Commons Attribution-No Derivative Works 3.0.

How it all works

Awesomium itself runs in a separate process, which can create a number of different WebViews. A WebView is like a browser window or tab. You can tell it to load a page and (eventually) render it to a pixel buffer.

What you want to do with the pixel buffer is up to you. In my case, I have an OpenGL viewport which I’m already drawing into, so I can just use glDrawPixels to get the page displayed on the screen.

Getting user input into Awesomium is a simple matter of calling some functions. In my case I’m using GLUT and passing input from there straight into Awesomium (although it would be easy to add conditional passing on input if required).

The UI itself is defined as a simple HTML file a linked CSS file. Tooltips are implemented with some simple Javascript. Because of this, the UI can actually be opened by Chrome (and debugged if necessary).

Communication between Awesomium and Haskell land is a little uni-directional at the moment. The different buttons call Javascript functions defined Haskell-side. At some point I’ll implement calling Javascript callbacks from Haskell but it’s not something I require right now.

Future work

My bindings do not yet implement the full 1.6.x API but I’ll continue to chip away at it. Version 1.7.x of Awesomium will scrap the C API and I will need to either a) use a C wrapper created by the community or b) create a custom wrapper, because Haskell’s FFI doesn’t support C++ (yet).

I would also like to investigate a monadic interface since my bindings operate only operate in IO at present. That’ll be interesting…

I’ll upload the package to Hackage when there is a complete set of bindings for the C API – but for now the code is available on GitHub.

Existential Quantification

Being still rather new to Haskell, I have yet to familiarise myself with many of the extensions to the language GHC makes available to programmers. However, there is one that I’ve become quite fond of (possibly due to my OO experience): -XExistentialQuantification.

At a simplistic level, Haskell can be viewed as a more human-friendly wrapper around a typed lambda calculus called System F. When GHC compiles a module, it converts it into a slightly custom version of System F called Core. By comparison with System F, Haskell’s data types are rather constrained and although they get the job done for the most part, there are times where you might want to make use of some System F features in your code.

For instance, say you wanted a concrete data type that could contain a value of any type – a polymorphic value. We can use -XExistentialQuantification to define this.

data PolyContainer = forall a. PolyContainer a

If we wanted, we could have a list of PolyContainers, each with a different type of value inside it. While this will type check and is sort of interesting, it’s not very useful. The polymorphic value is just an ‘a’. We can’t do anything useful with it on its own.

Existentially quantified types become a lot more useful of we include some sort of function that can convert them to a concrete type. Here’s an example of a “Show box”; a data type that wraps abstract values with a function that converts them to a string.

data ShowBox = forall a. SB a (a -> String)

You can see that as well as an existentially quantified type ‘a’, there is also a function that can take an ‘a’ and turn it into a string. To make things easier, here are some functions to build ShowBoxes:

mkShowInt :: Int -> ShowBox
mkShowInt n = SB n show

mkShowChar :: Char -> ShowBox
mkShowChar c = SB c show

We can also define an instance of Show for ShowBox that applies the function to the value:

instance Show ShowBox where
show (SB x f) = f x

ghci> [mkShowInt 5, mkShowChar 'Z']
[5,'Z'] :: [ShowBox]

But really, we want a system that will work for any showable value. A simple solution would be to define a mkShowBox that takes any Show a:

mkShowBox :: Show a => a -> ShowBox
mkShowBox x = ShowBox x show

ghci> [genShow 5, genShow "Hello", genShow 'Z', genShow True]
[5,"Hello",'Z',True] :: [ShowBox]

This works or course, but what if we wanted to restrict the set types accepted by the predicate forall a. to just the Show typeclass?

Restricting Types

So far we have been using the predicate forall a., accepting all types into out ShowBox. We can restrict this predicate to a specific typeclass – providing access to typeclass methods.

data ShowBox = forall a. Show a => SB a

Notice we no longer have to supply an a -> String function, instead we can just apply show to the value because we know it’s an instance of the Show typeclass. So we’ll just define the new Show instance to do just that:

instance Show ShowBox where
show (SB x) = show x

ghci> show [SB 5, SB "This is a string", SB 'Z']
[5,"This is a string",'Z'] :: [ShowBox]

A Real-World Use Case

I’m implementing a ray tracer in Haskell as part of my final year project at university. As far as the bulk of the renderer is concerned, geometry is geometry. It really doesn’t care what its actual type is so long as it can perform ray intersections with it. (At some point I’ll write a bit about how to deal with situations where you do care – but it’s a bit beyond the scope of this blog post).

In my case, I have a variety of types (Sphere, Plane, Mesh, etc.), each of which are instances of a typeclass Shape – which provides a method: intersect.

But when passing geometry to the renderer, I don’t want to give it a list of spheres, a list of planes and a list of meshes; I would rather just give it a list of generic shapes. I need a ShapeBox like the ShowBox we defined above. I actually ended up calling it Object because it carries additional material data and ShapeBox didn’t really sound sensible to me:

data Object = forall a. Shape a => Object a Material

When a Shape value is inside an Object, the range of operations you can perform on it are reduced to the set performable on any arbitrary instance of Shape because it is now of a polymorphic type. As a result, you can’t pattern match on it and you can’t inspect it. You can only use it.

I’ve found this to be a useful technique for implementing data hiding and abstraction. I’ve encountered a few niggles along the way and I would like to cover these at some point as they’re also a good opportunity to talk about Data.Typeable.

PHP Makes Things Hard

I really should post on here more often…

Today was an interesting day. I’ve been working on a project (unfortunately written in PHP) and decided it would be fun to try adding real-time updates using Pusher. As it turned out, this was super easy (I’m actually quite impressed with how easy it was).

However, Pusher doesn’t provide any access to message history. What I really want is for the last four or so messages to be sent to a user when a page loads so the activity stream isn’t initially empty. Pusher doesn’t allow you to do this so I started looking for workarounds.

All that is really required is a circular buffer kept in memory that PHP can read from and push to. The problem is that PHP doesn’t really provide any way to preserve internal state between requests – you have to use an external database, cookies, the filesystem or a memcache server. In comparison, a sane language like Python or Haskell would allow you to do something like this quite easily (although I can’t think of an elegant solution for Happstack right now).

Maybe there is a way to handle this in PHP, but I don’t know it and I haven’t found anything online. I guess the best way would be to use one of the systems above or create a custom buffer server that only does this one job to ensure maximum speed.

In the end, it’s just one more reason for me to dislike PHP.

A Symphony of WAT

I’ve just started a unit at University called “Desktop Application Development” which is supposed to give students “an understanding of the processes and techniques used to develop end-user software that includes modern graphical interface components” and “practical experience in constructing desktop applications within an advanced software development environment”. In this case the framework of choice is WinForms via C# and the IDE is Visual Studio 2010.

It all sounds quite reasonable – and for the most part,  it is. The lectures have been good thus far and I’m interested to see what will be discussed later in the term. However, the coursework is a different matter entirely and while the concept seems solid, I feel very strongly that the implementation just isn’t suitable for the course.

Concept

The practical component of the coursework is to develop an image library application which has a simple file-based database of collections and information about the photographs each record points to. I feel this is a good coursework task because:

  • it requires implementation of file IO
  • the end application will be fairly complex, making use of multiple windows and dialogs (including custom and common dialogs)
  • the final product should be useful in some way
  • the final product can be extended to add extra features.

However, with the good out the way, now we get the the weird…

The Weird

Rather than getting students to start from scratch, a project is provided as a starting point, with the main form already populated with the required controls and even some predefined methods. Three other dialog forms are also included (similarly pre-populated) along with complete classes for handling images and image collections.

At first I was really surprised by this (questioning why not encourage students to build an application completely from scratch) but after a little thought I can see the motivation behind this decision. I think they want students to focus on the more important aspects of the application. However, I can’t help feeling that too much has been done already, in particular I think the image and image collection classes shouldn’t just be given to students as a whole, but I’ll let that slide because my main concerns are with how the existing code base was written.

WAT.

Starting with the main form (because that’s the first thing I saw), straight away I see something a little… odd:

Comic sans though. Really?You’re supposed to be teaching us about GUI design and you use non-system font in the coursework template? What about core UI design concepts like consistency and design guidelines? Do they mean nothing to you?

Moving on from the non-standard font choice, what else is there?

Panels?It looks like the components in the coloured areas are grouped together into relevant containers, which would be nice. Except, those coloured areas aren’t containers at all. They’re not even coloured rectangles. They’re actually coloured labels with an empty text property. Wat. Seriously. They’re not even aligned properly!

Not only is this a strange choice for adding colour to an application, but also a bad one. What if someone using the high-contrast theme wanted to use the application?

Humorously the text is now even less readable now because the theme set the default colour to white (expecting a dark background).

So what about the code? Well other than using Hungarian notation everywhere and prefixing classes with ‘C’ (personal dislike) and enums with ‘e’ (which I have never seen before) the codebase isn’t all that bad. However, there are a number of inefficiencies which I would normally overlook except that this project will set the standard to which students will tend to write their code. For example:

public CPSImage()
{
strFileName = "empty";
lstCategories = new List<eCategory>();
bmpImage = null;
lstKeywords = new List<string>(); //list of keywords associated with the image
strComment = "empty";
rftOrientation = RotateFlipType.RotateNoneFlipNone;
}
public CPSImage(string strFileLoc)
{
strFileName = strFileLoc;
lstCategories = new List<eCategory>();
lstKeywords = new List<string>(); //list of keywords associated with the image
strComment = "empty";
bmpImage = null;
rftOrientation = RotateFlipType.RotateNoneFlipNone;
}

Considering that one of the core mantras taught to students throughout the course is to minimize duplicating functionality in your code, it seems very odd to have it happen all over the place in this project.

Of course, in order to avoid duplication of code, this should really be written as:

public CPSImage(string strFileLoc)
{
strFileName = strFileLoc;
lstCategories = new List<eCategory>();
lstKeywords = new List<string>(); //list of keywords associated with the image
strComment = "empty";
bmpImage = null;
rftOrientation = RotateFlipType.RotateNoneFlipNone;
}
public CPSImage() : this("empty")
{
}

And even then I protest the default definition of any string to “empty” (why not use an empty string?).

Although it has never been suggested at any point that one should improve the template to improve it, I will certainly be doing exactly that. However, if this was actually expected of us, it was never made clear and I highly doubt anyone else will bother.

Resuming Scheduled Programme

It’s been a while but I’ve decided to start blogging again (again). In recent months I’ve been finding new topics of interest and keeping myself busy with a variety of different things.

More recently though I’ve discovered Haskell and the world of functional programming and as a result I’ve turned my attention towards theoretical computer science, mathematics and Haskell programming. This is very likely to end up being the focus for my final year unit selection and engineering project, and I will be writing some articles here over the coming months covering some of these topics.

Some of the topics I’d like to write about include category theory (and how it’s relevant to Haskell), Haskell in general, Windows 8, and education.

Keyframe Animation In XNA

Just thought I’d share this. Someone else might find this useful.

Yesterday I had a need to animate an alpha value over time. This was to animate title text in and out when a level loads in my current XNA project for Windows Phone 7 (ooh, exciting!). My first implementation was truly horrific but after a little thought I crafted a nifty little keyframe animation class to make things easier. (There may be a better way to do this but I have yet to see it).

To use the class, just set a load of keyframes when you initialize an Animation object, call Start() and call Update() with the current game time and a reference to the value you want to animate.

Check it out, clone it or whatever on gist.github.com.

Imagine Cup 2011

Yesterday, I competed with my team-mate, Edd Slipszenko at the Microsoft Imagine Cup UK final (hosted at Microsoft’s UK headquarters in Reading). The day itself was a lot of fun and I got to meet and talk to a lot of cool people as well as play some Kinect, Portal 2 and Halo: ODST.

@bennuk dancing to Lady Gaga's "Poker Face" on hard. Over 800,000 points I think

The road to the Imagine Cup final was an interesting one. Edd suggested that we should enter and we eventually wrote the entry for the first round – a proposal for an application to help optimize the use of farm land in the third world. At a loss for a better name, we called it OptiFarm. We also started writing code for the application. We decided to use C# and Windows Presentation Foundation but were lacking experience in these areas. We knew it would be a challenge – but were definitely up for one.

To our surprise, we made it through to round 2: the video presentation round. For this round we had to make a video talking about our project – hopefully with a demo of what we had working so far. At the time we hand almost no actual code to show. All we had were a few classes for storing the data and the start of the internal database. No user interface and no actual results from the application. Despite this, we still managed to create a nice video talking about OptiFarm – although this was rather last minute with the file still uploading to the server when the deadline passed.

With this video we made it through to the UK final ad the real work could begin – except we let it wait and decided not to go with a week to finish. I thought it would be impossible to create even a reasonably good application with 5 days to go. Then Edd convinced me to try anyway.

Within 5 days we created almost 100% of the application we eventually demonstrated to the judges. In that time I think I learned more than I have this year at University – turning from a complete WPF novice into a reasonably competent developer. (Maybe not.)

Either way, we eventually found ourselves at Microsoft’s UK offices to present the fruits of our labours. Although we came 4th, we both learned a lot – both on the way and at the finals. Had we actually worked on it more and prepared our presentation better, I’m sure we could have done better. I’m definitely up for entering again next year.

Congratulations to the winners: Kevin Pfister and Project OVE – I hope you guys have fun in New York (and win the global final of course). I would like to thank Ben Nunney and everybody else who was there for a great experience yesterday.

University: The Ultimate Distraction

In about a weeks time I will have been living and working in Portsmouth for two whole months and even though you could say it’s still early days: it’s been awesome.

There’s a new link in the intra-site bar above ‘CompSci Portfolio’. This is technically coursework for my Web Authoring and Design module and even though only the WEBAUD section needs to function, I hope to keep it current for the duration of my course.

At present it is targeted purely at Google Chrome. I do not expect it to work in other browsers and right now it isn’t worth my time to maximize compatibility. Having said that, it is (supposed to be) valid HTML 5 and CSS 3 and will work wonderfully in Chrome.

It’s built on a custom PHP framework using both Markdown and raw HTML for writing pages. More interesting is the setup I have using Git in conjunction with multiple development servers. I can develop new features in their own branches and merge them into master and pull them onto the production server when they are finished. It’s quite awesome.

In other news: expect much Python awesomeness. Enough number crunching: time to make some games!