Download - Truth, deduction, computation lecture f

Transcript
Page 1: Truth, deduction, computation   lecture f

Truth, Deduction, ComputationLecture FUntyped λ... and other things

Vlad PatryshevSCU2013

Page 2: Truth, deduction, computation   lecture f

Functions

f: X → Y; X - domain, X - codomain.

∀ x:X ∃ y:Y (y=f(x))

y y

x x

A Function Not a Function

Page 3: Truth, deduction, computation   lecture f

Functions. Definitions

Monomorphism, aka injective function, aka one-to-one function

∀x ∀y (f(x)=f(y) → x=y)f: X ↣ Y

Page 4: Truth, deduction, computation   lecture f

Functions. Definitions

Epimorphism, aka surjective function, aka onto function

∀y ∃x (f(x)=y)f: X ↠ Y

Page 5: Truth, deduction, computation   lecture f

Functions. Definitions

Bijection, aka bijective function, aka one-to-one correspondence

f is injection and surjectionf: A

Page 6: Truth, deduction, computation   lecture f

Functions. Composition

● f: X → Y● g: Y → Z● h=g∘f

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

h

Page 7: Truth, deduction, computation   lecture f

Functions. Identity

● id: X → X● id∘f=f=f∘id

id(x)=x

X

IdX

Page 8: Truth, deduction, computation   lecture f

Functions. Make a monoid

f,g: X → X

● binary operation: f∘g● neutral element: id● associativity: (f∘g)∘h = f∘(g∘h)

Page 9: Truth, deduction, computation   lecture f

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

Page 10: Truth, deduction, computation   lecture f

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 }

Page 11: Truth, deduction, computation   lecture f

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)

Page 12: Truth, deduction, computation   lecture f

Peano Arithmetic Illustrated

Giuseppe Peano Domino model of Peano Arithmetic

Page 13: Truth, deduction, computation   lecture f

Peano Arithmetic: define +

● x+0 = x● x+s(y) = s(x+y)● have a monoid

Page 14: Truth, deduction, computation   lecture f

Peano Arithmetic: define ×

● x×0 = 0● x×s(y) = x + (x×y)● have a monoid

Page 15: Truth, deduction, computation   lecture f

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)

Page 16: Truth, deduction, computation   lecture f

Peano Arithmetic: Universal Property

Page 17: Truth, deduction, computation   lecture f

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

Page 18: Truth, deduction, computation   lecture f

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

Page 19: Truth, deduction, computation   lecture f

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

Page 20: Truth, deduction, computation   lecture f

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

Page 21: Truth, deduction, computation   lecture f

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?

Page 22: Truth, deduction, computation   lecture f

Same things in Javascript

● x● function(x) {return x}● x(y)● (function(x) {return x})(z(t))● function(x) {return x(x)(x(x))}

Page 23: Truth, deduction, computation   lecture f

Rules of Transformation

● α-equivalence, renaming bound(*) variables● β-conversion (substitution/λ intro)● η-conversion

Page 24: Truth, deduction, computation   lecture f

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)

Page 25: Truth, deduction, computation   lecture f

α-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.

Page 26: Truth, deduction, computation   lecture f

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) } }}

Page 27: Truth, deduction, computation   lecture f

β-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’]

Page 28: Truth, deduction, computation   lecture f

β-conversion is β-reduction + β-abstraction

β-reduction

(λx E) E’ ⇒ E[x := E’]

β-abstraction

(λx E) E’ ⇐ E[x := E’]

Page 29: Truth, deduction, computation   lecture f

β-reduction in Javascript

(function(x) {return M})(N)

Substitute all x in M with N

Page 30: Truth, deduction, computation   lecture f

β-reduction - tautological case

● t = λx x // identity function, right?● t t = ?● (λx x) M = M● (λx x) (λa a) = λa a

Page 31: Truth, deduction, computation   lecture f

β-reduction - tautology, Javascript

id = function(x) { println(“<<x>>”); return x }

id(id)

Page 32: Truth, deduction, computation   lecture f

β-reduction - weird case

● t = λx (x x)● t t = ?● (λx (x x)) (λx (x x)) =...● (λx (x x)) (λx (x x)) - oops.

Page 33: Truth, deduction, computation   lecture f

That’s it for today