Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc.,

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
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 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.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
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
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
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.
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming Element of Functional Programming.
Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.
Introduction to the λ-Calculus and Functional Programming Languages Arne Kutzner Hanyang University 2015.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
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.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory.
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.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Semantic Construction lecture 2. Semantic Construction Is there a systematic way of constructing semantic representation from a sentence of English? This.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
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.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
Chapter 12 Theory of Computation Introduction to CS 1 st Semester, 2014 Sanghyun Park.
CS321 Functional Programming 2 © JAS The λ Calculus This is a formal system to capture the ideas of function abstraction and application introduced.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Comp 205: Comparative Programming Languages The Untyped λ-calculus representing values Lecture notes, exercises, etc., can be found at:
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Comp 205: Comparative Programming Languages
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.
CS 320 Principles of Programming Languages
The Metacircular Evaluator
Functional Programming
Announcements Quiz 6 HW7 due Tuesday, October 30
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
L Calculus.
A simple function.
CSE S. Tanimoto Lambda Calculus
OBJ first-order functional language based on equational logic
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Presentation transcript:

Comp 205: Comparative Programming Languages Semantics of Functional Languages Term- and Graph-Rewriting The λ-calculus Lecture notes, exercises, etc., can be found at:

Term Rewriting A straightforward way of implementing a functional programming language is to implement term-rewriting. The Haskell interpreter evaluates expressions (terms) by "substituting equals for equals".

For example, given the following definitions: fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib (n-2), if n>1 An evaluation might proceed as follows: fib 3  (fib 2) + (fib 1)  (fib 1) + (fib 0) + (fib 1)  1 + (fib 0) + (fib 1)  (fib 1)   3

Terms and Trees A standard way of representing terms is as trees: + Fn.Appl. + fib1 Fn.Appl. fib 10

Term Rewriting Term rewriting replaces (sub)trees: Fn.Appl. fib1 1  (rewrites to)

Rewriting Trees... giving the new tree: Fn.Appl. fib 10

Side-Effects In the imperative paradigm, all evaluation can change the current state (e.g., by side-effects). int funnyCount=0; int funny(int i) { return funnyCount++ * i; } funny(2) + funny(2) // might be 0+2

Functional Expressions In the functional paradigm there is no state, so an expression always denotes the same value, and evaluation simply converts an expression to its value. This important property of functional languages is referred to as "referential transparency".

Referential Transparency Referential Transparency: any expression denotes a single value, irrespective of its context. Consequently, (sub)expressions can be replaced by their values without changing the behaviour of a program. (Referential transparency = no side-effects)

Graphs Expressions can also be represented by graphs: + + Fn.Appl. fib 10

Graph Rewriting This allows identical subexpressions to be rewritten "in one go": + + Fn.Appl. fib Fn.Appl. fib 1 0  (rewrites to)

The λ-Calculus The λ-calculus was developed by the mathematician Alonzo Church as a tool to study functions and computability. The λ-calculus provides a very simple model of computable function, and inspired the designers of the first functional programming language, LISP. The λ-calculus also provides an operational semantics for functional languages.

Computability Alan Turing developed an abstract machine (the Turing Machine) and showed that it provided a universal model of computation. He showed that the Turing-computable functions were exactly the general recursive functions. Church showed that the Turing-computable functions were exactly those that could be represented in the λ-calculus (the λ-computable functions).

Church-Turing Hypothesis Both Turing and Church conjectured that their model of computability formalised the intuitive notion of mathematical computability. The Church-Turing Hypothesis states that the equivalent notions of Turing- and λ-computability capture precisely "every function that would naturally be regarded as computable".

λ-Terms Church designed the λ-calculus as a tool to study the fundamental notion of computable function. He therefore sought to strip away all but the bare essentials. The syntax for λ-terms is accordingly very simple. λ-terms are built from: variables function application λ-abstraction (declaring formal parameters) (and brackets)

Syntax of λ-Terms The set Λ of λ-terms is defined by: Λ ::= Var | Λ Λ | λVar. Λ | (Λ) where Var is a set of variables. Looking at each clause in turn: variables Typically, we use a, b, c,..., x, y, z for variables; if we run out of variable names, we can use x', x'', x''', etc.

Syntax of λ-Terms function application If M and N are λ-terms, so too is M N, which represents the application of M to N. (All λ-terms can be considered functions.) λ-abstraction If M is a λ-term, so too is λx.M, which can be thought of as a function with formal parameter x, and with body M.

Some Examples x x y λy.(x y) λx.λy.x y brackets λ-abstraction binds less tightly than function application, so the last example above should be read as λx.(λy.(x y)) not (λx.(λy.x)) y. Also, x y z should be read as (x y) z.

Evaluation of λ-Terms A λ-term λx.M represents a function whose formal parameter is x and whose body is M. When this function is applied to another λ-term N, as in the λ-term (λx.M) N, evaluation proceeds by replacing x with N in the body M. For example, (λx.λy.x) (λz.z)  λy.λz.z In order to make this precise, we need the notions of free and bound variables.

Free and Bound Variables Given a λ-term λx.M, we say that λ binds ocurrences of x in M. We also say that x is bound in (λx.M). A free variable is one that is not bound by a λ-abstraction. A λ-term is closed if it has no free variables.

Free Variables Given a λ-term M, we write FV(M) for the set of free variables in M. This set is defined as follows: FV( x ) = { x } In a λ-term consisting of just a variable, that variable is free. FV( M N ) = FV(M)  FV(N) Function application doesn't bind variables. FV( λx.M ) = FV(M) - { x } If x is a bound variable, then x is not free.

Evaluation of λ-Terms Given a function application of the form (λx.M) N, evaluation proceeds by replacing all free occurrences of x in the body M with N. For example, (λx.λy.x) λz.z  λy.λz.z Here, the argument λz.z replaces the variable x, which is free in the body λy.x of the function being applied.

Evaluation of λ-Terms However, (λx.(λx.x)) λz.z  λx.x Here, there is no substitution, since x is not free in the body λx.x.

β-Conversion In general, we write (λx.M) N  β M[x  N], where M[x  N] denotes the result of replacing all free occurrences of x in M with N. This form of evaluation is referred to as β-conversion, for which we use the symbol  β.

Example ((λx. λy. y x) (λz.z)) (λu. λv. u)  β ( (λy. y x)[x  (λz.z)] ) (λu. λv. u) = (λy. y (λz.z)) (λu. λv. u)  β ( (y (λz.z))[x  (λu. λv. u)] ) = (λu. λv. u) (λz.z)  β λv. λz. z

α-Conversion Just as with functions, formal parameters simply serve as place-holders, representing possible arguments. In the λ-calculus, formal parameters (i.e., bound variables) can be renamed. This is referred to as α-Conversion (written  α ). For example, λx. λy. y  α λx. λv. v  α λu. λv. v

β-Conversion Again In general, we write (λx.M) N  β M[x  N], where M[x  N] denotes the result of replacing all free occurrences of x in M with N, provided that no free variables in N become bound as a result of this substitution. For example, (λx. λy. y x) (λz.y)  β λy. y (λz.y) is not allowed.

α- and β-Conversion Sometimes it is necessary to apply α-conversion before β-conversion can be applied. For example, (λx. λy. y x) (λz.y)  α (λx. λu. u x) (λz.y)  β (λu. u x)[x  (λz.y)] = λu. u (λz.y)

η-Conversion A final reduction relation on λ-terms is η-conversion. This applies to λ-terms of the form λx. M x, where x is not free in M. Such a function takes an argument and applies M to that argument; it is therefore equal to M itself. This is the idea behind η-conversion (  η ): λx. M x  η M provided that x is not free in M.

Example λy. (λx. y x)  η λy. y But η-conversion is not applicable to λx. (λy. y x) λy. (λx. (x y) x)

Computing with λ-Terms How does the λ-calculus relate to real programming languages such as Haskell?

Summary Key points: λ-terms β-conversion α-conversion η-conversion Next: Computing with λ-terms