OCaml Compiler Hacking: how to add a primitive
cedeela.fr
cedeela.fr
I know it is not accepted form to post "adverts" here but in this case I'm sticking my neck out as I think/hope this language may well be on the cusp of a second chance, after a peak then trough in 2005-2014, as judged by my googling around. I know for a fact that some super clever people seem really to be pushing the boundaries with this. JS-of-ocaml, mirage os, Coq, Jane Street core. Seems more practical than Haskell. Anti-disclosure: I have nothing to do with any of the Ocaml people. I'm just interested.
My interest has been piqued by R, after being a Python scientific computing programmer. R has some really neat functional constructs and I'm keen to dig further. I have been quite struck by how much one can achieve in a single line or two of code in the functional style.
Here's a couple of books I'm enjoying so far:
Think OCaml/How To Thinke Like A (Functional) Programmer - http://greenteapress.com/thinkocaml/thinkocaml.pdf (same author as all the other awesome Think X books!)
Unix Systems Programming in OCaml - http://ocaml.github.io/ocamlunix/
People also say really great things about Real World OCaml but I haven't had a chance to go through it yet.
[1] https://www.cl.cam.ac.uk/teaching/1415/L28/materials.html
Module.foo (fun (x,y) -> ...) ...
It's still not a typed calculus, but types will be mainstream soon. my 2 cts bet.
If you are coming from Python, ML is going to seem pretty weird. The code doesn't look too different but the compiler will be yelling at you a lot. Press on. (As a big fan of Python,) my feeling is that with Python it's easy to start programs and with OCaml (ML) it's easy to finish them.
If you are learning ML, in the beginning try to avoid using fancy features and really avoid non-functional features (i.e. mutable variables). These have a place but most of the time aren't needed. Using them gets in the way of learning this new great style.
I've heard good things about Real World OCaml (haven't read it myself - came out after I'd learned the language).
The language itself isn't rocket science. If whatever you're reading isn't a good fit, try to find something else.
Strongly agree. I now write all of my everyday “glue code” in F#, and use C# for the low-level algorithmic/library stuff. This is directly opposed to the received wisdom, but I find that it works far better (although I think in ML no matter what language I’m using).
My theory as to why this works is that designing and writing robust “line-of-business apps” is really tantamount to designing and implementing a protocol, which, in turn, requires writing something like a parser or recognizer, and it just so happens that ML is really good for writing parsers and recognizers.
> my feeling is that with Python it's easy to start programs and with OCaml (ML) it's easy to finish them.
I’m stealing this.
So far it looks great, and is very pragmatic and also covers useful tools for working in Ocaml (imho that should be in every modern specific programming language book, but it probably will date faster the rest of the book).
And: https://realworldocaml.org/v1/en/html/a-guided-tour.html
If you're a Haskeller: http://blog.ezyang.com/2010/10/ocaml-for-haskellers/
I think that's the thing that keeps me interested in OCaml and is making me interested in F#: the core ML language is not very complex, and it can carry you a long way before you start requiring advanced features. I wrote a few compilers in OCaml using only the core language and very simple modules (basically just signature + struct, no functors) and the end result was very easy to read and modify. I think very few languages can make a similar claim: either the language doesn't get you all the way and you need external help (e.g. C and its pre-processor) or overshoots into too much complexity (e.g. C++).
ML is not a language for all tasks (I am learning Rust for those times when OCaml is not appropriate), but I think that ML languages could be used beneficially in many more applications today.
/Scala myself, but that's a slightly different niche.
Not sure if that's true as I use neither and only did minimal functional programming in LISP. I mostly do an imperative style along lines of Cleanroom methodology. I'd like to hear veterans' opinions as to whether Ocaml's learning curve would be easier on imperative programmers than Haskell.
The most important part of these languages is really how they try to enhance your abilities to write modular programs. You might be surprised to find out languages from the 1970s and 1980s have better abstractions (e.g. functors/modules) than some designed today. :)
But fundamentally I think some of it will come down to personal choice and aesthetics at some level; e.g. I really like Haskell's syntax (principled, block structured with no semicolons) and I really like laziness in general, because it makes it really easy to 'float out' and refactor code. OCaml has a much better module system, a very fast compiler, and is very well designed and thought out IMO. None of those are dealbreakers - it's just a matter of picking your poison.
"You might be surprised to find out languages from the 1970s and 1980s have better abstractions (e.g. functors/modules) than some designed today. :)"
...is uniquely appropriate as I've been amazed by and posted so much old work on forums that there's little that surprise me. Far as abstractions, I think Ten15 (below) was most interesting I found given its potential as an integrator. Burroughs Architecture, IBM System/38, Wirth's layered design of Lilith, Genera LISP's developer flow... the best attributes of these still haven't been matched imho by modern work. Still worth remembering and factoring into one's next project if possible.
* Predictable performance; No need to worry about space leaks. * The module system is pretty nice. * Named/optional function parameters. * Being able to write filthy imperative code without needing to use monads for everything. :)
https://queue.acm.org/detail.cfm?id=2038036
Esper Tech is another one using it and wrote up why here:
http://tech.esper.com/2014/07/15/why-we-use-ocaml/
Esterel also used it to develop a highly-assured code generator for DO-178B-certified software. That's one of the toughest projects you can do and it made it easier.
http://www.researchgate.net/publication/220802806_Certified_...
Hope this helps to show its practical value. I've always thought Ocaml's design had some of the best compromises between how we'd like it in theory vs getting things done.
These are programs used by large numbers of generally unaware real world users. Why would they need to know - from the outside they look and behave like C binaries.
Anyway, for some big open source projects written in OCaml, take a look:
https://github.com/libguestfs/libguestfs/blob/master/v2v/v2v... https://github.com/libguestfs/libguestfs/tree/master/sparsif... https://github.com/libguestfs/libguestfs/tree/master/resize https://github.com/libguestfs/libguestfs/tree/master/builder https://github.com/libguestfs/libguestfs/tree/master/sysprep