OCaml Weekly News

Previous Week Up Next Week

Hello

Here is the latest OCaml Weekly News, for the week of November 21 to 28, 2023.

Table of Contents

Riot v0.0.3: an actor-model multi-core scheduler for OCaml 5

ostera announced

Hi folks! :wave: wanted to share something I’ve been working on these last few weeks.

Riot is an actor-model multi-core scheduler for OCaml 5. It brings Erlang-style concurrency to the language, where lightweight processes communicate via typed message-passing.

Why Actors?

It raises the abstraction of multi-core so you don’t have to think about threads and low-level multithreading primitives, and instead can focus on structuring your application in terms of hierarchies of processes that do some work, and how they communicate with each other.

Erlang (and by extension Elixir) has been doing this for impure functional programming for decades now and in my experience it is an incredibly productive way of working. So I’m hoping to establish the foundation for doing this kind of application development but with types.

NB: This is the dual of the Caramel project, which is an alternative backend for OCaml that compiles to Core Erlang, thus letting you run some OCaml on the Erlang VM.

Getting Started

You can get started with: opam install riot

  • Hello world

    The simplest Riot program you can write is this:

    Riot.run @@ fun () -> print_endline "hello world"
    

    But there’s a small caveat here: this program doesn’t terminate.

    Riot, like the Erlang VM, is designed for long-running applications. Despite this, it starts up reasonably fast (4ms on a 10-core M1 Max). To terminate the runtime we must call ~Riot.shutdown ().

    The smallest Riot program is then: Riot.(run shutdown)

Hello world, with messages!

The next smallest Riot program you can write is a hello-world that uses Processes and Messages:

open Riot
type Message.t += Hello_world

let () =
  Riot.run @@ fun () ->
  Runtime.Log.set_log_level (Some Info);
  let pid =
    spawn (fun () ->
        match receive () with
        | Hello_world ->
            Runtime.Log.info (fun f -> f "hello world from %a!" Pid.pp (self ())))
  in
  send pid Hello_world

Hello world, with applications

Once you want to start building applications with supervision trees, to nicely decouple the subsystems of your app, you can use the Application interface, and Riot.start ~apps:

module My_app = struct
  let start () =
     let pid = spawn_link (fun () -> (* ... do work! ... *)) in
     Ok pid
end

Riot.start ~apps:[
  (module Riot.Logger); (* we want to start the logger app first *)
  (module Riot.Telemetry); (* we then start the telemetry app *)
  (module My_db); (* then start the database server *)
  (module My_app); (* finally we start our app *)
] ()

Next Steps

If you’re interested in trying Riot, I’ve started working on a tutorial that starts from zero and works its way up. It’s not complete yet but it should get you up and running and building interesting programs!

I’d love any feedback y’all have about this :) especially interested in what would you like to build with it or what libraries you’d like to use in this space.

Also happy to answer any questions :D

/ Leandro

Caper 0.9

niksu announced

Caper has reached v0.9. It is a tool for understanding and processing “pcap expressions” (also known as tcpdump filters) which are used for network packet analysis. It is entirely written in OCaml and includes pcap analysis logic, a from-scratch BPF compiler, and conversion to/from English expressions.

You can use Caper online through the BPF Exam site.

Caper’s README contains motivation, building, and usage examples, and its CHANGELOG describes recent updates.

A huge thanks goes to Caper’s contributers. Further contributions and feedback are welcome – a list of contribution ideas is included on Caper’s web page.

OCaml User Survey 2023

Kim Nguyễn announced

Hi everyone,

we are delighted to announce the OCaml User Survey, 2023 edition. With this survey, the OCSF is trying to get a better picture of the OCaml community and its needs. It would be very helpful if you could take a few minutes (10 to 15) to fill the survey and share it with other OCaml programmers.

https://forms.gle/iteoR7QMzFXw2mp89

The survey is run by the OCaml Software Foundation . It builds on previous iterations. The results will be published here on discuss and on the website of the OCSF . The OCSF would like to thank all the people that have helped in devising this or previous versions of the survey. Don’t hesitate to give us your feedback (you can post here or send me a message/email).

The survey will remain opened until December 8th 2023 (AOE). Please take the survey and don’t hesitate to share the link!

Some remarks regarding this year’s survey:

  • Most of the questions are similar to the ones in the previous survey, some have been fixed, some have been updated (e.g. to list more recent versions of the OCaml compiler)
  • A new section at the end on Demographics, Diversity and Inclusion. The related questions are more personal but again completely optional and would greatly help us get a better picture of our community and areas were the OCSF can help improve things
  • Again, we resorted to using Google Forms. I spent quite some time trying to find a viable alternative, but none were satisfactory (either from a technical, accessibility or bureaucratic point of view). I understand that this can be a disappointment for some. If you are interested in running the survey next year on behalf of the OCSF, get in touch!

Set up OCaml 2.1.2

Sora Morimoto announced

This release contains a workaround for an upstream issue that is causing post process to become incredibly slow. If you would like to access related information, check out the PR. https://github.com/ocaml/setup-ocaml/pull/724

https://github.com/ocaml/setup-ocaml/releases/tag/v2.1.2

OCaml.org Newsletter: October 2023

Sabine Schmaltz announced

Welcome to the October 2023 edition of the OCaml.org newsletter! This update has been compiled by the OCaml.org team. You can find previous updates on Discuss.

Our goal is to make OCaml.org the best resource for anyone who wants to get started and be productive in OCaml. The OCaml.org newsletter provides an update on our progress towards that goal and an overview of the changes we are working on.

We couldn’t do it without all the amazing OCaml community members who help us review, revise, and create better OCaml documentation. Your feedback enables us to better prioritize our work and make progress towards our goal. Thank you!

This month, our priorities were:

  • Learn Area: We continue our efforts to make OCaml.org a great resource for learning OCaml. This month, we’ve validated a new version of the designs for the Learn area, and we’ve published two new documentation pages!
  • Outreachy Internship Application Period: OCaml.org is participating in the Outreachy Internship Program with two internship projects. As part of the application period, we’ve received tons of fantastic contributions from Outreachy applicants!
  • General Improvements: As usual, we also worked on general maintenance and improvements based on user feedback, so we’re highlighting some of our work below.

Learn Area

Outreachy

OCaml.org is participating in the Outreachy Internship Program. Outreachy provides internships to people subject to systemic bias and impacted by underrepresentation in the technical industry where they are living.

A substantial part of this month has been spent on creating issues, reviewing pull requests, and mentoring Outreachy applicants on the OCaml Discord server.

The contributions include small fixes, implementing Figma designs, and small feature additions. Notably, the OCaml Changelog now has an RSS feed, and some outstanding designs for the package area have been applied.

8 out of the 21 medium difficulty issues have been completed in October, while only 6 out of 30 simple outreachy issues remain open. For the remaining issues, we will support the contributors in finishing their work, and free any abandoned issues so that community members can pick them up.

Relevant PRs and Activities:

General Improvements

This month, we’re welcoming 1 new contributor:

Relevant PRs and Activities:

  • We now log a message instead of crashing when failing to parse the opam file - ocaml/ocaml.org#1575

dream-html 1.2.0

Yawar Amin announced

Small addition to allow checking if a node or attr is ’null’ (i.e. empty). This can be useful when you get a node or attr passed in to your function and you need to decide what to render depending on whether it’s empty or not.

As a reminder, ’null’ or empty nodes and attributes are ones which are simply not rendered into the final HTML.

New Draft Tutorial on Polymorphic Variants

Cuihtlauac Alvarado announced

Dear OCamlers,

The OCaml.org continues working on new tutorials. We have a draft on polymorphic variants; we’d like your feedback on it:

Previously announced tutorials form a series.

  1. Installing OCaml
  2. A Tour of OCaml
  3. Your First OCaml Program
  4. Values and Functions
  5. Basic Datatypes and Pattern Matching

But this one is not intended to follow right after those. It is designed to be taken by people familiar with OCaml’s basics and willing to master polymorphic variants.

As a draft, it has gaps, most notably:

  • The section on Performance Drawbacks needs to be strengthened
  • An example inspired by @garrigue “Code reuse through polymorphic variants” paper is missing

Share your feedback here or in GitHub, but do not use the “Contribute” link at the bottom of the page.

Hope it helps

Ppxlib dev meetings

Continuing this thread, Sonja Heinze said

It was a nice and short meeting with @ceastlund and me. Here are the meeting notes: https://github.com/ocaml-ppx/ppxlib/wiki/Dev-meeting-21-11-2023

varray 0.2

art-w announced

Hello everyone,

I’m not so pleased to announce a bugfix release of the varray package on opam. This library provides an implementation of dynamic arrays, which automatically resize as elements are added or removed from the array. It’s based on the really fun paper “Tiered Vectors: Efficient Dynamic Arrays for Rank-Based Sequences” by Michael T. Goodrich and John G. Kloss II. When I first heard about it, I could not resist implementing this datastructure because its algorithmic complexities are rather fancy:

  • O(1) to get/set elements anywhere in the array
  • O(1) to add or pop an element at the front/back of the array
  • O(ᵏ√N) to add or pop an element anywhere in the middle of the array (for any k >= 1)

And because there’s a fun way of exposing the API using OCaml’s functors:

module One   = Varray.Circular   (* k=1 => O(N) complexity for insert/delete *)
module Two   = Varray.Root (One) (* k=2 => O(√N) complexity *)
module Three = Varray.Root (Two) (* k=3 => O(³√N) complexity *)

More details on the github repo and the online API documentation (which includes a teaser for odoc upcoming search feature, thanks to @EmileTrotignon, @panglesd and the wonderful odoc team :heart: )

Yet, I never publicly announced the initial release of this package on opam… because I don’t think dynamic arrays are actually useful when programming in OCaml! Using integer indices to address elements is prone to “index out of bounds” bugs, so it’s rarely the right choice. If you do have a usecase for them, I would love to hear it :)

But ok, so, wait, why am I announcing it now? Well because @n-osborne found a bug in my code which could trigger a segfault! This terrible bug was only affecting the lesser-used delete_at operation, and so it lay dormant for two years. This was a very dumb mistake: This specific function was missing a check for out of bound indexes (ha!)

While I would prefer to avoid the public walk of shame, how the bug was discovered is too cool not to share: @n-osborne and @shym wrote a Gospel specification of how the Varray library should behave, which enabled them to stress test it and discover a counter-example where my code was misbehaving. Here’s an extract of the faulty function specification, where Gospel specs are written as special comments (*@ ... *) in the mli documentation:

type 'a t
(*@ mutable model contents : 'a sequence *)
(* ^^^ Gospel modelization of the abstract varray type
       as a "mutable sequence called contents" *)

val delete_at : 'a t -> int -> unit
(** [delete_at t i] removes the element [t.(i)].
    Every element on the right of [i] is shifted by one to the left. *)
(*@ delete_at t i
    checks inside i t.contents
    modifies t.contents
    ensures t.contents = old (t.contents[..(i - 1)] ++ t.contents[(i + 1)..]) *)

Note the use of the keyword old on the last line, which allows the postcondition ensures to refer to the state of the input before it was imperatively modified. Formal specifications are generally reserved to specialists, but look how readable this is! (see the full varray spec)

Gospel specs can then be interpreted by a variety of tools. To discover this specific bug, @n-osborne used Ortac to automatically translate his specification into an executable QCheck-STM test to search for incoherencies between the model and the actual implementation.

Very very cool stuff. If you are looking for more resources on the subject, I found Chapter 2 of Clément Pascutto’s PhD thesis to be a very nice introduction to Gospel specifications :) (and later chapters describe his cutting edge research for Ortac!)

zapashcanon then said

For those speaking french and interested in Gospal/Ortac, there’s a video of Clément’s talk given at the oups meetup last year.

First release of urn: Urns for fast functional random sampling

Justin Frank announced

Hi all! I’m pleased to announce the first release of urn a library that implements urns, a very nifty pure functional data structure for randomly sampling with or without replacement from weighted discrete distributions in log time. It provides implementations for using ~int~s and ~float~s as weights, though it can be extended to any number like thing that can be uniformly sampled from.

The package is available through opam with opam install urn, and is distributed under the MIT license.

Draft Tutorial on Mutability, Loops, and Imperative Programming

Cuihtlauac Alvarado announced

Dear OCamlers,

The OCaml.org team has yet another draft tutorial. This time, the title is: “Mutability, Loops, and Imperative Programming”. We want your feedback on it:

The target audience is developers learning OCaml. No functional programming knowledge is assumed. However, it comes after the “Get Started” series:

  1. Installing OCaml
  2. A Tour of OCaml
  3. Your First OCaml Program

And it comes at the end of the “Introduction” series:

  1. Values and Functions
  2. Basic Datatypes and Pattern Matching
  3. If Statements and Recursions
  4. Lists
  5. Labelled & Optional Arguments
  6. Mutability, Loops, and Imperative Programming

As the previously announced draft on polymorphic variants this one contains overlooked issues. We want to make it better with your help.

Share your feedback here or in GitHub, but do not use the “Contribute” link at the bottom of the staging page.

Hope it helps

First release of pretty_expressive: A Pretty Expressive Printer

Sorawee Porncharoenwase announced

Hi everyone!

I am happy to announce the release of pretty_expressive, an implementation of A Pretty Expressive Printer (OOPSLA’23).

Unlike other pretty printers in the OCaml ecosystem which are mostly focused on the OCaml style (as far as I can tell), pretty_expressive is general-purpose, making it suitable for formatting various styles (the pretty printer was originally created to format programs in the Racket language). pretty_expressive is further distinguished from other general-purpose pretty printers (e.g., Hughes’, Wadler’s, Bernardy’s) by its expressiveness and optimality.

This is also my first OCaml project (not counting Reason). Any feedback is welcome!

Thanks, Sorawee

Other OCaml News

From the ocaml.org blog

Here are links from many OCaml blogs aggregated at the ocaml.org blog.

Old CWN

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 to the caml-list.