Version française
Home     About     Download     Resources     Contact us    
Browse thread
Ocaml compiler features
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] Ocaml compiler features
On Mon, 15 Jan 2007, Vincent Hanquez wrote:

> On Sun, Jan 14, 2007 at 08:49:57PM +0000, Jon Harrop wrote:
> > Does it make code unsharable or can you apply multiple camlp4 macros to one
> > piece of code?
>
> What I meant, is when you need to read code that used camlp4 you need to
> learn almost a new language. It obviously depends of the changes, but
> it's not pure OCaml.
>
> I can trivially find a lots of different incompatible syntax doing a 5s
> search, that's the problem.
>
> The common syntax "extensions" should come with OCaml so that everyone
> that use OCaml use the same language. I'm not saying camlp4 has no use,
> but it should be limited to very specific use.

OCaml is well-enough designed so that there is no "common" syntax
extension. The problem is that people use it for a variety of things, and
each domain may benefit from particular enhancements of the syntax.
Either you incorporate all those extensions into the standard syntax,
which is totally insane, or you consider them just as libraries. Loading
a library is no big deal, whether it provides more functions or more
syntax.

IMHO the problem is more that writing camlp4 syntax extensions is very
different from writing everyday OCaml code. In other words,
it is inaccessible to beginners, and most other people think it's too
complicated anyway. It requires too much expertise and only few people are
able to maintain such code.

Here is a list of things that I suggest if one ever wants to have a
standard and widely use way of extending the syntax in OCaml:

* implement a 2-stage (2, not n) compilation so that syntax extensions can
be defined in standard OCaml modules.

* each "macro" has a name and is provided as a module item.

* there should a standard syntax for macro expansions, so that external
readers know that a special syntax is being used. E.g.
  <:Mymodule.mymacro< blabla >>
where blabla is parsed using the standard OCaml lexer, and the custom
"mymacro" parser provided by module "Mymodule" (as opposed to current
quotations where the content is parsed with any lexer).

* defining a syntax extension should be done with some code which looks as
much as possible like standard Ocaml.


Here is an example. There are three new keywords "macro", "quote" and
"pragma", plus some special syntax for writing the parser.
"macro" is used to introduce the definition of a syntax extension.
"quote" is used to represent the OCaml syntax tree and is available only
in macro definitions. It would be a standard replacement for
<:expr< ... >>, <:patt< ... >>, etc. Its type (expr, patt, str_item, ...)
should be figured out automatically.
"pragma" would introduce normal OCaml code which must be compiled during
the first stage of the compilation (not shown in the example).

macro cond stream =
  let subst e1 e2 e3 = (* could be defined externally using "pragma" *)
     match e1 with
         quote true -> e2
       | quote false -> e3
       | _ -> quote (if $e1 then $e2 else $e3) in

  parse stream with (* equivalent of an EXTEND statement with a single,
                       new entry *)
   [ [ e1 = Ocaml.expr; e2 = Ocaml.expr; e3 = Ocaml.expr -> subst e1 e2 e3
     | e1 = Ocaml.expr; e2 = Ocaml.expr -> subst e1 e2 (quote ()) ] ]


(* same program, continuing *)

let test_cond x y z =
  <:cond< (x + y = z) (z + 1) 0 >>

(* is equivalent to:

let test_cond x y z =
  if (x + y = z) then (z + 1) else 0

*)


OK, there are many issues to solve, and it may not look as simple as
advertised, but I am sure it can be done.


Martin

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