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 (19:07)
From: Markus Mottl <markus.mottl@g...>
Subject: Camlp4: extending syntax of record definitions

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 &&

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!

Best regards,

Markus Mottl