Functional Programming
Kaushiki Upadhyaya
Topics to be covered in the module
⩥ Introduction to lambda calculus
⩥ Functional Programming Concepts
⊳ First-class function values and higher-order functions
⊳ Extensive polymorphism
⊳ List types and operators
⊳ Structured function returns
⊳ Constructors (aggregates) for structured objects
⊳ Garbage collection
⩥ Evaluation order
⊳ Strictness
⊳ Lazy evaluation
⩥ Higher order Functions
⊳ Currying
⩥ I/O streams
⩥ Monads
Mathematical Notation
Ordered pair
λ-calculus
⩥ The λ-calculus is an elegant notation for working
with applications of functions to arguments
⩥ X2 −2⋅x+5
⩥ What is the value of this expression when x=2?
⩥ 22−2⋅2+5, which we can further reduce to get the
answer 5.
⩥ The terminology in λ-calculus is that we want to apply this expression to
an argument, and get a value
β-reduction
⩥ (λx[x2−2⋅x+5])2 ⊳ 22−2⋅2+5⟨Substitute 2 for x ⟩
⩥ =4−4+5⟨Arithmetic⟩
⩥ =5⟨Arithmetic⟩
Lambda Calculus
⩥ Language to express function application
⊳ Ability to define anonymous functions
⊳ Ability to "apply" functions
⩥ Functional programming derives from lambda
calculus
⊳ Scheme
⊳ ML
⊳ Haskell
⊳ F#
Adam Doupé, Principles of Programming Languages
Syntax
⩥ Everything in lambda calculus is an expression
(E)
E → ID
E → λ ID . E
E→EE
E → (E)
Adam Doupé, Principles of Programming Languages
Examples
E → ID
E → λ ID . E
E→EE
E → (E)
x
λx.x
xy
λλx.y
λx.yz
foo λ bar . (foo (bar bar))
Adam Doupé, Principles of Programming Languages 9
Ambiguous Syntax
⩥ How to parse
xyz
Exp Exp
Exp z x Exp
x y y z
Adam Doupé, Principles of Programming Languages 10
Ambiguous Syntax
⩥ How to parse
λx.xy
Exp Exp
λ Exp Exp
x Exp
y
Exp Exp λ x Exp
x y x
Adam Doupé, Principles of Programming Languages 11
Disambiguation Rules
⩥ E → E E is left associative
⊳ x y z is
■ (x y) z
⊳ w x y z is
■ ((w x) y) z
⩥ λ ID . E extends as far to the right as possible, starting with
the λ ID .
⊳ λ x . x y is
■ λ x . (x y)
⊳ λ x . λ x . x is
■ λ x. ( λ x . x)
Adam Doupé, Principles of Programming Languages 12
Examples
⩥ (λ x . y) x is the same as λ x . y x
⊳ No!
⩥ λ x . (x) y is the same as
⊳ λ x . ((x) y)
⩥λa.λb.λc.abc
⊳ λ a . (λ b . (λ c . ((a b) c)))
Adam Doupé, Principles of Programming Languages 13
Semantics
⩥ Every ID that we see in lambda calculus is called a
variable
⩥ E → λ ID . E is called an abstraction
⊳ The ID is the variable of the abstraction (also
metavariable)
⊳ E is called the body of the abstraction
⩥E→EE
⊳ This is called an application
Adam Doupé, Principles of Programming Languages 14
Semantics
⩥ λ ID . E defines a new anonymous function
⊳ This is the reason why anonymous functions
are called "Lambda Expressions" in Java 8
(and other languages)
⊳ ID is the formal parameter of the function
⊳ Body is the body of the function
⩥ E → E1 E2, function application, is similar to
calling function E1 and setting its formal
parameter to be E2
Adam Doupé, Principles of Programming Languages 15
Example
⩥ Assume that we have the function + defined and the
constant 1
⩥λx.+x1
⊳ Represents a function that adds one to its argument
⩥ (λ x . + x 1) 2
⊳ Represents calling the original function by
supplying 2 for x and it would "reduce" to (+ 2 1)
=3
⩥ How can + function be defined if abstractions only
accept 1 parameter?
Adam Doupé, Principles of Programming Languages 16
Currying
⩥ Technique to translate the evaluation of a function that
takes multiple arguments into a sequence of functions that
each take a single argument
⩥ Define adding two parameters together with functions that
only take one parameter:
⊳ λ x . λ y . ((+ x) y)
⊳ (λ x . λ y . ((+ x) y)) 1
■ λ y . ((+ 1) y)
⊳ (λ x . λ y . ((+ x) y)) 10 20
■ (λ y . ((+ 10) y)) 20
■ ((+ 10) 20) = 30
Adam Doupé, Principles of Programming Languages 17
Free Variables
⩥ A variable is free if it does not appear within the
body of an abstraction with a metavariable of the
same name
⩥ x free in λ x . x y z?
⩥ y free in λ x . x y z?
⩥ x free in (λ x . (+ x 1)) x?
⩥ z free in λ x . λ y . λ z . z y x?
Adam Doupé, Principles of Programming Languages 18
Free Variables
⩥ x is free in E if:
⊳ E=x
⊳ E = λ y . E1, where y != x and x is free in E1
⊳ E = E1 E2, where x is free in E1
⊳ E = E1 E2, where x is free in E2 and every
occurrence of
Adam Doupé, Principles of Programming Languages 19
Examples
⩥ x free in x λ x . x ?
⩥ x free in (λ x . x y) x ?
⩥ x free in λ x . y x ?
Adam Doupé, Principles of Programming Languages 20
Combinators
⩥ An expression is a combinator if it does not have
any free variables
⩥ λ x . λ y . x y x combinator?
⩥ λ x . x combinator?
⩥ λ z . λ x . x y z combinator?
Adam Doupé, Principles of Programming Languages 21
Bound Variables
⩥ If a variable is not free, it is bound
⩥ Bound by what abstraction?
⊳ What is the scope of a metavariable?
Adam Doupé, Principles of Programming Languages 22
Bound Variable Rules
⩥ If an occurrence of x is free in E, then it is bound by
λ x . in λ x . E
⩥ If an occurrence of x is bound by a particular λ x . in
E, then x is bound by the same λ x . in λ z . E
⊳ Even if z == x
⊳ λx.λx. x
■ Which lambda expression binds x?
⩥ If an occurrence of x is bound by a particular λ x . in
E1, then that occurrence in E1 is tied by the same
abstraction λ x . in E1 E2 and E2 E1
Adam Doupé, Principles of Programming Languages 23
Examples
⩥ (λ x . x (λ y . x y z y) x) x y
⊳ (λ x . x (λ y . x y z y) x) x y
⩥ (λ x . λ y . x y) (λ z . x z)
⊳ (λ x . λ y . x y) (λ z . x z)
⩥ (λ x . x λ x . z x)
⊳ (λ x . x λ x . z x)
Adam Doupé, Principles of Programming Languages 24