caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin_jambon@emailuser.net>
To: Markus Mottl <markus.mottl@gmail.com>
Cc: OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Camlp4: extending syntax of record definitions
Date: Mon, 18 Apr 2005 13:19:43 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0504181226420.1318-100000@localhost> (raw)
In-Reply-To: <f8560b805041812072f446158@mail.gmail.com>

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 r.foo &&
>     int_positive r.bar
>
> 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 pa_o.ml 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:
  http://martin.jambon.free.fr/extend-ocaml-syntax.html#types


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr


  reply	other threads:[~2005-04-18 20:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-18 19:07 Markus Mottl
2005-04-18 20:19 ` Martin Jambon [this message]
2005-04-19  7:25   ` too few public grammar entry points Stefano Zacchiroli
2005-04-19 15:16     ` [Caml-list] " Markus Mottl
2005-04-19 15:13   ` [Caml-list] Camlp4: extending syntax of record definitions Markus Mottl
2005-04-19 21:51     ` Martin Jambon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0504181226420.1318-100000@localhost \
    --to=martin_jambon@emailuser.net \
    --cc=caml-list@inria.fr \
    --cc=markus.mottl@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).