OCaml Weekly News

Previous Week Up Next Week


Here is the latest OCaml Weekly News, for the week of July 20 to 27, 2021.

Table of Contents

pyre-ast: full-fidelity Python parser in OCaml

Jia Chen announced

I am happy to announce the initial opam release of pyre-ast, a Python parsing library.

The library features its full-fidelity to the official Python spec. Apart from a few technical edge cases, as long as a given file can be parsed/rejected by the CPython interpreter, pyre-ast will be able to parse/reject it as well. Furthermore, abstract syntax trees obtained from pyre-ast is guaranteed to 100% match the results obtained by Python's own ast.parse API, down to every AST node and every line/column number.

Another notable feature of this library is that it represents the Python syntax using the tagless-final style. This style typically offers more flexibility and extensibility for the downstream consumers of the syntax, and allow them to build up their analysis without explicitly constructing a syntax tree. That said, for developers who are less familiar with the tagless-final approach, we also offer alternative interfaces that operates on traditional syntax tree represented as algebraic data types.

Documentation of the library can be found here.

The reason why we can can claim full-conformance with CPython is really simple: the library is, under the hood, merely an OCaml wrapper around the parsing logic in CPython source code. The project was initially motivated to replace the custom menhir-based parser currently used in the Pyre type checker (hence the name), but I figured that it would be useful to release this as a standalone opam package to the community so other static Python analyzers or other DSLs with Python-based syntax can leverage it as well.

The library has yet to be put into production for Pyre (I'm working on it though) so please do expect bugs/jankiness at times. Feedback and bug reports are very welcomed.

Happy parsing!

OCaml+Opam Images for Docker for Windows

Antonin Décimo announced

I'm glad to announce the availability of OCaml and opam [native Windows Container][windows-containers] images for Docker for Windows. This is the result of my hard work at Tarides, with precious help from @dra27, @talex5, @avsm, and the rest of the team.

They can be found under the [ocaml/opam][hub] repository in the Docker Hub. Try them with [Docker for Windows][docker-for-windows]! Be sure to [switch Docker to Native Windows Containers][enable-native].

docker run -it ocaml/opam:windows-mingw
docker run -it ocaml/opam:windows-msvc

We provide images for the mingw-w64 (from OCaml 4.02 to 4.12) and the MSVC (from OCaml 4.06 to 4.12) ports. They are based on each release of Windows 10 amd64 currently supported by [Microsoft on the Docker Hub][mcr]. The images use opam 2.0, and we plan to update to opam 2.1 when it's released. The images also ship a [Cygwin][cygwin] installation, [Git for Windows][git-for-windows], and the [winget package manager][winget].

We use @fdopen's [OCaml for Windows][ocaml-for-windows] distribution and opam-repository fork. As it is getting deprecated at the end of August 2021, we'll transition to opam 2.1 and the standard opam-repository when that happens.

In order to get the correct environment for any RUN command involving OCaml or opam, prefix the command with

  • ocaml-env exec --64 -- if based on mingw-w64; or
  • ocaml-env exec --64 --ms=vs2019 -- if based on MSVC.

The images are built at https://base-images.ocamllabs.io/, using an [OCurrent][ocurrent] pipeline that [builds Docker images][docker-base-images]. You can rebuild them yourself using the [OCluster][ocluster] set of tools that I have ported to Windows.

We provide a comprehensive set of tags (replace port with either mingw or msvc):

  • windows-port: the latest version of OCaml for each Windows version;
  • windows-port-winver: the latest version of OCaml for Windows 10 winver;
  • windows-port-ocaml-mlver: OCaml version mlver for each Windows version;
  • windows-port-winver-ocaml-mlver: OCaml version mlver for Window 10 winver.

When the Windows version is not specified in the tag, the image is a multiarch image that will work on every supported version of Windows 10. Docker automatically selects the appropriate one based on the host version.

We will be using these images in the upcoming ocaml-ci and opam-repo-ci for Windows.

Further work on these include the transition to opam 2.1, and we'll provide the Cygwin port of OCaml when it's fixed upstream and available in the Cygwin package repository.

Happy hacking!

Borns a stream talking about OCaml/Reason & ReScript language

David Sancho announced

I'm very excited to announce starting a new show in Twitch to bring OCaml, Reason and ReScript community best brains to casually talk. It's called emelleTV

It's made by @fakenickels and myself @davesnx, and we will try to do our best!

Our first guest is @___zth___


We go live on http://twitch.tv/emelletv next Wednesday. Subscribe to not miss it!

Thanks for reading, hope to see you there!

An Update on the State of the PPX Ecosystem and ppxlib's Transition

Sonja Heinze announced

I hope you're all having a nice summer (or a nice whichever season you're in, of course)! We've set up a new wiki page on the ppxlib repository containing a status overview of the current ppxlib transition, which aims at keeping the PPX ecosystem always up-to-date. We'll keep that wiki page up-to-date, as well.

@jeremiedimino and @NathanReb have already explained our three-part plan for this transition in different posts here on discuss. Nothing has changed in that plan, but it has been a while since we last posted about the overall transition and even longer since we last posted about the Astlib transition in detail. So if you want, you can refresh your memory about that transition and get updated about its current state (in more detail than the new wiki page) by reading this post.

Which Issues ppxlib was Facing

With ocaml-migrate-parsetree (OMP), the PPX ecosystem became cross-compiler-compatible. With ppxlib, the latest compiler features were supported more easily and broadly within the PPX ecosystem, while ppxlib also brought along other improvements such as the one in performance and the clear composition semantics when using several PPXs. With that, both OMP and ppxlib have taken away several maintenance burdens from the PPX maintainers and have created a more homogeneous and up-to-date PPX ecosystem. However, we were facing the following issues:

  1. To keep the PPX ecosystem cross-compiler compatible
    1. ppxlib was handling parts of the unstable compiler-libs API to abstracting them away;
    2. the OMP~/~ppxlib maintainers needed to keep the AST migration information up-to-date by coordination with the compiler devs.
  2. To guarantee new feature support, ppxlib needed to bump the ppxlib AST to the newest version.
  3. Bumping the AST implies a breaking change. That was an issue for a homogeneous and up-to-date PPX ecosystem.
  4. Not all PPXs migrated from OMP to ppxlib. That was also an issue for a homogeneous and up-to-date PPX ecosystem.

Some time ago, there was the very ambitious plan of tackling Issues 1, 2, and 3 all at once by writing a stable AST abstraction and upstreaming it to the compiler. That plan has been put on ice for now. Instead we're currently on track with a more down-to-earth plan, outlined below.

Tackling the Issues in Three Parts

The plan we're currently following contains three simultaneous parts. It approaches three of the four issues I've pointed out above. However, it leaves the need to bump the AST (Issue 2) untouched.

Part One: Astlib as an Interface between ppxlib and the Compiler

The first part works towards continuous cross-compiler compatibility (Issue 1 above) while making the situation of still having PPXs based on OMP (Issue 4 above) even more of a problem. It consists of implementing an interface module called Astlib between ppxlib and the compiler, then upstreaming it to the compiler. As long as Astlib is stable and up-to-date, the rest of ppxlib won't be affected by any compiler changes—neither by new AST versions nor by compiler library changes.

The first step of this part of the plan was moving the OMP driver and other OMP features from OMP to ppxlib. That was done in August 2020, and it introduced OMP2. Since the PPX driver has to be unique, this was the start of having the PPX ecosystem split into the two incompatible worlds of OMP1 PPXs on one hand and ppxlib PPXs on the other hand.

By now, we have written Astlib as an internal ppxlib library and have reduced ppxlib's compiler library usage as much as possible to keep Astlib minimal. As you can see, it contains a minimal compiler library sub-API in addition to the former OMP modules of our supported ASTs and the migration information between them. We will upstream Astlib to the compiler asking for it to be kept stable and up-to-date, while also keeping our local copy for old compiler support.

Part Two: Sending Patch PRs when Bumping the AST

So, thanks to Part One of the plan, ppxlib will always be compatible with the development compiler OCaml trunk and the newest compiler version. However, to also support the newest compiler features, we need to bump the internal ppxlib AST to the newest version. That modifies some of the AST nodes and so it breaks any PPX that rewrites one of those nodes (Issue 3 above). Usually just a handful of PPXs are affected, but we still want them to be up-to-date.

Our current plan doesn't provide a solution for that problem, but it does make handling the problem more efficient and, once again, it takes away the burden from the PPX maintainers. Since the AST bump to 4.10, whenever we bump the AST, we send patch PRs to the PPXs we break. Not much has changed since February, when @NathanReb last explained our workflow of sending patch PRs in detail. To some it up: we create a workspace with all ppxlib reverse dependencies on opam fulfilling a certain standard, which we call the ppx-universe. We then fix the PPXs that break all at once and open the PRs.

Lately, the ppx-universe has also proven very useful to make well-founded decisions regarding our API by having an easy look at our reverse dependencies. You can find a ppx-universe snapshot, currently from March, on our repo.

In our experience, once the ppx-universe is created and "builds up to the expected breakages," writing a couple of patches takes very little time, so we plan to make the tooling that creates and interacts with the workspace more sophisticated.

Part Three: Porting PPXs to Put an End to the "Split-World Situation"

As explained above, Part One split the PPXs into the two incompatible worlds of OMP1 PPXs on one hand and ppxlib PPXs on the other hand. That made the fact that some PPXs were still based on OMP (Issue 4 above) even more of a problem. For some PPX maintainers, the reason to avoid porting their PPXs to ppxlib was that ppxlib depended on base and stdio, so we decided to tackle this situation by three means:

  • Dropping the base and the stdio dependencies, which was done in August last year. Now, all dependencies are the very basic ocaml, dune, ocaml-compiler-libs, stdlib-shims, sexplib0 and ppx_derivers.
  • Porting and reviewing some of the most important PPXs ourselves. So far we've ported js_of_ocaml, bisect_ppx, and tyxml with the help of the respective maintainers, and we've also reviewed several ports.
  • Spreading the word about the need to port PPXs and asking for help.

About a year ago, we made a non-exhaustive list of PPXs that needed to be ported. Since then, this community has proven to be awesome and there has been an amazing porting effort by a lot of people. So by now, all packages on that list have been ported with the exception of one(*). So hopefully the "split world" situation can soon be considered past. :tada:

By the way, thanks to all involved in porting PPXs to ppxlib! It has been a great joint effort so far. :heart: And if anyone still has or comes across a project somewhere that needs porting and wants to port it, that's awesome!

You can find the full list of opam packages that are still stuck in the OMP1 world by filtering for them in opam's health check pipeline. However, notice that that's a generated list, so it also contains libraries that intrinsically form part of the OMP1 ecosystem (such as ppx_tools_versioned), PPXs that have already been ported but haven't relesed their port on opam yet (such as graphql_ppx), deprecated PPXs that aren't marked as deprecated yet (such as mirage-dns), and several PPXs that only transitively depend on OMP1.

(*) ppx_import has a PR for a port to ppxlib, but it's not quite ready to be merged just yet.

How to send email from Dream

Joe Thomas announced

I’ve written a short blog post about what I learned building simple email features for a web server written in the Dream framework. The accompanying source code is available here:


I’m interested in adding more examples and tutorials to the OCaml ecosystem and would be happy to get your feedback, positive or negative, on this write-up (here or via email/github/discord).


If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.