caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Camlp4: extending syntax of record definitions
@ 2005-04-18 19:07 Markus Mottl
  2005-04-18 20:19 ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 6+ messages in thread
From: Markus Mottl @ 2005-04-18 19:07 UTC (permalink / raw)
  To: OCaml

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!

Best regards,
Markus

--
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Camlp4: extending syntax of record definitions
  2005-04-18 19:07 Camlp4: extending syntax of record definitions Markus Mottl
@ 2005-04-18 20:19 ` Martin Jambon
  2005-04-19  7:25   ` too few public grammar entry points Stefano Zacchiroli
  2005-04-19 15:13   ` [Caml-list] Camlp4: extending syntax of record definitions Markus Mottl
  0 siblings, 2 replies; 6+ messages in thread
From: Martin Jambon @ 2005-04-18 20:19 UTC (permalink / raw)
  To: Markus Mottl; +Cc: OCaml

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* too few public grammar entry points
  2005-04-18 20:19 ` [Caml-list] " Martin Jambon
@ 2005-04-19  7:25   ` 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
  1 sibling, 1 reply; 6+ messages in thread
From: Stefano Zacchiroli @ 2005-04-19  7:25 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

On Mon, Apr 18, 2005 at 01:19:43PM -0700, Martin Jambon wrote:
> 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.

I think this is a major problem with current campl4 situation. In my
experience one starts extending an entry point of the ocaml grammar,
then discover that he needs to extend something else, but unfortunately
that "something else" is not public.

Why there are so few grammar entry points made public?

I think camlp4-ers life woule be a lot easier if more of them are made
public.

Cheers.

-- 
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
If there's any real truth it's that the entire multidimensional infinity
of the Universe is almost certainly being run by a bunch of maniacs. -!-


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Camlp4: extending syntax of record definitions
  2005-04-18 20:19 ` [Caml-list] " Martin Jambon
  2005-04-19  7:25   ` too few public grammar entry points Stefano Zacchiroli
@ 2005-04-19 15:13   ` Markus Mottl
  2005-04-19 21:51     ` Martin Jambon
  1 sibling, 1 reply; 6+ messages in thread
From: Markus Mottl @ 2005-04-19 15:13 UTC (permalink / raw)
  To: Martin Jambon; +Cc: OCaml

On 4/18/05, Martin Jambon <martin_jambon@emailuser.net> wrote:
> 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.

I had already copied half of the grammar to add my extension, but then
I thought that this is insane. I couldn't believe that there is no
simpler way of doing that, but obviously there really isn't.

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

Right. I had to essentially copy everything from "type_declaration"
downwards, which is quite a lot.

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

I don't think that this is necessary, because it's only the type
declaration I want to change, not the syntax for record expressions.
The user must call the validation functions manually on his values.

> You can look at this example (uncommented), which is very similar:
>   http://martin.jambon.free.fr/extend-ocaml-syntax.html#types

Thanks, that's a very nice tutorial on Camlp4. Maybe it could be added
to the documentation index for OCaml at INRIA?

Best regards,
Markus

-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] too few public grammar entry points
  2005-04-19  7:25   ` too few public grammar entry points Stefano Zacchiroli
@ 2005-04-19 15:16     ` Markus Mottl
  0 siblings, 0 replies; 6+ messages in thread
From: Markus Mottl @ 2005-04-19 15:16 UTC (permalink / raw)
  To: Inria Ocaml Mailing List

On 4/19/05, Stefano Zacchiroli <zack@bononia.it> wrote:
> On Mon, Apr 18, 2005 at 01:19:43PM -0700, Martin Jambon wrote:
> > 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.
> 
> I think this is a major problem with current campl4 situation. In my
> experience one starts extending an entry point of the ocaml grammar,
> then discover that he needs to extend something else, but unfortunately
> that "something else" is not public.
> 
> Why there are so few grammar entry points made public?
> 
> I think camlp4-ers life woule be a lot easier if more of them are made
> public.

I second this suggestion: please add more public entry points to the grammar.

Regards,
Markus

-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Camlp4: extending syntax of record definitions
  2005-04-19 15:13   ` [Caml-list] Camlp4: extending syntax of record definitions Markus Mottl
@ 2005-04-19 21:51     ` Martin Jambon
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Jambon @ 2005-04-19 21:51 UTC (permalink / raw)
  To: Markus Mottl; +Cc: caml-list

On Tue, 19 Apr 2005, Markus Mottl wrote:

> On 4/18/05, Martin Jambon <martin_jambon@emailuser.net> wrote:
> > 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.
>
> I had already copied half of the grammar to add my extension, but then
> I thought that this is insane. I couldn't believe that there is no
> simpler way of doing that, but obviously there really isn't.
>
> > 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.
>
> Right. I had to essentially copy everything from "type_declaration"
> downwards, which is quite a lot.

Besides from redistributing a modified pa_o.ml or a program that patches
pa_o.ml and makes every entry public, I don't see any solution.

There would probably be problems for syntax extensions that are supposed
to work for both the regular and the revised syntax (I don't use the
revised syntax myself so it's only a problem if I want to please the
users of the revised syntax...).

Also, the minor grammar entries in pa_o.ml happen to change from one
version of OCaml/Camlp4 to another, so it would be (even more) difficult
to maintain compatibility across various versions of OCaml if a syntax
extension had to rely on them.

I would love to know the point of view of the developers of Camlp4...

> > 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:
>
> I don't think that this is necessary, because it's only the type
> declaration I want to change, not the syntax for record expressions.
> The user must call the validation functions manually on his values.

I see.

> > You can look at this example (uncommented), which is very similar:
> >   http://martin.jambon.free.fr/extend-ocaml-syntax.html#types
>
> Thanks, that's a very nice tutorial on Camlp4. Maybe it could be added
> to the documentation index for OCaml at INRIA?

Thanks. I thought I should fill the gaps first (mainly English
sentences).

Martin

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-04-19 21:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-18 19:07 Camlp4: extending syntax of record definitions Markus Mottl
2005-04-18 20:19 ` [Caml-list] " Martin Jambon
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

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).