Download presentation
Presentation is loading. Please wait.
Published byMelissa Franklin Modified over 9 years ago
1
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ
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 3302
3
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
4
CMSC 3304 Lambda Calculus A lambda calculus expression is defined as e ::= xvariable | λx.efunction | e efunction application λx.e is like (fun x -> e) in OCaml That’s it! Only higher-order functions
5
CMSC 3305 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/
6
CMSC 3306 Puzzle Pieces Hungry alligators: eat and guard family Old alligators: guard family Eggs: hatch into new family
7
CMSC 3307 Example Families Families are shown in columns Alligators guard families below them
8
CMSC 3308 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
9
CMSC 3309 Eating Rule Practice What happens to these alligators? Puzzle 1:
10
CMSC 33010
11
CMSC 33011
12
CMSC 33012
13
CMSC 33013
14
CMSC 33014
15
CMSC 33015
16
CMSC 33016
17
CMSC 33017
18
CMSC 33018
19
CMSC 33019
20
CMSC 33020 Eating Rule Practice What happens to these alligators? Puzzle 1:Puzzle 2: Answer 1:Answer 2:
21
CMSC 33021 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.
22
CMSC 33022 Puzzle Rule 3: The Old Alligator Rule An old alligator that is guarding only one family dies.
23
CMSC 33023 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)
24
CMSC 33024 Naming Families When family Not eats family True it becomes family False and when Not eats False it becomes True
25
CMSC 33025 Lambda Calculus A 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)
26
CMSC 33026 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
27
CMSC 33027 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
28
CMSC 33028 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 → z y (λz.z) y → y z y λy.z y λy.((λz.z z)y)x → (λz.z z)x → x x
29
CMSC 33029 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
30
CMSC 33030 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)
31
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
32
Church-Turing Thesis All algorithms can be represented by lambda calculus and executed on a Turing machine. CMSC 330 Turing Machines Alan Turing (1936) Lambda Calculus Alonzo Church (1940) Church-Turing Thesis Stephen Kleene (1952)
33
CMSC 33033 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...
34
CMSC 33034 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
35
CMSC 33035 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
36
CMSC 33036 Pairs (a,b) = λx.if x then a else b fst = λf.f true snd = λ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
37
CMSC 33037 Natural Numbers (Church*) 0 = λf.λy.y 1 = λf.λy.f y 2 = λ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) true –Recall that this is equivalent to λg.((g (λy.false)) true) *(Named after Alonzo Church, developer of lambda calculus)
38
CMSC 33038 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
39
CMSC 33039 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.
40
CMSC 33040 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
41
CMSC 33041 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)) =...
42
CMSC 33042 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
43
CMSC 33043 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.