Dynamic loading of functions


I have a more general question regarding dynamic loading of functions. I am implementing a compiler, and I would like to have the ability to also interpret different intermediate representations of my program. The source language I am trying to compile allows the definition of functions. So far, I have written an interpreter that basically takes the AST of the function and the AST of the arguments and evaluates them together. In order to make the process faster (some of the inputs to the function may be symbolic, and I have to redo the evaluation with many different inputs), I would like to first compile the functions to OCaml code.

Then I would only have to evaluate the arguments to the function, call the native OCaml function with them, and wrap the result back into an AST node.

I figured I could use the Dynlink library. However, the process seems rather complicated. So I was thinking that maybe there are some other solutions to this problem. After all, it seems that toplevels like utop are basically doing the same thing, right? If I could transfer the AST for a function into a string that represents the equivalent OCaml code for the function, is there an easy way of actually loading that function dynamically at runtime?

Thank you for any input :slight_smile:


Hi @nhuber,
I do not have a clear answer, but I think your question is somewhat related to what is discussed in the following topic: