Text substitution based interpreter: adding integer type

Source: Internet
Author: User
Tags eval

To implement an interpreter methodically, I will follow the following three steps:

1. Clear grammar

2. Evaluation procedure for syntax description

3. Write code implementation based on evaluation process

Grammar

The (\lambda\) calculus is not suitable for use as a practical programming language. The (\lambda\) calculus has only two types of variables and functions, while other common types such as integers, Boolean, and characters are not. Although it is possible to represent these common types in a coded way, this can be cumbersome. It is usually directly extended \ (\lambda\) calculus, adding some common types and basic operations for these types. This extension of the language is referred to as Iswim, the full name unknown ...

For simplicity, I only add integer types, as well as addition and subtraction. The expanded syntax is as follows: \begin{eqnarray*} M, N, L &=& X \ &|& b \ &|& \lambda x.m \ &|& (+ \; M \; N) \ &|& (-\; M \; N) \ &|& (M \; N) \end{eqnarray*} The new joined second line \ (b\) represents an integer, the fourth row is an expression of an addition operation, and the fifth row is an expression of a subtraction operation.

Evaluation process

To describe the evaluation process of an interpreter that computes the language, the first step is to explicitly evaluate the stop condition. We stipulate that the interpreter has found the final result when we prescribe one of the three expressions of x\, (b\), \ (\lambda x.m\). These three expressions are called values and are represented by the letter \ (v\). \begin{eqnarray*} V &=& X \ &|& b \ &|& \lambda x.m \end{eqnarray*}

The result of the evaluation of the expression \ (m\) is represented by a notation \ (eval (M) \). For values, the evaluation simply returns to themselves. \begin{eqnarray*} eval (x) &=& x \ eval (b) &=& b \ eval (\lambda x.m) &=& \lambda x.m \end{eqnarra y*} plus subtraction and function calls these three lines are recursively defined, so the evaluation process is recursive. \begin{eqnarray*} eval (+ \; M \; N) &=& eval (+ \; V_1 \; v_2) \ eval (-\; M \; N) &=& eval (-\; V_1 \; v_2)) \ eval ((M \; N) &=& eval (v_1 \; v_2) \end{eqnarray*} where \ (V_1=eval (M) \), \ (V_2=eval (N) \).

To make the interpreter as simple as possible, assume that the input program is correct. That is, the addition and subtraction of \ (v_1\) and \ (v_2\) are integers, recorded as \ (b_1\) and \ (b_2\), and \ (v_1\) in a function call is a function \ (\lambda x.l\).

Add and subtract, as the literal intention, is to add and subtract. The function call procedure is a \ (\beta\) reduction process. \begin{eqnarray*} eval ((+ \ b_1 \ b_2)) &=& b_1 + b_2 \ eval ((-\ b_1 \ b_2)) &=& b_1-b_2 \ eval ( \lambda X.L \; v_2)) &=& eval (l[x \leftarrow v_2]) \end{eqnarray*}

Because the new syntax is added, the replacement process also adds the appropriate procedure. The entire replacement process is listed here: \begin{eqnarray*} x_1[x_1 \leftarrow N] &=& n \ x_2[x_1 \leftarrow N] &=& x_2 \ && X_1 \neq x_2 \ b[x \leftarrow n] &=& b \ (\lambda x_1.m) [x_1 \leftarrow n] &=& (\lambda x_1.m) \ (\lamb Da x_1.m) [x_2 \leftarrow N] &=& (\lambda x_3.m[x_1 \leftarrow x_3][x_2 \leftarrow N]) \ && x_1 \neq 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]) \ (-\; M_1 \; m_2) [X \leftarrow N] &=& (-\; M_1[x \leftarrow N] \; M_2[x \leftarrow N]) \ (m_1 \; m_2) [X \leftarrow n] &=& (m_1[x \leftarrow n] \; M_2[x \leftarrow N]) \end{eqnarray*}

The final evaluation process is summarized as follows: \begin{eqnarray*} eval (x) &=& x \ eval (b) &=& b \ eval (\lambda x.m) &=& \lambda x.m \ eval (+ \; M \; N) &=& eval (M) + eval (n) \ eval (-\; M \; N) &=& eval (m)-eval (n) \ eval (m \; N) &=& eval (l[x \leftarrow eval (N))) \ && where eval (M) = \lambda x.l \end{eqnarray*}

Realize

This uses the racket language to write the interpreter. Interpreter input does not use strings, but instead uses a racket notation system. The use of symbolic systems is to simplify the work of parsing. The syntax analysis can be realized conveniently by using racket pattern matching. In addition, the computer input \ (\lambda\) is still troublesome, so in the language of implementation (Lambda X M) instead of \ (\lambda x.m\).

The interpreter is a program that implements the \ (eval\) function. The code is the formula for the evaluation procedure, which is not explained in one sentence. Value-of is a value-seeking process:

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.