CS 312 Recitation 8
Beyond signatures: Functors

Recall that the implementation of queue given in class made use
of two lists. More generally, a functional queue can be implemented using
two stacks, s1
and s2. Stack s1 is used for enqueuing, s2 for dequeuing. When dequeuing, if
stack s2 was empty,
reverse s1 and consider it the new s2. A linked list is a possible (but not the only)
implementation for a stack. Here is an implementation using stacks:

Suppose now that you had another implementation of stacks, say Stack2. Suppose you wanted to have
at your disposal two implementations of Queue, one using Stack and the other using Stack2. The only way to
achieve this with the above code is to duplicate it, creating two
different versions of Queue.
This is clearly not ideal - for one, you now have to maintain twice the
code. Furthermore, it is not clear exactly what Queue assumes about the
functionality of Stack. How
do we know that substituting Stack2 for
Stack will not break any of
the Queue functionality?
After all, it's possible that Stack and
Stack2 don't even have the
same signature.

Functors

To solve this problem, ML provides a mechanism called functors.
The intuition behind functors is quite simple. In the
above example, we know that our implementation of Queue will use a stack
implementation. Creating a specific Queue
structure could involve the following steps:

1. Create a stack structure S in
the desired implementation (Stack
or Stack2),
2. Create a Queue structure
making use of S, by passing S in as a parameter to some sort
of "function".

For step 2, we need a "function" that takes in a structure and
returns another structure - in this case, takes in a stack and returns
a queue. Such "functions" are precisely the ML functors.

Our Queue functor will also need to specify (and check that its
argument satisfies) all the stack
functionality expected by Queue.
Fortunately, we already know how to do this; after all, specifying
functionality is exactly what signatures do. The Queue functor
will specify a signature for the stack structure it expects, and will
only accept structures that instantiate that signature.

As you see, functors provide a very powerful abstraction mechanism.
Another example of their use is when we need to construct a structure
that will have an ordering, such as a Binary Search Tree or a
Dictionary. You can, of course, fix the ordering by explicitly passing
in the ordering function to the ADT each time the ADT is constructed.
However, we could also use functors to do this. Given the following
signature:

More fun with functors

Suppose you have a structure that makes use of not one, but two or more
other structures. This seems to call for a functor that can take in
more than one structure as an argument. However, as you have seen with
ordinary functions, we don't need to worry about multiple arguments, as
long as we can "package" them into a single value somehow. In the case
of functions, we could pass in a tuple containing all the arguments. In
the case of functors, we can "wrap" our argument structures into
another structures, and pass only this in.

As an example, suppose we want to pass in two independent structures A
and B to a functor FooFn. We proceed as follows:

Since this looks a bit clunky, ML provides a special abbreviation:
we can forget about the surrounding structure and pass in the
specification of the elements of the structure directly. Thus, the
above example can be rewritten:

functor FooFn (structure A : A_SIGstructure B : B_SIG) = ...

structure Foo = FooFn (structure A = Astructure B = B)

You may be slightly worried that this abbreviation loses the name for
the wrapping structure (Arg).
To reassure you, have a look at how the above abbreviation is actually
implemented.