10. 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 }
11. 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)
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)
17. 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
18. 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
25. α-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)))
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)