untyped λ calculusrxg/cpsc509/untypedlambdacalculus.pdfthe calculi of lambdaconversion. (book)...
Embed Size (px)
TRANSCRIPT

Faridah Akinotcho, Lily Bryant, Junze Wu
Untyped λ  Calculus

Outline
● History
● Construction and reductions
● Encoding data types and structures
● Recursion and the Y combinator
● Variants and influence

Historical Figures

Why λ?

Formal Descriptionλ

Definition of λtermsAssume infinite set of variables V = {x, y, z, …}.
Set Λ of λterms:
(Variable) If u ∊ V, then u ∊ Λ.(Application) If M and N ∊ Λ, then (M N) ∊ Λ.(Abstraction) If u ∊ V and M ∊ Λ, then (λu.M) ∊ Λ.
Example:
((λx.x) (λy.(y z)))

Terminology: Scoping and Boundness
Free Variables
FV: Λ ➝ 𝒫(V)(Variable) FV(x) = x(Abstraction) FV(λx.M) = FV(M) \ {x}(Application) FV(M N) = FV(M) ∪ FV(N)
➡ A λterm M is closed if FV(M) = ∅.M is also called a combinator.
Bound Variables
BV: Λ ➝ 𝒫(V)(Variable) BV(x) = ∅
(Abstraction) BV(λx.M) = {x} ∪ BV(M)(Application) BV(M N) = BV(M) ∪ BV(N)

Terminology: SubstitutionGiven a λterm M, we denote the substitution of a variable x in M by N as M[x:=N] such that:
● x[x:=N] = N
● y[x:=N] = y if x ≠ y
● (PQ)[x:=N] = (P[x:=N])(Q[x:=N])
● (λy.P)[x:=N] = λz.(Pʸ�ᶻ[x:=N]) where Pʸ�ᶻ stands for P with every instance of y renamed to z, such that z ∉ FV(N)

Terminology: Currying
A common shorthand is to use multiargument functions. These can be rewritten as nested singleargument functions where each “level” takes an argument and returns another function.
Examples:
(λx y.(x y)) ≣ (λx.(λy.(x y)))
((λx y.(x y)) 5) ≣ ((λx.(λy.(x y))) 5) →ᵦ (λy.(5 y)) “partial application”

Reductions
● αconversion: Argument renaming in functions
● βreduction: Function application with substitution● ηconversion: Function equivalence

Reductions: 𝛼conversionLet M ∊ Λ.
𝛼conversion performs the following renaming, provided y ∉ FV(M) and y ∉ BV(M):
λx.M =𝛼 λy.(M[x:=y])
➡ λx.x and λz.z are 𝛼equivalent, but not syntacticallyequivalent.

Reductions: βreduction semantics

Reductions: βreduction terminology
➡ The left hand side is called a redex and the right hand side is the contractum.
➡ If P →ᵦ L, then P and L are said to be βequivalent, denoted P =ᵦ L.

Reductions: βreduction terminology
➡M is in βnormal form (or βnf) if M does not contain a redex.
➡ M is weaklynormalizable if there exists some N in βnf where M ↠ᵦ N.
➡M is stronglynormalizable if there does not exist an infinite reduction
path from M to N.

Reductions: βreduction theorems
ChurchRosser Theorem
λcalculus is confluent under βreductions, i.e. supposing M, M1, M2, L ∊ Λ,
If M ↠ᵦ M1 and M ↠ᵦ M2, then there exists some L such that M1 ↠ᵦ L andM2 ↠ᵦ L.
Consequences:
➡ A λterm has at most one normal form.➡ Ordering of reduction choice does not matter (nondeterministic reduction!). This even applies in many typed versions of the calculus.

Reductions: βreduction theoremsA set L ⊆ Λ is closed under βconversion if
∀M, N ∊ Λ, ((M ∊ L ∧ M =ᵦ N) ⇒ N ∊ L).
ScottCurry Theorem
If two nonempty, disjoint sets of λterms are closed under βconversion,
then they are recursively inseparable.
Consequence:
➡ Showing two nonempty λterms are βequivalent is undecidable.

If function F takes its argument and immediately applies the argument to G, then F is functionallyequivalent to G.
λx.(M x) ⟶η M where x ∉ FV(M)
➡ Intuitively: two functions are identical if they do the same thing.
Reductions: ηconversion

Encoding data types and structures

Boolean LogicTruth values
TRUE := λx y. x
FALSE := λx y. y
With these two terms, we can define:
NOT := λp. ((p FALSE) TRUE)AND := λp q. ((p q) p)OR := λp q. ((p p) q)
IFTHENELSE := λc t e. ((c t) e))

NumbersChurch numerals
ZERO := λf x. x
ONE := λf x. (f x)
TWO := λf x. (f (f x))
THREE := λf x. (f (f (f x)))
…
n := λf x. f n x
Peano numbers
A NATURAL is one of:
● ZERO
● SUCC NATURAL
SUCC := λn f x. (f ((n f) x))

ArithmeticElements needed: successor, predecessor, addition, subtraction,
multiplication, division, exponentiation.
Immediately, we can define:
PLUS:= λm n. ((m SUCC) n)
MULT := λm n. ((m (PLUS n)) ZERO)
Alternatively, MULT := λm n f. (m (n f))
EXP := λm n. (n m)

Pairs and Lists
For predecessor and subtraction, we first need to introduce pairs:
Encoding pairs and lists
CONS := (λa b s.((s a) b))
CAR := (λp.(p TRUE))
CDR := (λp.(p FALSE))
NIL := FALSE

Arithmetic (cont.)The “wisdom tooth trick”:
T := (λp.((CONS (SUCC (CAR p))) (CAR p)))))
PRED := (λn.(CDR ((n T) ((CONS ZERO) ZERO))))
SUB := (λm n.(n PRED) m))
Note: PRED ZERO is ZERO

PredicatesZERO? :=λn.((n (λx. FALSE)) TRUE)LEQ? := λm n.ZERO?((SUB m) n)
GT? := λm n.NOT ((LEQ? m) n)
EQ? := λm n.((AND ((LEQ? m) n)) ((LEQ? n) m)))

Named Constants
We can introduce let binding, with the following relation:
LET x = n IN m ≣ ((λx.m) n)

Arithmetic (cont.)
Division is a recursive procedure:
(define (divide n m)
(if (>= n m)
(+ 1 (divide ( n m) m))
0))
But we don’t have a way to do recursion yet...

Recursion Y Combinator

An example, the factorial function
#lang racket
(define factorial
(λ (n)
(if (zero? n)
1
(* n (factorial (sub1 n))))))
;; Sadly, we can’t do this in λcalculus

An example, the factorial function
#lang racket
(define almostfactorial
(λ (f)
(λ (n)
(if (zero? n)
1
(* n (f (sub1 n)))))))
What function should we pass in for f in order to get the actual factorial function back?
It is the actual factorial function that we are trying to define!

An example, the factorial function
So the f that we are looking for is a “fixedpoint” of almostfactorial:
(almostfactorial f) = f

FixedpointA function has a fixed point at a if f(a) = a.
Function f applied to a returns a again: “Fixed” by f.
Examples (functions defined on real numbers):
f(n) = n2 has two fixed points: 0 and 1
f(n) = n + 1 has no fixed points
f(f(f ...f(f(f(f(a))...) = a

Fixedpoint theorem
For all L ∈ Λ there is M ∈ Λ such that (L M) =ᵦ M.
Proof:
For given L, define M := ((λx.(L(x x))) (λx.(L(x x)))).
This M is a redex, so we have:
M ≡ ((λx. L(x x))(λx. L(x x))) →ᵦ L(((λx. L(x x)) (λx. L(x x)))) ≡ (L M).
Hence, (L M) =ᵦ M.

Recursion: Y combinator
Fixedpoint combinator:
Y := λf.(λx.(f (x x)) λx.(f (x x)))
Y is a higher order function that computes the fixedpoint of the argument function. For any λterm f, (Y f) is a fixed point of f:
(Y f) = (f (Y f))

An example, the factorial function
#lang lazy
(define Y (λ (f) ((λ (x) (f (x x))) (λ (x) (f (x x))))))
(define factorial (Y
(λ (f)
(λ (n)
(if (zero? n)
1
(* n (f (sub1 n))))))))

An example, the factorial function
#lang lazy
(define Y (λ (f) ((λ (x) (f (x x))) (λ (x) (f (x x))))))
(define FACTORIAL (Y
(λ (f)
(λ (n)
(((ZERO? n)
ONE)
((MULT n) (f (PRED n))))))))

Division(define divide
(Y (λ (f)
(λ (n)
(λ (m)
(if (>= n m)
(add1 ((f ( n m)) m))
0))))))

Variants and Influence

Variants in Programming

Reduction Strategies
We define a strategy as a function S,
S: Λ → Λ
If S(M) = N, then M →ᵦ N.

Reduction Strategies
Full Beta Reductions : Any redex can be reduced at any time
Applicative Order Reduction : The rightmost, innermost term is always evaluated first, i.e. the arguments to a function are always evaluated firstIt is a callbyvalue evaluation strategy.
((𝜆x.M) (𝜆y.P))

Reduction StrategiesNormal Order Reduction : The leftmost, outermost redex is always reduced first, i.e. whenever possible the arguments are substituted into the body of an abstraction before they are reduced.
(((𝜆x.M) N) (𝜆y.P))
It is similar to callbyname strategy except reductions can happen in the body of an abstraction, ex:
λx.(λx.x)x is in normal form according to the callbyname strategy, but not the normal order since it contains the redex (λx.x)x

Reduction Strategies: an example
We define omega, a combinator Ω=((λx.(xx))(λx.(xx)))
Let M:=(𝜆u.v) Ω
There is an infinite reduction by contracting Ω and a one step reduction, in other words M is weakly normalizing.

Typed λcalculus

Functional Programming● λcalculus and its variants provide the theoretical foundation for
functional languages such as Lisp, ML, and Scheme.● System F is a typed λcalculus that formed the basis for some typed
functional languages, such as Haskell and OCaml, and define parameter polymorphism: the idea allowing of generic type variables.
● Anonymous functions and other aspects of functional programming have also been introduced into many nonfunctional languages, such as Java, C, and MATLAB.

Variants in Logic

De Bruijn IndicesAlternative to Church’s notation which removes the overhead of naming variables.
Each variable becomes a natural number equal to the number of variables/lambdas introduced between its definition and usage (inclusive).
Example:
((𝜆x.𝜆y.𝜆z. w y x z) (𝜆u.𝜆v.v u)) � ((𝜆 𝜆 𝜆 4 2 3 1) (𝜆 𝜆 1 2))
w is a free variable
➡ αequivalent (renamed) terms are now syntacticallyequivalent

SKI calculusS: (S x y z) ≣ (λx y z.((x z) (y z))) →ᵦ (x z (y z))
K: (K x y) ≣ (λx y.x) →ᵦ x
I: (I x) → 𝜂 ((S K K) x) →ᵦ x
➡ For all M ∊ Λ, there exists a combination of S and K that is functionallyequivalent to M. S and K form the basis of all λterms.
➡ Essentially, a tiny Turingcomplete language!
➡ Combinatory logic was developed around the same time as λcalculus and works with this idea of combinators (without abstraction) as a basis for reasoning.

Conclusion𝜆calculus is..
● A simple way of capturing computability while still providing many powerful results
● Very helpful for building up the logical basis of functional reasoning● Less helpful for finding intuition on very high level descriptions of
algorithms

References(Book) Church, A. (1941). The calculi of lambdaconversion.
(Book) Barendregt, H.P. (1984) .The Lambda Calculus Its Syntax and Semantics.
(Paper) Barendregt, H.P. (1997). The Impact of the Lambda Calculus in Logic and Computer Science.
(Paper) Plotkin, G.D. (1974). CallbyName, CallbyValue, and The Lambda Calculus.
(Course notes, chapter 4) Felleisen, M & Flatt, M. (2007). Programming Languages and Lambda Calculi.
(Paper) Felleisen, M. & Friedman, D. P .(1989). A Syntactic Theory of Sequential State.
(Paper) Moggi, E. (1989). Computational lambdacalculus and monads.
(Book) Barendregt, H. P. & Dekkers, W., Statman, R. (2013). Lambda calculus with types.
(Book) Nederpelt, R. & Geuvers, H. (2014). Type theory and Formal Proofs.
(Screencast) Lambda Calculus: PyCon 2019 Tutorial