Version française
Home     About     Download     Resources     Contact us    
Browse thread
Immediate recursive functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <andrieu@i...>
Subject: Re: [Caml-list] Camlp4 with traditional syntax
 > Bardur Arantsson [Mon, 21 Feb 2005]:
 > On Mon, Feb 21, 2005 at 01:28:25PM +0100, Alex Baretta wrote:
 > 
 > > Hendrik Tews wrote:
 > > >Alex Baretta <alex@barettadeit.com> writes:
 > 
 > > >   There is one more issue with Camlp4: it does not allow for quotations
 > > >   to expand to generic syntactic elements. Often, I use quotations which
 > > >   expand to module definitions. I had to implement my own quotation
 > > >   expander, bypassing the limitations of Camlp4 to achieve this.
 > 
 > > >I don't quite understand, what's wrong with 
 > 
 > > >let me = <:module_expr< struct $ list of module def's $ end >>
 > > >in
 > > >  <:str_item< module $some_name$ = $me$ >> 
 > 
 > > We use quotation expanders to embed completely different
 > > languages, such as SQL, within Ocaml code. Specifically, the SQL
 > > quotation expander compiles SQL code to an Ocaml module. CamlP4
 > > signals an error because quotations are only meant to be used as
 > > expressions or as patterns, IIRC.
 > 
 > IIRC quotations can expand to arbitrary ASTs. Only the point of
 > *use* (ie. substitution) determines which types of ASTs will be
 > accepted.

No, quotations can expand to only expressions or patterns. See the
type of a quotation expander in quotation.mli :

  type expander =
  | ExStr of (bool -> string -> string)
  | ExAst of ((string -> MLast.expr) * (string -> MLast.patt))

As Alex mentionned, ocpp expands quotations everywhere (but only deals
with string-expanding quotations, not the AST ones), so it can be used
to generate structure items (module elements).

 > Of course, if you're generating things like module interfaces and
 > implementations, you'll need to generate them side by side since
 > there is no "combined module interface+implementation" AST node
 > type.

The camlp4 AST does have this kind of nodes, as Martin mentionned.
You can use :

# fun a b -> <:str_item< declare $a$ ; $b$ ; end >> ;;
- : MLast.str_item -> MLast.str_item -> MLast.str_item = <fun>

or :

# fun l -> <:str_item< declare $list:l$ end >> ;;
- : MLast.str_item list -> MLast.str_item = <fun>

-- 
   Olivier