Unleashing Sum Types in Pure C99

By Temirkhan Myrzamadi

Temirkhan Myrzamadi
Image for post

Some time ago, I was writing a TIFF encoder. A TIFF file consists of a header and a sequence of TIFF entries. To represent a TIFF entry, I wrote this:

typedef struct {
enum {
} tag;

Well, clearly not the best code I ever wrote. The pattern here is called a tagged union — a structure consisting of a tag and a payload. Here, the tag is the enumeration of TIFFEntryValueTagSmall and TIFFEntryValueTagBlock, both of which correspond to small_value and block, respectively: if tag is TIFFEntryValueTagSmall, then small_value must be present, and if TIFFEntryValueTagBlock, then block must be present.

Below the structure itself, the two functions are called value constructors — they construct values of TIFFEntryValue.

To match a TIFF value, a switch statement is used:

switch (entry.tag) {
case TIFFEntryValueTagSmall:
// Work with entry.small_value...
case TIFFEntryValueTagBlock:
// Work with entry.block...

Bad news: when matching TIFFEntryValueTagSmall, we can still access entry.block — the same holds for TIFFEntryValueTagBlock. Even more, writing value constructors is tedious, and it is not surprising that someone will skip them and later construct something like { .tag = TIFFEntryValueTagBlock, .small_value = 42 }.

Hopefully, our problem has been solved a long time ago; the solution is called sum types. Put it simple, a sum type encodes alternative data representations and allows to work with them in a safe, convenient manner. For example, our TIFFEntryValue would look like this:

(TIFFEntryValueSmall, uint32_t),
(TIFFEntryValueBlock, size_t, const void *)

To construct a TIFF value, write TIFFEntryValueSmall(42) or TIFFEntryValueBlock(3 + 1, "abc"). To match a TIFF value, write

match(entry) {
of(TIFFEntryValueSmall, x) {
// Work with uint32_t x...
of(TIFFEntryValueBlock, size, data) {
// Work with size_t size and const void *data...

There are two differences between the first and the last versions:

1. Significantly less boilerplate with sum types.

2. Significantly safer manipulation with sum types: normally we cannot access a block data when we match a small value and vice versa.

Believe or not, exactly the same syntax sugar is possible in pure, standard-confirming C99! The key is my new library, datatype99. I have released it after approximately a year of experimentation with different approaches and underlying metaprogramming libraries. It features a clean, type-safe interface, as well as formally defined code generation semantics, thus allowing to write an FFI for libraries exposing sum types in their interface.

The installation instructions are laid out in README.md. Now, I am going to present a few more examples of sum types, first of which is a traversal of a binary tree:

[ examples/binary_tree.c ]

#include <datatype99.h>

Here, BinaryTree is a sum type with two alternatives: Leaf and Node. It is recursively traversed inside sum.

Sum types are also highly applicable in compiler/interpreter development. Consider this encoding of an abstract syntax tree:

[ examples/ast.c ]

#include <datatype99.h>

Our little language consists of constants of double and compound expressions derived from either +, -, *, or /. The interpreter, also called an evaluator, evaluates the language and returns double.

Yet another example is a representation of tokens:

[ examples/token.c ]

#include <datatype99.h>

I hope that now, the usage and syntax of sum types is perfectly clear, as well as the rationale behind them. The next article will be dedicated to zero-cost, convenient error handling using sum types.

Learn more about datatype99: https://github.com/Hirrolot/datatype99.