5/17/2023 0 Comments Stack haskell![]() ![]() In the case of (2 ), it is the only fixed point. ![]() ![]() So feeding undefined (i.e., ⊥) to (2 ) gives us undefined back. Now we can understand how fix finds fixed points of functions like (2 ): The special value undefined is also denoted by this ⊥. Divergent computations are denoted by a value of ⊥, i.e., we have that fix (2 ) = ⊥. So the values with type, for example, Int include, in fact, ⊥ as well as 1, 2, 3 etc. Every Haskell type actually includes a special value called bottom, written ⊥. We can repair this property, however, if we bring in some denotational semantics. But sometimes fix seems to fail at this, as sometimes it diverges. Which is precisely the definition of a fixed point! So it seems that fix should always find a fixed point. All we need to do is write the equation for fix the other way around: In fact, it's obvious from the definition of fix that it finds a fixed point. (We'll come to what "least defined" means in a minute.) For the two aforementioned examples that converge, this is readily seen:Īnd since there's no number x such that 2 x = x, it also makes sense that fix (2 ) diverges.įor each of the functions f in the above exercises for which you decided that fix f converges, verify that fix f finds a fixed point. This is where the name of fix comes from: it finds the least-defined fixed point of a function. For example, 0 is a fixed point of the function (* 3) since 0 * 3 = 0. Lastly, iteratively calculating an approximation of a square root of a number,įix (\next guess tol val -> if abs(guess^2-val) if abs(guess^2-val) cycle (1:x))įix and fixed points įix can also be defined in a way that does not introduce structural sharing:Ī fixed point of a function f is a value a such that f a = a. This is lazy evaluation at work: the printing function doesn't need to consume its entire input string before beginning to print, it starts printing as soon as it can. Let us try to see what actually happens in the first example: We first import the module to bring fix (which is also exported by the Data.Function module) into scope. Prelude > fix (const "hello") - Example 2 ![]()
0 Comments
Leave a Reply. |