You are on page 1of 79

Module M02

Partha Pratim
Das

Relations
Principles of Programming Languages
Functions Module M02: λ-Calculus: Syntax
Composition
Currying

λ-Calculus
Concept of λ

Syntax
Partha Pratim Das
λ-expressions
Notation
Examples
Department of Computer Science and Engineering
Simple
Indian Institute of Technology, Kharagpur
Composition
Boolean ppd@cse.iitkgp.ac.in
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11 January 05, 10, 12 & 17, 2022
Principles of Programming Languages Partha Pratim Das M02.1
Table of Contents

Module M02 1 Relations


Partha Pratim
Das
2 Functions
Composition
Relations

Functions
Currying
Composition 3 λ-Calculus
Currying

λ-Calculus
Concept of λ
Concept of λ 4 Syntax
Syntax
λ-expressions
λ-expressions
Notation Notation
Examples
Simple
Examples
Composition Simple
Boolean Composition
Numerals
Boolean
Recursion
Multi-variable
Numerals
Functions Recursion
Higher Order
Functions Multi-variable Functions
C++11 Higher Order Functions
C++11
Principles of Programming Languages Partha Pratim Das M02.2
Relations

Module M02

Partha Pratim
Das

Relations

Functions
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Relations
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.3


Relations

Module M02

Partha Pratim • r is a relation between two sets A and B:


Das
r ⊆ A × B or r = {(u, v ) : u ∈ A, v ∈ B}
Relations

Functions
• Set of relations between A and B is 2A×B , where 2X is the power set of a set X
Composition
Currying
• If A = B, r is said to be a relation over A
λ-Calculus • r is
Concept of λ

Syntax ◦ Reflexive: ∀t ∈ A ⇒ (t, t) ∈ r


λ-expressions ◦ Symmetric: ∀u, v ∈ A : (u, v ) ∈ r ⇒ (v , u) ∈ r
Notation
Examples ◦ Transitive: ∀u, v , w ∈ A : (u, v ), (v , w ) ∈ r ⇒ (u, w ) ∈ r
Simple
Composition
◦ Antisymmetric: ∀u, v ∈ A : (u, v ), (v , u) ∈ r ⇒ u = v
Boolean ◦ Equivalence relation: Reflexive, Symmetric, and Transitive
Numerals
Recursion • A relation r may be n-ary over sets A1 , A2 , · · · , An
Multi-variable
Functions
Higher Order
r ⊆ A1 × A2 × · · · × An
Functions
C++11 • An n-ary relation may be decomposed into a number of binary relations
Principles of Programming Languages Partha Pratim Das M02.4
Functions

Module M02

Partha Pratim
Das

Relations

Functions
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Functions
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.5


Functions

Module M02
• f : A → B is a function from A to B if
Partha Pratim
Das ◦ f is a relation between A and B (that is, f ∈ A × B), and
Relations
◦ ∀(s1 , s2 ), (t1 , t2 ) ∈ f , s1 = t1 ⇒ s2 = t2
Functions • f is total if ∀u ∈ A, ∃(u, v ) ∈ f
Composition
Currying • f is partial, otherwise
λ-Calculus
Concept of λ
• Set of functions from A to B is B A ⊂ 2A×B
Syntax • A is the domain, B is the codomain or range
λ-expressions
Notation • Image f (A) of f is {v : ∀u ∈ A, f (u) = v }
Examples
Simple • A total function f is
Composition
Boolean ◦ Injective (one-to-one): ∀u, v ∈ A, f (u) = f (v ) ⇒ u = v
Numerals
Recursion ◦ Surjective (onto): f (A) = B
Multi-variable
Functions
◦ Bijective (one-to-one and onto): Injective and Surjective
• f −1 = {(v , u) : (u, v ) ∈ f } is the inverse of f .
Higher Order
Functions
C++11

• f −1 is a function iff f is a bijection; relation otherwise


Principles of Programming Languages Partha Pratim Das M02.6
Function Compositions

Module M02

Partha Pratim • Given the mathematical functions:


Das

Relations f (x) = x 2 , g (x) = x + 1


Functions
Composition
Currying
f ◦ g is the composition of f and g :
λ-Calculus
Concept of λ (f ◦ g )(x) = f (g (x))
Syntax
λ-expressions
Notation
(f ◦ g )(x) = f (g (x)) = f (x + 1) = (x + 1)2 = x 2 + 2x + 1
Examples
Simple (g ◦ f )(x) = g (f (x)) = g (x 2 ) = x 2 + 1
Composition
Boolean
Numerals
• Function composition, therefore, is not commutative: (f ◦ g )(x) ̸= (g ◦ f )(x)
Recursion
Multi-variable
• Function composition can be regarded as a (higher-order) function with the following
Functions
Higher Order
type:
Functions
C++11
◦ : (Z → Z ) × (Z → Z ) → (Z → Z )

Principles of Programming Languages Partha Pratim Das M02.7


Function Compositions: f ◦ g ̸= g ◦ f

Module M02

Partha Pratim
Das

Relations

Functions
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.8


Curried Functions

Module M02
• Using currying1 , one-variable functions can represent multiple-variable functions
Partha Pratim
Das • Consider:
Relations h(x, y ) = x + y of type h : Z × Z → Z
Functions
Composition
• Represent h as h of type2 c

Currying
hc : Z → Z → Z or hc : Z → (Z → Z ) or hc : Z → Z Z
λ-Calculus
Concept of λ such that
Syntax h(x, y ) = hc (x)(y ) = x + y
λ-expressions

◦ For example, hc (2) = g , where g (y ) = 2 + y


Notation
Examples

• hc is the curried version of h.


Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
1 Haskell
Curry used this mechanism in the study of functions. Incidentally, Moses Schönfinkel developed
Functions
C++11
currying before Curry
2 → associates to right

Principles of Programming Languages Partha Pratim Das M02.9


λ-Calculus

Module M02

Partha Pratim
Das

Relations

Functions
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
λ-Calculus
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.10


λ-Calculus

Module M02

Partha Pratim • Developed by Alonzo Church and his doctoral student Stephen Cole Kleene in the 1930
Das
• Can represents all computable functions
Relations

Functions
• Has equal power as of Turing Machine
Composition
Currying

λ-Calculus
Concept of λ

Syntax
Source: λ- Calculus Overview
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.11


Importance of λ-Calculus

Module M02

Partha Pratim • Uncomplicated syntax and semantics provide an excellent vehicle for studying the
Das
meaning of programming language concepts
Relations
• All functional programming languages can be viewed as syntactic variations of the
Functions
Composition λ-calculus
Currying

λ-Calculus
• Denotational semantics is based on the λ-calculus and expresses its definitions using the
Concept of λ higher-order functions of the λ-calculus
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.12


Concept of λ

Module M02

Partha Pratim • A function is a mapping from the elements of a domain set to the elements of a
Das
codomain set given by a rule
Relations
• Example,
Functions
Composition
Currying
cube : Integer → Integer
λ-Calculus
Concept of λ
where
Syntax
λ-expressions
cube(n) = n3
Notation
Examples • Questions:
Simple
Composition ◦ What is the value of the identifier cube?
Boolean
Numerals
◦ How can we represent the object bound to cube?
Recursion ◦ Can this function be defined without giving it a name?
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.13


Concept of λ

Module M02

Partha Pratim • λ-notation for an anonymous function:


Das

Relations λn. n3
Functions
Composition defines the function that maps each n in the domain to n3
Currying

λ-Calculus • Expression represented by


Concept of λ
λn. n3
Syntax
λ-expressions
Notation
is the value bound to the identifier cube
Examples
Simple
• To represent the function evaluation cube(2) = 8, we use the following λ-calculus
Composition syntax:
(λn. n3 2) ⇒ 23 ⇒ 8
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.14


Concept of λ: Parallel in C / C++

Module M02
• Function Abstraction
Partha Pratim
Das ◦ Mathematical Notation: cube : Integer → Integer
Relations
cube(n) = n3
Functions
n ⊢ n3
Composition ◦ λ-notation:
λn. n3 = λn. (n ∗ n ∗ n)
Currying

λ-Calculus
Concept of λ ◦ C Function:
Syntax int cube(int n) { return n * n * n; }
λ-expressions
Notation
Examples
Simple • Function Application
Composition
Boolean ◦ Mathematical Notation: cube(2) = 8
Numerals
Recursion
◦ λ-notation:
Multi-variable
Functions
(λn. n3 ) 2 ≡ 2 ∗ 2 ∗ 2 = 8
Higher Order
Functions
◦ C Function:
C++11 int n_cube = cube(2);
Principles of Programming Languages Partha Pratim Das M02.15
Concept of λ

Module M02

Partha Pratim • The number and order of the parameters to the function are specified between the λ
Das
symbol and an expression
Relations
• Example: Expression
Functions
Composition n2 + m
Currying

λ-Calculus is ambiguous as the definition of a function rule:


Concept of λ

Syntax
(3, 4) ⊢ 32 + 4 = 13
λ-expressions
Notation
Examples
or
Simple
Composition (3, 4) ⊢ 42 + 3 = 19
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.16


Concept of λ

Module M02

Partha Pratim • λ-notation resolves the ambiguity by specifying the order of the parameters:
Das
λn. λm. n2 + m, that is, (3, 4) ⊢ 32 + 4 = 13
Relations

Functions
λm. λn. n2 + m, that is, (3, 4) ⊢ 42 + 3 = 19
Composition
Currying
Notationally (by left-to-right order 3 binds to n and 4 binds to m):
λ-Calculus (λn. λm. (n2 + m) 3 4) = (λm. (32 + m) 4) = (λm. (9 + m) 4) = (9 + 4) = 13
Concept of λ

Syntax
• Most functional programming languages allow anonymous functions as values
λ-expressions
Notation
• Example: The function λn.n3 is represented as
Examples
◦ Scheme: (lambda (n)(∗ n n n))
Simple
Composition ◦ Standard ML: fn n ⇒ n ∗ n ∗ n
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.17


Concept of λ: Parallel in C / C++

Module M02
• Function Abstraction
Partha Pratim
Das ◦ Mathematical2 Notation: f , g : Integer × Integer → Integer
f (n, m) = n + m
Relations g (m, n) = n2 + m
Functions
◦ λ-notation:2
Composition
λn. λm. n + m
Currying λm. λn. n2 + m
λ-Calculus
◦ C Function:
Concept of λ
int f(int n, int m) { return n * n + m; }
int g(int m, int n) { return n * n + m; }
Syntax
λ-expressions
Notation • Function Application
Examples ◦ Mathematical Notation:
Simple f (3, 4) = 13
Composition g (3, 4) = 19
Boolean
◦ λ-notation: 2
Numerals
(λn. λm. n + m) 3 4 = 32 + 4 = 13
Recursion
Multi-variable
(λm. λn. n2 + m) 3 4 = 42 + 3 = 19
Functions ◦ C Function:
Higher Order
Functions
int r_f = f(3, 4); // 13
C++11 int r_g = g(3, 4); // 19

Principles of Programming Languages Partha Pratim Das M02.18


Syntax of λ-Calculus

Module M02

Partha Pratim
Das

Relations

Functions
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Syntax of λ-Calculus
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.19


Syntax of λ Calculus

Module M02

Partha Pratim
λ-expressions come in four varieties:
Das
• Variables
Relations
◦ Usually, lowercase letters
Functions
Composition • Predefined Constants
Currying

λ-Calculus
◦ Act as values and operations
Concept of λ ◦ Allowed in an impure or applied λ-calculus
Syntax
λ-expressions • Function Applications
Notation
Examples
◦ Combinations
Simple
Composition
• λ-Abstractions
Boolean
Numerals
◦ Function definitions
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.20


Syntax of λ Calculus

Module M02

Partha Pratim
BNF Syntax of λ-Calculus:
Das

Relations < expression > ::= < variable > ; lowercase identifiers
Functions | < constant > ; predefined objects
Composition
Currying
| (< expression >< expression >) ; combinations
λ-Calculus | (λ < variable > . < expression >) ; abstractions
Concept of λ In short:
Syntax
λ-expressions
Notation
e ::= v ; variables / constants
Examples
Simple
| (e e) ; function application
Composition | (λv .e) ; function abstractions
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.21


Syntax of λ Calculus

Module M02

Partha Pratim • Identifiers of more than one letter may stand as variables and constants
Das
• Pure λ-calculus
Relations

Functions
◦ has no predefined constants, but
Composition ◦ it still allows the definition of all of the common constants and functions of
Currying
arithmetic and list manipulation
λ-Calculus
Concept of λ • Predefined constants
Syntax
λ-expressions ◦ Numerals (for example, 34),
Notation
Examples
◦ add (addition), mul (multiplication), succ (successor function), and sqr (squaring
Simple function)
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.22


Syntax of λ Calculus

Module M02 • For a list in Lisp


Partha Pratim head or car 3 returns the first item of the list it is called on
Das
tail or cdr 4 returns a new list consisting of all but the first item
Relations of the list it is called on
Functions
Composition
cons takes an argument and returns a new list whose head
Currying is the argument and whose tail is the list it is called
λ-Calculus on
Concept of λ

Syntax
isEmpty returns true if the list it is called on is the empty
λ-expressions list, returns false otherwise
Notation
Examples • (cons y nil) = (y )
Simple
Composition • (cons x (y )) = (x y )
Boolean
Numerals • (car (cons x y )) = x
Recursion
Multi-variable
Functions
• (cdr (cons x y )) = (y )
Higher Order
Functions 3 Contents
C++11
of the Address part of Register number
4 Contents of the Decrement part of Register number
Principles of Programming Languages Partha Pratim Das M02.23
Free and Bound Variable

Module M02

Partha Pratim • In an abstraction, the variable named is referred to as the bound variable and the
Das
associated λ-expression is the body of the abstraction
Relations
• In an expression of the form:
Functions
Composition λv . e
Currying

λ-Calculus occurrences of variable v in expression e are bound


Concept of λ

Syntax
• All occurrences of other variables are free
λ-expressions
Notation
• Example:
Examples
Simple
Composition
((λx. λy . (xy ))(yw ))
Boolean
Numerals ◦ x, and y are bound in first part
Recursion
Multi-variable ◦ y , and w are free in second part
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.24


Concept of λ: Parallel in C / C++

Module M02 • Function Abstraction


Partha Pratim
◦ Mathematical Notation: f : Integer × Integer × Integer × Integer → Integer
Das f (n, l, s, g ) = n + l + s + g
◦ λ-notation:
Relations λn. λl.λs. n + l + s + g // Bound = n, l, s, Free = g
Functions
◦ C Function:
Composition int g; // Free, global g - to be set from environment
Currying int f(int n) { // Bound, parameter n
λ-Calculus
int l = 3; // Bound, automatic local l
Concept of λ
static int s = 7; // Bound, static local s
Syntax return n + l + s + g;
λ-expressions
}
Notation
Examples
Simple • Function Application
Composition ◦ Mathematical Notation: f (2, 3, 7, g ) = 12 + g
Boolean
Numerals
◦ λ-notation:
(λn. λl.λs. n + l + s + g ) 2 3 7 = 2 + 3 + 7 + g = 12 + g
Recursion
Multi-variable
◦ C Function:
Functions g = 5; // Free global g set from environment to 5
Higher Order
Functions
f(2); // 17
C++11
g = 3; // Free global g set from environment to 3
f(2); // 15
Principles of Programming Languages Partha Pratim Das M02.25
Function Application

Module M02

Partha Pratim • With a function application (E1 E2 ), it is expected that E1 evaluates to a predefined
Das
function (a constant) or an abstraction, say (λx. E3 ), in which case the result of the
Relations application will be the evaluation of E3 after every free occurrence of x in E3 has been
Functions
Composition
replaced by E2
Currying (λn. n3 2) ⇒ 23 ⇒ 8
λ-Calculus
Concept of λ (λn. (∗ (∗ n n) n) 2) ⇒ 23 ⇒ 8
Syntax
λ-expressions • In a combination (E1 E2 ), the function or operator E1 is called the rator and its
Notation
Examples
argument or operand E2 is called the rand
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.26


Notation for λ-expressions

Module M02

Partha Pratim • Uppercase letters and identifiers beginning with capital letters will be used as
Das
meta-variables ranging over λ-expressions
Relations

Functions
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.27


Notation for λ-expressions

Module M02

Partha Pratim • Function application associates to the left


Das

Relations E1 E2 E3
Functions
Composition means
Currying

λ-Calculus
Concept of λ ((E1 E2 ) E3 )
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.28


Notation for λ-expressions

Module M02

Partha Pratim • The scope of λ < variable > in an abstraction extends as far to the right as possible:
Das

Relations λx. E1 E2 E3
Functions
Composition
Currying
means
λ-Calculus (λx. (E1 E2 E3 )) and not ((λx. E1 E2 ) E3 )
Concept of λ

Syntax • Application has a higher precedence than Abstraction


λ-expressions
Notation
• Parentheses are needed for
Examples
(λx. E1 E2 ) E3
Simple
Composition
Boolean where E3 is intended to be an argument to the function
Numerals
Recursion
Multi-variable λx. E1 E2
Functions
Higher Order
Functions
C++11
and not part of the body of the function as above

Principles of Programming Languages Partha Pratim Das M02.29


Notation for λ-expressions

Module M02

Partha Pratim • An abstraction allows a list of variables that abbreviates a series of λ abstractions
Das

Relations λx y z. E
Functions
Composition
Currying
means
λ-Calculus (λx. (λy . (λz. E )))
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.30


Notation for λ-expressions

Module M02

Partha Pratim • Functions defined as λ-expression abstractions are anonymous, so the λ-expression itself
Das
denotes the function
Relations
• As a notational convention, λ-expressions may be named using the syntax
Functions
Composition
Currying define < name > = < expression >
λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.31


Notation for λ-expressions

Module M02

Partha Pratim • For example, given


Das
define Twice = λf . λx. f (f x)
Relations

Functions
it follows that
Composition (Twice (λn. (add n 1)) 5) = 7
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.32


Concept of λ: Parallel in C / C++

Module M02

Partha Pratim
• Variables: n
Das • Constants: ∗ : Integer → Integer (binary multiplication), 2

Relations
• Function Abstraction

Functions
◦ Mathematical3 Notation: cube : Integer → Integer
cube(n) = n
Composition
Currying
◦ λ-notation: 3
cube ≡ λn. n = λn. (n ∗ n ∗ n) // Untyped λ
λ-Calculus
cube ≡ λ(n : int). n3 // Typed λ, return type inferred
Concept of λ
◦ C Function:
Syntax int cube(int n) { return n * n * n; } // return type explicit
λ-expressions ◦ C++ λ Function:
Notation auto cube = [](int n) { return n * n * n; }; // return type inferred
Examples
auto cube = [](int n) -> int { return n * n * n; }; // return type explicit
Simple
Composition • Function Application
Boolean ◦ Mathematical Notation: cube(2) = 8
Numerals ◦ λ-notation:
Recursion
(λn. n3 ) 2 ≡ 2 ∗ 2 ∗ 2 = 8
Multi-variable
Functions ◦ C / C++ λ Function:
Higher Order int n cube = cube(2);
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.33


Notation for λ-expressions: Example

Module M02

Partha Pratim • Group the terms in the following λ-expression


Das

Relations (λn. λf . λx. f (n f x)) (λg . λy . g y )


Functions
Composition • λ Abstractions
Currying

λ-Calculus
Concept of λ (λx. f (n f x)) (λy . g y )
Syntax (λf . (λx. f (n f x))) (λg . (λy . g y ))
λ-expressions
Notation
(λn. (λf . (λx. f (n f x))))
Examples
Simple
Composition • Completely parenthesized expression:
Boolean
Numerals
Recursion ((λn. (λf . (λx. (f ((n f ) x))))) (λg . (λy . (g y ))))
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.34


Examples of λ-expressions

Module M02
• Elementary
Partha Pratim
◦ Identity Function
Das ◦ Successor Function
Relations
◦ Constant Function
Functions • Composition
Composition
◦ Application
Currying

λ-Calculus
▷ twice
Concept of λ ▷ thrice
Syntax ◦ Composition
λ-expressions
Notation
• Church Boolean
Examples
Simple
◦ Selector Function (TRUE , FALSE )
Composition ◦ Conditional Test IF
Boolean
Numerals
◦ Boolean Algebra
Recursion • Church Numerals
Multi-variable
Functions • Recursion
Higher Order
Functions
◦ Self Application
C++11 ◦ Y Combinator
▷ factorial
Principles of Programming Languages Partha Pratim Das M02.35
λ-expressions: Identity Function

Module M02

Partha Pratim • The λ-expression


Das
ID = λx. x
Relations

Functions
denotes the identity function in the sense that
Composition
Currying
((λx. x) E ) = E
λ-Calculus
Concept of λ

Syntax for any λ-expression E


λ-expressions
Notation
◦ Identity function has type A → A for every type A
Examples ◦ Functions that allow arguments of many types, such as this identity function, are
Simple
Composition known as polymorphic operations
Boolean
Numerals
◦ The λ-expression (λx. x) acts as an identity function on the set of integers, on a set
Recursion of functions of some type, or on any other kind of object
Multi-variable
Functions
Higher Order
• The token ID is not part of the λ-calculus – just an abbreviation for the term (λx. x)
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.36


λ-expressions: Successor Function

Module M02

Partha Pratim • The λ-expression


Das
λn. (add n 1)
Relations

Functions
denotes the successor function on the integers so that
Composition
Currying
(λn. (add n 1)) 5 = 6
λ-Calculus
Concept of λ

Syntax • add and 1 need to be predefined constants to define this function, and 5 must be
λ-expressions predefined to apply the function
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.37


λ-expressions: Constant Function

Module M02

Partha Pratim • The λ-expression


Das
K = λx. λy . x
Relations

Functions
builds a constant function (generator)
Composition
Currying
• (K 0) = (λx. λy . x) 0 = λy . 0 = 0, is a constant function returning 0
λ-Calculus • (K 1) = (λx. λy . x) 1 = λy . 1 = 1, is a constant function returning 1
Concept of λ

Syntax • ···
λ-expressions
Notation • (K n) = (λx. λy . x) n = λy . n = n, is a constant function returning n
Examples
Simple
◦ (λy . n) 0 = 0
Composition
Boolean
◦ (λy . n) 12 = 12
Numerals ◦ (λy . n) 935 = 935
Recursion
Multi-variable
◦ (λy . n) m = m, m is a constant
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.38


λ-expressions: Application

Module M02

Partha Pratim • The λ-expression


Das
apply = λf . λx. f x
Relations

Functions
takes a function and a value as argument and applies the function to the argument
Composition
Currying
• Since f is a function and it takes x as an argument, say of type A, then f must be of
λ-Calculus type A → B for some B
Concept of λ

Syntax
• Type of apply then is: (A → B) → A → B
λ-expressions
Notation
• A → B is a possible type of f , A is the possible type of x, and B is the result type of
Examples apply which is the same as result type of f
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.39


λ-expressions: twice

Module M02

Partha Pratim • The λ-expression


Das
twice = λf . λx. f (f x)
Relations

Functions
is similar to apply but applies the function f twice
Composition
Currying
• It applies f to x obtaining a result, and applies f to this result once more
λ-Calculus • Unlike apply , since f is applied again to the result of f , the argument and result types
Concept of λ

Syntax
of f should be the same, say A
λ-expressions
Notation
• So, the type of twice is (A → A) → A → A
Examples
Simple
• If sqr is the (predefined) integer function, then
Composition
Boolean
Numerals
((twice sqr ) 3) ⇒ (((λf . (λx. (f (f x)))) sqr ) 3) ⇒
Recursion
Multi-variable
Functions ((λx. (sqr (sqr x))) 3) ⇒ (sqr (sqr 3)) ⇒ (sqr 9) ⇒ 81
Higher Order
Functions
C++11
• Similarly, (twice (λn. (add n 1)) 5) = 7
Principles of Programming Languages Partha Pratim Das M02.40
λ-expressions: thrice

Module M02

Partha Pratim • The λ-expression


Das
thrice = λf . λx. f (f (f x))
Relations

Functions
applies f thrice
Composition
Currying
• The type of thrice is (A → A) → A → A
λ-Calculus • If sqr is the (predefined) integer function, then
Concept of λ

Syntax
λ-expressions ((thrice sqr ) 3) ⇒ (((λf . (λx. f (f (f x)))) sqr ) 3) ⇒
Notation
Examples
Simple
((λx. (sqr (sqr (sqr x)))) 3) ⇒ (sqr (sqr (sqr 3))) ⇒
Composition
Boolean (sqr (sqr 9)) ⇒ (sqr 81) ⇒ 6561
Numerals
Recursion
Multi-variable
• Similarly, (thrice (λn. (add n 1)) 5) = 8
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.41


λ-expressions: Composition

Module M02

Partha Pratim • The λ-expression


Das
comp = λg . λf . λx. g (f x)
Relations

Functions
is the mathematical composition: (comp g f ) ≡ g ◦ f
Composition
Currying
• If f is of type A → B and g is of type B → C , then type of g ◦ f is A → C
λ-Calculus • Given an argument, g ◦ f first applies f to the argument and then applies g to the
Concept of λ

Syntax
result of this application
λ-expressions
Notation
• The type of comp is (B → C ) → (A → B) → (A → C )
Examples
Simple
• twice f ≡ (comp f f )
Composition
Boolean
• thrice f ≡ (comp f (comp f f )) ≡ (comp (comp f f ) f )
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.42


λ-expressions: Selector Function

Module M02

Partha Pratim • The λ-expression


Das
TRUE = fst = λx. λy . x
Relations

Functions
denotes the fst selector function
Composition
Currying
• It takes two arguments and returns the first argument as the result (ignoring the second
λ-Calculus argument)
Concept of λ

Syntax
• Note: (λx. λy . x) M N ≡ (λy . M) N ≡ M
λ-expressions ◦ The fst function is first given an argument, say of type A (of M), and it returns a
Notation
Examples function
Simple
Composition
◦ This (returned) function takes another argument, say of type B (of N), and returns
Boolean the original first argument (of type A)
Numerals
Recursion ◦ Hence, the type of fst is A → (B → A)
Multi-variable
Functions • The token TRUE is not part of the lambda-calculus – just an abbreviation for the term
Higher Order
Functions (λx. λy . x)
C++11

Principles of Programming Languages Partha Pratim Das M02.43


λ-expressions: Selector Function

Module M02

Partha Pratim • The λ-expression


Das
FALSE = snd = λx. λy . y
Relations

Functions
denotes the snd selector function
Composition
Currying
• It takes two arguments and returns the second argument as the result (ignoring the first
λ-Calculus argument)
Concept of λ

Syntax
• Note: (λx. λy . y ) M N ≡ (λy . y ) N ≡ N
λ-expressions ◦ The snd function is first given an argument, say of type A (of M), and it returns a
Notation
Examples function
Simple
Composition
◦ This (returned) function takes another argument, say of type B (of N), and returns
Boolean the same argument (of type B)
Numerals
Recursion ◦ Hence, it has a type A → (B → B)
Multi-variable
Functions • The token FALSE is not part of the lambda-calculus – just an abbreviation for the term
Higher Order
Functions (λx. λy . y )
C++11

Principles of Programming Languages Partha Pratim Das M02.44


λ-expressions: Conditional Test IF

Module M02

Partha Pratim • IF should take three arguments b, t, f , where b is a Boolean value and t, f are
Das
arbitrary terms
Relations
• The function should return t if b = TRUE and f if b = FALSE
Functions
Composition • Now (TRUE t f ) ≡ t and (FALSE t f ) ≡ f
Currying

λ-Calculus • IF has to apply its Boolean argument to the other two arguments:
Concept of λ

Syntax
IF = λb. λt. λf . b t f
λ-expressions
Notation
Examples
Simple
Composition
Boolean
• If b is not of Boolean type, the result is undefined
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.45


λ-expressions: Boolean Algebra

Module M02

Partha Pratim • Boolean operators can be defined using IF , TRUE , and FALSE :
Das

Relations AND = λb. λb ′ . IF b b ′ FALSE


Functions
Composition
OR = λb. λb ′ . IF b TRUE b ′
Currying
NOT = λb. IF b FALSE TRUE
λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
• Using the above definitions prove the De Morgan’s Laws of Boolean Algebra
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.46


λ-expressions: Practice

Module M02

Partha Pratim • (λz. z)(λy . y y )(λx. x a)


Das
• (λz. z)(λz. z z)(λz. z y )
Relations

Functions
• (λx. λy . x y y )(λa. a) b
Composition
Currying
• ((λx. λy . x y y )(λy . y ) y
λ-Calculus • (λx. x x)(λy . y x) z
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.47


Church Numerals: Links

Module M02

Partha Pratim • http://www.cs.unc.edu/~stotts/723/Lambda/church.html


Das
• http://www.cs.cornell.edu/courses/cs312/2008sp/recitations/rec26.html
Relations

Functions
• http://www.shlomifish.org/lecture/Lambda-Calculus/slides/lc_church_
Composition ops.scm.html
Currying

λ-Calculus • http://okmij.org/ftp/Computation/lambda-calc.html
Concept of λ

Syntax
• https://en.wikipedia.org/wiki/Church_encoding
λ-expressions
Notation
Examples
Simple
Source: http://www.wikibooks.org; Wikibooks home
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.48


Church Numerals

Module M02

Partha Pratim • Natural numbers are non-negative


Das
• Given a successor function, succ, which adds one, we can define the natural numbers in
Relations
terms of zero (0) and succ:
Functions
Composition
Currying 1 = (succ 0)
λ-Calculus
Concept of λ 2 = (succ 1)
Syntax
= (succ (succ 0))
λ-expressions
Notation
Examples
3 = (succ 2)
Simple
Composition
= (succ (succ (succ 0)))
Boolean
Numerals
...
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.49


Church Numerals

Module M02

Partha Pratim
• A number n will be that number of successors of zero
Das
• If f and x are λ-terms, and n > 0 a natural number, write f n x for the term
Relations f (f (...(f x)...)), where f occurs n times
Functions
Composition
• For each natural number n, we define a λ-term n, called the nth Church Numeral, as
n = λf . λx. f n x
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions • First few Church numerals are:
Notation
Examples
Simple
C0 = 0 = λf . λx. x
Composition
Boolean
C1 = 1 = λf . λx. (f x)
Numerals
Recursion
C2 = 2 = λf . λx. (f (f x))
Multi-variable
Functions C3 = 3 = λf . λx. (f (f (f x)))
Higher Order
Functions
C++11
Cn = n = λf . λx. f n x

Principles of Programming Languages Partha Pratim Das M02.50


Church Numerals: Successor

Module M02

Partha Pratim • The successor is defined as:


Das
succ = λn. λf . λx. (f ((n f ) x))
Relations

Functions
• Apply f on n applications of f (that is, n)
Composition
Currying
• Hence it leads to n + 1 applications of f (that is, n + 1):
λ-Calculus
Concept of λ succ 0 = (λn. λf . λx. (f ((n f ) x))) 0
Syntax
λ-expressions = (λn. λf . λx. (f ((n f ) x)))(λf . λx. x)
Notation
Examples = λf . λx. (f (((λf . λx. x) f ) x))
Simple
Composition = λf . λx. (f (((λg . λy . y ) f ) x))
Boolean
Numerals = λf . λx. (f ((λy . y ) x))
Recursion
Multi-variable = λf . λx. (f x)
Functions
Higher Order
Functions
= 1
C++11

Principles of Programming Languages Partha Pratim Das M02.51


Church Numerals: Successor

Module M02

Partha Pratim
Das

Relations
succ 1 = (λn. λf . λx. (f ((n f ) x))) 1
Functions = (λn. λf . λx. (f ((n f ) x)))(λf . λx. (f x))
Composition
Currying = λf . λx. (f (((λf . λx. (f x)) f ) x))
λ-Calculus
Concept of λ = λf . λx. (f (((λg . λy . (g y )) f ) x))
Syntax
= λf . λx. (f ((λy . (f y )) x))
λ-expressions
Notation
Examples
= λf . λx. (f (f x))
Simple
Composition
= 2
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.52


Church Numerals: Successor

Module M02

Partha Pratim • succ = λn. λf . λx. (f ((n f ) x))


Das

Relations succ n = (λn. λf . λx. (f ((n f ) x))) n


Functions
Composition
= λf . λx. (f ((n f ) x))
Currying
= λf . λx. (f (((λf . λx. (f n x)) f ) x))
λ-Calculus
Concept of λ = λf . λx. (f (((λg . λy . (g n y )) f ) x))
Syntax
λ-expressions = λf . λx. (f ((λy . (f n y )) x))
λf . λx. (f (f n x))
Notation
Examples =
λf . λx. (f n+1 x)
Simple
Composition =
Boolean
Numerals = n+1
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.53


Church Numerals: Addition

Module M02

Partha Pratim • succ = λn. λf . λx. (f ((n f ) x)) goes one step from n
Das
• For addition of m with n, we need to go n steps from m
Relations

Functions
• The addition is defined as:
Composition
Currying
add = λm. λn. λf . λx. ((((m succ) n) f ) x)
λ-Calculus • Compute n successor of m. Apply n applications of f on m
Concept of λ

Syntax • succ ≡ add 1


λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.54


Church Numerals: Addition

Module M02

Partha Pratim • Example:


Das

Relations (add 2 2) = ((add 2) 2)


Functions
Composition
= ((λm.λn.λf .λx.((((m succ) n) f ) x) 2) 2)
Currying
= (λn.λf .λx.((((2 succ) n) f ) x)
λ-Calculus
Concept of λ = λf .λx.((((2 succ) 2) f x)
Syntax
λ-expressions = λf .λx.((((λg .λy .(g (g y )) succ) 2) f x)
Notation
Examples = λf .λx.(((λy .(succ (succ y )) 2) f ) x)
Simple
Composition = λf .λx.(((succ (succ 2)) f ) x)
Boolean
Numerals = λf .λx.(((succ 3) f ) x)
Recursion
Multi-variable = λf .λx.(((4) f ) x)
Functions
Higher Order
Functions
= 4
C++11

Principles of Programming Languages Partha Pratim Das M02.55


Church Numerals: Multiplication

Module M02

Partha Pratim • The multiplication function is defined as:


Das
mul = λm. λn. λx. (m (n x))
Relations

Functions
• Apply n applications of f (n) m times
Composition
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.56


Church Numerals: Multiplication

Module M02

Partha Pratim • Example:


Das

Relations (mult 2 3) = ((mult 2) 3)


Functions
Composition
= ((λm. λn. λx. (m (n x)) 2) 3)
Currying
= (λn. λx. (2 (n x) 3)
λ-Calculus
Concept of λ = λx. (2 (3 x))
Syntax
λ-expressions = λx. (2 (λg . λy . (g (g (g y ))) x))
Notation
Examples = λx. (2 (λy . (x (x (x y )))))
Simple
Composition = λx. (λf . λz. (f (f z))) λy . (x (x (x y )))))
Boolean
Numerals = λx. λz. (λy . (x (x (x y ))) (λy . (x (x (x y ))) z))
Recursion
Multi-variable = λx. λz. (λy . (x (x (x y ))) (x (x (x z))))
Functions
Higher Order
Functions
= λx. λz. (x (x (x (x (x (x z))))))
C++11
= 6
Principles of Programming Languages Partha Pratim Das M02.57
Church Numerals: Exponentiation

Module M02 • The exponentiation (nm ) function is defined as:


Partha Pratim exp = λm. λn. (m n)
Das
• Example:
Relations
(exp 2 3) = ((exp 2) 3)
Functions
Composition = ((λm. λn. (m n) 2) 3)
Currying
= (λn. (2 n) 3)
λ-Calculus
Concept of λ = (2 3)
Syntax = (λf . λx. (f (f x)) 3 )
λ-expressions
= λx. (3 (3 x))
Notation
Examples = λx. (3 (λg . λy . (g (g (g y ))) x))
Simple
Composition = λx. (3 λy . (x (x (x y ))))
Boolean = λx. (λg . λz. (g (g (g z))) λy . (x (x (x y ))))
Numerals
Recursion = λx. λz. (λy . (x (x (x y )))(λy . (x (x (x y )))(λy . (x (x (x y ))) z)))
Multi-variable
Functions
= λx. λz. (λy . (x (x (x y )))(λy . (x (x (x y )))(x (x (x z)))))
Higher Order = λx. λz. (λy . (x (x (x y )))(x (x (x (x (x (x z)))))))
Functions
C++11 = λx. λz. (x (x (x (x (x (x (x (x (x z)))))))))
= 9
Principles of Programming Languages Partha Pratim Das M02.58
Church Numerals: Predecessor

Module M02 • The predecessor is defined as:


Partha Pratim
Das pair = λx. λy . λf . ((f x) y )
prefn = λf . λp. ((pair (f (p first))) (p first))
Relations

Functions
pred = λn. λf . λx. (((n (prefn f )) (pair x x)) second)
Composition
Currying

λ-Calculus • Example: Show: (pred 3) = 2


Concept of λ

Syntax
• Note:
λ-expressions ◦ Kleene discovered how to express the operation of subtraction within Church’s scheme (yes, Church
Notation
Examples
was unable to implement subtraction and subsequently division, within that calculus)!
Simple ◦ Other landmarks then followed, such as the recursive function Y.
Composition ◦ In 1937 Church and Turing, independently, showed that every computable operation (algorithm) can
Boolean
be achieved in a Turing machine and in the Lambda Calculus, and therefore the two are equivalent.
Numerals
Recursion
◦ Similarly Godel introduced his description of computability, again independently, in 1929, using a
Multi-variable third approach which was again shown to be equivalent to the other 2 schemes.
Functions
Higher Order
◦ It appears that there is a ”platonic reality” about computability. That is, it was ”discovered” (3
Functions times independently) rather than ”invented”. It appears to be natural in some sense.
C++11
Source: Natural Numbers as Church Numerals
Principles of Programming Languages Partha Pratim Das M02.59
Church Numerals
Practice Problems
Module M02

Partha Pratim • Show: add 2 3 = 5


Das
• Show: mul 2 3 = 6
Relations

Functions
• Show: exp 3 2 = 8
Composition
Currying
• Show: add n 0 = n
λ-Calculus • Show: mul n 1 = n
Concept of λ

Syntax • Show: exp 0 n = 1


λ-expressions
Notation • Prove: add and mul are commutative
Examples
Simple • Prove: add and mul are associative
Composition
Boolean • Prove: mul c (add a b) = add (mul c a) (mul c b)
Numerals
Recursion • Define: sub m n, where sub(m, n) = (m − n ≥ 0)? m − n : 0
Multi-variable
Functions
• Define: div m n, where div (m, n) = (m − m % n)/n
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.60


λ-expressions: Self Application

Module M02

Partha Pratim • The λ-expression


Das
sa = λx. x x
Relations

Functions
takes an argument x, which is apparently a function and applies the function to itself
Composition and returns whatever is the result
Currying

λ-Calculus • x is a function that can take itself as an argument!


Concept of λ

Syntax
• (sa id) = id id = (λx. x) id = id
λ-expressions
Notation
• (sa fst) = fst fst = (λx. λy . x) fst = λy . fst
Examples
Simple
• (sa snd) = snd snd = (λx. λy . y ) snd = id
Composition
Boolean
• (sa twice) = twice twice = (λf . λx. f (f x)) twice = (λx. twice (twice x)) =
Numerals comp twice twice
Recursion
Multi-variable
Functions
• Finally! (sa sa) = sa sa = (λx. x x) sa = sa sa
Higher Order
Functions
◦ Infinite Loop in λ-Calculus, denoted by Ω
C++11

Principles of Programming Languages Partha Pratim Das M02.61


λ-expressions: Y Combinator

Module M02 • The λ-expression


Partha Pratim
Y = λu. (λx. u (x x)) (λx. u (x x))
Das
is called the Y combinator
Relations

Functions
• Consider:
Composition
Currying
Y t = (λx. t (x x)) (λx. t (x x))
λ-Calculus
Concept of λ = (λy . t (y y )) (λx. t (x x))
Syntax
λ-expressions
= t ( (λx. t (x x)) (λx. t (x x)) )
Notation
Examples
= t (Y t)
Simple
Composition
Boolean
• (Y t) is function t applied to itself! Repeatedly unfolding:
Numerals
Recursion
Multi-variable
Y t = t (Y t) = t (t (Y t)) = t (t (t (Y t))) = · · ·
Functions
Higher Order
Functions • Another form of an infinite loop? No – it is quite useful
C++11
• Used to encode recursive functions in λ-calculus
Principles of Programming Languages Partha Pratim Das M02.62
λ-expressions: Fixed Point

Module M02
• The fixed point of a function f : A → A is a value x ∈ A such that f (x) = x
Partha Pratim
Das • Examples:
Relations
◦ f (x) = x 2 − 3x + 4 has a fixed point f (2) = 2
Functions
◦ f (x) = x 3 has 3 fixed points f (−1) = −1, f (0) = 0, and f (+1) = +1
Composition
◦ cos x = x has a fixed point cos 0.739085133 = 0.739085133
Currying

λ-Calculus
Concept of λ

Syntax
λ-expressions ◦
Notation
Examples
Simple
Composition
Boolean √ √
Numerals ◦ f (x) = x
+ x1 has a fixed point f ( 2) = 2. Starting with x0 = 1, we have: x0 = 1,
2
Recursion
x1 = 1/2 + 1/1 = 3/2, x2 = 3/4 + 2/3 = 17/12, x3 = 17/24 + 12/17 ≈ 1.41421569, · · ·
Multi-variable
Functions ◦ Not all functions have fixed points:
Higher Order
Functions ▷ f (x) = x + 1
C++11 ▷ Collatz Sequence f (n) = (n mod 2)? 3n + 1 : n/2 cycles between 4, 2, 1.
Principles of Programming Languages Partha Pratim Das M02.63
λ-expressions: Fixed Point

Module M02

Partha Pratim • The fixed point of a function f : N → N is a value x ∈ N such that


Das

Relations f x =x
Functions
Composition
Currying

λ-Calculus • Since y f = f (y f )
Concept of λ

Syntax
◦ (y f ) is a fixed point of the function f
λ-expressions ◦ Hence, y is called the fixed point combinator
Notation
Examples ▷ When y is applied to a function, it answers a value x in that function’s domain
Simple
Composition
▷ When we apply the function to x, we get x
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.64


λ-expressions: Y Combinator – factorial

Module M02

Partha Pratim • define factorial = λn. if (= n 1) 1 (∗n (factorial (−n 1)))


Das
• The above is circular. So rewrite as:
Relations

Functions
Composition
define factorial = T factorial
Currying
define T = λf . λn. if (= n 1) 1 (∗n (f (−n 1)))
λ-Calculus
Concept of λ

Syntax
λ-expressions
Notation
• Y T = T (Y T ), is then the factorial
Examples
Simple
Composition factorial = (Y T )
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.65


λ-expressions: Y Combinator – factorial

Module M02 • define T = λf . λn. if (= n 1) 1 (∗n (f (−n 1)))


Partha Pratim • Sample:
Das

(Y T ) 1 = T (Y T ) 1 = λn. if (= n 1) 1 (∗n ((Y T ) (−n 1))) 1


Relations
= if (= 1 1) 1 (∗ 1 ((Y T ) (−1 1)))
Functions
Composition = 1
Currying
(Y T ) 2 = T (Y T ) 2 = λn. if (= n 1) 1 (∗n ((Y T ) (−n 1))) 2
λ-Calculus
= if (= 2 1) 1 (∗ 2 ((Y T ) (−2 1)))
Concept of λ
= (∗ 2 ((Y T ) 1))
Syntax
λ-expressions = (∗ 2 1)
Notation
= 2
Examples
Simple (Y T ) 3 = T (Y T ) 3 = λn. if (= n 1) 1 (∗n ((Y T ) (−n 1))) 3
Composition
= if (= 3 1) 1 (∗ 3 ((Y T ) (−3 1)))
Boolean
Numerals = (∗ 3 ((Y T ) 2))
Recursion
= (∗ 3 2)
Multi-variable
Functions = 6
Higher Order
Functions
C++11
• Use induction to prove that (Y T ) n = factorial(n)
Principles of Programming Languages Partha Pratim Das M02.66
λ-expressions: Fibonacci Function

Module M02
• The Fibonacci function in the λ-calculus
Partha Pratim
Das
fibo(n)= fibo(n − 1) + fibo(n − 2), if n > 1
Relations
= 1, if n = 1
Functions
Composition
= 0, if n = 0
Currying • Using the Y combinator, we can define Fibonacci function in the λ-calculus
λ-Calculus • Define function F , whose fixed-point will be Fibonacci:
Concept of λ

Syntax
λ-expressions F = λf . λn. (if (= 0 n) 0 (if (= 1 n) 1 (+ (f (− n 1) f (− n 2)))))
Notation
Examples
Simple
Composition
Boolean
• Then take the fixed point of F :
Numerals fibo = (Y F )
Recursion
Multi-variable
Functions
Higher Order
Functions • Show: fibo(5) = 5
C++11
• Use induction to prove that (Y F ) n = fibo(n)
Principles of Programming Languages Partha Pratim Das M02.67
λ-expressions: Ackermann Function

Module M02 • The Ackermann function A(x, y ) is defined for integers x and y by:
Partha Pratim
Das
A(x, y ) = y + 1, if x = 0
= A(x − 1, 1), if y = 0
Relations = A(x − 1, A(x, y − 1)), otherwise
Functions Special values for x include the following:
Composition
Currying
A(0, y ) = y +1
λ-Calculus A(1, y ) = y +2
Concept of λ A(2, y ) = 2∗y +3
Syntax y +3
A(3, y ) = 2 −3
λ-expressions
2
Notation ..
2.
Examples A(4, y ) = 2 −3
Simple
Composition
Boolean A(0, 2) = 2+1=3
Numerals
A(1, 2) = 2+2=4
Recursion
Multi-variable A(2, 2) = 2∗2+3=7
Functions
2+3
Higher Order A(3, 2) = 2 − 3 = 29
Functions
22
C++11
22 65536
A(4, 2) = 2 −3=2 − 3 = an integer of 19, 729 decimal digits
Principles of Programming Languages Partha Pratim Das M02.68
λ-expressions: Ackermann Function

Module M02

Partha Pratim • The Ackermann function grows faster than any primitive recursive function, that is: for
Das
any primitive recursive function f , there is an n such that
Relations

Functions A(n, x) > f x


Composition
Currying

λ-Calculus
Concept of λ
• So A cannot be primitive recursive
Syntax
λ-expressions • Can we define A in the λ-calculus?
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.69


λ-expressions: Ackermann Function

Module M02

Partha Pratim • The Ackermann function in the λ-calculus


Das

Relations A(x, y ) = y + 1, if x = 0
Functions = A(x − 1, 1), if y = 0
Composition
Currying
= A(x − 1, A(x, y − 1)), otherwise
λ-Calculus
Concept of λ
• Using the Y combinator, we can define Ackermann function in the λ-calculus, even
Syntax
λ-expressions
though it is not primitive recursive!
Notation
• Define function aG , whose fixed-point will be ackermann:
Examples
Simple
Composition aG = (if (= 0 x) (succ y ) (if (= 0 y ) (f (pred x) 1) (f (pred x) (f x (pred y )))))
Boolean
Numerals
Recursion
Multi-variable
Functions
• Then take the fixed point of aG :
Higher Order
Functions
C++11
ackermann = (y aG )

Principles of Programming Languages Partha Pratim Das M02.70


Multi-variable Functions

Module M02

Partha Pratim • λ-calculus directly permits functions of a single variable only


Das
• The abstraction mechanism allows for only one parameter at a time
Relations

Functions
• Many useful functions, such as binary arithmetic operations, require more than one
Composition parameter; for example,
Currying

λ-Calculus
sum(a, b) = a + b
Concept of λ

Syntax
matches the syntactic specification
λ-expressions
Notation
Examples
sum : N × N → N
Simple
Composition
Boolean
where N denotes the natural numbers
Numerals
Recursion
• λ-calculus admits two solutions for this
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.71


Multi-variable Functions: Using Ordered Pairs

Module M02

Partha Pratim • Allow ordered pairs as λ-expressions


Das
• Use the notation < x, y >, and define the addition function on pairs:
Relations

Functions
Composition
sum < a, b > = a + b
Currying

λ-Calculus
Concept of λ

Syntax
◦ Pairs can be provided by using a predefined cons operation as in Lisp, or
λ-expressions ◦ Pairing operation can be defined in terms of primitive λ-expressions in the pure
Notation
Examples λ-calculus
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.72


Multi-variable Functions: Using Curried Functions

Module M02

Partha Pratim • Use the curried version of the function with the property that arguments are supplied
Das
one at a time:
Relations add : N → N → N
Functions
Composition where add a b = a + b
Currying

λ-Calculus • Now
Concept of λ
(add a) : N → N
Syntax
λ-expressions
Notation
is a function with the property that
Examples
Simple
Composition
((add a) b) = a + b
Boolean
Numerals
Recursion
Thus, the successor function can be defined as (add 1)
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.73


Curried Functions

Module M02

Partha Pratim • The operations of currying and uncurrying a function can be expressed in the λ-calculus
Das
as
Relations
define Curry = λf . λx. λy . f < x, y >
Functions
Composition define Uncurry = λf . λp. f (head p)(tail p)
Currying

λ-Calculus
provided the pairing operation < x, y >= (cons x y ) and the functions (head p) and
Concept of λ (tail p) are available, either as predefined functions or as functions defined in the pure
Syntax λ-calculus
λ-expressions
Notation • The two versions of the addition operation are related as:
Examples
Simple Curry sum = add and Uncurry add = sum
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.74


Higher Order Functions

Module M02

Partha Pratim • Currying permits the partial application of a function


Das
• Consider an example using Twice that takes advantage of the currying of functions:
Relations

Functions
Composition
define Twice = λf . λx. f (f x)
Currying

λ-Calculus
Concept of λ

Syntax • Twice is a polymorphic function as it may be applied to any function and element as
λ-expressions long as that element is in the domain of the function and its image under the function
Notation
Examples is also in that domain
Simple
Composition • The mechanism that allows functions to be defined to work on a number of types of
Boolean
Numerals
data is also known as parametric polymorphism
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.75


Higher Order Functions

Module M02

Partha Pratim • If D is any domain, the syntax (or signature) for Twice can be described as
Das

Relations Twice : (D → D) → D → D
Functions
Composition
Currying
Given the square function, sqr : N → N where N stands for the natural numbers, it
λ-Calculus follows that
Concept of λ (Twice sqr ) : N → N
Syntax
λ-expressions
Notation
Examples is itself a function. This new function can be named
Simple
Composition
Boolean define FourthPower = Twice sqr
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.76


Higher Order Functions

Module M02

Partha Pratim • FourthPower is defined without any reference to its argument


Das
• Defining new functions in this way embodies the spirit of functional programming
Relations

Functions
• Power of a functional programming language lies in its ability to define and apply
Composition higher-order functions
Currying

λ-Calculus ◦ functions that take functions as arguments and/or return a function as their result
Concept of λ ◦ Twice is higher-order since it maps one function to another
Syntax
λ-expressions
Notation
Examples Source: Higher-order functions in Multiple Languages
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11

Principles of Programming Languages Partha Pratim Das M02.77


C++11: Functors

Module M02

Partha Pratim • Function objects (Functors) are objects specifically designed to be used with a syntax
Das
similar to that of functions. In C++, this is achieved by defining member function
Relations operator() in their class, like for example:
Functions
Composition
Currying
// Function Objects
λ-Calculus
Concept of λ
struct myclass {
Syntax int operator()(int a) { return a; }
λ-expressions } myobject;
Notation
Examples
Simple
Composition
int x = myobject(0); // function-like syntax with object myobject
Boolean
Numerals
Recursion • They are typically used as arguments to functions, such as predicates or comparison
Multi-variable
Functions functions passed to standard algorithms.
Higher Order
Functions
C++11
Source: <functional> in STL
Principles of Programming Languages Partha Pratim Das M02.78
C++11: λ

Module M02
#include <iostream>
Partha Pratim
Das
#include <functional> // Provides template <class Ret, class... Args> class function<Ret(Args...)>;
using namespace std;
Relations
// lambda expressions
Functions
auto f = [](int i) { return i + 3; };
Composition
auto sqr = [](int i) { return i * i; };
Currying
auto twice = [](const function<int(int)>& g, int v) { return g(g(v)); };
λ-Calculus auto comp = [](const function<int(int)>& g, const function<int(int)>& h, int v) { return g(h(v)); };
Concept of λ

Syntax int main() {


λ-expressions auto a = 7, b = 5, c = 3; // Type inferred as int
Notation
Examples cout << f(a) << endl; // 10
Simple cout << twice(f, a) << " " << comp(f, f, a) << endl; // 13 13
Composition
cout << twice(sqr, b) << " " << comp(sqr, sqr, b) << endl; // 625 625
Boolean
cout << comp(sqr, f, c) << " " << comp(f, sqr, c) << endl; // 36 12
Numerals
Recursion
}
Multi-variable
Functions
Source:
Higher Order
Functions • <functional> in STL
C++11
• std::function in <functional>
Principles of Programming Languages Partha Pratim Das M02.79

You might also like