Home > front end >  executable comments in OCaml
executable comments in OCaml

Time:10-06

In Haskell, there is this handy feature where one can write a special comment, and it will be evaluated in the context of the file.

Is there a similar feature in the OCaml ecosystem ?


Details in Haskell : enter image description here

When I click "refresh", the expression evalT2 IType ex1 will be (re)evaluated, and its output printed below


In OCaml, I would do something along

(** cd /ml/compil
    dune utop
    utop # #require "compil";;
    utop # Compil__Compil_intf.x;;
    - : int option = Some 256
    *)

CodePudding user response:

There is, to my knowledge, no assimilable functionality "included" in the language, however, it is possible to build interactive documents using Notebook Jupyter, which has an OCaml kernel.

In addition, during the writing of the second version of Real World OCaml, a tool, mdx was designed to simplify the integration of code fragments into textual content, making these fragments executable and approaching the "literate" approach to programming.

The tool (mdx) integrates quite well with Dune (OCaml's canonical build-system) and allows "unit test-like" execution of Markdown documents and is being integrated seamlessly into ODoc (OCaml's new documentation generator). You can see some examples on this project which uses mdx to make the examples executable and share the test suite: Preface Guide.

  • Related