As I have great difficulty in naming the style of programming I am
about to present in this section, I coin a word
temptorial (as a shorthand for template-functorial) to
refer to this style, which makes essential use of function templates that
are implemented generically as well as specifically.
Suppose that we have interfaces for two function templates foo and
bar. We give a generic template implementation for bar that makes use of
foo but we cannot or do not want to implement foo generically. When an
instance of bar is called, certain instances of foo may need to be
implemented specifically so as to support the call. Let us now imagine a
design where foo and bar are replaced with collections of function
templates corresponding to various operations on integers and rationals.
This is precisely the idea employed in the design of a temptorial package
for rationals.
The following interfaces for function templates are declared in a
file named ratfun_tmp.sats, which is available
on-line:
In another file named
ratfun_tmp.dats, which is also
available
on-line, the
second half of the above interfaces for function templates are implemented
based on the function templates for which the interfaces are declared in
the first half. As an example, the function template
rat_make_numer_denom is implemented as follows:
Note that the backslash symbol (
\) temporarily assigns the
infix status to the identifier immediately following it. For instance, the
syntax
p \intdiv r simply stands for the function application
intdiv(p, r).
There is some testing code available
on-line that
demonstrates a typical way to use a temptorial package. For instance, if we
want to use a package for rationals where integers are represented as
values of the type double, then we can first give the following
specific implementations for instances of function templates corresponding
to certain integer operations:
With these implementations, we can call the corresponding instances of
those function templates in
ratfun_tmp.sats (e.g.,
ratadd<double> and
ratsub<double>)
that have already been implemented generically.