简体   繁体   English

lambda 演算异或表达式由真假

[英]lambda calculus xor expression by true false

I am trying to understand xor in context on lambda calculus.我试图在 lambda 演算的上下文中理解异或。 I understand xor (Exclusive or) as boolean logic operation in https://en.wikipedia.org/wiki/Exclusive_or and the truth table of xor.我将异或(异或)理解为https://en.wikipedia.org/wiki/Exclusive_or 中的布尔逻辑运算和异或的真值表。

But how why is it true as a xor b=(a)((b)(false)(true))(b) from http://safalra.com/lambda-calculus/boolean-logic/ it is indeed what what expect in lambda calculus.但是为什么它是来自http://safalra.com/lambda-calculus/boolean-logic/的 xor b=(a)((b)(false)(true))(b) 真的是什么?期望在 lambda 演算中。 When I saw true=λab.a false=λab.b I kinda have to see the true and false as a lambda calc true and false since it returns the first element in case of true.当我看到 true=λab.a false=λab.b 时,我不得不将 true 和 false 视为 lambda calc true 和 false,因为它在为 true 的情况下返回第一个元素。 But is it correct to understand that the xor here is also a name but not the same as xor in boolean logic?但是,理解这里的 xor 也是一个名称但与布尔逻辑中的 xor 不同是否正确?

Intuitively, we can think of A XOR B as直观地,我们可以将 A XOR B 视为

  1. if A, then not B如果A,那么不是B
  2. otherwise, B否则,B

.... or in some pseudocode: .... 或者在一些伪代码中:

func xor (a,b)
  if a then
    return not b
  else
    return b

Let's get lambda calculusing让我们开始 lambda 演算

true := λa.λb.a
false := λa.λb.b

true a b
// a

false a b
// b

next we'll do not接下来我们not

not := λp.p false true

not true a b
// b

not false a b
// a

we can do if next (note, that this is sort of silly since true and false already behave like if )我们可以做if next (注意,这有点愚蠢,因为truefalse已经表现得像if

if := λp.λa.λb.p a b

if true a b
// a

if false a b
// b

Ok, lastly write xor好了,最后写xor

xor := λa.λb.if a (not b) b

(xor true true) a b
// b

(xor true false) a b
// a

(xor false true) a b
// a

(xor false false) a b
// b

Remember if is kind of dumb here, we can just remove it请记住, if这里有点愚蠢,我们可以将其删除

xor := λa.λb.a (not b) b

Now if we want to write it all with pure lambda, just replace the not with its definition现在,如果我们想用纯 lambda 编写所有内容,只需将not替换为其定义

xor := λa.λb.a (not b) b
->β [ not := λp.p false true ]

xor := λa.λb.a ((λp.p false true) b) b
->β [ p := b ]

xor := λa.λb.a (b false true) b

At this point, you can see we have the definition from your question一点,你可以看到我们有定义从你的问题

a xor b = (a)((b)(false)(true))(b) a xor b = (a)((b)(false)(true))(b)

But of course that introduced additional free variables false and true – you can replace those with a couple additional beta reductions但是当然这引入了额外的自由变量falsetrue - 你可以用几个额外的 beta 减少来替换它们

xor := λa.λb.a (b false true) b
->β [ false := (λa.λb.b) ]

xor := λa.λb.a (b (λa.λb.b) true) b
->β [ true := (λa.λb.a) ]

// pure lambda definition
xor := λa.λb.a (b (λa.λb.b) (λa.λb.a)) b

考虑a(b FT)b,中间表达式本质上是(not b),所以a(not b)b 仅在a 和b 不同时才为真。

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

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