# Val top\_level : string -\> string

**URL:** <https://discuss.ocaml.org/t/val-top-level-string-string/15547>\
**Category:** Learning\
**Tags:** toplevel\
**Created:** [November 3, 2024, 4:35pm UTC](https://discuss.ocaml.org/t/val-top-level-string-string/15547 "2024-11-03T16:35:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Harrop](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jon_harrop/32/2015_2.png) [@Jon\_Harrop](https://discuss.ocaml.org/u/Jon_Harrop)\
**Post date:** [November 3, 2024, 4:35pm UTC](https://discuss.ocaml.org/t/val-top-level-string-string/15547/1 "2024-11-03T16:35:25Z")

</div>

I’d like a function that takes OCaml code like `2+3` in the form of a string, runs the code through a fresh top-level and returns the string that the top-level responded with. Does that already exist and, if not, how might I write it?

---

<div class="post-metadata">

**Author:** ![sim642](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sim642/32/3597_2.png) [@sim642](https://discuss.ocaml.org/u/sim642)\
**Post date:** [November 3, 2024, 9:08pm UTC](https://discuss.ocaml.org/t/val-top-level-string-string/15547/2 "2024-11-03T21:08:43Z")

</div>

mdx seems to expose the mdx.top library which seems to have a simple enough interface: [mdx 2.4.1 (latest) · OCaml Package](https://ocaml.org/p/mdx/latest/doc/Mdx_top/index.html).

Underneath it builds on the compiler-libs.toplevel library which is a lot less user-friendly.
