Why did you learn OCaml?

So to also get back on the original topic:

After some Basic, Pascal, Maple, Mathematica, and 90’s Casio/TI calculator programming, my intro-CS class in Engineering School was using Caml-Light (which is very good for teaching but leaves students with the false impression that functional programming is just for didactic toy-languages).

After that came a ton of Java, C, C++, assemblers, Matlab, …

First job after school, I wanted to see how people make software that actually works, so went into “embedded/safety-critical avionics” → tiny subset of C with less than 1% of the time spent programming very dumbed-down algorithms (instead, by very far, the fun part is writing tests: very complicated scenarios that make many crazy situations happen).

Then got bored, started PhD in multimedia/distributed-systems (2006). Still interested in making things work instead of seg-faulting/CVE-ing, so I got interested again in OCaml, and because it was for my PhD I could just start just using it, nobody cared — first I wrote “first-person/walk-through” 3D engine with LablGL(UT) → my own horrible pile of java-like code with a ton of objects and Option.get_exn's (= “ocaml.lang.NonePointerException” :)).

Fun to be the only statically-typed-FP person in a community were most people believe that somewhere on Earth, there exist programmers capable of handling things like C++ or dynamic typing and that those hypothetical superhumans are actually among them!

But what I was making worked & had good performance for a tenth of the effort, so :man_shrugging:.

Then some security, some biology/biomedical research software, and now cryptocurrencies, all mostly in OCaml, with music software as hobby programming too.

Over the years, sometimes frustrated with some OCaml quirks, I’ve taken a hard look at, learned, or tried every language that has a type system, I always come back to OCaml as by far the best compromise (and yes I have quite a bit of Haskell experience and I think type-classes are a toxic anti-feature ;)). Moreover, things that were annoying me for years have been fixed now by the community: a fast & hackable build system and decent automatic code formatting :pray:.

This is all very nice and shiny but The Future is OCaml becoming just glue code around Coq-extracted/generated code; so I hope I’ll get to use & write more real-world Coq as soon as possible (sorry for those who know me and have heard this a 100 times: go read/make-the-exercises of Software Foundations IMHO best book ever written on all of computer science).

9 Likes

Like anything there are always pro-and-cons I guess. It would be interesting to hear your perspective. Why do you think type-classes are a “toxic anti-feature” ?

1 Like

This would be great in a separate (OT) thread perhaps.

1 Like

A bit late to the party but I’ll add my story.

I came to OCaml via the Real World OCaml book. Absolutely fantastic resource and looking forward to a second edition that I can point people at.

Previously I’d been working in Erlang, Javascript and Ruby professionally and was getting very frustrated with a large Javascript and Ruby codebase that I was migrating from one JS framework to another. The lack of types and help from the compilers was costing us a huge amount of time and causing many runtime bugs in our production system. I could see that a typed language could save us real money and headaches, so I started looking at OCaml and Haskell.

I’d previously had exposure to Haskell via Uni and the local functional programming group (FP-SYD) but I didn’t feel like I could sell Haskell to my colleagues. So went with OCaml as the more pragmatic option that I could support and explain to people, while still getting the core benefits of ADTs and type inference. Since then I’ve worked in both OCaml and Haskell professionally and really like both of them for different reasons.

In OCaml I really like the Object system and polymorphic variants, and the tooling ecosystem is absolutely fantastic (opam, dune, etc)

7 Likes

Reads like poetry! :slight_smile: Has the thought occurred to you that you might be more intuitive (or right-brain) than rational (or left-brain), to such a degree that you’ve come to “simulate” reason with powerful intuition? I’ve always been a math and science person, with mostly-ignored artistic talents which surface sometimes. I had trouble “showing my work” with computations, but was frustrated because I didn’t know how I was working.

Decades later I had developed a lot of insight into my own thinking, partly from work on videogame AI and using my own problem-solving as inspiration. And it dawned on me that I’m not very “rational and formulaic” at all. I don’t remember recipes – I remember ideas, so I always improvise. I “compute” by messy abstractions scattered across my thoughts being tested in parallel, and narrowing on ever more promising results. But the way I solve a similar problem can easily vary from one instance to the next… constant improvisation. I get a sense that my naturally-intuitive thinking has been hammered and molded toward “rational problems” like math and programming. My thinking and learning are quirky, but my talents are in innovating… which seems to fit with my theory of my own thinking.

Anyway, the way you described your language-quest and working with abstracted impressions seemed familiar.


My own discovery of OCaml was while working at a game studio… one of the fresh students was opposite me over a low cubicle wall. He mentioned one day that a language he’d love to get a chance to work with again was OCaml. “O-what!?” I’d never heard of it (this was 2004). He had one class with it at university (Stanford maybe?). He explained that it was functional but compiled, so I might like it. My style of C (and also C++, as much as it could be done at the time) was somewhat functional – preference for expressions over statements, and composition of functions rather than everything in objects. I’d just never considered a functional language because I thought they were interpreted (for me, in my ignorance, it always brought to mind Lisp).

Anyway, I took a rare “weekend off” to explore OCaml and… I’d already hated C++ and merely found C to be “acceptable” for what it does… now I couldn’t tolerate C++ at all. So frustrating. At every turn I found myself wanting ADTs and closures. Previously I’d done ugly boilerplate-heavy workarounds to simulate what closures offer automatically. Well, I was still doing that, because I was still working with C++03.

It took almost a year before I really fell for modules… which just meant another thing I now can’t live without in other languages. :stuck_out_tongue:

I watched the birth of Rust with hope… as an even lower-level performance-oriented “OCaml”. Then it became a better alternative to C++, which is really awesome… but not enticing enough to lure me from OCaml. Oh, I’d be totally happy working with Rust at a game studio. But OCaml has ruined me for most other languages.

5 Likes

Doesn’t everyone do it that way? :joy:

Me and @jerben have similar reason why we got to OCaml. Basically we got annoyed by many other languages when writing bigger systems (mainly writing web apps/services). Ruby, Python and JavaScript allow you to start fast, but quite soon after code starts to evolve, it gets hard to keep everything correctly wired. In JS world, we looked into Typescript, but it was simply too weak. Then @jerben went through quite big research to test different languages and frameworks. As result of this we came into conclusion that OCaml is the one to go for, even when we need to do something as crazy as writing our own web framework that lead us to start building Sihl. We would have end up writing much of the framework anyways, because none of the existing frameworks we providing the abstractions we needed. On the other hand lots of low level blumbing we could have had for free that we are missing in OCaml.

Basically our bet is that in the long run, having strong type system allows you to be faster when you spend less time in debugging your tests when something broke down after refactoring the code. And there’s also less test code to maintain. Only downside so far has been with OCaml ecosystem being small and we need to implement a lot things ourselves. Though I hope this is kind is a one time cost. Otherwise experience with OCaml has been really nice.

14 Likes

After migrating a project with @mikoni from JavaScript to TypeScript, we still still couldn’t handle the growth like we wanted to. Changing code was still too expensive. So we decided to migrate parts to Reason on Node, which lead to really nice results. The amount of bugs we introduced as we added new features were very low. That’s when we knew we found a great tool. After having a closer look at the OCaml web development story, we decided to ditch Node and to switch to Reason native. Shortly after we switched the syntax to OCaml as well.
We are still struggling with the state of the web development ecosystem from time to time, but all the nice things of OCaml make up for that easily. And this is not a problem that can’t be fixed! :slight_smile:

16 Likes

I’m not so sure about that—or at least I think that sets the bar pretty low for “high barrier to entry”. From where I sit, the main barrier to entry to OCaml is that the syntax is different from any popular language, but Python syntax is also different, and nobody thinks the barrier to entry is high there. What else is actually challenging in OCaml?

Variants? Just a tagged union.
High order functions? Most languages have them these days.
Module functors? Meh. If you know what a module is and what a high-order function is, it’s nothing special.

There are legitimately novel features in OCaml like polymorphic variants (which are not that hard) and GADTs (which were kind of hard for me to grok), but I you can pretty much avoid these topics as a beginner—GADTs for sure!

3 Likes

I came to OCaml after Clojure first revealed the joy of functional programming to me, and later as a refugee from Elm when I wanted something in that vein but more flexible and something I could write general applications in. I’ve used OCaml for for a few years as a prototyping tool and for hobby projects, but I’ve recently had the good fortune to be able to use it for some significant projects at work. For me it strikes the perfect balance - it’s expressive, performs well and compiles quickly. I like how you can keep it simple, but the powerful things are there to be leveraged should you need them (functors, first class modules, the object system, GADTs etc).

I also like all things French so that probably biased me somewhat.

5 Likes

Ron Minsky in one of his talks said that because OCaml barrier of entry is high, it draws high quality candidates to Jane Street. :slight_smile:

Barrier of entry might not only have to do with the syntax. The fact that there are very few jobs and resources are scarce, requires the people who want to learn and use it to invest much more energy.

With respect to the syntax, most experienced developers come from OO languages. Even though there is O in OCaml, it’s not particularly encouraged. Variants? Again, OO folks were trained to create interfaces and derive classes from it as opposed to a big switch. Lambdas and HOF? Most “entrenched” OO developers don’t bother with them, that’s my experience.

I still believe that the barrier is up there.

2 Likes

Here, you definitely have a point. Figuring how I was supposed to learn OCaml was… harder than it should have been. RWO is great, but it would be helpful to have a good “official tutorial” that is concise and covers the main language features. There are tutorials on the official website… but… we all know about those. The manual is not too bad, but it’s quite dense and technical.

3 Likes

I would not go that far. (For example, operator()() is prevalent in the C++ world.) OCaml does, however, have a very peculiar feature: curryfication. The difference between f x y + f x z and let g = f x in g y + g z can be subtle, if not error-prone, depending on the definition of f. Most OO languages make a syntactic difference between a partly applied function and a function returning a closure. OCaml does not, which can be quite confusing for beginners.

As for the original question, CamlLight was the first programming language I encountered during my computer science curriculum. Nowadays, most tools I contribute to happen to be written in OCaml, so it has become my main programming language, though I am still a C++ programmer at heart.

3 Likes

I ran away from 12 years of C++. Couldn’t stand the complexity, compile times, etc.
OCaml is for recreational programming + some tools at work.

2 Likes

I haven’t quite learned OCaml yet, because I’m still learning OCaml :smile: .

Being more of a web developer, I got introduced to OCaml via Reason, but once I started learning more about the core language, I instantly got hooked.

OCaml strikes such a good balance between being expressive, yet delivering excellent performance. I love that it’s also a very minimal language, but powerful enough to really build whatever you want.

OCaml makes me happy.

9 Likes

I’ve been fooling around with Rust for a couple weeks (that all? sure seems longer …), and it kind of makes me laugh to hear about a high barrier to OCaml. Goodness what a complicated language. The form is visually similar to C … so what?

It’s very popular, is this right? I can understand the attraction - no one cares if I use it or not, but I’m still on it, there’s much to like in this glittering edifice. But it’s like a covert intro to ML - heavily used Option type, let binding, etc. - and when the “Rustacean” eventually finds his or her way to OCaml, won’t it have a refreshing elegance by comparison? Starkly minimal by comparison, free of magic operations, blissfully simple memory management.

10 Likes

I made this meme a while ago to poke gentle fun at the Rustaceans among my younger colleagues. (Rust is a fine language, and I hope it succeeds in displacing C++ where it seems OCaml and Haskell have come up short.)

ocaml-back-in-my-day

p.s. All respect to the Modula-3 hackers who told me in my youth that C++ would be a mistake.

12 Likes

I am learning OCaml so that I can learn Hardcaml, which is a hardware construction language (H.C.L.) based on OCaml, and use it more effectively.

That way, I can compare Hardcaml with other H.C.L.s and modern hardware description languages (H.D.L.s), like PyMTL, PyRTL, PyLog, PyGears, Chisel HDL (based on Scala), and Clash (based on Haskell).

That way, I can develop electronic design automation (E.D.A.) and VLSI formal verification software that support these H.C.L.s and modern H.D.L.s. for my research, and avoid the old H.D.L.s.

I was first exposed to it while working on the then- world leading MathSAT satisfiabilities modulo theories (S.M.T.) solver team. A teammate, Dr. Anders Franzen, who was then a graduating Ph.D. student, used it to help develop the parser for S.M.T. formulas. I had came across it when looking at material on formal verification; however, as a student trying to work on hot problems in E.D.A., I cared more about VLSI/hardware model checking than theorem proving, which saw more uses of OCaml.

1 Like

I had been flirting with the idea of trying out a real functional programming language after switching to writing Python in a functional style and really enjoying it. I got intimidated by Haskell after hearing about its very steep learning curve. After searching online for a bit I stumbled across Ocaml, and the syntax seemed more user friendly to me at first glance. What motivated me the most was seeing how thin the scientific computing stack is in Ocaml and I figured i’d eventually do something about it by implementing some of my favorite libraries in Ocaml. I’m liking every minute of learning and hopefully one day i’ll be able to contribute significantly.

12 Likes

Not really. At first there were a few things here and there… but if anything, I found that I didn’t need most of GHC’s features. In Haskell, I felt that the language has so many features that encourage you to think about higher and higher levels of abstraction (which is certainly interesting and definitely worth exploring for one’s own edification), and if I wasn’t using all those features, then I wasn’t “doing it right”. And I often felt fatigued by the over-engineering that tends to permeate the ecosystem, particularly when reading code written by people who are much smarter than me.

Moreover, this is a less salient point, but I found some aspect of Haskell’s syntax confusing. When reading blog posts about some of the more advanced features, I would often find myself confused about whether the thing I was looking at was a type or a value. In Haskell, type constructors, types, typeclasses, and typeclass variables all start with a capitalized letter… and to add to the confusion, a lot of the advanced techniques involved using some notions of dependent types, type families, higher kinded polymorphism, etc… It’s a little thing, but I really like how easy it is to identify a type vs a value vs a type constructor in OCaml.

In that sense, it was the absence of those features that felt like fresh air. OCaml has just enough features to be extremely powerful, without feeling excessive and overwhelming, and I feel a lot more focused when using it due to the more straightforward idiomatic use of the language.

6 Likes