the lambda calculus (pre lecture) · 2019-12-10 · lambda calculus ian expression-rewriting system...
TRANSCRIPT
The Lambda Calculus (Pre Lecture)
Dr. Neil T. Dantam
CSCI-561, Colorado School of Mines
Fall 2020
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 1 / 62
Introduction
Lambda CalculusI An expression-rewriting system
I General model of computation(TM-equivalent)
I Close model of software / structuredprograms / functional programming
OutcomesI Understand basics of lambda calculus
operation
I Relate lambda calculus andprogramming structures
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 2 / 62
Outline
Lambda Calculus
Universal Computation
Programming in the Lambda Calculus
Church-Turing Thesis
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 3 / 62
Lambda Calculus
Outline
Lambda Calculus
Universal Computation
Programming in the Lambda Calculus
Church-Turing Thesis
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 4 / 62
Lambda Calculus
Calculus?
Definition: Calculus
A well defined method for mathematical reasoning employing axiomsand rules of inference or transformation. A formal system or rewritesystem.
Examples: differential calculus, first-order logic (predicate calculus),lambda calculus
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 5 / 62
Lambda Calculus
Lambda Calculus Expressions
Lambda Calculus Common Lisp
ID: a ’a
Definition:λ
parameter︷︸︸︷x .
body︷︸︸︷α
(lambda (x) α)
Call:function︷ ︸︸ ︷(λx .α)
parameter︷︸︸︷y
(funcall (lambda (x) α) ’y)
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 6 / 62
Lambda Calculus
Function CallsBinding and Substitutionλparameter name︷︸︸︷
x .
body︷︸︸︷α
︸ ︷︷ ︸
function definition
y︸︷︷︸parameter value
(λ x . a0 . . . x . . . a1) y a0 . . . y . . . a1
bind x ← y
substitute parameter into body
Example
I (λx . x) y
y
I (λx . x) (λy . y)
λy . y
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 7 / 62
Lambda Calculus
Examples: Lambda CalculusCommon Lisp
(λx . x) y
( ( lambda ( x ) x )’ y )
; ; => ’ y
(λx . x) (λy . y)
( ( lambda ( x ) x )( lambda ( y ) y ) )
; ; => ( lambda ( y ) y )
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 8 / 62
Lambda Calculus
Function Call AssociativityEvaluate Left to Right
(a0a1a2a3) = ((((a0a1)a2)a3)
call
call
call
a0
fun.
a1arg.
fun.
a2
arg.
fun.
a3
arg.
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 9 / 62
Lambda Calculus
Function Call Parenthesization
(a0 a1) (a2 a3)
call
call
a0
fun.
a1
arg.
fun.
call
a2
fun.
a3
arg.
arg.
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 10 / 62
Lambda Calculus
Lambda Calculus Grammar
〈e〉 → 〈id〉| [λ] 〈id〉 [.] 〈e〉| 〈e〉〈e〉| [(] 〈e〉 [)]
〈id〉 → [x] | [y] | [z] | . . .
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 11 / 62
Lambda Calculus
Exercise: Lambda Calculus Execution (1/3)
I(λx . (λy . xy)) z
λy . zy
I(λx . (λy . xy)) λz . z
λy . (λz . z)y λy . y
I(λx . x x) (λx . x x)
(λx . x x) (λx . x x) (λx . x x) (λx . x x)
. . .
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 12 / 62
Lambda Calculus
Exercise: Lambda Calculus Execution (2/3)
(λx . (λy . xy)) z
( ( lambda ( x )( lambda ( y )
( f u n c a l l x y ) ) )’ z )
; ; => ( lambda ( y ); ; ( f u n c a l l ’ z y ) )
(λx . (λy . xy)) λz . z
( ( lambda ( x )( lambda ( y )
( f u n c a l l x y ) ) )( lambda ( z ) z ) )
; ; => ( lambda ( y ); ; ( ( lambda ( z ) z ); ; y ) ); ; => ( lambda ( y ) y )
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 13 / 62
Lambda Calculus
Exercise: Lambda Calculus Execution (3/3)
(λx . x x) (λx . x x)
( ( lambda ( x ) ( f u n c a l l x x ) )( lambda ( x ) ( f u n c a l l x x ) ) )
; ; => STACK OVERFLOW / INFINITE LOOP
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 14 / 62
Universal Computation
Outline
Lambda Calculus
Universal Computation
Programming in the Lambda Calculus
Church-Turing Thesis
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 15 / 62
Universal Computation
λ-calculus is Turing-complete
Theorem
The Lambda Calculus is Turing-complete.
Proof Outline.
1. A Turing machine can simulate the Lambda calculus
2. The Lambda Calculus can simulate a Turing machine
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 16 / 62
Universal Computation
Lambda Calculus vs. Turing Machine
Lambda Calculus
I Focus: Transformation of Expressions
I Model for Software
Turing Machine
I Focus: Operation of Machine
I Model for Hardware
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 17 / 62
Universal Computation
λ-Calculus Reduces to TMLazy proof
I A Turing machine can simulate the Lambda Calculus:
1. Lambda calculus reduces to Common Lisp (/ ML / Haskell)2. Common Lisp (/ ML / Haskell) reduces to the RAM Machine3. The RAM Machine reduces to the Turing Machine
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 18 / 62
Universal Computation
TM Reduces to λ-CalculusOutline
I The Lambda Calculus can simulate a Turing machine:
1. A Turing machine reduces to functional programming.That is, we can simulate a TM in a functional programming language.
2. Functional programming reduces to the Lambda Calculus
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 19 / 62
Programming in the Lambda Calculus
Outline
Lambda Calculus
Universal Computation
Programming in the Lambda Calculus
Church-Turing Thesis
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 20 / 62
Programming in the Lambda Calculus
Binary function to Unary function ReductionCommon Lisp
Binary Function
( ( lambda ( a b )(− a b ) )
52)
; ; => 3
Two Unary Functions
( f u n c a l l ( ( lambda ( a )( lambda ( b )
(− a b ) ) )5)
2)
; ; => ( f u n c a l l ( lambda ( b ); ; (− 5 b ) ); ; 2); ; => 3
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 21 / 62
Programming in the Lambda Calculus
Binary to Unary function ReductionLambda Calculus
Unary:λa .α
Binary:
binary︷ ︸︸ ︷λab .α
unary︷ ︸︸ ︷λa .
λb .α︸ ︷︷ ︸unary
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 22 / 62
Programming in the Lambda Calculus
Exercise: N-ary function to Unary function Reduction
Unary:λa .α
Binary:
binary︷ ︸︸ ︷λab .α
unary︷ ︸︸ ︷λa .
λb .α︸ ︷︷ ︸unary
N-ary:
arity: n+1︷ ︸︸ ︷
λx0x1 . . . xn .α
unary︷ ︸︸ ︷λx0 .
λx1 . . . xn .α︸ ︷︷ ︸arity: n
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 23 / 62
Programming in the Lambda Calculus
Functional Programming: Currying
Binary Curry: λfa . (λb . fab)
( defun curry−2 ( f u n c t i o n arg−a )( lambda ( arg−b )
( f u n c a l l f u n c t i o n arg−a arg−b ) ) )
; ; Example :; ; ( f u n c a l l ( curry−2 #’+ 1); ; 2); ; => 3
Haskell B. Curry
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 24 / 62
Programming in the Lambda Calculus
Historical InterludeCurry, Hilbert, Church, and Turing
Haskell Curry
David Hilbert
Alonzo Church
Alan Turing
“decision problem”via λ-calculus
“decision problem”
via Turing machines
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 25 / 62
Programming in the Lambda Calculus
Let to Lambda ReductionCommon Lisp
Let
( l e t ( ( a 5)( b 3 ) )
(− a b ) )
; ; => 2
Lambda
( f u n c a l l ( lambda ( a b )(− a b ) )
53)
; ; => 2
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 26 / 62
Programming in the Lambda Calculus
Let to Lambda ReductionLambda Calculus
let
variable︷︸︸︷x ←
value︷︸︸︷α in
body︷︸︸︷β (λ
variable︷︸︸︷x .
body︷︸︸︷β )
value︷︸︸︷α
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 27 / 62
Programming in the Lambda Calculus
Sequential Assignment to Let Reduction
def x ← α;β let x ← α in β
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 28 / 62
Programming in the Lambda Calculus
Global FunctionsReduction to Sequential Assignment
defun
name︷︸︸︷f
(arguments︷ ︸︸ ︷x0 . . . xn
)→
body︷︸︸︷β ;
γ︸︷︷︸more expressions
def f ← λx0 . . . xn .β;γ︸︷︷︸
more expressions
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 29 / 62
Programming in the Lambda Calculus
Church BooleansCommon Lisp
Church Booleans: True
( l e t( ( t r u ( lambda ( t t f f ) t t ) )
( f l s ( lambda ( t t f f ) f f ) )( t e s t ( lambda ( b then e l s e )
( f u n c a l l bthene l s e ) ) ) )
( f u n c a l l t e s t t r u ’ a ’ b ) ) )
; ; => ( f u n c a l l t r u ’ a ’ b ); ; => ’A
Church Booleans: False
( l e t( ( t r u ( lambda ( t t f f ) t t ) )
( f l s ( lambda ( t t f f ) f f ) )( t e s t ( lambda ( b then e l s e )
( f u n c a l l bthene l s e ) ) ) )
( f u n c a l l t e s t f l s ’ a ’ b ) ) )
; ; => ( f u n c a l l f l s ’ a ’ b ); ; => ’B
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 30 / 62
Programming in the Lambda Calculus
Church BooleansLambda Calculus
defun tru (t f )→ t;defun fls (t f )→ f ;defun test (b t f )→ b t f ;
if χ then τ else η test χ τ η
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 31 / 62
Programming in the Lambda Calculus
Church Boolean Operators
AND: α ∧ β α β fls
OR: α ∨ β α tru β
NOT: ¬α α fls tru
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 32 / 62
Programming in the Lambda Calculus
PairsCommon Lisp – fst
Pairs
( l e t ∗ ( ( t r u ( lambda ( t t f f ) t t ) )( f l s ( lambda ( t t f f ) f f ) )( p a i r ( lambda ( f s )
( lambda ( b )( f u n c a l l b f s ) ) ) )
( f s t ( lambda ( p )( f u n c a l l p t r u ) ) )
( snd ( lambda ( p )( f u n c a l l p f l s ) ) ) )
( f u n c a l l f s t( f u n c a l l p a i r ’ x ’ y ) ) )
; ; => ’ x
1. (funcall fst
(funcall pair ’x ’y))
2. (funcall fst
(lambda (b)
(funcall b ’x ’y)))
3. (funcall (lambda (b)
(funcall b ’x ’y))
tru)
4. (funcall tru ’x ’y)
5. ’x
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 33 / 62
Programming in the Lambda Calculus
PairsCommon Lisp – snd
Pairs
( l e t ∗ ( ( t r u ( lambda ( t t f f ) t t ) )( f l s ( lambda ( t t f f ) f f ) )( p a i r ( lambda ( f s )
( lambda ( b )( f u n c a l l b f s ) ) ) )
( f s t ( lambda ( p )( f u n c a l l p t r u ) ) )
( snd ( lambda ( p )( f u n c a l l p f l s ) ) ) )
( f u n c a l l snd( f u n c a l l p a i r ’ x ’ y ) ) )
; ; => ’ y
1. (funcall snd
(funcall pair ’x ’y))
2. (funcall snd
(lambda (b)
(funcall b ’x ’y)))
3. (funcall (lambda (b)
(funcall b ’x ’y))
fls)
4. (funcall fls ’x ’y)
5. ’y
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 34 / 62
Programming in the Lambda Calculus
PairsLambda Calculus
I defun tru (t f )→ t;
I defun fls (t f )→ f ;
I defun pair (f s)→ λb . b f s;
I defun fst (p)→ p tru;
I defun snd (p)→ p fls;
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 35 / 62
Programming in the Lambda Calculus
PairsEmpty Lists
I defun pair (f s)→ λb . b f s;I Desired behavior:
I isempty p flsI isemptynil tru
I defun isempty (p)→ p (λf s . fls);
I def nil← λb .tru;
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 36 / 62
Programming in the Lambda Calculus
PairsEmpty Lists – Example 0
I defun pair (f s)→ λb . b f s;
I defun isempty (p)→ p (λf s . fls);
I def nil← λb .tru;
I isempty (pair x y)
1.
isempty︷ ︸︸ ︷(λp . p (λf s . fls)) (
pair︷ ︸︸ ︷(λf s . λb . b f s) x y)
2.
isempty︷ ︸︸ ︷(λp . p (λf s . fls))
pair x y︷ ︸︸ ︷(λb . b x y)
3.
pair x y︷ ︸︸ ︷(λb . b x y) (λf s . fls)
4. (λf s . fls)x y5. fls
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 37 / 62
Programming in the Lambda Calculus
Exercise: PairsEmpty Lists
I defun pair (f s)→ λb . b f s;
I defun isempty (p)→ p (λf s . fls);
I def nil← λb .tru;
I isemptynil
1.
isempty︷ ︸︸ ︷(λp . p (λf s . fls))
nil︷ ︸︸ ︷(λb .tru)
2.
nil︷ ︸︸ ︷(λb .tru)(λf s . fls)
3. tru
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 38 / 62
Programming in the Lambda Calculus
Lists
list (x0) pair x0 nillist (x0 x1) pair x0 (pair x1 nil)
list (x0 x1 . . . xn) pair x0 (list (x1 . . . xn))
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 39 / 62
Programming in the Lambda Calculus
StructuresReduction to Lists
defstruct S (f0 f1 . . . fn)
defun make-S (f0 f1 . . . fn)→ list (f0 f1 . . . fn) ;defun S-f0 (s)→ fst s;defun S-f1 (s)→ fst (snd s);. . .
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 40 / 62
Programming in the Lambda Calculus
NumbersChurch Numerals
Number Church Numeral0 λs z . z1 λs z . s z2 λs z . s (s z)3 λs z . s (s (s z))4 . . ....
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 41 / 62
Programming in the Lambda Calculus
Increment
defun succ (n)→ λs z . s (n s z);
I 0 λs z . z
I 1 succ(0) (λs z . s (n s z)) (λs z . z) (λs z . s ((λs z . z) s z)) (λs z . s z)
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 42 / 62
Programming in the Lambda Calculus
Exercise: Increment
defun succ (n)→ λs z . s (n s z);
I 2 succ(1)
(λs z . s (n s z)) (λs z . s z) (λs z . s ((λs z . s z) s z)) (λs z . s (s z))
I 3 succ(2)
(λs z . s (n s z)) (λs z . s (s z)) (λs z . s ((λs z . s (s z)) s z)) (λs z . s (s (s z)))
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 43 / 62
Programming in the Lambda Calculus
Add
defun plus (mn)→ λs z .m s (n s z);
I 0 + 1
(λmn . (λs z .m s (n s z)))
0︷ ︸︸ ︷(λs z . z)
1︷ ︸︸ ︷(λs z . s z)
λs z .
m=0︷ ︸︸ ︷(λs z . z) s (
n=1︷ ︸︸ ︷(λs z . s z)sz)
λs z . ((λs z . z) s (sz)) λs z . (s z)
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 44 / 62
Programming in the Lambda Calculus
Exercise: Add
defun plus (mn)→ λs z .m s (n s z);
I 1 + 1
(λmn . (λs z .m s (n s z)))
1︷ ︸︸ ︷(λs z . s z)
1︷ ︸︸ ︷(λs z . s z)
λs z .
m=1︷ ︸︸ ︷(λs z . s z) s (
n=1︷ ︸︸ ︷(λs z . s z)s z)
λs z . ((λs z . s z) s (s z)) λs z . (s (s z))
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 45 / 62
Programming in the Lambda Calculus
Is Zero
defun iszro (m)→ m (λx . fls) tru;
I iszro(0)
1.
iszro︷ ︸︸ ︷(λm .m (λx . fls) tru)
0︷ ︸︸ ︷(λs z . z)
2.
m=0︷ ︸︸ ︷(λs z . z) (λx . fls) tru
3. tru
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 46 / 62
Programming in the Lambda Calculus
Exercise: Is Zero
defun iszro (m)→ m (λx . fls) tru;
I iszro(1)
1.
iszro︷ ︸︸ ︷(λm .m (λx . fls) tru)
1︷ ︸︸ ︷(λs z . s z)
2.
m=1︷ ︸︸ ︷(λs z . s z) (λx . fls) tru
3.
(λx . fls) tru
4.
fls
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 47 / 62
Programming in the Lambda Calculus
DecrementOverview
pred(n) =
{0 n = 0
n − 1 n > 0
I def zz← pair 0 0;
I defun ss (p)→ pair (snd p) (plus 1 (snd p));
I defun prd (m)→ fst (m ss zz);
I prd 0
1.
prd︷ ︸︸ ︷(λm . fst (m ss zz))
0︷ ︸︸ ︷(λs z . z)
2. fst (
m=0︷ ︸︸ ︷(λs z . z) ss zz)
3. fst zz fst (pair 0 0)4. 0
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 48 / 62
Programming in the Lambda Calculus
Exercise: Decrement 1prd 1
I prd 1
1.
prd︷ ︸︸ ︷(λm . fst (m ss zz))
1︷ ︸︸ ︷(λs z . s z)
2.
fst (
m=1︷ ︸︸ ︷(λs z . s z) ss zz)
3. fst (ss zz)
4.
fst (
ss︷ ︸︸ ︷(λp .pair (snd p) (plus 1 (snd p)))
zz︷ ︸︸ ︷(pair 0 0))
5.
fst (pair (snd (pair 0 0)) (plus 1 (snd (pair 0 0))))
6.
fst (pair 0 (plus 1 0))
7.
fst (pair 0 1)
8.
0
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 49 / 62
Programming in the Lambda Calculus
DecrementSummary
I def zz← pair 0 0;
I defun ss (p)→ pair (snd p) (plus 1 (snd p));
I defun prd (m)→ fst (m ss zz);
I ss (pair i j) pair j (j + 1)I i ss zz
I (i − 1) ss (ss zz)I (i − 1) ss (pair 0 1)I (i − 2) ss (ss (pair 0 1))I (i − 2) ss (pair 1 2)I ∗ pair (i − 1) i
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 50 / 62
Programming in the Lambda Calculus
RecursionA problem
I defun p (a)→ if g a then x else h (p a);β
I def p ← λa . (if g a then x else h (p a)) ;β
I let p ← λa . (if g a then x else h (p a)) in β
I (λp .β) (λa . (if g a then x else h (p a)))
I (λp .β)
(λa .
if g a then x else h ( p︸︷︷︸unbound!
a)
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 51 / 62
Programming in the Lambda Calculus
RecursionY Combinator
I def Y ← λf . (λx . f (x x)) (λx . f (x x)) ;
I defun p (a)→ if g a then x else h (p a);
I def p′ ← λr . λa . if g a then x else h (r a);def p ← Y p′;
I p b (Y p′) b (
Y︷ ︸︸ ︷λf . (λx . f (x x)) (λx . f (x x)) p′) b
Yp′=αα︷ ︸︸ ︷(λx . p′ (x x)
) (λx . p′ (x x)
)b (λx . p′ (x x)) α b
(p′
β︷ ︸︸ ︷(αα)) b
(
p′︷ ︸︸ ︷λr . λa . if g a then x else h (r a))β b
(λa . if g a then x else h (β a)) b
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 52 / 62
Programming in the Lambda Calculus
RecursionY Combinator–continued
I p b
p′︷ ︸︸ ︷λr . λa . if g a then x else h (r a)
β b
(λa . if g a then x else h (β a)) b
I β αα αα=Yp′=p︷ ︸︸ ︷(
λx . p′ (x x)) (λx . p′ (x x)
)
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 53 / 62
Programming in the Lambda Calculus
Local Recursive Functions
letrec
name︷︸︸︷f
(arguments︷ ︸︸ ︷x0 . . . xn
)=
body︷ ︸︸ ︷β0 . . . f︸︷︷︸
recurse
. . . βn in γ︸︷︷︸more expressions
let f ← Yλr . λx0 . . . xn .
body︷ ︸︸ ︷β0 . . . r︸︷︷︸
recurse
. . . βn in γ︸︷︷︸more expressions
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 54 / 62
Programming in the Lambda Calculus
Global Recursive Functions
defun
name︷︸︸︷f
(arguments︷ ︸︸ ︷x0 . . . xn
)→
body︷ ︸︸ ︷β0 . . . f︸︷︷︸
recurse
. . . βn;
γ︸︷︷︸more expressions
letrec
name︷︸︸︷f
(arguments︷ ︸︸ ︷x0 . . . xn
)=
body︷ ︸︸ ︷β0 . . . f︸︷︷︸
recurse
. . . βn in γ︸︷︷︸more expressions
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 55 / 62
Programming in the Lambda Calculus
RecursionFixpoint Combinator
I def fix← λf . (λx . f (x x)) (λx . f (x x)) ;I defun p (a)→ if g a then x else h (p a);I def p′ ← λr . λa . if g a then x else h (r a);
def p ← fix p′;
I p b (fix p′) b (
fix︷ ︸︸ ︷λf . (λx . f (λy . x x y)) (λx . f (λy . x x y)) p′) b
fix p′=αα︷ ︸︸ ︷(λx . p′ (λy . x x y)) (λx . p′ (λy . x x y)) b (λx . p′ (λy . x x y)) α b
(p′β︷ ︸︸ ︷
λy .ααy) b
p′︷ ︸︸ ︷λr . λa . if g a then x else h (r a)
β b
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 56 / 62
Programming in the Lambda Calculus
RecursionFixpoint Combinator–continued
I p b
p′︷ ︸︸ ︷λr . λa . if g a then x else h (r a)
β b
(λa . if g a then x else h (β a)) b
I β a (λy .αα y) a
fix p′=p︷︸︸︷αα a
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 57 / 62
Programming in the Lambda Calculus
Functional ProgrammingReduction to λ-calculus summary
let x ← α in β (λx .β)αdef x ← α;β (λx .β)α
defun f (x)→ β; γ (λf . γ) (λx .β)letrec f (x) = β0 . . . f . . . βn in γ (λf . γ) (Y λr .β0 . . . r . . . βn)
tru λt f . tfls λt f . f
if α then β else γ αβγpair f s λb . b f s
0, 1, 2, . . . (λs z . z), (λs z . s z), (λs z . s (s z)), . . .
A “complete” programming language.
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 58 / 62
Church-Turing Thesis
Outline
Lambda Calculus
Universal Computation
Programming in the Lambda Calculus
Church-Turing Thesis
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 59 / 62
Church-Turing Thesis
Church-Turing Thesis
Church-Turing Thesis
A method, or procedure, M, for achieving some desired result is calledeffectively calculable if its values can be found by some purelymechanical process. That is:
1. M is described in a finite number of exact instructions, (eachinstruction being expressed by means of a finite number ofsymbols);
2. M will, if executed without error, produce the desired result in afinite number of steps;
3. M can (in practice or in principle) be carried out by a humanunaided by any machinery save paper and pencil;
4. M demands no insight or ingenuity on the part of the humancarrying it out.
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 60 / 62
Church-Turing Thesis
Lambda Calculus—Turing Machine Equivalence
λa .α
Finite ControlQ
{accept, reject}
σi . . . σn t . . .. . .σ0
read-write
headtape
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 61 / 62
Church-Turing Thesis
References
Alt. Textbook: Benjamin Pierce. Type Systems andProgramming Languages.
I Ch 5 The Untyped Lambda-Calculus
Dantam (Mines CSCI-561) The Lambda Calculus (Pre Lecture) Fall 2020 62 / 62