Wednesday, April 29, 2009

Getting started with OCaml

A friend with some experience with SML asked me how to get started with OCaml, so I thought I'd write this incomplete guide.

Apparently, the internet also has a "Getting started with OCaml" guide. My guide will remain competitive by being much more concise (and therefore, incomplete).

Steps to getting started:
  1. Download an OCaml compiler from somewhere. Other useful things to have are ocamlfind (which finds your OCaml libraries), ocamllex (for lexing), and ocamlyacc (for parsing). Another useful thing is OCamlMakefile, which helps you manage builds with Make.
  2. Read a tutorial or two. This is a good thing to read. Also, bookmark the Index of modules.
  3. Write some programs while compiling frequently. (Compiling too late in the game may cause death of the Ocaml experience.)
  4. If you want to lex and parse, look into ocamllex and ocamlyacc.
Some details:
  • OCaml is very similar to SML. The only different parts are loops and things like that. I try to stay away from those because they are creepy.
  • The file extension is .ml; the file extension for interfaces is .mli. You don't need an interface file; without one you export everything by default.
  • The contents of file somefile.ml are in the Somefile namespace, which you can open with "open Somefile." By default all of these little guys are exposed. You can hide things by creating modules and writing module interfaces. Read more about that here.

Some important things to know:
  • OCaml is the future because (arguably) a lot of people are using it. It is the new big thing for scientific computing because you can actually get good performance out of it!!
  • OCaml is more practical than Haskell for some things and also yields better performance more easily.
  • One thing OCaml is not great for is backwards compatibility. Each new release of OCaml supposedly breaks a lot of things. From my understanding a lot of libraries change/break, causing everyone to experience organ failure.
How I learned OCaml:
  1. In fall of 2006, I spent a long time struggling with the MLton SMLNJ compiler. It gave me lots of obscure errors and I cried.
  2. From fall of 2006 to spring of 2007, I came to not only be master* of the MLton compiler but also the Moscow ML compiler. These compilers are not only difficult to use but also often difficult to install, so you should be very impressed.
  3. After learning SMLNJ was not really being maintained anymore, I said "screw this shit" and moved on to Haskell.
  4. In January of 2009, I realized** that OCaml is the future. For my next project I decided to use OCaml, which I picked up in an ad hoc ways throgh the steps above, skipping step 2 (but compiling very frequently). I am still in this step.
* This is subjective.
** Through a combination of talking to people about what people used for different things, trying to do 3D arrays in Haskell, and meeting myself at Xavier Leroy.

4 comments:

Joe said...
This comment has been removed by the author.
Jesse A. Tov said...

I would strongly recommend changing your first step to install OCaml using GODI. I didn't know about GODI at first, but having it makes everything way easier. OCaml is generally not so good about infrastructure, and their solutions tend to be ugly and ad-hoc, but GODI is still a huge improvement over findlib alone. (Nothing about the OCaml experience seems very much designed toward elegance.)

On the other hand, I found OCamlMakefile a pain in the ass. Some people swear by ocamlbuild, and I suspect if you're doing the kind of things that are anticipated, it makes those things easy. Generally I find myself not doing only those things, in which case writing a Makefile manually may be easier than the hassle of OCamlMakefile. But that does require understanding make ;-)

I second that ocamllex and ocamlparse are awesome. I have heard rumors of a replacement for ocamlparse that is even more awesome, but I have no first-hand experience and can't find its name right now.

"OCaml is very similar to SML. The only different parts are loops and things like that." That may be true if your friend isn't an SML expert, but otherwise, there are some differences in the module system that SMLers I know consider salient. OCaml also has polymorphic variants, which are useful and worth understanding, and objects, which all true Camlists disdain.

I have found that OCaml's lack of ad-hoc polymorphism makes some programming tasks troubling and verbose. No, functors are not enough. I have found that Haskell's lack of viable syntactic extension mechanisms make some programming takes troubling and verbose. No, laziness is not nearly enough.

I think both are "the future," though I've noticed some French people using Haskell of late, if you'd take that as a sign.

ChriS said...

Hi Jean,

Hopefully your OCaml skills got better and better and you like it even more now!

Since your links doe not work, I just wanted to mention the site http://ocaml.org/ which is the main portal of the language with many resources. You (everybody) is very welcome to contribute.

Also, the package manager "opam" makes it very easy to install compilers, libraries and programs made by a dedicated community (IMHO it is better than godi). Note that oasis makes the compilation and distribution of libraries very easy (it uses ocamlbuild in the back but hides most of its complexity). The infrastructure is definitely improving.

Finally, when you say "One thing OCaml is not great for is backwards compatibility.", I have to disagree. I think backward compatibility is a string point of OCaml and the features that may not be stable are clearly labeled "Language extensions" in the manual.

xcod4r said...

Icons in C and Mouse programming
c programming samples