Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re : [Caml-list] Ask for a more efficient way to deallocate memory (full version)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Matthieu Wipliez <mwipliez@y...>
Subject: Re : [Caml-list] Ask for a more efficient way to deallocate memory (full version)
Oliver Bandel <>:
 > So, is it just a kind of pre-compilation?
> The core-OCaml compiler will be the same?
> I have not looked at the OCaml-compiler in detail,
> but from what you wrote I would assume, there
> are at least two parts.

Yes. The OCaml compilers accept a -pp option to pre-process the source code. This is how one can create a syntax extension without changing anything to the compilers. Traditionally, the pre-processor parses the code in the extended syntax, transforms it back to original syntax (where each extended construct becomes a chunk of code for example), and gives it to the compiler, as an AST.

> Is the TryFinally-code slight enough, so that
> it's behaving like handwritten OCaml? Did you look
> at the TryFinally in detail?

Looking at

The effect of the EXTEND ... END construct is to extend a syntax entry with rules. Here we extend "expr" (the entry for expressions) with a new [ "try"; expr; "finally"; expr ] rule. When Camlp4 parses code using this rule, it calls the "expand" function.

This function returns <:expr< code written in the revised syntax >>. This is a quotation. Even if you're not familiar with the revised syntax, I assume you can see the point there: try finally will be transformed to
"let result = try <body of the try> with ... in
let () = <body of the finally> in
match result with ...".

This syntax extension must be compiled with something like camlp4orf (original syntax in the language, revised syntax inside the quotations). camlp4 will replace each quotation by its equivalent AST representation. Note that the identifiers between $ are called antiquotations, and insert AST nodes where used.

For example, "let result = try ... in expr" is represented by something like:
ExLet (_loc, false, BiEq(_loc, PaId(_loc, IdLid(_loc, result)), ExTry(...)), expr)

You can pre-process code that uses a syntax extension and pretty-print it in original syntax: "camlp4o -parser TryFinally.cmo"
And you would see that it is exactly as if you wrote "let result = ..." everywhere instead of "try finally". The only additional cost is, as already mentioned, that the parsing is not as fast as using Yacc. Yet Yacc can hardly dynamically accept new syntaxes, so...
Compiling the code is done just by using the -pp option: ocamlc -pp "camlp4o -parser TryFinally.cmo"

 > Oh, well.... that's new to me.
> I had not needed camlp4 so far. And I don't know if it makes sense to
> look at it now, because there will be (or already is?) a replacement
> (from 3.10 on?).

Camlp4 has been "partially reimplemented" by Nicolas Pouillard for 3.10 version (as said on Most of these changes required the authors of syntax extensions to update their code so that it would compile with the new Camlp4. Daniel De Rauglaudre, the author of the original (pre 3.10) Camlp4, thus proposes Camlp5, which can be installed in a transitional mode to ensure backward compatibility.

> I hope the camlp4-tutorial will be updated to the camlp4-replacement.
> Possibly later I might use it. Then it would be good to have
> introductional material.

Camlp4 3.10:

> Ciao,
>    Oliver


Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo! Mail