Turn OCaml signatures into RPC interfaces


At Skydeck we use ONC RPC to communicate between parts of our server infrastructure. ONC RPC is an old, simple, reliable remote procedure call protocol. It fits well with OCaml since it deals in values and functions, rather than objects and methods, and it has a good implementation in Ocamlnet. However, since all of our ONC RPC clients and servers are written in OCaml, it is a little annoying to have to write interfaces using the somewhat clumsy ONC RPC specification language. It’s nice to be able to stay in the OCaml type system from end to end.
(more…)

Skydeck API and OAuth for OCaml


The Skydeck API had only been out a few days when someone began a Ruby binding for it. We’re thrilled that there’s been so much interest already, and look forward to some great applications built against our API.

Of course, we like OCaml here, so we are pleased to announce the release of an OCaml binding to the API. You can find it on our developer downloads page.

As part of our API design we decided to use OAuth, so you can allow applications to access your data without giving away your Skydeck username and password. We are also pleased to announce the release of ooauth, an OCaml implementation of OAuth.

Ooauth implements both the consumer and service provider parts of OAuth (we use it in our server and in the API binding above), so you can use it to implement your own API or to consume the many APIs that use OAuth, such as the Google Data APIs. It is also available on the developer downloads page.

More stack traces in OCaml


Following up on our earlier patch, here is a patch to show backtraces in the OCaml top level and for dynamically-loaded code (this patch subsumes the previous one). This feature is often requested on the OCaml mailing list, and we have found it very useful in development to get a quick idea of where something is breaking.

(more…)

ocamljs, OCaml to Javascript compiler


It can be hard to go back to a conventional language once you’ve enjoyed the freedoms of typed functional programming. That’s why, when we needed to write some Javascript at Skydeck, our first inclination was to find some way around it. Javascript is not such a terrible language—it has higher-order functions at least—but we missed the safety of static type inference and OCaml’s expressive features like variant types and pattern matching. So instead of writing Javascript, we wrote ocamljs, a back end to the OCaml compiler that generates Javascript.

(more…)

Stack traces in OCaml


One annoyance of OCaml (compared to Java or most any of the scripting languages) is that it does not automatically print a stack trace when your program throws an exception. To get one you have to compile with -g to include debugging information in the executable, and launch the runtime with -b so that it captures a stack trace on an exception. By default the runtime doesn’t spend time capturing the stack trace; this reflects a design choice to make exceptions very lightweight. Because exceptions are cheap it is good OCaml style to use them freely as a control-flow mechanism, while in most other languages such use is discouraged—exceptions are meant to be used only for error handling.

Once you’ve -g and -b‘d your way to a stack trace you can only get it printed to standard error, and then only if it reaches the top level. This is usually fine for a batch program, but for a long-running program like a web server it is a severe restriction. So we’ve written a small patch to the OCaml runtime that lets you get the stack trace as a string so you can show it in a web page, log it, etc. You can also turn the capturing of backtraces on and off inside the program so you incur the cost only when you need it.

(more…)

OCaml for the recovering Java programmer, part 1: objects and subtyping


The late, great Numo the hedgehog, courtesy of loosetooth.com

It’s said that the fox knows many tricks, but the hedgehog knows one big trick. If Java is the hedgehog, with objects as its one big trick, then OCaml is the fox, with lots of different tools for structuring code. Many of the things you’d use objects for in Java have simpler, cleaner, or safer alternatives in OCaml: tuples and records for structuring data, higher order functions in place of one-method anonymous inner classes, parametric polymorphism for collections instead of pervasive downcasts (although this has improved with the introduction of Java generics), functors and signatures in place of (compile-time) parameterization of code with interfaces.

Nonetheless, sometimes you want objects—as I did recently when interfacing with some object-oriented native code—and you can get them in OCaml too (objects are of course the O). But they aren’t quite the objects that you’re used to in Java. In Java, you can put two objects with a common superclass into a single List. I tried to do that in OCaml and got a mysterious type error. It took me some time, a little research, and a little profanity, but I got my code working and learned some things. (more…)

Unit test in OCaml with OUnit


It’s pretty easy to get overly confident about the correctness of your code when you program in OCaml. After all, the strict type checking eliminates whole classes of bugs at compile time; you never fight them because your code never builds with them. Even so, I still love running automated tests. The compiler can’t catch all boundary conditions and it certainly can’t verify any situation that is dynamically built at runtime.

At Skydeck, we use OUnit for our unit tests. It’s really straightforward to use because, not surprisingly, it follows the typical n-unit pattern: set-up and tear-down test cases, implement individual tests, and gather them all into suites for execution. Similar to other implementations — cppunit for example — you need to write your own test suites with OUnit. It’s repetitive work, ripe for automation and almost makes you miss reflection (but not quite). So we threw together a module to automate this and hooked it into ocamlbuild. Now adding a new test is simple: we just write new test functions and OCaml does the rest. I love being able to add tests quickly even more than I love that feeling when all the tests pass.

Below is the code that generates the test suite. If you’re using ocamlbuild and want to integrate with it, I’ve included the relevant sections from our myocamlbuild.ml too. (more…)

Start Hacking in OCaml in 5 Steps


hacksaw.jpgWe love OCaml at Skydeck, as Jake has written about before, and we often hear from programmers with questions and comments about it. Some love OCaml, some don’t know anything about it, but many have it on their list of languages to try yet haven’t gotten around to it. Well, it’s pretty simple to get up and running — just download, build, and start hacking!

Really, it’s that simple. If you want to get there as quickly as possible, follow these five steps:

(more…)

Sailing the ship of state


Apollo LEM returning from the moon

In fanciful moments I like to think of a running program as a little spaceship following a trajectory through the computer’s “state space”–the space of possible values of each of its memory locations. This is a big space (2 to the power of the number of bits of memory in the machine) so easy to get lost in. The problem of writing a correct program is how to keep the spaceship on course.

If the ship is on course it’s easy to move it in the right direction. If it’s gotten off course, we have to figure out where it is and make some correction to get it back on course. If we don’t realize it’s gotten off course we’ll move it in what we think is the right direction, and probably make things worse. If we get too far off course we might not be able to recover at all. We’re better off staying on course in the first place.

(more…)

Languages for talking to phones


There are a number of runtime environments available or in the works for open phone platforms (e.g. Python, Perl, Ruby, Java). Instead of using one of these, we decided to port a runtime for a language you may not have heard of, called OCaml.

If you’re interested in writing code faster and with fewer bugs, you should give OCaml a look. Its advanced type system prevents whole classes of bugs in the same way that garbage collection does. We like it so much that we use it whenever we can.

At first we thought that “whenever we can” meant only on the server; we thought the time it would take to port OCaml to phone operating systems would be prohibitive. However, after a little investigation, we decided to port the bytecode runtime to the Nokia S60 platform. It is turning out to be easy. The runtime is a beautiful piece of code and very portable.

Doing our own port gives us the same degree of control as writing native code, while retaining the productivity advantages of a runtime environment. It also helps maintain portability across different platforms.

We expect OCaml to make a big difference in our ability to get working software out the door. Programming everything in C++ seems insane by comparison.