The -Calculus

Here is a complete conversion of all the slides from your document into Markdown format.
CMPSC 461 Programming Language Concepts
-Calculus
Professor: Suman Saha
Penn State College of Engineering
ELECTRICAL ENGINEERING AND COMPUTER SCIENCE
The -Calculus
Alonzo Church, 1930s
A pure
- Any variable
is a -term - If
is a -term, so is (abstraction) - If
, are -terms, so is (application)
Analogy in C:
- Abstraction:
int f (int x) {return x+1}(lambda (x) (+ x 1))
- Application:
f(2)
The -Calculus Syntax
The -Calculus to Programming Language
This slide illustrates the relationship between foundational computation models and modern programming languages.
-Calculus (abstract symbol rewriting, functional computation) and Turing Machine (hypothetical device, state-based computation) are foundational. - The Turing Machine model leads to real computers (Control Unit, ALU, Memory, etc.), which run machine code and assembly languages.
- This path evolves into higher-level machine-centric languages and then higher-level abstract stateful languages (like C, Java, Python).
- The
-Calculus model directly influences purely functional programming languages (like OCaml, Haskell).
Anonymous Functions
- Functions have the form of
- Functions are anonymous
(lambda (x) (+ x 1))
In C, we write:
int id (int x) {return x}
In
-Term Example
How to parse a
-
-binding extends to the rightmost part - is parsed as
-
Applications are left-associative
- is parsed as
-Term Example
How to parse a
-
-binding extends to the rightmost part is parsed as
-
Applications are left-associative
is parsed as
Questions:
? - Parses to:
- Parses to:
? - Parses to:
- Parses to:
-Term Example (Parsing)
This diagram shows the parse tree for
graph TD
subgraph (λx.(x ((λy.y) z)))
Abs1(λx.t)
Abs1 --> App1(t₁ t₂)
App1 -- "t₁" --> Var1(x)
App1 -- "t₂" --> App2(t₁ t₂)
App2 -- "t₁" --> Abs2(λy.t)
Abs2 -- "t" --> Var2(y)
App2 -- "t₂" --> Var3(z)
endNumber of Parameters
- In the pure
-calculus, only binds one parameter - For convenience, we write
as a shorthand for
- This process of removing parameters is called currying
The -Calculus
A pure
graph TD
subgraph t (λ-term)
direction TB
t --> Var(x)
t --> Abs(λx.t)
Abs --> t_body(t)
t --> App(t₁ t₂)
App -- "t₁" --> t1(t)
App -- "t₂" --> t2(t)
end- Any variable
is a -term - If
is a -term, so is (abstraction) - If
are -terms, so is (application)
We use
The definition above defines an infinite set, named
Syntax vs. Semantics
- Syntax: the structure/form of lambda terms
- Semantics: the meaning of lambda terms
- Lambda calculus defines computation
- What computation is defined by
?
Capture-Avoiding Substitution
- In
-calculus, computation is defined by capture-avoiding substitution means substitute all free in with
Example:
- The first
is bound - The second
is free
- The first
Analogy in C:
int f (int x) {return x} // 'x' is bound
print(x) // 'x' is free
Capture-Avoiding Substitution (Example)
: - The
in the body is bound, not free. - The
in the body is free. - The substitution
does not apply here (this seems to be a distractor).
Let's look at a different example from the slide:
-
- Here,
- The
in the body is free. - Result:
- Here,
-
- Here
- The outer
binds the in . - Wait, the example is
. The substitution applies to the whole term. - This is an application, not just a substitution on a term
. The slide is confusing. Let's stick to the definition: - Let
- This becomes
(substituting the free , not the bound ).
- Here
Bound vs. Free Variables
- In
, the variable in is bound to - A variable is free if it is not bound to any
- A variable is bound to the closest
Examples:
- The
after the dot is bound to the . - The
at the very end is free.
- The
- This is a function that takes a parameter, and returns the identity function.
- The inner-most
is bound to the second (inner) .
More Formally
In
A variable is free if it is not bound to any
Systematically, we define free variables as follows:
Let
Free Variables: Examples
-
,
-
,
Free Variables: Example
,
Free Variables: Example
- Final Answer:
-Reduction (Informal)
- Identity function:
- ... is the same as
and etc. - Observation: the name of a parameter is irrelevant in
-calculus
Analogy in C:
int f (int x) {return x}
Is same as
int f (int y) {return y}
Is same as
int f (int z) {return z}
-Reduction (formal)
where
- (Here
, . We substitute for . is not met, but the substitution works.) - Let's re-read the rule:
means replace free in with . - In
, all in are bound. - The rule means: rename the bound variable
to and replace all its occurrences in the body with . rename to (Correct) (Incorrect substitution)
- (Here
- Correct
-reduction of outer : - Correct
-reduction of inner :
- Correct
Subtle point: what if
- Example:
- We want to rename
to : - This is WRONG. The new
is captured by the inner . - We must first rename the inner
: - Now rename
to : (Correct)
-Reduction (Informal)
- Identity function:
- Observation: we can substitute the formal parameter with the true parameter (argument).
Analogy in C:
- Abstraction:
int f (int x) {return x} - Application:
f(y)evaluates toy
-Reduction
The key reduction rule in
(Capture-avoiding substitution)
-Reduction Example
Example 1:
- Rule:
- Substitute all free occurrences of
in with . - Both
's in are free (relative to the body ). - Result:
-Reduction Example
Example 1:
- In this case,
corresponds to , corresponds to . is not in , hence, not bound. - The first rule of substitution applies, which gives
-Reduction Example
Example 2:
- This is an application
- Reduce by substituting
for in : - Let
(the identity function). The expression is . - Reduce
: - The expression becomes
, which is again. - This reduces again to
. - Final Result:
-Reduction Example
Example 3:
- Let
- The expression is
- This is an application
(which is itself) - Reduce by substituting
for in : - This is the exact same expression we started with!
- This is an infinite loop.