Closed rizo closed 6 years ago
My question is: when does ppx_stage code evaluation happen? Am I correct to assume that the generated binary is a code generator that produces the expanded version of the code? And if so does this mean that the produced source code still needs to be compiled by the standard OCaml compiler?
Yep, that's it. I'd like to automate this step of calling out to the OCaml compiler, but haven't gotten around to it yet.
Thank you for clarifying that. Do you have any ideas how that could be done? Also could you recommend any literature on staging or macros in general?
I'm going to close this issue, thanks again.
@rizo Well, that's what metaocaml is for, isn't it ? :) You can read this webpage.
Technically, calling out to ocamlopt and passing it some source code (or a binary parse tree) isn't particularly difficult. What's more annoying is getting the library search paths and dependencies right, and it does depend on the runtime environment being the same as the build. Most of the annoyances here are build-system trouble.
@Drup's link is a good resource, and I'd also recommend the first few sections of Stream Fusion, to Completeness (even if you're not particularly interested in optimising streaming, there's a good intro to staging in the first few sections).
@stedolan You sort of gloss over one point which is non trivial: linking back against the generated code and have the generated program access the objects in memory of the host program without serialization.
You sort of gloss over one point which is non trivial: linking back against the generated code and have the generated program access the objects in memory of the host program without serialization
ppx_stage
doesn't do implicit CSP, so I don't think there's any particular difficulty here? I agree that implicit CSP is a tricky feature to implement!
It's not really a matter of implicitness, and more a matter of the semantics you choose. You could have something that is explicit but doesn't serialize.
Also, it goes both way: you need to retrieve the results also. :p
I'm afraid I still don't understand what the difficulty is. I'm imagining passing the generated source code to ocamlopt, building a .cmxs
, and using Dynlink to load the resulting .cmxs
. Dynlink will automatically link any references to global modules, and that's the only sort of references that ppx_stage allows from staged to host code. Once linked, calling a staged function is just like any other, and involves no serialisation, either to pass the arguments or retrieve the result.
MetaOCaml-style CSP does make this trickier, but lacking that I don't see the difficulty.
@stedolan Ah, sorry, I was really just making a general comment on your answer "execution of staged code is easy" [not sic :p].
In the case of ppx_stage, You already have a specific semantics in mind that avoid the question, although it's arguably a bit boring. :)
(Sorry for the delayed reply!)
@Drup:
Well, that's what metaocaml is for, isn't it ? :) You can read this webpage.
Right. I was under the impression that MetaOCaml follows one particular approach that is tightly integrated with the type-system for macro type-checking (I don't understand all the details unfortunately). My main problem is understanding the relation between MetaOCaml, Modular Macros and the approach implemented in ppx_stage
(which seems to be very straightforward).
Although I am very interested in staged programming I come from the C++/D background where staging is not strongly-typed but still very useful. I'm trying to learn about staging and super-compilation from implementor's perspective but navigating in this domain without guidance has been an obscure journey so far! :)
@stedolan: Thanks for clarifying how you want to achieve code execution I think it makes a lot of sense.
@Drup: Also could you explain why you consider explicit CSP boring? :)
I wasn't really sure where to ask this simple question, sorry if I'm misunderstanding the purpose of this ppx.
My question is: when does
ppx_stage
code evaluation happen? Am I correct to assume that the generated binary is a code generator that produces the expanded version of the code? And if so does this mean that the produced source code still needs to be compiled by the standard OCaml compiler?This is the conclusion I make by looking at a basic example which I include for completeness.
Original input:
Using ppx_stage this code will compile a generator that when run produces the following output source code:
This is a string that can be stored in a file and can be compiled to produce the desired final output.
Again for completeness here's the code of the generator generated by ppx_stage:
Which is clearly not a "residual" program.
Are my assumptions correct? How would a complete compilation pipeline look for an actual application? Is it possible to evaluate the generated code in compile-time (using the bytecode interpreter for example)?
Thanks in advance!