kuniga.me > NP-Incompleteness > Mutually Recursive Modules in OCaml

16 Nov 2017

In Chapter 10 of *Purely Functional Data Structures*, Okasaki describes a technique called data structure bootstrapping. It’s a way to reuse existing implementation of data structures to construct (bootstrap) new ones.

In one of the examples he creates a new heap implementation with an efficient merge operation using another heap as basis, but it turns out that to implement this, we need to rely on mutually recursive modules, that is, two modules A and B, where A depends on B, and B depends on A.

In this post we’ll study the bootstrapped heap and learn how to implement mutually recursive modules in OCaml.

Assume we have a heap implementation with `O(1)`

`insert`

, and `O(log n)`

`merge`

, `findMin`

and `deleteMin`

operations. We’ve seen such an implementation with Skewed Binomial Heaps

We’ll see how to construct a new heap implementation which will improve the merge complexity to `O(1)`

.

Let’s call the base heap `PrimaryHeap`

and define our heap type as

this type can be either empty or a node with an element (root) and a primary heap whose element is the bootstrapped heap itself, that is, `heap`

and `PrimaryHeap.heap`

form a mutually recursive types. Note that the above **is not a valid OCaml code**. We’re using it to explain the theoretical concepts.

We can think of this as a k-ary tree where the element is the root and the children of that node are the subtrees, but these subtrees are stored in a heap instead of an array.

The root element at each node is the smallest among all of the subtrees. Hence, to obtain the minimum element for a heap, `findMin`

, is trivial: we can simply return that element:

Merging two bootstrapped heaps is analogous to linking two trees. The only invariant we need to maintain is that the smallest root continues being the root.

Since the primary heap has `O(1)`

`insert`

, the bootstrapped heap has `O(1)`

`merge`

, which was our goal. Note that we can implement `insert`

using `merge`

by creating a *singleton* node and merging it with an existing heap.

We need to handle the deletion of the minimum element, which is the more involved operation. It consists in discarding the root of the present node, and finding a new root from the primary heap.

Since each element in the primary heap is a bootstrapped heap, we first obtain the bootstrapped heap containing the smallest element:

then we remove this node from the `primaryHeap`

, and we merge the `minPrimaryHeap`

back into `primaryHeap`

.

finally we make `newMinElem`

the new root element of our top level bootstrapped heap. The complete code is

The only missing part is defining the correct type of the bootstrapped heap.

Okasaki mentions that recursive structures are not supported in Standard ML (at least at the time my copy of the book was printed), but they are supported in OCaml.

To make modules mutually depend on another, we need to mark it as recursive via the `rec`

keyword, and declaring both modules at the same time by using the `and`

connector. Let’s work with a toy example: two modules `Even`

and `Odd`

, where each depend on the other.

This will lead to a compilation error:

Error: Recursive modules require an explicit module type.

We need to write the signatures explicitly:

This blog post from Jane Street describes a way to define mutually recursive modules by only listing its signatures:

The OCaml compiler can infer the implementation part from the type definitions, but unfortunately this won’t work if the module has function definitions, which is the case for our heap implementation.

Things get more complicated in our case because the primary heap implementation uses a functor to generate a heap taking the element’s module as parameter. In this case the element’s module is our bootstrapped heap. A valid module definition is given below:

Let’s understand what is going on.

`type t = Empty | Heap of Element.t * PrimaryHeap.heap`

is the definition we presented above. We also implement the methods from the `Set.OrderedType`

interface, namely `compare`

, since this is the interface the heap maker expects. The comparison is based solely on the root element.

Then we declare the PrimaryHeap type at the same time, with type `IHeapWithMerge`

, and because `tv`

is unbound in that interface, we need to bind it to `BootstrappedElement.t`

:

`PrimaryHeap: IHeapWithMerge with type tv := BootstrappedElement.t`

Finally we provide the implementation, using the result of the `SkewBinomialHeap()`

functor having the `BootstrappedElement`

module as element type:

`PrimaryHeap (...) = SkewBinomialHeap(BootstrappedElement)`

The syntax is pretty involved, but it accomplishes what we wanted. We can further refine this definition by adding

`include Set.OrderedType with type t := t`

to the `BootstrappedElement`

signature. This includes all the interface of `Set.OrderedType`

.

These newly defined modules are defined within a functor, the `BootstrappedHeap`

, together with the methods we defined above. Like other heap generators, the functor takes a module representing the element type as parameter. In this case we can also allow the primary heap type to be passed as parameter so we don’t have to use `SkewBinomialHeap`

as implementation. Any heap with `merge`

will do.

The constructors define within `BootstrappedElement`

are visible within `BootstrappedHeap`

but they need qualification, such as `BootstrappedElement.Heap`

. To avoid repeating this qualifier, we can use:

`include BootstrappedElement`

The complete implementation for `BootstrappedHeap`

can be found on github.

The idea of using implementations of a given data structure to yield improve implementations is amazing! The mutual recursion nature of the bootstrap heap got me at first, but making analogies with a k-ary tree made it easier to understand.

I was struggling a lot to get the syntax right for the recursive modules required for this implementation until I stumbled upon this github repository, from which I learned many new things about OCaml.