caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Camlp4: generating printers of types
@ 2002-10-09 13:13 Daniel de Rauglaudre
  2002-10-09 17:18 ` Markus Mottl
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Daniel de Rauglaudre @ 2002-10-09 13:13 UTC (permalink / raw)
  To: caml-list

Hi everybody,

In Camlp4 tutorial (current version), I added a section explaining how
to make a syntax extension which, when loaded, automatically generates
printers of all your types:

For example, if your input file contains:

    type colour = Red | Green | Blue

this will be interpreted like this:

    type colour = Red | Green | Blue
    let print_colour =
      function
        Red -> print_string "Red"
      | Green -> print_string "Green"
      | Blue -> print_string "Blue"

If you are interested, this is here:
    http://caml.inria.fr/camlp4/tutorial.new/tutorial007.html#toc51

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-10-09 13:13 [Caml-list] Camlp4: generating printers of types Daniel de Rauglaudre
@ 2002-10-09 17:18 ` Markus Mottl
  2002-10-25 19:20 ` Ken Rose
  2002-12-10  2:01 ` Ken Rose
  2 siblings, 0 replies; 10+ messages in thread
From: Markus Mottl @ 2002-10-09 17:18 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list

On Wed, 09 Oct 2002, Daniel de Rauglaudre wrote:
> In Camlp4 tutorial (current version), I added a section explaining how
> to make a syntax extension which, when loaded, automatically generates
> printers of all your types:

Thanks a lot, this looks very interesting! Especially debugging might
become much easier with this, because one wouldn't have to adapt
pretty-printers all the time when types change during development.

Regards,
Markus Mottl

-- 
Markus Mottl                                             markus@oefai.at
Austrian Research Institute
for Artificial Intelligence                  http://www.oefai.at/~markus
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-10-09 13:13 [Caml-list] Camlp4: generating printers of types Daniel de Rauglaudre
  2002-10-09 17:18 ` Markus Mottl
@ 2002-10-25 19:20 ` Ken Rose
  2002-10-25 20:02   ` Chris Hecker
  2002-10-26  0:11   ` Daniel de Rauglaudre
  2002-12-10  2:01 ` Ken Rose
  2 siblings, 2 replies; 10+ messages in thread
From: Ken Rose @ 2002-10-25 19:20 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list

Daniel de Rauglaudre wrote:
> 
> Hi everybody,
> 
> In Camlp4 tutorial (current version), I added a section explaining how
> to make a syntax extension which, when loaded, automatically generates
> printers of all your types:

<snip>

> 
> If you are interested, this is here:
>     http://caml.inria.fr/camlp4/tutorial.new/tutorial007.html#toc51

I'm very interested, but unfortunately it doesn't work in ocaml-3.06. 
Is it practical to make it work there (maybe by replacing pcaml with the
CVS versions)?  How should that be done?  I work on both Linux and
Windows.

Thanks

 - ken
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-10-25 19:20 ` Ken Rose
@ 2002-10-25 20:02   ` Chris Hecker
  2002-10-26  0:11   ` Daniel de Rauglaudre
  1 sibling, 0 replies; 10+ messages in thread
From: Chris Hecker @ 2002-10-25 20:02 UTC (permalink / raw)
  To: rose, Daniel de Rauglaudre; +Cc: caml-list


>I'm very interested, but unfortunately it doesn't work in ocaml-3.06.
>Is it practical to make it work there (maybe by replacing pcaml with the
>CVS versions)?  How should that be done?  I work on both Linux and
>Windows.

If you sync the cvs camlp4, ./configure, make, make install it should just 
work.

Chris


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-10-25 19:20 ` Ken Rose
  2002-10-25 20:02   ` Chris Hecker
@ 2002-10-26  0:11   ` Daniel de Rauglaudre
  1 sibling, 0 replies; 10+ messages in thread
From: Daniel de Rauglaudre @ 2002-10-26  0:11 UTC (permalink / raw)
  To: caml-list

Hi,

On Fri, Oct 25, 2002 at 12:20:33PM -0700, Ken Rose wrote:

> >     http://caml.inria.fr/camlp4/tutorial.new/tutorial007.html#toc51
> 
> I'm very interested, but unfortunately it doesn't work in ocaml-3.06. 

I think that the only problem is that Pcaml.type_declaration is not
defined. Is it that? In this case, if you don't want to update the
current CVS version, you can use the trick I used in IoXML:

let type_declaration : Grammar.Entry.e 'a =
  Obj.magic (Grammar.Entry.find Pcaml.str_item "type_declaration");;

and replace "Pcaml.type_declaration" by "type_declaration".

-- 
Daniel de RAUGLAUDRE
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-10-09 13:13 [Caml-list] Camlp4: generating printers of types Daniel de Rauglaudre
  2002-10-09 17:18 ` Markus Mottl
  2002-10-25 19:20 ` Ken Rose
@ 2002-12-10  2:01 ` Ken Rose
  2002-12-10 12:25   ` Daniel de Rauglaudre
  2002-12-10 13:20   ` Damien Doligez
  2 siblings, 2 replies; 10+ messages in thread
From: Ken Rose @ 2002-12-10  2:01 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list

Daniel de Rauglaudre wrote:
> 
> Hi everybody,
> 
> In Camlp4 tutorial (current version), I added a section explaining how
> to make a syntax extension which, when loaded, automatically generates
> printers of all your types:
> 
> For example, if your input file contains:
> 
>     type colour = Red | Green | Blue
> 
> this will be interpreted like this:
> 
>     type colour = Red | Green | Blue
>     let print_colour =
>       function
>         Red -> print_string "Red"
>       | Green -> print_string "Green"
>       | Blue -> print_string "Blue"


I've been working on extending this to more general types, which I
suppose would be easy for someone who's had some practice with camlp4. 
I'm running into 2 problems, and anticipating a third one.

1. Is there a clean way to handle (sum) types with constructors with
multiple module qualifications?  I hacked the example to deal with
Foo.bar, but it doesn't seem to extend automatically to Foo.Bar.baz.  As
I understand it, I'd need a new pattern for each additional uid.

2. How are tuples represented in camlp4's syntax?  This is so I can
match them when analyzing a type.

3. This is the big one.  When working on a type like:

type t = int

with the obvious extention to Daniel's code, the preprocessor produces

type t = int let rec print_t = print_int

Which the compiler rejects with "This kind of expression is not allowed
as right-hand side of `let rec'"  I haven't been able to figure out a
way to sneak it past, either.  What's going on here?  Why can't I rename
print_int?

Thanks

 - ken
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-12-10  2:01 ` Ken Rose
@ 2002-12-10 12:25   ` Daniel de Rauglaudre
  2002-12-10 15:33     ` Ken Rose
  2002-12-10 13:20   ` Damien Doligez
  1 sibling, 1 reply; 10+ messages in thread
From: Daniel de Rauglaudre @ 2002-12-10 12:25 UTC (permalink / raw)
  To: caml-list

Hi,

On Mon, Dec 09, 2002 at 06:01:41PM -0800, Ken Rose wrote:

> 1. Is there a clean way to handle (sum) types with constructors with
> multiple module qualifications?  I hacked the example to deal with
> Foo.bar, but it doesn't seem to extend automatically to Foo.Bar.baz.  As
> I understand it, I'd need a new pattern for each additional uid.

I don't understand that: in the type definition, there is no qualifications
for the constructors.

> 2. How are tuples represented in camlp4's syntax?  This is so I can
> match them when analyzing a type.

In Revised syntax, they are like in normal syntax, but the parentheses
are compulsory.

> 3. This is the big one.  When working on a type like:
> type t = int
> with the obvious extention to Daniel's code, the preprocessor produces
> type t = int let rec print_t = print_int
> Which the compiler rejects with "This kind of expression is not allowed
> as right-hand side of `let rec'"  I haven't been able to figure out a
> way to sneak it past, either.  What's going on here?  Why can't I rename
> print_int?

This message of OCaml just means that there are syntactic restrictions
of the "let rec". In this case, you should produce either a "let" without
"rec" or, an eta extension: "let rec print_t x = print_int x".

-- 
Daniel de RAUGLAUDRE
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-12-10  2:01 ` Ken Rose
  2002-12-10 12:25   ` Daniel de Rauglaudre
@ 2002-12-10 13:20   ` Damien Doligez
  1 sibling, 0 replies; 10+ messages in thread
From: Damien Doligez @ 2002-12-10 13:20 UTC (permalink / raw)
  To: caml-list

On Tuesday, Dec 10, 2002, at 03:01 Europe/Paris, Ken Rose wrote:

> type t = int let rec print_t = print_int
>
> Which the compiler rejects with "This kind of expression is not allowed
> as right-hand side of `let rec'"  I haven't been able to figure out a
> way to sneak it past, either.  What's going on here?  Why can't I 
> rename
> print_int?

The problem here is that you are using "let rec" to define a
non-functional value which is not recursive.  O'Caml 3.06 can handle
some such definitions but not all of them.  We have worked a bit on
the "let rec" restrictions and the current CVS version of O'Caml accepts
all non-recursive definitions in a let rec.

You have several solutions:
1. wait for the next release of O'Caml (no expected date yet)
2. use the CVS version (and be a beta-tester)
3. get rid of the "rec" in your generated code in the case where you
    are only renaming a function
4. use eta-conversion to make it a functional value:
      let rec print_t x = print_int x

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-12-10 12:25   ` Daniel de Rauglaudre
@ 2002-12-10 15:33     ` Ken Rose
  2002-12-10 17:38       ` Daniel de Rauglaudre
  0 siblings, 1 reply; 10+ messages in thread
From: Ken Rose @ 2002-12-10 15:33 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list

Hi,

I worded things badly enough that nobody understood what I was actually
after.  What I want is help extending your automatically generated
printing functions to handle all the types I actually use.

Daniel de Rauglaudre wrote:
> On Mon, Dec 09, 2002 at 06:01:41PM -0800, Ken Rose wrote:
> 
> > 1. Is there a clean way to handle (sum) types with constructors with
> > multiple module qualifications?  I hacked the example to deal with
> > Foo.bar, but it doesn't seem to extend automatically to Foo.Bar.baz.  As
> > I understand it, I'd need a new pattern for each additional uid.
> 
> I don't understand that: in the type definition, there is no qualifications
> for the constructors.

This is for a type like:

type x = A of int * Foo.t * Bar.Baz.t
       | ...

Where I don't want to have to put in 
    | <:ctyp< $uid:m$.$lid:s$ >> -> <:expr< $uid:m$.$lid:fun_name s$ >>
and
    | <:ctyp< $uid:m2$.$uid:m$.$lid:s$ >> -> <:expr<
$uid:m2$.$uid:m$.$lid:fun_name s$ >>

and so on into gen_print_type.  Or do I not understand what $uid:x$
does?



> > 2. How are tuples represented in camlp4's syntax?  This is so I can
> > match them when analyzing a type.
> 
> In Revised syntax, they are like in normal syntax, but the parentheses
> are compulsory.

Here, I want to match 
type t = int * int
within gen_one_print_fun, to produce a printer instead of "failwith...".

> > 3. This is the big one.  When working on a type like:
> > type t = int
> > with the obvious extention to Daniel's code, the preprocessor produces
> > type t = int let rec print_t = print_int
> > Which the compiler rejects with "This kind of expression is not allowed
> > as right-hand side of `let rec'"  I haven't been able to figure out a
> > way to sneak it past, either.  What's going on here?  Why can't I rename
> > print_int?
> 
> This message of OCaml just means that there are syntactic restrictions
> of the "let rec". In this case, you should produce either a "let" without
> "rec" or, an eta extension: "let rec print_t x = print_int x".

I'd suspected that, though I'm still curious as to why that's so. 
Still, what I really want is a generally useful tool to produce these
useful functions, without having to spend too much time right now
learning camlp4.  It's for my regular work, where I'm building a
compiler for a DSL.  I don't understand camlp4 well enough to know how
to suppress the "rec", and I'm hoping to avoid learning, at least for a
while.  It's a great tool, but I have only so much time.

Thanks for any help you're willing to give,

 - ken
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Camlp4: generating printers of types
  2002-12-10 15:33     ` Ken Rose
@ 2002-12-10 17:38       ` Daniel de Rauglaudre
  0 siblings, 0 replies; 10+ messages in thread
From: Daniel de Rauglaudre @ 2002-12-10 17:38 UTC (permalink / raw)
  To: caml-list

Hi,

On Tue, Dec 10, 2002 at 07:33:23AM -0800, Ken Rose wrote:

> and so on into gen_print_type.  Or do I not understand what $uid:x$
> does?

If x is a string <:expr< $uid:x$ >> produces "the uppercase identifier
named by the value of x". If x is "Foo", this produces the identifier
Foo. Notice that it works even if x does not contain an uppercase
identifier: OCaml with interpret it as a module name or as a
constructor name (depending on its position), whatever its value.

Sorry, for your Baz.Baz.t, you need your <:ctyp< $uid:m2$.$uid:m$.$lid:s$ >>
The better is to use an iterator, or use the recursion to treat the
general case.

> > In Revised syntax, they are like in normal syntax, but the parentheses
> > are compulsory.
> 
> Here, I want to match 
> type t = int * int
> within gen_one_print_fun, to produce a printer instead of "failwith...".

In Normal syntax, you can write "int * int" but inside the predefined
quotations of AST, you have to use the Revised syntax. See the chapter
about it in the reference manual.

You must write it <:ctyp< (int * int) >>. And if tl is a list of ctyp,
you can obtain the type tuple of this list of types with:
    <:ctyp< ($list:tl$) >>

See the chapter about AST quotations.

> > This message of OCaml just means that there are syntactic restrictions
> > of the "let rec". In this case, you should produce either a "let" without
> > "rec" or, an eta extension: "let rec print_t x = print_int x".
> 
> I'd suspected that, though I'm still curious as to why that's so. 

Damien Doligez answered you on that point; this is typing and
therefore not the work of Camlp4.

> Still, what I really want is a generally useful tool to produce these
> useful functions, without having to spend too much time right now
> learning camlp4.  It's for my regular work, where I'm building a
> compiler for a DSL.  I don't understand camlp4 well enough to know how
> to suppress the "rec", and I'm hoping to avoid learning, at least for a
> while.  It's a great tool, but I have only so much time.

Well, if you don't have time to look at it, I can help.

-- 
Daniel de RAUGLAUDRE
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-12-10 17:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-09 13:13 [Caml-list] Camlp4: generating printers of types Daniel de Rauglaudre
2002-10-09 17:18 ` Markus Mottl
2002-10-25 19:20 ` Ken Rose
2002-10-25 20:02   ` Chris Hecker
2002-10-26  0:11   ` Daniel de Rauglaudre
2002-12-10  2:01 ` Ken Rose
2002-12-10 12:25   ` Daniel de Rauglaudre
2002-12-10 15:33     ` Ken Rose
2002-12-10 17:38       ` Daniel de Rauglaudre
2002-12-10 13:20   ` Damien Doligez

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