The fixed point combinator doesn't always produce the right answer given the definition:
fix f = f (fix f)
The following code does not terminate:
fix (\x->x*x) 0
Of course, fix
can't always produce the right answer, but I was wondering, can this be improved?
Certainly for the above example, one can implement some fix that looks like
fix f x | f x == f (f x) = f x
| otherwise = fix f (f x)
and gives the correct output.
What is the reason the above definition (or something even better, as this one only handle function with 1 parameter) is not used instead?
fix will find a fixed point of fact' , i.e. the function f such that f == fact' f . But let's expand what this means: f = fact' f = \n -> if n == 0 then 1 else n * f (n-1)
A combinator is a closed lambda expression, meaning that it has no free variables.
Julia Language Combinators The Y or Z Combinator This combinator takes a function and returns a function that when called with argument x , gets passed itself and x .
Fixed point combinator finds the least-defined fixed point of a function, which is ⊥ in your case (non-termination indeed is undefined value).
You can check, that in your case
(\x -> x * x) ⊥ = ⊥
i.e. ⊥
really is fixed point of \x -> x * x
.
As for why is fix
defined that way: the main point of fix
is to allow you use anonymous recursion and for that you do not need more sophisticated definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With