
OCaml for the Masses (2011) - luu
http://queue.acm.org/detail.cfm?id=2038036
======
gnuvince
I recently picked up a project in OCaml that I had begun last summer and never
finished. I have to say, compared to my day-to-day tools (mostly Java these
days, Python before), OCaml is a breath of fresh air. For instance, today I
did a refactoring where I replaced a type that was simply an alias to string
with a proper abstract data type. Fixing the code was simply a matter of
compiling, going to the file/line number in the error message, fix and repeat.
Algebraic data types (sometimes called discriminated unions) are great for
properly separating the logic of a data structure instead of sticking
everything into one big class.

I highly recommend you give it a try. By the way, although we hear about OCaml
less often than other languages, it is still used in many well known places
such as Facebook (for pfff and Hack), Microsoft (SLAM, a verification tool for
drivers), and Airbus (they use the ASTRÉE static verification tool which is
written in OCaml).

~~~
brianberns
> Although we hear about OCaml less often than other languages, it is still
> used in many well known places such as ... Microsoft

Microsoft's F# language is essentially a .NET version of OCaml.

~~~
_pmf_
> Microsoft's F# language is essentially a .NET version of OCaml.

Oh, how I wish there was a JVM variant of F# (no, Scala does not count).

~~~
timclark
Scala came at the problem from the wrong direction. F# started with the
functional pieces of ML/OCaml and added the 'OO' stuff in record types
resulting in a simple and powerful language. Scala seems to have started with
the 'OO' stuff and then has kept on adding and adding and adding.

~~~
frowaway001
That's ... nonsense?

You know what's the defining property of OCaml's OO/module systems?

That nobody actually uses it.

Compared to that, Scala tries to make all parts of the language work in an
orthogonal and consistent fashion.

In OCaml, one part of the language is crippled intentionally, due to what
seems to be ideological reasons.

Apart from that, I'm unsure how OCaml counts as "functional". It doesn't even
have typeclasses, let alone higher-kinded types.

~~~
chriswarbo
> I'm unsure how OCaml counts as "functional". It doesn't even have
> typeclasses, let alone higher-kinded types.

Erm, what? Functional languages are those which implement as much as possible
using functions, rather than adding language primitives (eg. if/then/else,
looping, mutable state, etc.). If anything, typeclasses make a language _less_
functional, since they're a language primitive which could be implemented with
functions instead (by passing records explicitly).

Lambda Calculus doesn't have typeclasses or higher-kinded types, does that
mean it doesn't count as "functional"? Hell, even Joy is a purely-functional
language and it can't even call functions!

Type systems are a completely orthogonal concept to functional style; they're
a way of embedding a formal logic into a programming language. It just-so-
happens that many logics make heavy use of implication (a -> b), and that
corresponds to functions. It's possible to give a rich type system to, for
example, Prolog or assembly, but the logical operators would be quite
unfamiliar.

~~~
lmm
In Scala typeclass is not a language primitive, it's just an idiom that can be
easily expressed in the language. Whereas programming in the typeclass style
with F# is difficult to say the least, basically requiring Greenspunning.

"Functional" means many things to many people, but it's the term a lot of
people use when talking about the recent rise of languages like Haskell,
Scala, F# and OCaml - much of which rise is, IMO, attributable to their type
systems. Whatever "it" is that these languages have (and sure, it might be
more accurate to talk about "languages providing ADTs and controlled
sequencing of effects" or some such), I think it's fair to say that OCaml/F#
have less of it in this area, because their type systems don't allow you to
express higher-kinded types.

F# may have its advantages, but you're definitely missing out on some of the
"functional language renaissance" \- because whatever the terminology, at
least some of that renaissance is about the value of powerful type systems.

~~~
theseoafs
> "Functional" means many things to many people, but it's the term a lot of
> people use when talking about the recent rise of languages like Haskell,
> Scala, F# and OCaml - much of which rise is, IMO, attributable to their type
> systems.

These are new languages in the functional paradigm but functional programming
isn't itself a new concept, and strong static typing has never been a
prerequisite in order to be "functional". The first functional language, Lisp,
was dynamic, and to this day no commonly used Lisp has been statically typed
out-of-the-box. Erlang and the array languages (APL, J, etc.) are other
examples of dynamically typed functional languages.

You can say that functional languages are _more likely_ to have strong type
systems than other languages, but it's totally disingenuous to claim that
languages are somehow "less functional" because they lack typeclasses and
higher-kinded types.

~~~
klibertp
To be precise most array languages are not functional, although they do
support "function-level programming" \- the distinction is not clear to me
just yet, but I hope to improve. :)

The K language, which presumably incorporates parts of Scheme, is a notable
exception to this.

~~~
theseoafs
Unless you're going to argue that Wikipedia's classification is entirely
wrong, the array languages are certainly functional.

~~~
klibertp
On the J Wikipedia page there's this sentence:

Like the original FP/FL languages, J supports function-level programming via
its tacit programming features ( _note that function-level programming is not
the same as functional programming_ ).

As I said, it's unclear to me what the difference is, but that's what I based
my previous comment on.

~~~
theseoafs
That sentence only means to say that J supports function-level programming,
which itself is different from functional programming. The very same Wikipedia
page also calls J a functional language -- the fact that it supports function-
level programming doesn't contradict the fact that it also supports functional
programming.

------
cordite
As a haskeller, I've heard some disconcerting things about OCaml.

Namely, the standard lib is more or less made just to be able to compile
itself, and that the standard lib is not pure, which makes developers go to
alternate implementations to do the job in a way that is expected.

Please do correct me if what I've heard is wrong.

My only indirect experience with OCaml is the Opa language compiler--so all
I've seen is that it is a language favored for implementing languages.

~~~
yminsky
(Author of the article here)

The stdlib that ships with the compiler is indeed minimal, though it is used
for things other than the compiler. It can be used for other projects, but you
probably want something more full-featured. Core, and the more minimal and
portable Core_kernel
([https://github.com/janestreet/core_kernel](https://github.com/janestreet/core_kernel))
is a full-featured alternative that is growing in popularity, and is what the
book I worked on (Real World OCaml,
[http://realworldocaml.org](http://realworldocaml.org)) is based on.

OCaml (and most of both the stdlib and Core) default to immutable data
structures, but OCaml has good support for programming imperatively, to its
credit, in my view.

~~~
jzelinskie
I've heard that Jane Street uses their own extensions to the language. Also,
how does one deal with the development of OCaml being largely done in French?

~~~
masklinn
> Also, how does one deal with the development of OCaml being largely done in
> French?

OCaml is developed by french people, I've never heard of it being done _in_
french (aside from interpersonal banter I guess). The french version of the
official site doesn't even work correctly (when you click on the "manual" link
you get the english manual)

~~~
kd0amg
_I 've never heard of it being done in french (aside from interpersonal banter
I guess)_

And an April Fools joke --
[http://gallium.inria.fr/blog/ocaml-5/](http://gallium.inria.fr/blog/ocaml-5/)

------
tieTYT
What made me give up on Haskell was all the libraries had native linux
bindings. It was hell reusing code if you're developing on windows (and I've
heard this is true on macs, too). Does OCaml have this issue?

~~~
yminsky
Unix support is definitely better than Windows. That said, the core language
and base libraries are pretty portable. Here's one nice retrospective on a
move from Python to OCaml for a program that needed portability to Windows as
a fundamental concern.

[http://roscidus.com/blog/blog/2014/06/06/python-to-ocaml-
ret...](http://roscidus.com/blog/blog/2014/06/06/python-to-ocaml-
retrospective/)

Probably the biggest problem on Windows is that OPAM, the package manager,
doesn't work there. That will come eventually, though.

~~~
shadowfox
> Probably the biggest problem on Windows is that OPAM, the package manager,
> doesn't work there. That will come eventually, though

Wodi [1] is a reasonable choice until OPAM starts supporting Windows. It
includes many of the interesting/useful libraries (including, importantly,
batteries [2] and core [3])

[1] [http://wodi.forge.ocamlcore.org/](http://wodi.forge.ocamlcore.org/) [2]
[http://batteries.forge.ocamlcore.org/](http://batteries.forge.ocamlcore.org/)
[3]
[https://github.com/janestreet/core_kernel](https://github.com/janestreet/core_kernel)

------
skybrian
This is from 2011. I wonder how OCaml implementations have changed since then?

~~~
yminsky
The biggest change is OPAM, [http://opam.ocaml.org/](http://opam.ocaml.org/),
a top-notch package management system that makes installing OCaml packages
much easier.

Shameless plug: there's also a newish O'Reilly book, Real World OCaml
[http://realworldocaml.org](http://realworldocaml.org), which I think is a big
help in learning the language.

Oh, and there's OCaml Labs, a new lab at Cambridge University that's dedicated
to improving the language.

And of course the compiler is constantly making progress. The upcoming 4.02
release is a pretty fun one, which I documented here:
[https://blogs.janestreet.com/ocaml-4-02-everything-
else/](https://blogs.janestreet.com/ocaml-4-02-everything-else/) And before
that, changes like GADTs and first-class modules landed, which have been quite
useful extensions.

Really, it's a very active and fun community these days. The language is
getting better quite quickly, but mostly in conservative and tasteful ways.
The people in charge of the core language have been doing a great job, and the
community infrastructure (things like OPAM) have been making big strides as
well.

~~~
girvo
OPAM is awesome, made it super easy to install "Batsh"[0], a neat compile-to-
bash-or-BAT language. Makes programming in shell not painful :D

[0] [https://github.com/BYVoid/Batsh](https://github.com/BYVoid/Batsh)

------
jmgrosen
I've often seen OCaml compared to F#; as someone who knows Haskell and Rust
(among others), which is better to learn? I'm not really a Windows guy, which
makes me think OCaml, but I've seen that Xamarin now works with F#, which is
appealing to my inner app developer.

~~~
bsamuels
I prefer F#, but only so because it has decent visual studio tooling and .net
interop. F# power tools' refactoring is great, and its syntax coloring is very
good as well.
[http://puu.sh/a1vM5/c735f3d319.png](http://puu.sh/a1vM5/c735f3d319.png) (i
swear this looks a lot nicer on the washed out screen i program with)

The interop with .net libs is helpful too - but i've found that if there's
anything that's really hurting F#, it's all the OOP tacked onto it to make the
.net interop work. Decent tradeoff if you ask me, but it's cringeworthy to see
people using F# like an oop language when the oop only appears to be there for
the interop magic to work.

------
simplify
I considered picking up OCaml at one point, but got scared off by the global
interpreter lock. Can an experienced OCaml dev tell me when this would really
become an issue, and certain cases where it wouldn't make a difference?

~~~
yminsky
My experience has been building parallel and distributed programs using
multiple runtime instances that communicate via message passing. There are
libraries that help automate this, link our own Async_parallel
([https://blogs.janestreet.com/async-
parallel/](https://blogs.janestreet.com/async-parallel/)). One advantage of
this approach is that it scales beyond a single box.

There is work going on at OCaml Labs on a parallel runtime. I suspect it will
be useful, but in the end, message passing is I think a better idiom than
shared memory threads for parallel programming. When the true parallel runtime
lands, I'm not sure that we'll actually use it much for running truly parallel
threads.

~~~
mercurial
> I suspect it will be useful, but in the end, message passing is I think a
> better idiom than shared memory threads for parallel programming.

It really depends on the problem you are working with. You wouldn't want to
make a high-performance HTTP server this way. Especially if you get to copy
2MB of POST data every time.

~~~
yminsky
For an HTTP server you should use Async or LWT, which are lightweight
concurrency libraries. You can handle quite high levels of concurrency with
either one. My impression is that people building highly parallel HTTP servers
do quite well with collections of processes with a load-balancer in front, but
it's not my area of expertise.

------
rrss1122
I tried OCaml once. It was to figure out the algorithm Yaron Minsky (author of
linked article) used for his orbital simulator, Planets. I only learned enough
to port the code over to JS, and quickly forgot it. Coming from a procedural
programming background, the learning curve was too steep for OCaml.

Here is Minsky's Planet's project, by the way:
[http://planets.homedns.org/](http://planets.homedns.org/)

------
ALee
We're also using OCaml at Esper. Here's our implementation for folks who are
interested in using functional programming lagnuages:
[http://stackoverflow.com/questions/19350590/designing-
large-...](http://stackoverflow.com/questions/19350590/designing-large-
projects-in-ocaml)

------
robert_nsu
I've played with OCaml in the past, but I decided to get comfortable with F#
then give OCaml another go.

------
hackerboos
We were given this by Dr Grant Malcolm when studying semantics in the
University of Liverpool.

Although I didn't run off and learn OCaml at the time it did spark my interest
in functional programming - eventually picking up Scala and Erlang.

