CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.

Post on 12-Jan-2016

245 views 0 download

Transcript of CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.

CMSC 330: Organization of Programming Languages

Lambda Calculus Introduction

λ

CMSC 330 2

Review of CMSC 330

• Syntax– Regular expressions– Finite automata– Context-free grammars

• Semantics– Operational semantics– Lambda calculus

• Implementation– Concurrency

– Generics

– Argument passing

– Scoping

– Exceptions

CMSC 330 3

Introduction

• We’ve seen that several standard language “conveniences” aren’t strictly necessary– Multi-argument functions: use currying or tuples– Loops: use recursion– Side-effects: we don't need them either

• Goal: come up with a “core” language that’s as small as possible and still Turing complete– This will give a way of illustrating important language

features and algorithms

CMSC 330 4

Lambda Calculus

• A lambda calculus expression is defined as

e ::= x variable

| λx.e function

| e e function application

• λx.e is like (fun x -> e) in OCaml

• That’s it! Only higher-order functions

CMSC 330 5

Intuitive Understanding

• Before we work more with the mathematical notation of lambda calculus, we’re going to play a puzzle game!

http://worrydream.com/AlligatorEggs/

CMSC 330 6

Puzzle Pieces

• Hungry alligators: eat and guard family

• Old alligators: guard family

• Eggs: hatch into new family

CMSC 330 7

Example Families

• Families are shown in columns• Alligators guard families below them

CMSC 330 8

Puzzle Rule 1: The Eating Rule

• If families are side-by-side:– Top left alligator eats

the entire family to her right

– The top left alligator dies

– Any eggs she was guarding of the same color hatch into what she just ate

CMSC 330 9

Eating Rule Practice

• What happens to these alligators?

Puzzle 1:

CMSC 330 10

CMSC 330 11

CMSC 330 12

CMSC 330 13

CMSC 330 14

CMSC 330 15

CMSC 330 16

CMSC 330 17

CMSC 330 18

CMSC 330 19

CMSC 330 20

Eating Rule Practice

• What happens to these alligators?

Puzzle 1: Puzzle 2:

Answer 1: Answer 2:

CMSC 330 21

Puzzle Rule 2: The Color Rule

• If an alligator is about to eat a family and a color appears in both families then we need to change that color in one of the families.– In the picture below, green and red appear in both the first and second

families. So, in the second family, we switch all of the greens to cyan, and all of the reds to blue.

• If a color appears in both families, but only as an egg, no color change is made.

CMSC 330 22

Puzzle Rule 3: The Old Alligator Rule

• An old alligator that is guarding only one family dies.

CMSC 330 23

Challenging Puzzles!

• Try to reduce these groups of alligators as much as possible using the three puzzle rules:

• More links on website (see “Resources” page)

CMSC 330 24

Naming Families

• When family Not eats family True it becomes family False and when Not eats False it becomes True

CMSC 330 25

Lambda Calculus

• A lambda calculus expression is defined as

e ::= x variable (x: egg)

| λx.e function (λx: alligator)

| e e function application

(adjacency of families)

• Use parentheses to group expressions

(old alligators)

CMSC 330 26

Precedence and Associativity

• The scope of λ extends as far to the right as possible– λx.λy.x y is λx.(λy.(x y))

• Function application is left-associative– x y z is (x y) z– Same rule as OCaml

CMSC 330 27

Operational Semantics

• All we’ve got are functions, so all we can do is call them

• To evaluate (λx.e1) e2– Evaluate e1 with x bound to e2

• This application is called “beta-reduction”– (λx.e1) e2 → e1[x/e2] (the eating rule)

• e1[x/e2] is e1 where occurrences of x are replaced by e2• Slightly different than the environments we saw for Ocaml

– Substitutions instead of environments

CMSC 330 28

Examples

• (λx.x) z →• (λx.y) z →• (λx.x y) z →

– A function that applies its argument to y

• (λx.x y) (λz.z) →• (λx.λy.x y) z →

– A curried function of two arguments that applies its first argument to its second

• (λx.λy.x y) (λz.z z) x →

zy

(λz.z) y → y

z y

λy.z y

λy.((λz.z z)y)x → (λz.z z)x → x x

CMSC 330 29

Static Scoping and Alpha Conversion

• Lambda calculus uses static scoping

• Consider the following– (λx.x (λx.x)) z → ?

• The rightmost “x” refers to the second binding

– This is a function that takes its argument and applies it to the identity function

• This function is “the same” as (λx.x (λy.y))– Renaming bound variables consistently is allowed

• This is called alpha-renaming or alpha conversion (color rule)

– Ex. λx.x = λy.y = λz.z λy.λx.y = λz.λx.z

CMSC 330 30

Static Scoping (cont’d)

• How about the following?– (λx.λy.x y) y → ?– When we replace y inside, we don’t want it to be

“captured” by the inner binding of y

• This function is “the same” as (λx.λz.x z)

CMSC 330

Church-Turing Thesis

• Informally: If an algorithm exists to carry out a particular calculation, then– the same calculation can also be computed by a

Turing machine, and– the same calculation can also be represented by a

λ-function.

• More of a definition than a thesis– Cannot be proven– However, is nearly universally accepted

CMSC 330

Church-Turing Thesis

All algorithms can be represented by lambda calculus and executed on a Turing machine.

Turing MachinesAlan Turing

(1936)

Lambda CalculusAlonzo Church

(1940)

Church-Turing ThesisStephen Kleene

(1952)

CMSC 330 33

What does this mean?

• The Church-Turing thesis implies that we can encode any computation we want in lambda calculus– ... but ONLY if we’re sufficiently clever...

CMSC 330 34

Booleans

true = λx.λy.x

false = λx.λy.y

if a then b else c is defined to be the λ expression: a b c

• Examples:– if true then b else c → (λx.λy.x) b c → (λy.b) c → b– if false then b else c → (λx.λy.y) b c → (λy.y) c → c

CMSC 330 35

Booleans

Other Boolean operations:

not = λx.((x false) true)

and = λx.λy.((x y) false)

or = λx.λy.((x true) y)

• Given these operations, we can build up a logical inference system

• Exercise: Show that not, and and or have the desired properties

CMSC 330 36

Pairs

(a,b) = λx.if x then a else bfst = λf.f truesnd = λf.f false

• Examples:– fst (a,b) = (λf.f true) (λx.if x then a else b) → (λx.if x then a else b) true → if true then a else b → a– snd (a,b) = (λf.f false) (λx.if x then a else b) → (λx.if x then a else b) false → if false then a else b → b

CMSC 330 37

Natural Numbers (Church*)

0 = λf.λy.y1 = λf.λy.f y2 = λf.λy.f (f y)3 = λf.λy.f (f (f y))

i.e., n = λf.λy.<apply f n times to y>

succ = λz.λf.λy.f (z f y)iszero = λg.g (λy.false) true

– Recall that this is equivalent to λg.((g (λy.false)) true)

*(Named after Alonzo Church, developer of lambda calculus)

CMSC 330 38

Natural Numbers (cont’d)

• Examples:succ 0 =

(λz.λf.λy.f (z f y)) (λf.λy.y) →

λf.λy.f ((λf.λy.y) f y) →

λf.λy.f y = 1

iszero 0 =

(λz.z (λy.false) true) (λf.λy.y) →

(λf.λy.y) (λy.false) true →

(λy.y) true →

true

CMSC 330 39

Arithmetic defined• Addition, if M and N are integers (as λ expressions):

M + N = λx.λy.(M x)((N x) y)Equivalently: + = λM.λN.λx.λy.(M x)((N x) y)

• Multiplication: M * N = λx.(M (N x))• Prove 1+1 = 2.

1+1 = λx.λy.(1 x)((1 x) y) →λx.λy.((λx.λy.x y) x)(((λx.λy.x y) x) y) → λx.λy.(λy.x y)(((λx.λy.x y) x) y) → λx.λy.(λy.x y)((λy.x y) y) →λx.λy.x ((λy.x y) y) →λx.λy.x (x y) = λf.λy.f (f y) = 2

• With these definitions, can build a theory of integer arithmetic.

CMSC 330 40

Looping

• Define D = λx.x x• Then

– D D = (λx.x x) (λx.x x) → (λx.x x) (λx.x x) = D D

• So D D is an infinite loop– In general, self application is how we get looping

CMSC 330 41

The “Paradoxical” Combinator

Y = λf.(λx.f (x x)) (λx.f (x x))• Then

Y g =

(λf.(λx.f (x x)) (λx.f (x x))) g →

(λx.g (x x)) (λx.g (x x)) →

g ((λx.g (x x)) (λx.g (x x)))

= g (Y g)

• Thus Y g = g (Y g) = g (g (Y g)) = ...

CMSC 330 42

Example

fact = λf. λn.if n = 0 then 1 else n * (f (n-1))– The second argument to fact is the integer– The first argument is the function to call in the body

• We’ll use Y to make this recursively call fact

(Y fact) 1 = (fact (Y fact)) 1

→ if 1 = 0 then 1 else 1 * ((Y fact) 0)

→ 1 * ((Y fact) 0)

→ 1 * (fact (Y fact) 0)

→ 1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1))

→ 1 * 1 → 1

CMSC 330 43

Discussion

• Using encodings we can represent pretty much anything we have in a “real” language– But programs would be pretty slow if we really

implemented things this way– In practice, we use richer languages that include built-

in primitives

• Lambda calculus shows all the issues with scoping and higher-order functions

• It's useful for understanding how languages work