Philosophy and foundation of ocaml

Hello to all friends
I recently started learning ocaml. And at the very beginning a few questions came to me that I’d like to give you guidance.

  1. As far as I know, ocaml is a language that is programmable with oo and functional. What does the ML programming style mean?
  2. In which areas(web development, microservice , AI , Data Analyze…)?
    3… What is the ranking of programming languages.I mean high or low level?
    Thanks

Welcome ! I’ll give it a try :

  1. OO features are rarely used except for really specific cases, and I think the ML style refers to module-based programming, because that is one of OCaml’s best features. One usually writes OCaml in a functional style, but we can use imperative features to keep the code clean and readable when it is needed.

  2. A lot of areas can use OCaml. It has been historically used in compilers because of its very strong type system, and still to this day. For example, Facebook uses it for static analysis tools. Its type system brings a lot to web development (see ReasonML). Any company in need of verified software can rely on Coq. Clean network applications can be developed thanks to MirageOS. Some successful companies use OCaml too (JaneStreet, Tezos, OCamlPro, …).

  3. OCaml is a high level programming language. Having such things as algebraic data types, pattern matching, and first class functions and modules, make this language really powerful to abstract concepts. It is a garbage collected language, so you don’t have to explicitly manage memory. Performance remains really good though, as it is a compiled language. Low level work can be done too when needed, but I don’t know much about it. I’ll just say that you can handle bytes one by one if you need it, with modules of the standard library, and manage buffers or that kind of stuff.

I hope I gave you the start of an answer. I’ll let more experienced users tell you more about it !

1 Like

I don’t know what level of understanding you’re looking for. But I’ll answer the title of your post, rather than the questions themselves. Though in a way, it also answers #1

(1) The “ML programming style” is based on “well-founded induction”, and also on the idea (“value-oriented programming”) of never modifying data, but instead taking it part (via induction) and constructing new data. This is what unifies both Ocaml/SML and Haskell. Ocaml/SML go further, and allow modifying data, but even so, most ML code adheres to this discipline.

(2) To really understand OCaml, I think it would be useful to read and understand three things:

(a) The Hindley-Milner type inference algorithm

(b) Xavier Leroy’s Master’s thesis The ZINC Experiment: An Economical Implementatoin of ML

© read and understand The Little MLer

This is a starting-point. I think that another good place to look to really understand well the idea of induction, and its centrality to “thinking in ML” is to read and understand Gerard Huet’s paper The Constructive Engine where he teaches you how to view lambda-terms as objects on which one can perform induction, but in a manner that respects alpha-conversion. It. Is. A. Tour. De. Force.

(3) But perhaps you’re looking for something more … surface. In which case, I’d suggest Yaron Minsky’s Real World Ocaml

(4) Ok, now to your question #2: Really, it can be used in all these areas. I would suggest that basically, ML (and hence, OCaml) can be used as a work-alike for C++, where you’re trying to decrease programmer investment while maintaining code-quality.

In short, the thing that would make me choose C++ over Ocaml, is that I need something that is -screamingly- fast, and/or exploits multicore. And I’m willing to do the extra work to implement it, b/c it’ll be a LOT HARDER to implement in C++ than in Ocaml.

For most things, if you don’t need crazy performance or multicore scalability (which sometimes you can get via multiple processes) the only reason to not choose Ocaml is that you also don’t care about correctness.

So here’s another way of thinking of it: I program in three languages:

#1 perl when I need the program NOW, and it’s not too terribly complex

#2 Ocaml when I need the program SOON, and it’s really complex

#3 C++ when I need the program to run like the wind, and I’m OK with it taking a while, and with limitations on the complexity of the problem I can address.

4 Likes

There can be a middle ground between C++ and OCaml - Rust. So if you are already familiar with OCaml, and need something -screamingly- fast - it would be a good choice. It is much closer to the OCaml and Haskell spirit, and learning it is easier with FP background.

Two thoughts:

(1) I looked at Rust 10yr ago. At the time, I was unable to find a “neutral” description of the type system. As a defrocked priest of the Church of Lambda, this is pretty much a show-stopper. It’s as if we had YACC, but not a description of the LALR algorithm. When I have a bug in my YACC grammar, I break out the LALR algorithm, and figure out what it is, and why it happened. At the time, this did not exist for Rust.

Does it exist today? I mean, a truly neutral, no-need-to-run-the-compiler description of the type system?

(2) From what I can see, Rust lacks C++ templates, and ML modules/functors. Even in C++, when I write code I end up writing a shit-ton of code and need the modularity that templates provide. Also, typically templates are not a substitute for parametric polymorphism, but rather for modules/functors.

(3) There’s one important class of applications, where I don’t need C++, even for getting screamingly-fast code: I/O-intensive applications. There, a combination of the “iovecs” of Mark Hayden’s Ensemble system (which I’ve worked with extensively in the past) and pthread “workers” to deal with low-level I/O-parallelism is sufficient to saturate most communications hardware – at that point, why bother even going to C++ ?

There are other reasons, but I’ve left the most important one for last: I already am an experienced C++ programmer. Perhaps one day I’ll invest the energy and time to learn Rust, but why do so until it’s … much closer to reality, and closer in power to C++, and to Ocaml’s module system? I mean, if you don’t know C++, or are afraid of using it, sure I get why Rust might be appealing. But for an old-time C++ jock, it’s just not an issue.

I’m not saying that there’s no reason to write Rust. Nor that I’ll never do it. Just that, given the tradeoffs we all make in “time spent learning” vs “time spent coding”, it isn’t clear that it’s a win.

1 Like

Formally defining Rust type system and model is in progress still, see this discussion on Rust Internals discourse instance. Regarding the modular system, Rust has it, just a bit different from ML:

There are even some efforts for some more higher-kinded FP in Rust:

For all intent and purposes, that does not actually exists for OCaml either. All the descriptions are extremely partial. :slight_smile:
Rust didn’t existed 10years ago, and has changed quite a lot with time. I suggest you take another look. It has all you need to replace C++, especially if you care about safety.

In any case: https://arxiv.org/abs/1903.00982 (not published yet).

2 Likes

Ah, fair cop: it was in 2013.