Download Introduction to Lambda Calculus - CSE IITK

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C Sharp (programming language) wikipedia , lookup

Currying wikipedia , lookup

Anonymous function wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Combinatory logic wikipedia , lookup

Standard ML wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Transcript
Functional Programming
and
λ Calculus
Amey Karkare
Dept of CSE, IIT Kanpur
0
Software Development Challenges
Growing size and complexity of
modern computer programs
Complicated architectures

Massively parallel architectures,
Memory hierarchy, distributed
systems,…
Fast and cost effective software
development
Above all: Correctness!

Proof that the program works for all
cases
1
Well-structured Software
Easy to write and debug
Reusable modules
Amenable to proofs
Permit rapid prototyping
Solutions to the development challenges.
Programming style to support development
of well-structured software.
2
Functional Languages
Fundamental operation is the
application of functions to
arguments.
Main features to improve
modularity:
No (almost none!!) side effects
 Higher order functions
 Lazy evaluation

3
Example
Summing the integers 1 to 10 in C:
int total = 0, i;
for (i = 1; i <= 10; ++i)
total = total+i;
Values change for both total and i during program
execution
4
Example
Summing integers 1 to 10 in a pure
functional language

No side effect => No assignments to
variables!
sum (m, n) = if (m > n) 0
else m + sum (m+1, n)
sum (1, 10) // main function
5
Historical Background
[source: http://www.cs.nott.ac.uk/~gmh/chapter1.ppt]
1930s:
Alonzo Church develops the lambda calculus,
a simple but powerful theory of functions.
6
Historical Background
[source: http://www.cs.nott.ac.uk/~gmh/chapter1.ppt]
1950s:
John McCarthy develops Lisp, the first functional
language, with some influences from the lambda
calculus, but retaining variable assignments.
7
Historical Background
[source: http://www.cs.nott.ac.uk/~gmh/chapter1.ppt]
1970s:
John Backus develops FP, a functional
language that emphasizes higher-order
functions and reasoning about programs.
8
Trivia
John Backus : Proposed (in 1954) a
program that translated high level
expressions into native machine code.
Fortran I project (1954-1957): The
first compiler was released
1977 ACM Turing Award
“for profound, influential, and lasting
contributions to the design of practical
high-level programming systems,
notably through his work on
FORTRAN, and for publication of
formal procedures for the specification
of programming languages.”
Introduced FP in his Turing Award lecture
"Can Programming be Liberated from the von
Neumann Style?".
9
Quicksort: English description
1. Empty list is already sorted.
2. For a non empty list
a. Pick the first element, pivot, from the array.
b. Recursively quicksort the array of elements with
values less than the pivot. Call it S.
c. Recursively quicksort the array of elements with
values greater than or equal to the pivot, except
the pivot. Call it G.
d. The final sorted array is: the elements of S
followed by pivot, followed by the elements of
G.
10
Quicksort: Functional
(Haskell) description*
quicksort [] = []
quicksort (x:xs) =
quicksort [y | y <- xs, y<x]
++ [x]
++ quicksort [y | y <- xs, y>=x]
* source: https://www.haskell.org/tutorial/haskell-98-tutorial.pdf
11
Higher order function
add x y = x + y
inc = add 1
map f [] = []
map f (x:xs) = f x : map f xs
• map is a higher order function. It takes a function
as argument.
• Functional programming treats functions as firstclass citizens. There is no discrimination between
function and data.
map inc [1, 2, 3] =>
[2, 3, 4]
12
Lazy evaluation
Do not evaluate an expression unless
it is needed
Never evaluate an expression more
than once
length [1/1, 2/2, 0/0, 4/4]
=>
4
numsFrom n = n : numsFrom (n+1)
squares = map (^2) (numsfrom 0)
take 5 squares
=> [0,1,4,9,16]
13
Lambda calculus
The “assembly language” of
functional programming
The Abstract Syntax
A really tiny language of expressions
// An expression can be a
// Variable
𝑒∷𝑥
| 𝜆𝜆. 𝑒1 // Function Definition
| 𝑒1 𝑒2 // Function Application
| (𝑒1 )
That’s all the Syntax!!
15
Conventions
𝜆𝜆. 𝑒1 𝑒2 𝑒3 is an abbreviation for
𝜆𝜆. 𝑒1 𝑒2 𝑒3 , i.e., the scope of 𝑥 is as far
to the right as possible until it is


terminated by a ) whose matching ( occurs
to the left of the 𝜆, or
terminated by the end of the term
Application associates to the left:𝑒1 𝑒2 𝑒3 is
to be read as (𝑒1 𝑒2 )𝑒3 and not as 𝑒1 (𝑒2 𝑒3 )
𝜆𝜆𝜆𝜆. 𝑒 is an abbreviation for 𝜆𝜆𝜆𝑦𝜆𝑧. 𝑒
which in turn is actually 𝜆𝜆. (𝜆𝑦. 𝜆𝜆. 𝑒 )
16
𝛼-renaming
The name of a bound variable has no
meaning except for its use to identify
the bounding λ.
Renaming a λ variable including all its
bound occurrences does not change
the meaning of an expression.
For example, 𝜆𝜆. 𝑥 𝑥 𝑦 is equivalent to
𝜆𝑢. 𝑢 𝑢 𝑦


But it is not same as 𝜆𝜆. 𝑥 𝑥 𝑤
Can not change free variable!
17
𝛽-reduction(Execution)
if an abstraction 𝜆𝜆. 𝑒1 is applied to a
term 𝑒2 then the result of the
application is

the body of the abstraction 𝑒1 with all free
occurrences of the formal parameter 𝑥
replaced with 𝑒2 .
For example,
𝜆𝜆𝜆𝜆. 𝑓 (𝑓 𝑥) 𝑡𝑡𝑡𝑡𝑡
𝑡𝑡𝑡𝑡𝑡 (𝑡𝑡𝑡𝑡𝑡 𝑥)
𝛽
→
18
Caution
During 𝛽-reduction, make sure a free
variable is not captured inadvertently.
The following reduction is WRONG
𝜆𝜆. 𝜆𝜆. 𝑥 𝜆𝜆. 𝑦 → 𝜆𝜆. 𝜆𝜆. 𝑦
Use 𝛼-renaming to avoid variable
capture
𝜆𝜆. 𝜆𝜆. 𝑥 𝜆𝜆. 𝑦 → 𝜆𝑢𝑢𝑢. 𝑢 𝜆𝑥. 𝑦
→ 𝜆𝜆. 𝜆𝜆. 𝑦
19
Exercise
Apply 𝛽-reduction as far as possible
1. (𝜆𝜆 𝑦 𝑧. 𝑥 𝑧 𝑦 𝑧 ) 𝜆𝜆 𝑦. 𝑥 (𝜆𝜆. 𝑦)
2. 𝜆 𝑥. 𝑥 𝑥 𝜆𝑥. 𝑥 𝑥
3. 𝜆𝜆 𝑦 𝑧. 𝑥 𝑧 (𝑦 𝑧) 𝜆𝜆 𝑦. 𝑥 ( 𝜆𝜆. 𝑥 𝑥 𝜆𝜆. 𝑥 𝑥 )
20
Church-Rosser Theorem
Multiple ways to apply 𝛽-reduction
Some may not terminate
However, if two different reduction
sequences terminate then they always
terminate in the same term
𝑒1
𝑒
𝑒′
𝑒2
Leftmost, outermost reduction will find
the normal form if it exists
21
But what about other stuff?
Constants ?


Numbers
Booleans
Complex Types ?


Lists
Arrays
Don’t we need “data”?
Recall: functions are first-class citizens!
Function is data and data is function.
22
Numbers
We need a “Zero”

“Absence of item”
And something to count

“Presence of item”
Intuition: Whiteboard and Marker



Blank board represents Zero
Each mark by marker represents a count.
However, other pairs of objects will work
as well
Lets translate this intuition into λ-expr
23
Numbers
Zero = 𝜆𝜆. 𝜆𝜆. 𝑤

No mark on whiteboard
One = 𝜆𝑚. 𝜆𝑤. 𝑚 𝑤
Two = 𝜆𝑚. 𝜆𝑤. 𝑚 𝑚 𝑤
…
What about operations?

add, multiply, subtract, divide …
24
Operations on Numbers
succ = 𝜆𝜆𝜆𝜆. 𝑚 (𝑥 𝑚 𝑤)

Verify that 𝑠𝑠𝑠𝑠 𝑁 = 𝑁 + 1
add = 𝜆𝑥𝑥𝑥𝑤. 𝑥 𝑚 𝑦 𝑚 𝑤

Verify that 𝑎𝑎𝑎 𝑁 𝑀 = 𝑁 + 𝑀
mult = 𝜆𝑥𝑥𝑥𝑥. 𝑥 𝑦 𝑚 𝑤

Verify that 𝑚𝑚𝑚𝑚 𝑁 𝑀 = 𝑁 ∗ 𝑀
called
Church Numerals.
25
Booleans
True and False
Intuition: Select one out of two
possible choices.
λ-expressions


True = 𝜆𝜆 𝜆𝜆. 𝑥
False = 𝜆𝜆 𝜆𝜆. 𝑦
26
Operations on Booleans
Logical operations
𝑎𝑎𝑎 = 𝜆𝑝 𝑞. 𝑝 𝑞 𝑝
𝑛𝑛𝑛 = 𝜆𝜆 𝑡 𝑓. 𝑝 𝑓 𝑡
…
The conditional function 𝑖𝑖

𝑖𝑖 𝑐 𝑒1 𝑒2 reduces to 𝑒1 if 𝑐 reduces to
True and 𝑒2 if 𝑐 reduces to False
𝑖𝑖 = 𝜆𝜆 𝑒𝑡 𝑒𝑓 . (𝑐 𝑒𝑡 𝑒𝑓 )
27
More…
More such types can be found at

https://en.wikipedia.org/wiki/Church_enc
oding
It is fun to come up with your own
definitions for constants and
operations over different types

or to develop understanding for existing
definitions.
28
We are missing something!!
The machinery described so far does
not allow us to define Recursive
functions

factorial, Fibonacci …
There is no concept of “named”
functions

So no way to refer to a function
“recursively”?
Fix-point computation comes to rescue
29
Fix-point and 𝑌-combinator
A fix-point of a function 𝑓 is a value
𝑝 such that 𝑓 𝑝 = 𝑝
Assume existence of a magic
expression, called 𝑌-combinator, that
when applied to a λ-expression, gives
its fixed point
𝑌 𝑓 = 𝑓 (𝑌 𝑓)
𝑌–combinator gives us a way to apply
a function recursively
30
Factorial
fact =
λn. if (isZero n) One (mult n (fact (pred n)))
= (λfλn. if (isZero n) One (mult n (f (pred n)))) fact
fact = g fact
fact is a fixed point of function
g = λfλn. if (isZero n) One (mult n (f (pred n))))
Using Y-combinator,
fact = Y (λfλn. if (isZero n) One (mult n (f (pred n))))
=Yg
31
Verify
fact 2
= (Y g) 2 = g (Y g) 2
// Y f = f (Y f), definition of Y-combinator
= (λfλn. if (is0 n) 1 (* n (f (pred n)))) (Y g) 2
= (λn. if (is0 n) 1 (* n ((Y g) (pred n)))) 2
= if (is0 2) 1 (* 2 ((Y g) (pred 2)))
= (* 2 ((Y g) 1))
…
= (* 2 (* 1 (if (is0 0) 1 (* 0 ((Y g) (pred 0)))))
= (* 2 (* 1 1)) = 2
32
Recursion
Y-combinator allows to unroll the body
of loop once – similar to one unfolding
of recursive call
Sequence of Y-combinator applications
allow complete unfolding of recursive
calls
BUT, what about the existence of Ycombinator?
33
Y-combinators
Many candidates exist
𝑌1 = 𝜆𝜆 𝜆𝜆. 𝑓 𝑥 𝑥 𝜆𝜆. 𝑓 𝑥 𝑥
𝑇 = 𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆𝜆.
𝑟 𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡𝑡
𝑌𝑓𝑓𝑓𝑓𝑓 = 𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇𝑇
• Verify that (Y f) = f (Y f) for each
34
Summary
A cursory look at λ-calculus to
understand how Functional
Programming works

How it is different from imperative
programming
Functions are data, and Data are
functions!
Church Turing Thesis => The power of
λ calculus equivalent to that of Turing
Machine
35
36