# Dynamic loading of functions

**URL:** <https://discuss.ocaml.org/t/dynamic-loading-of-functions/12748>\
**Category:** Learning\
**Tags:** dynlink\
**Created:** [August 2, 2023, 1:58pm UTC](https://discuss.ocaml.org/t/dynamic-loading-of-functions/12748 "2023-08-02T13:58:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhuber](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nhuber/32/3939_2.png) [@nhuber](https://discuss.ocaml.org/u/nhuber)\
**Post date:** [August 2, 2023, 1:58pm UTC](https://discuss.ocaml.org/t/dynamic-loading-of-functions/12748/1 "2023-08-02T13:58:49Z")

</div>

Hej!

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 🙂

---

<div class="post-metadata">

**Author:** ![recoules](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/recoules/32/1742_2.png) [@recoules](https://discuss.ocaml.org/u/recoules)\
**Post date:** [August 8, 2023, 9:37pm UTC](https://discuss.ocaml.org/t/dynamic-loading-of-functions/12748/2 "2023-08-08T21:37:32Z")

</div>

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:

> [@What is the status of JIT compilation for OCaml today?](https://discuss.ocaml.org/t/what-is-the-status-of-jit-compilation-for-ocaml-today/9395/5):
>
> Just a small thing I’d like to add: I extended @dra27’s ocaml-jit to support dynlinking directly from OCaml source without a toplevel: [Allow dynlink/eval usage by copy · Pull Request #5 · NathanReb/ocaml-jit · GitHub](https://github.com/NathanReb/ocaml-jit/pull/5) The patch is pretty small, mostly moving usages of the toplevel api out of the library and exposing functions to add symbols that can be used by the evaluated code. An eval example looks like this: [ocaml-jit/eval.ml at 57ef5c906681562186565ff28eeb49eb8e05527e · NathanReb/ocaml-jit ·…](https://github.com/NathanReb/ocaml-jit/blob/57ef5c906681562186565ff28eeb49eb8e05527e/bin/eval.ml)

Regards,  
Frédéric
