Combinators?

Status
Not open for further replies.

petrov

Ars Tribunus Militum
2,714
From this thread:
http://episteme.arstechnica.com/eve/forums?a=tpc&s=50009562&f=6330927813&m=8720974763&r=5740956173#5740956173
I went and looked up combinators, since most of my PLT class is now a blur. I've been reading this page:
http://www.cs.oberlin.edu/classes/dragn/labs/combinators/combinators.html
which is a pretty good introduction to combinators. I'm about halfway through and I have to wonder "why?" What's the use of this excessively reductionist method of defining all numbers and functions?

What are the practical applications of combinator theory?

--sam
 

newton_meter

Ars Scholae Palatinae
1,424
Cool question, petrov. I don't know your background, so feel free to skim (or say "blah blah blah") over the parts that seem obvious. If you want to know the answer now, it's "none".

In one sense, combinatory logic is important because it is foundational. It's analogous to set theory in mathematics: it turns out that most of math can be encoded in (Zermelo-Fraenkel) set theory. That is, we can represent numbers as sets, and define arithmetic operations on them as set operations; from there we can represent ordered pairs of numbers as sets; from there we can represent functions (sets of ordered pairs) as sets; etc.

A set theoretician would say that sets are more powerful (or maybe, more basic) than these other things. An overzealous set theoretician would say that these things are sets. Everything is a set! (he would say). A number theoretician or algebraist would say "bullshit". Certainly, it would be foolish to want to implement the numbers and algebra as sets (that my opinion, some would disagree). If we were doing some complex analysis (I literally mean, "complex analysis" as in calculus with complex numbers), we wouldn't want to reduce everthing to sets and set operations. If we were proving a theorem in differential geometry, we wouldn't want to reduce everything to sets.

On the other hand, there's something satisfying about sets. We might do some really complicated higher-level stuff, but underneath, we can think of it as "just sets".

So, sets are not everything (in math), but they're a useful model to describe everything. In particular, when we start to question why stuff works (in math), we only have to go back to the sets, and see if they make sense. In the 1930s, that's what Goedel, Church, Turing, Curry, Post, Kleene, and crew were trying to do. Figure out if math made sense, by figuring out if its foundations made sense.

A Turing Machine is a foundational model of computation. That's the "Church-Turing Thesis". Every "reasonable" computation can be performed by a Turing Machine. That doesn't mean that we want to implement programs on TMs, but it is a useful arena for thinking about the kinds of problems that it is theoretically possible (or impossible) to solve. There's something satisfying here, too. No matter how complicated or hard to think about our computers get, we can always think of them as performing the fundamental operations of reading symbolic input, making decisions based on internal state and input data, changing internal state, and producing symbolic output.

There are other systems equivalent in power to the Turing Machine (Turing Complete, if you will). Examples are Post Production Systems (or context sensitive grammars) and Church's lambda-calculus. For instance, a Turing Machine can simulate the lambda-calculus, and the lambda-calculus can simulate a Turing Machine. Thus, they compute exactly the same things and are equivalent in power.

The lambda-calculus is a syntactic theory of functions. It contains essentially two operations: abstracting over a value (via lambda) and naming a value (via application). This is satisfying, too. The essense of computation is not I/O and decision making, but naming and abstraction (the lambda-calculatist would say). Everything can be understood in terms of naming (in fact, one of my graduate advisors claims that all of computation is merely different kinds of naming) and reference. That's not to say that everything should be implemented that way.

Finally, combinatory logic. CL is equivalent to the lambda calculus. Any (closed) lambda term can be translated to an equivalent CL term, and any CL term can be translated to an equivalent (closed) lambda term. Since the translation of lambda-calculus to TM only requires closed lambda terms, we also have that CL is equivalent to Turing's machine. CL is a theory with two basic operations: K and S. K chooses between two "things" (always taking the first, but we can arrange for things to go in in the right order) and S duplicates a "thing" and distributes it to two other "things". There are no variables at all in CL (very much unlike the lambda-calculus).

So, wait a minute! Computation isn't all about I/O and decisions, and it isn't all about naming and references. It's all about choosing and duplicating. It would be silly to implement things that way, but it's not such a bad way to think about it.

Church, Curry, Turing, et. al. were trying to figure out what were the foundations of "computation", not as the activity of a digital computer, but in a more optimistic sense as the cognitive activity of computing an answer to a problem. It is striking that there are several foundational models that all seem equally "good".

The practical effects in CS of CL are very few, as far as I know: it serves as a compilation technique for some functional programming languages. It serves as a sometimes useful model of what compuation "is". It could potentially be used as a model of computation for programming (much like TMs underly modern imperative languages and lambda-calculus underlies functional programming languages), though there's probably not a huge market for that. It could be used in language semantics and proving things about programs, but the PL community seems satisfied with using lambda-calculus for that. It makes a neat parlor trick.

(kg*m*m)/(s*s)
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by Thanatos:
I need a smiley showing the previous post whooshing over my head...

Oops. Sorry about that. Here's a paraphrase: expressing complicated things in terms of simpler things is sometimes a useful way to describe and understand the complicated things. It's almost always a bad way to implement the complicated things.

Combinatory logic is a little formal system that has two operations: taking two things and giving one back (I won't try ASCII art--imagine a black box with two inputs and one output; one of the inputs is hard wired to the output) and making a copy of a thing (imagine a black box with one input and two outputs, with the same thing as the input appearing on both outputs). Surprisingly, we can put these things together to compute every computable function.

But it's actually not surprising to anyone who's seen NAND gates.

(kg*m*m)/(s*s)
 

petrov

Ars Tribunus Militum
2,714
your post provides a lot of historical context that answers the question of "why are combinators around?" but not very much of actually "what are combinators useful for?" which was more what I'm looking for. But it sounds like they aren't used for much of anything. For the things that I thought that they might be useful (theorem proving), it sounds like most profs just use lambda calculus for that.

My background is an undergraduate CS degree from Rice (I took intro to programming from Felleisen, the author of the Little Schemer, w00t!), so I understood your explanation (and the theory of combinators), but just not the applicability, which sounds like it's minimal.

--sa
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by petrov:
so I understood your explanation (and the theory of combinators), but just not the applicability, which sounds like it's minimal.

Yeah, that's the message. I can think of only a few practical uses of combinators:

1. Flemming Nielson and Hanne-Riis Nielson developed two-level semantics to model code generation and other explicitly staged computation (such as macro expansion and partial evaluation). They use two different levels of meta-languages. In the original paper Two-level Semantics and Code Generation (I couldn't find it online), they used a lambda-calculus for one level, and a combinator language for the other, to avoid confusing them. This is sometimes still done, but sometimes both levels are lambda-calculi (one level is boldfaced or underlined).

2. Haskell and Miranda (lazy functional languages) are often compiled by representing the program as combinators (actually "supercombinators", which treat each function definition as a combinator defining a reduction on programs) and combinator graph reduction. Simon Peyton-Jones has a book The Implementation of Functional Programming Languages, and several papers, notably The Spineless Tagless G-Machine and others about it.

3. There is a style of programming in functional languages known as "points-free", which defines functions without references to their formal parameters (think: no lambdas). This is programming with combinators. You can see a points-free version of factorial in Haskell (as well as an almost total CL version) here. Notice that in Haskell, the explicit CL version is fast, because Haskell is compiled by combinator graph reduction. So I suppose you could add "performance tuning Haskell" as a practical use of CL.

kg m²/s²
 
Oops. Sorry about that. Here's a paraphrase: expressing complicated things in terms of simpler things is sometimes a useful way to describe and understand the complicated things. It's almost always a bad way to implement the complicated things.

Combinatory logic is a little formal system that has two operations: taking two things and giving one back (I won't try ASCII art--imagine a black box with two inputs and one output; one of the inputs is hard wired to the output) and making a copy of a thing (imagine a black box with one input and two outputs, with the same thing as the input appearing on both outputs). Surprisingly, we can put these things together to compute every computable function.

But it's actually not surprising to anyone who's seen NAND gates.
I was actually with you for most of the discussion but damn, that smiley would be useful once in a while.

(Thanatos gets out his copy of photoshop and starts drawing little dots)
 
Status
Not open for further replies.