Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

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.

Monday, April 9, 2007

Recreational Computer Science Society Meeting Tonight

For those in the greater Victoria area, tonight is a meeting of the Recreational Computer Science Society, which is a bunch of geeks in Victoria who get together about once a month to talk about geeky computer stuff. At tonight's meeting I'm doing a very short talk about a ML-derivitive programming language named Alice, so if you're interested, feel free to come out (it's on UVic's campus in the Engineering/Computer Science or ECS building). There are other speakers as well, so get your geek on and come check us out. For more info check out:

http://groups.google.com/group/reccompsci/msg/1b4f49ba7947b616

Tuesday, February 27, 2007

What makes a good programmer?

Saw an interesting article off of Digg today talking about how most programmers can't write code to save their lives. You can find it at:

http://www.digg.com/programming/Why_Can_t_Programmers_Program

(it's currently been dugg, so you might have to check out diggmirror.com) While the article was interesting, there was an interesting comment left on the forums about what makes a good programmer.

It appeared as the following:


"It really all boils down to the fact that, as I see it, there are two types of us programmers.

Type 1: Career Programmer
This is the guy who, when asked what his job is, he says "programming". When you ask what he does in his spare time, he might reply with "I bungee jump, party, get drunk, party, etc etc". Programming is strictly a job, and when he comes home he might not even have a computer. Or he has a computer that is basically used for accessing MySpace. This is the guy FizzBuzz trips up, and he sees no value in it.

Type 2: The "Programming is an Art / Science" Programmer
When you ask this guy what his job is, he'll say "programming". Ask him what he does in his spare time, he might reply with "Well I just finished skimming through the Rails Recipes book. I use .NET at 'work', but I like to learn new languages and technologies when I have some extra time. Oh, I also spend my extra time keeping my blog on .NET Tips & Tricks up to date! By the way, have you read Getting Real by 37signals? It has some great ideas.".

You see, Type 2 is not just a programmer from 9 to 5, he really enjoys what he does and makes it his passion. He's also not simply a person who just likes to bang out code and go home. He takes the initiative to learn new languages. He reads books about the SDLC and methodologies.

This is the guy you want if you want quality. He excels in smaller environments. He's not simply a body filling a position. To find Type 2, ask the candidate what books he's read related to his profession in the last year or so. Ask what tech sites he visits, and why."


I actually disagreed with this description of the "two types". This is one of the most common misconceptions I hear all the time amongst collegues about programmers and what makes a "good" programmer. In my experience type 2 tends to be the stereotypical antisocial computer "geek" who is standoffish, and writes code that is wildly effecient but horribly difficult to maintain and that tends to be viewed as "good enough". They often have difficulty working with others, and just tend to "do things themselves". They have little balance in their lives.

I think there's also a type #3: the programmer who enjoys being challenged with new problems, and while he/she doesn't read an O'Reilley book or learn a new programming language every week, he/she isn't afraid to broaden his/her horizons. He/she often has varied interests away from computing (perhaps has a wife/husband and family). Because of the balance in his/her life, he/she is often able to interact well with others. Takes pride in his/her work, and often while not a prolific code writer, produces code of the highest quality and is always looking to improve code even further. Puts an emphasis on dividing a problem into smaller divisible tasks and has no reservations about handing them out to others.

I think it's type #3 that is the ideal programmer, yet it's type #3 that will tend to be hurt by the common types of obscure questions that are asked in programming job interviews (What's the dynamic_cast operator in C++? What's call by value-result semantics? What's your favourite data structure?)

Programming skill has little to do with acquired knowledge, but rather the ability to problem solve and think critically. Yes you need familiarity with the language in question, but if you cannot break a problem into manageable chunks then you'll never come up with a great solution to it irregardless of how familiar you are with the language.

Monday, July 31, 2006

GamerCard Python script

Thought I'd share this with the world, I wrote a simple little command-line script in Python that given a Xbox gamer id shows the data from the user's gamercard. For example, output for my id (Pedle Zelnip) would be:

Your gamertag is Pedle Zelnip
Your GamerScore is 2510
You are a Silver member of Xbox Live
Your zone is Recreation
The last few games you've played are:
- Geometry Wars Evolved
- GALAGA
- King Kong
- Hexic HD
- Cloning Clyde

No, I'm not a member of the XCDP, this was all done just by examining the public http://gamercard.xbox.com/YOURGAMERID.card URL (of course as a result if MS ever changes the format of gamercards, my code may very well break). If you want to check it out, you can grab it from:

http://www.csc.uvic.ca/~aparkin/python/gamerCard.py

Usage is just "python gamerCard.py userid" where userid is the user you want to check out.