A λ-calculus interpreter written in C preprocessor macros
github.com
github.com
Compiling this file (cd examples; cmake .; make) took just under half a minute. Adding an exponentiation test
#define POW LAM(LAM(APPL(VAR(1), VAR(2))))
#define TWO_TO_THE_FOUR APPL(APPL(POW, TWO), FOUR)
#define FOUR_SQUARED APPL(APPL(POW, FOUR), TWO)
ASSERT_REDUCES_TO(TWO_TO_THE_FOUR, FOUR_SQUARED);
nearly doubles the time.Adding some Pythagoras
#define FIVE APPL(SUCC, FOUR)
#define SQUARE LAM(APPL(APPL(MUL, VAR(1)), VAR(1)))
ASSERT_REDUCES_TO(APPL(APPL(ADD, APPL(SQUARE, THREE)), APPL(SQUARE, FOUR)), APPL(SQUARE, FIVE));
bumps the time to 3 minutes.Currying works fine too; one can simplify the definitions
#define IF LAM(LAM(LAM(APPL(APPL(VAR(3), VAR(2)), VAR(1)))))
#define MUL LAM(LAM(LAM(LAM(APPL(APPL(VAR(4), APPL(VAR(3), VAR(2))), VAR(1))))))
to #define I LAM(VAR(1))
#define IF I
#define MUL LAM(LAM(LAM(APPL(VAR(3), APPL(VAR(2), VAR(1))))))
I tried to see at what point things break; perhaps at this point #define SEVENTEEN APPL(SUCC, APPL(SQUARE, FOUR))
#define FOUR_CUBED APPL(APPL(POW, FOUR), THREE)
#define THREE_TO_THE_FOUR APPL(APPL(POW, THREE), FOUR)
ASSERT_REDUCES_TO(APPL(APPL(ADD, FOUR_CUBED), SEVENTEEN), THREE_TO_THE_FOUR);
that yields an error: pasting formed ';_IMPL', an invalid preprocessing tokenI like the choice of False for Nil, which I also made in my Binary Lambda Calculus [1]
[1] https://tromp.github.io/cl/Binary_lambda_calculus.html#Delim...
This is hard to follow.
More examples needed.
Also, the author says that you can use this "to drastically improve quality of your code -- make it safer, cleaner, and more maintainable."
I hope this is somewhat sarcastic, because I doubt just about anyone else would like to find your code using this.
The linked code uses other macros too, that's right, since it's written in a metalanguage atop of the standard preprocessor.
Pretty cool! wonder what brought him to lambda calculus.
Take a look at their GH; it's fully of handy, and not-so-handy, C preprocessor hacks.