OCaml - first impressions

An API like @dbuenzli’s bos that uses Result liberally, typically will force you to use monadic operators, b/c otherwise it’s just too cumbersome to work with.

ETA: There’s nothing wrong with the bos API: it’s actually lovely, and I use it by preference to any other way to access the filesystem and O.S. But it is very monad-heavy, and it’s one of the most common reasons I use monads in my code.

1 Like

Monad is an FP idiom when you use some parametric type (like option, result, list…).
Basically, a parametric type is a function from type to type and we can have such a diagram (where types are written with uppercase letters):

A --------> B ----------> C
|           |             |
F           F             F
|           |             |
F A ------>F B --------->F C

Here A, B and C are types and F a polymorphic type (like list, option…). When you have functions f : A -> B and g : B -> C, you can compose them or map them (for some type F) to work at the bottom level. A monad is a type F that allow you to combine the diagonals of this diagram, i.e. functions of types A -> F B and B -> F C. For instance, if you work with options, you can combine functions that return optional value.

I have to say, as someone who comes from a new media art background (c+±ofx, c for externals in MaxMSP, opengl, glsl) who has primarily programmed components for new media programming environments, I have found using the opam ecosystem to be the smoothest package management system I’ve ever encountered. Everything has simply worked and I haven’t had hit any annoying snags across the entire process (I can’t count the amount of times I’ve said “is this even real?” in the last few days). I’ve hacked around with dune and its amazing to see see things just build. Opam packages appear to be of the highest quality. The language itself, being theoretically influenced by category theory, just makes sense. I fell in love with category theory in philosophy grad school, which I went to initially to get away from writing software, but now I recognize that what crushed my soul isn’t computing, but the imperative paradigm itself.

I’m working through Cornell’s CS3110 and only finishing the third section so I haven’t had any practical programming experience with OCaml yet, but I’m hoping I’ll find that this experience translates nicely into the practical realm for an independent new media artist, because if it does I imagine I will be a lifelong OCaml user.

5 Likes

I think your experience of “is this real? it all just works” is in great part due to the diligence and care of @avsm and his collaborators on opam and the OCaml Platform. Yes, the community as a whole is very talented and skilled. But the discipline enforced by opam, and applied by the reviewers and their CI infrastructure, is to my mind a critical force for turning that talent and skill into the stability and smoothness you experience.

8 Likes

Bravo @avsm, what a beacon you’ve constructed

3 Likes

Very true. I often find myself wishing I could use OPAM for all the cross-platform cross-language build and test integration we have to do at my day job. For grins, I once built myself a private system of OPAM packages that build LLVM/Clang as a cross-toolchain for ARM in a Qemu environment, just to prove it was possible. The experience was gratifying, and disappointing at the same time, because there’s just no way politically to displace the giant Git monorepo and the Bazel infrastructure, no matter how awesome the technology.

1 Like

Keep in mind that you can define as many submodules as you want within each file. You also don’t always need a .mli / module type. They can be useful to make the interface more explicit and to help you avoid accidentally changing the interface when refactoring, but especially during early prototyping it can be helpful to let the compiler infer the types.

4 Likes