Lambda 演算中布尔值的查询

发布于 2024-08-24 00:34:31 字数 141 浏览 3 评论 0原文

这是 AND 运算符的 lambda 演算表示形式:

lambda(m).lambda(n).lambda (a).lambda (b). m(n a b) b

任何人都可以帮助我理解这种表示形式吗?

This is the lambda calculus representation for the AND operator:

lambda(m).lambda(n).lambda (a).lambda (b). m(n a b) b

Can anyone help me in understanding this representation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许一世地老天荒 2024-08-31 00:34:31

要理解如何在 lambda 演算中表示布尔值,考虑一下 IF 表达式“if a then b else c”会有所帮助。这是一个表达式,如果为真,则选择第一个分支 b,如果为假,则选择第二个分支 c。 Lambda 表达式可以非常轻松地做到这一点:

lambda(x).lambda(y).x

将为您提供第一个参数,并

lambda(x).lambda(y).y

为您提供第二个参数。因此,如果 a 是这些表达式之一,则

a b c

给出 bc,这正是我们希望 IF 执行的操作。因此,define

 true = lambda(x).lambda(y).x
false = lambda(x).lambda(y).y

ab c 的行为类似于 if a then b else c

查看 (nab) 的表达式,这意味着 if n then a else b
那么 m (nab) b 表示

if m then (if n then a else b) else b

如果 mn 都为 true,则此表达式的计算结果为 a,否则为b。由于 a 是函数的第一个参数,而 b 是第二个参数,并且 true 已被定义为一个函数,该函数给出其第一个参数两个参数,那么如果 mn 都为 true,则整个表达式也是如此。否则为。这就是 and 的定义!

这一切都是由 Alonzo Church 发明的,他发明了 lambda 演算。

To understand how to represent Booleans in lambda calculus, it helps to think about an IF expression, "if a then b else c". This is an expression which chooses the first branch, b, if it is true, and the second, c, if it is false. Lambda expressions can do that very easily:

lambda(x).lambda(y).x

will give you the first of its arguments, and

lambda(x).lambda(y).y

gives you the second. So if a is one of those expressions, then

a b c

gives either b or c, which is just what we want the IF to do. So define

 true = lambda(x).lambda(y).x
false = lambda(x).lambda(y).y

and a b c will behave like if a then b else c.

Looking inside your expression at (n a b), that means if n then a else b.
Then m (n a b) b means

if m then (if n then a else b) else b

This expression evaluates to a if both m and n are true, and to b otherwise. Since a is the first argument of your function and b is the second, and true has been defined as a function that gives the first of its two arguments, then if m and n are both true, so is the whole expression. Otherwise it is false. And that's just the definition of and!

All this was invented by Alonzo Church, who invented the lambda calculus.

无言温柔 2024-08-31 00:34:31

在 lambda 演算中,布尔值由一个带有两个参数的函数表示,一个表示成功,一个表示失败。这些参数被称为延续,因为它们继续进行其余的计算;布尔值 True 称为成功延续,布尔值 False 称为失败延续。这种编码称为 Church 编码,其思想是布尔值非常类似于“if-then-else 函数”。

所以我们可以说

true  = \s.\f.s
false = \s.\f.f

其中 s 代表成功,f 代表失败,反斜杠是一个 ASCII lambda。

现在我希望你能明白事情的发展方向。我们如何编码?好吧,在 C 中我们可以将其扩展为

n && m = n ? m : false

只有这些是函数,所以

(n && m) s f = (n ? m : false) s f = n ? (m s f) : (false s f) = n ? (m s f) : f

但是,当在 lambda 演算中编码时,三元构造只是函数应用,所以我们有

(n && m) s f = (n m false) s f = n (m s f) (false s f) = n (m s f) f

所以最终我们到达

&& = \n . \m . \s . \f . n (m s f) f

如果我们重命名成功和失败延续对于 ab,我们返回到原始值

&& = \n . \m . \a . \b . n (m a b) b

与 lambda 演算中的其他计算一样,尤其是在使用 Church 编码时,使用代数计算通常更容易定律和方程推理,然后最后转换为 lambda。

In the lambda calculus, a Boolean is represented by a function that takes two arguments, one for success and one for failure. The arguments are called continuations, because they continue with the rest of the computation; the boolean True calls the success continuation and the Boolean False calls the failure continuation. This coding is called the Church encoding, and the idea is that a Boolean is very much like an "if-then-else function".

So we can say

true  = \s.\f.s
false = \s.\f.f

where s stands for success, f stands for failure, and the backslash is an ASCII lambda.

Now I hope you can see where this is going. How do we code and? Well, in C we could expand it to

n && m = n ? m : false

Only these are functions, so

(n && m) s f = (n ? m : false) s f = n ? (m s f) : (false s f) = n ? (m s f) : f

BUT, the ternary construct, when coded in the lambda calculus, is just function application, so we have

(n && m) s f = (n m false) s f = n (m s f) (false s f) = n (m s f) f

So finally we arrive at

&& = \n . \m . \s . \f . n (m s f) f

And if we rename the success and failure continuations to a and b we return to your original

&& = \n . \m . \a . \b . n (m a b) b

As with other computations in lambda calculus, especially when using Church encodings, it is often easier to work things out with algebraic laws and equational reasoning, then convert to lambdas at the end.

浅浅淡淡 2024-08-31 00:34:31

实际上它不仅仅是 AND 运算符。这是 lambda 演算的 if m and n then a else b 版本。解释如下:

在 lambda 演算中,true 表示为接受两个参数* 并返回第一个参数的函数。 False 表示为接受两个参数*并返回第二个参数的函数。

上面显示的函数需要四个参数*。从表面上看,m 和 n 应该是布尔值,a 和 b 应该是其他值。如果 m 为真,它将计算其第一个参数,即 na b。如果 n 为真,则计算结果为 a;如果 n 为假,则计算结果为 b。如果 m 为 false,它将计算其第二个参数 b。

所以基本上,如果 m 和 n 都为 true,则该函数返回 a,否则返回 b。

(*) 其中“采用两个参数”表示“采用一个参数并返回采用另一个参数的函数”。

编辑以回应您的评论:

and true false 如维基页面上所示,工作原理如下:

第一步是将每个标识符替换为其定义,即 (λm.λn.mnm )(λa.λb.a)(λa.λb.b)。现在应用函数(λm.λn.mnm)。这意味着 mn m 中每次出现的 m 都会替换为第一个参数 ((λa.λb.a)),每次出现的 n 都会替换为第二个参数((λa.λb.b))。所以我们得到(λa.λb.a) (λa.λb.b) (λa.λb.a)。现在我们只需应用函数(λa.λb.a)。由于该函数的主体只是 a,即第一个参数,因此其计算结果为 (λa.λb.b),即 false(如 λx.λy y 表示)。

Actually it's a little more than just the AND operator. It's the lambda calculus' version of if m and n then a else b. Here's the explanation:

In lambda calculus true is represented as a function taking two arguments* and returning the first. False is represented as function taking two arguments* and returning the second.

The function you showed above takes four arguments*. From the looks of it m and n are supposed to be booleans and a and b some other values. If m is true, it will evaluate to its first argument which is n a b. This in turn will evaluate to a if n is true and b if n is false. If m is false, it will evaluate to its second argument b.

So basically the function returns a if both m and n are true and b otherwise.

(*) Where "taking two arguments" means "taking an argument and returning a function taking another argument".

Edit in response to your comment:

and true false as seen on the wiki page works like this:

The first step is simply to replace each identifier with its definition, i.e. (λm.λn. m n m) (λa.λb. a) (λa.λb. b). Now the function (λm.λn. m n m) is applied. This means that every occurrence of m in m n m is replaced with the first argument ((λa.λb. a)) and every occurrence of n is replaced with the second argument ((λa.λb. b)). So we get (λa.λb. a) (λa.λb. b) (λa.λb. a). Now we simply apply the function (λa.λb. a). Since the body of this function is simply a, i.e. the first argument, this evaluates to (λa.λb. b), i.e. false (as λx.λy. y means false).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文