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

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
The lambda calculus David Walker CS 441. the (untyped) lambda calculus a language of functions e ::= x | \x.e | e1 e2 v ::= \x.e there are several different.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.
Foundations of Programming Languages: Introduction to Lambda Calculus
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,
CS 454 Theory of Computation Sonoma State University, Fall 2011 Instructor: B. (Ravi) Ravikumar Office: 116 I Darwin Hall Original slides by Vahid and.
CS5205: Foundation in Programming Languages Lecture 1 : Overview
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory.
Introduction to CS Theory Lecture 15 –Turing Machines Piotr Faliszewski
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
1 Relational Algebra and Calculas Chapter 4, Part A.
MATH 224 – Discrete Mathematics
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Alonzo Church: Mathematician. Philosopher. Computer Scientist? Who is Alonzo Church? Alonzo Church was a man who was very important to the computer science.
CMSC 330: Organization of Programming Languages Operational Semantics.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
Boolean Algebra and Computer Logic Mathematical Structures for Computer Science Chapter 7 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Boolean Logic.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
The Church-Turing Thesis Chapter Are We Done? FSM  PDA  Turing machine Is this the end of the line? There are still problems we cannot solve:
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
More on Lambda Calculus
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CS 320 Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Programming Languages and Compilers (CS 421)
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
The Church-Turing Thesis and Turing-completeness
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

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