7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.

Slides:



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

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
10. Applications of Logic Programming. © O. Nierstrasz PS — Applications of Logic Programming 10.2 Roadmap 1. Search problems —SEND + MORE = MONEY 2.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
ESE Einführung in Software Engineering 7. Modeling Behaviour Prof. O. Nierstrasz.
Introduction to Software Engineering 7. Modeling Behaviour.
10. Petri Nets Prof. O. Nierstrasz. Roadmap  Definition: —places, transitions, inputs, outputs —firing enabled transitions  Modelling: —concurrency.
Foundations of Programming Languages: Introduction to Lambda Calculus
5. Types and Polymorphism. © O. Nierstrasz PS — Type Systems 5.2 Roadmap  Static and Dynamic Types  Type Completeness  Types in Haskell  Monomorphic.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap  Syntax and Semantics  Semantics of Expressions 
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
4. Functional Programming. © O. Nierstrasz PS — Functional Programming 4.2 Roadmap Overview  Functional vs. Imperative Programming  Pattern Matching.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
CP — Concurrent Programming 12. Petri Nets Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
4. Functional Programming. © O. Nierstrasz PS — Functional Programming 4.2 Roadmap  Functional vs. Imperative Programming  Pattern Matching  Referential.
Doaitse Swierstra Atze Dijkstra Doaitse Swierstra Atze Dijkstra Type Systems.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
ESE Einführung in Software Engineering 6. Modeling Objects and Classes Prof. O. Nierstrasz.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
SWOT Analysis Strengths Weaknesses SWOT Opportunities Threats.
Collecting Images & Clippings Chapter 2.3 in Sketching User Experiences: The Workbook.
Win8 on Intel Programming Course The challenge Paul Guermonprez Intel Software
Multicore Programming
© Copyright Showeet.com I NSERT YOUR TITLE HERE. © Copyright Showeet.com Insert Your Title Here 2 Master text styles –Second level Third level –Fourth.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
The Animated Sequence Chapter 5.1 in Sketching User Experiences: The Workbook.
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.
© 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.
Sketching Vocabulary Chapter 3.4 in Sketching User Experiences: The Workbook Drawing objects, people, and their activities.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Win8 on Intel Programming Course Paul Guermonprez Intel Software
Design of Everyday Things Part 2: Useful Designs? Lecture /slide deck produced by Saul Greenberg, University of Calgary, Canada Images from:
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Just Enough Type Theory or, Featherweight Java A Simple Formal Model of Objects Jonathan Aldrich
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Images of pesticides By: Leslie London, University of Cape Town This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Sketching Vocabulary Chapter 3.4 in Sketching User Experiences: The Workbook Drawing objects, people, and their activities.
CS 550 Programming Languages Jeremy Johnson
A lightening tour in 45 minutes
Byzantine-Resilient Colorless Computaton
Pre and post workshop assessments
FOTW Worksheet Slides Christopher Penn, Financial Aid Podcast Student Loan Network.
Simulations and Reductions
CSE S. Tanimoto Lambda Calculus
Presentation transcript:

7. Fixed Points

© O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus  The polymorphic lambda calculus  Other calculi

© O. Nierstrasz PS — Fixed Points 7.3 References  Paul Hudak, “Conception, Evolution, and Application of Functional Programming Languages,” ACM Computing Surveys 21/3, Sept. 1989, pp

© O. Nierstrasz PS — Fixed Points 7.4 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus  The polymorphic lambda calculus  Other calculi

© O. Nierstrasz PS — Fixed Points 7.5 Recall these encodings … True  x y. x False  x y. y pair  ( x y z. z x y) (x, y)  pair x y first  ( p. p True ) second  ( p. p False )

© O. Nierstrasz PS — Fixed Points 7.6 Representing Numbers There is a “standard encoding” of natural numbers into the lambda calculus: Define: 0  ( x. x ) succ  ( n. (False, n) ) then: 1  succ 0  (False, 0) 2  succ 1  (False, 1) 3  succ 2  (False, 2) 4  succ 3  (False, 3)

© O. Nierstrasz PS — Fixed Points 7.7 Working with numbers  What happens when we apply pred 0? What does this mean? We can define simple functions to work with our numbers. Consider: iszero  first pred  second then: iszero 1=first (False, 0)  False iszero 0= ( p. p True ) ( x. x )  True pred 1=second (False, 0)  0

© O. Nierstrasz PS — Fixed Points 7.8 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus  The polymorphic lambda calculus  Other calculi

© O. Nierstrasz PS — Fixed Points 7.9 Recursion Suppose we want to define arithmetic operations on our lambda- encoded numbers. In Haskell we can program: so we might try to “define”: plus  n m. iszero n m ( plus ( pred n ) ( succ m ) ) Unfortunately this is not a definition, since we are trying to use plus before it is defined. I.e, plus is free in the “definition”! plus n m | n == 0= m | otherwise= plus (n-1) (m+1)

© O. Nierstrasz PS — Fixed Points 7.10 Recursive functions as fixed points We can obtain a closed expression by abstracting over plus: rplus  plus n m. iszero n m ( plus ( pred n ) ( succ m ) ) rplus takes as its argument the actual plus function to use and returns as its result a definition of that function in terms of itself. In other words, if fplus is the function we want, then: rplus fplus  fplus I.e., we are searching for a fixed point of rplus...

© O. Nierstrasz PS — Fixed Points 7.11 Fixed Points A fixed point of a function f is a value p such that f p = p. Examples: fact 1= 1 fact 2= 2 fib 0= 0 fib 1= 1 Fixed points are not always “well-behaved”: succ n = n + 1  What is a fixed point of succ?

© O. Nierstrasz PS — Fixed Points 7.12 Fixed Point Theorem Theorem: Every lambda expression e has a fixed point p such that (e p)  p.  e: Y e  e (Y e) Proof: Let:Y  f. ( x. f (x x)) ( x. f (x x)) Now consider: p  Y e  ( x. e (x x)) ( x. e (x x))  e (( x. e (x x)) ( x. e (x x))) =e p So, the “magical Y combinator” can always be used to find a fixed point of an arbitrary lambda expression.

© O. Nierstrasz PS — Fixed Points 7.13 How does Y work? Recall the non-terminating expression  = ( x. x x) ( x. x x)  loops endlessly without doing any productive work. Note that (x x) represents the body of the “loop”. We simply define Y to take an extra parameter f, and put it into the loop, passing it the body as an argument: Y  f. ( x. f (x x)) ( x. f (x x)) So Y just inserts some productive work into the body of 

© O. Nierstrasz PS — Fixed Points 7.14 Using the Y Combinator  What are succ and pred of (False, (Y succ))? What does this represent? Consider: f  x. True then: Y f  f (Y f)by FP theorem = ( x. True) (Y f)  True Consider: Y succ  succ (Y succ)by FP theorem  (False, (Y succ))

© O. Nierstrasz PS — Fixed Points 7.15 Recursive Functions are Fixed Points We seek a fixed point of: rplus  plus n m. iszero n m ( plus ( pred n ) ( succ m ) ) By the Fixed Point Theorem, we simply take: plus  Y rplus Since this guarantees that: rplus plus  plus as desired!

© O. Nierstrasz PS — Fixed Points 7.16 Unfolding Recursive Lambda Expressions plus 1 1=(Y rplus) 1 1  rplus plus 1 1 (NB: fp theorem)  iszero 1 1 (plus (pred 1) (succ 1) )  False 1 (plus (pred 1) (succ 1) )  plus (pred 1) (succ 1)  rplus plus (pred 1) (succ 1)  iszero (pred 1) (succ 1) (plus (pred (pred 1) ) (succ (succ 1) ) )  iszero 0 (succ 1) (...)  True (succ 1) (...)  succ 1  2

© O. Nierstrasz PS — Fixed Points 7.17 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus  The polymorphic lambda calculus  Other calculi

© O. Nierstrasz PS — Fixed Points 7.18 The Typed Lambda Calculus There are many variants of the lambda calculus. The typed lambda calculus just decorates terms with type annotations: Syntax: e ::= x  | e 1  2   1 e 2  2 | ( x  2.e  1 )  2   1 Operational Semantics: Example: True  ( x A. ( y B. x A ) B  A ) A  (B  A) x  2. e  1  y  2. [y  2 /x  2 ] e  1 y  2 not free in e  1 ( x  2. e 1  1 ) e 2  2  [e 2  2 /x  2 ] e 1  1 x  2. (e  1 x  2 )  e1e1 x  2 not free in e  1

© O. Nierstrasz PS — Fixed Points 7.19 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus  The polymorphic lambda calculus  Other calculi

© O. Nierstrasz PS — Fixed Points 7.20 The Polymorphic Lambda Calculus Polymorphic functions like “map” cannot be typed in the typed lambda calculus! Need type variables to capture polymorphism:  reduction (ii): ( x. e 1  1 ) e 2  2  [  2/ ] [e 2  2 /x ] e 1  1 Example: True  ( x . ( y . x  )  )  (  ) True  (  ) a A b B  ( y . a A )  A b B  a A

© O. Nierstrasz PS — Fixed Points 7.21 Hindley-Milner Polymorphism Hindley-Milner polymorphism (i.e., that adopted by ML and Haskell) works by inferring the type annotations for a slightly restricted subcalculus: polymorphic functions. If: then is ok, but if then is a type error since the argument len cannot be assigned a unique type! doubleLen len len' xs ys = (len xs) + (len' ys) doubleLen length length “aaa” [1,2,3] doubleLen' len xs ys = (len xs) + (len ys) doubleLen' length “aaa” [1,2,3]

© O. Nierstrasz PS — Fixed Points 7.22 Polymorphism and self application Even the polymorphic lambda calculus is not powerful enough to express certain lambda terms. Recall that both  and the Y combinator make use of “self application”:  = ( x. x x ) ( x. x x )  What type annotation would you assign to ( x. x x)?

© O. Nierstrasz PS — Fixed Points 7.23 Built-in recursion with letrec AKA def AKA µ  Most programming languages provide direct support for recursively-defined functions (avoiding the need for Y) (def f.E) e  E [(def f.E) / f] e (def plus. n m. iszero n m ( plus ( pred n ) ( succ m ))) 2 3  ( n m. iszero n m ((def plus. …) ( pred n ) ( succ m ))) 2 3  (iszero 2 3 ((def plus. …) ( pred 2 ) ( succ 3 )))  ((def plus. …) ( pred 2 ) ( succ 3 ))  …

© O. Nierstrasz PS — Fixed Points 7.24 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus  The polymorphic lambda calculus  Other calculi

© O. Nierstrasz PS — Fixed Points 7.25 Featherweight Java Igarashi, Pierce and Wadler, “Featherweight Java: a minimal core calculus for Java and GJ”, OOPSLA ’99 doi.acm.org/ / Used to prove that generics could be added to Java without breaking the type system.

© O. Nierstrasz PS — Fixed Points 7.26 Other Calculi Many calculi have been developed to study the semantics of programming languages. Object calculi: model inheritance and subtyping.. —lambda calculi with records Process calculi: model concurrency and communication —CSP, CCS, pi calculus, CHAM, blue calculus Distributed calculi: model location and failure —ambients, join calculus

A quick look at the π calculus © Oscar Nierstrasz Safety Patterns 27 (x)(x.0 | x(y).y.x(y).0) | z(v).v.0  (x)(0 | z.x(y).0) | z(v).v.0  (x)(0 | x(y).0 | x.0)  (x)(0 | 0 | 0) en.wikipedia.org/wiki/Pi_calculus new channeloutput input concurrency

© O. Nierstrasz PS — Fixed Points 7.28 What you should know!  Why isn’t it possible to express recursion directly in the lambda calculus?  What is a fixed point? Why is it important?  How does the typed lambda calculus keep track of the types of terms?  How does a polymorphic function differ from an ordinary one?

© O. Nierstrasz PS — Fixed Points 7.29 Can you answer these questions?  How would you model negative integers in the lambda calculus? Fractions?  Is it possible to model real numbers? Why, or why not?  Are there more fixed-point operators other than Y?  How can you be sure that unfolding a recursive expression will terminate?  Would a process calculus be Church-Rosser?

© Oscar Nierstrasz ST — Introduction 1.30 Attribution-ShareAlike 3.0 Unported You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. License