I have question about SKI-Combinators.
Can XOR (exclusive or) be expressed using S
and K
combinators only?
I have
True = Cancel
False = (Swap Cancel)
where
Cancel x y = K x y = x
Swap: ff x y = S ff x y = ff y x
I have question about SKI-Combinators.
Can XOR (exclusive or) be expressed using S
and K
combinators only?
I have
True = Cancel
False = (Swap Cancel)
where
Cancel x y = K x y = x
Swap: ff x y = S ff x y = ff y x
Yes! In 2020, Stephen Wolfram constructed combinators for all 16 two-input Boolean functions, including XOR. It turns out XOR can be expressed as s[s[s[s[s]][s[s[s[k]]]][s]]][k] where k represents the value True and s[k] represents the value False.
For example, here is the combinator form of XOR operating on true and false the Wolfram Language.
true = k;
false = s[k];
ResourceFunction["CombinatorFixedPoint"][s[s[s[s[s]][s[s[s[k]]]][s]]][k][true][false], SKGlyphs -> {s, k}]
It returns the expected output – True:
k
Booleans
Your question is a bit unclear on the details, but it seems that what you mean is that you have the following representation of booleans:
This works because it means the following reductions hold:
in other words,
b t e
can be interpreted asIF b THEN t ELSE e
.XOR in terms of
IF _ THEN _ ELSE _
So given this framework, how do we implement XOR? We can formulate XOR as an
IF
expression:which can be eta-reduced to
Some function combinators
We have
id = SKK
as standard, andnot
can be expressed asflip
, sinceflip b t e = b e t = IF b THEN e ELSE t = IF (not b) THEN t ELSE e
.flip
it self is quite involved but doable asNow we just need to figure out a way to write a function that takes
x
and applies it on the two termsNOT
andID
. To get there, we first note that if we setthen
and so,
We are almost there, since everything so far shows that
The last step is to make that last line point-free on
x
. We can do that with a function composition operator:where the requirement on
compose
is thatwhich we can get by setting
Putting it all together
To summarize, we got
or, fully expanded: