Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http://www.csg.csail.mit.edu/6.827 -calculus: A Basis for.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

1 Turing Machines and Equivalent Models Section 13.2 The Church-Turing 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.
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.
INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS Instructors: Crista Lopes Copyright © Instructors.
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.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming Element of Functional Programming.
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.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
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.
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.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
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.
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.
-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 Lambda Calculus and Types.
CSE 5317/4305 L12: Higher-Order Functions1 Functional Languages and Higher-Order Functions Leonidas Fegaras.
CMSC 330: Organization of Programming Languages Operational Semantics.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
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. L05-1 September 21, 2006http:// Types and Simple Type.
A LISP interepreter for the Lambda Calculus Functional Programming
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lecture 6: Lambda Calculus
Lambda Calculus CSE 340 – Principles of Programming Languages
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.
Lambda Calculus Revisited
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Lesson2 Lambda Calculus Basics
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
A-calculus with Constants and Let-blocks
Announcements Quiz 6 HW7 due Tuesday, October 30
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
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:

Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for Functional Languages September 14, 2006

L03-2 September 14, 2006http:// Functions f may be viewed as a set of ordered pairs where d  D and r  R a method of computing value r corresponding to argument d some important notations – -calculus (Church) – Turing machines (Turing) – Partial recursive functions Domain Range f : D   R

L03-3 September 14, 2006http:// The -calculus: a simple type-free language to express all computable functions to directly express higher-order functions to study evaluation orders, termination, uniqueness of answers... to study various typing systems to serve as a kernel language for functional languages –However, -calculus extended with constants and let- blocks is more suitable

L03-4 September 14, 2006http:// -notation a way of writing and applying functions without having to give them names a syntax for making a function expression from any other expression the syntax distinguishes between the integer "2” and the function "always_two" which when applied to any integer returns 2 always_two x = 2;

L03-5 September 14, 2006http:// Pure-calculus: Syntax variable abstraction application E = x | x.E | E E 1. application E 1 E 2 function argument - application is left associative E 1 E 2 E 3 E 4  ((( E 1 E 2 ) E 3 ) E 4 ) 2. abstraction x.E bound variable body or formal parameter - the scope of the dot in an abstraction extends as far to the right as possible x.x y x.(x y) (x.(x y))  (x.x y)  (x.x) y

L03-6 September 14, 2006http:// Free and Bound Variables -calculus follows lexical scoping rules Free variables of an expression FV(x) = {x} FV(E 1 E 2 )= ? FV(x.E)= ? FV(E 1 ) U FV(E 2 ) FV(E) – {x} A variable occurrence which is not free in an expression is said to be a bound variable of the expression combinator: a -expression without free variables, aka closed -expression

L03-7 September 14, 2006http:// ß-substitution (x.E) E a  E[E a /x] replace all free occurrences of x in E with E a E[A/x] is defined as follows by case on E: variable y[E a /x]= E a if x y y[E a /x]= otherwise ? application (E 1 E 2 )[E a /x] = ? abstraction (y.E 1 )[E a /x] = y.E 1 if x  y (y.E 1 )[E a /x] = y (E 1 [E a /x] E 2 [E a /x]) (y.E 1 [E a /x]) otherwise What if E a contains y ? ? z.((E 1 [z/y])[E a /x]) otherwise where z   FV(E 1 ) U FV(E a ) U FV(x)

L03-8 September 14, 2006http:// ß-substitution: an example (p.p (p q)) [(a p b) / q]  (z.z (z q)) [(a p b) / q]  (z.z (z (a p b)))

L03-9 September 14, 2006http:// -Calculus as a Reduction System Syntax E = x | x.E | E E Reduction Rule   -rule:x.E y.E [y/x] if y  FV(E)   -rule:(x.E) E a  E [E a /x] -rule:(x.E x)  E if x  FV(E) Redex (x.E) E a Normal Form An expression without redexes

L03-10 September 14, 2006http://  and  Rules -rule says that the bound variables can be renamed systematically: (x.x (x.a x)) b  (y.y (x.a x)) b -rule can turn any expression, including a constant, into a function: x.a x  a -rule does not work in the presence of types

L03-11 September 14, 2006http:// A Sample Reduction  f.f ( x. y.x)) (C a b) (( C a b) ( x. y.x) (( x. y.x) a b  ( y.a) b  a   H (C a b)a T (C a b)b C x.y.f.f x y Hf.f (x.y. x) Tf.f (x.y. y) What is H (C a b) ?

L03-12 September 14, 2006http:// Integers: Church's Representation 0 x.y. y 1 x.y. x y 2 x.y. x (x y)... n x.y. x (x...(x y)...) succ ? If n is an integer, then (n a b) gives n nested a’s followed by b the successor of n should be a (n a b) succn.a.b.a (n a b) plus? mul ? m.n.m succ n m. n.m (plus n) 0

L03-13 September 14, 2006http:// Booleans and Conditionals Truex.y.x Falsex.y.y zero?n. n (y.False) True zero? 0 ? zero? 1 ?    x. y.y) ( y.False) True  y. y) True TT rue  x. y.x y) ( y.False) True  ( y.False) True FF alse   condb.x.y. b x y cond True E 1 E 2  ? cond False E 1 E 2  ? E1E2E1E2

L03-14 September 14, 2006http:// Recursion ? Assuming suitable combinators, fact can be rewritten as: fact = n. cond (zero? n) 1 (mul n (fact (sub n 1))) How do we get rid of the fact on the RHS? fact n = if (n == 0) then 1 else n * fact (n-1) Suppose H = f.n.cond (zero? n) 1 (mul n (f (sub n 1))) then fact = H fact --- fact is a solution of this equation??? more on recursion in the next lecture

L03-15 September 14, 2006http:// Choosing Redexes 1. ((x.M) A) ((x.N) B)     ((x.M) ((y.N)B))     Does   followed by    produce the same expression as   followed by   ? Notice in the second example   can destroy or duplicate   

L03-16 September 14, 2006http:// Church-Rosser Property A reduction system is said to have the Church-Rosser property, if E E 1 and E E 2 then there exits a E 3 such that E 1 E 3 and E 2 E 3. E E 1 E 2 E 3 also known as CR or Confluence Theorem: The -calculus is CR. (Martin-Lof & Tate)    

L03-17 September 14, 2006http:// Interpreters An interpreter for the -calculus is a program to reduce -expressions to “answers”. It requires: the definition of an answer a reduction strategy - a method to choose redexes in an expression a criterion for terminating the reduction process

L03-18 September 14, 2006http:// Definitions of “Answers” Normal form (NF): an expression without redexes Head normal form (HNF): x is HNF (x.E) is in HNF if E is in HNF (x E 1... E n ) is in HNF Semantically most interesting- represents the information content of an expression Weak head normal form (WHNF): An expression in which the left most application is not a redex. x is in WHNF (x.E) is in WHNF (x E 1... E n ) is in WHNF Practically most interesting   “Printable Answers”

L03-19 September 14, 2006http:// Reduction Strategies Two common strategies applicative order: left-most innermost redex aka call by value evaluation normal order: left-most (outermost) redex aka call by name evaluation ( x.y) (( x.x x) ( x.x x)) 11 22 applicative order normal order

L03-20 September 14, 2006http:// Facts 1.Every -expression does not have an answer i.e., a NF or HNF or WHNF (x.x x) (x.x x) =   2. CR implies that if NF exists it is unique 3. Even if an expression has an answer, not all reduction strategies may produce it (x.y.y)  leftmost redex:(x.y.y) y.y innermost redex:(x.y.y)   ( x. y. y) ...

L03-21 September 14, 2006http:// Normalizing Strategy A reduction strategy is said to be normalizing if it terminates and produces an answer of an expression whenever the expression has an answer. aka the standard reduction Theorem: Normal order (left-most) reduction strategy is normalizing for the  -calculus.

L03-22 September 14, 2006http:// A Call-by-name Interpreter Answers: WHNF Strategy: leftmost redex cn(E): Definition by cases on E E = x | x.E | E E cn(x)= x cn(x.E)= x.E cn(E 1 E 2 )= let f = cn(E 1 ) in case f of x.E 3 = cn(E 3 [E 2 /x]) _ = (f E 2 ) Apply the function before evaluating the arguments

L03-23 September 14, 2006http:// Better syntax... [[....]] represents syntax E = x | x.E | E E cn([[x]])= x cn([[x.E]])= x.E cn([[E 1 E 2 ]])= let f = cn([[E 1 ]]) in case f of [[x.E 3 ]] = cn(E3[E2/x]) _ = (f E 2 ) still messy Meta syntax

L03-24 September 14, 2006http:// A Call-by-value Interpreter Answers: WHNF Strategy: leftmost-innermost redex but not inside a -abstraction cv(E): Definition by cases on E E = x | x.E | E E cv(x)= x cv(x.E)= x.E cv( E 1 E 2 )= let f = cv(E 1 ) a = cv(E 2 ) in case f of x.E 3 = cv(E 3 [a/x]) _ = (f a) Evaluate the argument before applying the function

L03-25 September 14, 2006http:// Normalizing? ( x.y) (( x.x x) ( x.x x)) call by value reduction call by name reduction ( x.y) (( x.x x) ( x.x x)) y Which interpreters (if any) are normalizing for computing WHNF ? call-by-value call-by-name Clearly not May be The proof to show that the call-by-name interpreter is normalizing is non-trivial

L03-26 September 14, 2006http:// Big Step Semantics Consider the following rule E 1 E 2  E 1  x.E b E b [E 2 / x] Can we compute using this rule? What does it compute? Will it compute every thing that the –calculus can?