73 points · by headalgorithm · 6y ago · 16 comments
Lennart Augustsson won the IOCCC a couple of times by basically writing a program in Lazy ML and hand-compiling it to C using the usual graph reduction techniques http://ioccc.org/winners.html#A I think using supercombinators rather than SK style.
This book is where I learned this stuff: https://www.microsoft.com/en-us/research/publication/the-imp...
Lambda calculus allows for a surprisingly simple self-interpreter [1]. Here is one, written with De Bruijn indices:
(λ 1 1)(λ λ λ 1(λ λ λ λ 3(λ 5(3(λ 2(3(λ λ 3(λ 1 2 3)))(4(λ 4(λ 3 1(2 1)))))) (1(2(λ 1 2))(λ 4(λ 4(λ 2(1 4)))5))))(3 3)2)(λ 1((λ 1 1)(λ 1 1)))
This includes a tokenizer/parser of (binary encoded) lambda calculus terms.
Of course, de Bruijn indices can help with this, and I'm a big fan. They get a lot of hate though, e.g. there seems to be an entire CS sub-discipline dedicated to ever-more-elaborate ways to avoid/hide de Bruijn indices ;) (e.g. https://en.wikipedia.org/wiki/Higher-order_abstract_syntax https://arxiv.org/pdf/1807.04085.pdf https://arxiv.org/pdf/1111.0085.pdf http://lambda-the-ultimate.org/node/544 etc.)
Fresh out of college I was really into lambda calculus and combinators, and wrote a pretty similar blog post to this article, maybe just a bit simpler: https://log.kv.io/post/2008/03/02/ski-calculus-in-haskell-sh...
[1] https://www.amazon.com/dp/0954300653
[2] "The Lambda Calculus — Its Syntax and Semantics" and "Lambda Calculus with Types"
I go back and forth on how useful these sorts of combinators are. I do appreciate how they at least have to be explicitly declared in Haskell, as opposed to verb trains in J. But I've still gotten complaints from teammates about what the fuck `liftM2 . liftM2` is supposed to mean.
Of course they're not really useful for programming; they're more interesting as a compilation target, formal reasoning, and some fun puzzles.
(although in this case I just didn't find I = WK)
I should actually add a note about that, since a few people have pointed it out.
Ah, that makes a lot of sense. I mostly just thought my version was the obvious one - my first thought of how to do nothing was to duplicate something and then drop one of the dupes. I have very little grounding in lambda calculus, though, and generally find the other combinators harder to reason about.
WKx
=> Kxx
=> x
CKCx
=> KxC
=> x
After the first application they both end up in essentially the same state (the `C` in the second case is going to be discarded so it really doesn't matter what's there).