Extreme STATIC late-binding

Before addressing some of the questions on building C libraries using ATS,
I would like to give some explanation on the kind of template-based library
that
I think ATS is good at building.

Let me first quote Alan Kay:

OOP to me means only messaging, local retention and protection and hiding
of state-process, and extreme LateBinding
http://c2.com/cgi/wiki?LateBinding of all things.

Of course, he was talking about some form of DYNAMIC late-binding. What I
want to talk about here is STATIC late-binding.

Say we want to quick-sort (qsort) an integer array. We can clearly use the
qsort function declared in stdlib.h. Here is the interface
for qsort:

void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void
*, const void *));

It is a higher-order function involving unknown function calls. If we just
want to sort an integer array, then it is pretty easy to implement
a specific version of qsort that is more efficient as we can simply inline
the calls to integer comparison. If you think that implementing qsort
as a C-macro is the solution, then I have doubts that you value your time
spent on debugging as much as I do.

In ATS, we can use the existing template for qsort to implement a specific
version for sorting integer arrays:

fun qsort_int (…) = let
//
implement qsort$cmp (x, y) = x <= y
//
in
qsort (…)
end // end of [qsort_int]

This is a form of extreme static late-binding: qsort$cmp used in qsort is
only bound when qsort_int is being compiled.

I only showed the simplest form of static late-binding in the above
example. The gist is that a template-based library
in ATS enables the programmer to do this kind of static late-binding so as
to deliver maximal performance.