5

I was thinking of a way to represent algebraic numbers in Haskell as a stream of approximations. You could probably do this by some root finding algorithm. But that's no fun. So you could add x to the polynomial, reducing the problem to finding it's fixed points.

So if you have a function in Haskell like

f :: Double -> Double
f x = x ^ 2 + x

I don't conceptually understand why fix doesn't work, which is to say, I can easily verify for myself that it doesn't work, but isn't 0 the true least fixed point of f? Is there another simple (as in definition size) fixed point function that would work?

  • 6
    `fix` finds the least _defined_ fixed point which in many cases is ⊥ (non-termination, error, ...). See also: http://stackoverflow.com/a/8099449/700253 – Vitus Jul 26 '13 at 20:03
  • This is a very good question, but I find it quite difficult to answer without going too abstract. I'm trying to formulate a good answer. – hivert Jul 26 '13 at 20:19
  • 2
    As vitus says, the ordering where fix finds the least fixed point is domain ordering, rather than the regular ordering on Double. – augustss Jul 26 '13 at 20:55
  • If you look at the tag wiki for [fixed-point], you'll see that it's not the tag you were looking for. – Gabe Jul 28 '13 at 04:10
  • @Vitrus, thanks for the clarification. I was also wondering if there is a fixed point function that doesn't only find the least fixed point. – Senjougahara Hitagi Jul 28 '13 at 08:26

1 Answers1

2

Here is the implementation of the fix function:

fix :: (a -> a) -> a
fix f = let x = f x in x

It doesn't work for primitive types like Double. It's intended for types that have a more complex structure to them. For instance:

g :: Maybe Int -> Maybe Int
g i = Just $ case i of
    Nothing -> 3
    Just _ -> 4

This function will work with fix because it yields information about its result faster than it reads its input. in other words, the Just portion is known without looking at i at all, which enables it to reach a fixed point.

When your function is Double -> Double, and examines its input, fix won't work because there's no way to partially evaluate a Double.

NovaDenizen
  • 4,555
  • 12
  • 24