Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Ömer Sinan Ağacan" <omeragacan@gmail.com>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: Jacques Carette <carette@mcmaster.ca>,
	OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Problems with printing MetaOCaml generated code
Date: Sun, 3 May 2015 10:28:39 -0400	[thread overview]
Message-ID: <CAMQQO3mkzhP=9VtOXhA-dBvsv=V1_=JcT+a68B3rkRxdRKD1MA@mail.gmail.com> (raw)
In-Reply-To: <CAPFanBEOBJ0jAiRARp93zhhRxNZKFqX935R+x0j-dYwYSNU5Lg@mail.gmail.com>

> .< x >. represents a piece of code that is just the variable x. Printed to a
> string, it is "x". (Regardless of the definition of x, for example (A))

I don't think this is true, as far as I can see MetaOCaml doesn't have open code
values that are only runnable in environments that bind free variables in the
code.

  # .<a>.;;
  Error: Unbound value a
  # let a = 1 in .<a>.;;
  - : int code = .<1>.

In this example `.<a>.` didn't mean a code that is just the variable `a`,
instead, `a` is lifted in code and code is now `.<1>.`.

I think this automatic lifting is why MetaOCaml doesn't have explicit "lift"
operation like MetaML does: variables bound in the environment automatically
lifted, so no open code values are possible.

> When printing the code, they are fundamentally different, because indeed the
> variable .< x >. may not mean anything in printed code if it is only
> meaningful in the local context. Consider:
>
>   print_code (let x = A in .< x >.)
>
> what could the program print here? "x" would be wrong, because the receiver
> of the code has no way to know what "x" means (the declaration of x is at
> the previous stage, it is not printed).

That's exactly what I'm trying to ask here... People are trying to explain that
this should be printed as "A":

  print_code (let x = .< A >. in .< .~ x >.);

But this shouldn't:

  print_code (let x = A in .< x >.);

This doesn't make sense at all. In your example you just said that "x would be
wrong, because receiver of the code has no way to know what x means". Here the
code works like a closure, it captures `x`, but when it comes to printing, it
just prints `CSP value x` instead of printing the value of it, which is
completely serializable, indeed it serializes it in the first case.

Also, note that if the code is only printed in the first case, then it's
impossible to print anything. Because at some point I have to lift some values
in my code values, but then I'm losing the ability to print anyway. Example:

  let stx = B A in
  let c   = .< stc >. in
  print_code .< .~ c >.;;

Prints .< (* CSP stx *) >. . So once you lift something to code values, you lose
the ability to print.

2015-05-03 4:40 GMT-04:00 Gabriel Scherer <gabriel.scherer@gmail.com>:
> .< x >. represents a piece of code that is just the variable x. Printed to a
> string, it is "x". (Regardless of the definition of x, for example (A))
>
> .< ~x >. represents a piece of code that is contained in the value of the
> variable x. For example, if the value of x is .< A >. , then the string
> representation of .< ~x >. would be "A".
>
> When running code in the same MetaOCaml program, you can use them in closely
> related ways: running .< f ~x >. will run the function f with the value of
> x, A, but running .< f x >. will also work because x acts here as a
> reference to a previous stage, and the value of the previous stage (because
> it is "serializable") can be transported to the current stage. (One can
> think of this as a runtime reference to a compile-time value or computation)
>
> When printing the code, they are fundamentally different, because indeed the
> variable .< x >. may not mean anything in printed code if it is only
> meaningful in the local context. Consider:
>
>   print_code (let x = A in .< x >.)
>
> what could the program print here? "x" would be wrong, because the receiver
> of the code has no way to know what "x" means (the declaration of x is at
> the previous stage, it is not printed).
>
> On the contrary, with:
>
>   lib.ml:
>     let x = A
>
>   main.ml:
>     print_code (let open Lib in .< x >.)
>
> there, because x is a reference that *can be named* at the toplevel of an
> OCaml program, you can print something, namely "Lib.x".
>
> On Sun, May 3, 2015 at 5:19 AM, Ömer Sinan Ağacan <omeragacan@gmail.com>
> wrote:
>>
>> > But that's the whole point: you are not persisting a value, you are
>> > persisting a local variable.
>>
>> Can you explain what's wrong with that? I'm making sure that the local
>> variables I'm trying to persist are persistable, as in my last example:
>>
>>   let stx = A in .< stx >.
>>
>> (Btw, MetaML has explicit `lift` function for this operation, I'm
>> wondering if
>> there are any differences between MetaML's `lift` and MetaOCaml's implicit
>> lifting)
>>
>> > In the context of staging, a variable and its value are radically
>> > different,
>> > unlike in the traditional functional programming context, where this
>> > difference can be safely and harmlessly blurred.
>>
>> I understand that code values are similar to closures in some ways, for
>> example
>> they capture some variables, and they're not always serializable. My
>> problem
>> here is that they're almost never serializable in the case of MetaOCaml.
>> It
>> even fails to serialize a code object that just holds a single constructor
>> for
>> a simple type with one constructor, for example.
>>
>> (It'd be really great if you could elaborate on how they're "radically
>> different")
>>
>> > If you want to persist "named values", then give globally accessible
>> > names to
>> > these values [which I believe others have already told you].
>>
>> ...
>>
>> > If you want to persist values, then use my solution or a variant of
>> > that.
>>
>> Can you explain how is this a "solution"? To rephrase my goal one more
>> time: I'm
>> generating specialized code in runtime, but instead of running it, I want
>> to
>> serialize it so that I can 1) inspect the generated code and make sure I'm
>> really doing the optimizations I'm expecting to do 2) I can compile my
>> code
>> separately. But MetaOCaml is just refusing to show data values in my
>> generated
>> code.
>>
>> I started to feel like the story of MetaOCaml serialization as described
>> in
>> manual's "Many ways to run the code" section is just wrong.
>>
>> 2015-05-02 22:28 GMT-04:00 Jacques Carette <carette@mcmaster.ca>:
>> > But that's the whole point: you are not persisting a value, you are
>> > persisting a local variable.
>> >
>> > In the context of staging, a variable and its value are radically
>> > different,
>> > unlike in the traditional functional programming context, where this
>> > difference can be safely and harmlessly blurred.
>> >
>> > If you want to persist "named values", then give globally accessible
>> > names
>> > to these values [which I believe others have already told you].  If you
>> > want
>> > to persist values, then use my solution or a variant of that.
>> >
>> > Jacques
>> >
>> >
>> > On 2015-05-02 9:56 PM, Ömer Sinan Ağacan wrote:
>> >>
>> >> That's not a solution. I should be able to generate some values in
>> >> code generation time and persist them in code values, that's the whole
>> >> point here.
>> >>
>> >> 2015-05-02 16:49 GMT-04:00 Jacques Carette <carette@mcmaster.ca>:
>> >>>
>> >>> try instead
>> >>>     let stx1 = .< A >. in
>> >>> and then
>> >>>     print_code std_formatter .< .~stx1 >. ;
>> >>>
>> >>> That ought to work as you wish.
>> >>>
>> >>> Jacques
>> >>>
>> >>>
>> >>> On 2015-05-02 2:45 PM, Ömer Sinan Ağacan wrote:
>> >>>>
>> >>>> In case anyone's still interested, I produced a very simple example
>> >>>> that
>> >>>> demonstrates the issue:
>> >>>>
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗ ls
>> >>>>     Main.ml  Syntax.ml
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗ cat Syntax.ml
>> >>>>     type stx =
>> >>>>       | A
>> >>>>       | B of stx
>> >>>>       | C of (stx * stx)
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗ cat Main.ml
>> >>>>     open Format
>> >>>>     open Print_code
>> >>>>     open Runcode
>> >>>>     open Syntax
>> >>>>
>> >>>>     let _ =
>> >>>>       let stx1 = A in
>> >>>>       let stx2 = B A in
>> >>>>       let stx3 = C (A, A) in
>> >>>>
>> >>>>       print_code std_formatter .< stx1 >.;
>> >>>>       print_code std_formatter .< stx2 >.;
>> >>>>       print_code std_formatter .< stx3 >.;
>> >>>>
>> >>>>       print_closed_code std_formatter (close_code .< stx1 >.);
>> >>>>       print_closed_code std_formatter (close_code .< stx2 >.);
>> >>>>       print_closed_code std_formatter (close_code .< stx3 >.);
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗ metaocamlc
>> >>>> Syntax.ml
>> >>>> -c
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗ metaocamlc
>> >>>> Syntax.cmo Main.ml -o main
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗ ./main
>> >>>>     .<(* CSP stx1 *) Obj.magic 0>. .<(* CSP stx2 *)>. .<(* CSP stx3
>> >>>> *)>.
>> >>>> .<
>> >>>>     (* CSP stx1 *) Obj.magic 0>. .<(* CSP stx2 *)>. .<(* CSP stx3
>> >>>> *)>.
>> >>>>     ➜  metaocaml_serialization_issue git:(master) ✗
>> >>>>
>> >>>>
>> >>>> 2015-05-01 12:53 GMT-04:00 Ömer Sinan Ağacan <omeragacan@gmail.com>:
>> >>>>>>
>> >>>>>> You can't serialize `eval_ref` as `eval_ref` because that is a
>> >>>>>> local
>> >>>>>> identifier. If you print out `eval_ref` into some other ml file and
>> >>>>>> compiler
>> >>>>>> it, it is going to give an "Unbound identifier eval_ref" error.
>> >>>>>
>> >>>>> That's true. Just to make sure and make the output more clear, I
>> >>>>> moved
>> >>>>> the
>> >>>>> relevant code to another module, and now it's printing this:
>> >>>>>
>> >>>>> .<Unlambda.eval_ref (* CSP p' *) []>.
>> >>>>>
>> >>>>> My main question is that it should serialize p' here, but it
>> >>>>> doesn't.
>> >>>>> I'm
>> >>>>> trying to understand why.
>> >>>
>> >>>
>> >
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>

  reply	other threads:[~2015-05-03 14:29 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
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 [this message]
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='CAMQQO3mkzhP=9VtOXhA-dBvsv=V1_=JcT+a68B3rkRxdRKD1MA@mail.gmail.com' \
    --to=omeragacan@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=carette@mcmaster.ca \
    --cc=gabriel.scherer@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