Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory.

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

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
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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.
Applied Computer Science II Chapter 3 : Turing Machines Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
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
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
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.,
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS5205: Foundation in Programming Languages Lecture 1 : Overview
Prof. Fateman CS 164 Lecture 23
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
1 SML fn x => e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors fun f x = e variables.
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.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
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 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.
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.
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.
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.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
CMSC 330: Organization of Programming Languages Operational Semantics.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction.
Overview of the theory of computation Episode 3 0 Turing machines The traditional concepts of computability, decidability and recursive enumerability.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
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.
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.
CS5205: Foundations in Programming Languages
Introduction to the λ-Calculus and Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
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.
CS 320 Principles of Programming Languages
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
Programming Languages
Announcements Quiz 6 HW7 due Tuesday, October 30
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
CSE S. Tanimoto Lambda Calculus
Programming Languages
Presentation transcript:

Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory

Hossein HojjatLambda Calculus2 “Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus.” Landin, Peter J. The next 700 programming languages. Communications of the ACM, 9(3):157–166, March 1966.

Hossein HojjatLambda Calculus3 Part 1 : Introduction

Hossein HojjatLambda Calculus4 History  David Hilbert (a German mathematician) delivered a famous speech to the Second International Congress of Mathematicians in Paris (1900) –The Problems of Mathematics  Perhaps the most influential speech ever given to mathematicians, given by a mathematician, or given about mathematics

Hossein HojjatLambda Calculus5 History  Hilbert outlined 23 major mathematical problems to be studied in the coming century  David Hilbert's tenth problem was to devise an algorithm that tests whether a polynomial has an integral root –Entscheidungsproblem (English: decision problem)  Find a process which determines the result in a finite number of operations

Hossein HojjatLambda Calculus6 History  Church and Turing independently proved that the Entscheidungsproblem is unsolvable  Church proved that there is no algorithm (defined via recursive functions) which decides for two given lambda calculus expressions whether they are equivalent or not  Turing reduced the halting problem for Turing machines to the Entscheidungsproblem

Hossein HojjatLambda Calculus7 λ-calculus strength  Although both models appear very different from one another, Turing later showed that were equivalent –They each pick out the same set of mathematical functions  Church-Turing thesis: any problem that has an algorithmic solution can be solved on a Turing machine –It implies a universality among different models of computation.

Hossein HojjatLambda Calculus8 Suitability  Lambda-calculus is more suitable as an abstract model of programming language rather than as a practical programming language –It is used to study programming languages semantics from a mathematical perspective called Denotational Semantics

Hossein HojjatLambda Calculus9 Overview  In the mid 1960s, Peter Landin observed that a complex programming language can be understood by formulating it as –A tiny core calculus capturing the language's essential mechanisms –A collection of convenient derived forms whose behavior is understood by translating them into the core

Hossein HojjatLambda Calculus10 Overview  The core language used by Landin was the lambda-calculus  Following Landin's insight, the lambda- calculus has seen widespread use in the specification of programming language features –in language design and implementation –in the study of type systems

Hossein HojjatLambda Calculus11 Overview  The lambda-calculus is just one of a large number of core calculi that have been used for similar purposes –The pi-calculus has become a popular core language for defining the semantics of message- based concurrent languages –The object calculus distills the core features of object-oriented languages

Hossein HojjatLambda Calculus12 Overview  Most of the concepts and techniques of the lambda-calculus can be transferred quite directly to other calculi

Hossein HojjatLambda Calculus13 Enriching the calculus  The lambda-calculus can be enriched in a variety of ways  It is often convenient to add special concrete syntax for features like numbers, tuples, records, etc.  The behavior of these features can already be simulated in the core language

Hossein HojjatLambda Calculus14 Enriching the calculus  We can add more complex features such as mutable reference cells or nonlocal exception handling  These features can be modeled in the core language only by using rather heavy translations  Such extensions lead eventually to languages such as ML, Haskell or Scheme

Hossein HojjatLambda Calculus15 Basics  Procedural (or functional) abstraction is a key feature of essentially all programming languages  Instead of writing the same calculation over and over –Write a procedure or function that performs the calculation generically in terms of one or more named parameters –Instantiate this function as needed, providing values for the parameters in each case

Hossein HojjatLambda Calculus16 Example  Consider (5*4*3*2*1) + (7*6*5*4*3*2*1) - (3*2*1)  Normally rewrite it as factorial(5) + factorial(7) - factorial(3), where factorial(n) = if n=0 then 1 else n * factorial(n-1)  Instantiating the function factorial (for n≥0) with the argument n yields the factorial of n as result

Hossein HojjatLambda Calculus17 The λ Notation  We write "λn...." as a shorthand for “The function that, for each n, yields...,”  factorial = λn. if n=0 then 1 else n * factorial(n-1)  The lambda-calculus (or λ-calculus) embodies this kind of function definition and application in the purest possible form  Everything is a function!

Hossein HojjatLambda Calculus18 Why lambda?  Principia Mathematica : a three-volume work on the foundations of mathematics –written by Alfred North Whitehead and Bertrand Russell in  In this book the notation for the function f with is  Church originally intended to use the notation

Hossein HojjatLambda Calculus19 Why lambda?  The typesetter could not position the hat on the top of the x, and placed it in front of it  Another typesetter, changed it to lambda!

Hossein HojjatLambda Calculus20 Syntax  A term is defined as follows: e::=x(identifier) |e 0 e 1 (application) |λx. e(abstraction)  In an abstraction: –x is the argument –e is the body of the function

Hossein HojjatLambda Calculus21 λ Expression  The only keywords used in the language are λ and dot  An expression can be surrounded with parenthesis for clarity

Hossein HojjatLambda Calculus22 Example  The identity function : λx. x  The name after the λ is the identifier of the argument of this function  The expression after the point is the “body” of the definition

Hossein HojjatLambda Calculus23 Application  Functions can be applied to expressions (λx.x) y  This is the identity function applied to y  Parenthesis are used for clarity in order to avoid ambiguity –Function application is left associative  The result is : y

Hossein HojjatLambda Calculus24 Scope  In λ calculus all names are local to definitions  λx.t : The scope of x is the term t  An occurrence of the variable x is said to be bound when it occurs in the body t of an abstraction λx.t (λx. y x)(λy. x y) bound free bound free

Hossein HojjatLambda Calculus25 Free variables  We can define formally the free variables of an expression E recursively as follows: Free(x) = {x} Free(E 1 E 2 ) = Free(E 1 )  Free(E 2 ) Free(λx. E) = Free(E) – {x}  Example: Free(λx. x (λy.x y z)) = {z}

Hossein HojjatLambda Calculus26 Computation  In its pure form, the lambda-calculus has no built-in constants or primitive operators –no numbers, arithmetic operations, conditionals, records, loops, sequencing, I/O, etc.  Computation is achieved by application of functions to arguments (which themselves are functions)  Each step in the computation consists of abstraction the right-hand component for the bound variable in the abstraction's body

Hossein HojjatLambda Calculus27 Computation  Graphically, we write: – means: “the term obtained by replacing all free occurrences of x in M by N”

Hossein HojjatLambda Calculus28 Example  What does λs.(s s) mean? –Self application function  (λs.(s s) λx.x) = (λx.x λx.x) = λx.x  What does this lambda expression mean? (λs. (s s) λs. (s s))

Hossein HojjatLambda Calculus29 Example  λfunc. λarg. (func arg)  This is the function application function  It returns a second function which then applies the first function's argument to the second function’s argument

Hossein HojjatLambda Calculus30 Example  λfirst. λsecond. first –Selecting the first of two arguments –(λx. λy. x) a b = (λy. a) b = a  λfirst. λsecond. second –Selecting the second of two arguments –(λx. λy. y) a b = (λy. y) b = b

Hossein HojjatLambda Calculus31 Naming Conventions  A term of the form (λx. M) N is called a redex ("reducible expression")  The operation of rewriting a redex according to the above rule is called beta-reduction

Hossein HojjatLambda Calculus32 Evaluation Strategies  Several different evaluation strategies for the lambda-calculus have been studied over the years by programming language designers and theorists  Each strategy defines which redex or redexes in a term can fire on the next step of evaluation

Hossein HojjatLambda Calculus33 Evaluation Strategies  For example, consider: (λx.x) ((λx.x) (λz. (λx.x) z))  We can write it more readable as: id (id (λz. id z))  This term contains three redexes: id (id (λz. id z))

Hossein HojjatLambda Calculus34 Full beta-reduction  Any redex may be reduced at any time  At each step we pick some redex, anywhere inside the term we are evaluating, and reduce it  For example, we can begin with the innermost redex, then do the one in the middle, then the outermost id (id (λz. id z)) → id (id (λz.z)) → id (λz.z) → λz.z ↛

Hossein HojjatLambda Calculus35 Normal Order  The leftmost, outermost redex is always reduced first  id (id (λz. id z)) → id (z. id z) → λz. id z → λz.z ↛

Hossein HojjatLambda Calculus36 Why Not Normal Order?  In most (all?) programming languages, functions are considered values (fully evaluated)  Thus, no reduction is done inside of functions (under the lambda)  No popular programming language uses normal order

Hossein HojjatLambda Calculus37 Call by Name-Call by Value  Consider the application e 0 e 1  Call by value: evaluate the argument e 1 to a value before β reduction  Call by name: reduce the application, without evaluating e 1  In both cases: A value is an abstraction: λx. e

Hossein HojjatLambda Calculus38 Call by Name-Call by Value  Call by name: id (id (λz. id z)) → id (λz. id z) → λz. id z ↛  Call by value: id (id (λz. id z)) → id (λz. id z) → λz. id z ↛

Hossein HojjatLambda Calculus39 Comparison  The call-by-value strategy is strict  The arguments to functions are always evaluated, whether or not they are used by the body of the function  Non-strict (or lazy) strategies evaluate only the arguments that are actually used –call-by-name –call-by-need

Hossein HojjatLambda Calculus40 Call by Value vs. Name  The debate about whether languages should be by value or by name is nearly 20 years old  This debate is confined to the functional programming community  Outside the functional community call by name is rarely considered

Hossein HojjatLambda Calculus41 Languages Hierarchy  Call-by-name lambda calculus –Haskell –Miranda  Call-by-value lambda calculus –Lisp –Scheme –ML

Hossein HojjatLambda Calculus42 Name Clashes  Consider the function application function –def apply = λfunc. λarg. (func arg)  Consider: ((apply arg) salam) = (( (λfunc. λarg. (func arg)) arg) salam)  arg is used both as a –function bound variable –free variable

Hossein HojjatLambda Calculus43 Name Clashes  If we carry out β reduction: (( (λfunc. λarg. (func arg)) arg) salam) → (λarg. (arg arg) salam) → (salam salam)  This was not intended at all!  The argument arg has been substituted in the scope of the bound variable arg,and appears to create a new occurrence of that variable

Hossein HojjatLambda Calculus44 Renaming  We can avoid this problem by using consistent renaming  For example, we can replace the bound variable arg with arg1 (( (λfunc. λarg1. (func arg1)) arg) salam) → (λarg1. (arg arg1) salam) → (arg salam)

Hossein HojjatLambda Calculus45 α-Conversion  A name clash arises when –A β-reduction places an expression with a free variable in the scope of a bound variable with the same name as the free variable  The names of bound variables are unimportant (as far as the meaning of the computation goes)  We will treat λx. x and λy. y as if they are (absolutely and totally) indistinguishable –we can always use one in place of the other

Hossein HojjatLambda Calculus46 Equivalence Relation  An equivalence relation is defined that captures the intuition that two expressions denote the same function  This equivalence relation is defined by the so- called alpha-conversion rule and the beta- reduction rule  There is a third rule, η-conversion, which may be added to these two to form a new equivalence relation

Hossein HojjatLambda Calculus47 η-Conversion  η-Conversion is useful to eliminate redundant lambda abstractions  If the sole purpose of a lambda abstraction is to pass its argument to another function, then the lambda abstraction is redundant  λx. (E x)  E, if x  Free(E)

Hossein HojjatLambda Calculus48 η-Conversion Usage  η-Conversion is similar to the proxy pattern in OO programming  In “eager evaluation” languages is used (like Scheme)  It creates a wrapper around a lambda expression to prevent immediate evaluation

Hossein HojjatLambda Calculus49 Examples  Valid conversion λx. add x → η add λy. add x y → η add x  Invalid conversion λx. add x x → η add x  Valid conversion λx. (λy. y) x → η λy. y  What about λx. λy. y x

Hossein HojjatLambda Calculus50 Part 2 : Programming in the Lambda-Calculus

Hossein HojjatLambda Calculus51  The lambda-calculus is much more powerful than its tiny definition might suggest  In this part, we develop a number of standard examples of programming in the lambda- calculus  How to make –Booleans and conditional functions –Numerals and arithmetic functions

Hossein HojjatLambda Calculus52 Church Booleans  We represent TRUE by select_first function def true = λt. λf. t  We represent FALSE by select_second function def false = λt. λf. f  As a motivation for this representation, consider the C conditional expression: ? :

Hossein HojjatLambda Calculus53 Conditional Expression  The definition of TRUE and FALSE suggest the definition for a conditional construct def cond = λe1. λe2. λc. ((c e1) e2)  We will use the conditional definition with true and false to model some logical operators

Hossein HojjatLambda Calculus54 Not  The Not operator can be written using a conditional expression: x ? FALSE : TRUE  def not = λx. ( ( (cond false) true) x)  This expression can be written as follows: λx. ((x false) true)  Exercise: Use the above result to calculate NOT FALSE

Hossein HojjatLambda Calculus55 And  x  y  ( x ? y) : FALSE  def and = λx. λy. ( ( (cond y) false) x)  This expression can be written as follows: λx. λy. ( (x y) false)  Exercise: Use the above result to calculate AND TRUE FALSE  Exercise: Find an expression for OR

Hossein HojjatLambda Calculus56 Church Numerals  There are several possible ways to define the natural numbers in lambda calculus  The most common are the Church numerals  First we define f n x which means n application of f to x f 5 x = f ( f ( f ( f ( f x) ) ) )

Hossein HojjatLambda Calculus57 Church Numerals  The intuition behind Church numerals is to represent a number n as a function that transforms any function f into the function f n 0 := λ f. λ x. x 1 := λ f. λ x. f x 2 := λ f. λ x. f( f x) … n := λ f. λ x. f n x

Hossein HojjatLambda Calculus58 Successor Function  Given this definition of the Church numerals, we can define a successor function  This implementation uses the argument number n to construct the function f n, then applies f one more time to get f n+1  def succ = λ n. λ f. λ x. f (n f x)  Exercise: Try succ 1 == 2

Hossein HojjatLambda Calculus59 Addition  Adding m to n is simply taking the successor of n, m times  add m n = m succ n = λm. λn. λf. λx. m f ( n f x)  Let’s check it: add m n → λf. λx. m f ( n f x) → λf. λx. f m ( f n x) → λf. λx. f m+n x  m + n

Hossein HojjatLambda Calculus60 Multiplication  Since add takes its arguments one at a time, applying it to just one argument n yields the function that adds n to whatever argument it is given  Passing this function as the first argument to m and 0 as the second argument means: –apply the function that adds n to its argument, iterated m times, to zero, i.e., add together m copies of n.  def mult = λm. λn. m (add n) 0

Hossein HojjatLambda Calculus61 Exercise  Define a term for raising one number to the power of another.