Friday, February 13, 2009

Type Inference

I'm currently teaching CSC 212 - The Practice of Computer Science at UVic this term, and one of the topics we've touched on in the course is that of type inference -- the process by which a statically typed language can deduce the types of expressions without programmer annotation.

First some motivation from personal experience: there truly are moments in every programmers life when they learn about some new technique, concept, or abstraction and they stop, turn into Neo in the Matrix and go "woah". One of the first of them for me was when I learned about inheritance and polymorphism. Another was when I learned about type inference. Until you've used it you really can't appreciate how useful it is. It's yet another argument in favour of static typing, and it is really, really cool. It is without a doubt one of the coolest features I've seen in functional languages (though it is not limited to FP languages -- more on this in a second).

So what is it? Let's look at an example. In Java or C++ you might write a function with a header like:

Vector<somereallylongclassname> myMethodName (SomeOtherClassName foo)

And I don't know about you, but typing "SomeReallyLongClassName" and "SomeOtherClassName" is a pain in the ass. C/C++ alleviates this a bit with typedefs, but you still have to type extra characters to specify types of arguments (and Java doesn't have the typedef keyword). Wouldn't it be nice if the compiler could figure this out for us and save us some keystrokes? And of course it would be, and that's where type inference comes in.

In a language which supports type inference, the types of identifiers in your source code are deduced by the compiler depending upon the context in which the identifier is used. The trivial example I usually show students is the following function declaration in SML (a functional language designed by Robin Milner who is oftentimes credited as the guy who came up with the notion of type inference):

fun foo x = x + 3;

When the above code is compiled, the compiler deduces the type signature of the function foo() to be "int -> int", or (in English) a function which takes an integer and returns an integer. Note however, there is no syntactic evidence of type annotations -- nowhere did I write "int x" or something similar. The compiler figures out the type of x by the "x + 3" expression in foo()'s definition. Since 3 is an int, and the + operator is one which takes two arguments of the same type it infers that x must therefore be an int (and additionally since the + operator returns a value of the same type as its operands it also deduces that foo must return an int as well).

This is magic.

It's also the best of both worlds: all the type safety of static typing, and the "clean" or "uncluttered" code of dynamic typing.

One thing I've been thinking about lately though is how type inference would play out in a OO language. One of the reasons the algorithm works well in a language like ML is because functions can neither be overloaded or overrided (though they can be rebound or redefined). So for example, if I have a function declaration like:

fun foo x = bar (x);

There's no trouble in figuring out what which function bar() refers to (since there's only one). In an OO language like Java you could have a situation like:

Foo f = new SubclassofFoo();
f.someMethod(x, y, z);

and suddenly the problem becomes more complex -- is someMethod() a part of the Foo class or SubclassOfFoo? Or is it defined in some other parent class to Foo? And since we can overload methods in Java we also need to know the types of x, y, and z to figure out which version of someMethod we need.

Now imagine we lived in the C++ world and we bring in multiple inheritance and suddenly the world gets even more complicated as now someMethod() could be in one of many parent classes.

I suppose this is about the time I put on my snooty elitist academic hat and say that this is another example of why we should all be coding in functional languages, but I'll save that for another blog entry. :)

At any rate, type inference is a staple feature in many functional languages: SML, Haskell and CAL all use it. The Gem Cutter visual programming environment I'm using in my thesis work is a great tool as well for learning about type inference -- as you make or break connections between functions, type inference is applied and the types of inputs and outputs change accordingly. This allows newer programmers to more visually see the cause and effect nature of the algorithm.

Thursday, January 8, 2009

Xbox Avatars on your desktop

So last November Microsoft introduced the New Xbox Experience (NXE) and along with it, the addition of Avatars. Now we can have little digital versions of ourselves on our 360's. Additionally a few websites have made use of them as well (for example www.360voice.com displays your avatar on your Xbox's blog page).

I wanted to be able to check out the avatar's of some of my friends on XBL, and I thought it would be cool to have them on my desktop. From this, a Perl script for doing so was born.

For example, right now my desktop on my netbook looks like:


As you can see, the avatar's (and gamercards!) for 4 of my friends are drawn on my wallpaper. This script which started off so simple, has now become quite complex (as most programming projects do), and is quite versatile. You can now:

  • Specify the number of columns of gamers (in the above screenshot this is set to 1, but you could have 2 or 3 or as many columns of avatar's and gamercards as you want)
  • Set the opacity (transparency) of the avatar's so that you could have your background image partially show through.
  • Have as many or as few gamers as you wish
  • Change the card look by specifying a different base URL from MyGamerCard.net
  • Output the image in 8 bit, 24 bit, or 32 bit colour depth
  • Output the image as a PNG file or Windows BMP
  • Only generate an image consisting of Avatars & gamercards or have them drawn on a suppplied background image (in GIF, JPEG or PNG format)
  • Specify the width & height of avatar's and/or gamercards
  • And much more
The script itself is written in Perl, and runs fine with v5.10.x of ActivePerl for Windows (if using older versions, you'll need to install the GD library as well). It is command-line driven and intended for relatively "advanced" users. For help:

perl genWall.pl -?

will show all the options. And lastly the script itself can be found at:

http://webhome.csc.uvic.ca/~aparkin/xbox/genWall.zip


And it is released under the conditions of the GNU Public License (GPL), so is free to use and modify as you see fit. Enjoy!

Monday, August 25, 2008

Teaching programming

It occurred to me that I've never blogged about my work, so why don't I start...

My thesis work is centered around the use of a visual programming environment (VPE) to teach students new to computer programming how to construct software. This isn't a new idea of course, others have done so (Alice, Squeak, etc), however use of a VPE based upon the functional programming paradigm in educational settings is rather uncommon. The VPE I'm using is known as the Gem Cutter, which is a part of the OpenQuark framework originally developed at Business Objects.

To give a bit of background: OpenQuark is a framework which consists of a language called CAL which is a very Haskell-like language which compiles down to Java bytecode, and thus is interoperable with existing Java code. The Gem Cutter is a VPE which is built upon the CAL language (that is, all components or gems in the Gem Cutter correspond to CAL functions, or imported Java routines). Much more info can be found on the Wikipedia page on CAL and Quark, as well as the main homepage. It is similar in some ways to the Scala programming language, although Scala is intended as a language by itself (and compiles down to Java bytecode) whereas CAL is intended as a way to bring the functional paradigm to Java.

The reason that I find the Gem Cutter interesting is because typically I've found VPE's counterintuitive, not particularly useful, or "dumbed-down". For example, I've always found Alice a bit hard to take seriously (to be fair it has been designed to be friendly to younger audiences) due to the emphasis on kid-like "toy" animations rather than general programming constructs.

The Gem Cutter OTOH is a tool designed for and made by professional software developers working at a major software company. In particular, the functional paradigm seems particularly well suited to this, as (like dataflow programming) it has a natural visual mapping of having functions (or "gems" in Gem Cutter terminology) composed together with outputs of one feeding into the inputs (or arguments) to another. So rather than seemingly bending the paradigm to fit the visual model, it seems to be a much more natural fit.

Now you're probably thinking I'm overselling Gem Cutter, and I am. There are warts on it. Numerous times while working with it I've run into bugs, or "gotchas". Part of my thesis will be the exposition of these issues from the perspective of one trying to use the tool in a learning environment. Having said that however, it really is a cool piece of software to play around with, and in particular if you're a Java developer who's ever had the desire to pass around higher order functions then I highly recommend checking it all out. And more imporantly, it's all open source released under a BSD-like license so you can do pretty well whatever you want with it.

Firefox 3.0 revision 2 (or okay it's growing on me)

Well, I was somewhat critical of the newest version of Firefox before, but I'd have to say that it really is growing on me. The fact that my extensions including the two "cannot live without" ones (Session Manager and All in One Gestures) have now been made to be FF 3.0 compatible.

I still haven't run into any real technical issues, although in spite of claims about improved memory usage, FF is still a pig when it comes to using memory (as I write this on my Windows XP machine it reports ~128MB of memory usage with 7 tabs open). Occasionally I get CPU usage spikes, but they're thankfully quite rare.

All-in-all it is working well.

Monday, August 4, 2008

My streak is now done....

Well, I was on quite a roll this year, keeping a (I think) pretty impressive achievement streak going, but alas, as the cliche goes, all good things must come to an end. I'm currently away from home in the Kooteneys doing a combination of work and visiting family. I brought a 360 with me, but after the 9+ hour drive yesterday I didn't find the time to get any gaming in and as a result my 216 day achievement streak came to an end.

I really gotta tip my hat to people like Knuckles Dawson who did close to 2 solid years getting at least one achievement a day. That's incredible perseverance.

Oh well, maybe now I'll get that masters degree finished. :p

Some stats about my streak:
  • Length: 216 days (Dec 31, 2007 to Aug 2, 2008)
  • Total Gamerscore gained: 33,808
  • Total Number of achievements: 1,475
  • Number of Games 100% Completed during the streak: 23 (20 retail and 3 arcade)
And for the record, during my streak I also was doing full time studies at the University of Victoria working on my masters degree. I'm also married, to a wonderful woman who for some strange reason didn't divorce me even though I spent many an evening cursing at pixelated bad guys on the TV. I also taught in the months January to April. So yes, I did have a life while doing this.

Monday, June 23, 2008

Firefox 3.0

So, those of you who read this and know me will know I'm a fairly strong supporter of the Foxy web browser known as Firefox. This past week Mozilla released version 3.0 of Firefox, and I wanted to share some thoughts.

It wasn't a good start: upon installation 6/16 of my extensions (including 2 absolutely "cannot live without" ones) weren't compatible. The new address-bar is interesting, but I'm wondering where it pulls data from, as upon installation it knew I liked achievement sites, and yet I have my history disabled. As a privacy nut, I have concerns about what's being stored, and what isn't (if I say don't remember any history, it shouldn't remember anything about where I've been).

Haven't had any problems with speed/performance at all. Some have raved about how smooth the page scrolling is, but it looks the same to me as any other version. Memory usage seems to be slightly better, although it's WAY to early to say for sure (and to be honest, the mem usage problems of FF are somewhat exaggerated).

Haven't decided yet if I'll go back or not, but my initial impressions are not good. Admittedly though, I'm tough to please, most people will be completely tickled with this version. I just don't see any reason to stick with v3, but a lot of reasons not to. Perhaps if my "must-have" extensions become compatible then I might be more inclined to stick with v3, but given that there are extensions which aren't even v2 compatible that I really love and that have made me want to stay with v1.5.x on most of my machines I won't hold my breath.

Now for the rant:

I've been with FF since pre-version 1.0. It's amusing to me to look at its evolution as it started out as a lean no-frills browser that was supposed to be for the hardcore crowd. The idea being that if you didn't want a feature, you wouldn't have to have it, and if you did want a feature, that's where extensions came in. Now, it seems that feature-bloat has occurred with it, as with each version comes new advanced features that I don't really want, but are added from a marketing POV (so that when tech blogs & such can do those side-by-side comparisons of FF vs IE it doesn't look one-sided). I want the old FF back, where the goal is to create a minimal platform where extensions provide the functionality. If that were the case, then one of the big goals would be extension compatibility. One of the problems for me with FF is that I don't really care for the browser, but love the extensions, and as such I become very dependent upon them. The problem though is that every time a new version of FF comes out, half the extensions I use are no longer compatible (it's one of the reasons I still use v1.5.x on most of my machines). For me, that's a showstopping problem, and I'm not convinced the FF devs realize how much of an issue it is. I find myself now in a position on this machine where I either A) have to live without functionality I've become used to, or B) go through the pain-in-the-ass process of uninstalling v3 and reinstalling an older version along with all my extensions. That's not the kind of situation that is going to win support and/or adoption of the browser from the masses. One of the big criticisms of Microsoft is that they force features down your throat that you simply don't want. Now we see the same thing happening with the Mozilla team with Firefox, and the hacker in me can't help but feel his heart hurt a bit at that realization. I'm not going to go all hippie-ish and say that they "sold out", but there is a greater sense that there's less of a difference between FF and IE now (security issues aside) than there used to be.

Thursday, May 22, 2008

I should quit now, but only one round to go (round 4 predictions)

Okay, admittedly my predictions this year have been less than stellar, but only one more series to go:

Detroit vs Pittsburgh - Tough series, both teams are on huge rolls right now. Both are looking extremely impressive, and both have been dominating the competition thus far. They match up very well too, both have great offense, solid goaltending, and solid defence. I'd give the slight edge in defence to Detroit, and the slight edge in offense to Pittsburgh, and goaltending I'd say they're about equal (Osgood has been much better than his stats indicate, and Fleury hasn't quite been as spectacular as his stats would indicate). Tough series to call, but I'll give the edge to the Wings for two reasons: home ice and experience. My prediction: Detroit in 6.