Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: "Ömer Sinan Ağacan" <omeragacan@gmail.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Problems with printing MetaOCaml generated code
Date: Thu, 30 Apr 2015 22:35:54 +0100	[thread overview]
Message-ID: <CAAxsn=EdKDtoUyWAfhSphWrZqs-ESpTdd564WZ6GF87HF2mEDw@mail.gmail.com> (raw)
In-Reply-To: <CAMQQO3ngKhpyTm74ouh32QfLREp2T2B7r8SyjCnziLor7dgT=A@mail.gmail.com>

On 30 April 2015 at 21:57, Ömer Sinan Ağacan <omeragacan@gmail.com> wrote:
> Let's talk on an example:
>
> -- contents of TestData.ml
>   type foo = Foo of int
>
> -- contents of main
>   open TestData
>
>   let global_ref : int ref = ref 0
>
>   let gen_inc =
>     .< let _ = global_ref := !global_ref + 1 in () >.

You can improve the output here (and everywhere else that you use
global_ref in a quotation) by putting the global reference in a
separate file, so that MetaOCaml can persist it as a path:

   let gen_inc =
     .< let _ = Globals.global_ref := !Globals.global_ref + 1 in () >.

>   let gen_write i =
>     .< match i with
>        | Foo i -> global_ref := i
>      >.

In this case the problem is that 'i' is a local variable bound to a
current-stage value.  It's better to make it a future-stage (code)
expression, and splice that in:

   let gen_write i =
     .< match .~i with
        | Foo i -> global_ref := i
      >.

You'll also need to change the call:

    gen_write .<Foo 10>.

>   let gen_cls_wrt cls =
>     .< global_ref := cls () >.

This time there are two problems.  First, cls is local, as with 'i'
above.  Second, 'cls' is a closure (function), so it doesn't have a
printed representation.  You can address the problem by making cls
either a code value or a top-level path, e.g.

     .< Globals.global_ref := Globals.cls () >.

or

     .< Globals.global_ref := .~cls () >.

With these changes the program runs without warnings or errors and
generates compilable code without CSP comments.

> * I don't know if this is MetaOCaml related, but output of this program is
>   interleaved. Instead of printing a line than code it prints two lines and
>   then two code etc. How is this possible??

The interleaving is probably due to internal buffering in the format library.

Jeremy.

  reply	other threads:[~2015-04-30 21:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-30 18:36 Ömer Sinan Ağacan
2015-04-30 19:52 ` Jacques Carette
2015-04-30 20:25   ` Ömer Sinan Ağacan
2015-04-30 20:57     ` Ömer Sinan Ağacan
2015-04-30 21:35       ` Jeremy Yallop [this message]
2015-05-01 11:21       ` oleg
2015-05-01 14:34         ` Ömer Sinan Ağacan
2015-05-01 16:16           ` Leo White
2015-05-01 16:41             ` Ömer Sinan Ağacan
2015-05-01 16:45               ` Leo White
2015-05-01 16:53                 ` Ömer Sinan Ağacan
2015-05-02 18:45                   ` Ömer Sinan Ağacan
2015-05-02 20:49                     ` Jacques Carette
2015-05-03  1:56                       ` Ömer Sinan Ağacan
2015-05-03  2:28                         ` Jacques Carette
2015-05-03  3:19                           ` Ömer Sinan Ağacan
2015-05-03  8:40                             ` Gabriel Scherer
2015-05-03 14:28                               ` Ömer Sinan Ağacan
2015-05-03 15:24                                 ` Leo White
2015-05-03 15:50                                   ` Ömer Sinan Ağacan
2015-05-06  9:50           ` oleg
2015-05-06 15:58             ` Jeremy Yallop
2015-05-06 16:45               ` Yotam Barnoy

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='CAAxsn=EdKDtoUyWAfhSphWrZqs-ESpTdd564WZ6GF87HF2mEDw@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=omeragacan@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