CS321 Functional Programming 2 © JAS 2005 4-1 The λ Calculus This is a formal system to capture the ideas of function abstraction and application introduced.

Slides:



Advertisements
Similar presentations
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Advertisements

Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Catriel Beeri Pls/Winter 2004/5 functional-language 1 Substitution Semantics of FL – a simple functional language FL is EL + (non-recursive) function creation.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
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.
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.
Restricted Satisfiability (SAT) Problem
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.,
Slides 01 1 Type-Free λ-Calculus The λ-calculus is a family of prototype programming languages invented by the logician, Alonzo Church, in the 1930's.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Introduction to the λ-Calculus and Functional Programming Languages Arne Kutzner Hanyang University 2015.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
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.
Lexicografie computationala Feb., 2012 Anca Dinu University of Bucharest.
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.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Universidad Nacional de ColombiaUniversidad Nacional de Colombia Facultad de IngenieríaFacultad de Ingeniería Departamento de Sistemas- 2002Departamento.
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.
EXPRESSIONS. Vocabulary A variable is a symbol, usually a letter, used to represent a number. –Example: 4x (x is the variable) A coefficient is the number.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
CS321 Functional Programming 2 © JAS The SECD Machine The SECD machine has become a classic way of implementing functional languages. It is based.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.
CSE-321 Programming Languages -Calculus (II) POSTECH March 27, 2006 박성우.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
-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
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
STROUD Worked examples and exercises are in the text PROGRAMME F3 EXPRESSIONS and EQUATIONS.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
CS321 Functional Programming 2 © JAS Implementation using Combinators This approach builds upon the λ-calculus model. The aim is to translate.
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
Context-Free Grammars: an overview
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Lesson2 Lambda Calculus Basics
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
CHAPTER 2 Context-Free Languages
Announcements Quiz 6 HW7 due Tuesday, October 30
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

CS321 Functional Programming 2 © JAS The λ Calculus This is a formal system to capture the ideas of function abstraction and application introduced by Alzonzo Church and Haskell B Curry in the 1930s. A λ-term is defined as:- a variable x conventionally a lower case letter an abstraction λx.(M) an upper case letter conventionally represents any λ-term an application M N a bracketed term (M) Haskell has λ-terms – \x->M Note that application groups to the left – MNK means (MN)K

CS321 Functional Programming 2 © JAS Function application is straightforward. (λx.(f x)) y => f y substitute y for x in (f x) (λx.(f x)) y == [y/x](f x) (λx z.(z x)) y => (λx.(λz.(z x))) y => (λz.(z y)) (λx y.(y x)) y => (λx.(λy.(y x))) y => (λ.( )) but the two y ’s are different! y is said to be a bound variable (an argument) y is said to be a free variable (not bound) yyy

CS321 Functional Programming 2 © JAS We need to define the difference between free and bound variables. 1.The variable x occurs free in the expression x. No variable is bound in an expression consisting of a single variable. 2.The variable x occurs free (or bound) in YZ if and only if it occurs free (or bound) in Y or Z. 3.If a variable x does not occur in V then it occurs free (or bound) in λV.Y iff it occurs free (or bound) in Y. All occurrences of the elements of V are bound in λV.Y. 4.The variable x occurs free (or bound) in (Y) iff it occurs free (or bound) in Y.

CS321 Functional Programming 2 © JAS We can now define our substitution rules for [M/x]Y Rule 1 – single variables a) when Y is the variable x then [M/x]Y => M b) when Y is a variable other than x then [M/x]Y => Y Rule 2 – function applications when Y is the application W Z then [M/x]Y => ([M/x]W)([M/x]Z)

CS321 Functional Programming 2 © JAS Rule 3 – function abstraction We will assume all bound variables are preceded by a λ a)when Y is the abstraction λx.W then [M/x]Y => λx.W b) when Y is the abstraction λy.W and there are no free occurrences of x in W then [M/x]Y is λy.W c)when Y is the abstraction λy.W and there are no free occurrence of y in M then [M/x]Y is λy.[M/x]W d)when Y is the abstraction λy.W and there is a free occurrence of y in M then [M/x]Y is λz.[M.x]([z/y]W) where z is a variable name that does not occur free in M or Y. Therefore the binding of abstraction takes precedence and protects a function body from the effects of substitution if there is a name clash.

CS321 Functional Programming 2 © JAS Simplifying λ-terms There are 3 rules called conversion rules:- α : if there are no free occurrences of y in Z then λx.Z cnv α λy.[y/x]Z β : (λx.M) N cnv β [N/x] M η: if there are no free occurrences of x in M then (λx.M x) cnv η M β and η usually simplify (reduce the complexity of) expressions and are therefore called reduction rules.

CS321 Functional Programming 2 © JAS An expression that can be reduced is called a redex. (λx.M) N is a β-redex. (λx.M x) is an η-redex. An expression containing no redexes is said to be in Normal Form

CS321 Functional Programming 2 © JAS The First Church-Rosser Theorem If X cnv Y then there exists an expression Z such that X red Z and Y red Z. Corollary If an expression reduces to two normal forms then they must be inter-convertible using α-conversions.

CS321 Functional Programming 2 © JAS If A and B are two redexes in an expression M and the first occurrence of λ in A is to the left of the first occurrence of λ in B then A is said to be to the left of B. If A is a redex in M and it is to the left of all other redexes in M then A is called the leftmost redex of M. The Second Church-Rosser Theorem If X red Y and Y is in normal form then there is a reduction sequence from X to Y that involves successively reducing the leftmost redex. This is known as Normal Order Reduction

CS321 Functional Programming 2 © JAS Extending the λ-Calculus We will now add some simple extensions to the λ-calculus. (We could actually define these extensions with our existing calculus). numerical and logical constants basic arithmetic and logical operations a conditional expression construct plus = λx y. x + y max = λab. if a > b then a else b fib = λn.if n < 3 then 1 else fib(n-1)+fib(n-2)

CS321 Functional Programming 2 © JAS By simple referential transparency we should be able to substitute the λ -expression for fib, but if we do this we get an infinite expansion. To avoid this problem we need to define the concept of a Fixed Point of a function. The fixed points of a function are the set of values for which the function performs an identity transformation. fixed points of f = {X| forall xєX,f(x) = x} F = λgn.if n<3 then 1 else g(n-1)+g(n-2) fib is a fixed point of F

CS321 Functional Programming 2 © JAS Hypothesise existence of function Y that produces fixed point for arbitrary function f. f = Y F Every function (in a functional programming language) has at least one fixed point This gives us a fixed point X of F as X = ( λx.F(xx)) ( λx.F(xx)) X cnv β F(( λx.F(xx))( λx.F(xx))) => F(X) This gives us Y = λh.(( λx.h(xx))( λx.h(xx)))

CS321 Functional Programming 2 © JAS Theorem – Böhm Given the expression G = λyf.f(yf), then M is a fixed point operator iff M = GM. Hence we can confirm that Y is a fixed point operator Y = λh.((λx.h(xx))(λx.h(xx))) cnv β λh.h((λx.h(xx))(λx.h(xx))) cnv β λh.h((λf.λx.f(xx))(λx.f(xx)))h) = λh.h(Yh) = GY