1. 27
  1. 5

    The book came out in 2013, but has recently been released free-of-charge.

    1. 2

      Footer says

      Copyright Coherent Press 2013. Last updated 2022.

      Which might imply that it has been updated for current OCaml releases?

      1. 5

        Yes. It’s easy to miss thanks to those big and ugly headers, but John said that “The book has been updated for OCaml 4.14 in all formats” in the mail I linked to.

    2. 3

      What/where is OCaml used? What is the language like, compared to “mainstream” things? I think I sometimes see mentions of OCaml on my Linux day-to-day, but I don’t know much beyond that.

      Is it something used a lot, or is it very niche (kinda like Rust vs Zig maybe)?

      1. 5

        Comparing it to more mainstream things: it’s a bit like Go, in that it’s an ahead-of-time compiled language that can produce fast, native binaries, but with a runtime that includes a garbage collector.

        It’s a strictly evaluated imperative language, and you can read the code and predict quite accurately what instructions the compiler will produce. You can also write in a pretty high-level functional style: OCaml has a very wide range from “high level” to “low level” coding. You basically never have to call out to C in order to do something “fast enough,” as long as you avoid heap allocations (the GC only runs in response to heap allocation, you can get a very strongly typed language without any runtime overhead if you’re careful).

        It also has a type system reminiscent of Haskell’s, which means you can make massive changes to large codebases pretty fearlessly. But — unlike Haskell — OCaml supports implicit side effects (like most languages), so it doesn’t have much of a learning curve. It also lacks typeclasses, and most of the other fancy type system things that make Haskell tricky to learn.

        OCaml also has a shockingly good JavaScript backend — you can compile OCaml code to JS and use that to share code between client and server, if you’re doing web stuff. Autogenerate correctly typed APIs and stuff (if, you know, your only clients are also using OCaml). I don’t know any other language that comes close here.

        Subjectively: OCaml is a very ugly language, with lots of weird syntax and strange language warts. But if you can look past that, it’s a very practical language. It’s not fun the way that Haskell is, but it’s old and stable and works well, and the type system is the best you’re going to find in an imperative language. (Reason — an attempt to provide an alternate syntax for the OCaml compiler — was disappointingly incomplete the last time I checked. Don’t know if it’s still a thing.)

        But the community is very small. Jane Street publishes some very thorough libraries covering all the basic stuff — containers, async scheduling, system IO, etc — but coverage for what you might think of as basic necessities (especially if you’re doing web development) is a lot more spotty.

        So it occupies sort of a weird place in the world. It’s a solid, conservative, relatively performant language. But you probably don’t want to build a product on top of it, because hiring will be pretty expensive. And I don’t think it’s particularly interesting from a mind-expanding point of view — Haskell has a lot more bang for the buck there.

        1. 2

          It’s a strictly evaluated imperative language

          What’s your definition of imperative? If you limit “functional” to “pure”, then it’s quite against the mainstream opinion that classifies Scheme and often even CommonLisp as “functional”. Presence of mutable values does not make a language non-functional—absence of support for first-class functions and primitives for passing immutable values between them does.

          Most real-life OCaml code, at least in public repositories, is as functional as typical Haskell, i.e. centered around passing an immutable state around, with wide use of benign side effects (like logging) and occasional use of mutable values when it’s required for simplicity of efficiency.

          (For the uninitiated, you need to declare mutable variables or record fields explicitly, by default everything is immutable, unlike in Scheme)

          It also lacks typeclasses, and most of the other fancy type system things that make Haskell tricky to learn.

          What you aren’t saying and what someone who doesn’t know it yet may want to hear is that lack of type classes makes type inference decidable. With any “normal” (non-GADT) types, the compiler will infer types of any value/fuction automatically. There are no situations when adding an annotation will make ill-typed code well-typed. The only reason to add type annotations is for humans, but humans can as well view them in the editor (via Merlin integration).

          Well, module interfaces do need type annotations. Which is another thing you seem to dismiss: the module system. Functors provide ad hoc polymorphism when it’s required, and their expressive power is greater. My recent use case was to provide a pluggable calendar lib dependency for a TOML library. OCaml is the only production-ready language that allows anything like that.

          But — unlike Haskell — OCaml supports implicit side effects (like most languages), so it doesn’t have much of a learning curve. it’s particularly interesting from a mind-expanding point of view — Haskell has a lot more bang for the buck there.

          Not mind-expanding for someone who already saw dependently-typed languages for sure. For someone with only Go or Python background, it’s going to be as mind-blowing as Haskell, or any actually functional language for that matter.

          Technically, it’s possible to write OCaml as if it was Pascal, but it’s neither what people actually do nor something encouraged by the standard library. People will also run into monads pretty soon, whether a built-in one (Option, Result) or in concurrency libs.

          Jane Street publishes some very thorough libraries covering all the basic stuff — containers, async scheduling, system IO

          My impression is that the last time you looked was quite a while ago. Sure they do, but for each of those there’s at least one non-JaneStreet alternative, in case of Lwt, more popular than the JaneStreet one. Compare the reverse dependencies of Async vs Lwt.

          Sure, that community is still smaller than those of many other languages, but it’s far from “you will never find a lib you need”.

          1. 1

            What’s your definition of imperative? If you limit “functional” to “pure”, then it’s quite against the mainstream opinion that classifies Scheme and often even CommonLisp as “functional”.

            By imperative I mean that OCaml has statements that are executed in order, as opposed to something like Prolog or APL or a (primarily!) expression-oriented language like Haskell. I avoided calling it a “functional language” because I don’t know what that term means to the person I was replying to. I would describe OCaml as functional as well. I don’t think the label is mutually exclusive with imperative.

            What you aren’t saying and what someone who doesn’t know it yet may want to hear is that lack of type classes makes type inference decidable.

            If this tips anyone over the fence into learning OCaml, I will be delightfully surprised :)

            Which is another thing you seem to dismiss: the module system. Functors provide ad hoc polymorphism when it’s required, and their expressive power is greater.

            I think you’re reading more into my comment than is really there. I was trying to give a rough overview of “what is OCaml” to someone who does not know OCaml. The module system is neat. I’m not dismissing it. Typing on a phone takes a long time.

            Not mind-expanding for someone who already saw dependently-typed languages for sure. For someone with only Go or Python background, it’s going to be as mind-blowing as Haskell, or any actually functional language for that matter.

            Yeah, this is fair. If the choice is between OCaml or nothing, definitely study OCaml! But Haskell has a larger community, a lot more learning resources, and will force you to think differently in more ways than OCaml. Which makes it hard to recommend OCaml to someone who is functional-curious, as much as I personally like the language.

            My impression is that the last time you looked was quite a while ago. Sure they do, but for each of those there’s at least one non-JaneStreet alternative, in case of Lwt, more popular than the JaneStreet one. Compare the reverse dependencies of Async vs Lwt.

            From this response I get the impression that you read my comment as “the only libraries that exist are the ones Jane Street published.” What I meant was to assure the person I was replying to that OCaml has a healthy set of basic libraries available, with an existential proof of that statement.

            Sure, that community is still smaller than those of many other languages, but it’s far from “you will never find a lib you need”.

            We are in complete agreement here.

          2. 2

            Subjectively: OCaml is a very ugly language, with lots of weird syntax and strange language warts. But if you can look past that, it’s a very practical language. It’s not fun the way that Haskell is, but it’s old and stable and works well, and the type system is the best you’re going to find in an imperative language.

            the syntax does have its share of odd corners, but i don’t find it ugly on the whole. i quite enjoy working in it. also, having given both a decent try, i found it more fun than haskell, and ultimately it was the fp language i ended up sticking with.

          3. 2

            The “standard” reply is a company called Jane Street, that apparently requires every employee(?) to take a course in OCaml.

            1. 6

              It is not used a lot, but its user base is growing fast recently. Companies/institutions using Ocaml also include: Citrix (xenserver), Facebook, Bloomberg (where rescript was born), Tezos, Ahrefs, INRIA (COQ to name one), Aesthetic Integration, Tarides, to name a few. It is used a lot for writing compilers (also Rust started with an OCaml implementation) but it is a pretty good language for system programming, most general purpose programming in fact.

              The community is not huge, so you don’t have as many libraries as other languages do, but the ones that are there are usually pretty solid

            2. 2

              It’s getting fairly popular. I have posted Haskell and OCaml skills in HN Who’s hiring threads, and getting tons of emails back lately due to the OCaml part. I know OCaml (and SML) for a good 15 years, and it has gone from really niche to decently easy to find a job that uses it.

              I think this is due to the increasing popularity of functionaly programming and modern type systems. Aside from this, Facebook and many others use it for building static analyzers. Furthermore, it’s a good companion for Coq.

              Sadly, Haskell is still quite unpopular, but that’s a topic for another discussion.

              1. 2

                To complement the sibling replies, consider: OCaml is already a mainstream language. You are most likely to experience it on your desktop through FFTW, a ubiquitous signal-processing library which has been available for a couple decades.

                1. 1

                  A lot of people have answered your question, I’d also add that F# is a direct descendant of OCaml and shares a lot of the core syntax. It’s probably more widely used than OCaml and seems to be the CLR language that people get the most enthusiastic about.