Why use OCaml over Python

Hi Folks,

A bit of background. I heard about OCaml long ago, but I’ve never looked at it properly. Then Facebook released the Flow for javascript and ReasonML. It was then I realised the power of the OCaml’s type system. Over the years, I’ve done some Clojure and Elixir, so I am familiar enough with functional programming, and I like it a lot.

But I’ve been a software developer professionally for nearly 15 years. Though I’ve flirted with other languages, the primary language I have been using is Python. And it’s getting more and more popular. I am working on a new web app backend. It’s a new project I would like to get it off the ground soon, and it’s AI related. As we all know, Python is well-positioned in this space.

Yet, I have been learning OCaml in the last few days :slight_smile: I really want to use OCaml as API backend for this product. But at the same time, it’s such a paradigm shift since I have been mainly using Python and Python as a supported ecosystem in the LLM space.

So I want to hear the folks who used OCaml in production to know the pros and cons of using OCaml in prod. And hope you can convince me to use OCaml? I know, as any seasonal dev would say, use the tools that suit the job and use the tools you know the best. But my heart keeps telling me to learn OCaml :smile:

8 Likes

Hi tianyi,

I am in a situation not unlike yours (18+ years of python but an eye on OCaml / Haskell for as long) and started 3+ years ago using elm on the front end. It is certainly delightful to have such a nice type system and a compiler that flags everything wrong very quickly. Now I have been watching the OCaml space for some time, with the same idea as yours (why not use it instead of Python for the backend side ?), and though I still want to get there at some point I have come to realize how radical a change it will be. Having a 6 months break from normal work duties would certainly help !

Hi @zogzog,

Glad to hear that I am not the only one. It’s very strange that OCaml has this strong pull for people like us. Maybe not just OCaml, Elm might be in the same category. I think it’s the desire to use a Pratical, Strong typed Functional programming language which also has a fast compiler :slight_smile:

1 Like

I recently got back into (circuit)python after a long spell with OCaml.

I immediately cried out, “Why doesn’t it work?” Because with OCaml, I’ve become accustomed to getting an error as soon as the code is compiled.

OCaml is more rigorous but heavier to set up. On the other hand, the benefits are felt in the long term. When a project starts to become too complex to know every line of code by heart, I’ll have more confidence in OCaml, which allows me to be sure that every part connects correctly with the rest of the code.

Make a list of the dependencies/libraries required for your project before you start (in some areas, OCaml isn’t appropriate because the ecosystem isn’t as well developed).

6 Likes

Thanks @Chimrod

This makes total sense. Long term maintainablity is defietnetly a pro for OCaml I think.
On the ecosystem part, you need people to develop it.
If everyone comes into OCaml, and look at the ecosystem, and say, hey there’s no such library for my project and they leave. Then the eco system will never grow, right? Catch 22 situation really.

Hi, here is another experience report from someone who uses ReScript (which is based on OCaml): Just pitched using rescript at my company and was shot down - #10 by moondaddi - react - ReScript Forum

I found there are a couple of interesting points in there about why to invest in this kind of language.

Speed. Due to the lack of native compilation, Python will never reach the same performance (PyPy isn’t really practical). Moreover, multicore OCaml and libraries could help drive this gap even further depending on the task.

Raw speed, for sure. But for a back-end API applications, sometimes raw speed is not the most important thing. It can be mitigated by using caching, a queue system for heavy computing etc. Even using OCaml, one should implenmeting these strategies also.

Instead of raw performance(though it is a plus), I am more thinking if OCaml can provide more developer productivity in the long run. Can anyone confirm some hypotheses I have?

With the strong type system:

  1. It can help the team collaborate better.
  2. It can help make refactoring easier and less error-prone.
  3. Write fewer tests comparing dynamic langs.

With the fast compiler:

  1. Faster feedback loop.
  2. Do tests run faster too? One of the biggest pain I have with python is when a project grows, the testes can be very slow.

The last one is hiring, and it’s a pro and a con.

Con: fewer people know OCaml

Pro: It can attract people who want to use or learn OCaml. This type of programmer is not just looking for a job. And if they like the language and the ecosystem, they will produce better work, too.

4 Likes

You should likely write as much tests[1].

But you can skip the stupid tests that one needs to write in a dynamic language to be able to refactor representations with peace of mind. That, the static type system will catch for you and tell you exactly where you need to make your changes.


  1. Personally guilty of not writing enough of them. ↩︎

3 Likes

A few years ago we switched from Python to Ocaml as our main programming language. At this point we had used Python for about 20 years.

The main improvements were in runtime stability and code organization. The single most important reason for the stability improvements is OCaml’s explicit handling of None values. OCaml’s module system turned out to be a vast improvement over OOP for us.

We use a quite basic subset of OCaml, no fancy features like GADTs, hardly any functors, no OOP.

We are very happy with the switch and didn’t look back.

9 Likes

About a decade ago Thomas Leonard (now project lead of the Multicore effect system Eio) did a large real-world OSS project migration from Python to OCaml and wrote about it: Python to OCaml: retrospective - Thomas Leonard's blog

8 Likes

If you’re building with LLM products that have APIs and not necessarily training models and whatnot yourself, you’ll likely be fine.

Don’t be like me and try to get eio to work with your web server, your postgres library, and whatever else hahaha. Stick with lwt or async and you’ll generally have all the available tools to build web apps (if that’s what you’re building).

2 Likes

Thanks to pioneers like you it’ll be easier for the next person though.

1 Like

That’s great to hear!

1 Like

Yeah, that’s exactly it. My plan now is to build the main API layer using OCaml, if the certain 3rd party serviece I use does not support it, I will just switch to Python and let have some backend rpc calls should be fine.

And thanks for the advice.

This 100%. I think we need people in the community to do things the hard way. So people like me can learn from it, and I hope I will someday help newcomers the same way!

This tweet from a very exeperinced Python developer is timely for this discusssion. And OCaml will be easier in the long run.

because I like functional programming, OCaml is the goat.

1 Like

Consider Python is a swiss army knife.
Living in Japan, I can tell you, OCaml is a pretty sharp katana.

Now, consider your problem: how big is it?

Do you want to try cutting your problem using a swiss army knife, or a katana?

4 Likes

Katana that you can cut yourself on :sweat_smile:. I almost disagreed, but yes this is a fair, funny, and accurate metaphor.