Docsity

Introduction to Lambda Calculus: Motivation and Computational Model, Study notes of Computer Science

Toggle sidebar
bg1
1
1Based on slides by Mattox Beckman, as updated by Vikram
Adve, Gul Agha, Elsa Gunter, and Mark Hills
Introduction to Lambda Calculus1
CS421 Programming Languages and Compilers
http://www.cs.uiuc.edu/class/cs421
Dongyun Jin ([email protected])
2
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
☆☆
Outline
• Motivation
•λ-Calculus
– Syntax
– Features and Facts
– Terminology
• Computational Model
– Evaluation Rules
– Order of Evaluation
3
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
☆☆
Motivation
•λ-Calculus
– A Theory of Computation
• Computation
– All sequential programs may be viewed as
functions
from input (initial state and input values)
to output (resulting state and output values)
– “Everything is Really a Function”
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
★☆
4
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
☆☆
Motivation
•λ-Calculus
– A Theory of Computation
– A Mathematical Formalism of Functions and
Functional Computations
• Purpose
– To capture the essence of functions,
function applications, and evaluation
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
★★
5
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
☆☆
λ-Calculus: Syntax of λ-Calculus
• Three kinds of expressions
– Variables: x, y, z, w, …
– Abstraction: λx.
exp
Function Creation, like fun x -> exp
– Application:
exp
1
exp
2
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
★☆☆
Motivation
★★
6
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
☆☆☆
Motivation
☆☆
λ-Calculus: Syntax of λ-Calculus
• Formal BNF Grammar
<expression> ::= <variable>
| <abstraction>
| <application>
| ( <expression> )
<abstraction> ::= λ<variable> . <expression>
<application> ::= <expression> <expression>
Computational Model
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
λ-Calculus
★☆☆
Motivation
★★
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Lambda Calculus: Motivation and Computational Model and more Study notes Computer Science in PDF only on Docsity!

1 Based on slides by Mattox Beckman, as updated by Vikram

Adve, Gul Agha, Elsa Gunter, and Mark Hills

Introduction to Lambda Calculus^1 CS421 Programming Languages and Compilers http://www.cs.uiuc.edu/class/cs Dongyun Jin ([email protected])

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Outline

  • Motivation
  • λ-Calculus
    • Syntax
    • Features and Facts
    • Terminology
  • Computational Model
    • Evaluation Rules
    • Order of Evaluation

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Motivation

  • λ-Calculus
    • A Theory of Computation
  • Computation
    • All sequential programs may be viewed as functions from input (initial state and input values) to output (resulting state and output values)
    • “Everything is Really a Function” Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ★ ☆

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Motivation

  • λ-Calculus
    • A Theory of Computation
    • A Mathematical Formalism of Functions and Functional Computations
  • Purpose
    • To capture the essence of functions, function applications, and evaluation Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ λ-Calculus: Syntax of λ-Calculus

  • Three kinds of expressions
    • Variables: x, y, z, w, …
    • Abstraction: λ x.exp Function Creation, like fun x -> exp
    • Application:exp 1 exp (^2) Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ☆ ☆ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ λ-Calculus: Syntax of λ-Calculus

  • Formal BNF Grammar ::= | | | ( ) ::= λ. ::= Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ☆ ☆ Motivation ★ ★

λ-Calculus: Features and Facts

  • Computing Power
    • Turing Complete
      • Can express any sequential computation
    • Integers, Booleans?
    • Recursion?
    • Constants?
    • Other statements, i.e. if_then_else_?
    • Syntactic sugar

λ-Calculus: Features and Facts

  • Typed vs. Untyped λ-Calculus
    • λ-Calculus has no notion of type
    • Types restrict what function can be applied to what arguments
    • Types are not just syntactic sugar: some terms are illegal in the typed calculus
    • The simply typed λ-calculus is less powerful than the untyped λ-calculus – it is NOT Turing complete, since it lacks recursion

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ λ-Calculus: Features and Facts

  • Use of λ-Calculus
    • The typed and untyped λ-Calculus enable the theoretical study of sequential programming languages.
    • A functional language is essentially the λ- Calculus extended with predefined constants, syntactic constructs, and types.
    • Ocaml is close to the λ-Calculus:
      • fun x -> exp  λx. exp
      • let x = e 1 in e 2  (λx. e 2 ) e (^1) Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ☆ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ λ-Calculus: Terminology

  • Occurrence
    • A location of a subterm in a term
  • Variable Binding
    • Variable x is said to be bound in λx.e
  • Scope
    • The scope of this binding is all of e except any terms within e that are of the form λ x.e’
  • Bound Occurrence
    • An occurrence of x in e is a bound occurrence if it is within the scope of some binding for x
  • Free Occurrence
    • Otherwise, an occurrence is said to be a free occurrence (aka free variable) Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ λ-Calculus: Terminology

  • Variable Binding
    • Variable x is said to be bound in λx.e (λx.λy.y (λx.x y)) x Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ λ-Calculus: Terminology

  • Scope
    • The scope of this binding is all of e except any terms within e that are of the form λ x.e’ (λx.λy.y (λx.x y)) x Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model: α-Equivalence

  • α-Equivalence is the smallest congruence containing α-Conversion
  • Generally, α-Equivalent terms are considered equal – an individual term is a representative of an equivalence class of terms.

Computational Model: α-Equivalence Example

  • Show: λx.(λy.y x) x ∼α^ λy.(λx.x y) y
    • λx.(λy.y x) x α^ λz.(λy.y z) z so λx.(λy.y x) x ∼α^ λz.(λy.y z) z
    • (λy.y z) α^ (λx.x z) so λz.(λy.y z) z ∼ α^ λz.(λx.x z) z
    • λz.(λx.x z) z α^ λy.(λx.x y) y so λz.(λy.y z) z ∼α^ λy.(λx.x y) y
    • Thus, λx.(λy.y x) x ∼α^ λy.(λx.x y) y

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: η-Reduction

  • η-Reduction is defined as λx.f x η^ f If x is not free in f
  • Not valid in OCaml
  • Not equivalent to (λx.f) x (this is function application)
  • Example: λx.(λy.y) x η^ λy.y Computational Model ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: Substitution

  • Substitution is defined over α-Equivalence classes of terms. P [N/x] means replace every free occurrence of x in P by N - Requirement: No free variable in P should become bound in P [N/x] – hence definition over α-Equivalence classes - Renaming/α-Conversion used to avoid name capture Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: Substitution Rules

  • Substitution defined inductively over the structure of λ terms - x[N/x] = N - y[N/x] = y if y ≠ x - (e 1 e 2 )[N/x] = (e 1 [N/x]) (e 2 [N/x]) - (λx.e)[N/x] = (λx.e) - (λy.e)[N/x] = λy.(e[N/x]) if y ≠ x and y not free in N (rename y if needed to ensure) Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: Substitution Example

  • (λy.y z)[(λx.x y)/z] =?
  • Any problems?
    • z in redex (term being substituted into) in scope of y binding
    • y if free in the residue (term being substituted) (λy.y z)[(λx.x y)/z] α (λw.w z)[(λx.x y)/z] = λw.w (λx.x y) Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model: Substitution Example 2

  • (λy.y z (λz.z))[(λx.x)/z] =?
  • How do we do substitution here?
  • Is this correct? (λy.y (λx.x) (λz.(λx.x)))
  • No, this isn’t, since we only replace free occurrences. λy.y (λx.x) (λz.z)

Computational Model: β-Reduction

  • β-Reduction is the essence of computation in the λ-Calculus β rule: (λx.P) N β^ P[N/x]
  • Usually defined on α-Equivalence classes of terms

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: β-Reduction Example

  • (λz.(λx.x y) z)(λy.y z) β (λx.x y) (λy.y z) β (λy.y z) y β y z
  • (λx.x x)(λx.x x) β (λx.x x)(λx.x x) β (λx.x x)(λx.x x) β …… Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: αβ-Equivalence

  • αβ-Equivalence is the smallest congruence containing α-Equivalence and β-Reduction
  • Definition: A term is in normal form if no subterm is α-Equivalent to a term that can be β-reduced – i.e. there is no way to reduce the term further
  • Hard fact (Church-Rosser): if e 1 and e 2 are αβ-Equivalent and both are normal forms, then they are α-Equivalent Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: Order of Evaluation

  • Order of Evaluation refers to the order of operations we choose to perform when reducing λ terms. For instance, should we substitute a λ term before or after reducing it? With this, there are two important points: - Not all terms reduce to normal forms - Not all reduction strategies will produce a normal form if one exists Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★

Computational Model ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ λ-Calculus ☆ ☆ ☆ Motivation ☆ ☆ Computational Model: Lazy Evaluation

  • Always reduce the left-most, top-most application
  • Stop when left-most term is not an application of an abstraction to a term
  • Example: (λz. (λx. x)) ((λy. y y) (λy. y y)) β^ (λx. x) Computational Model ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ☆ ☆ λ-Calculus ★ ★ ★ Motivation ★ ★