Fixed Point Illustrations. Simple Examples f : int -> int f(n) = 5 n = 5 is a unique fixed point. f(n) = n^2 – 2 n = 2 and n = -1 are both fixed points.

Slides:



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

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
School of Computing and Mathematics, University of Huddersfield CHA2545 Last Lecture LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: Revision.
MinML: an idealized programming language CS 510 David Walker.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
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.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
CMSC 330: Organization of Programming Languages Lambda Calculus Introduction λ.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
M180: Data Structures & Algorithms in Java
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
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.
Going Functional Primož Gabrijelčič. Functional programming.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
Logical and Functional Programming
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
-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
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
CMSC 330: Organization of Programming Languages Operational Semantics.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 8 LECTURE: LAMBDA CALCULUS SEMANTICS TUTORIAL: exercises.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Functional Programming
Chapter 2: Lambda Calculus
Type Checking and Type Inference
CS 550 Programming Languages Jeremy Johnson
Lambda Calculus CSE 340 – Principles of Programming Languages
10.3 Details of Recursion.
A lightening tour in 45 minutes
Comp 205: Comparative Programming Languages
Control Flow Chapter 6.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
L Calculus.
Programming Languages and Compilers (CS 421)
CSC 143 Recursion.
Curry A Tasty dish? Haskell Curry!.
CSE S. Tanimoto Lambda Calculus
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Presentation transcript:

Fixed Point Illustrations

Simple Examples f : int -> int f(n) = 5 n = 5 is a unique fixed point. f(n) = n^2 – 2 n = 2 and n = -1 are both fixed points. f(n) = n This has infinite fixed points.

Fixed point computing function (fix f) = f (fix f) Declarative specification In certain formalizations of mathematics, such as the lambda calculus and combinatorial calculus, every function has a fixed point.lambda calculuscombinatorial calculus In Denotational Semantics, the meaning associated with a recursive definition is given in terms of least fixed point. E.g., while-loop, etc From a more practical point of view, this fixed point approach allows the definition of anonymous recursive functions. Somewhat surprisingly, the fixed-point function can be defined with non-recursive lambda abstractions.recursive functionslambda abstractions

Fixed Point Combinator A fixed point combinator Y is a function such that f(Y(f)) = Y(f) for all functions f. Well-known fixed point combinators –Discovered by Haskell B. CurryHaskell B. Curry Y = λf.(λx.(f (x x)) λx.(f (x x))) –Discovered by Alan TuringAlan Turing Θ = (λx.λy.(y (x x y)) λx.λy.(y (x x y)))

Factorial Function fac :: Int -> Int fac n = if (n<1) 1 (n * fac (n-1)) Now the factorial (using a fixed point combinator) looks like: factorial = Y ( fac n -> if (n<1) 1 (n * fac (n-1)))

F = \f.\x.if (x = 0) then 1 else (f (x-1)) Now we run factorial(3) as follows... ((Y F) 3) => F (Y F) 3 (note we now have F followed by 2 arguments, the first is the function copy and the second is the number 3). F (Y F) 3 => if (3 = 0) then 1 else ((Y F) (3-1)) Where Y F is used to copy F once again if (3 = 0) then 1 else 3 * ((Y F) (3-1)) if (3 = 0) then 1 else 3 * F (Y F) 2 if (3 = 0) then 1 else 3 * (if (2 = 0) then 1 else 2 * (Y F) 1) if (3 = 0) then 1 else 3 * (if (2 = 0) then 1 else 2 * F (Y F) 1) if (3 = 0) then 1 else 3 * (if (2 = 0) then 1 else 2 * (if (1 = 0) then 1 else 1 * (Y F) 0)) …

… if (3 = 0) then 1 else 3 * (if (2 = 0) then 1 else 2 * (if (1 = 0) then else 1 * (if (0 = 0) then 1 else 0 * (Y F) -1) )) Which we can now simply evaluate... if (3 = 0) then 1 else 3 * (if (2 = 0) then 1 else 2 * (if (1 = 0) then 1 else 1 * (1) )) if (3 = 0) then 1 else 3 * (if (2 = 0) then 1 else 2 * 1) if (3 = 0) then 1 else 3 * 2 6

SML code fun fix f = f (fix f); (* val fix = fn : ('a -> 'a) -> 'a *) fix (fn n => 5); ( fix ( fn f => (fn n => if n = 0 then 1 else n * f (n-1)) ) ) 5; (* Infinite loop due to "call by value" parameter passing mechanism GC # : (0 ms) GC # : (156 ms) *)

Haskell Code -- :load "I:\tkprasad\cs776\test.hs" fix f = f (fix f) -- fix (\n -> 5) -- Main> fix (\n -> 5) -- 5 :: Integer -- Main> ( fix ( \f -> (\n -> if n == 0 then 1 else n * f (n-1)) ) ) :: Integer -- Main> ( fix ( \f -> (\n -> if n == 0 then 1 else n * f (n-1)) ) ) :: Integer -- Main> ( fix ( \f -> (\n -> if n == 0 then 1 else n * f (n-1)) ) ) :: Integer -- Main> fix (\n -> n*n - 2) -- ERROR - C stack overflow

Scheme Code (define (Y f) ( (lambda (x) (f f)) (lambda (x) (f f)) ) ) (define (fact-nr f) (lambda (n) (if (zero? n) 1 (* n ((f f) (- n 1)))) )) ((Y fact-nr) 3) 6 ((Y fact-nr) 5) 120

References for Combinatory Logic To Kill a Mocking Bird – by Harper Lee To Mock a Mocking Bird – by Raymond Smullyan