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

of 43 /43
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ

Embed Size (px)

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

PowerPoint Presentation

CMSC 330: Organization of Programming LanguagesLambda Calculus Introduction

1Review of CMSC 330SyntaxRegular expressionsFinite automataContext-free grammarsSemanticsOperational semanticsLambda calculusImplementationConcurrencyGenericsArgument passingScopingExceptionsCMSC 33022CMSC 3303IntroductionWeve seen that several standard language conveniences arent strictly necessaryMulti-argument functions: use currying or tuplesLoops: use recursionSide-effects: we don't need them eitherGoal: come up with a core language thats as small as possible and still Turing completeThis will give a way of illustrating important language features and algorithms

3CMSC 3304Lambda CalculusA lambda calculus expression is defined as

e ::= xvariable | x.efunction | e efunction application

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

Thats it! Only higher-order functions4CMSC 3305Intuitive UnderstandingBefore we work more with the mathematical notation of lambda calculus, were going to play a puzzle game!

http://worrydream.com/AlligatorEggs/

5CMSC 3306Puzzle PiecesHungry alligators: eat and guard family

Old alligators: guard family

Eggs: hatch into new family

6CMSC 3307Example FamiliesFamilies are shown in columnsAlligators guard families below them

7CMSC 3308Puzzle Rule 1: The Eating RuleIf families are side-by-side:Top left alligator eats the entire family to her rightThe top left alligator diesAny eggs she was guarding of the same color hatch into what she just ate

8CMSC 3309Eating Rule PracticeWhat happens to these alligators?Puzzle 1:

9CMSC 33010

10CMSC 33011

11CMSC 33012

12CMSC 33013

13CMSC 33014

14CMSC 33015

15CMSC 33016

16CMSC 33017

17CMSC 33018

18CMSC 33019

19CMSC 33020Eating Rule PracticeWhat happens to these alligators?Puzzle 1:Puzzle 2:

Answer 1:Answer 2:

20CMSC 33021Puzzle Rule 2: The Color RuleIf 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.

21CMSC 33022Puzzle Rule 3: The Old Alligator RuleAn old alligator that is guarding only one family dies.

22CMSC 33023Challenging Puzzles!Try to reduce these groups of alligators as much as possible using the three puzzle rules:

More links on website (see Resources page)

23CMSC 33024Naming FamiliesWhen family Not eats family True it becomes family False and when Not eats False it becomes True

24CMSC 33025Lambda CalculusA lambda calculus expression is defined as

e ::= xvariable (x: egg) | x.efunction (x: alligator) | e efunction application(adjacency of families)

Use parentheses to group expressions(old alligators)

25CMSC 33026Precedence and AssociativityThe scope of extends as far to the right as possiblex.y.x y is x.(y.(x y))

Function application is left-associativex y z is (x y) zSame rule as OCaml26CMSC 33027Operational SemanticsAll weve got are functions, so all we can do is call themTo evaluate (x.e1) e2Evaluate e1 with x bound to e2This 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 e2Slightly different than the environments we saw for OcamlSubstitutions instead of environments27CMSC 33028Examples(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 yz y y.z y y.((z.z z)y)x (z.z z)x x x28CMSC 33029Static Scoping and Alpha ConversionLambda calculus uses static scoping

Consider the following(x.x (x.x)) z ?The rightmost x refers to the second bindingThis 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 allowedThis is called alpha-renaming or alpha conversion (color rule)Ex. x.x = y.y = z.z y.x.y = z.x.z29CMSC 33030Static Scoping (contd)How about the following?(x.y.x y) y ?When we replace y inside, we dont want it to be captured by the inner binding of y

This function is the same as (x.z.x z)30Church-Turing ThesisInformally: If an algorithm exists to carry out a particular calculation, thenthe same calculation can also be computed by a Turing machine, andthe same calculation can also be represented by a -function.

More of a definition than a thesisCannot be provenHowever, is nearly universally acceptedCMSC 330Church-Turing ThesisAll algorithms can be represented by lambda calculus and executed on a Turing machine.

CMSC 330

Turing MachinesAlan Turing(1936)Lambda CalculusAlonzo Church(1940)Church-Turing ThesisStephen Kleene(1952)32CMSC 33033What does this mean?The Church-Turing thesis implies that we can encode any computation we want in lambda calculus... but ONLY if were sufficiently clever...33CMSC 33034Booleanstrue = x.y.xfalse = x.y.yif 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 bif false then b else c (x.y.y) b c (y.y) c c

34CMSC 33035BooleansOther 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 systemExercise: Show that not, and and or have the desired properties

35CMSC 33036Pairs(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 asnd (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

36CMSC 33037Natural 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.

succ = z.f.y.f (z f y)iszero = g.g (y.false) trueRecall that this is equivalent to g.((g (y.false)) true)*(Named after Alonzo Church, developer of lambda calculus)37CMSC 33038Natural Numbers (contd)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 true38CMSC 33039Arithmetic definedAddition, 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) = 2With these definitions, can build a theory of integer arithmetic.39CMSC 33040LoopingDefine D = x.x xThenD D = (x.x x) (x.x x) (x.x x) (x.x x) = D DSo D D is an infinite loopIn general, self application is how we get looping40CMSC 33041The Paradoxical CombinatorY = f.(x.f (x x)) (x.f (x x))ThenY 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)) = ...41CMSC 33042Examplefact = f. n.if n = 0 then 1 else n * (f (n-1))The second argument to fact is the integerThe first argument is the function to call in the bodyWell 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 142CMSC 33043DiscussionUsing encodings we can represent pretty much anything we have in a real languageBut programs would be pretty slow if we really implemented things this wayIn practice, we use richer languages that include built-in primitivesLambda calculus shows all the issues with scoping and higher-order functionsIt's useful for understanding how languages work43