
Extension Points, or how OCaml is becoming more like Lisp - edwintorok
https://blogs.janestreet.com/extension-points-or-how-ocaml-is-becoming-more-like-lisp/?utm_source=rss&utm_medium=rss&utm_campaign=extension-points-or-how-ocaml-is-becoming-more-like-lisp
======
lispm
One thing that's not nice in typical Lisp macros: for really complex
transformations one needs a code walker. Unfortunately that part has a few
problems: no standard code walker, complex api, different ways to access
crucial information about the code in various implementations, ...

~~~
rwmj
OCaml has a type-safe AST walker (two actually - one is part of camlp4 and the
other called ocaml-ast-analyze).

------
edwintorok
It would be interesting to see how the speed of -ppx extensions compares to
camlp4. If camlp4 is run in bytecode mode it can be quite slow, I looked at
'top' while running 'opam install core' at some point, and most of the time
was taken by camlp4. Since -ppx extensions are supposed to run natively
(without requiring natdynlink support even) I would expect them to be faster.

Has anyone done any comparisons?

~~~
avsm
There are pros and cons to not using dynlink, since it requires another fork
(although this can be avoided using a custom compiler driver).

Avoiding bytecode in the compilation toolchain is really the main benefit
though. This was originally due to the fact that only the bytecode backend
supported dynamic loading (which is used by camlp4 to load the syntax
extension modules). When native dynamic code loading was added to OCaml circa
2009 or so, the build systems around camlp4 never really took up the change to
switch to the much faster native code version. All the ppx extensions emerging
now are at least guaranteed to have a native code version on all the major
supported architectures (x86, ARM, Sparc, PPC)

------
a-nikolaev
Cool. Language extensions are easier to write. Honestly, never had the guts to
really learn camlp4 beyond an introductory tutorial, so I am happy to hear
that this new approach is easier and also more practical.

There is a link from the article with a more complete example of how the
extension points really work: [http://whitequark.org/blog/2014/04/16/a-guide-
to-extension-p...](http://whitequark.org/blog/2014/04/16/a-guide-to-extension-
points-in-ocaml/)

OCaml is great because it is doing a lot of things right. Hope, this new
feature will become more widely used than camlp4, and become a stable feature
of the language.

------
wes-exp
As languages improve, they keep moving towards Lisp — a story that has been
playing out for decades. There is a reason Lisp is called "the final
language".

~~~
yminsky
I think Lisp and Scheme have excellent macro systems, which are eminently
worth stealing. I don't think OCaml will evolve into them on the type-system
level though...

~~~
hajile
Shen lisp has the world's only turing complete type system (making it even
more advanced than Haskell's system).

~~~
read
Thank you for Shen Lisp, I wasn't aware of it.

The downside of the phrase Turing complete is that there isn't a practical
limitation that instantly comes to mind when you hear that phrase. You don't
hear people say "Oh no, that wouldn't be Turing complete". Complete or not,
how does it affect a real application?

~~~
pjc50
For a type system, being Turing complete is a _disadvantage_ : you can't prove
that your typechecker will always terminate.

~~~
hajile
This is only partially true. The type system is only unable to prove
termination if you choose to use those constructs which may disallow
termination.

~~~
chongli
I don't know about you but I'd rather not have my type checker hang and force
me to interrupt the process all the time.

------
p4bl0
I'm not convinced by the example given at the end of the article. For this
precise very simple use case it might be ok, but what about more complicated
syntax extensions?

~~~
avsm
Camlp4 is still very much around and maintained out of tree, and will work in
the next release of OCaml
([https://github.com/ocaml/camlp4](https://github.com/ocaml/camlp4)).
Extension points are intended to be simpler and more easily integrated into
editors for the common case, but you can still use camlp4 for extensions that
genuinely extend the OCaml grammar.

------
pjmlp
Interesting, seem similar to annotations/attributes. Also some similarity with
AST handling in other languages.

Now I have something to play with on the upcoming weekend. :)

------
obamacaress
been writing OCaml since years. Def good news

