Church Numerals

As it is Sunday morning and I am avoiding the children’s musical service, let’s talk about Church Numerals in Perl 6. The basic notion is to represent the natural numbers in the lambda calculus in a simple, straightforward manner. Actually, the Wikipedia definition is probably better than any explaining I could do — if you don’t know them already, go and check that out.

So anyway, I decided to try to port the Haskell implementation to Perl 6. Here’s my first stab at the basic functions:

This is a pretty straightforward translation of the Haskell version, and seems to work okay. Obviously the Perl 6 version is a tad more verbose — Haskell is optimized for this sort of thing, whereas the Perl 6 syntax falls out of the new for loop syntax. (Maybe not quite the best way to say that, but if you did Perl 6 for loops with the Haskell lambda syntax, for loops would be nearly unreadable, IMO.)

Maybe I’m misunderstanding something, but this function, at least as it is in Perl 6, seems like something of a cheat. That is, when you say church(4), you don’t get back the Perl 6 equivalent of f(f(f(f(x)))). Rather, you get f(church(3)(f)(x)).

At least, that’s what I think happens. I have to admit I still find reasoning about lambda functions like this very confusing. And this equally straightforward conversion doesn’t work: