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
Quotations and the functorial camlp4 interface
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-08-06 (21:25)
From: Hendrik Tews <tews@o...>
Subject: Quotations and the functorial camlp4 interface

Camlp4 parsers have a functorial interface (eg.
Camlp4OCamlParser.Make), which makes it possible to build two
camlp4 syntax modules in one application that parse different

How can I add quotation support to such camlp4 parsing modules?
[Unfortunately, there is no functor

More in detail: Assume I obtained two camlp4 syntax modules SO
and SR, such that SO parses the orginal syntax and SR parses the
revised syntax. I now want to add quotation support to both SO
and SR. Following Camlp4OCamlRevisedQuotationExpander I do

  module PreCast = Camlp4.PreCast
  module type Camlp4Syntax = Camlp4.Sig.Camlp4Syntax 
    with module Loc = PreCast.Loc
    and module Ast = PreCast.Ast

  module Make
    (HostSyntax : Camlp4Syntax)
    (MakeQuotationSyntax : functor(EmptySyntax : Camlp4Syntax) -> Camlp4Syntax) 
    module Gram = PreCast.MakeGram(PreCast.Lexer)
    module EmptySyntax = 
    module QuotationSyntax = MakeQuotationSyntax(EmptySyntax)
    module X = 

now I can add the quotation support to SO and SR with

  let module M = Make(SO)(Camlp4OCamlRevisedParser.Make) in
  let module M = Make(SR)(Camlp4OCamlRevisedParser.Make) in

Could anybody comment on this?

The solution above is based on the following observations:

1. Camlp4QuotationCommon.Make(QuotationSyntax)(..) installs the
   parser in QuotationSyntax as (various) quotation expander(s)
   in QS.Quotation. This has only an effect on HostSyntax if the
   references that Camlp4QuotationCommon.Make mutates are
   actually identical in QuotationSyntax and HostSyntax.

2. The two MakeQuotationSyntax candidates
   Camlp4OCamlRevisedParser.Make and Camlp4OCamlParser.Make do
   only mutate the grammar entries in their argument modules.
   Therefore one always has to create new EmptySyntax'es but can
   reuse all the rest (especially Lexer, Ast and Loc) from

3. The functor Camlp4QuotationExpander.Make cannot be used
   instead of Make above, because it creates reflective grammers
   (where the host syntax and the quotation syntax are

Are these observations right?

Finally I have the following question:

4. Why does Camlp4OCamlRevisedQuotationExpander generate a new
   grammar module Gram and does not reuse Camlp4.PreCast.Gram?
   Can the latter be changed by some syntax extension?

[This post actually belongs to the thread,
which discusses how to make those functors SO and SR.]


Hendrik Tews