Download 02157 Functional Programming - A brief introduction to Lambda

Document related concepts

Currying wikipedia , lookup

Standard ML wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Anonymous function wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Combinatory logic wikipedia , lookup

Transcript
02157
Functional
Programming
02157 Functional Programming
Michael R. Ha
A brief introduction to Lambda calculus
Lambda
calculus
Background
Syntax
reductions
Michael R. Hansen
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
1
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Purpose
02157
Functional
Programming
Michael R. Ha
The theoretical underpinning of functional languages is λ-calculus.
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
2
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Purpose
02157
Functional
Programming
Michael R. Ha
The theoretical underpinning of functional languages is λ-calculus.
Lambda
calculus
Background
The purpose is to hint on this underpinning and to introduce
concepts of functional languages.
• Informal introduction to λ-calculus
• computations of lambda-calculus and functional languages
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
3
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Purpose
02157
Functional
Programming
Michael R. Ha
The theoretical underpinning of functional languages is λ-calculus.
Lambda
calculus
Background
The purpose is to hint on this underpinning and to introduce
concepts of functional languages.
• Informal introduction to λ-calculus
• computations of lambda-calculus and functional languages
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Today you will be introduced to basic concepts of λ-calculus and you
will get a feeling for the power of these concepts by the construction
of an interpreter for a λ-calculus based language (optional
mini-project).
4
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: background
02157
Functional
Programming
Michael R. Ha
• Invented in the 1930’s by the logician Alonzo Church in logical
studies and in investigations of function definition and
application, and recursion.
• Comprise full computability.
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
• First uncomputability results were discovered using λ-calculus.
Church
numerals
Fixpoint
combinators
5
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: background
02157
Functional
Programming
Michael R. Ha
• Invented in the 1930’s by the logician Alonzo Church in logical
studies and in investigations of function definition and
application, and recursion.
• Comprise full computability.
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
• First uncomputability results were discovered using λ-calculus.
Church
numerals
Fixpoint
combinators
Some questions
• Does the mathematical expression x − y denote a function, say
f , of x or a function, say g, of y , or . . .?
• Does the notation h(z) mean a function h or h applied to z?
6
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: informal ideas
02157
Functional
Programming
• λx.e denotes the anonymous function of x given by e.
Examples of function definitions:
Michael R. Ha
Lambda
calculus
Background
• Let f be λx.x − y
Syntax
The expression x − y considered as a function f : Z → Z of x
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
7
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: informal ideas
02157
Functional
Programming
• λx.e denotes the anonymous function of x given by e.
Examples of function definitions:
Michael R. Ha
Lambda
calculus
Background
• Let f be λx.x − y
Syntax
The expression x − y considered as a function f : Z → Z of x
• g = λy .x − y
The expression x − y considered as a function g : Z → Z of y
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
8
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: informal ideas
02157
Functional
Programming
• λx.e denotes the anonymous function of x given by e.
Examples of function definitions:
Michael R. Ha
Lambda
calculus
Background
• Let f be λx.x − y
Syntax
The expression x − y considered as a function f : Z → Z of x
• g = λy .x − y
The expression x − y considered as a function g : Z → Z of y
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
• h = λx.λy.x − y
The expression x − y considered as a higher-order function
h : Z → (Z → Z)
9
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: informal ideas
02157
Functional
Programming
• λx.e denotes the anonymous function of x given by e.
Examples of function definitions:
Michael R. Ha
Lambda
calculus
Background
• Let f be λx.x − y
Syntax
The expression x − y considered as a function f : Z → Z of x
• g = λy .x − y
The expression x − y considered as a function g : Z → Z of y
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
• h = λx.λy.x − y
The expression x − y considered as a higher-order function
h : Z → (Z → Z)
Examples of function applications:
• f (1) = 1 − y and g(1) = x − 1
10
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: informal ideas
02157
Functional
Programming
• λx.e denotes the anonymous function of x given by e.
Examples of function definitions:
Michael R. Ha
Lambda
calculus
Background
• Let f be λx.x − y
Syntax
The expression x − y considered as a function f : Z → Z of x
• g = λy .x − y
The expression x − y considered as a function g : Z → Z of y
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
• h = λx.λy.x − y
The expression x − y considered as a higher-order function
h : Z → (Z → Z)
Examples of function applications:
• f (1) = 1 − y and g(1) = x − 1
• h(2) = λy .2 − y
11
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: informal ideas
02157
Functional
Programming
• λx.e denotes the anonymous function of x given by e.
Examples of function definitions:
Michael R. Ha
Lambda
calculus
Background
• Let f be λx.x − y
Syntax
The expression x − y considered as a function f : Z → Z of x
• g = λy .x − y
The expression x − y considered as a function g : Z → Z of y
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
• h = λx.λy.x − y
The expression x − y considered as a higher-order function
h : Z → (Z → Z)
Examples of function applications:
• f (1) = 1 − y and g(1) = x − 1
• h(2) = λy .2 − y and h(2)(5) = 2 − 5 = −3
12
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: syntax
02157
Functional
Programming
The set of λ-terms or just terms Λ is generated from a set V of
variables by the rules:
• if x ∈ V , then x ∈ Λ
Michael R. Ha
atom
• if x ∈ V and t ∈ Λ, then (λx.t) ∈ Λ
abstraction
• if t1 , t2 ∈ Λ, then (t1 t2 ) ∈ Λ
application
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
13
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: syntax
02157
Functional
Programming
The set of λ-terms or just terms Λ is generated from a set V of
variables by the rules:
• if x ∈ V , then x ∈ Λ
Michael R. Ha
atom
• if x ∈ V and t ∈ Λ, then (λx.t) ∈ Λ
abstraction
• if t1 , t2 ∈ Λ, then (t1 t2 ) ∈ Λ
application
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Notational conventions to avoid parenthesis:
• Applications associated to the left, i.e. t1 t2 t3 means ((t1 t2 )t3 )
14
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Fixpoint
combinators
MRH 24/11/2016
Lambda calculus: syntax
02157
Functional
Programming
The set of λ-terms or just terms Λ is generated from a set V of
variables by the rules:
• if x ∈ V , then x ∈ Λ
Michael R. Ha
atom
• if x ∈ V and t ∈ Λ, then (λx.t) ∈ Λ
abstraction
• if t1 , t2 ∈ Λ, then (t1 t2 ) ∈ Λ
application
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Notational conventions to avoid parenthesis:
• Applications associated to the left, i.e. t1 t2 t3 means ((t1 t2 )t3 )
Fixpoint
combinators
• Abstraction extends as far as possible to the right,
i.e. λx.t1 t2 means (λx.(t1 t2 ))
15
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: syntax
02157
Functional
Programming
The set of λ-terms or just terms Λ is generated from a set V of
variables by the rules:
• if x ∈ V , then x ∈ Λ
Michael R. Ha
atom
• if x ∈ V and t ∈ Λ, then (λx.t) ∈ Λ
abstraction
• if t1 , t2 ∈ Λ, then (t1 t2 ) ∈ Λ
application
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Notational conventions to avoid parenthesis:
• Applications associated to the left, i.e. t1 t2 t3 means ((t1 t2 )t3 )
Fixpoint
combinators
• Abstraction extends as far as possible to the right,
i.e. λx.t1 t2 means (λx.(t1 t2 ))
• λx1 x2 · · · xn .t means (λx1 .(λx2 .(· · · (λxn .t) · · · )))
16
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: syntax
02157
Functional
Programming
The set of λ-terms or just terms Λ is generated from a set V of
variables by the rules:
• if x ∈ V , then x ∈ Λ
Michael R. Ha
atom
• if x ∈ V and t ∈ Λ, then (λx.t) ∈ Λ
abstraction
• if t1 , t2 ∈ Λ, then (t1 t2 ) ∈ Λ
application
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Notational conventions to avoid parenthesis:
• Applications associated to the left, i.e. t1 t2 t3 means ((t1 t2 )t3 )
Fixpoint
combinators
• Abstraction extends as far as possible to the right,
i.e. λx.t1 t2 means (λx.(t1 t2 ))
• λx1 x2 · · · xn .t means (λx1 .(λx2 .(· · · (λxn .t) · · · )))
Terms could be enriched with constants and constructs for pairs, e.g.
λ(x, y).x − y
17
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda calculus: syntax
02157
Functional
Programming
The set of λ-terms or just terms Λ is generated from a set V of
variables by the rules:
• if x ∈ V , then x ∈ Λ
Michael R. Ha
atom
• if x ∈ V and t ∈ Λ, then (λx.t) ∈ Λ
abstraction
• if t1 , t2 ∈ Λ, then (t1 t2 ) ∈ Λ
application
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Notational conventions to avoid parenthesis:
• Applications associated to the left, i.e. t1 t2 t3 means ((t1 t2 )t3 )
Fixpoint
combinators
• Abstraction extends as far as possible to the right,
i.e. λx.t1 t2 means (λx.(t1 t2 ))
• λx1 x2 · · · xn .t means (λx1 .(λx2 .(· · · (λxn .t) · · · )))
Terms could be enriched with constants and constructs for pairs, e.g.
λ(x, y).x − y where − is a constant.
18
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Free and bound variables
A occurrence of a variable x is bound in t, if it occurs within the
scope of an abstraction λx.M in t; otherwise it is free.
If x has at least one free occurrence in t, then it is called a free
variable of t.
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
19
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Free and bound variables
A occurrence of a variable x is bound in t, if it occurs within the
scope of an abstraction λx.M in t; otherwise it is free.
If x has at least one free occurrence in t, then it is called a free
variable of t.
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
Examples:
reductions
• x (λy .y x) v .
Both occurrences of y are bound. Both x’s are free. v is free.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
20
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Free and bound variables
A occurrence of a variable x is bound in t, if it occurs within the
scope of an abstraction λx.M in t; otherwise it is free.
If x has at least one free occurrence in t, then it is called a free
variable of t.
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
Examples:
reductions
• x (λy .y x) v .
Both occurrences of y are bound. Both x’s are free. v is free.
• (λx.y x)(x z). Two left-hand occurrences of x are bound, the
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
right-hand occurrence of x is free. x, y , z are free.
21
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Free and bound variables
A occurrence of a variable x is bound in t, if it occurs within the
scope of an abstraction λx.M in t; otherwise it is free.
If x has at least one free occurrence in t, then it is called a free
variable of t.
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
Examples:
reductions
• x (λy .y x) v .
Both occurrences of y are bound. Both x’s are free. v is free.
• (λx.y x)(x z). Two left-hand occurrences of x are bound, the
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
right-hand occurrence of x is free. x, y , z are free.
These concepts are also know from mathematics, logic and
programming languages. For example:
2
• Σ10
x=1 x
• ∀x.∃y .x + y > 0 ⇒ z + x > y
• int f(int x,int y) = { return x + y - z; }
where the occurrences of x and y are bound and those of z are free.
22
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Substitution
Let t[e/x] denote the term obtained from t by substituting e for every
free occurrence of x.
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Examples:
Background
Syntax
• (u λx.y x)[v w/y ] = u λx.v w x.
reductions
Lambda
terms as
programs
• (y λx.x y )[v w/y] = v wλx.x (v w).
Church
numerals
Fixpoint
combinators
23
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Substitution
Let t[e/x] denote the term obtained from t by substituting e for every
free occurrence of x.
In doing so, we rename bound variables to avoid clashes.
Examples:
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
• (u λx.y x)[v w/y ] = u λx.v w x.
reductions
Lambda
terms as
programs
• (y λx.x y )[v w/y] = v wλx.x (v w).
Church
numerals
• (λx.y )[x/y ] = (λz.y)[x/y ] = λz.x.
Fixpoint
combinators
Rename x to avoid clashes.
24
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Substitution
Let t[e/x] denote the term obtained from t by substituting e for every
free occurrence of x.
In doing so, we rename bound variables to avoid clashes.
Examples:
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
• (u λx.y x)[v w/y ] = u λx.v w x.
reductions
Lambda
terms as
programs
• (y λx.x y )[v w/y] = v wλx.x (v w).
Church
numerals
• (λx.y )[x/y ] = (λz.y)[x/y ] = λz.x.
Fixpoint
combinators
Rename x to avoid clashes.
Comment:
• (λx.y ) denote the constant function whose value is y .
25
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Substitution
Let t[e/x] denote the term obtained from t by substituting e for every
free occurrence of x.
In doing so, we rename bound variables to avoid clashes.
Examples:
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
• (u λx.y x)[v w/y ] = u λx.v w x.
reductions
Lambda
terms as
programs
• (y λx.x y )[v w/y] = v wλx.x (v w).
Church
numerals
• (λx.y )[x/y ] = (λz.y)[x/y ] = λz.x.
Fixpoint
combinators
Rename x to avoid clashes.
Comment:
• (λx.y ) denote the constant function whose value is y .
• Therefore, (λx.y )[x/y] should intuitively denote the constant
function with value x (as λz.x also does).
26
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Substitution
Let t[e/x] denote the term obtained from t by substituting e for every
free occurrence of x.
In doing so, we rename bound variables to avoid clashes.
Examples:
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
• (u λx.y x)[v w/y ] = u λx.v w x.
reductions
Lambda
terms as
programs
• (y λx.x y )[v w/y] = v wλx.x (v w).
Church
numerals
• (λx.y )[x/y ] = (λz.y)[x/y ] = λz.x.
Fixpoint
combinators
Rename x to avoid clashes.
Comment:
• (λx.y ) denote the constant function whose value is y .
• Therefore, (λx.y )[x/y] should intuitively denote the constant
function with value x (as λz.x also does).
• The renaming is necessary as λx.x denotes the identity
function.
27
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
α- conversions
02157
Functional
Programming
Renaming bound variables does not change the meaning:
2
10
2
• Σ10
x=1 x is equal to Σk =1 k
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
28
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
α- conversions
02157
Functional
Programming
Renaming bound variables does not change the meaning:
2
10
2
• Σ10
x=1 x is equal to Σk =1 k
• ∀x.∃y .x + y > 0 ⇒ z + x > y is equivalent to
Michael R. Ha
Lambda
calculus
Background
Syntax
∀a.∃b.a + b > 0 ⇒ z + a > b
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
29
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
α- conversions
02157
Functional
Programming
Renaming bound variables does not change the meaning:
2
10
2
• Σ10
x=1 x is equal to Σk =1 k
• ∀x.∃y .x + y > 0 ⇒ z + x > y is equivalent to
Background
reductions
• int f(int x,int y) = { return x + y - z; }
is the same as
int f(int a,int b) = { return a + b - z; }
DTU Compute, Technical University of Denmark
Lambda
calculus
Syntax
∀a.∃b.a + b > 0 ⇒ z + a > b
30
Michael R. Ha
A brief introduction to Lambda calculus
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
α- conversions
02157
Functional
Programming
Renaming bound variables does not change the meaning:
2
10
2
• Σ10
x=1 x is equal to Σk =1 k
• ∀x.∃y .x + y > 0 ⇒ z + x > y is equivalent to
Michael R. Ha
Lambda
calculus
Background
Syntax
∀a.∃b.a + b > 0 ⇒ z + a > b
reductions
• int f(int x,int y) = { return x + y - z; }
is the same as
int f(int a,int b) = { return a + b - z; }
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Renaming a bound variable in a term is called an α-conversion:
• Alpha: λx.t →α λy.t[y /x], when y is not free in t.
31
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
α- conversions
02157
Functional
Programming
Renaming bound variables does not change the meaning:
2
10
2
• Σ10
x=1 x is equal to Σk =1 k
• ∀x.∃y .x + y > 0 ⇒ z + x > y is equivalent to
Michael R. Ha
Lambda
calculus
Background
Syntax
∀a.∃b.a + b > 0 ⇒ z + a > b
reductions
• int f(int x,int y) = { return x + y - z; }
is the same as
int f(int a,int b) = { return a + b - z; }
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Renaming a bound variable in a term is called an α-conversion:
• Alpha: λx.t →α λy.t[y /x], when y is not free in t.
Example: y (λx.xz)y →α y (λv .vz)y
32
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
β-reduction
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
The β-reduction rule formalizes the application of a function to an
argument.
Background
Syntax
reductions
Lambda
terms as
programs
Example: (λx.x + 2)3 reduces to 3+2.
Church
numerals
Fixpoint
combinators
33
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
β-reduction
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
The β-reduction rule formalizes the application of a function to an
argument.
Background
Syntax
reductions
Lambda
terms as
programs
Example: (λx.x + 2)3 reduces to 3+2.
Church
numerals
Fixpoint
combinators
• Beta: (λx.t) e →β t[e/x]
function application
34
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
35
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
• λx.λy .(λx.λy .x x y) y x →β λx.λy .(λv .y y v ) x →β
λx.λy .y y x
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
36
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
• λx.λy .(λx.λy .x x y) y x →β λx.λy .(λv .y y v ) x →β
λx.λy .y y x
Michael R. Ha
Lambda
calculus
• (λx.a) b →β a
Background
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
37
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
• λx.λy .(λx.λy .x x y) y x →β λx.λy .(λv .y y v ) x →β
λx.λy .y y x
Michael R. Ha
Lambda
calculus
• (λx.a) b →β a
Background
• (λx.x) (λy .a) b →β (λy .a) b →β a
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
38
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
• λx.λy .(λx.λy .x x y) y x →β λx.λy .(λv .y y v ) x →β
λx.λy .y y x
Michael R. Ha
Lambda
calculus
• (λx.a) b →β a
Background
• (λx.x) (λy .a) b →β (λy .a) b →β a
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Reductions may give bigger terms. Let Ω = λx.x x x.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Ω Ω ≡ (λx.x x x) Ω →β Ω Ω Ω ≡ (λx.x x x) Ω Ω →β Ω Ω Ω Ω ≡ . . .
39
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
• λx.λy .(λx.λy .x x y) y x →β λx.λy .(λv .y y v ) x →β
λx.λy .y y x
Michael R. Ha
Lambda
calculus
• (λx.a) b →β a
Background
• (λx.x) (λy .a) b →β (λy .a) b →β a
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Reductions may give bigger terms. Let Ω = λx.x x x.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Ω Ω ≡ (λx.x x x) Ω →β Ω Ω Ω ≡ (λx.x x x) Ω Ω →β Ω Ω Ω Ω ≡ . . .
Termination depends on reduction strategy:
• (λx.a) (Ω Ω) →∗β (λx.a) (Ω Ω Ω) →β
40
DTU Compute, Technical University of Denmark
∗
...
applicative-order reduction
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples
02157
Functional
Programming
• (λx.λy .x + y ) a →β λy .a + y
• λx.λy .(λx.λy .x x y) y x →β λx.λy .(λv .y y v ) x →β
λx.λy .y y x
Michael R. Ha
Lambda
calculus
• (λx.a) b →β a
Background
• (λx.x) (λy .a) b →β (λy .a) b →β a
Syntax
reductions
where free variables, e.g. +, a, b are considered as constants.
Reductions may give bigger terms. Let Ω = λx.x x x.
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Ω Ω ≡ (λx.x x x) Ω →β Ω Ω Ω ≡ (λx.x x x) Ω Ω →β Ω Ω Ω Ω ≡ . . .
Termination depends on reduction strategy:
• (λx.a) (Ω Ω) →∗β (λx.a) (Ω Ω Ω) →β
• (λx.a) (Ω Ω) →β a
41
DTU Compute, Technical University of Denmark
∗
...
applicative-order reduction
normal-order reduction
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
42
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
In F# the reduction strategy is called eager (or applicative-order
reduction). An application e1 e2 is evaluated as follows:
• Evaluate e1 to an abstraction λx.e (written e1
λx.e).
43
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
In F# the reduction strategy is called eager (or applicative-order
reduction). An application e1 e2 is evaluated as follows:
• Evaluate e1 to an abstraction λx.e (written e1
λx.e).
• Evaluate e2 to a value v (written e2
v ).
44
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
In F# the reduction strategy is called eager (or applicative-order
reduction). An application e1 e2 is evaluated as follows:
• Evaluate e1 to an abstraction λx.e (written e1
λx.e).
• Evaluate e2 to a value v (written e2
v ).
• Perform the beta-reduction (λx.e) v →β e[v /x].
45
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
In F# the reduction strategy is called eager (or applicative-order
reduction). An application e1 e2 is evaluated as follows:
• Evaluate e1 to an abstraction λx.e (written e1
λx.e).
• Evaluate e2 to a value v (written e2
v ).
• Perform the beta-reduction (λx.e) v →β e[v /x].
Hence, the ”bigstep” evaluation is e1 e2
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
e[v /x]
This eager strategy is efficient when functions need their arguments.
46
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
In F# the reduction strategy is called eager (or applicative-order
reduction). An application e1 e2 is evaluated as follows:
• Evaluate e1 to an abstraction λx.e (written e1
λx.e).
• Evaluate e2 to a value v (written e2
v ).
• Perform the beta-reduction (λx.e) v →β e[v /x].
Hence, the ”bigstep” evaluation is e1 e2
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
e[v /x]
This eager strategy is efficient when functions need their arguments.
In the textbook the notion environment is used instead of substitution:
e1 e2
(e, [x 7→ v ]).
47
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Lambda terms as programs
02157
Functional
Programming
• A program p is a lambda term
• Computations are given by beta-reductions
In a ”real” functional programming language, the syntax is ”sugared”
lambda terms, and computations are based on a specific strategy for
applying beta-reduction.
Michael R. Ha
Lambda
calculus
Background
Syntax
In F# the reduction strategy is called eager (or applicative-order
reduction). An application e1 e2 is evaluated as follows:
• Evaluate e1 to an abstraction λx.e (written e1
λx.e).
• Evaluate e2 to a value v (written e2
v ).
• Perform the beta-reduction (λx.e) v →β e[v /x].
Hence, the ”bigstep” evaluation is e1 e2
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
e[v /x]
This eager strategy is efficient when functions need their arguments.
In the textbook the notion environment is used instead of substitution:
e1 e2
(e, [x 7→ v ]).
F# is a strict language with a functional core
48
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Church numerals. Natural number computations
02157
Functional
Programming
Michael R. Ha
Natural numbers are represented by, for example, Church numerals:
0
λf .λx.x
1
λf .λx.fx
2
λf .λx.f (fx)
3
λf .λx.f (f (fx))
···
...
n
λf .λx.f n x
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
49
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Church numerals. Natural number computations
02157
Functional
Programming
Michael R. Ha
Natural numbers are represented by, for example, Church numerals:
0
λf .λx.x
0
1
λf .λx.fx
where f x = x, f
i+1
2
λf .λx.f (fx)
3
λf .λx.f (f (fx))
···
...
n
λf .λx.f n x
i
x = f (fx)
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
• the main idea is to use a unary representation of numbers.
Fixpoint
combinators
Inefficient – but it works.
Let n denote the Church numeral for the natural number n.
50
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Successor and additions operations
02157
Functional
Programming
• Successor: suc = λn.λf .λx.n f (f x)
Michael R. Ha
Lambda
calculus
Background
Reductions:
suc n =
=
=
51
Syntax
reductions
n
λf .λx.(λf .λx.f x) f (f x)
λf .λx.(λx.f n x) (f x)
λf .λx.f n (f x) = λf .λx.f n+1 x = n + 1
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
Successor and additions operations
02157
Functional
Programming
Michael R. Ha
• Addition: add = λm.λn.λf .λx.m f (n f x)
Lambda
calculus
Background
Reductions:
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
add m n =
=
=
=
52
λf .λx.(λf .λx.f m x) f (n f x)
λf .λx.(λx.f m x) (n f x)
λf .λx.f m ((λf .λx.f n x) f x)
λf .λx.f m (f n x) = λf .λx.f m+n x = m + n
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Successor and additions operations
02157
Functional
Programming
• Successor: suc = λn.λf .λx.n f (f x)
Michael R. Ha
• Addition: add = λm.λn.λf .λx.m f (n f x)
Lambda
calculus
Background
Reductions:
suc n =
=
=
add m n =
=
=
=
53
Syntax
reductions
n
λf .λx.(λf .λx.f x) f (f x)
λf .λx.(λx.f n x) (f x)
λf .λx.f n (f x) = λf .λx.f n+1 x = n + 1
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
λf .λx.(λf .λx.f m x) f (n f x)
λf .λx.(λx.f m x) (n f x)
λf .λx.f m ((λf .λx.f n x) f x)
λf .λx.f m (f n x) = λf .λx.f m+n x = m + n
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Recursion in Lambda Calculus
How to make recursive functions in Lambda calculus?
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
54
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Recursion in Lambda Calculus
How to make recursive functions in Lambda calculus?
02157
Functional
Programming
Michael R. Ha
Answer: use a fixpoint combinator Y.
• An element x ∈ A is a fixpoint of a function f : A → A, if x = f (x)
• A fixpoint combinator is a higher-order function Y that computes
a fixpoint of another function F , i.e. Y F = F (Y F )
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
55
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Recursion in Lambda Calculus
How to make recursive functions in Lambda calculus?
02157
Functional
Programming
Michael R. Ha
Answer: use a fixpoint combinator Y.
• An element x ∈ A is a fixpoint of a function f : A → A, if x = f (x)
• A fixpoint combinator is a higher-order function Y that computes
a fixpoint of another function F , i.e. Y F = F (Y F )
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Example: Let F = λf .λn.if n = 0 then 1 else n ∗ f (n − 1).
56
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Fixpoint
combinators
MRH 24/11/2016
Recursion in Lambda Calculus
How to make recursive functions in Lambda calculus?
02157
Functional
Programming
Michael R. Ha
Answer: use a fixpoint combinator Y.
• An element x ∈ A is a fixpoint of a function f : A → A, if x = f (x)
• A fixpoint combinator is a higher-order function Y that computes
a fixpoint of another function F , i.e. Y F = F (Y F )
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Example: Let F = λf .λn.if n = 0 then 1 else n ∗ f (n − 1).
Fixpoint
combinators
The factorial function n! is a fixpoint for F , as
n! = λn.if n = 0 then 1 else n ∗ (n − 1)!
57
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Recursion in Lambda Calculus
How to make recursive functions in Lambda calculus?
02157
Functional
Programming
Michael R. Ha
Answer: use a fixpoint combinator Y.
• An element x ∈ A is a fixpoint of a function f : A → A, if x = f (x)
• A fixpoint combinator is a higher-order function Y that computes
a fixpoint of another function F , i.e. Y F = F (Y F )
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Example: Let F = λf .λn.if n = 0 then 1 else n ∗ f (n − 1).
Fixpoint
combinators
The factorial function n! is a fixpoint for F , as
n! = λn.if n = 0 then 1 else n ∗ (n − 1)! = F n!
58
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Recursion in Lambda Calculus
How to make recursive functions in Lambda calculus?
02157
Functional
Programming
Michael R. Ha
Answer: use a fixpoint combinator Y.
• An element x ∈ A is a fixpoint of a function f : A → A, if x = f (x)
• A fixpoint combinator is a higher-order function Y that computes
a fixpoint of another function F , i.e. Y F = F (Y F )
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Example: Let F = λf .λn.if n = 0 then 1 else n ∗ f (n − 1).
Fixpoint
combinators
The factorial function n! is a fixpoint for F , as
n! = λn.if n = 0 then 1 else n ∗ (n − 1)! = F n!
Thus the factorial function fact is declared by Y F , and e.g.
fact 2 = Y F 2 =
=
59
(λn.if n = 0 then 1 else n ∗ (Y F (n − 1))) 2
if 2 = 0 then 1 else 2 ∗ (Y F 1) = · · · = 2
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Fixpoint combinators
There are many lambda terms Y satisfying: Y F = F (Y F ).
02157
Functional
Programming
Michael R. Ha
One is due to Haskell Curry:
Lambda
calculus
Yc = λx.(λy.x (y y ))(λy .x (y y ))
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
60
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Fixpoint combinators
There are many lambda terms Y satisfying: Y F = F (Y F ).
02157
Functional
Programming
Michael R. Ha
One is due to Haskell Curry:
Lambda
calculus
Yc = λx.(λy.x (y y ))(λy .x (y y ))
Background
Syntax
reductions
Another is due to Alan Turing:
Lambda
terms as
programs
Yt = (λx.λy.y (x x y)) (λx.λy.y (x x y))
An advantage of Yt over Yc is that Yt F = F (Yt F ) can be established
by reductions only (actually by normal-order reductions – an
exercise), i.e. Yt is preferable for computational use.
61
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
Fixpoint combinators
There are many lambda terms Y satisfying: Y F = F (Y F ).
02157
Functional
Programming
Michael R. Ha
One is due to Haskell Curry:
Lambda
calculus
Yc = λx.(λy.x (y y ))(λy .x (y y ))
Background
Syntax
reductions
Another is due to Alan Turing:
Lambda
terms as
programs
Yt = (λx.λy.y (x x y)) (λx.λy.y (x x y))
An advantage of Yt over Yc is that Yt F = F (Yt F ) can be established
by reductions only (actually by normal-order reductions – an
exercise), i.e. Yt is preferable for computational use.
Church
numerals
Fixpoint
combinators
Notice: These operators cannot be represented by F# expressions.
They contain self-applications (of the form t t) and such expressions
are not well-typed.
62
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Summary
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
• Very brief introduction to lambda calculus.
reductions
• Hint a the theoretical underpinning of functional languages. (F#
is actually more directly related to typed lambda calculus).
• Hint at the general computability capability of lambda calculus.
63
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
Haskell
02157
Functional
Programming
Emerged in the late 80’ties aiming at a common lazy functional
language. An excellent textbook:
• Haskell: The Craft of functional programming (third edition)
Michael R. Ha
Lambda
calculus
Background
Simon Thompson, Addison Wesley, 2011
Syntax
reductions
Lambda
terms as
programs
Example:
Church
numerals
square :: Int -> Int
square x = x*x
64
DTU Compute, Technical University of Denmark
Fixpoint
combinators
A brief introduction to Lambda calculus
MRH 24/11/2016
Haskell
02157
Functional
Programming
Emerged in the late 80’ties aiming at a common lazy functional
language. An excellent textbook:
• Haskell: The Craft of functional programming (third edition)
Michael R. Ha
Lambda
calculus
Background
Simon Thompson, Addison Wesley, 2011
Syntax
reductions
Lambda
terms as
programs
Example:
Church
numerals
square :: Int -> Int
square x = x*x
Fixpoint
combinators
Uses normal-order reduction:
square (2+3)
(2+3) + (2+3)
plus sharing to avoid repeated calculations (of 2+3 in this example)
Normal-order reduction + sharing is called lazy evaluation
65
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples on Lists and trees
Many programs are similar to F# programs:
02157
Functional
Programming
sumList [] = 0
sumList (a:x) = a + sumList x
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
66
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples on Lists and trees
Many programs are similar to F# programs:
02157
Functional
Programming
sumList [] = 0
sumList (a:x) = a + sumList x
Michael R. Ha
data Expr = Lit Int | Add Expr Expr | Sub Expr Expr
Lambda
calculus
Background
eval
eval
eval
eval
:: Expr
(Lit n)
(Add e1
(Sub e1
-> Int
= n
e2) = (eval e1) + (eval e2)
e2) = (eval e1) - (eval e2)
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
67
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Examples on Lists and trees
Many programs are similar to F# programs:
02157
Functional
Programming
sumList [] = 0
sumList (a:x) = a + sumList x
Michael R. Ha
data Expr = Lit Int | Add Expr Expr | Sub Expr Expr
Lambda
calculus
Background
eval
eval
eval
eval
:: Expr
(Lit n)
(Add e1
(Sub e1
-> Int
= n
e2) = (eval e1) + (eval e2)
e2) = (eval e1) - (eval e2)
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
Lazy evaluation by default:
ones = 1:ones
from(n) = n:from(n+1)
Sieve of Eratosthenes (exploits lazy lists)
primes :: [Int]
primes = sieve [2..]
where
sieve (p:xs) = p : sieve [x|x <- xs, x ‘mod‘ p > 0]
68
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Type classes
Supports
• Overloading
• Object-oriented features
Simple examples:
02157
Functional
Programming
Michael R. Ha
S. Thompson
class Eq t where
(==), (/=) :: t -> t -> Bool
x/=y = not (x==y)
instance Eq Bool
True == True
False == False
_
== _
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
where
= True
= True
= False
Fixpoint
combinators
allEqual :: Eq t => t -> t -> t -> Bool
allEqual n m p = (n==m) && (m==p)
class Eq t => Ord t where
(<), (<=), (>), (>=) :: t -> t -> Bool
max, min
:: t -> t -> t
69
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
The Maybe monad
• Hides low-level details of a computation that can go wrong
02157
Functional
Programming
Michael R. Ha
data Maybe t = Just t | Nothing
Lambda
calculus
return :: a -> Maybe a
return x = Just x
Background
Syntax
reductions
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
(>>=) m g = case m of
Nothing -> Nothing
Just x -> g x
70
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
The Maybe monad
• Hides low-level details of a computation that can go wrong
02157
Functional
Programming
Michael R. Ha
data Maybe t = Just t | Nothing
Lambda
calculus
return :: a -> Maybe a
return x = Just x
Background
Syntax
reductions
(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
(>>=) m g = case m of
Nothing -> Nothing
Just x -> g x
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
• Monads in F# are called computation expressions.
• Monads are used in F#’s sequence and async expressions to
hide low-level computation details.
• Computation expressions (of F#) will be discussed in the last
lecture.
71
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
The Maybe monad: A simple example
02157
Functional
Programming
f::Int -> Maybe Int
f 0 = Nothing
f x = Just x
Michael R. Ha
Lambda
calculus
g :: Int -> Maybe Int
g 100 = Nothing
g x
= Just x
Background
Syntax
reductions
Lambda
terms as
programs
h ::Int -> Maybe Int
h x = case f x of
Just n -> g n
Nothing -> Nothing
72
DTU Compute, Technical University of Denmark
Church
numerals
Fixpoint
combinators
A brief introduction to Lambda calculus
MRH 24/11/2016
The Maybe monad: A simple example
02157
Functional
Programming
f::Int -> Maybe Int
f 0 = Nothing
f x = Just x
Michael R. Ha
Lambda
calculus
g :: Int -> Maybe Int
g 100 = Nothing
g x
= Just x
Background
Syntax
reductions
Lambda
terms as
programs
h ::Int -> Maybe Int
h x = case f x of
Just n -> g n
Nothing -> Nothing
Church
numerals
Fixpoint
combinators
“Tagging” is avoided using monads:
h’ :: Int -> Maybe Int
h’ x = (f x) >> \ n -> (g n)
73
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
The Maybe monad: A simple example
02157
Functional
Programming
f::Int -> Maybe Int
f 0 = Nothing
f x = Just x
Michael R. Ha
Lambda
calculus
g :: Int -> Maybe Int
g 100 = Nothing
g x
= Just x
Background
Syntax
reductions
Lambda
terms as
programs
h ::Int -> Maybe Int
h x = case f x of
Just n -> g n
Nothing -> Nothing
Church
numerals
Fixpoint
combinators
“Tagging” is avoided using monads:
h’ :: Int -> Maybe Int
h’ x = (f x) >> \ n -> (g n)
An alternative formulation using the “sugared” do-notation:
h’ x = do n <- f x
g n
74
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
About Haskell
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
• Haskell is a pure functional, lazy (non-strict) language,
• with a very advanced polymorphic type system
• allowing infinite data (by default) due to lazy evaluation.
75
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
LISP
LISt Processing
John McCarthy – late 50ties
• An untyped, minimalistic functional language with a syntax
centered around so-called S-expressions.
02157
Functional
Programming
Michael R. Ha
• An untyped, eager (strict) language allowing imperative features
Used, e.g in the Boyer-Moore family of theorem provers
Lambda
calculus
Background
Example programs in the LISP dialect Scheme:
Syntax
reductions
Lambda
terms as
programs
(define (square x) (* x x))
(define fib
(lambda (n)
(if (or (= n 1) (= n 2))
1
(+ (fib (- n 1)) (fib (- n 2))))))
Church
numerals
Fixpoint
combinators
• An S-expression (define fib e) is used to define the function
fib by the expression e
• The expression e is another S-expression defining the function
of n given by the if-then-else expression.
• ...
76
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Structured data
• A pair z = (x, y ) is constructed using the primitive function
cons x y
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
77
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Structured data
• A pair z = (x, y ) is constructed using the primitive function
cons x y
• The components of a pair are extracted using primitive functions
(car z) = x and (cdr z) = y
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
78
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Structured data
• A pair z = (x, y ) is constructed using the primitive function
cons x y
02157
Functional
Programming
• The components of a pair are extracted using primitive functions
Michael R. Ha
(car z) = x and (cdr z) = y
• Lists (and general tree structures) are constructed by repeated
use of cons.
no type restriction
Lambda
calculus
• (cons 1 (cons 2 (cons 3 nil))) represents the list 1,2,3.
• (cons (cons 1 ”A”) (cons ”B” 2)) represents a complete tree with
depth 2 with leaves 1, ”A”, ”B”, 2.
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
79
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Structured data
• A pair z = (x, y ) is constructed using the primitive function
cons x y
02157
Functional
Programming
• The components of a pair are extracted using primitive functions
Michael R. Ha
(car z) = x and (cdr z) = y
• Lists (and general tree structures) are constructed by repeated
use of cons.
no type restriction
Lambda
calculus
• (cons 1 (cons 2 (cons 3 nil))) represents the list 1,2,3.
• (cons (cons 1 ”A”) (cons ”B” 2)) represents a complete tree with
depth 2 with leaves 1, ”A”, ”B”, 2.
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Some programs on lists:
Fixpoint
combinators
(define (nth n xs)
(if (= n 0) (car xs) (nth (- n 1) (cdr xs))))
(define (length xs)
(define (lengthA a ys)
(if (null? ys)
a
(lengthA (+ 1 a) (cdr ys))))
(lengthA 0 xs))
80
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Higher-order facilities
• car: “Contents of the Address part of Register number”
• cdr: “Contents of the Decrement part of Register number”
IBM 704 computer terminilogy (late 1950s).
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
81
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Higher-order facilities
• car: “Contents of the Address part of Register number”
• cdr: “Contents of the Decrement part of Register number”
IBM 704 computer terminilogy (late 1950s).
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
Background
Using well-known encodings from λ-calculus, we can define cons,
car and cdr using just functions:
(define (cons x y) (lambda (m) (m x y)))
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
(define (car z) (z (lamda (p q) p)))
(define (cdr z) (z (lamda (p q) q)))
The following properties are easily shown:
car(cons x y )
cdr(cons x y )
82
DTU Compute, Technical University of Denmark
=
=
x
y
A brief introduction to Lambda calculus
MRH 24/11/2016
cons, car and cdr in F#
let cons x y = fun m -> m x y;;
val cons: ’a -> ’b -> (’a -> ’b -> ’c) -> ’c
02157
Functional
Programming
Michael R. Ha
let car z = z (fun p q -> p);;
val car : ((’a -> ’b -> ’a) -> ’c) -> ’c
Lambda
calculus
Background
Syntax
let cdr z = z (fun p q -> q);;
val cdr : ((’a -> ’b -> ’b) -> ’c) -> ’c
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
83
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
cons, car and cdr in F#
let cons x y = fun m -> m x y;;
val cons: ’a -> ’b -> (’a -> ’b -> ’c) -> ’c
02157
Functional
Programming
Michael R. Ha
let car z = z (fun p q -> p);;
val car : ((’a -> ’b -> ’a) -> ’c) -> ’c
Lambda
calculus
Background
Syntax
let cdr z = z (fun p q -> q);;
val cdr : ((’a -> ’b -> ’b) -> ’c) -> ’c
With a little inconvenience to cope with “value restriction”:
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
let
let
let
val
combinators
t1 = fun m -> cons 1 "A" m;;
t2 = fun m -> cons "B" 2 m;;
t = fun m -> cons t1 t2 m;;
t :(((int -> string -> ’a) -> ’a) ->
((string -> int -> ’b) -> ’b) -> ’c) -> ’c
car(cdr t);;
val it : string = "B"
car(car t);;
val it : int = 1
84
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
About LISP
02157
Functional
Programming
Michael R. Ha
• LISP has an extremely simple syntax closely related to that of
λ-calculus
Lambda
calculus
Background
Syntax
reductions
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
85
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
About LISP
02157
Functional
Programming
Michael R. Ha
• LISP has an extremely simple syntax closely related to that of
λ-calculus
Lambda
calculus
Background
Syntax
reductions
• A program is easily represented as data, i.e. LISP supports
Meta-Programming
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
86
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
About LISP
02157
Functional
Programming
Michael R. Ha
• LISP has an extremely simple syntax closely related to that of
λ-calculus
Lambda
calculus
Background
Syntax
reductions
• A program is easily represented as data, i.e. LISP supports
Meta-Programming
• Structure and Interpretation of Computer Programs, Abelson,
Sussman and Sussman, MIT-press 1996 is a classical textbook
87
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
MRH 24/11/2016
About LISP
02157
Functional
Programming
Michael R. Ha
• LISP has an extremely simple syntax closely related to that of
λ-calculus
Lambda
calculus
Background
Syntax
reductions
• A program is easily represented as data, i.e. LISP supports
Meta-Programming
• Structure and Interpretation of Computer Programs, Abelson,
Sussman and Sussman, MIT-press 1996 is a classical textbook
Lambda
terms as
programs
Church
numerals
Fixpoint
combinators
• Clojure is a new LISP dialect that targets the Java Virtual
Machine (and CLR)
88
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
MRH 24/11/2016
Summary
02157
Functional
Programming
Michael R. Ha
Lambda
calculus
• λ − calculus is the theoretical underpinning of a huge variety of
functional languages.
Background
Syntax
reductions
Lambda
terms as
programs
• You may have fun with the construction of a λ-calculus
interpreter – a mini-project that is not mandatory
http://www.imm.dtu.dk/˜mire/FSharpBook/
miniProjects.html
89
DTU Compute, Technical University of Denmark
A brief introduction to Lambda calculus
Church
numerals
Fixpoint
combinators
MRH 24/11/2016