Lecture2 6up
Lecture2 6up
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 1/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 2/27
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 3/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 4/27
Recursion Recursion
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 5/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 6/27
1
Recursion What about Recursion?
I Next, expand the let binding: I On the face of it, λ-calculus does not seem to allow recursion
I Recall: let x = e1 in e2 defined as e2 [e1 /x ] I But this would make λ-calculus very boring; not many
interesting functions can be computed without recursion
I let f = λn.( if n = 0 then 1 else n ∗ (f (n − 1))) in (f 3) →
(λn.( if n = 0 then 1 else n ∗ (f (n − 1))) 3 I Amazing fact: It is possible to encode recursion in λ-calculus
I Left with undefined symbol f I It is just a little bit involved (but very instructive to
understand)
I Conclusion: We cannot encode recursion using named
functions I Any ideas?
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 7/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 8/27
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 9/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 10/27
I Intuition: The fixed point of h is applying h until v = h(v ), I This bizarre expression is called Y-combinator
i.e., the base case of the recursion is hit
I Recall property of fixed-point: v = h(v ) for any function h.
I But completely unclear how we can compute fixed-point of h
I Lets confirm that Y has this property:
I An expression that computes a fixed point is called a fixed
I Y h → λf .(λx .f (x x ))(λx .f (x x )) h →
point operator
(λx .h(x x ))(λx .h(x x )) → (h(x x ))[(λx .h(x x ))/x ] →
h(λx .h(x x ))(λx .h(x x )) → h(Yh)
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 11/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 12/27
2
Using the Y -combinator Fixed points Summary
I Let’s see how we can use the Y −combinator to compute
factorial:
I We can compute recursive functions in λ-calculus using
I Recall: G = λf .λn.( if n = 0 then 1 else n ∗ (f (n − 1)))
fixed-point operators
I Claim: Factorial of n can be computed as (YG) n
I We have seen the most famous fixed-point operator, the
I Example: Y -combinator
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 13/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 14/27
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 15/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 16/27
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 17/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 18/27
3
More L Examples Functions in L
I let g =
lambda a.if a>0 then 2*a else 3*a
in I For convenience, L also has built-in function definitions:
let u = 12 in
(g u) I fun compute_grade with percent =
if percent>90 then "A" else
I Value: ”24” if percent>80 then "B" else "F"
in
I L also supports currying:
(compute_grade 73)
I let x = lambda a,b.a+b in I Result: ”F”
let y = (x 2) in
(y 3)
I Value: ”5”
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 19/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 20/27
Recursion in L Input/Output in L
I Unlike λ-calculus, L allows you to write ”naturally” recursive
functions
I L has special operators for input/output:
I fun factorial with n =
if n<=0 then 1 else n* (factorial (n-1)) I let _ = print "Please enter an integer: " in
in let i = readInt in
... let _ = print "Please enter a string: " in
let s = readString in
I Can also write ”naturally recursive” anonymous functions: let _ = print "Integer read: " in
let _ = print i in
I let fact = let _ = print "String read: " in
lambda n. if n=0 then 1 else n* (fact (n-1)) let _ = print s in
in 0
(fact 6)
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 23/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 24/27
4
More List Examples Run-time errors
I How about computing the length of a list? I There are many run-time errors possible in L programs:
Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 25/27 Thomas Dillig, CS345H: Programming Languages Lecture 2: Lambda Calculus II and Introduction to L 26/27
I Assignment 1: Lexer
I Assignment 2: Parser
I Assignment 3: Interpreter