SlideShare a Scribd company logo
CS776 Prasad 1
Foundations of Programming Languages:
Introduction to Lambda Calculus
CS776 Prasad 2
Lecture Outline
• Why study lambda calculus?
• Lambda calculus
– Syntax
– Evaluation
– Relationship to programming languages
• Next time: type systems for lambda calculus
CS776 Prasad 3
Lambda Calculus. History.
• A framework developed in 1930s by Alonzo
Church to study computations with functions
• Church wanted a minimal notation
– to expose only what is essential
• Two operations with functions are essential:
– function creation
– function application
CS776 Prasad 4
Function Creation
• Church introduced the notation
lx. E
to denote a function with formal argument
x and with body E
• Functions do not have names
– names are not essential for the computation
• Functions have a single argument
– once we understand how functions with one
argument work we can generalize to multiple args.
CS776 Prasad 5
History of Notation
• Whitehead & Russel (Principia Mathematica)
used the notation ˆ P to denote the set of x’s
such that P holds
• Church borrowed the notation but moved ˆ
down to create x E
• Which later turned into lx. E and the calculus
became known as lambda calculus
x
CS776 Prasad 6
Function Application
• The only thing that we can do with a function
is to apply it to an argument
• Church used the notation
E1 E2
to denote the application of function E1 to
actual argument E2
• All functions are applied to a single argument
CS776 Prasad 7
Why Study Lambda Calculus?
• l-calculus has had a tremendous influence on
the design and analysis of programming
languages
• Realistic languages are too large and complex
to study from scratch as a whole
• Typical approach is to modularize the study
into one feature at a time
– E.g., recursion, looping, exceptions, objects, etc.
• Then we assemble the features together
CS776 Prasad 8
Why Study Lambda Calculus?
• l-calculus is the standard testbed for studying
programming language features
– Because of its minimality
– Despite its syntactic simplicity the l-calculus can
easily encode:
• numbers, recursive data types, modules, imperative
features, exceptions, etc.
• Certain language features necessitate more
substantial extensions to l-calculus:
– for distributed & parallel languages: p-calculus
– for object oriented languages: -calculus
CS776 Prasad 9
Why Study Lambda Calculus?
“Whatever the next 700 languages turn out to
be, they will surely be variants of lambda
calculus.”
(Landin 1966)
CS776 Prasad 10
Syntax of Lambda Calculus
• Only three kinds of expressions
E ::= x variables
| E1 E2 function application
| lx. E function creation
• The form lx. E is also called lambda
abstraction, or simply abstraction
• E are called l-terms or l-expressions
CS776 Prasad 11
Examples of Lambda Expressions
• The identity function:
I =def lx. x
• A function that given an argument y discards
it and computes the identity function:
ly. (lx. x)
• A function that given a function f invokes it on
the identity function
lf. f (l x. x)
CS776 Prasad 12
Notational Conventions
• Application associates to the left
x y z parses as (x y) z
• Abstraction extends to the right
as far as possible
lx. x ly. x y z parses as
l x. (x (ly. ((x y) z)))
• And yields the the parse tree:
lx
app
x ly
z
app
app
x y
CS776 Prasad 13
Scope of Variables
• As in all languages with variables, it is
important to discuss the notion of scope
– Recall: the scope of an identifier is the portion of a
program where the identifier is accessible
• An abstraction lx. E binds variable x in E
– x is the newly introduced variable
– E is the scope of x
– we say x is bound in lx. E
– Just like formal function arguments are bound in
the function body
CS776 Prasad 14
Free and Bound Variables
• A variable is said to be free in E if it is not
bound in E
• We can define the free variables of an
expression E recursively as follows:
Free(x) = { x}
Free(E1 E2) = Free(E1)  Free(E2)
Free(lx. E) = Free(E) - { x }
• Example: Free(lx. x (ly. x y z)) = { z }
• Free variables are declared outside the term
CS776 Prasad 15
Free and Bound Variables (Cont.)
• Just like in any language with static nested
scoping, we have to worry about variable
shadowing
– An occurrence of a variable might refer to
different things in different context
• E.g., in Cool: let x  E in x + (let x  E’ in x) + x
• In l-calculus: lx. x (lx. x) x
CS776 Prasad 16
Renaming Bound Variables
• Two l-terms that can be obtained from each
other by a renaming of the bound variables are
considered identical
• Example: lx. x is identical to ly. y and to lz. z
• Intuition:
– by changing the name of a formal argument and of
all its occurrences in the function body, the behavior
of the function does not change
– in l-calculus such functions are considered identical
CS776 Prasad 17
Renaming Bound Variables (Cont.)
• Convention: we will always rename bound
variables so that they are all unique
– e.g., write l x. x (l y.y) x instead of l x. x (l x.x) x
• This makes it easy to see the scope of
bindings
• And also prevents serious confusion !
CS776 Prasad 18
Substitution
• The substitution of E’ for x in E (written
[E’/x]E )
– Step 1. Rename bound variables in E and E’ so they
are unique
– Step 2. Perform the textual substitution of E’ for x
in E
• Example: [y (lx. x) / x] ly. (lx. x) y x
– After renaming: [y (lv. v)/x] lz. (lu. u) z x
– After substitution: lz. (lu. u) z (y (lv. v))
CS776 Prasad 19
Evaluation of l-terms
• There is one key evaluation step in l-calculus:
the function application
(lx. E) E’ evaluates to [E’/x]E
• This is called b-reduction
• We write E b E’ to say that E’ is obtained
from E in one b-reduction step
• We write E *
b E’ if there are zero or more
steps
CS776 Prasad 20
Examples of Evaluation
• The identity function:
(lx. x) E  [E / x] x = E
• Another example with the identity:
(lf. f (lx. x)) (lx. x) 
[lx. x / f] f (lx. x)) = [(lx. x) / f] f (ly. y)) =
(lx. x) (ly. y) 
[ly. y /x] x = ly. y
• A non-terminating evaluation:
(lx. xx)(lx. xx) 
[lx. xx / x]xx = [ly. yy / x] xx = (ly. yy)(ly. yy)  …
CS776 Prasad 21
Functions with Multiple Arguments
• Consider that we extend the calculus with the
add primitive operation
• The l-term lx. ly. add x y can be used to add
two arguments E1 and E2:
(lx. ly. add x y) E1 E2 b
([E1/x] ly. add x y) E2 =
(ly. add E1 y) E2 b
[E2/y] add E1 y = add E1 E2
• The arguments are passed one at a time
CS776 Prasad 22
Functions with Multiple Arguments
• What is the result of (lx. ly. add x y) E ?
– It is ly. add E y
(A function that given a value E’ for y will compute
add E E’)
• The function lx. ly. E when applied to one
argument E’ computes the function ly. [E’/x]E
• This is one example of higher-order
computation
– We write a function whose result is another
function
CS776 Prasad 23
Evaluation and the Static Scope
• The definition of substitution guarantees that
evaluation respects static scoping:
(l x. (ly. y x)) (y (lx. x)) b lz. z (y (lv. v))
(y remains free, i.e., defined externally)
• If we forget to rename the bound y:
(l x. (ly. y x)) (y (lx. x)) *
b ly. y (y (lv. v))
(y was free before but is bound now)
CS776 Prasad 24
The Order of Evaluation
• In a l-term, there could be more than one
instance of (l x. E) E’
(l y. (l x. x) y) E
– could reduce the inner or the outer lambda
– which one should we pick?
(l y. (l x. x) y) E
(ly. [y/x] x) E = (ly. y) E [E/y] (lx. x) y =(lx. x) E
E
inner outer
CS776 Prasad 25
Order of Evaluation (Cont.)
• The Church-Rosser theorem says that any
order will compute the same result
– A result is a l-term that cannot be reduced
further
• But we might want to fix the order of
evaluation when we model a certain language
• In (typical) programming languages, we do not
reduce the bodies of functions (under a l)
– functions are considered values
CS776 Prasad 26
Call by Name
• Do not evaluate under a l
• Do not evaluate the argument prior to call
• Example:
(ly. (lx. x) y) ((lu. u) (lv. v)) bn
(lx. x) ((lu. u) (lv. v)) bn
(lu. u) (lv. v) bn
lv. v
CS776 Prasad 27
Call by Value
• Do not evaluate under l
• Evaluate an argument prior to call
• Example:
(ly. (lx. x) y) ((lu. u) (lv. v)) bv
(ly. (lx. x) y) (lv. v) bv
(lx. x) (lv. v) bv
lv. v
CS776 Prasad 28
Call by Name and Call by Value
• CBN
– difficult to implement
– order of side effects not predictable
• CBV:
– easy to implement efficiently
– might not terminate even if CBN might terminate
– Example: (lx. l z.z) ((ly. yy) (lu. uu))
• Outside the functional programming language
community, only CBV is used
CS776 Prasad 29
Lambda Calculus and Programming Languages
• Pure lambda calculus has only functions
• What if we want to compute with booleans,
numbers, lists, etc.?
• All these can be encoded in pure l-calculus
• The trick: do not encode what a value is but
what we can do with it!
• For each data type, we have to describe how it
can be used, as a function
– then we write that function in l-calculus
CS776 Prasad 30
Encoding Booleans in Lambda Calculus
• What can we do with a boolean?
– we can make a binary choice
• A boolean is a function that given two choices
selects one of them
– true =def lx. ly. x
– false =def lx. ly. y
– if E1 then E2 else E3 =def E1 E2 E3
• Example: if true then u else v is
(lx. ly. x) u v b (ly. u) v b u
CS776 Prasad 31
Encoding Pairs in Lambda Calculus
• What can we do with a pair?
– we can select one of its elements
• A pair is a function that given a boolean
returns the left or the right element
mkpair x y =def l b. x y
fst p =def p true
snd p =def p false
• Example:
fst (mkpair x y)  (mkpair x y) true  true x y  x
CS776 Prasad 32
Encoding Natural Numbers in Lambda Calculus
• What can we do with a natural number?
– we can iterate a number of times
• A natural number is a function that given an
operation f and a starting value s, applies f a
number of times to s:
0 =def lf. ls. s
1 =def lf. ls. f s
2 =def lf. ls. f (f s)
and so on
CS776 Prasad 33
Computing with Natural Numbers
• The successor function
succ n =def lf. ls. f (n f s)
• Addition
add n1 n2 =def n1 succ n2
• Multiplication
mult n1 n2 =def n1 (add n2) 0
• Testing equality with 0
iszero n =def n (lb. false) true
CS776 Prasad 34
Computing with Natural Numbers. Example
mult 2 2 
2 (add 2) 0 
(add 2) ((add 2) 0) 
2 succ (add 2 0) 
2 succ (2 succ 0) 
succ (succ (succ (succ 0))) 
succ (succ (succ (lf. ls. f (0 f s)))) 
succ (succ (succ (lf. ls. f s))) 
succ (succ (lg. ly. g ((lf. ls. f s) g y)))
succ (succ (lg. ly. g (g y))) * lg. ly. g (g (g (g y))) = 4
CS776 Prasad 35
Computing with Natural Numbers. Example
• What is the result of the application add 0 ?
(ln1. ln2. n1 succ n2) 0 b
ln2. 0 succ n2 =
ln2. (lf. ls. s) succ n2 b
ln2. n2 =
lx. x
• By computing with functions, we can express
some optimizations
CS776 Prasad 36
Expressiveness of Lambda Calculus
• The l-calculus can express
– data types (integers, booleans, lists, trees, etc.)
– branching (using booleans)
– recursion
• This is enough to encode Turing machines
• Encodings are fun
• But programming in pure l-calculus is painful
– we will add constants (0, 1, 2, …, true, false, if-
then-else, etc.)
– and we will add types

More Related Content

PPTX
Lambda Calculus
PPTX
Context free grammar
PPTX
Semantic analysis
PPTX
#1 formal methods – introduction for software engineering
PPTX
CFG to CNF
PPT
User defined data type
PPTX
Church Turing Thesis
PPT
Non regular languages
Lambda Calculus
Context free grammar
Semantic analysis
#1 formal methods – introduction for software engineering
CFG to CNF
User defined data type
Church Turing Thesis
Non regular languages

What's hot (20)

PPT
Air Cargo transport
PPTX
If statements in c programming
PPSX
Type conversion
PPTX
Candidate elimination algorithm in ML Lab
PPTX
Lambda calculus
PPTX
Control Flow Statements
PPTX
Pumping lemma for regular language
PDF
Introduction to OpenMP
PDF
Chapter 11d coordination agreement
PPTX
Seminar on MATLAB
PPTX
Context free grammar
PPTX
Alpha-beta pruning (Artificial Intelligence)
PDF
Daa notes 3
PDF
Operator precedence
PPT
Lecture 8
PPTX
Solution of N Queens Problem genetic algorithm
PPTX
Syntax Analysis in Compiler Design
PPTX
While , For , Do-While Loop
PDF
itft-Decision making and branching in java
Air Cargo transport
If statements in c programming
Type conversion
Candidate elimination algorithm in ML Lab
Lambda calculus
Control Flow Statements
Pumping lemma for regular language
Introduction to OpenMP
Chapter 11d coordination agreement
Seminar on MATLAB
Context free grammar
Alpha-beta pruning (Artificial Intelligence)
Daa notes 3
Operator precedence
Lecture 8
Solution of N Queens Problem genetic algorithm
Syntax Analysis in Compiler Design
While , For , Do-While Loop
itft-Decision making and branching in java
Ad

Similar to Introduction to lambda calculus (20)

PPTX
Funtional Programming
PDF
Lambda? You Keep Using that Letter
PPTX
Principles of programming language intro
PPTX
8. Functional Programming_updated(1).pptx
PPTX
Chapter 3.pptx
PDF
Functional Programming #FTW
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
PDF
201801 CSE240 Lecture 17
PPTX
CS152 Programming Paradigm
PPT
INTRODUCTION TO LISP
PPTX
Introduction to programming language using c
PPTX
Functional Programming Concepts for Imperative Programmers
PPT
Expressions in c++
PDF
15 functional programming
PDF
15 functional programming
PPTX
LISP:Program structure in lisp
PPTX
LISP: Program structure in lisp
PPTX
Formal methods 5 - Pi calculus
KEY
Functional programming in clojure
PPTX
Plc part 3
Funtional Programming
Lambda? You Keep Using that Letter
Principles of programming language intro
8. Functional Programming_updated(1).pptx
Chapter 3.pptx
Functional Programming #FTW
[FLOLAC'14][scm] Functional Programming Using Haskell
201801 CSE240 Lecture 17
CS152 Programming Paradigm
INTRODUCTION TO LISP
Introduction to programming language using c
Functional Programming Concepts for Imperative Programmers
Expressions in c++
15 functional programming
15 functional programming
LISP:Program structure in lisp
LISP: Program structure in lisp
Formal methods 5 - Pi calculus
Functional programming in clojure
Plc part 3
Ad

More from Afaq Siddiqui (9)

PPT
Introduction of storage devices(Brief Knowledge)
PPT
Components of System Unit
PPTX
Use Of Calculus In Programming
PPTX
Why We Need To Study Calculus
PPTX
Introduction of calculus in programming
PPTX
Wireless Personal Area Network(WPAN)
PPTX
WPAN According To ZIGBEE
PPTX
Job analysis
PPTX
World wide web with multimedia
Introduction of storage devices(Brief Knowledge)
Components of System Unit
Use Of Calculus In Programming
Why We Need To Study Calculus
Introduction of calculus in programming
Wireless Personal Area Network(WPAN)
WPAN According To ZIGBEE
Job analysis
World wide web with multimedia

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PPTX
Pharma ospi slides which help in ospi learning
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Structure & Organelles in detailed.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
Pharma ospi slides which help in ospi learning
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
GDM (1) (1).pptx small presentation for students
Cell Structure & Organelles in detailed.
102 student loan defaulters named and shamed – Is someone you know on the list?
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Renaissance Architecture: A Journey from Faith to Humanism
FourierSeries-QuestionsWithAnswers(Part-A).pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis

Introduction to lambda calculus

  • 1. CS776 Prasad 1 Foundations of Programming Languages: Introduction to Lambda Calculus
  • 2. CS776 Prasad 2 Lecture Outline • Why study lambda calculus? • Lambda calculus – Syntax – Evaluation – Relationship to programming languages • Next time: type systems for lambda calculus
  • 3. CS776 Prasad 3 Lambda Calculus. History. • A framework developed in 1930s by Alonzo Church to study computations with functions • Church wanted a minimal notation – to expose only what is essential • Two operations with functions are essential: – function creation – function application
  • 4. CS776 Prasad 4 Function Creation • Church introduced the notation lx. E to denote a function with formal argument x and with body E • Functions do not have names – names are not essential for the computation • Functions have a single argument – once we understand how functions with one argument work we can generalize to multiple args.
  • 5. CS776 Prasad 5 History of Notation • Whitehead & Russel (Principia Mathematica) used the notation ˆ P to denote the set of x’s such that P holds • Church borrowed the notation but moved ˆ down to create x E • Which later turned into lx. E and the calculus became known as lambda calculus x
  • 6. CS776 Prasad 6 Function Application • The only thing that we can do with a function is to apply it to an argument • Church used the notation E1 E2 to denote the application of function E1 to actual argument E2 • All functions are applied to a single argument
  • 7. CS776 Prasad 7 Why Study Lambda Calculus? • l-calculus has had a tremendous influence on the design and analysis of programming languages • Realistic languages are too large and complex to study from scratch as a whole • Typical approach is to modularize the study into one feature at a time – E.g., recursion, looping, exceptions, objects, etc. • Then we assemble the features together
  • 8. CS776 Prasad 8 Why Study Lambda Calculus? • l-calculus is the standard testbed for studying programming language features – Because of its minimality – Despite its syntactic simplicity the l-calculus can easily encode: • numbers, recursive data types, modules, imperative features, exceptions, etc. • Certain language features necessitate more substantial extensions to l-calculus: – for distributed & parallel languages: p-calculus – for object oriented languages: -calculus
  • 9. CS776 Prasad 9 Why Study Lambda Calculus? “Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus.” (Landin 1966)
  • 10. CS776 Prasad 10 Syntax of Lambda Calculus • Only three kinds of expressions E ::= x variables | E1 E2 function application | lx. E function creation • The form lx. E is also called lambda abstraction, or simply abstraction • E are called l-terms or l-expressions
  • 11. CS776 Prasad 11 Examples of Lambda Expressions • The identity function: I =def lx. x • A function that given an argument y discards it and computes the identity function: ly. (lx. x) • A function that given a function f invokes it on the identity function lf. f (l x. x)
  • 12. CS776 Prasad 12 Notational Conventions • Application associates to the left x y z parses as (x y) z • Abstraction extends to the right as far as possible lx. x ly. x y z parses as l x. (x (ly. ((x y) z))) • And yields the the parse tree: lx app x ly z app app x y
  • 13. CS776 Prasad 13 Scope of Variables • As in all languages with variables, it is important to discuss the notion of scope – Recall: the scope of an identifier is the portion of a program where the identifier is accessible • An abstraction lx. E binds variable x in E – x is the newly introduced variable – E is the scope of x – we say x is bound in lx. E – Just like formal function arguments are bound in the function body
  • 14. CS776 Prasad 14 Free and Bound Variables • A variable is said to be free in E if it is not bound in E • We can define the free variables of an expression E recursively as follows: Free(x) = { x} Free(E1 E2) = Free(E1)  Free(E2) Free(lx. E) = Free(E) - { x } • Example: Free(lx. x (ly. x y z)) = { z } • Free variables are declared outside the term
  • 15. CS776 Prasad 15 Free and Bound Variables (Cont.) • Just like in any language with static nested scoping, we have to worry about variable shadowing – An occurrence of a variable might refer to different things in different context • E.g., in Cool: let x  E in x + (let x  E’ in x) + x • In l-calculus: lx. x (lx. x) x
  • 16. CS776 Prasad 16 Renaming Bound Variables • Two l-terms that can be obtained from each other by a renaming of the bound variables are considered identical • Example: lx. x is identical to ly. y and to lz. z • Intuition: – by changing the name of a formal argument and of all its occurrences in the function body, the behavior of the function does not change – in l-calculus such functions are considered identical
  • 17. CS776 Prasad 17 Renaming Bound Variables (Cont.) • Convention: we will always rename bound variables so that they are all unique – e.g., write l x. x (l y.y) x instead of l x. x (l x.x) x • This makes it easy to see the scope of bindings • And also prevents serious confusion !
  • 18. CS776 Prasad 18 Substitution • The substitution of E’ for x in E (written [E’/x]E ) – Step 1. Rename bound variables in E and E’ so they are unique – Step 2. Perform the textual substitution of E’ for x in E • Example: [y (lx. x) / x] ly. (lx. x) y x – After renaming: [y (lv. v)/x] lz. (lu. u) z x – After substitution: lz. (lu. u) z (y (lv. v))
  • 19. CS776 Prasad 19 Evaluation of l-terms • There is one key evaluation step in l-calculus: the function application (lx. E) E’ evaluates to [E’/x]E • This is called b-reduction • We write E b E’ to say that E’ is obtained from E in one b-reduction step • We write E * b E’ if there are zero or more steps
  • 20. CS776 Prasad 20 Examples of Evaluation • The identity function: (lx. x) E  [E / x] x = E • Another example with the identity: (lf. f (lx. x)) (lx. x)  [lx. x / f] f (lx. x)) = [(lx. x) / f] f (ly. y)) = (lx. x) (ly. y)  [ly. y /x] x = ly. y • A non-terminating evaluation: (lx. xx)(lx. xx)  [lx. xx / x]xx = [ly. yy / x] xx = (ly. yy)(ly. yy)  …
  • 21. CS776 Prasad 21 Functions with Multiple Arguments • Consider that we extend the calculus with the add primitive operation • The l-term lx. ly. add x y can be used to add two arguments E1 and E2: (lx. ly. add x y) E1 E2 b ([E1/x] ly. add x y) E2 = (ly. add E1 y) E2 b [E2/y] add E1 y = add E1 E2 • The arguments are passed one at a time
  • 22. CS776 Prasad 22 Functions with Multiple Arguments • What is the result of (lx. ly. add x y) E ? – It is ly. add E y (A function that given a value E’ for y will compute add E E’) • The function lx. ly. E when applied to one argument E’ computes the function ly. [E’/x]E • This is one example of higher-order computation – We write a function whose result is another function
  • 23. CS776 Prasad 23 Evaluation and the Static Scope • The definition of substitution guarantees that evaluation respects static scoping: (l x. (ly. y x)) (y (lx. x)) b lz. z (y (lv. v)) (y remains free, i.e., defined externally) • If we forget to rename the bound y: (l x. (ly. y x)) (y (lx. x)) * b ly. y (y (lv. v)) (y was free before but is bound now)
  • 24. CS776 Prasad 24 The Order of Evaluation • In a l-term, there could be more than one instance of (l x. E) E’ (l y. (l x. x) y) E – could reduce the inner or the outer lambda – which one should we pick? (l y. (l x. x) y) E (ly. [y/x] x) E = (ly. y) E [E/y] (lx. x) y =(lx. x) E E inner outer
  • 25. CS776 Prasad 25 Order of Evaluation (Cont.) • The Church-Rosser theorem says that any order will compute the same result – A result is a l-term that cannot be reduced further • But we might want to fix the order of evaluation when we model a certain language • In (typical) programming languages, we do not reduce the bodies of functions (under a l) – functions are considered values
  • 26. CS776 Prasad 26 Call by Name • Do not evaluate under a l • Do not evaluate the argument prior to call • Example: (ly. (lx. x) y) ((lu. u) (lv. v)) bn (lx. x) ((lu. u) (lv. v)) bn (lu. u) (lv. v) bn lv. v
  • 27. CS776 Prasad 27 Call by Value • Do not evaluate under l • Evaluate an argument prior to call • Example: (ly. (lx. x) y) ((lu. u) (lv. v)) bv (ly. (lx. x) y) (lv. v) bv (lx. x) (lv. v) bv lv. v
  • 28. CS776 Prasad 28 Call by Name and Call by Value • CBN – difficult to implement – order of side effects not predictable • CBV: – easy to implement efficiently – might not terminate even if CBN might terminate – Example: (lx. l z.z) ((ly. yy) (lu. uu)) • Outside the functional programming language community, only CBV is used
  • 29. CS776 Prasad 29 Lambda Calculus and Programming Languages • Pure lambda calculus has only functions • What if we want to compute with booleans, numbers, lists, etc.? • All these can be encoded in pure l-calculus • The trick: do not encode what a value is but what we can do with it! • For each data type, we have to describe how it can be used, as a function – then we write that function in l-calculus
  • 30. CS776 Prasad 30 Encoding Booleans in Lambda Calculus • What can we do with a boolean? – we can make a binary choice • A boolean is a function that given two choices selects one of them – true =def lx. ly. x – false =def lx. ly. y – if E1 then E2 else E3 =def E1 E2 E3 • Example: if true then u else v is (lx. ly. x) u v b (ly. u) v b u
  • 31. CS776 Prasad 31 Encoding Pairs in Lambda Calculus • What can we do with a pair? – we can select one of its elements • A pair is a function that given a boolean returns the left or the right element mkpair x y =def l b. x y fst p =def p true snd p =def p false • Example: fst (mkpair x y)  (mkpair x y) true  true x y  x
  • 32. CS776 Prasad 32 Encoding Natural Numbers in Lambda Calculus • What can we do with a natural number? – we can iterate a number of times • A natural number is a function that given an operation f and a starting value s, applies f a number of times to s: 0 =def lf. ls. s 1 =def lf. ls. f s 2 =def lf. ls. f (f s) and so on
  • 33. CS776 Prasad 33 Computing with Natural Numbers • The successor function succ n =def lf. ls. f (n f s) • Addition add n1 n2 =def n1 succ n2 • Multiplication mult n1 n2 =def n1 (add n2) 0 • Testing equality with 0 iszero n =def n (lb. false) true
  • 34. CS776 Prasad 34 Computing with Natural Numbers. Example mult 2 2  2 (add 2) 0  (add 2) ((add 2) 0)  2 succ (add 2 0)  2 succ (2 succ 0)  succ (succ (succ (succ 0)))  succ (succ (succ (lf. ls. f (0 f s))))  succ (succ (succ (lf. ls. f s)))  succ (succ (lg. ly. g ((lf. ls. f s) g y))) succ (succ (lg. ly. g (g y))) * lg. ly. g (g (g (g y))) = 4
  • 35. CS776 Prasad 35 Computing with Natural Numbers. Example • What is the result of the application add 0 ? (ln1. ln2. n1 succ n2) 0 b ln2. 0 succ n2 = ln2. (lf. ls. s) succ n2 b ln2. n2 = lx. x • By computing with functions, we can express some optimizations
  • 36. CS776 Prasad 36 Expressiveness of Lambda Calculus • The l-calculus can express – data types (integers, booleans, lists, trees, etc.) – branching (using booleans) – recursion • This is enough to encode Turing machines • Encodings are fun • But programming in pure l-calculus is painful – we will add constants (0, 1, 2, …, true, false, if- then-else, etc.) – and we will add types