(Reference: "Type and Programming Languages")
First, the syntax:
T:: = terms:
X variable
ΛX.T Abstraction
T T application
ΛX.T can be understood as: a function that takes a variable x as a parameter and returns a term T. T is the body of the abstract . (the body of an LAMDA expression always extends as long as possible)
L if X appears in λx.t T, it is called X by the abstract constraint (BIND), called λx is a binderwith a range of T.
If X appears in a position and is not constrained by an abstraction on a closed x, the x is called free . For example: x y and Λy. x in X Y
L If a term does not contain a free variable, it is called a closed (closed), or called a combinator.
Second, the operation of the syntax:
There are no built-in constants and basic operators in the syntax definition above, so the only "calculation" is to function on the parameter (also the function).
The one-step calculation is equivalent to rewriting a application: substituting the element on its right-hand side with the bound variable in the abstract body of the left-hand side. That
The right part indicates that all the free x in T1 is replaced with T2.
Example: (λx.x) y→y
(λx.x (λx.x)) (u r) →u R (λx.x)
"Because the abstract body:x (λx.x) on the left, only the first X is free. 】
The term is called redex("reducible expression"), and the above rule overrides a redex, called Beta-reduction.
Third, the Lambda-calculus evaluation strategy:
1.full Beta-reduction:any Redex is reduced at any time.
2.normal Order strategy: The leftmost and most outward redex are always prioritized for simplification/normalization.
3.call by name: Strategy is not allowed within the abstract.
4.call by Value strategy: most widely used. (only outermost redexes is reduced?) )
Iv. Programming with LAMDA expression
1. Multiple parameters: use higher-order functions.
For example, with a value pair as a parameter, the function f =λ (x, y) of S is returned. S can be written as F =λx.λy.s, meaning "F is a function: pass in the parameter U as x, return an incoming parameter V as Y return the result s of the function". Then f u v =>λy. [X→u]s v = [y→v][x→u]s
2. Church Booleans
Define term Tru and FLS as follows (different from constant true and flase):
Tru =λt. Λf. T
FLs =λt. Λf. F
The Tru v W returns V;fls v W to return W.
You can define Boolean operations such as: and =λa. Λb. A b FLs, then return to FLS only if you and V are Tru and W v return to Tru.
3. Pairs
We can define value pairs as term:
Pair =λa. Λb. Λf. f a B; Here f should be Tru or FST
FST =λp. P Tru; The p here are all pair
SND =λp. P FLS;
Example: FST (pair v W) →fst (λb. b v w) → (λp. P tru) (λb. b v w) → (λb. B v W) Tru→tru v w→v
4. Church numerals
Define church numerals as follows:
C0 =λs. Λz. Z
C1 =λs. Λz. s z;
C2 =λs. Λz. s (s z);
C3 =λs. Λz. S (S (z));
...
Each number n is represented as a combinator cn: Passing in two parameters S and Z (representing "successor" and "Zero" respectively), and s acting on Z n times.
Then we can define the successor function as follows:
SCC =λn. Λs. Λz. S (n s z);
The SCC is a combinator, passing in a church numeral N, returning to the subsequent relay.
Similarly, it is possible to define:
Plus =λm. Λn. Λs. Λz. M s (n s z);
Times =λm. Λn. M S (plus n) c0;
Iszro =λm. M (λx FLs) Tru;
The implementation of subtraction is more complicated by predecessor function: Incoming C0 returns C0, incoming ci+1 returns CI. The function SS returns the pair CJ cj+1 with a pair CI CJ as the parameter. So the SS M is applied to the pair c0 C0 can get the pair c0 C0 (when m=0) or pair cm-1 cm (when m>0).
ZZ = pair C0 C0;
SS =λp. Pair (SND p) (plus C1 (SND p));
PRD =λm. FST (M SS ZZ);
6. Enriching
As you can see from the front, the Boolean value, the number, and the action on it can be encoded by a pure LAMDA expression. Basic Boolean and numeric types are introduced for convenience. The rich system is called ΛNB. The two expressions in ΛNB can be converted to each other. To convert a church Boolean to a basic Boolean value, simply apply it to true and false:
Realbool =λb. b true false;
The reverse direction conversion is done through the IF expression:
Churchbool =λb. If B then Tru else FLS;
A equality function that returns a true Boolean value on the church numeral can be written as:
Realeq =λm. Λn. (equal m n) True false;
Similarly, a church numeral is converted to a corresponding base value by acting on SUCC and 0:
Realnat =λm. M (λx. SUCC x) 0;
7. Recursion
As mentioned earlier, a term that can no longer apply the law of evaluation is called the normal form. However, some term cannot be evaluated to the normal form. For example, the following divergent Combinator:
Omega = (λx x x) (λx. x x);
It contains only one redex, and the result of its attribution is still omega itself.
A term that does not have a normal form is called a diverge.
Omega Combinator is generically known as Fixed-point combinator (Call-by-value y-combinator) and can be used to define recursive functions, such as the following factorial functions:
Fix =λf. (Λx f (λy. x x y)) (λx. f (λy. x x y));
Lamda Expressions (Λ-calculus) Learn (1)