CAMLP4 TUTORIAL PDF

Camlp4 Deprecation Warning: this tutorial describes technology that is considered obsolete. It’s been replaced by extensions points and ppx rewriters. Camlp4 is part of the standard OCaml distribution and Camlp5 is distributed The title of the tutorial says Camlp4 but that is because it was written prior to the. Many of the tutorials below need updating and tutorials on many new topics are Camlp4 Tutorial (by Jake Donham), to build syntax extensions for OCaml with.

Author: Dutilar Dataur
Country: Cameroon
Language: English (Spanish)
Genre: Spiritual
Published (Last): 11 November 2018
Pages: 126
PDF File Size: 3.9 Mb
ePub File Size: 11.83 Mb
ISBN: 597-9-94965-790-8
Downloads: 91956
Price: Free* [*Free Regsitration Required]
Uploader: Dotaxe

This post is meant to be a guide to the available documentation and tutorials about CamlP4, assuming no previous experience with it. The topics are presented in the best order IMO, of course for learning, under this assumption. Before attempting to learn CamlP4, it is recommended to learn how to program in OCaml reasonably well, and to have at least some familiarity with parsing and programming language tools.

The tuorial is to give the Big Futorial, so that the details can be worked out later. Given textual input, CamlP4 parses the input into an abstract syntax tree, which is then printed in some format. As this list shows, it is a very powerful and useful tool for parsing and metaprogramming. It is often used to write syntax extensions to OCaml, like adding support for a notation tutoria, monads or for list comprehensions. Many factors contribute to this.

So the first order of business if you want to learn to use CamlP4 is learning the Revised syntax. This brings up another source of confusion: Daniel de Rauglaudre wrote the original CamlP4, which was available for OCaml since its early versions.

So a fork ensued: CamlP4 is the version included in the official OCaml distribution, maintained by the core team; CamlP4 is mostly compatible with the old CamlP4, but enough changes were made so that most code written for the old version does not work with the current one.

It is or was completely compatible with the old versions of CamlP4, although apparently it is now introducing incompatible changes. Back to the Revised syntax: Actually, although the old CamlP4 had an official reference manual and tutorial, the new CamlP4 has neither.

The section about the Revised syntax in the latest official reference manual for the old CamlP4 version 3. For now, there are no better options, so the only way out is to read both sources and try to integrate them mentally.

Or read the CamlP4 sources, in this case the OCaml parsers. The relevant files are pointed later, in the section about parsers. However, there is a good source of examples of the revised syntax: CamlP4 itself is written in this syntax. Quotations allow the programmer to treat a piece of code as data instead of being part of the program itself.

They are widely used in Lisp because of its uniform representation for code and data, and are widely used when programming in CamlP4 because they make it easier to generate code. Tutorail, this type is recursive as expected for an AST and trees for any non-trivial piece of tutorila will be complicated to create as a value of the AST type. For example, this piece of code:. If, instead, you simply quote the line of code above, CamlP4 will expand the quotation into the same AST.

The only thing to be aware of is that OCaml code inside quotations must use the revised syntax.

  AVISHAI COHEN SONGBOOK VOLUME 1 PDF

Camlp4 – Wikipedia

Support for the original syntax inside quotations was added in OCaml 3. Quotations also allow for antiquotations, tytorial are parts of a quotation that should be evaluated instead of directly transformed to AST nodes. This is tutoria in code generation: Instead, each template has gaps that must be filled with data which depends on the situation. This is similar to gutorial way format strings work in printf-like functions.

It is also possible to have a quotation inside an antiquotation, and an antiquotation inside this quotation that is inside the antiquotation of the original quotation, and so on. I know this sounds confusing, as messing with quotations can often be, but in most cases it is easier to learn them by example.

As with most things in CamlP4, quotations are also customizable. Expanders can also generate strings instead of AST nodes, although this is less useful. To learn how to use quotations to generate OCaml AST nodes, you can look at this appendix from the same manual. The CamlP4 wiki has a page on quotationsand a page making tutorizl analogy between quotations and strings.

Jake Donham has written a series of posts in his blog about CamlP4. Part 2 and part 3 are about quotations from the perspective of a user, while part 8 and part 9 are about implementing tuttorial quotations and antiquotations. CamlP4 makes it easy to create parsers, because it includes an embedded notation for parser generation. The user defines a grammar using a special notation, and CamlP4 generates a parser for it.

The reason for the title will be explained in a bit.

The good thing about grammars and parsers in CamlP4 is that they are extensible. Any loaded module can extend a grammar defined in another module, and an extension can not only add new productions, but also change existing ones or even delete them. And gutorial for the punchline: CamlP4 comes with parsers for the syntax of OCaml revised and original variants, possibly others.

Besides the above-linked tutorials in the CamlP4 wiki, the section about grammars in the old manual is still very useful. So CamlP4 parses its input and then builds an abstract syntax tree out of it. This AST will be emitted, or printed, in a chosen format. Between parsing and printing, it is possible to define AST Filters that can transform the tree, including maps and folds over it. From the point of view of syntax extensions for OCaml, CamlP4 parses OCaml code, most likely using an extended syntax, generates an AST that may be filtered, and then prints it.

The generated AST can be emitted by a pretty-printer, showing code in a readable format for humans. You could feed the output of the pretty-printer to the OCaml compiler, thus effectively activating the syntax extension. However, this has some disadvanges:.

CamlP4 has a printer that emits the AST in a binary, marshaled form for the OCaml compiler, which can then skip the parsing stages and use the tree directly. The marshaled tree also includes location information, which allows the compiler to report errors correctly for the input source.

To use this from the OCaml compilers, you just need to use the -pp command-line flag.

This page in the CamlP4 wiki has a good overview about using CamlP4 by itself and together with a compiler. Conceptually, the plan is simple: To extend the OCaml parser, it may be useful to take a look at how it is defined for the standard syntax es. The latter one is defined as an czmlp4 of the former, so you may need to consult both. A good idea is to look at examples of ccamlp4 syntax extensions. The wiki has a page with a simple extension for float expressions this extension uses a map over the AST to avoid having to rewrite many grammar productions.

  AUTUMN BRIDGE TAKASHI MATSUOKA PDF

Richard Jones posted an example in the official Caml-list for wrapping pattern matching in a predicate. More examples can be found in the recommended tutoria, below. Cam,p4 gives OCaml programmers much of the power of metaprogramming available in Lisp languages, added with static type checking and customizable components.

Some things CamlP4 can do, like integration of external syntaxes in OCaml programs, are not easy to replicate in Lisp. However, it is a quite complex piece of software and this is sometimes exposed to users.

Furthering the difficulties, it is now fragmented CamlP4 and CamlP5 and not very well documented. I hope this post helps people get up to speed in using this handy tool. As I mentioned, this is not a tutorial on CamlP4. A proper tutorial would be quite useful, but it would also demand much more time from me, so I decided to do the next best thing: CamlP4 uses an alternative concrete syntax for OCaml. To learn CamlP4, you must learn this alt syntax. CamlP4 is generally used to generate OCaml code, one way or another.

With quotations this is easier. Grammars and Extensible Parsers: CamlP4 has an embedded notation for parser generation. This can be used for defining parsers or extending existing ones. This tree can also be filtered before printing. Putting it all together: Most of them are linked in a previous part. CamlP4 is mostly used as a metaprogramming tool for OCaml, but it can be used in many ways: If you know the revised syntax, you can start to use quotations to generate OCaml code.

Quotations and Abstract Syntax Quotations allow the programmer to treat a piece of code as data instead of being part of the program itself. For example, this piece of code: Grammars and Extensible Parsers CamlP4 makes it easy to create parsers, because it includes an embedded notation for parser generation.

Filters and Printers So CamlP4 parses its input and then builds an abstract syntax tree out of it. However, this has some disadvanges: It is also possible to define new printers, though most of the time this is not very useful.

OCaml syntax extensions Conceptually, the plan is simple: Sources and Final Thoughts CamlP4 gives OCaml programmers much of the power of metaprogramming available in Lisp languages, added with static type checking and customizable components.

OCaml tutorials

The series of posts on CamlP4 over at Ambassador at the Computers is a good source, with some caveats. Jake Donham probably knows a lot more about this stuff than me, but sometimes he seems to be writing to people who already know about CamlP4, especially in the first few posts. The sequence could start better. He also uses quotations in original syntax in the earlier parts, rendering the example code unusable in current versions of OCaml in some cases he linked to newer tutorisl that work.

The new CamlP4 wiki has useful stuff, although it is incomplete both as a tutorial and as a reference.