简体   繁体   English

可以使用SKI组合器表示XOR吗?

[英]Can XOR be expressed using SKI combinators?

I have question about SKI-Combinators. 我对SKI组合器有疑问。

Can XOR (exclusive or) be expressed using S and K combinators only? 只能使用SK组合器来表示XOR(异或)吗?

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

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: 您的问题在细节上还不清楚,但是您的意思似乎是您具有以下布尔值表示形式:

T := K
F := S K

This works because it means the following reductions hold: 之所以有效,是因为它意味着以下减少适用:

T t e => t
F t e => e

in other words, bte can be interpreted as IF b THEN t ELSE e . 换句话说, bte可以解释为IF b THEN t ELSE e

XOR in terms of IF _ THEN _ ELSE _ 根据IF _ THEN _ ELSE _ XOR

So given this framework, how do we implement XOR? 因此,有了这个框架,我们如何实现XOR? We can formulate XOR as an IF expression: 我们可以将XOR表示为IF表达式:

xor x y := IF x THEN (not y) ELSE y = (IF x THEN not ELSE id) y

which can be eta-reduced to 可以减少到

XOR x := IF x THEN not ELSE id = x not id

Some function combinators 一些功能组合器

We have id = SKK as standard, and not can be expressed as flip , since flip bte = bet = IF b THEN e ELSE t = IF (not b) THEN t ELSE e . 我们将id = SKK作为标准,而not表示为flip ,因为flip bte = bet = IF b THEN e ELSE t = IF (not b) THEN t ELSE e flip it self is quite involved but doable as flip它本身是相当参与但可行的

flip := S (S (K (S (KS) K)) S) (KK)

Now we just need to figure out a way to write a function that takes x and applies it on the two terms NOT and ID . 现在,我们只需要找出一种编写函数的方法,该函数接受x并将其应用于NOTID这两个术语。 To get there, we first note that if we set 要到达那里,首先要注意的是

app := id

then 然后

app f x = (id f) x = f x

and so, 所以,

(flip app) x f = f x

We are almost there, since everything so far shows that 我们快到了,因为到目前为止的所有事情都表明

((flip app) id) ((flip app) not x) = ((flip app) not x) id = (x not) id = x not id

The last step is to make that last line point-free on x . 最后一步是使最后一行在x无点。 We can do that with a function composition operator: 我们可以使用函数组合运算符来做到这一点:

((flip app) id) ((flip app) not x) = compose ((flip app) id) ((flip app) not) x

where the requirement on compose is that compose的要求是

compose f g x = f (g x)

which we can get by setting 我们可以通过设置来获得

compose f g := S (K f) g

Putting it all together 放在一起

To summarize, we got 总而言之,我们得到了

xor := compose ((flip app) id) ((flip app) not)

or, fully expanded: 或完全扩展:

xor = S (K ((flip app) id)) ((flip app) not)
    = S (K ((flip app) (SKK))) ((flip app) flip)
    = S (K ((flip SKK) (SKK))) ((flip SKK) flip)
    = S (K (((S (S (K (S (KS) K)) S) (KK)) SKK) (SKK))) (((S (S (K (S (KS) K)) S) (KK)) SKK) (S (S (K (S (KS) K)) S) (KK)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM