esolang
Learn · Ideas28 languages show it

Combinators

Functions with no variables, glued together by application. Two of them, S and K, can compute anything.

●●● hard · where to see it

The idea

In most languages a function names its argument, as in f(x) = x + 1. Combinatory logic gets rid of the names. There are a few fixed functions, the combinators, and the only thing you can do is apply one thing to another.

Read the story

Moses Schönfinkel introduced the idea in 1924, and Haskell Curry developed it independently soon after. Two combinators, S and K, are universal; every computable function can be written as a tree of S's and K's.

No variables means no scope, no environment, no bookkeeping. That makes combinators a favourite Turing tarpit; the language is tiny, and the programs are unreadable.

How it works

Application is written by putting things side by side, and it groups to the left, so S K K x means ((S K)…

Application is written by putting things side by side, and it groups to the left, so S K K x means ((S K) K) x. The rules.

I x      →  x                 identity
K x y    →  x                 keep the first, throw the second away
S x y z  →  x z (y z)         give z to both x and y, then apply one result to the other

Tracing I = S K K

I is not even needed. S K K behaves exactly like it.

S K K x
→ K x (K x)      S rule, with x:=K, y:=K, z:=x
→ x              K rule: keep x, drop (K x)

Composition

S (K f) g is "f after g".

S (K f) g x
→ K f x (g x)    S rule
→ f (g x)        K rule

Running forever

S I I applies its argument to itself. Apply it to itself.

S I I (S I I)
→ I (S I I) (I (S I I))
→ S I I (I (S I I))          and I (S I I) is just S I I again ...

It never reaches a form where no rule applies. That is the combinator version of an infinite loop, and the seed of the Y combinator (see fixed points).

Getting rid of variables

Any function with variables can be turned into S, K and I by bracket abstraction. To remove x from a body.

  • x itself becomes I;
  • anything that does not mention x becomes K of it;
  • an application M N becomes S (M without x) (N without x).

The output grows fast, but it always works.

One combinator

Chris Barker's Iota uses a single combinator, ι, defined so that ι x = x S K. Then ι ι acts as I.

ι ι a  →  ι S K a  →  S S K K a  →  S K (K K) a  →  K a (K K a)  →  a

Seen in

  • Unlambda. S, K and I with a backquote for application, plus I/O and continuations.
  • Iota. One combinator; a program is a tree of ι's.
  • Jot. Barker's follow-up, where every string of 0s and 1s is a valid program.
  • Lazy K. Pure SKI with lazy evaluation; a program is a function from input to output.
  • Underload. Its stack operations mirror combinators, but in concatenative form.

Try this

  1. Reduce K I x y. What does K I do?
  2. Reduce S K S x. Then S K anything x. What do you notice?
  3. Hard: use bracket abstraction to write λx. f x x (apply f to x twice) with S, K and I.

Further

Where to see it