Hacker News new | ask | show | jobs
by mikewarot 2021 days ago
Elsewhere in the thread, was a YouTube link showing demos of church numbers, all in javascript. It was interesting, and there was a function to take church numbers and convert them back to normal whole numbers. I get the big picture, but am hung up on a detail or two.

>S K t e = K e (t e) = e

S K t e = K e (t e) // that makes sense

What is (t e)? I got to the same point in the python example down thread.

1 comments

Yeah, I tried to mention that near the end, but it can be hard to explain.

It is "t" applied to "e" - so it would be t(e) in javascript notation. This could be nonsensical, but because of lazy evaluation it never gets expanded/evaluated. (K doesn't use its second argument.)

The SKI stuff is defined as applying those replacement rules from the outside in, so:

    K e (t e) ... ->  e ...
you take that whole expression from the front (K and the next two expressions) and replace it with the expression that was in the first argument's location. But (t e) here is dropped without ever being expanded / evaluated.

Most languages fully evaluate arguments before passing them to functions, but a few rare languages like Haskell don't evaluate the arguments until they're needed. So for

    K e (t e)
it will assign the whole tree (t e) wherever y occurs in the function definition, and just keep passing it along until something needs its value (if you use apply it to something like a function, use it in a case statement, or pass it to a primitive function like plus).

The miranda language (whose source was recently published) is also lazy, and works by compiling your code down to SKI combinators and then evaluating them. So this actually has been used in practice.