Home InternationalLearning OCaml: Printing Data Structures...
International⭐ Featured

Learning OCaml: Printing Data Structures

If there’s one thing that frustrated me early on in my OCaml journey, it was printing stuff. In Ruby I can p anything and get a useful representation. In Clojure, prn just works on every data structure. In OCaml? There’s no generic print that works on any type – the type information is erased at runtime, so the language simply doesn’t know how to stringify an arbitrary value.

6 April 2026 at 07:45 pm
1 views
Learning OCaml: Printing Data Structures

Learning OCaml: Printing Data Structures

When I first started learning OCaml, one of the most challenging aspects was figuring out how to print data structures. In languages like Ruby, you can simply use the `p` command to print any object, and it provides a useful representation of the data. Similarly, in Clojure, the `prn` function works seamlessly on any data structure, offering a straightforward way to inspect values. However, OCaml is different. The absence of a generic print function that works on any type can be quite frustrating, especially for beginners.

The reason behind this difference lies in OCaml's type system. Unlike Ruby or Clojure, OCaml is a statically typed language, which means that type information is preserved at compile time but erased at runtime. This erasure means that the language doesn't retain enough information at runtime to automatically determine how to convert an arbitrary value into a string. As a result, developers must explicitly define how their data structures should be printed.

To address this challenge, OCaml provides a mechanism for defining custom string representations for data types. One common approach is to use the `String` module's `string_of_xxx` functions, where `xxx` is the name of the data type. For example, if you have a custom data type called `my_type`, you would define a function named `string_of_my_type` that takes a value of type `my_type` and returns its string representation.

Let's consider a simple example. Suppose you have a data type representing a point in a 2D plane:

```ocaml

type point = { x: int; y: int }

```

To print a `point` value, you would define a `string_of_point` function:

```ocaml

let string_of_point p =

sprintf "(%d, %d)" p.x p.y

```

Now, when you use the `print_string` function with a `point` value, it will automatically call `string_of_point` to convert the value to a string:

```ocaml

let p = { x = 3; y = 4 }

print_string (string_of_point p)

```

This output would be `(3, 4)`, which is a clear and concise representation of the point.

However, defining a `string_of_xxx` function for every data type can be tedious, especially for complex projects with numerous types. To simplify this process, OCaml offers the `Deriving` module, which allows you to automatically derive string representations for data types. The `Deriving` module works by generating the necessary `string_of_xxx` functions at compile time, based on the structure of the data type.

To use `Deriving`, you first need to include the necessary modules:

```ocaml

open Deriving

open Deriving.String

```

Then, you can define your data type and derive the string representation:

```ocaml

type point = { x: int; y: int } deriving (Show)

```

The `Show` derive instance tells the compiler to generate a `string_of_point` function. Now, you can print `point` values directly:

```ocaml

let p = { x = 3; y = 4 }

print_string (show p)

```

The `show` function is provided by the `Deriving.String` module and serves as a generic way to convert values to strings. It internally calls the appropriate `string_of_xxx` function based on the type of the value.

In addition to `Deriving`, OCaml also provides the `Format` module, which offers a powerful and flexible way to format strings. The `Format` module allows you to create reusable format specifications and apply them to values. This can be particularly useful when dealing with complex data structures or when you need to generate formatted output for various purposes.

For example, to print a `point` value using the `Format` module, you could define a format specification:

```ocaml

let point_fmt = "%d, %d"

let print_point p =

format "Point: %a" point_fmt p

```

Here, `%a` is a placeholder that expects a value and applies the corresponding `string_of_xxx` function. The `point_fmt` specification tells the `Format` module to print the `x` and `y` coordinates of the `point` value, separated by a comma.

Using the `Format` module, you can create highly customized and reusable string formatting solutions. This can be especially useful when working with complex data structures or when you need to generate consistent output across different parts of your program.

In conclusion, while printing data structures in OCaml can be more challenging than in dynamically typed languages like Ruby or Clojure, the language provides several mechanisms to address this need. By defining custom `string_of_xxx` functions, leveraging the `Deriving` module, or using the `Format` module, developers can create clear and informative string representations of their data. Understanding these tools and techniques is essential for effective OCaml programming, particularly when working with complex data structures and needing to inspect or debug values during development.

Source: OCaml Planet
📰 Related News
Ollama 0.2.6 Released with Native Gemma 4 Support and Enhanced Performance
Ollama 0.2.6 Released with Native Gemma 4 Support and Enhanced Performance
Ollama 0.2.6 is now live, featuring native support for Google's Gemma 4 models and improved local inference performance for Windows, macOS, and Linux.
14 Apr
Weekly news roundup: Shortages spread to MLCCs; SK Hynix reportedly in talks with Microsoft and Google
Weekly news roundup: Shortages spread to MLCCs; SK Hynix reportedly in talks with Microsoft and Google
Below are the most-read DIGITIMES Asia stories from the week of April 6-April 13, 2026:
14 Apr
cutile-stencil 0.2.0
cutile-stencil 0.2.0
An xDSL-based stencil compiler that generates optimized GPU kernels via NVIDIA cuTile
14 Apr
merlin-llm added to PyPI
merlin-llm added to PyPI
Merlin — a fast local LLM for agentic coding on Apple Silicon
14 Apr
Fluent Cut - Craft and compose videos programmatically in PHP with an elegant fluent API
Fluent Cut - Craft and compose videos programmatically in PHP with an elegant fluent API
Craft and compose videos programmatically in PHP with an elegant fluent API - b7s/fluentcut
14 Apr
Crypto Investor at Center of Trump Corruption Allegations Now Sees Himself as ‘Victim’
Crypto Investor at Center of Trump Corruption Allegations Now Sees Himself as ‘Victim’
Justin Sun has accused Trump-affiliated World Liberty Financial of misconduct and a general lack of transparency.
14 Apr
nvidia-nat-weave 1.7.0a20260413
nvidia-nat-weave 1.7.0a20260413
Subpackage for Weave integration in NeMo Agent Toolkit
14 Apr
nvidia-nat-s3 1.7.0a20260413
nvidia-nat-s3 1.7.0a20260413
Subpackage for S3-compatible integration in NeMo Agent Toolkit
14 Apr
Social Security Trust Fund to Run Dry in 2032: Just 6 Years From Now
Social Security Trust Fund to Run Dry in 2032: Just 6 Years From Now
Six years. That is how much time separates retirees from a Social Security system that, by its own projections, runs out of money. If you are 56 years old...
14 Apr
cane-gpu-perf added to PyPI
cane-gpu-perf added to PyPI
GPU inference benchmarking with opinionated diagnostics
13 Apr