Truth, Deduction,
Computation
Lecture F
Untyped λ... and other things
Vlad Patryshev
SCU
2013
Functions
f: X → Y; X - domain, X - codomain.
∀ x:X ∃ y:Y (y=f(x))

y

y

x
A Function

x
Not a Function
Functions. Definitions
Monomorphism, aka injective function, aka one-to-one function

∀x ∀y (f(x)=f(y) → x=y)
f: X ↣ Y
Functions. Definitions
Epimorphism, aka surjective function, aka onto function

∀y ∃x (f(x)=y)
f: X ↠ Y
Functions. Definitions
Bijection, aka bijective function, aka one-to-one correspondence

f is injection and surjection
f: A
Functions. Composition
● f: X → Y
● g: Y → Z
● h=g∘f

(g∘f)(x) = g(f(x))
h

→
Functions. Identity
● id: X → X
● id∘f=f=f∘id

IdX

→

X

id(x)=x
Functions. Make a monoid
f,g: X → X
● binary operation: f∘g
● neutral element: id
● associativity: (f∘g)∘h = f∘(g∘h)
Currying
Having a function
f: (x1,x2,...xn) → y
is equivalent to having a function
f c: x 1 → x 2 → … → x n → y
Functions in Programming languages
Language

Code Sample

Java

interface Function<X,Y> {
public Y apply(x: X)
}
Function<Int,String> f =
new Function<Int,String>() {
public String apply(n: Int) {
return "n=" + n;
}

Scala

val f:(Int => String) = (n:Int) => "n=" + n

Javascript

f = function(n) { return "n=" + n }
Peano Arithmetic
1.
2.
3.
4.
5.
6.
7.
8.

objects are called natural numbers
equality is defined, with usual properties
0 is a natural number
function s: s(x) is natural number
∀x (s(x) ≠ 0)
∀x∀y (s(x) = s(y) → x = y)
P(0), ∀x (P(x)→P(s(x))) ⊢ ∀x P(x)
define 1 as s(0)
Peano Arithmetic Illustrated

Giuseppe Peano

Domino model of Peano Arithmetic
Peano Arithmetic: define +
● x+0 = x
● x+s(y) = s(x+y)
● have a monoid
Peano Arithmetic: define ×
● x×0 = 0
● x×s(y) = x + (x×y)
● have a monoid
Peano Arithmetic: define ↑ etc
● x↑0 = 1
● x↑s(y) = x × (x↑y)
● (strangely, no associativity)
● x⇑↑0 = 1
● x⇑s(y) = x ↑ (x⇑y)
(Some people argue lately that this makes it
inconsistent)
Peano Arithmetic: Universal Property
Alonzo Church
●
●
●

Peano arithmetics is undecidable
Every effectively calculable function is a
computable function
Invented λ-calculus which is equivalent to
effectively calculable functions

Is it so that all that machines can do is equivalent to
lambdas?
Church-Rosser thesis: YES!
https://files.nyu.edu/cb125/public/Lambda/barendregt.94.pdf
So, can we build Peano arithmetic?
They are called Church Numerals
0:
1:
2:
…
n:

f → id
f → f
f → f∘f
f → f∘…∘f

}

●
●
●
●
●

n times
Simplest Language for Calculations
●
●
●
●

<λ
<λ
<λ
<λ

term>::=<variable>
term>::=<λ term> <λ term>
term>::=(λ <variable> <λ term>)
term>::=(<λ term>)

application
abstraction

http://www.itu.dk/people/sestoft/lamreduce/lamframes.html
Javascript Interpretation
● we have as many variables as we like
● λ x N ≡ function(x) {return N}
● (M N) ≡ M(N)

http://myjavatools.com/js.html
Examples of λ terms
●
●
●
●
●

x
λ x x
(x y)
((λ x x)(z t))
λ x ((x x) (x x))
Does not make much sense so far, right?
Same things in Javascript
●
●
●
●
●

x
function(x) {return x}
x(y)
(function(x) {return x})(z(t))
function(x) {return x(x)(x(x))}
Rules of Transformation
● α-equivalence, renaming bound(*) variables
● β-conversion (substitution/λ intro)
● η-conversion
Substitutions
1.
2.
3.
4.
5.

x[x:=N]
y[x:=N]
(M1 M2)[x:=N]
(λx M)[x:=N]
(λy M)[x:=N]

≡
≡
≡
≡
≡

N
y, if x ≠ y
(M1[x:=N]) (M2[x:=N])
λx M
λy (M[x:=N])//if x≠y and

y∉FV(N)
α-equivalence rule
Bound variable in a lambda expression - the one
which name is found right after λ.

● λx (λy (x (y z)))
Two lambda expressions differing only in names of
bound variables are equivalent.

λx (λy (x (y z))) ⇔ λt (λw (t (w z)))
λx (λy (x (y z))) ⇔ λz (λw (z (w z)))
Like in Javascript
function(x) {
return function(y) {
return y(function(z) { return z(x) }
}
}
// same as
function(a) {
return function(b) {
return b(function(c) { return c(a) }
}
}
β-conversion rule
(λx E) E’ ⇔ E[x := E’]

●
●
●
●

(λx
(λx
(λx
(λy

x) (y z) ⇔ (y z)
x) (λy y) ⇔ λy y ⇔ λx x
x) (λy y y) ⇔ λy y y
y y) (λx x) ⇔ (λx x) (λx x) ⇔ (λx x)
β-conversion is
β-reduction + β-abstraction
β-reduction
(λx E) E’ ⇒ E[x := E’]

β-abstraction
(λx E) E’ ⇐ E[x := E’]
β-reduction in Javascript
(function(x) {return M})(N)
Substitute all x in M with N
β-reduction - tautological case
●
●
●
●

t =
t t
(λx
(λx

λx x // identity function, right?
= ?
x) M = M
x) (λa a) = λa a
β-reduction - tautology, Javascript
id = function(x) { println(“<<x>>”);
return x }
id(id)
β-reduction - weird case
●
●
●
●

t =
t t
(λx
(λx

λx (x x)
= ?
(x x)) (λx (x x)) =...
(x x)) (λx (x x)) - oops.
That’s it for today

Truth, deduction, computation lecture f

  • 1.
    Truth, Deduction, Computation Lecture F Untypedλ... and other things Vlad Patryshev SCU 2013
  • 2.
    Functions f: X →Y; X - domain, X - codomain. ∀ x:X ∃ y:Y (y=f(x)) y y x A Function x Not a Function
  • 3.
    Functions. Definitions Monomorphism, akainjective function, aka one-to-one function ∀x ∀y (f(x)=f(y) → x=y) f: X ↣ Y
  • 4.
    Functions. Definitions Epimorphism, akasurjective function, aka onto function ∀y ∃x (f(x)=y) f: X ↠ Y
  • 5.
    Functions. Definitions Bijection, akabijective function, aka one-to-one correspondence f is injection and surjection f: A
  • 6.
    Functions. Composition ● f:X → Y ● g: Y → Z ● h=g∘f (g∘f)(x) = g(f(x)) h →
  • 7.
    Functions. Identity ● id:X → X ● id∘f=f=f∘id IdX → X id(x)=x
  • 8.
    Functions. Make amonoid f,g: X → X ● binary operation: f∘g ● neutral element: id ● associativity: (f∘g)∘h = f∘(g∘h)
  • 9.
    Currying Having a function f:(x1,x2,...xn) → y is equivalent to having a function f c: x 1 → x 2 → … → x n → y
  • 10.
    Functions in Programminglanguages Language Code Sample Java interface Function<X,Y> { public Y apply(x: X) } Function<Int,String> f = new Function<Int,String>() { public String apply(n: Int) { return "n=" + n; } Scala val f:(Int => String) = (n:Int) => "n=" + n Javascript f = function(n) { return "n=" + n }
  • 11.
    Peano Arithmetic 1. 2. 3. 4. 5. 6. 7. 8. objects arecalled natural numbers equality is defined, with usual properties 0 is a natural number function s: s(x) is natural number ∀x (s(x) ≠ 0) ∀x∀y (s(x) = s(y) → x = y) P(0), ∀x (P(x)→P(s(x))) ⊢ ∀x P(x) define 1 as s(0)
  • 12.
    Peano Arithmetic Illustrated GiuseppePeano Domino model of Peano Arithmetic
  • 13.
    Peano Arithmetic: define+ ● x+0 = x ● x+s(y) = s(x+y) ● have a monoid
  • 14.
    Peano Arithmetic: define× ● x×0 = 0 ● x×s(y) = x + (x×y) ● have a monoid
  • 15.
    Peano Arithmetic: define↑ etc ● x↑0 = 1 ● x↑s(y) = x × (x↑y) ● (strangely, no associativity) ● x⇑↑0 = 1 ● x⇑s(y) = x ↑ (x⇑y) (Some people argue lately that this makes it inconsistent)
  • 16.
  • 17.
    Alonzo Church ● ● ● Peano arithmeticsis undecidable Every effectively calculable function is a computable function Invented λ-calculus which is equivalent to effectively calculable functions Is it so that all that machines can do is equivalent to lambdas? Church-Rosser thesis: YES! https://files.nyu.edu/cb125/public/Lambda/barendregt.94.pdf
  • 18.
    So, can webuild Peano arithmetic? They are called Church Numerals 0: 1: 2: … n: f → id f → f f → f∘f f → f∘…∘f } ● ● ● ● ● n times
  • 19.
    Simplest Language forCalculations ● ● ● ● <λ <λ <λ <λ term>::=<variable> term>::=<λ term> <λ term> term>::=(λ <variable> <λ term>) term>::=(<λ term>) application abstraction http://www.itu.dk/people/sestoft/lamreduce/lamframes.html
  • 20.
    Javascript Interpretation ● wehave as many variables as we like ● λ x N ≡ function(x) {return N} ● (M N) ≡ M(N) http://myjavatools.com/js.html
  • 21.
    Examples of λterms ● ● ● ● ● x λ x x (x y) ((λ x x)(z t)) λ x ((x x) (x x)) Does not make much sense so far, right?
  • 22.
    Same things inJavascript ● ● ● ● ● x function(x) {return x} x(y) (function(x) {return x})(z(t)) function(x) {return x(x)(x(x))}
  • 23.
    Rules of Transformation ●α-equivalence, renaming bound(*) variables ● β-conversion (substitution/λ intro) ● η-conversion
  • 24.
    Substitutions 1. 2. 3. 4. 5. x[x:=N] y[x:=N] (M1 M2)[x:=N] (λx M)[x:=N] (λyM)[x:=N] ≡ ≡ ≡ ≡ ≡ N y, if x ≠ y (M1[x:=N]) (M2[x:=N]) λx M λy (M[x:=N])//if x≠y and y∉FV(N)
  • 25.
    α-equivalence rule Bound variablein a lambda expression - the one which name is found right after λ. ● λx (λy (x (y z))) Two lambda expressions differing only in names of bound variables are equivalent. λx (λy (x (y z))) ⇔ λt (λw (t (w z))) λx (λy (x (y z))) ⇔ λz (λw (z (w z)))
  • 26.
    Like in Javascript function(x){ return function(y) { return y(function(z) { return z(x) } } } // same as function(a) { return function(b) { return b(function(c) { return c(a) } } }
  • 27.
    β-conversion rule (λx E)E’ ⇔ E[x := E’] ● ● ● ● (λx (λx (λx (λy x) (y z) ⇔ (y z) x) (λy y) ⇔ λy y ⇔ λx x x) (λy y y) ⇔ λy y y y y) (λx x) ⇔ (λx x) (λx x) ⇔ (λx x)
  • 28.
    β-conversion is β-reduction +β-abstraction β-reduction (λx E) E’ ⇒ E[x := E’] β-abstraction (λx E) E’ ⇐ E[x := E’]
  • 29.
    β-reduction in Javascript (function(x){return M})(N) Substitute all x in M with N
  • 30.
    β-reduction - tautologicalcase ● ● ● ● t = t t (λx (λx λx x // identity function, right? = ? x) M = M x) (λa a) = λa a
  • 31.
    β-reduction - tautology,Javascript id = function(x) { println(“<<x>>”); return x } id(id)
  • 32.
    β-reduction - weirdcase ● ● ● ● t = t t (λx (λx λx (x x) = ? (x x)) (λx (x x)) =... (x x)) (λx (x x)) - oops.
  • 33.