[HN Gopher] A l-calculus interpreter written in C preprocessor m...
       ___________________________________________________________________
        
       A l-calculus interpreter written in C preprocessor macros
        
       Author : Hirrolot
       Score  : 77 points
       Date   : 2021-10-17 11:16 UTC (11 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | tromp wrote:
       | Nice demonstration of preprocessor powers.
       | 
       | 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 token
       | 
       | I 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...
        
         | Hirrolot wrote:
         | Yeah, the macro-based lambda calculus is pretty slow, since
         | it's essentially an interpreter upon interpreter upon
         | interpreter! The first one is preprocessor, which executes
         | Metalang99, which executes the final calculus.
        
           | lisper wrote:
           | For the benefit of those like me who hadn't heard of
           | Metalang99:
           | 
           | https://news.ycombinator.com/item?id=27919448
        
       | etaioinshrdlu wrote:
       | This is truly quite insane. I'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.
       | 
       | 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.
        
         | Hirrolot wrote:
         | This was not sarcastic; I've explained in more detail in my
         | blog post: https://hirrolot.github.io/posts/macros-on-steroids-
         | or-how-c....
         | 
         | The linked code uses other macros too, that's right, since it's
         | written in a metalanguage atop of the standard preprocessor.
        
       | dleslie wrote:
       | Ha, I suspected this would be Hirrolot.
       | 
       | Take a look at their GH; it's fully of handy, and not-so-handy, C
       | preprocessor hacks.
        
       | yodsanklai wrote:
       | > I'm a 17 y/o software engineer
       | 
       | Pretty cool! wonder what brought him to lambda calculus.
        
       ___________________________________________________________________
       (page generated 2021-10-17 23:01 UTC)