Download Functional Programming and Lambda Calculus

Document related concepts
no text concepts found
Transcript
Functional Programming
and Lambda Calculus
CSCI 3136
Principles of Programming Languages
Faculty of Computer Science
Dalhousie University
Winter 2012
Reading: Chapter 10
Review: Programming Models
Imperative programming: Computation is a sequence of side effects.
Functional Programming: Computation by function evalution. There are no
side effects.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Review: Programming Models
Imperative programming: Computation is a sequence of side effects.
Functional Programming: Computation by function evalution. There are no
side effects.
This requires functions to be first-class objects: they can be passed as function
arguments and returned as the results of computations.
Functions that operate on functions and/or return functions are called
higher-order functions.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Review: Programming Models
Imperative programming: Computation is a sequence of side effects.
Functional Programming: Computation by function evalution. There are no
side effects.
This requires functions to be first-class objects: they can be passed as function
arguments and returned as the results of computations.
Functions that operate on functions and/or return functions are called
higher-order functions.
Iteration is impossible in functional languages because iteration inherently
requires side effects. Iteration can be simulated using recursion.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Review: Programming Models
Imperative programming: Computation is a sequence of side effects.
Functional Programming: Computation by function evalution. There are no
side effects.
This requires functions to be first-class objects: they can be passed as function
arguments and returned as the results of computations.
Functions that operate on functions and/or return functions are called
higher-order functions.
Iteration is impossible in functional languages because iteration inherently
requires side effects. Iteration can be simulated using recursion.
Other common features of functional programming languages
•
•
•
•
Strong reliance on list types and strong support for manipulating them
Structured function returns
Structured data constructors
Garbage collection
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Functional Programming and the Real World
Advantages of functional programming
• The lack of side effects makes it easier to reason about program:
– Formal proofs of program correctness become easier.
– Tests always produce the same result.
• Higher level of abstraction → more expressive
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Functional Programming and the Real World
Advantages of functional programming
• The lack of side effects makes it easier to reason about program:
– Formal proofs of program correctness become easier.
– Tests always produce the same result.
• Higher level of abstraction → more expressive
Real-world concerns
• The interaction with the real world (file I/O, user I/O, . . . ) happens
through side effects.
• Most functional programming languages have support for side effects to
accommodate this.
– Scheme: very flexible: supports mixing of different programming styles
– Haskell: Controlled side effects through monads
Lambda Calculus
CSCI 3136: Principles of Programming Languages
λ-Calculus (Alonzo Church 1941)
. . . is the theoretical foundation of functional programming. It expresses
computation entirely using function application.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
λ-Calculus (Alonzo Church 1941)
. . . is the theoretical foundation of functional programming. It expresses
computation entirely using function application.
A context-free grammar for lambda expressions
Expr → name | number | λ name . Expr | Func Arg
Func → name | (λ name . Expr) | Func Arg
Arg → name | number | (λ name . Expr) | (Func Arg)
Numbers are not really part of this, but we introduce them here to make the
exposition easier.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
λ-Calculus (Alonzo Church 1941)
. . . is the theoretical foundation of functional programming. It expresses
computation entirely using function application.
A context-free grammar for lambda expressions
Expr → name | number | λ name . Expr | Func Arg
Func → name | (λ name . Expr) | Func Arg
Arg → name | number | (λ name . Expr) | (Func Arg)
Numbers are not really part of this, but we introduce them here to make the
exposition easier.
Examples
•
•
•
•
•
•
x, times, plus, 7
λx.x
λ y . times
λx.x yz
λ y . times y y
λ x . (times x λ y. y)
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Interpretation
A variable is bound if it is in the scope of a λ and free otherwise.
Examples
• x is bound in λ x . x y but free in the subexpression x y
• x is free in λ y . x y
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Interpretation
A variable is bound if it is in the scope of a λ and free otherwise.
Examples
• x is bound in λ x . x y but free in the subexpression x y
• x is free in λ y . x y
λ x . Expr (function definition)
• is a function with formal parameter x, which becomes a bound variable in
Expr
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Interpretation
A variable is bound if it is in the scope of a λ and free otherwise.
Examples
• x is bound in λ x . x y but free in the subexpression x y
• x is free in λ y . x y
λ x . Expr (function definition)
• is a function with formal parameter x, which becomes a bound variable in
Expr
(λ x . Expr) A ≡ Expr[A \ x] (function application)
• replaces every free occurrence of x in Expr with A
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Interpretation
A variable is bound if it is in the scope of a λ and free otherwise.
Examples
• x is bound in λ x . x y but free in the subexpression x y
• x is free in λ y . x y
λ x . Expr (function definition)
• is a function with formal parameter x, which becomes a bound variable in
Expr
(λ x . Expr) A ≡ Expr[A \ x] (function application)
• replaces every free occurrence of x in Expr with A
(λ x . λ y . Expr) A B ≡ (λ y . Expr[A \ x]) B ≡ Expr[A \ x and B \ y]
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Computing Using λ-Calculus
Computation is done using three transformations:
β-reduction
• (λx . E) M →β E[M \ x]
• Permissible only if no free variable of M becomes bound in E[M \ x]
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Computing Using λ-Calculus
Computation is done using three transformations:
β-reduction
• (λx . E) M →β E[M \ x]
• Permissible only if no free variable of M becomes bound in E[M \ x]
α-conversion
• λ x . E →α λ y . E[ y \ x], where y is a variable not occurring in E
• Needed to make β-reduction possible
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Computing Using λ-Calculus
Computation is done using three transformations:
β-reduction
• (λx . E) M →β E[M \ x]
• Permissible only if no free variable of M becomes bound in E[M \ x]
α-conversion
• λ x . E →α λ y . E[ y \ x], where y is a variable not occurring in E
• Needed to make β-reduction possible
η-reduction
• λ x . F x →η F , where F contains no free occurrences of x
• Eliminates “surplus” lambda abstractions
Lambda Calculus
CSCI 3136: Principles of Programming Languages
The Need for α-Conversion
(λ x . λ y . x y)(λ z . y) c
Lambda Calculus
CSCI 3136: Principles of Programming Languages
The Need for α-Conversion
(λ x . λ y . x y)(λ z . y) c
Substitution without α-conversion
(λ x . λ y . x y)(λ z . y) c →β (λ y . (λ z . y) y) c
→β (λ z . c) c
→β c
Lambda Calculus
CSCI 3136: Principles of Programming Languages
The Need for α-Conversion
(λ x . λ y . x y)(λ z . y) c
Substitution without α-conversion
(λ x . λ y . x y)(λ z . y) c →β (λ y . (λ z . y) y) c
→β (λ z . c) c
→β c
Substitution with α-conversion
(λ x . λ y . x y)(λ z . y) c →α (λ x . λ w . x w)(λ z . y) c
→β (λ w . (λ z . y) w) c
→η (λ z . y) c
→β y
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Associativity in λ-Expressions
Function definition is right-associative:
λ x . λ y . λ z . E ≡ λ x . (λ y . (λ z . E)))
Function application is left-associative:
A B C D ≡ ((A B) C) D
λ extends as far to the right as possible (to the end of the expression or to the
next parenthesis):
λ x . A B C D ≡ λ x . (A B C D)
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Labels
Labels can be created as shortcuts for long expressions. This is kind of like a
global function definition in a programming language.
Examples
square ≡ λ x . times x x
id ≡ λ x . x
const ≡ λ x . λ y . x
hypot ≡ λ x . λ y . sqrt (plus (square x) (square y))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (1)
true ≡ λ x . λ y . x
false ≡ λ x . λ y . y
not ≡ λ t . t false true
if ≡ λ c . λ t . λ e . c t e
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (1)
true ≡ λ x . λ y . x
false ≡ λ x . λ y . y
not ≡ λ t . t false true
if ≡ λ c . λ t . λ e . c t e
Example 1
not true ≡ (λ t . t (λx . λ y . y) (λ x . λ y . x)) (λ x . λ y . x)
→β (λ x . λ y . x) (λx . λ y . y) (λ x . λ y . x)
→β (λ y . (λ x . λ y . y)) (λ x . λ y . x)
→β (λ x . λ y . y)
≡ false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (1)
true ≡ λ x . λ y . x
false ≡ λ x . λ y . y
not ≡ λ t . t false true
if ≡ λ c . λ t . λ e . c t e
Example 1
not true ≡ (λ t . t (λx . λ y . y) (λ x . λ y . x)) (λ x . λ y . x)
→β (λ x . λ y . x) (λx . λ y . y) (λ x . λ y . x)
→β (λ y . (λ x . λ y . y)) (λ x . λ y . x)
→β (λ x . λ y . y)
≡ false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (1)
true ≡ λ x . λ y . x
false ≡ λ x . λ y . y
not ≡ λ t . t false true
if ≡ λ c . λ t . λ e . c t e
Example 1
not true ≡ (λ t . t (λx . λ y . y) (λ x . λ y . x)) (λ x . λ y . x)
→β (λ x . λ y . x) (λx . λ y . y) (λ x . λ y . x)
→β (λ y . (λ x . λ y . y)) (λ x . λ y . x)
→β (λ x . λ y . y)
≡ false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (1)
true ≡ λ x . λ y . x
false ≡ λ x . λ y . y
not ≡ λ t . t false true
if ≡ λ c . λ t . λ e . c t e
Example 1
not true ≡ (λ t . t (λx . λ y . y) (λ x . λ y . x)) (λ x . λ y . x)
→β (λ x . λ y . x) (λx . λ y . y) (λ x . λ y . x)
→β (λ y . (λ x . λ y . y)) (λ x . λ y . x)
→β (λ x . λ y . y)
≡ false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (1)
true ≡ λ x . λ y . x
false ≡ λ x . λ y . y
not ≡ λ t . t false true
if ≡ λ c . λ t . λ e . c t e
Example 1
not true ≡ (λ t . t (λx . λ y . y) (λ x . λ y . x)) (λ x . λ y . x)
→β (λ x . λ y . x) (λx . λ y . y) (λ x . λ y . x)
→β (λ y . (λ x . λ y . y)) (λ x . λ y . x)
→β (λ x . λ y . y)
≡ false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Logic and Control Flow (2)
Example 2
if true 3 4 ≡ (λ c . λ t . λ e . c t e) (λ x . λ y . x) 3 4
→β (λ t . λ e . (λ x . λ y . x) t e) 3 4
→β (λ e . (λ x . λ y . x) 3 e) 4
→β (λ x . λ y . x) 3 4
→β (λ y . 3) 4
→β 3
What about “and” and “or”?
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Representing Natural Numbers Using Functions
An inductive definition of
numbers
• 0 ≡ const id ≡ λ f . λ x . x
• succ ≡ λ n. λ f . λ x . f (n f x)
• For n > 0, n ≡ succ (n − 1)
–
–
–
–
1 ≡ λ f .λ x . f x
2 ≡ λ f . λ x . f ( f x)
3 ≡ λ f . λ x . f ( f ( f x))
...
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Representing Natural Numbers Using Functions
An inductive definition of
numbers
• 0 ≡ const id ≡ λ f . λ x . x
• succ ≡ λ n. λ f . λ x . f (n f x)
• For n > 0, n ≡ succ (n − 1)
–
–
–
–
1 ≡ λ f .λ x . f x
2 ≡ λ f . λ x . f ( f x)
3 ≡ λ f . λ x . f ( f ( f x))
...
Arithmetic operations
• Addition: + ≡
λ a . λ b . λ f . λ x . a f (b f x)
• Multiplication:
∗ ≡ λ a . λ b . λ f . a (b f )
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Representing Natural Numbers Using Functions
An inductive definition of
numbers
• 0 ≡ const id ≡ λ f . λ x . x
• succ ≡ λ n. λ f . λ x . f (n f x)
• For n > 0, n ≡ succ (n − 1)
–
–
–
–
1 ≡ λ f .λ x . f x
2 ≡ λ f . λ x . f ( f x)
3 ≡ λ f . λ x . f ( f ( f x))
...
Arithmetic operations
• Addition: + ≡
λ a . λ b . λ f . λ x . a f (b f x)
• Multiplication:
∗ ≡ λ a . λ b . λ f . a (b f )
Example in Haskell
type Number a = (a -> a) -> a -> a
zero :: Number a
zero = \f -> \x -> x
succ :: Number a -> Number a
succ = \n -> \f -> \x -> f (n f x)
plus, times :: Number a -> Number a -> Number a
plus = \a -> \b -> \f -> \x -> a f (b f x)
times = \a -> \b -> \f -> \x -> a (b f) x
one, two,
one
=
two
=
three
=
five
=
fifteen =
three, five, fifteen :: Number a
succ zero
succ one
succ two
plus two three
times five three
> three (+1) 0
3
> five (+1) 0
5
> fifteen (+1) 0
15
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (1)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (1)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 1
null? nil ≡ (λ l . l (λ x . λ y . false)) (λ x . true)
→β (λ x . true) (λ x . λ y . false)
→β true
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (2)
cons ≡ λ h . λ t . λ x . x h t
(prepend single element h to list t)
car ≡ λ l . l (λ x . λ y . x)
(return head of list l)
cdr ≡ λ l . l (λ x . λ y . y)
(return tail of list l)
nil ≡ λ x . true
(empty list)
null? ≡ λ l . l (λ x . λ y . false)
(is list l empty?)
Example 2
null? (cons 1 nil) ≡ (λ l . l (λ x . λ y . false)) ((λ h . λ t . λ x . x h t) 1 (λ x . true))
→β (λ h . λ t . λ x . x h t) 1 (λ x . true) (λ x . λ y . false)
→β (λ t . λ x . x 1 t) (λ x . true) (λ x . λ y . false)
→β (λ x . x 1 (λ x . true)) (λ x . λ y . false)
→β (λ x . λ y . false) 1 (λ x . true)
→β (λ y . false) (λ x . true)
→β false
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Building Data Structures: Lists (3)
Example 3
car (cdr (cons 1 (cons 2 (cons 3 nil))))
≡ (λ l . l (λ x . λ y . x)) (cdr (cons 1 (cons 2 (cons 3 nil))))
→β cdr (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
≡ (λ l . l (λ x . λ y . y)) (cons 1 (cons 2 (cons 3 nil))) (λ x . λ y . x)
→β cons 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 1 (cons 2 (cons 3 nil)) (λ x . λ y . y) (λ x . λ y . x)
→∗β (λ x . λ y . y) 1 (cons 2 (cons 3 nil)) (λ x . λ y . x)
→∗β (cons 2 (cons 3 nil)) (λ x . λ y . x)
≡ (λ h . λ t . λ x . x h t) 2 (cons 3 nil) (λ x . λ y . x)
→∗β (λ x . λ y . x) 2 (cons 3 nil)
→∗β 2
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying
(Haskell Curry, logician)
Currying: No need for multi-argument functions. Instead, represent such
functions as functions returning functions returning functions . . .
Currying makes “partial function application” possible:
• times ≡ λ x . λ y . x ∗ y (a function with two arguments)
• Full application: times 2 3 ≡ 6 (a number)
• Partial application: times 2 ≡ λ y . 2 ∗ y (a function doubling its argument)
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (1)
Example: Depending on a condition cond, multiply every element in a
sequence by 10 or add 2.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (1)
Example: Depending on a condition cond, multiply every element in a
sequence by 10 or add 2.
C
Attempt 1
if( cond ) {
for( i = 0; i < n; i++ ) A[i] *= 10;
} else {
for( i = 0; i < n; i++ ) A[i] += 2;
}
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (1)
Example: Depending on a condition cond, multiply every element in a
sequence by 10 or add 2.
C
Attempt 1
if( cond ) {
for( i = 0; i < n; i++ ) A[i] *= 10;
} else {
for( i = 0; i < n; i++ ) A[i] += 2;
}
Code
duplication
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (1)
Example: Depending on a condition cond, multiply every element in a
sequence by 10 or add 2.
C
Attempt 1
if( cond ) {
for( i = 0; i < n; i++ ) A[i] *= 10;
} else {
for( i = 0; i < n; i++ ) A[i] += 2;
}
Code
duplication
Attempt 2
for( i = 0; i < n; i++ )
if( cond )
A[i] *= 10;
else
A[i] += 2;
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (1)
Example: Depending on a condition cond, multiply every element in a
sequence by 10 or add 2.
C
Attempt 1
if( cond ) {
for( i = 0; i < n; i++ ) A[i] *= 10;
} else {
for( i = 0; i < n; i++ ) A[i] += 2;
}
Code
duplication
Attempt 2
for( i = 0; i < n; i++ )
if( cond )
A[i] *= 10;
else
A[i] += 2;
Inefficient if cond
is expensive to evaluate
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (2)
Attempt 3
int plus2( int x ) { return 2+x; }
int times10( int x ) { return 10*x; }
void map( int *A, int n, int *f( int ) ) {
for( i = 0; i < n; i++ ) A[ i ] = f( A[ i ] );
}
if( cond )
map( A, n, times10 );
else
map( A, n, plus2 );
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (2)
Attempt 3
int plus2( int x ) { return 2+x; }
int times10( int x ) { return 10*x; }
void map( int *A, int n, int *f( int ) ) {
for( i = 0; i < n; i++ ) A[ i ] = f( A[ i ] );
}
if( cond )
map( A, n, times10 );
else
map( A, n, plus2 );
Way too much
boilerplate
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (3)
Scheme (Anonymous functions and higher-order functions for list processing,
but no currying)
(if cond
(map (lambda (x) (* 10 x)) A)
(map (lambda (x) (+ 2 x)) A))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Currying Is Cool (3)
Scheme (Anonymous functions and higher-order functions for list processing,
but no currying)
(if cond
(map (lambda (x) (* 10 x)) A)
(map (lambda (x) (+ 2 x)) A))
Haskell (Anonymous functions + currying)
if cond then map (* 10) A
else map (+ 2) A
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (1)
Example: Computing factorials
Attempt 1 (Does not work)
fac ≡ λ n . if (= n 0) 1 (∗ n (fac (− n 1)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (1)
Example: Computing factorials
Attempt 1 (Does not work)
fac ≡ λ n . if (= n 0) 1 (∗ n (fac (− n 1)))
The reason this is not correct is because “fac” is just a shorthand for the
λ-expression on its right-hand side. In order to obtain a valid λ-expression, all
such shorthands need to be replaced with the λ-expressions they represent.
This leads to an infinite process here.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (1)
Example: Computing factorials
Attempt 1 (Does not work)
fac ≡ λ n . if (= n 0) 1 (∗ n (fac (− n 1)))
The reason this is not correct is because “fac” is just a shorthand for the
λ-expression on its right-hand side. In order to obtain a valid λ-expression, all
such shorthands need to be replaced with the λ-expressions they represent.
This leads to an infinite process here.
Towards a correct definition
fac ≡ (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))) fac
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (1)
Example: Computing factorials
Attempt 1 (Does not work)
fac ≡ λ n . if (= n 0) 1 (∗ n (fac (− n 1)))
The reason this is not correct is because “fac” is just a shorthand for the
λ-expression on its right-hand side. In order to obtain a valid λ-expression, all
such shorthands need to be replaced with the λ-expressions they represent.
This leads to an infinite process here.
Towards a correct definition
fac ≡ (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))) fac
In other words,
fac ≡ F fac,
where
F ≡ (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (1)
Example: Computing factorials
Attempt 1 (Does not work)
fac ≡ λ n . if (= n 0) 1 (∗ n (fac (− n 1)))
The reason this is not correct is because “fac” is just a shorthand for the
λ-expression on its right-hand side. In order to obtain a valid λ-expression, all
such shorthands need to be replaced with the λ-expressions they represent.
This leads to an infinite process here.
Towards a correct definition
fac ≡ (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))) fac
In other words,
fac ≡ F fac,
where
F ≡ (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))
“fac” is a fixed point of F .
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (2)
The Y-combinator (fixed-point operator)
Y ≡ λ h . (λ x . h (x x)) (λ x . h (x x))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (2)
The Y-combinator (fixed-point operator)
Y ≡ λ h . (λ x . h (x x)) (λ x . h (x x))
Whenever we would normally write a recursive function
f ≡ λ x . ... f ...,
we now write
F ≡ Y (λ f . λ x . . . . f . . . )
and then evaluate F x instead of f x.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (2)
The Y-combinator (fixed-point operator)
Y ≡ λ h . (λ x . h (x x)) (λ x . h (x x))
Whenever we would normally write a recursive function
f ≡ λ x . ... f ...,
we now write
F ≡ Y (λ f . λ x . . . . f . . . )
and then evaluate F x instead of f x.
Back to factorials:
• Incorrect: fac ≡ λ n . if (= n 0) 1 (∗ n (fac (− n 1)))
• Correct: Fac ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (3)
Fac 3 ≡ Y (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
≡ (λ h . (λ x . h (x x)) (λ x . h (x x)))
(λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) 3
→β (λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x)) 3
→β (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1))))
((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 3
→∗β if (= 3 0) 1
(∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 3 1)))
→∗β ∗ 3 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 2)
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (4)
...
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
→∗β ∗ 3 (∗ 2 (∗ 1 (if (= 0 0) 1
(∗ 0 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 0 1))))))
→∗β ∗ 3 (∗ 2 (∗ 1 1))
→∗β 6
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (4)
...
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
→∗β ∗ 3 (∗ 2 (∗ 1 (if (= 0 0) 1
(∗ 0 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 0 1))))))
→∗β ∗ 3 (∗ 2 (∗ 1 1))
→∗β 6
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (4)
...
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
→∗β ∗ 3 (∗ 2 (∗ 1 (if (= 0 0) 1
(∗ 0 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 0 1))))))
→∗β ∗ 3 (∗ 2 (∗ 1 1))
→∗β 6
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Recursion in λ-Calculus (4)
...
→∗β ∗ 3 (∗ 2 (∗ 1 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) 0)))
→∗β ∗ 3 (∗ 2 (∗ 1 (if (= 0 0) 1
(∗ 0 (((λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))
(λ x . (λ f . λ n . if (= n 0) 1 (∗ n ( f (− n 1)))) (x x))) (− 0 1))))))
→∗β ∗ 3 (∗ 2 (∗ 1 1))
→∗β 6
Lambda Calculus
CSCI 3136: Principles of Programming Languages
(Non-)Termination of Reductions
Some reductions never terminate:
(λ x . x x) (λ x . x x) →β (λ x . x x) (λ x . x x)
Lambda Calculus
CSCI 3136: Principles of Programming Languages
(Non-)Termination of Reductions
Some reductions never terminate:
(λ x . x x) (λ x . x x) →β (λ x . x x) (λ x . x x)
Others may produce longer and longer expressions:
(λ x . x x x) (λ x . x x x) →β (λ x . x x x) (λ x . x x x) (λ x . x x x)
→β (λ x . x x x) (λ x . x x x) (λ x . x x x) (λ x . x x x)
→β . . .
Lambda Calculus
CSCI 3136: Principles of Programming Languages
(Non-)Termination of Reductions
Some reductions never terminate:
(λ x . x x) (λ x . x x) →β (λ x . x x) (λ x . x x)
Others may produce longer and longer expressions:
(λ x . x x x) (λ x . x x x) →β (λ x . x x x) (λ x . x x x) (λ x . x x x)
→β (λ x . x x x) (λ x . x x x) (λ x . x x x) (λ x . x x x)
→β . . .
This is the same as infinite loops or infinite recursion in incorrect programs.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Normal-Order vs Applicative-Order Reduction (1)
Normal-order reduction: Subtitute the argument into the function
unevaluated and continue reducing the resulting expression.
(λ x . h)((λ x . x x) (λ x . x x)) →β h
Applicative-order reduction: Reduce both the function and the argument
expressions to the simplest possible form before substituting the argument into
the function. Then continue to reduce the resulting expression.
(λ x . h)((λ x . x x) (λ x . x x)) →∗β . . . (infinite expansion)
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Normal-Order vs Applicative-Order Reduction (2)
Why does this matter?
Normal-order reduction allows us to construct infinite objects and use them, as
long as we only ever use a finite part of them.
Example: Fibonacci numbers in Haskell
fibonacci = 1:1:(zipWith (+) fibonacci (tail fibonacci))
> take 10 fibonacci
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Church-Rosser Theorem
If two reductions of the same λ-expression terminate, they terminate with the
same result.
If some reduction of a λ-expression terminates, the normal-order reduction
terminates.
Lambda Calculus
CSCI 3136: Principles of Programming Languages
Related documents