Truth, Deduction, ComputationLecture FUntyped λ... and other things
Vlad PatryshevSCU2013
Functions
f: X → Y; X - domain, X - codomain.
∀ x:X ∃ y:Y (y=f(x))
y y
x x
A Function 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 surjectionf: 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
id(x)=x
X
→
IdX
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: (x
1,x
2,...x
n) → 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. objects are called natural numbers2. equality is defined, with usual properties3. 0 is a natural number4. function s: s(x) is natural number5. ∀x (s(x) ≠ 0)6. ∀x∀y (s(x) = s(y) → x = y)7. P(0), ∀x (P(x)→P(s(x))) ⊢ ∀x P(x)8. 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: f → id● 1: f → f● 2: f → f∘f● …● n: 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. x[x:=N] ≡ N2. y[x:=N] ≡ y, if x ≠ y3. (M
1 M
2)[x:=N] ≡ (M
1[x:=N]) (M
2[x:=N])
4. (λx M)[x:=N] ≡ λx M5. (λy M)[x:=N] ≡ λy (M[x:=N])//if x≠y and y∉FV(N)
α-equivalence rule
● λx (λy (x (y z)))
λx (λy (x (y z))) ⇔ λt (λw (t (w z))) λx (λy (x (y z))) ⇔ λz (λw (z (w z)))
Bound variable in a lambda expression - the one which name is found right after λ.
Two lambda expressions differing only in names of bound variables are equivalent.
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 x) (y z) ⇔ (y z)● (λx x) (λy y) ⇔ λy y ⇔ λx x● (λx x) (λy y y) ⇔ λy y y● (λy y y) (λx x) ⇔ (λx x) (λx x) ⇔ (λx x)
(λx E) E’ ⇔ E[x := E’]
β-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 = λx x // identity function, right?● t t = ?● (λx x) M = M● (λx x) (λa a) = λa a
β-reduction - tautology, Javascript
id = function(x) { println(“<<x>>”); return x }
id(id)
β-reduction - weird case
● t = λx (x x)● t t = ?● (λx (x x)) (λx (x x)) =...● (λx (x x)) (λx (x x)) - oops.
That’s it for today
Top Related