Home > Functional Meta-Language
Around 2004 as a physics student at The Australian National University I was getting into C++ and I learned from my then-advisor Antony Searle the curious fact that partial template specialization in C++ was sufficiently powerful that you could make a whole language out of it.
"Programs" in such a language would be C++ class specifications like f<g<h<x> > >, where the template classes f, g, and h are the "functions" of the language. The programs would "execute" completely at a compile time. The "virtual machine" was the C++ compiler's process of resolving template class definitions.
I decided to make such a language for a bit of fun, and to see if it would really work.
Here is a test program written in this language:
This program computes all prime numbers less than or equal to 50 at compile time. The resulting compiled program does nothing except print the following string:
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
The "language" is composed of various class templates that map to traditional functional operations, like constructing lists, mapping operations over lists, filtering lists according to a predicate, etc.
For example, here is the definition of the if_then_else "function":
And here is the definition of the prime "function" which uses the most trivial algorithm you can imagine to test primality:
All the various functions used here, such as fold and map, are likewise defined as template classes.
You can download the source code for this toy language. Just compile main.cpp.
By Andrew Moylan (email).