8

I want to learn and use ML but there are many compilers out there. I need:

  • speed
  • low memory usage
  • threading
  • mutable arrays and record types
  • continuations
  • ready for production code
  • easy ffi
  • up to date
  • ... helpful libraries
  • portable to some degree

I do not need:

  • objects
  • GUI
  • support for windows

For example I found OCaml, OCaml Batteries include and MLton. Any advice for me which one should I choose?

knivil
  • 916
  • 5
  • 10

3 Answers3

9

SML has better support for continuations. If you have a strong need for continuations, it is a better choice; OCaml has no in-language support for them, but there is the Delimcc library by Oleg for delimited continuations; being external, it is probably slower than SML's implementations. Otherwise, OCaml seems to have more traction these days, so you should expect the tooling and libraries to be slightly better.

More generally, SML tends to be rather more elegant, while OCaml has some more advanced features, quirks, and users. Performance-wise they're both good.

(See this for a syntactic comparison of the mostly-common fragments of both languages; of course OCaml's objects and polymorphic variants are not presented.)

gasche
  • 31,259
  • 3
  • 78
  • 100
  • +1 "More advanced features, quirks, and users." (and for generally-good answer) – Michael Ekstrand Mar 05 '11 at 20:02
  • 1
    Also, @knivil, you should know that OCaml's threading support does not support parallel compute threads at this time - there is a lock that keeps multiple threads from running in parallel. OCaml threads are still quite useful for I/O (standard I/O modules release the thread lock when blocking for IO), and C or FORTRAN code can run in its own thread while other OCaml code runs. For true parallelism/concurrency, there are Plasma (distributed map/reduce), Functory (parallel computation), and OCamlNet 3 has multiprocessing facilities. – Michael Ekstrand Mar 05 '11 at 20:06
5

Ocaml satisfies all the requirements from your list with the exception of continuations. It is fast, memory efficient, portable, has posix threads bindings and good libraries for lightweight cooperative threads. The standard library is pretty limited but there are many third-party libraries.

There are many companies who use OCaml in production for all sort of things. Some of the companies are mentioned on the Caml consortium website.

I also recommend reading some excellent OCaml experience reports:

OCaml at Jane Street Capital

OCaml at XenSource

alavrik
  • 2,151
  • 12
  • 16
  • 2
    I'd use Ocaml as well, even though I wrote code for MLton back in the day. The point is that SML is the elegant and academic language, where OCaml is a beast that gets work done in practice. So it depends on what you are trying to do. – I GIVE CRAP ANSWERS Mar 04 '11 at 17:28
5

Well for MLton:

  • Speed
    • The whole program optimisations definetley give you better speed than OCaml, however the penalty you serve for each and every recompile can be quite annoying. See this link for a simple informal comparison of MLton and OCaml
  • Threading
  • Mutable arrays and record types
  • Continuations
  • ready for production code
    • Obviously depends on the libraries you use. But the basis library is solid, however MLton has its own additions and a few others have repos with extensions listed here. However the MLton library project contains some nice code. However as with many of such libraries/extensions, they tend to rely on other extensions made by them self and thus it is hard to only use small parts of them.
  • Easy ffi
    • ForeignFunctionInterface and NoLonger-FFI are great. I have however had speed issues with the NL-FFI on big C codebases, which I think is due to MLton not removing the phantom types complety. However this is just a guess, I might have done something wrong.
  • Portable to some degree
Jesper.Reenberg
  • 5,944
  • 23
  • 31
  • 1
    Note that MLTon, while it has "threads", doesn't actually execute those threads concurrently. They all execute in a single OS thread. Thus, you get no benefits from multiple CPU cores. – Nate C-K Jun 21 '12 at 21:49
  • even the mlton.org page about ocaml admits that ocaml compiles faster than mlton: http://mlton.org/OCaml – johnbakers Oct 15 '16 at 10:24