2 November 2026 – 22 November 2026
Taking a break
Over the past few weeks, I’ve gotten quite busy with lecture courses/supervision, as I’ve ended up taking all of the ones offered this term (Part II CST students usually take around seven of 13 lecture courses offered across the year, but I’m doing six just this term) and also had a huge number of automated tests and interviews to go through for internships. I’m pleased to say that that search is over, and I’ll be working at Jane Street this summer! Seems like there’s a lot of really exciting work going on there so I’m looking forward to it.
Unfortunately, all this means my time to work on the project has been limited. I’m a bit behind where my project proposal timeline suggests I should be, but thanks to a few intense days of OCaml-writing I’ve not got too much to do to catch up, and my proposal has the next few weeks scheduled as holiday/catch-up time anyway.
The progress I’ve made has all been around the OSM XML parser I was thinking about last time, which I’ve now finished implementing!
Parsing OSM
Building off the research I did, I knew I wanted to use xmlm as a streaming parser for the XML. I decided the best way to allow for streaming the data through memory is to provide a fold/iter style interface, then if the user of the parser wants to accumulate elements in a more functional (fold) or imperative (iter) style they have the option to do either, and can choose what, if anything, they want to preserve in memory themselves. For example, if you just wanted to load the data from an OSM XML file in a similar way to the non-streaming osm_xml library and then print it out, you could do the following:
open Osmq
(* osm_xml style output record *)
type elements = {
nodes : Raw.Node.t list;
ways : Raw.Way.t list;
relations : Raw.Relation.t list;
}
[@@deriving show]
let () =
let els =
Xml_parser.fold
(fun els -> function
| Raw.Node n -> { els with nodes = n :: els.nodes }
| Raw.Way w -> { els with ways = w :: els.ways }
| Raw.Relation r -> { els with relations = r :: els.relations })
{ nodes = []; ways = []; relations = [] }
(Xml_parser.File Sys.argv.(1))
in
Format.printf "%a" pp_elements elsIn order to (re-)familiarise myself with writing OCaml and using xmlm, I wrote a parser for a simpler file format myself in plain OCaml and some more manual parsing of OSM data with xmlm in a more notebook-y fashion. This was super helpful, and I’m really glad I did it, as when I went into writing the main parser I was much more confident in my abilities and familiar with xmlm concepts1 and the OSM XML format2, so could focus more on the structure/design of my program. I also read through some guides on how to write modern, idiomatic OCaml and how to structure OCaml projects, which has left me with things broken down into quite a handful of modules with somewhat abstract interfaces, but in a way that I think is quite elegant.
I was also reminded of a great blog post I read a few years ago, “Parse, don’t validate”, which emphasises trying to ensure that your types/data structures make illegal states unrepresentable and push the “burden of proof” (as they call it) to the boundary of the program, i.e. reject illegal input at the point of input rather than storing it in some way that could cause a problem later. Given this, and that OSM XML files aren’t really made to be hand edited and are generally generated directly by a web API (e.g. Overpass) or tools like osmium, the parser rejects any input that doesn’t conform with the de facto OSM standard in a way that introduces ambiguity. The alternative would be silently making a choice about how to interpret the ambiguity, potentially leaving malformed data structures. As an example of using types to prevent this sort of error, the Osmq.Id module in the parser provides an abstract interface to IDs distinguishing between node/way/relation IDs. These are all actually implemented using a single int at runtime with no extra overhead, and share implementations of equal/compare/hash/etc., but the type system prevents you from e.g. accidentally adding an Id.way_id to an Id.node_id list. Additionally, the base types are defined as polymorphic variant tags, so you can express the idea of taking “an ID that could be for a node or for a way” as [ `Node | `Way ] Id.t (and some coercion).
The repository is here for anyone interested: https://github.com/mjocc/osmq, and the documentation here: https://mjocc.org/osmq/osmq/Osmq/index.html (it’s a bit lacklustre right now but I’ll be expanding it soon). There’s much more to discuss, but I didn’t want to make this (three)weeknote too long, so please get in touch with any questions (mjo52 [at] cam dot ac dot uk)!
Performance
Compared to the existing OCaml library osm_xml, mine already has comparable/slightly better performance even before I’ve properly started optimising it:3
My library parsing all the data OSM has on Falkland Islands and storing it in memory
The main existing OCaml OSM XML parsing library on the same data set
It also provides much stronger type guarantees than osm_xml does. For example, osm_xml uses a plain string for its ID types while mine uses the abstract type described above. My parser also has a lower memory profile, in part due to it using far fewer layers of boxed types to represent the underlying objects (e.g. using ints to represent IDs and (converted-to-Unix) timestamps where osm_xml uses strings).
Next steps
The first thing to do next is definitely writing expect tests and property-based tests for my library, which I ran out of time to do. Moving on from that, I might spend a small amount of time doing a bit of microbenchmarking and optimising the parser (e.g. by changing the implementation of some of my abstract interfaces like moving Tags.t from being a string map to an association list), before starting to designing the incompleteness semantics and working on the query engine. I decided the parser wouldn’t attempt to resolve IDs to the objects that reference, so we have more flexibility in the query layer to deal with incompleteness.
I have a Quantum Complexity Theory exam for my module this term next week, so that will take priority for now, but I’m looking forward to keeping going after that.
Things I read this week
- Error Handling - Real World OCaml
- Files, Modules, and Programs - Real World OCaml
- Abstraction Functions and Representation Invariants
- Variants - Real World OCaml
- OCaml - Polymorphic variants
- Parse, don’t validate
- Xmlm docs
- OSM XML - OpenStreetMap Wiki
- And many existing OCaml library interfaces that I have forgotten to note down
Footnotes
-
It’s worth noting I slightly modified
osm_xmlfor this test, as I had to adapt it to allow it to read the OSM export that was being tested (from Geofabrik, which removes user information), but this was a minor modification involving changing a handful of record fields to be options rather than definite values, so shouldn’t have changed much. ↩