Lambda Operator 3: The genius of Alonzo church

Source: Internet
Author: User
Tags arithmetic operators
Lambda Operator 3 the genius of Alonzo Church: the number in the lambda Operator

FrontAfter creating the basic rules for Lambda operations, you can use the lambda operator to do something interesting. For convenience, let's start with the syntax sugar flower difference, which is used to name the function. These syntactic sugar can make complex formulas easier to write.

We use "let" to introduce a "global" function (that is, we do not need to define this function once in each expression ):
Let squer = Lambda x. x ^ 2

This statement declares a function called "square", which is defined as lamdba x. x ^ 2. If we have an expression "Square 4", the above "let" means that this expression is the same as the following expression:
(Lambda square. Square 4) (lambda x. x ^ 2 ). This "let" is borrowed from Common Lisp or scheme. There is nothing in the lambda operator. Mathematicians advocate "without adding entities if necessary ". These keywords do not fall into their eyes. However, for those who are familiar with the program, these syntactic sugar is much more loose.

In our example, numbers and Arithmetic Operators are used. But remember that there are no numbers in the lambda operator. We only have functions! So we need to invent and use functions.Create numbers. Fortunately, Alonzo church is a genius. Since he invented the lambda operator, it is natural to use the lambda operator to represent numbers. The function he developed for numbers is naturally called the church numerals ).

In the Qiqi number, all numbers are two parameter functions:

  1. Zero is lambda s Z. Z
  2. First, Lambda s Z. S Z
  3. Second, Lambda s Z. S (s Z)
  4. For any number "N", its Qiqi number is a function. This function applies its first parameter to the second parameter n times. Lambda s Z. S Sn Z is a popular method. Ring. The unfortunate thing about formalizing the domain name is dealing with tongue twisters. How can this problem be solved? Of course, it is important to remember that feimanWhen connection machine is workingLearning Method: The simplest question. "Give me the simplest example ". "How can we verify that this is correct ?".
    For example, if the value is zero (lambda s Z. Z), the first parameter is "S". If the application is zero, the function is "Z ". What about number 1? When the first parameter S is applied to Z, the function body becomes "s Z ".

When I understand this definition, I think of "Z" as the name of the zero in the Qiqi number, and "S" as the name of the successor function. The "successor function" is actually very simple, and the ++ in C/C ++ is also. Therefore, zero is a function that returns the value "0". One is to apply the successor function to the previous zero function; the second is to apply the successor function to a previous function or zero function. 0 ++ gets 1, 1 ++ is equivalent to (0 ++) ++, and 1 ++ gets 2. now we can change 0 to Z and ++ to S. Everything is clear.

Now -- read it. If we want to add x + y, we need a function with four parameters. The two parameters represent the two numbers that are added and the "s" and "z" required for the result ".

Let add = Lambda s Z x Y. x s (Y s Z)

It seems like a bit of a cloud. However, we can use curry to separate "S" Z "from X and Y. First, the function obtained by curry includes two parameters, X and Y (this is like add (x, y), which meets our understanding of the plus sign ). Second, we need to normalize the S and Z required by X and Y so that X and Y share the binding of the same zero-sum and successor functions:

let add = lambda x y. (lambda s z . (x s (y s z)))

Taking a closer look, the formula above is nothing more than adding X and Y. We will first use "S" and "z" to create Qiu Qi number "Y ", apply "X" to y. "S" and "z" are "S" and "z" in "Y ". That is to say, the result we get is a function that adds itself to another function. Use examples to illustrate the problem. For example, 2 + 3:

Add (lambda s Z. S (s z) (lambda s Z. S (s z) News newz

In order to make the calculation a little easier, we first perform Alpha conversion on 2 and 3. Let 2 Use S2 and Z2, and 3 Use S3 and Z3:

add (lambda s2 z2 . s2 (s2 z2)) (lambda s3 z3 . s3 (s3 (s3 z3)))

Now we can replace "add" with its definition:

(Lambda x y. (lambda s Z. (x s y (s z) (lambda S2 Z2. s2 (s2 Z2) (lambda S3 Z3. s3 (S3 (S3 Z3 )))

Now we can use beta to change "add" (Note: Replace the X and Y parameters with the corresponding real parameters ):

Lambda s Z. (lambda S2 Z2. S2 (s2 Z2) S (lambda S3 Z3. S3 (S3 (S3 Z3) s Z)


Then we can perform beta conversion on the 3-level QPS. This step is actually "normalization" 3: Put the successor function and Zero Function in the definition of 3 (remember that zero is a function ?) Replace it with the successor function and Zero Function in the parameter list of add:

Lambda s Z. (lambda S2 Z2. S2 (s2 Z2) S (s z )))

Well, it's a little eye-catching. It's really beautiful now. The next beta transformation to 2. Let's see what we are going to do: 2 is a function with two parameters: one parameter is a successor function, and the other is a zero function. To add 2 to 3, we only need to use the successor function of the "add" function. That is to say, we regard the result after calculation 3 as the value of the zero function!

Lambda s Z. S (s z )))

In this formula, the number is 5!

What makes Qiu Qi's cool is that it abandons the concept of a traditional integer and replaces it with a function. It maps each number to a function. The Count operation corresponds to the number of times a function is applied (here it is a successor function. Of course, the above introduction is very simple. If you are interested in Qiqi, seeThis article.

What is the use of qiuqi for programming? I really don't know. However, Qiu Qi number (and then Qiu Qi coding) does have important applications in a series of basic theories, such as Lambda operators of different types. But is that important? Not important? Important? Not important? Research.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.