Friday, July 1, 2011

Los Angeles

Well, it looks like daily programming exercises are difficult to fit into my schedule in Los Angeles. Fortunately, I'm programming at work quite frequently (in LispWorks). It feels great to be programming a real contribution to a real AI project.

Anyway, here are a few programming-related thoughts.

Lesson one: OCaml "feels" more productive, but I am actually more productive when writing in Lisp.

There is no getting around it: Lisp is a messy language. OCaml makes me think about my types, whereas in Lisp I just make a function that spits out whatever kind of data is convenient (such as a list of lists of dotted lists, where (of course!) each level has a specific meaning). What's really great about OCaml is the number of errors that can be caught at compile time by the type checker; problems that might take me a minute or several to debug in Lisp. Yet, when I'm writing OCaml code, I actually end up spending so much time trying to decide the types I should use that the advantage disappears. This may be more my fault than OCaml's fault; there is not really a need to put so much information into my declared types... but since it's there, I want to use it.

In fact, I want to put as much information into my types as possible (to catch as many bugs in my code as possible), to the point where I consider switching to programming in a theorem prover like Coq where I can make a formal specification of my program as a type. This is (probably) silly, but makes me think.

The Qi language has a potentially helpful balance... optional type-checking with a Turing-complete type inference system based on sequent calculus, with prolog-like type inference. This sounds very cool to me, and I've installed it and played around just a bit...

The other productivity tool which Lisp has is simply a larger set of list manipulation functions built in. This would be easy to implement in OCaml, of course. (Perhaps a good project.)

An interesting difference between Lisp and Qi/OCaml is the pervasive use of quotation in Lisp. Quotation in Lisp is awesome; that's why I think metaOCaml should be merged with regular OCaml, say. However, in Lisp quotation seems to be needed too often. Forgetting to add a quote is a common error, as we need to quote almost any complicated literal data we feed to a function. This arises from the fundamentally incorrect implementation of lambda calculus that Lisp uses. Quoting is the opposite of evaluation, which seems natural and elegant until you start using other functional languages. Really, the two should be orthogonal: in lambda calculus, *all* data should "evaluate to itself" like numbers and nil do in Lisp. Qi and OCaml use proper lambda calculus, which means that we don't need to quote all over the place. But, since they don't need it so badly, they lack quotation altogether (at least OCaml does... maybe it's just a feature I haven't read about yet in Qi).

Now, a difference between Qi and OCaml is that Qi still uses lists to express function calls, whereas OCaml uses them as just a data structure. I like the idea in OCaml that parentheses are only ever needed to express grouping; it makes the syntax simpler.

I've started using structures more in my Lisp programs, which helps to reduce some of the messiness that I mentioned.

Oh, one more thing. I decided to stop trying to adapt myself to emacs. Instead, I've discovered a variety of tools for Gedit. It's possible to make a keyboard shortcut to run an arbitrary bash script using the "external tools" facility. I made a shortcut to run the current file in a Lisp session that opens in a shell, and a similar shortcut for testing ocaml code.I've also got Lisp syntax highlighting, and paren matching.