Saturday, January 19, 2008

Saturday, January 12, 2008

Haskell: Well-typed Programs Can't be Blamed

Last Wednesday, I went to a Bay Area Functional Programmers talk to see Philip Wadler give a talk on his paper Well-typed Programs Can't be Blamed.

Wadler was a good speaker. He was very friendly and interactive. He insisted on having people ask lots of questions. He called himself an "absent minded professor". Two things I didn't know about him were that he worked at Bell Labs and he also worked at Sun on Generic Java.

Having tried to cram technical papers into my head way too late at night, I was pleased to find out that he's pretty easy to understand in person. However, his slides weren't always bug free. There were a couple of errors in the equations. Apparently, academic papers can have bugs too ;)

The goal of the paper was to to mix typed and untyped modules in the same program. He admitted that untyped code lets you do fun and flexible things. His goal was to "coerce" a dynamic function to a typed function dynamically. This was done with a "contract", which is like a type declaration for a function that get applied dynamically.

I mentioned to him that Python 3000 had type annotations, and that Guido had envisioned that someone would come along and do type assertions in very much the same way contracts work. He admitted that people were using these techniques in the real world. In fact, the new version of JavaScript will have optional types. I forgot to bring up that Visual Basic had optional types years ago.

It's interesting to note that several people in the audience were interested in using Python and Haskell together in the same program. By the way, rather than calling Python an "untyped" language (which is clearly an insult to its dynamic nature), he called it a "unityped" language, because, basically, everything is an object.

A core theme of the paper was "the blame game": is it the function's fault, or the callers fault? Apparently, this is a normal part of talking about function semantics, but I had never heard it referred to as that before.

He extended his system to cover dynamic type systems (like Lisp), strict type systems (like Haskell), and super type systems (aka dependent type systems) all in the same program. A dependent type system lets you have types such as "int n where n > 15". Consider the ramifications of this. If you have two such numbers, and you subtract one from the other, you might end up with an int that doesn't match that constraint. Often, type constraints such as these must be enforced at runtime. What you end up with isn't so different from design by contract.

One interesting point he made is that the stronger your type system is, the braver you can be in relying on it. In a dynamic system, you might stick with simpler code because there's no compiler to catch your mistakes before running the code. This is an interesting counter-point to Bruce Eckel's argument that declaring your types in Java is a waste of time that prevents you from doing more useful work like writing unit tests. After all, you need to write unit tests anyway, since a type system can only catch a certain class of bugs.

Wadler admitted that even Haskell's type inferencer wasn't smart enough to figure out some of the things he wanted to do. He said that he felt himself sometimes wishing he could just tell the compiler to fall back to checking his types at runtime.

Someone in the audience was able to state that Wadler's conclusions applied equally well to languages that have assignment. His response was "Oh, that's too bad" ;) Apparently, that was a quote from some other computer scientist who was told that his research applied equally well to languages with a goto statement ;)

There was a point thirty minutes into the talk where some people started to get lost. However, there were a few people that understood everything he said and wanted more. I understood about 60% of what he wrote and 75% of what he said. I think that says a lot about how good a speaker he was.

His talk was very proof oriented. He thinks of himself as a mathematician. He brought up the fact that lambda calculus is isomorphic to proof reduction: "Programming isn't so arbitrary". This flew in the face of some of the things I've written, including Everything Your Professor Failed to Tell You About Functional Programming and Computer Science: What's Wrong with CS Research.

In thinking about interpreter design, I like to focus on what is useful, without regard to any similarities in the math world. (By the way, I am a mathematician too.) I have a feeling that computer science academics get too caught up in the math. Nonetheless, clearly, applying mathematical constraints can often be useful, and I found Wadler's point of view interesting.

Scala is a functional language, similar to Erlang, built for the JVM. Someone asked Wadler if he had looked at Scala yet. Clearly, Wadler has a functional bias ("It's what I prefer to work on."), and one big difference I discovered between Haskell and Scala is that Scala lets you interact with stateful Java libraries. After all, Java is very stateful. I could see how that would be a big turnoff for Wadler. Wadler's response was that clearly there's a need for state. A database of some kind is needed to hold long term state. He seemed happy with what Erlang had done with Mnesia. However, he felt that state was a "high level concept" and that it's better to keep low-level programming and concurrent programming stateless.

Agree or disagree, I definitely think Wadler's an interesting guy, and I'm glad I was able to go to his talk.

Wednesday, January 09, 2008

Computer Science: Everyone Wants to Write a Web Framework

I had my stint. I started a Python Web application framework called Aquarium seven years ago. It's now being used by IronPort all over the world. It's even been used in a couple apps at Yahoo.

In the Python Web world, we have about 1.5 times as many frameworks as we have programmers. I've heard people joke that you can't be a serious Python programmer until you've written your own templating engine in Python ;)

Ruby became mainstream, not thanks to its cool metaprogramming, but rather because DHH decided to write an app in it. Oh, and he also wrote some obscure Web framework that no one knows about ;)

One of the most "vocal" blogs supporting Erlang is Yariv's Blog. Erlang is wicked cool, so what does he do with it? He wrote a Web framework called ErlyWeb.

My buddy Alex Jacobson is a fascinating character. He's been coding in Haskell for almost as long as I've been coding in total. What's his project? HApps. It's labeled an "application server", but every time I see him playing with it, he's writing Web apps.

Surely, deep in the bowels of academia, there must be someone not interested in Web development. Someone, perhaps who only cares about computer science theory, right? For instance, Philip Wadler. He's made numerous contributions to Haskell and functional programming in general. He's presently a professor of Theoretical Computer Science in the School of Informatics at the University of Edinburgh. Surely he must be blissfully ignorant of this whole Web framework craze!

Nope, he's currently working on a new functional language designed for writing web applications called Links.

Thursday, January 03, 2008

Python: Oh How Long I've Known Thee

I have a buddy who was doing some Python programming with Oracle today. He has asked me a question, and after I thought about it, I said, "Hey, that sounds familiar. I think I have some code for that." I knew it was in my open source project, Aquarium, but I also knew it wasn't in newer releases. I eventually found the code and gave it to him.

Here's the punchline: It was from 2001! Yes, I've been coding Python that long.

In case you're wondering what it was, the Oracle driver doesn't have code to fetch rows as dicts, so that's what I was giving him. Here's what it looks like:
    def fetchonedict(self, cursor, force=1): 
"""Fetch one row as a dict.

If force is 1 and there are no more rows, I'll throw
an EXPECTED_RECORD_NOT_FOUND exception. Otherwise (if
force is 0 and there are no more rows), I'll simply return
None. Note, database modules don't *have* to use this
function, I'm just providing it for convenience.

row = cursor.fetchone()
if not row:
if force: raise self.EXPECTED_RECORD_NOT_FOUND
else: return None
fieldCount = len(cursor.description)
retDict = {}
for i in range(0, fieldCount):
# Note that the 0'th element of each tuple in
# cursor.description is the col name.
retDict[cursor.description[i][0]] = row[i]
return retDict
Aside from the camelCased variable names, that ain't bad for a piece of seven year old code!

Vim: Best Vim Article Ever

Why, oh WHY, do those #?@! nutheads use vi?

Wednesday, January 02, 2008

Vim: Gimme More Magic

I spent an hour reading about TextMate to get a feel for what I'm missing since I know it's so popular. I'm sure it's a lovely editor, but I have such an emotional attachment to Vim, Linux, free software, etc. Anyway, I'm sure you're tired of hearing me talk about that ;)

Anyway, I tried out the snippetsEmu plugin. It's a plugin that provides TextMate style snippets. Once you have it installed, you can edit a Python file, type "def<tab>" and it'll start doing interesting things.

I can see the appeal of snippetsEmu for more verbose languages. It knows how to type "public static void main" ;) However, it's hard to get excited about it in Python. Python has such low syntactic overhead that it's faster for me to just type things out, especially considering I'm a touch typist. It'd probably make more sense in Ruby, where you have to type "end" all the time. Nonetheless, I'm happy to have someone tell me how to use snippetsEmu more effectively.

I also tried out xmledit. That one's a keeper. Once you have it installed, you can type "<p><tab>" and it'll automatically add "</p>". Hit ">" again, and it'll even put the two tags on separate lines and position your cursor between them. Very nice! The one trick to remember though, is that when you install xml.vim into your ~/.vim/ftplugin directory, remember to symlink it to html.vim so it works for HTML too.

Tuesday, January 01, 2008

Python: Django: SQL Queries in the Templates

I think Django is a great framework for Python Web newbies. Those guys have done a great job providing a cohesive framework that's easy to get started with. However, one thing that has always bothered me is the templating system.

I was reading a bit of someone else's Django code, and I came across this in a template:
{% for service in %}
<li>{{ }}</li>
{% endfor %}
There are two problems. First of all, "" is not being HTML escaped. That means you have to worry about XSS vulnerabilities. Thankfully, I no longer need to worry so much about things like that because Genshi can auto-escape things smartly.

However, there's a much graver problem. "" is really a SQL query.

Django tries really hard to provide a restricted templating environment because it assumes template writers aren't programmers. This assumption drives me nuts because I do both the back end and the front end. A naive template writer could easily write things like "" in an inner loop, such as listing all the services of all the servers. The multitude of small queries could wreck havoc on the database if there were a ton of servers. A smart programmer could replace all those queries with a single query done ahead of time.

My point is that Django's restricted templating system handcuffs me and prevents me from easily writing functions to avoid duplicating markup while at the same time leaving enough rope for naive template writers to hang themselves.

I'm so much happier with Genshi. A smart programmer + a smart templating system = less code duplication. I might be a perfectionist with a deadline, but I also have to maintain my code, so DRY still applies.