Download presentation
Presentation is loading. Please wait.
Published byRandall Lane Modified over 9 years ago
1
Fixed Point Illustrations
2
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.
3
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
4
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)))
5
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)))
6
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)) …
7
… 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
8
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.0.0.0.1.8: (0 ms) GC #0.0.0.1.2.11: (156 ms) *)
9
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)) ) ) 3 -- 6 :: Integer -- Main> ( fix ( \f -> (\n -> if n == 0 then 1 else n * f (n-1)) ) ) 5 -- 120 :: Integer -- Main> ( fix ( \f -> (\n -> if n == 0 then 1 else n * f (n-1)) ) ) 6 -- 720 :: Integer -- Main> fix (\n -> n*n - 2) -- ERROR - C stack overflow
10
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
11
References for Combinatory Logic To Kill a Mocking Bird – by Harper Lee To Mock a Mocking Bird – by Raymond Smullyan
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.