caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Philippe Wang <mail@philippewang.info>
To: Jonathan Protzenko <jonathan.protzenko@gmail.com>
Cc: Ashish Agarwal <agarwal1975@gmail.com>, Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Literate (sort-of) programming in OCaml
Date: Tue, 3 Sep 2013 15:34:13 +0100	[thread overview]
Message-ID: <CAAFfW_rKVrFQYYfN0TU0a6RNoP8wkjL7oQ61MHUXMJgtr-=YcA@mail.gmail.com> (raw)
In-Reply-To: <5224F20C.1010905@gmail.com>

On Mon, Sep 2, 2013 at 9:16 PM, Jonathan Protzenko
<jonathan.protzenko@gmail.com> wrote:
> Hi everyone,
>
> Thanks for the numerous replies. There are a lot of good solutions in there,
> and the ones that looked closest to what I had in mind seem to be MPP and
> the rwo-run-toplevel script. Some questions about these two tools:
> - MPP seems to just have the built-in language. I couldn't find any example
> in the examples/ directory that used another language, such as OCaml. I
> guess that's because it's still in development?

Hi,

MPP currently has 2 things: the built-in language and its commands,
and the ability to bring OCaml as a pre-processor language.

With mpp -its (-its is an option to be less strict on trailing spaces
in commands) :

((x cmd ocamlopt foo.ml x))
will execute "ocamlopt foo.ml" in a shell and input its output.
Note that "x" is the name of the block. It could be FOO or 23NUI'è!çé
instead, so that one can write ((x cmd echo $((42+42)) x)) for
instance without being bothered by escaping stuff.
-> This is an example of the builtin commands.
To have the list of built-in commands: mpp -b

One may want to nest mpp commands. In this case, another pair of
tokens is used: {{ and }}.
so ((x cmd ocamlopt foo.ml x)) could be written {{x cmd ocamlopt
foo.ml x}} instead. In this precise case, there is absolutely no
difference. However, one can also write {{x cmd ocamlopt {{y get
filename y}} x}} or {{x cmd ocamlopt ((y get filename y)) x}}. But if
one write ((x ocamlopt ((y get filename y)) x)) then a shell will try
to run "ocamlopt ((y get filename y))", which is probably not what's
intended...

All tokens of the language provided by mpp are customisable on the
command line and live in mpp as well. mpp -so 42 will use "42" instead
of "((" to open blocks.
(( so TOTO)) will use TOTO to open blocks after this one, meaning that
to set the open token back to what it was, one could write TOTO so
(())

Well, it's up to the user to imagine how powerful it is.

Then, there is the ability to bring ocaml as a pre-processor language.
This is actually the main motivation for mpp.
Sometimes, you write something using a specific language and you just
wish you could easily use OCaml (or another language...) to preprocess
your file.
With mpp, the default tokens are {< and >} to embed ocaml.
This means that in any file, one can write ocaml and have its output.

Small example in markdown:

If you write this in the file e.md
```
# Number of the day
{< let foo = 42 >}
The number of the day is {< let () = Printf.printf "%d" foo >}.
```

And you run mpp -l ocaml over e.md (note the -l option), then you obtain
```
 let _ = print_string "# Number of the day\n\n"
 let foo = 42  let _ = print_string "\nThe number of the day is \n"
 let () = Printf.printf "%d" foo  let _ = print_string ".\n\n"
```
which is an ocaml programme that you can run to have your final document:

```
# Number of the day


The number of the day is
42.
```

You can see that the environment is shared. If you want different
environments, it's possible to run mpp several times with different
opening and closing tokens.

Of course, you may use ocaml as a preprocessor language in an ocaml
programme (instead of a markdown document), or any textual file
actually.

For the moment, ocaml is the only option supported by "mpp -l" but
I'll provide more language soon. (To provide a language X, one has to
write a "description" of how to convert a text file into an X
programme.)


I've not written much documentation for MPP yet because I've been
mainly working on OMD, a Markdown->HTML converted and Markdown parser
library in OCaml. Now I believe OMD is nearly done, so I'm getting
back to MPP soon...

-- 
Philippe Wang
   mail@philippewang.info

  parent reply	other threads:[~2013-09-03 14:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-02 15:39 Jonathan Protzenko
2013-09-02 15:55 ` Alain Frisch
2013-09-02 15:59 ` Török Edwin
2013-09-02 17:26   ` Maxence Guesdon
2013-09-02 16:03 ` Roberto Di Cosmo
2013-09-02 16:29 ` Ashish Agarwal
2013-09-02 20:16   ` Jonathan Protzenko
2013-09-03  8:34     ` Alain Frisch
2013-09-03 14:34     ` Philippe Wang [this message]
2013-09-02 16:39 ` Raphaël Proust
2013-09-02 16:50   ` Simon Cruanes
2013-09-03  0:15 ` oliver
2013-09-03  9:17 ` Alan Schmitt

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='CAAFfW_rKVrFQYYfN0TU0a6RNoP8wkjL7oQ61MHUXMJgtr-=YcA@mail.gmail.com' \
    --to=mail@philippewang.info \
    --cc=agarwal1975@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=jonathan.protzenko@gmail.com \
    /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).