6 ms·
OCaml Compiler Hacking: how to add a primitive
- vegabook 11y agoThis language seems to punch above its weight. There seem to be a lot of postively rated submissions on Ocaml on HN, though the comments secions are inevitably quite light. I wonder if there should be a greater effort to learn it. I personally am enrolled here: https://www.france-universite-numerique-mooc.fr/courses/parisdiderot/56002/session01/about https://www.france-universite-numerique-mooc.fr/courses/pari.... 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.
- rubiquity 11y agoI try and read/comment on all of the OCaml articles that surface here. I've been learning the language for a few months now and I'm having a blast! I'm hoping we can carve out a niche in the infrastructure space for OCaml. It's a really approachable language for the ML family! I hope it grows. 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 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/ 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.
- freefrag 11y agoI also recommend the notes for L28[1] - Advanced Functional Programming at Cambridge. They might be a little hard to follow, but they are extremely insightful and teach concepts that may be difficult to learn elsewhere [1] https://www.cl.cam.ac.uk/teaching/1415/L28/materials.html https://www.cl.cam.ac.uk/teaching/1415/L28/materials.html
- agumonkey 11y agoIndeed, very complete set of slides. Thanks a lot.
- haxeny1 11y agoI first started using ocaml (f# actually and then re-wrote it in ocaml) for writing a simple interpreter to generate java code from mysql schema tables to deal with hibernate boiler plates etc. The code was much succinct than a comparable parser written in say antlr. That was my first introduction to typesafety helping me move forward. Specially, when coming back to the code, the compiler will scream at you for either not handling all the patterns that matched or adding new ones that didnt exist yet and needed to be changed. I went away from OCAML towards Haskell because a coworker convinced me to switch: also haskell seems to have a better support for multicore architectures. Of course, my applications are more web/networking apps, crud, websites etc. So i could have gone either way. The one difference i find when programming with Haskell as opposed to ocaml is that in ocaml, my instinct would be to write a little interpreter(or a quasi quoter). In Haskell, I still think that template haskell is a necessary evil, therefore, do it only when I really know what I am doing. I found this link to be quite helpful: http://pleac.sourceforge.net/pleac_ocaml/ http://pleac.sourceforge.net/pleac_ocaml/
- agumonkey 11y agoReading the pleac pages, it's amazing how javascript is getting close and closer to ml. It's all going back to its roots since js was quite {scheme} so it's not that surprising but still a joy to see. Module.foo (fun (x,y) -> ...) ...
- adultSwim 11y agoI highly recommend checking it out. The great thing is that it's not the fancy features that (pushing the boundaries) that make it a good language. The core of ML is the real gold. I content that it would be a good choice as a default programming language for most programmers. 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).
- adultSwim 11y agoOne warning. Some functional programming materials (books, tutorials, etc) can be way too theoretical. However this has changed a lot in recent years. The language itself isn't rocket science. If whatever you're reading isn't a good fit, try to find something else.
- vegabook 11y agoYes it seems to attract a high-end CS crowd - there's lots of graduate-level CS assumption going on in some of the tutorials. But one of the things I really appreciate is the REPL ("toplevel") which let's you hack around and mess with examples in an interactive way. So good for a compiled language.
- klibertp 11y agoMuch better in this regard than Haskell, though. Being impure and eager makes the most die-hard academics go for Haskell instead.
- ufo 11y agoOcaml stuff seems to be a bit more down to earth in my experience. As long as you stay away from GADTs :)
- klibertp 11y agoDo yourself a favor and check out Julia before settling for R.
- deleted 11y ago[deleted]
- lmm 11y agoIt's a good language but I really struggle to see why you'd choose it over Haskell. You may not need the fancier types yet, but in Haskell they'll be there for when you do, whereas in OCaml you reach the infuriating position where you know exactly what you want to do but you can't quite express it in this language without repeating yourself several times over. I guess if you've never used higher-kinded types maybe you don't miss them? /Scala myself, but that's a slightly different niche.
- lpw25 11y agoFunctors => higher-kinded types
- vegabook 11y agoyeah but I'm dead scared of the stories I've heard about indeterminate execution time in certain cases. For my real-time production use case this would be a deal breaker. Am I misguided?
- lmm 11y agoIt's a risk - but then it's a risk in OCaml too. How real-time are we talking? I've seen Haskell used in videoconferencing and it performed very well.
- nickpsecurity 11y agoThere's at least one financial firm that chose Haskell and presented good results from it. Galois works miracles with Haskell left and right. So, there's a slow build-up of momentum in that direction. I'm hesitant to use it due to the learning curve. I have a feeling I could probably get productive in Ocaml faster than in Haskell. I figure I would learn them in that order. 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.
- thoughtpolice 11y ago
- nickpsecurity 11y agoJane Street uses Ocaml professionally in a financial trading application. Here's an excellent write-up on why they chose it, its advantages, examples in action, and a few limitations. https://queue.acm.org/detail.cfm?id=2038036 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/ 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_Development_Tools_Implementation_in_Objective_Caml 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.
- rwmj 11y agoThe project I've been involved with at Red Hat uses OCaml in a very procedural, imperative, workaday way. We purposely don't use fancy functional stuff, except in the very few cases where it makes sense. We also freely link to C code in the same programs. 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.ml https://github.com/libguestfs/libguestfs/blob/master/v2v/v2v... https://github.com/libguestfs/libguestfs/tree/master/sparsify https://github.com/libguestfs/libguestfs/tree/master/sparsif... https://github.com/libguestfs/libguestfs/tree/master/resize https://github.com/libguestfs/libguestfs/tree/master/resize https://github.com/libguestfs/libguestfs/tree/master/builder https://github.com/libguestfs/libguestfs/tree/master/builder https://github.com/libguestfs/libguestfs/tree/master/sysprep https://github.com/libguestfs/libguestfs/tree/master/sysprep
- rubiquity 11y agoI'm really impressed with how easy it is to bring C functions into your OCaml programs! Coming from Ruby where I still have nightmares about the C API the straight forwardness of this is. I can't imagine it's very often you need to pull in C code anyway but it's good to know it's a pretty clean interface to do so.
- AdieuToLogic 11y agoWhile OCaml definitely makes introducing C code easier than other languages, why not just us SWIG[1] and call it a day? Especially since it supports a lot[2] of languages effectively for free. 1 - http://swig.org/Doc1.3/Ocaml.html http://swig.org/Doc1.3/Ocaml.html 2 - http://swig.org/compat.html#SupportedLanguages http://swig.org/compat.html#SupportedLanguages