TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A λ-calculus interpreter written in C preprocessor macros

85 pointsby Hirrolotover 3 years ago

4 comments

trompover 3 years ago
Nice demonstration of preprocessor powers.<p>Compiling this file (cd examples; cmake .; make) took just under half a minute. Adding an exponentiation test<p><pre><code> #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); </code></pre> nearly doubles the time.<p>Adding some Pythagoras<p><pre><code> #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)); </code></pre> bumps the time to 3 minutes.<p>Currying works fine too; one can simplify the definitions<p><pre><code> #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)))))) </code></pre> to<p><pre><code> #define I LAM(VAR(1)) #define IF I #define MUL LAM(LAM(LAM(APPL(VAR(3), APPL(VAR(2), VAR(1)))))) </code></pre> I tried to see at what point things break; perhaps at this point<p><pre><code> #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); </code></pre> that yields an error: pasting formed &#x27;;_IMPL&#x27;, an invalid preprocessing token<p>I like the choice of False for Nil, which I also made in my Binary Lambda Calculus [1]<p>[1] <a href="https:&#x2F;&#x2F;tromp.github.io&#x2F;cl&#x2F;Binary_lambda_calculus.html#Delimited_versus_undelimited" rel="nofollow">https:&#x2F;&#x2F;tromp.github.io&#x2F;cl&#x2F;Binary_lambda_calculus.html#Delim...</a>
评论 #28897374 未加载
评论 #28901001 未加载
etaioinshrdluover 3 years ago
This is truly quite insane. I&#x27;d like to note that there seems to be much more to it than the one file linked here, it references other macros defined in other files.<p>Also, the author says that you can use this &quot;to drastically improve quality of your code -- make it safer, cleaner, and more maintainable.&quot;<p>I hope this is somewhat sarcastic, because I doubt just about anyone else would like to find your code using this.
评论 #28899617 未加载
yodsanklaiover 3 years ago
&gt; I’m a 17 y&#x2F;o software engineer<p>Pretty cool! wonder what brought him to lambda calculus.
评论 #28901708 未加载
dleslieover 3 years ago
Ha, I suspected this would be Hirrolot.<p>Take a look at their GH; it&#x27;s fully of handy, and not-so-handy, C preprocessor hacks.