The interpreter and lambda calculus based on text substitution

Source: Internet
Author: User
Tags definition contains expression variables

Recently more idle, intend to tidy up the previous study on the language of the program knowledge. The main content is to design the program language while writing the interpreter to implement it. This knowledge is essentially derived from programming Languages and Lambda calculi and essentials of programming Languages these two books.

I remember the high School Olympiad training when the teacher said: "To solve problems must grasp the definition." "As with programming and reconciliation questions, you have to grasp the definition." So before you write an interpreter, you have to define the program language that you want to explain. This program language is based on lambda calculus.

From the \ (\lambda\) calculus

I really don't want to talk about the (\lambda\) calculus ... Anyway, just a brief explanation. The position of the \lambda\ calculus in the programming language is like the set theory in mathematics. As with every mathematical textbook, it begins with the set theory, and each program language textbook is also to be \lambda\. But then again, the \lambda\ calculus is set. We will not go so far, tired and no meaning ...

The basic types in the (\lambda\) calculus are only variables and functions. Variables are expressed in uppercase letters \ (x\). Like \ (A,b,x,y,abc,... \) are variables. A function contains two elements: one is the function argument (the formal parameter), it is a variable, the other element is the function body, it is a \ (\lambda\) calculus expression (here is a recursive definition). Use (lambda x M) to represent a function where X is a variable, and M is a \ (\lambda\) calculus expression (don't spit out the slot parameter X where the parentheses are missing.) )。 To describe simplicity, you also use \ (\lambda x.m\) to represent a function.

For example, \ (\lambda x.x\) is an identity function \ (f (x) = x\). In mathematics it is generally used \ (f (a) \) to represent a function call, \ (a\) is an argument. In the \ (\lambda\) calculus, the function is also enclosed in parentheses, which is recorded as \ (\lambda x.x \ a) \). function calls are calculated by replacing the formal parameters with arguments in the function body. In this example \ ((\lambda x.x \ A) = A\). This computational process is called a reduction.

The functions of the \ (\lambda\) calculus contain only one parameter. If you want to use a multiple-parameter function, you can nest multiple functions. Here's an example: \[\lambda x.\lambda y. (x \ y) \] This technique is called currying.

From the above discussion, the \ (\lambda\) calculus contains only three expressions. The syntax for formally defining \ (\lambda\) calculus is as follows: \begin{eqnarray*} m, N, L &=& X \ &|& \lambda x.m \ &|& (M \; N) \end{eqnarray*} Here is an expression of the capital letter \ (m\), \ (n\) and \ (l\) representation (\lambda\) calculus, which is a recursive definition, where the second and third lines appear \ (m\) and \ (n\). The third line of expression is a function call, the general requirements in the function of the position of \ (m\) should be able to be reduced to a function, otherwise the reduction will not be able to go on.

Here are some examples of expressions for the \lambda\ calculation: \begin{eqnarray*} & x \ & \lambda x.x \ & (\lambda x.x \; y) \ & (\lambda x . (x; x) \; \lambda x.x) \ & (\lambda x. (x; x) \ \lambda x. (x; x) \end{eqnarray*}

The reduction of the (\lambda\) calculus depends on the substitution operation. You must first introduce the free variable before you introduce the replacement operation.

Free variable

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Examine an expression: \ ((\lambda x. (\LAMBDA x.x \ x) \ a) \). This expression is attributed to \ ((\lambda x.x \ a) \). As you can see, the \ (x.x) behind the midpoint of the variables \ (x\) and \ (\lambda x.x\) in the \lambda (\lambda x) function body \ (\lambda x\ \ x) is different. \ (x\) in the parameter position is replaced by \ (a\) and the \ (x\) behind the midpoint of \ (\lambda x.x\) is not replaced. The substituted \ (x\) is called the free variable of an expression \ (\lambda x.x \ x) \). Only the free variable is substituted during the substitution of the function call.

A free variable is a variable that is not constrained in an expression. Constraint means that the variable does not exist as an argument to a function. F\ is a free variable, \ (x\) is not a free variable, as in an expression \ (\lambda x. (f x) \). Use \ (FV (M) \) to represent a collection of all the free variables in expression \ (m\).

Starting from here, some definitions and algorithms that describe the \lambda\ calculus will follow the syntax definition of the \lambda\ calculus. So the arithmetic (FV (m) \) algorithm (also the exact definition of FV (m)) should be divided into variables, functions, and function calls three kinds of discussion: \begin{eqnarray*} FV (X) &=& \{x\} \ FV (\lambda x.m) & amp;=& FV (m) \backslash \{x\} \ FV ((M \; n)) &=& FV (M) \cup FV (n) \end{eqnarray*}

Replace

Use the notation \ (m[x \leftarrow n]\) to replace the free variable \ (x\) (if there is a free variable) in expression \ (m\) with an expression \ (n\). More accurate definitions such as the following formula: \begin{eqnarray*} x_1[x_1 \leftarrow n] &=& n \ x_2[x_1 \leftarrow N] &=& x_2 \ && its X_1 \neq x_2 \ (\lambda x_1.m) [x_1 \leftarrow n] &=& (\lambda x_1.m) \ (\lambda x_1.m) [x_2 \leftarrow N] &= & (\lambda x_3.m[x_1 \leftarrow x_3][x_2 \leftarrow N]) \ && where x_1 \neq x_2, X_3 \notin FV (n), X_3 \notin FV (M ) \backslash\{x_1\} \ \ (m_1 \; m_2) [X \leftarrow n] &=& (m_1[x \leftarrow n] \; M_2[x \leftarrow N]) \end{eqnarray*} The fourth formula looks more complex, in fact, in order to avoid the case of the free variable \ (x_1\) in \ (n\). For example, \ (\lambda x.y[y \leftarrow (x x)]\) should be replaced with \ (\lambda Z. (x) \). If replaced by \ (\lambda x. (x) \) is wrong.

If there is no free variable \ (x_1\) in \ (n\), then this formula can be reduced to: \begin{eqnarray*} (\lambda x_1.m) [x_2 \leftarrow n] = (\lambda x_1.m[x_2 \leftarrow N ]) \end{eqnarray*}

Reduction

The so-called reduction, can be understood as a value, or the expression of simplification (junior high school seems to have learned algebra expression of simplification). \ (\lambda\) calculus has three methods of reduction. Three kinds of \alpha\ are referred to as \ (\beta\) reduction and/(\eta\) reduction. The name looks very infiltration person, does not say these three kind of attribution is difficult to understand, only explained that the name person does not have a love to play the heart.

\alpha\ means that the variable name of a function parameter variable does not matter. For example, the same function represented by \ (\lambda x.x\) and \ (\lambda y.y\). This is basic, but it's almost impossible to use. \[\lambda x_1.m \rightarrow_\alpha \lambda x_2.m[x_1 \leftarrow x_2] \quad \text{where}x_2 \notin FV (M) \]

\ (\beta\) refers to the function call process, which is the most commonly used reduction. (\beta\) replaces the parameter variable (formal parameter) that appears in the function body with the input arguments (arguments) of the function call: \[(\lambda x.m \; N) \rightarrow_\beta m[x \leftarrow n] \]

\ (\eta\) refers to: \[\lambda x. (M \; X) \rightarrow_\eta M \quad \text{}x \notin FV (m) \] This is a bit strange, but it's not hard to understand.

The role of an interpreter is to enter an expression that outputs the expression to the simplest (no longer (\beta\) reduction) Form. Generally we want this minimalist form to be either a variable (\ (x\)) or a function (\ (\lambda x.m\)), because the function call is used to make a person make \ (\beta\). variables, or functions, are called "values." But there are also some broken expressions like \ ((x; x) \), because \ (x\) is a variable rather than a function, this expression can not be reduced to about. An expression that is usually considered illegal in such an expression. If the output of this result means that the input program is wrong, the program crashes. Other expressions cannot be attributed to some minimalist form, the infinite cycle (poor Sisyphus). A classic example of an infinite loop is this input: \ (\lambda x. (x; x) \; \lambda x. (x; x) \).

An interpreter, give it an input, it will have the following three kinds of conditions:

Output a value:-)

Crash xd

Infinite Loop @_@

Call! Finally finished.

Cnblogs Gu

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.