Taking OCaml and Eio for a Spin

mattjhall1 pts0 comments

Taking OCaml and Eio for a spin - Matt Hall

Matt Hall

About

Now

Projects

Blog

RSS

2026-07-15

Taking OCaml and Eio for a spin

My first blog post on this website was about<br>Haskell. I've always been interested in functional programming but the<br>complexity of Haskell never seemed worth.

I have been vaguely aware of OCaml for a long time but never dipped my toes in.<br>It has been having a bit of a renaissance recently with multicore support,<br>effects and a more systems focussed spin called OxCaml<br>from Jane Street. Now seems a great time to learn it and I was curious about<br>Eio, a concurrency framework. It uses the aforementioned effects but more<br>interesting to me is it is meant to be deterministic, which could be<br>helpful for testing.

Books & Tutorials

The first thing to do is pick a tutorial or a book. A quick google search later<br>I had found Real World OCaml. Assuming that<br>it would be similar to Real World Haskell (which I enjoyed and was very hands<br>on) and assuming I already had sufficient FP chops, I dived in.

Impatient to look at syntax and semantics I skipped over the more prosey<br>Prologue. If I had read it I would have been told that the book chooses to use Jane<br>Street's Base standard library replacement. It seems that historically the<br>real standard library was quite anemic, only intended to cover the needs of<br>the compiler. This is no longer true and it has slowly been growing. Still, it<br>doesn't seem unreasonable to me that a book called Real World OCaml use the<br>libraries from the biggest real world user of the language.

Anyway, I surely learnt my lesson to read things more carefully and went back to the beginning to<br>start again. The first<br>half of the book is well written and I happily followed along. I started to get<br>lost around GADTs and first class modules though. I think the book lacks for<br>exercises, so once something unfamilar comes up you don't have the full grasp of<br>the basics to bail you out.

I went searching for something a bit easier and found<br>CS3110 which has both videos and<br>text to suit all tastes. It also had exercises which helped a lot. I would<br>suggest to people that they start with this course and then graduate onto RWO<br>for the more advanced features. I like RWO although if<br>you are looking for a project based book like RWH, you'll be disappointed.

How It Feels In The Hand

Overall OCaml is very enjoyable to write code in. It has a nice mix of<br>functional and imperative programming. For example, to loop through a list you<br>are either using a recursive function or List.map and friends:

let print_recursive list =<br>let rec loop i list =<br>match list with<br>| [] -> ()<br>| x :: xs -><br>Printf.printf "[%d] => %i";<br>loop (i+1) xs<br>in<br>loop 0 list

let print_fn = List.iteri (fun i x -> Printf.printf "[%d] => %i\n" i x)

let () =<br>print_recursive ["foo"; "bar"; "baz"];<br>print_fn ["foo"; "bar"; "baz"];

(*<br>[0] => foo<br>[1] => bar<br>[2] => baz<br>[0] => foo<br>[1] => bar<br>[2] => baz<br>*)

but you still have mutable variables and the like if you need them:

type client = { mutable request_id : int }

let dispatch cli ~msg =<br>let request_id = cli.request_id in<br>cli.request_id request_id + 1; (* update request_id *)

Printf.printf "Dispatching %d/%s to server\n" request_id msg

let () =<br>dispatch_cli ~msg:"Hello";<br>dispatch_cli ~msg:"World"

(*<br>=> Dispatching 0/Hello to server<br>=> Dispatching 1/World to server<br>*)

I will say that the syntax feels quite wordy, e.g. the let .. in, match .. with constructs. I think syntax is the least interesting part of a<br>programming language to me - I was happy even back in the days where Rust had<br>sigils - so it doesn't bother me too much. Having named arguments (with the<br>~) is very nice.

The compiler bothers me more. When it encounters an error it seems to give up on<br>the rest of the file. This makes the iteration loop quite slow -<br>write code, get an error, fix the error, rebuild. There's very little chance to<br>fix errors in bulk, especially if you're focussed on one module.

Eio

Eio is an effects-based IO concurrency<br>library. It's relatively new but it has some interesting features such as<br>io_uring support. Ultimately my goal is to write an implementation of the<br>Raft consensus algorithm and try test it.

The Raft paper says to use remote procedure calls (RPCs) to communicate between<br>the servers. A dumb RPC client/server seems like a great first project. Let's<br>come up with a very simple protocol: a procedure call has a request id (int), a<br>name (string) and a single argument (string). Integers are a single byte and a<br>string is serialised as a byte for the length and then the data. As an example,<br>to call the Echo procedure with Hello as request 10, you would send:

00000000: 0a04 4563 686f 0548 656c 6c6f ..Echo.Hello

A response will just be the request id and the string return value.

Before we start, we need to familiarise ourselves with some jargon Eio uses for<br>its building blocks:

Fiber - a thread of execution. Not a system thread though!

Switch - something that groups fibers together. A...

ocaml list request_id book world printf

Related Articles