CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ
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 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 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
CMSC 3305 Intuitive Understanding Before we work more with the mathematical notation of lambda calculus, we’re going to play a puzzle game!
CMSC 3306 Puzzle Pieces Hungry alligators: eat and guard family Old alligators: guard family Eggs: hatch into new family
CMSC 3307 Example Families Families are shown in columns Alligators guard families below them
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
CMSC 3309 Eating Rule Practice What happens to these alligators? Puzzle 1:
CMSC 33010
CMSC 33011
CMSC 33012
CMSC 33013
CMSC 33014
CMSC 33015
CMSC 33016
CMSC 33017
CMSC 33018
CMSC 33019
CMSC Eating Rule Practice What happens to these alligators? Puzzle 1:Puzzle 2: Answer 1:Answer 2:
CMSC 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 Puzzle Rule 3: The Old Alligator Rule An old alligator that is guarding only one family dies.
CMSC 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 Naming Families When family Not eats family True it becomes family False and when Not eats False it becomes True
CMSC 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)
CMSC 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 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 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
CMSC 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 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)
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. CMSC 330 Turing Machines Alan Turing (1936) Lambda Calculus Alonzo Church (1940) Church-Turing Thesis Stephen Kleene (1952)
CMSC 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 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 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 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
CMSC 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)
CMSC 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 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 = = λ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 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 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 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 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