Untyped Lambda-Calculus

Untyped Lambda-Calculus Programming with the Lambda-Calculus ´ ˜ Nestor Catano [email protected] Faculty of Engineering Pontificia Universidad Javer...
Author: Mary Hunter
35 downloads 0 Views 74KB Size
Untyped Lambda-Calculus Programming with the Lambda-Calculus ´ ˜ Nestor Catano [email protected]

Faculty of Engineering Pontificia Universidad Javeriana

Untyped Lambda-Calculus – p.1/14

Lecture Plan 1. Untyped λ-calculus Introduction Deduction in the λ-calculus Reduction in the λ-calculus Programming with the λ-calculus

Untyped Lambda-Calculus – p.2/14

Lecture Plan 1. Untyped λ-calculus Introduction Deduction in the λ-calculus Reduction in the λ-calculus Programming with the λ-calculus

Untyped Lambda-Calculus – p.3/14

Programming With the Lambda Calculus: Arithmetic

Untyped Lambda-Calculus – p.4/14

Programming With the Lambda Calculus: Arithmetic 0 ≡ λx.λy. y 1 ≡ λx.λy. xy 2 ≡ λx.λy. x(xy) 3 ≡ λx.λy. x(x(xy))

Untyped Lambda-Calculus – p.4/14

Programming With the Lambda Calculus: Arithmetic 0 ≡ λx.λy. y 1 ≡ λx.λy. xy 2 ≡ λx.λy. x(xy) 3 ≡ λx.λy. x(x(xy)) s ≡ λn.λx.λy. x(n x y)

Untyped Lambda-Calculus – p.4/14

Programming With the Lambda Calculus: Arithmetic 0 ≡ λx.λy. y 1 ≡ λx.λy. xy 2 ≡ λx.λy. x(xy) 3 ≡ λx.λy. x(x(xy)) s ≡ λn.λx.λy. x(n x y) + ≡ λx.λy. xsy

Untyped Lambda-Calculus – p.4/14

Programming With the Lambda Calculus: Arithmetic 0 ≡ λx.λy. y 1 ≡ λx.λy. xy 2 ≡ λx.λy. x(xy) 3 ≡ λx.λy. x(x(xy)) s ≡ λn.λx.λy. x(n x y) + ≡ λx.λy. xsy ∗ ≡ λx.λy. x (+ y) 0

Untyped Lambda-Calculus – p.4/14

Conditionals and Truth Values t ≡ λt.λe.t f ≡ λt.λe.e

Untyped Lambda-Calculus – p.5/14

Conditionals and Truth Values t ≡ λt.λe.t f ≡ λt.λe.e and ≡ λp.λq. p q f or ≡ λp.λq. p t q not ≡ λp. p f t if ≡ λp.λt.λe. pte 0? ≡ λx.(x(λy.f )t)

Untyped Lambda-Calculus – p.5/14

Arithmetic, Conditionals and Truth Values β

Show that s 1 → →2 β

Show that + 2 1 → →3 β

Show that and t f → →f β

Show that if (0? 1) 2 3 → →3

Untyped Lambda-Calculus – p.6/14

Data Structures: Pairs pair ≡ λf.λs.λi. i f s f irst ≡ λp. p t second ≡ λp. p f

Untyped Lambda-Calculus – p.7/14

Arithmetic, Conditionals and Truth Values β

Show that second (pair 2 3) → →3

Untyped Lambda-Calculus – p.8/14

Primitive Recursion A function f is primitive recursive if it can be provided a definition of the following form : f (0) = m f (k + 1) = g(k, f (k))

Untyped Lambda-Calculus – p.9/14

Primitive Recursion A function f is primitive recursive if it can be provided a definition of the following form : f (0) = m f (k + 1) = g(k, f (k))

k 0 1 2 ... k k+1 ... n

f (k) m g(0, m) g(1, f (1)) ... f (k) g(k, f (k)) ... f (n)

λg. λp. pair (s (f irst p)) (g (f irst p) (second p)) make ≡ λg. λm. λn. second (n (transf orm g) (pair 0 m)) f ≡ λn. make g m n transf orm ≡

Untyped Lambda-Calculus – p.9/14

The Predecessor Function f (0) = m f (k + 1) = g(k, f (k))

Untyped Lambda-Calculus – p.10/14

The Predecessor Function f (0) = m f (k + 1) = g(k, f (k))

pred(0) = 0 pred(k + 1) = k

Untyped Lambda-Calculus – p.10/14

The Predecessor Function f (0) = m f (k + 1) = g(k, f (k))

pred(0) = 0 pred(k + 1) = k

That is m is 0 and g of two arguments is the first argument

Untyped Lambda-Calculus – p.10/14

The Predecessor Function f (0) = m f (k + 1) = g(k, f (k))

pred(0) = 0 pred(k + 1) = k

That is m is 0 and g of two arguments is the first argument pred ≡ λn. make t 0 n

Untyped Lambda-Calculus – p.10/14

The Factorial f (0) = m f (k + 1) = g(k, f (k))

Untyped Lambda-Calculus – p.11/14

The Factorial f (0) = m f (k + 1) = g(k, f (k))

f act(0) = 1 f act(k + 1) = (k + 1) ∗ f act(k)

Untyped Lambda-Calculus – p.11/14

The Factorial f (0) = m f (k + 1) = g(k, f (k))

f act(0) = 1 f act(k + 1) = (k + 1) ∗ f act(k)

That is m is 1 and g of two arguments multiplies the successor of the first argument and the second argument

Untyped Lambda-Calculus – p.11/14

The Factorial f (0) = m f (k + 1) = g(k, f (k))

f act(0) = 1 f act(k + 1) = (k + 1) ∗ f act(k)

That is m is 1 and g of two arguments multiplies the successor of the first argument and the second argument f act ≡ λn. make (λk. λr. ∗ (s k) r) 1 n

Untyped Lambda-Calculus – p.11/14

Fibonacci f (0) = m f (k + 1) = g(k, f (k))

Untyped Lambda-Calculus – p.12/14

Fibonacci f (0) = m f (k + 1) = g(k, f (k))

f ib(0) = 1, f ib(1) = 1 f ib(k + 1) = f ib(k) + f ib(k − 1)

Untyped Lambda-Calculus – p.12/14

Fibonacci f (0) = m f (k + 1) = g(k, f (k))

f ib(0) = 1, f ib(1) = 1 f ib(k + 1) = f ib(k) + f ib(k − 1) k 0 1 2 3 4 ...

f ib(k) (1, 0) (1, 1) (2, 1) (3, 2) (5, 3) ...

Untyped Lambda-Calculus – p.12/14

Fibonacci f (0) = m f (k + 1) = g(k, f (k))

f ib(0) = 1, f ib(1) = 1 f ib(k + 1) = f ib(k) + f ib(k − 1) k 0 1 2 3 4 ...

f ib(k) (1, 0) (1, 1) (2, 1) (3, 2) (5, 3) ...

That is m is (pair 1 0) and g is a function that ...

Untyped Lambda-Calculus – p.12/14

Fibonacci f (0) = m f (k + 1) = g(k, f (k))

f ib(0) = 1, f ib(1) = 1 f ib(k + 1) = f ib(k) + f ib(k − 1) k 0 1 2 3 4 ...

f ib(k) (1, 0) (1, 1) (2, 1) (3, 2) (5, 3) ...

That is m is (pair 1 0) and g is a function that ... f ib ≡ λn. f irst (make (λk. λr. (pair (+ (f irst r) (second r)) (f irst r))) (pair 1 0) n)

Untyped Lambda-Calculus – p.12/14

Addition f (0) = m f (k + 1) = g(k, f (k))

add(0, y) = y add(k + 1, y) = add(k, y) + 1

Untyped Lambda-Calculus – p.13/14

Addition f (0, y) = m f (k + 1, y) = g(k, f (k, y))

add(0, y) = y add(k + 1, y) = add(k, y) + 1

Untyped Lambda-Calculus – p.14/14

Addition f (0, y) = m f (k + 1, y) = g(k, f (k, y))

add(0, y) = y add(k + 1, y) = add(k, y) + 1

That is m is y and g of two arguments returns the successor of the second argument

Untyped Lambda-Calculus – p.14/14

Addition f (0, y) = m f (k + 1, y) = g(k, f (k, y))

add(0, y) = y add(k + 1, y) = add(k, y) + 1

That is m is y and g of two arguments returns the successor of the second argument add ≡ λn. λy. make (λk. λr. s r) y n

Untyped Lambda-Calculus – p.14/14