Getting Started with lambda calculus

Source: Internet
Author: User

Lambda operators are the basis of all functional languages, and it is understood that lambda operators have many advantages in mastering functional languages. Recently learned a little bit of knowledge, so write here to forget:

1 Basics

The lambda operator is the theoretical basis of functional programming and is another computational model outside Turing. It is very concise, only three rules, but it can express all the functions that can be computed.

The core concept of the lambda operator is expression expressions. The following rules are generated for the lambda operator:

<expression>:: = <name>|<function>|<application>
<function>::=λ<name>.<expression>
<application>:: = <expression><expression>

Be aware that:

    • The <name> representative identifier.
    • The expression defaults to the left combination (that is, from the leftmost start), and can be bracketed if the order needs to be changed.
1.1 function

function represents the definition of functions, using

Λ<name>.<expression>

The form. <name> represents the function's argument, and expression is the return value of the function.

The function in λ is a single-argument, as in the following example:

Λa.a+1

This example assumes that the + operation has been defined

So how do you define a multi-parameter function? You can use nested structures:

Λa.λb.a+b

This example can also be abbreviated as

Λa b.a+b

1.2 Application

Application represents the application of a function. In the application, the use of parameters is done by substitution, so the names of the parameters in the function are actually irrelevant, so

Λx.x≡λy.y≡λz.z

Use the following symbol to represent the substitution:

(λx.x) y = [y/x]x = y

[y/x] means that x is replaced by y, a simple notation is to think of the X component mother, after the next x is left only Y, so that the replacement is completed

1.3 free variables and constraint variables

In the lambda operator, we refer to the variable that appears in the λ parameter as a constraint variable, and the non-appearing lambda variable is called a free variable, such as:

Λx.xy

X is a constraint variable and y is a free variable.

The rules for judging free variables have the following three articles:

    • In <name> the corresponding part <name> is free
    • ,<name> is free in λ<name1>.<exp> and only if <name>≠<name1> and <name> is free in exp
    • In E1 E2,<name> is free when and only if <name> is free in E1 or <name> is free in E2

There are two rules for judging the constraint variables:

    • ,<name> in λ<name1>.<exp> is constrained when and only if <name>=<name1> or <name> is constrained in exp
    • ,<name> in E1 E2 is free when and only if <name> is constrained in E1 or <name> is constrained in E2

It is important to note that a variable may be both free and constrained in an expression, considering the following example:

(ΛX.XY) (ΛY.Y)

In Λx.xy, Y is free, but in λy.y y is constrained.

1.4 Replacement

In the lambda operator, the function is not a name, so it is a very troublesome thing to use, each time the function needs to write its definition again. Therefore, for the sake of simplicity, we can define individual names for the function:

f≡λx.x
FF = (λx.x) (ΛZ.Z)
= [Λz.z/x]x
=λx.x
= f

When there are free variables in a function, you need to be careful about the conflict of variable names:

(λx. (λy.xy)) y =λy.[x/y]y =λy.yy

It is important to note that X is a free variable in the expression Λy.xy. At this point, two Y has a conflict. Rename the constraint variable y in the function to T:

(λx. (λt.xt)) y =λt.[x/y]t =λy.yt

Finally, summarize the substitution rules. When the function λx.<exp> is applied to the expression E, all free x in exp is replaced with E. If the substitution causes the free variable to become a constraint variable, then the conflicting constraint variable should be renamed before the substitution.

Let's look at an example:

(λx. (Λy. (x (ΛX.XY))) y

In Λy (x (ΛX.XY)) only the first x is free, so you only need to replace the first X. However, substituting x is Y and x becomes a constraint variable, so you need to rename the constraint variable y:

[y/x] (Λt. (x (ΛX.XT))) = (λt. ([Y/x]x (ΛX.XT))) = (λt. (Y (ΛX.XT)))

It is important to note that in the substitution of λx.<exp>, the free variable x in <exp> is replaced, instead of the variable x in λx.<exp>, there is no free x in λx.<exp>.

In addition, when the function is used, if the given parameter is insufficient for the function definition, a new function is generated, and if the number of arguments is sufficient, the corresponding λ portion is eliminated.

Example≡λxy.x (y)
Example A≡ (λxy.x (y)) a
≡ (lambda y.a (y))
Example a b≡ (λxy.x (y)) a B
≡a (b)

2 Church numbers

With the lambda operator, many basic concepts can be redefined from a new perspective.

All natural numbers can be defined by the following definitions:

0≡λsz.z
1≡λsz.s (z)
2≡ΛSZ.S (S (z))

The number of such definitions is called Church number.

With church numbers, you can also perform operations on them:

S≡ΛWYX.Y (Wyx)
S0≡ (λwyx.y (Wyx)) (ΛSZ.Z)
≡λyx.y ((λsz.z) YX)
≡ΛYX.Y (x)
≡1
S1≡ (λwyx.y (Wyx)) (Λsz.s (z))
≡λyx.y ((Λsz.s (z)) YX)
≡λyx.y (y (x))
≡2

S can be added to a logarithmic operation. So how do you implement addition?

2s1≡ (Λsz.s (S (z))) (Λwyx.y (Wyx)) (Λuv.u (v))
≡ΛWYX.Y (Wyx) (Λwyx.y (Wyx) (λuv.u (v)))
≡ss1
≡ΛWYX.Y (Wyx) (Λyx.y (y (x)))
≡s2
≡3

In this example, the number 2 adds an operation nested two times, sequentially acting on 1, get 3; Similarly, when the calculation M+n is, M will add an operation nested m times and action on N, the result is m+n.

The multiplication is defined as follows:

X≡ (λxyz.x (YZ))
X 2 2≡ (λxyz.x (YZ)) 2 2
≡λz.2 (2z)
≡λz.2 ((Λuv.u (U (v))) z)
≡λz.2 (Λv.z (z (v)))
≡λz. ((ΛAB.A (A (b))) (Λv.z (z (v))))
≡λz. (Λb. (Λv.z (z (v)) ((Λv.z (z (v)))))
≡λzb. (Λv.z (z (v))) (Z (z (b))))
≡λzb.z (Z (z (b))))
≡4

3 Logical Operations

The lambda operator can also be used to define the logical value TRUE and false, and to operate on it:

t≡λxy.x
F≡λxy.y

The definition of correctness is simple, and the definition of logical operations is not complex:

∧≡λxy.xyf
∨≡λxy.xty
¬≡λx.xft

With a logical value, you can define a function with conditional branching. Now let's define a function so that input 0 returns T, and a positive number returns F:

Z≡λx.xf¬f

If it works on 0, it will unfold as follows:

Z0≡0f¬f≡ (ΛSZ.Z) (f¬) f≡¬f≡t

If the function is on a positive integer n, it expands to the following (2 for example):

Z2≡2f¬f≡ (Λsz.s (S (z))) (f¬) F
≡f (F (¬))) f≡faf≡f
A≡f (¬)

As the example shows, no matter how many F (f (f ()) are nested internally, the returned result is F, because it is all in the form of FAF.

After that, you can define a comparison function. Before you do this, you need to define a minus function first.

For any one two-tuple (x, y), you can define it with the following expressions:

Λzxy.xy

When Z is T, the first number of the two-tuple can be obtained, and the second number is available for F.

Now define a function g, give two tuples (M, N), return (M+1, m):

G≡λpz.z (S (PT)) (PT)

where P is a two-tuple

The minus 1 operation of the logarithmic m is to perform M-G operations on the z00 two-tuple, and then take its second element, so the minus one is defined as:

P≡λn.ng (λp.p00) F

Note that when input is 0 o'clock, the returned result is still 0

At this point, the x≥y can be defined as the result of X minus one operation on y to 0:

Gt≡λxy. Z (XPy)

In this way, the wait operation is x≥y and y≥x:

Eq≡λxy.∧ (GT XY) (GT YX)

By the large, equal and logical operation, you can define less than, small, and so on a series of comparison operations, this is skipped.

4 y operator

Cond

Getting Started with lambda calculus

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.