Thursday, February 24, 2011

Adventures in Game Boy Land

With Detours To Haskell, Lisp, C, and Go

What can I say, I've written a Game Boy emulator in Go. I don't have much to say about the emulator itself—it works, save for a few games1, has full sound emulation and configurable joystick/gamepad input—so much as the process of writing it.

The process was a long one spanning several months, even though the final version itself was almost entirely written in two to three days. The reason is the amount of time I spent messing around with prototypes written in other languages: two independent Haskell implementations, another one in C, and yet another in Common Lisp.

Thursday, December 23, 2010

easy-match: Simple Pattern Matching for Common Lisp

I was sitting around today dividing my attention between urgent bug fixes, IM, and working on my current Common Lisp project. For the past few days while working on the CL project, I've been occasionally irritated by not having a pattern matcher to examine lists and bind variables to parts of them.

A quick search online didn't reveal exactly what I wanted (cl-match didn't look quite right to me in some way), so I cooked up my own quick solution. Here's a brief look at my match macro in action.

  (defun foldr (f z list)
    (match list
      (()       z)
      ((x . xs) (funcall f x (foldr f z xs)))))

And of course a more pathological version of that:

  (defun foldr* (&rest args)
    (match args
      ((_ z ())       z)
      ((f z (x . xs)) (funcall f x (foldr* f z xs)))))

The use of an underscore to mark an unused value a la Haskell may raise a warning about an unused variable - this is one of the first things I'd like to fix, assuming it becomes bothersome in my current project. So far it hasn't come up.*

Before I continue on about how it works, I might as well provide the link to github.

The match macro essentially transforms into a cond expression. The pattern in each clause is scanned for the conditions required for a matching expression, and a list of bindings for a let expression is built at the same time.

There are definitely some gotchas right now in the construction of conditions for each pattern. For example, in the examples above, the empty list pattern must occur before the non-empty list, because the only condition for the non-empty list is listp, while the empty list is tested specifically for nil.**

Currently, the conditions are: check for nil, scan lists for more conditions, and check everything else for equality with eq (at the moment that's good enough for my purposes). Symbols are of course bound to values instead of checked for equality.

* Update: this came up, so it's now fixed. Any underscores in a pattern will be treated as a required but ignored value.

** Update 2: this is fixed. I have also added support for a guard expression for each clause.

Wednesday, December 15, 2010

bar (simple X11 status bar)

I've put a small application that I made for myself a couple of years ago up on github. It's a simple status bar for X11 window managers that don't provide one. It displays the time, CPU load, battery status, and the currently playing track in MPD (with support for displaying the composer tag rather than the artist, which is usually more useful for classical music). I used to use it with evilwm, but now that I've switched to xmonad, I don't have much use for it. However, I realized that I only have the code backed up on an external hard drive that I expect will die in the not too distant future. It's one of the few pieces of code that I've written for myself and actually used for a significant amount of time (the other major one being a CD ripping application, which should be sitting around somewhere; ever since I got an iPhone I have used iTunes to rip my collection).

Saturday, November 13, 2010

Re: Android Fragmentation, Exhibit 47

In response to this post by John Gruber, which refers to Netflix support on Android (or lack thereof) in comparison to its support on Windows Phone 7. Having worked on a project that almost always required extra support from OEMs—access to private APIs, firmware changes, etc.—I can comment on this. (We had a chance to work with devices before they hit the market).

More and more, I’m convinced that Android isn’t a single platform. It’s a meta-platform upon which handset makers build their own platforms.

Yes and no. I don't think it should be, but that's the way it is right now—I think that for most applications, Android behaves like a uniform platform. Games, productivity applications and the like should not run into issues like this.

The problem probably lies in some current shortcomings of the platform. For application vendors and OEMs, if there is some platform feature missing (like the platform security mechanism and DRM mentioned in the linked article), the easiest way around this is to implement their own workaround for each device's Android build. This is fine in the short term but in the long-run, if it's a commonly needed feature, we end up with myriad different implementations doing the same thing, each slightly different (even from the same manufacturer). Even worse, these features are only made available to the application vendors that made the deal with the manufacturers. One glaring example which I believe is finally being addressed is Bluetooth headset support.

Ideally, some of these changes would make it into core Android (and I hope they will), but I don't know if the manufacturers think that is currently in their best interests—somehow I doubt that they do. My feeling is that both the OEMs and the carriers have somewhat misguided notions of what they should be providing to the end user in an Android device.

Sunday, October 17, 2010

Select in Stackless Python

In Unix, select waits for file descriptors to become unblocked. More generally, we can define select as a mechanism for waiting on an arbitrary number of input or output sources for communication. We can refer to the input and output sources more simply as channels.

You can blame Andrew Francis for getting me into what follows. Thanks, Andrew!

A few months ago Andrew and I looked into implementing a version of select for Stackless Python based roughly on Rob Pike's implementation in Plan 9, which is examined in more detail in his paper "The Implementation of Newsqueak"[1]. I ended up writing a Python prototype and modifying the Stackless C sources to support it natively as well. Rather than just throwing a ~1000 line svn diff into the wild, I thought I'd write a bit about it in a blog article.

A naive implementation of select in Stackless Python would either involve a series of if/elif statements to check channel balances or an overly complicated system using delegate tasklets and channels whose sole purpose is to inform the working tasklets which channels are ready (i.e. not blocked).

Select can be implemented more elegantly if built into the stackless module itself. Python has no built-in case or switch statement, but as we will see, there are ways to work around this that work almost as nicely, and for some cases perhaps even better.

Prototype

To build a working prototype I wrote a small module (source on github). It wraps the channel and tasklet objects and provides a new tasklet operation called select. It also defines a new alt class of objects (this refers to the naming used by Pike) that represent channel operations. Alt objects are opaque, and the user of the module does not create them with a normal constructor. Instead, two new methods are added to channel objects: sends and receives, both of which return alt objects that can be passed in a list to select.

 
  import select
  
  ch1 = select.channel()
  ch2 = select.channel()
  ch3 = select.channel()
  
  # more code, tasklets, etc...
  
  def onReceive(ch,op,v): # receive callback
      print "Received", v, "from channel", ch
  
  task = select.current()
  task.select([ch1.sends("foo"),
               ch2.receives(onReceive),
               ch3.receives(onReceive)])

One nice feature of this approach is that in the likely case of a dispatcher or server that does little but wait for requests and ask other tasklets to take care of them, the list of alt objects can be reused and even modified from iteration to iteration without reconstruction.

Another thing demonstrated here is that alt objects can be associated with a callback. If a callback is used, it is fired before the select method returns, and the result of the callback is used as the return value of the select method. Otherwise, select returns a tuple of (channel,operation[,received_value]). Unfortunately using callbacks reduces the locality of select result handlers, but lacking any sort of built-in switch/case type of statement in Python, this is one of the few ways we can handle it.

The other point to note is that send and receive have been overridden - they are now implemented as single-case selects.

A Python to C Compiler (i.e. Yours Truly)

This is all very nice, but it's not transparently integrated into the stackless module itself. To do that in standard Stackless, we need to dive into some C code….

To that end, the Python prototype serves nicely as a basis for the C implementation. This presented me with the following tasks:

  • Implement a new alt type.
  • Move generic_channel_action logic into select (now PyTasklet_Select).
  • Hope that things don't blow up in my face.

The diff from a 2.6.5 svn checkout of Stackless is available on github. It works, but there are certainly bugs in the implementation and some debugging and introspection features aren't available (set_channel_callback, tasklet._channel). Some of the unit test currently fail, and there seem to be some reference counting issues that crop up in a few tests, but at this point I have no motivation to perfect it further.

The most difficult part, as someone approaching both the Stackless and Python C source code with little knowledge of it, was figuring out where to copy values between tasklets and fire the callbacks. Because callbacks need to be fired in the selecting tasklet, it took me a while to find where blocked tasklets are resumed. This is especially important when tasklets are running in different threads. Currently, this is not working correctly.

Here's the example from the prototype, re-written using the integrated implementation.

 
  import stackless
  
  ch1 = stackless.channel()
  ch2 = stackless.channel()
  ch3 = stackless.channel()
  
  # more code, tasklets, etc...
  
  def onReceive(ch,op,v): # receive callback
      print "Received", v, "from channel", ch
  
  stackless.select([ch1.sends("foo"),
                    ch2.receives(onReceive),
                    ch3.receives(onReceive)])

There is one limitation to the current implementation: a select cannot wait for both sends and receives on a single channel. The next example will not work.

 
  stackless.select([ch.sends(x), ch.receives()])

I believe that this is a rare enough use case that it need not be handled.

One Important Caveat

Alt objects must be created by the tasklet that intends to use them, because they are automatically associated with that tasklet. Passing them between tasklets will result in bad things (currently, a segmentation fault). There should probably be a runtime check to ensure this, but this is currently not implemented.

What I Don't Like

  • Copying values to the blocked tasklet is completed by the running tasklet instead of the newly unblocked tasklet. This means that any callbacks for tasklets are run by the wrong tasklet (i.e. they should be run right before select/send/receive return).
  • Creating an alt object for each operation. I don't know if there's a feasible way around this, and in any case the fact that they are reusable makes this mostly irrelevant.
  • Creating an alt object for each and every send() and receive(). This could be worked around by associating an alt with each tasklet that can be reused.
  • Using a static variable for random ready channel selection. This doesn't need to be extremely random anyway so it's probably not that big of a deal.

Possible Optimizations

There is one common case that we may be able to optimize in order to avoid any tear-down and reinitialization of select.

 
  def dispatch(req):
      # handle req...
      pass
  
  def handler(ch,op,v):
      dispatch(build_request(v))
  
  cases = [ch.receives(handler) for ch in channels]
  flag = True
  
  ## This is a common case for dispatching
  # while flag:
  #    stackless.select(cases)
  
  # To have more control over the select we may want a new
  # operation that looks something like this instead:
  stackless.select_while(cases, lambda: flag)

My gut feeling is that in order for this to work there is an important restriction that we must be able to guarantee: every channel that the select and its callbacks use must have preference set in favour of the select. This allows us to ensure that select will always be ready for the next operation, however this may not be necessary if all channel balances are properly restored between calls. The precise level of tear-down and reinitialization that cannot be avoided isn't yet clear to me.

The Future

I'm handing this off to Andrew for now. He'll be responsible for ensuring all of the unit tests pass. Oh, and if anybody cares, I've moved to Vancouver and am looking for work.

References

[1] The Implementation of Newsqueak [ps.gz] [pdf]

Saturday, August 7, 2010

Local Variables Should Not Be Variable

I can think of very few cases in which it is useful for function-local variables to have state. One problematic situation in particular can be variable initialization, which should always be possible to reduce to a single statement. If you are working in Java, this means you should be able to declare most if not all of your local variables final.

Why? Well, one upside is that it becomes a natural way to break code up into smaller pieces. It can also occasionally reveal errors that would have otherwise been hard to spot.

Take for (a contrived) example the initialization of a variable that depends upon some condition. Assume that for whatever reason we cannot use the ternary operator ?:.

int x;
if (getState() == ACTIVE) {
    x = ACTIVE_XPOS;
} else if (getState() == INACTIVE) {
    x = INACTIVE_XPOS;
}

I see this sort of construct all the time, and I ask myself the following questions.

  • Why is getState() called multiple times? Does it depend on shared state? If so, what happens if its result changes between the first and second call?
  • How can I prove to the compiler that x will always be initialized after these lines?
  • Furthermore, how can I tell the compiler that x will never change after it has been initialized?
  • More importantly (since the compiler is hopefully smart enough to figure these things out anyway), how can I tell this to the programmer who is reading it two years from now, and discourage them from doing silly things like modifying x?

The Lisper in me wants to write what the code is really trying to say, which is something like this:

final int state = getState();
final int x = (if (state == ACTIVE) {
                   ACTIVE_XPOS;
               } else if (state == INACTIVE) {
                   INACTIVE_XPOS;
               });

Clearly this is absolute nonsense. And yet…. The nice thing here is that I'm not forced to abstract the code away from where it's relevant, at least not until it does need to be reused. So the programmer should now understand my meaning, but the compiler does not.

The easy way to deal with this in Java is to abstract right away and create a method returning the correct value of x. This allows us to remove the extra clutter of declaring state in the current method. There's also the problem of a missing else block—what happens if getState() returns neither ACTIVE nor INACTIVE? Assuming that these are the only two documented return values, this is a fundamental failure of the method's contract and should be some sort of assertion failure. We can enforce this in Java with an exception.[1]

int getXForState() {
    final int state = getState();
    if (state == ACTIVE) {
        return ACTIVE_XPOS;
    } else if (state == INACTIVE) {
        return INACTIVE_XPOS;
    }
    throw new AssertionError("invalid state");
}
[2]

We can now happily reduce our original code to one line.

final int x = getXForState();

"But what about loop variables, iterators, accumulators and the like?"

Well, yeah, then there's that. That's why I prefer languages with proper tail-recursion and higher order functions. Unfortunately that's not what I am paid for.[3]

[1] Preferably of the Error variety. Sometimes I feel that Java's enforcement of declaring all thrown exceptions often prevents programmers from shooting themselves in the foot by forcing them to shoot themselves in their other foot.

[2] If anyone complains about the multiple return statements in this example, I will go postal. The amount of code that I have had to deal with that contorts itself into deeply nested blocks and avoids the very point I'm trying to make is extremely discouraging.

[3] At least, not yet!

Saturday, March 13, 2010

The Besnard Lakes at Il Motore

Well, last night was the release party for The Besnard Lakes's new album, "... Are the Roaring Night," and man was I ever tired. I had just arrived in the morning from Vancouver via the red-eye, and hadn't had much sleep. So since I was in no condition to review the show, without further ado, some bad iPhone snaps:

The show opened with The Sunday Sinners.

Then, the show we were all waiting for.

Il Motore is a great venue. I played there once last summer, but this was my first time seeing a show.

Tonight, if I can muster up the energy, Mad Parish at Katacombes.