caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Quotations in Caml-Special-Light and Caml-Light
@ 1995-11-17 13:52 Daniel de Rauglaudre
  1995-11-17 14:15 ` Pierre Weis
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel de Rauglaudre @ 1995-11-17 13:52 UTC (permalink / raw)
  To: caml-list, coq


There is a way to have macros in Caml-Light and Caml-Special-Light. I
implemented them in my private version of Caml-Special-Light. This mail
explains how these macros work.

You can include, in your source, "quotation" expressions and patterns:
quotations are tokens bracketed by << and >> holding any sequence of
characters, just like strings.

These quotations are expanded at parse time by a user function
previously loaded by the compiler. This function, the "expander", gets
as parameter the string of the sequence of characters of the quotation
and returns a new string, the source code converted, which is parsed by
the compiler.

For example, this is a definition of an expander that will transform
<<MAX>> into the value 350 and <<MIN>> to the value 8:
        let expander = function
            "MAX" -> "350"
          | "MIN" -> "8"
          | _ -> raise Parsing.Parse_error
        ;;
After having loaded this function in the compiler as a quotation
expander, you can use quotations. For example, here is a possible
toplevel session:
         #<<MAX>>;;
	 - : int = 350
	 #<<MIN>>;;
	 - : int = 8
	 #<<GRR>>;;
	 Uncaught exception: Parse_error
	 Raised while expanding quotation
         #match 350 with <<MAX>> -> 12 | x -> x + 3;;
         - : int = 12
         #match 351 with <<MAX>> -> 12 | x -> x + 3;;
         - : int = 354

Now how to load an expander? There is a new function, named
"add_expander" in a new module named "quotation" in the compiler.

For example, to load the function "expander" defined above:
         Quotation.add_expander "oops" expander;;

Quotation.add_expander is of type "string -> (string -> string) -> unit".
It guaranties that the expander is well typed (string -> string). The
first parameter is a name for the quotation (the usage of this name is
not described in this message).

In the toplevel, the order of operations is:
        load the expander function
        call Quotation.add_expander
        use quotations

Using the compiler, you have 2 steps: first make a file containing
the expander code and the call to Quotation.add_expander
(e.g. "foo.ml"):
        let expander =
          function
	    "MAX" -> "350"
	  | "MIN" -> "8"
	  | _ -> raise Parsing.Parse_error
	;;
	Quotation.add_expander "oops" expander;;

Just do the compiling phase (using the -c option):
        cslc -c foo.ml

Now, as a second step, you can use the "oops" quotations in
Caml-Special-Light source files. For example this one, "test.ml":
 	let f =
 	  function
 	    <<MAX>> -> "it is max"
 	  | <<MIN>> -> "it is min"
 	  | x -> "it is " ^ string_of_int x
 	;;
 	let test x = Printf.printf "%d: %s\n" x (f x); flush stdout in
 	test 350;
 	test <<MAX>>;
 	test 8;
 	test <<MIN>>;
 	test 25;;

Compile it with the option "-L foo.cmo". This is a new option telling
the compiler to load "foo.cmo" before starting the compilation:
        cslc -L foo.cmo test.ml

The execution of this test gives:
	350: it is max
	350: it is max
	8: it is min
	8: it is min
	25: it is 25

Remarks:

   * The quotation expanders are just of type "string -> string",
     independantly from the parsing technology. You can use lex/yacc,
     streams, or, like here, simple string pattern matchings. Etc.
   * The function Quotation.add_expander, although visible in sources
     like library interfaces, is local to the compiler. It is not
     possible to link it:
        $cslc foo.cmo
        Error while linking foo.cmo: Reference to undefined global `Quotation'
     its only usage is after the -L option, or in the toplevel.
   * The values defined in expanders are not visible in the compiled files
     using quotations: they need not to be. Only the expander code, loaded
     by the compiler is indirectly visible, since it is run when expanding
     quotations. In the example, you cannot access the function "expander",
     defined in "foo.ml", in "test.ml", unless you explicitely open the
     interface of "foo.ml", which would be unuseful since "foo.cmo" is
     not linkable: yes, in the toplevel...
   * The expander can generate any available input syntax. The example
     gives very simple syntax: "350", "8". But it can generate any
     expression or pattern code. This code is parsed by the compiler
     as expression or pattern, depending of its context.
   * All the source locations of the tree generated by the expander
     are "the quotation itself". When printing a typing error, for
     example, the whole quotation is showed. We are studying solutions
     to allow expanders to specify more precise locations.


Implementation

The implementation is quite simple, the new module "quotation.ml" in
the compiler (about 130 lines). Some modifications in the lexer and
the parser to accept quotations. And the function to load .cmo files
in the core of the compiler (got from the toplevel sources). The
examples given above, including the call to cslc, are real examples:
it works!

--------------------------------------------------------------------------
 Daniel de RAUGLAUDRE

 Projet Cristal - INRIA Rocquencourt
 Tel: +33 (1) 39 63 53 51
 Email: daniel.de_rauglaudre@inria.fr
 Web: http://pauillac.inria.fr:80/~ddr/
--------------------------------------------------------------------------




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

* Re: Quotations in Caml-Special-Light and Caml-Light
  1995-11-17 13:52 Quotations in Caml-Special-Light and Caml-Light Daniel de Rauglaudre
@ 1995-11-17 14:15 ` Pierre Weis
  1995-11-17 14:25   ` Daniel de Rauglaudre
  0 siblings, 1 reply; 4+ messages in thread
From: Pierre Weis @ 1995-11-17 14:15 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list, coq


> There is a way to have macros in Caml-Light and Caml-Special-Light. I
> implemented them in my private version of Caml-Special-Light. This mail
> explains how these macros work.

That seems to be a simple and elegant solution to the problem.

But you only gave example of compilation using csl. What could be the
specific difficulties to include quotations in Caml-Light ? Do you
already have it in a private version of Caml Light 0.7 ?

Pierre Weis
----------------------------------------------------------------------------
WWW Home Page: http://pauillac.inria.fr/~weis
Projet Cristal
INRIA, BP 105, F-78153 Le Chesnay Cedex (France)
E-mail: Pierre.Weis@inria.fr
Telephone: +33 1 39 63 55 98
Fax: +33 1 39 63 53 30
----------------------------------------------------------------------------




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

* Re: Quotations in Caml-Special-Light and Caml-Light
  1995-11-17 14:15 ` Pierre Weis
@ 1995-11-17 14:25   ` Daniel de Rauglaudre
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel de Rauglaudre @ 1995-11-17 14:25 UTC (permalink / raw)
  To: Pierre Weis; +Cc: caml-list, coq


> But you only gave example of compilation using csl. What could be the
> specific difficulties to include quotations in Caml-Light ? Do you
> already have it in a private version of Caml Light 0.7 ?

No specific difficulties: it is exactly the same problem, with the same
solution, but I did not do it.

--------------------------------------------------------------------------
 Daniel de RAUGLAUDRE

 Projet Cristal - INRIA Rocquencourt
 Tel: +33 (1) 39 63 53 51
 Email: daniel.de_rauglaudre@inria.fr
 Web: http://pauillac.inria.fr:80/~ddr/
--------------------------------------------------------------------------




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

* Re: Quotations in Caml-Special-Light and Caml-Light
       [not found] <199511171358.AA28851@peray.inria.fr>
@ 1995-11-17 16:29 ` Michel Mauny
  0 siblings, 0 replies; 4+ messages in thread
From: Michel Mauny @ 1995-11-17 16:29 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list, coq


Just a few more informations, following Daniel's message about
quotations.

First, as I said in an earlier message, macros (in the sense of
Scheme) and quotations aren't exactly the same thing. Scheme-like
macros provide arbitrary computations, returning values of arbitrary
types. In a strongly-typed language such as ML, one must make sure
that macro expansions return pieces of program, therefore a (general)
implementation of macros implies some form of dynamic type-checking.

On the other hand, quotations are guaranteed to return pieces of
program: ML abstract syntac trees, in our previous work, and character
strings in the implementation described by Daniel. In this latter
implementation, the type-correctness of expanded quotations (checking
that these strings represent valid pieces of program) is guaranteed by
the ML parser, which raises a syntax error or accepts the expanded
quotation as input.

For the sake of simplicity, Daniel, in his message, considered only a
simple case of usage of quotations. Quotations are indeed much more
powerful that this. I won't bother you with complex examples here,
but, if interested, a few interesting examples are described in a
paper by Daniel and myself (reference below). In this paper, the
expansion of quotations returns ML abstract syntac trees, making the
system a bit more complex than when expansions return strings. Despite
this small difference, the whole mechanism, together with different
implementation possibilities and examples, are given in the paper.

The paper is available from:

   ftp://ftp.inria.fr/INRIA/Projects/cristal/MLworkshop94/08-mauny.ps.Z

and here is the bibtex reference:

@InProceedings{Mauny-de-Rauglaudre94a,
        author =        "Michel Mauny and Daniel de Rauglaudre",
        title  =        "A complete and realistic implementation of
                        quotations for {ML}",
        booktitle =     "Record of the 1994 {ACM-SIGPLAN} Workshop on
                  {ML} and its Applications",
        page = "70--78",
        month = jun,
        year   =        1994
}

Cheers,

-- 
Michel




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

end of thread, other threads:[~1995-11-17 17:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-11-17 13:52 Quotations in Caml-Special-Light and Caml-Light Daniel de Rauglaudre
1995-11-17 14:15 ` Pierre Weis
1995-11-17 14:25   ` Daniel de Rauglaudre
     [not found] <199511171358.AA28851@peray.inria.fr>
1995-11-17 16:29 ` Michel Mauny

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