caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Vincent Hanquez <tab@snarc.org>
Cc: Jon Harrop <jon@ffconsultancy.com>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Ocaml compiler features
Date: Mon, 15 Jan 2007 12:23:32 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.58.0701151031020.6318@localhost> (raw)
In-Reply-To: <20070115000544.GA28731@snarc.org>

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


  parent reply	other threads:[~2007-01-15 20:24 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-13  5:37 Edgar Friendly
2007-01-13  5:56 ` [Caml-list] " Tom
2007-01-14 17:35   ` Edgar Friendly
2007-01-14 17:59     ` ketty
2007-01-14 18:21       ` Edgar Friendly
2007-01-14 18:29         ` Jon Harrop
2007-01-13  7:41 ` David Baelde
2007-01-13  9:31   ` ketty
2007-01-14 17:33   ` Edgar Friendly
2007-01-14 18:23     ` Jon Harrop
2007-01-14 18:41       ` Vincent Hanquez
2007-01-14 20:49         ` Jon Harrop
2007-01-14 23:38           ` Gabriel Kerneis
2007-01-15  0:55             ` Jon Harrop
2007-01-15  6:12               ` skaller
2007-01-15  0:05           ` Vincent Hanquez
2007-01-15  5:59             ` skaller
2007-01-15 20:23             ` Martin Jambon [this message]
2007-01-15 21:30               ` Jon Harrop
2007-01-15 22:13                 ` Try finally (was Re: [Caml-list] Ocaml compiler features) Daniel Bünzli
2007-01-15 22:27                   ` Vincent Hanquez
2007-01-15 22:40                     ` Quôc Peyrot
2007-01-15 23:08                       ` Vincent Hanquez
2007-01-15 22:17               ` [Caml-list] Ocaml compiler features Vincent Hanquez
2007-01-16  1:18                 ` skaller
2007-01-16  2:11                   ` Jon Harrop
2007-01-16  5:18                     ` Edgar Friendly
2007-01-16  6:36                       ` skaller
2007-01-16  6:33                     ` skaller
2007-01-16 13:55                     ` Brian Hurt
2007-01-16  9:00                   ` Vincent Hanquez
2007-01-16 14:14                     ` skaller
2007-01-16 15:00                       ` Vincent Hanquez
2007-01-16 17:47                         ` skaller
2007-01-16 19:24                           ` Edgar Friendly
2007-01-17  3:28                             ` skaller
2007-01-17 11:41                               ` Vincent Hanquez
2007-01-17 12:53                                 ` Olivier Andrieu
2007-01-17 13:18                                   ` Vincent Hanquez
2007-01-17 14:09                                 ` skaller
2007-01-16 19:42                           ` Jon Harrop
2007-01-16 21:15                             ` Florian Weimer
2007-01-17  3:46                             ` skaller
2007-01-17 11:50                               ` Vincent Hanquez
2007-01-15  5:56           ` skaller
2007-01-15  9:35         ` Nicolas Pouillard
2007-01-15 18:28           ` Martin Jambon
2007-01-15 19:02             ` ls-ocaml-developer-2006
2007-01-14 19:01       ` Edgar Friendly
2007-01-14 18:51     ` Vincent Hanquez
2007-01-14 20:49       ` Jon Harrop
2007-01-15  0:19         ` Vincent Hanquez
2007-01-20 19:19           ` Jon Harrop
2007-01-20 21:40             ` skaller
2007-01-14 21:47     ` Tom
2007-01-15 10:36 ` Richard Jones
2007-01-15 14:24   ` Vincent Hanquez
2007-01-16  8:45     ` Hendrik Tews
2007-01-16  9:08       ` Vincent Hanquez
2007-01-21 17:07         ` [Caml-list] native-code stack backtraces (was: Ocaml compiler features) Xavier Leroy
2007-01-21 18:53           ` Pierre Etchemaïté
2007-01-16  5:21   ` [Caml-list] Ocaml compiler features Edgar Friendly
2007-01-16  5:33     ` ketty
2007-01-16  6:00       ` Edgar Friendly
2007-01-16  6:10         ` ketty
2007-01-16  5:55     ` Christophe TROESTLER
2007-01-16 17:51       ` Edgar Friendly
2007-01-16 19:09         ` Jon Harrop
2007-01-16 19:21         ` Brian Hurt
2007-01-16 20:06         ` Jonathan Roewen
2007-01-16 20:13         ` Florian Weimer
2007-01-16  6:51     ` skaller
2007-01-16 18:01       ` Edgar Friendly
2007-01-17  2:23         ` skaller
2007-01-16  8:00   ` Florian Hars

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0701151031020.6318@localhost \
    --to=martin.jambon@ens-lyon.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    --cc=tab@snarc.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).