10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.

Slides:



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

Substitution & Evaluation Order cos 441 David Walker.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
1 Chapter 4:Constraint Logic Programs Where we learn about the only programming concept rules, and how programs execute.
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
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
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.
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.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
CS5371 Theory of Computation Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.
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.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
The Relational Model: Relational Calculus
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
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 Programming Languages and Paradigms Functional Programming.
Lambda Calculus1 The Untyped Lambda-Calculus Hossein Hojjat University of Tehran Formal Methods Laboratory.
10/26/20151 GC16/3011 Functional Programming Lecture 21 Parallel Graph Reduction.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
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.
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.
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.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CSE 5317/4305 L12: Higher-Order Functions1 Functional Languages and Higher-Order Functions Leonidas Fegaras.
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 박성우.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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
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.
Lesson2 Lambda Calculus Basics
Programming Languages and Compilers (CS 421)
Class 36: The Meaning of Truth CS200: Computer Science
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.
Programming Languages and Compilers (CS 421)
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Presentation transcript:

10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

10/12/20152 Contents Revision: syntax and rules Revision: syntax and rules   -reduction and  -renaming Reduction stategies Reduction stategies Normal Forms Normal Forms

10/12/20153 Untyped Extended Syntax (2) program :: expression expression :: x | constant | constant | operator | operator | expression expression | expression expression | expression operator expression | expression operator expression | x. expression | x. expression | ( expression ) | ( expression )

10/12/20154 Using (“Applying”) a function To apply the previously defined function to the constant number 3: To apply the previously defined function to the constant number 3: ( (  x. ( x + 1 ) ) 3 )

10/12/20155 Rules for Evaluation (1)    reduction    reduction    reduction ( if x not free in E)  x. E  y. E [ y / x ]  x. E ) z ) E [ z / x ]  x. ( E x ) E

10/12/20156 Rules for Evaluation (2)   rules  there is a separate rule for evaluating each primitive operator (such as +)  Example: the rule for + says that evaluates to 7  Name clashes :  x. ( (  x. ( x + 3 ) ) ( x + 4 ) ) ) 5  x. ( ) ) ( ) WRONG!!!

10/12/20157  reduction and  renaming The only tricky part of the rules The only tricky part of the rules e1[e2/x] must replace only FREE occurrences of x in e1 with e2 e1[e2/x] must replace only FREE occurrences of x in e1 with e2  x. ( (  x. ( x + 3 ) ) ( x + 4 ) ) ) 5 (  x. ( x + 3 ) ) ( x + 4 )  FREE x  (  x. ( x + 3 ) ) ( )  (  x. ( x + 3 ) ) ( )

10/12/20158  reduction and  renaming Identifying free variables in e1 is not enough! Identifying free variables in e1 is not enough! Free Variable Capture: Free Variable Capture:  f. (  a. ( f a) ) )  f. ( f a) )  BOUND a  FREE a (  a. (  f. ( f a) ) a) )  CAPTURED a

10/12/20159  reduction and  renaming Rule: during  reduction, if e2 contains any free variables that are bound in e1, then for each such free variable, it must be  - converted inside e1 BEFORE doing  - reduction! Rule: during  reduction, if e2 contains any free variables that are bound in e1, then for each such free variable, it must be  - converted inside e1 BEFORE doing  - reduction!  f. (  a. ( f a) ) )  f. ( f a) )   f. (  b. ( f b) ) )  f. ( f a) )  (  b. (  f. ( f a) ) b) )

10/12/ Reduction Strategies (( x. e1) e2) is a “reducible expression” or “redex” (( x. e1) e2) is a “reducible expression” or “redex” An expression containing no redexes is in “Normal Form” or NF An expression containing no redexes is in “Normal Form” or NF Execution = successive  -reduction to NF Execution = successive  -reduction to NF Whether an arbitrary e has a NF is undecidable (Halting Problem) Whether an arbitrary e has a NF is undecidable (Halting Problem) Many different sequences of  -reduction are possible – how does this affect the result? Many different sequences of  -reduction are possible – how does this affect the result?

10/12/ Church-Rosser Theorem The Church-Rosser theorem proves that all sequences (strategies) that terminate will converge on the same NF The Church-Rosser theorem proves that all sequences (strategies) that terminate will converge on the same NF Though some may not terminate! Though some may not terminate! Corollary: the NF for a given expression is unique (if it exists) Corollary: the NF for a given expression is unique (if it exists) So  -reductions can be done in any order So  -reductions can be done in any order Even in parallel! Even in parallel!

10/12/ Normalising orders Not all strategies (orders of performing  - reductions) terminate Not all strategies (orders of performing  - reductions) terminate So which should we choose? So which should we choose? “leftmost-outermost first” is guaranteed to terminate if termination is possible “leftmost-outermost first” is guaranteed to terminate if termination is possible “normalising” “normalising” The basis of Normal Order Reduction The basis of Normal Order Reduction

10/12/ Comparing strategies Normal Order Reduction Normal Order Reduction Safe but slow Safe but slow Corresponds to call-by-name parameter-passing Corresponds to call-by-name parameter-passing Applicative Order Reduction Applicative Order Reduction “leftmost-innermost first” “leftmost-innermost first” Fast but unsafe (may not terminate, = call-by-value) Fast but unsafe (may not terminate, = call-by-value) (  x. (x + x) ) (3 + 5) (  x. (3) ) ( (  x. (x x) ) (  x. (x x) ) )

10/12/ Reduction to Normal Form Practical implementations almost never reduce to full NF (wasted computation) Practical implementations almost never reduce to full NF (wasted computation) Weak Head Normal Form Weak Head Normal Form Head Normal Form Head Normal Form Normal Form Normal Form  x. ( (  y. ( + ) ) 4 (5 + 6) )  x. ( + (5 + 6) )  x. ( + 11 )

10/12/ Reduction to Normal Form NF is unique NF is unique HNF is not unique HNF is not unique Principal HNF is unique Principal HNF is unique always reduce the leftmost (“head”) redex always reduce the leftmost (“head”) redex WHNF not unique WHNF not unique Different only when result is a function Different only when result is a function

10/12/ WHNF and Principal HNF Both principal HNF and WHNF result from Normal Order Reduction Both principal HNF and WHNF result from Normal Order Reduction Leftmost-outermost redex first (the “head” redex) Leftmost-outermost redex first (the “head” redex) WHNF: WHNF: If leftmost-outermost redex is a function, DO NOT  - reduce inside the -body If leftmost-outermost redex is a function, DO NOT  - reduce inside the -body Lazy Evaluation! Lazy Evaluation! Principal HNF: Principal HNF: If leftmost-outermost redex is a function, DO  -reduce inside the -body, but apply a normal order strategy (leftmost-outermost first) If leftmost-outermost redex is a function, DO  -reduce inside the -body, but apply a normal order strategy (leftmost-outermost first)

10/12/ Summary Revision: syntax and rules Revision: syntax and rules   -reduction and  -renaming Free variable capture Free variable capture Reduction stategies Reduction stategies Church-Rosser theorem Church-Rosser theorem Normal Order, Applicative Order Normal Order, Applicative Order Normal Forms: NF, HNF, WHNF Normal Forms: NF, HNF, WHNF

10/12/ Enough Lambda Calculus! We will return to the Lambda Calculus later in the course: We will return to the Lambda Calculus later in the course: to see how it can be used in the implementation of a high-level programming language to see how it can be used in the implementation of a high-level programming language if time permits: to investigate recursion if time permits: to investigate recursion Next we will look at how to program in a high- level (functional) language based on the lambda calculus - Miranda Next we will look at how to program in a high- level (functional) language based on the lambda calculus - Miranda