Enumerating Trees (2013)
There&#x27;s a simple recipe for arithmetically encoding recursive algebraic data types (in the functional programming sense) which is related to this.<p>What you might have seen is Goedel numbering where a finite sequence of natural numbers a_0, a_1, ..., a_n (where n isn&#x27;t fixed but can vary per sequence) is mapped  bijectively onto p_0^a_0 a_1^p_1 ... a_n^p_n where p_0, p_1, ... is an enumeration of the primes.<p>However, if you want to represent trees instead of sequences, you have a better, simpler option. The key is the existence of a bijective pairing function between N^2 and N, which you can write as &lt;m, n&gt; for m, n in N.<p>You have a lot of choices for how to construct the pairing function. But a curious fact is that there is essentially one polynomial pairing function and it&#x27;s the one you saw in class when you learned that the rationals are countable: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fueter–Pólya_theorem" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fueter–Pólya_theo...</a>.<p>From this perspective it should be clear that bit interleaving provides one such pairing function.<p>Here&#x27;s a larger example of how you can use this for encoding algebraic data types. The data type in this article is an unlabeled binary tree:<p><pre><code>    data Tree = Leaf | Node Tree Tree
</code></pre>
I will use square brackets to denote the encoding. Then<p><pre><code>    [Leaf]     = 0
    [Node a b] = 1 + 2*&lt;[a], [b]&gt;
</code></pre>
So the tag for the sum type is the residue modulo 2, and if the tag is 1 you can divide by 2 (shift right by one bit) to extract the encoded pair of subtrees. If the sum type had three terms you could use 3 instead of 2:<p><pre><code>    data Tree = WhiteLeaf | BlackLeaf | Node Tree Tree

    [WhiteLeaf] = 0
    [BlackLeaf] = 1
    [Node a b]  = 2 + 3*&lt;[a], [b]&gt;
</code></pre>
This also works with labeled trees:<p><pre><code>    data Tree = Leaf N | Node Tree Tree

    [Leaf n]   = 0 + 2*n
    [Node a b] = 1 + 2*&lt;[a], [b]&gt;
</code></pre>
If the label type wasn&#x27;t a natural number, you&#x27;d just recursively encode it.<p>In the above I treated the &lt;m, n&gt; pairing as a black box that can be implemented in different ways (e.g. Cantor pairing, bit interleaving). You can abstract out the encoding of sums as well. You&#x27;re looking for a bijection between N and Inl N | Inr N and I made the particular choice<p><pre><code>    [Inl n] = 0 + 2*n
    [Inr n] = 1 + 2*n
</code></pre>
But you have other options as well, though this is probably the simplest. And once you have an encoding for sums and products you can apply them recursively to encode any recursive algebraic data type.<p>Note that the sum and pair encoding functions are bijective on the natural numbers, but the corresponding encodings for algebraic data types aren&#x27;t necessarily bijective, only injective. For the unlabeled binary tree example, the number 2 has a tag of 0 (leaf) but isn&#x27;t the encoding of any tree. However, the encoding for labeled binary trees is bijective if the label type is bijectively encoded. The problem with this kind of recursively constructed encoding that terminates in finite types is that you obviously cannot have a bijection between a finite set and an infinite set like N.<p>To tie it back into the article, he uses this encoding:<p><pre><code>    data Tree = Leaf | Node Tree Tree

    [Leaf]     = 0
    [Node a b] = 1 + &lt;[a], [b]&gt;
</code></pre>
You can extend this to my example with two kinds of leaves:<p><pre><code>    data Tree = WhiteLeaf | BlackLeaf | Node Tree Tree

    [WhiteLeaf] = 0
    [BlackLeaf] = 1
    [Node a b]  = 2 + &lt;[a], [b]&gt;
</code></pre>
Note that the tag ordering is all-important here. This only works when the tag ordering is &quot;tail recursive&quot;. If you used 0 as the tag for Node, then there&#x27;d be an ambiguity between leaves and proper subtrees; you wouldn&#x27;t know if 1 corresponded to a leaf or 0 + encoded_subtree_pair.<p>So, this kind of prefix-sum encoding is bijective but can only accommodate one unbounded term, which must be assigned the final tag. No tag ordering can work for this:<p><pre><code>    data Tree = Leaf | WhiteNode Tree Tree | BlackNode Tree Tree

    [Leaf] = 0
    [WhiteNode a b] = 1 + &lt;[a], [b]&gt;
    [BlackNode a b] = 2 + &lt;[a], [b]&gt; &#x2F;&#x2F; Wrong!
</code></pre>
I haven&#x27;t thought about ordinals for a long time, but this feels related to the fact that 1 + ω = ω is not equal to ω + 1.