Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Camlp4: extending syntax of record definitions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-04-18 (20:20)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] Camlp4: extending syntax of record definitions
On Mon, 18 Apr 2005, Markus Mottl wrote:

> Hi,
> I'd like to add annotations to record definitions that allow the automatic
> generation of validation functions.  Here is a simplified example:
>   let float_range lower upper x = lower <= x && x <= upper
>   let int_positive x = x >= 0
>   type some_record =
>     {
>       foo : float validate float_range 1.0 2.0;
>       bar : int validate int_positive;
>     }
> The "validate" keyword would essentially allow attaching a function to
> each record field, which is called to assert some properties.  E.g. the
> following function would be automatically generated from the above
> type definition:
>   let validate__some_record r =
>     float_range 1.0 2.0 &&
>     int_positive
> Not being a camlp4-expert my problem is that I'm not sure how to go about
> extending the grammar.  What would be the most convenient starting point?
> Extending existing grammars, starting one from scratch, etc.?  Some rough
> outline on what to do would be nice.  Thanks in advance!

My experience in that field tells me that the more you try to extend
existing syntaxic constructs, the more difficult it is.

You have to look at from the Camlp4 distribution first, and see if
the rule corresponding to record definitions is public (belongs to
a "GLOBAL" entry) or not.

If the entry that contains this rule is public, then you can delete the
rule and rewrite it, and everything is fine.
Otherwise, you have to improvise. Which is the case here: the type_kind
entry is not visible from outside.

One reasonable solution, in general, if you have a limited time or budget,
is to extend str_item (items of a module implementation) with
a whole new syntax. Something like:

record ('a, 'b) name = { label1 : type1 validate expr1;
                         label2 : type2 validate expr2;
                         ... }

For this, you can copy-paste the code for the label_declaration entry
and modify it to suit your needs (you will have specify an appropriate
priority "LEVEL" for the expressions if you want the ";" to be
considered as the end the label_declaration)

Besides the type definition, you have to make a way to create fresh
records (expr). Here the easiest way is probably to create a
labeled function just after the type definition and use that function.
However this doesn't prevent the user from creating the record using the
usual { ... } syntax, but you can achieve such an effect with a submodule,
using "private" record definitions as you can imagine.
(replacing the { ... } notation for record exprs would be too difficult
because of the "open" directives)

You can look at this example (uncommented), which is very similar:


Martin Jambon, PhD