Looking at the GHC source code I can see that the definition for fix is:
fix :: (a -> a) -> a
fix f = let x = f x in x
In an example fix is used like this:
fix (\f x -> let x' = x+1 in x:f x')
This basically yields a sequence of numbers that increase by one to infinity. For this to happen fix must be currying the function that it receives right back to that very function as it's first parameter. It isn't clear to me how the definition of fix listed above could be doing that.
This definition is how I came to understand how fix works:
fix :: (a -> a) -> a
fix f = f (fix f)
So now I have two questions:
- How does x ever come to mean fix x in the first definition?
- Is there any advantage to using the first definition over the second?
Let bindings in Haskell are recursive
First of all, realize that Haskell allows recursive let bindings. What Haskell calls "let", some other languages call "letrec". This feels pretty normal for function definitions. For example:
But it can seem pretty weird for value definitions. Nevertheless, values can be recursively defined, due to Haskell's non-strictness.
See A gentle introduction to Haskell sections 3.3 and 3.4 for more elaboration on Haskell's laziness.
Thunks in GHC
In GHC, an as-yet-unevaluated expression is wrapped up in a "thunk": a promise to perform the computation. Thunks are only evaluated when they absolutely must be. Suppose we want to
fix someFunction
. According to the definition offix
, that'sNow, what GHC sees is something like this.
So it happily makes a thunk for you and moves right along until you demand to know what
x
actually is.Sample evaluation
That thunk's expression just happens to refer to itself. Let's take the
ones
example and rewrite it to usefix
.So what will that thunk look like?
We can inline
ones
as the anonymous function\recur -> 1 : recur
for a clearer demonstration.Now then, what is
x
? Well, even though we're not quite sure whatx
is, we can still go through with the function application:Hey look, we're back at the definition we had before.
So if you believe you understand how that one works, then you have a good feel of how
fix
works.Yes. The problem is that the second version can cause a space leak, even with optimizations. See GHC trac ticket #5205, for a similar problem with the definition of
forever
. This is why I mentioned thunks: becauselet x = f x in x
allocates only one thunk: thex
thunk.It's easy to see how this definition works by applying equational reasoning.
What will
x
evaluate to when we try to evaluatefix f
? It's defined asf x
, sofix f = f x
. But what isx
here? It'sf x
, just as before. So you getfix f = f x = f (f x)
. Reasoning in this way you get an infinite chain of applications off
:fix f
=f (f (f (f ...)))
.Now, substituting
(\f x -> let x' = x+1 in x:f x')
forf
you getEdit: Regarding your second question, @is7s pointed out in the comments that the first definition is preferable because it is more efficient.
To find out why, let's look at the Core for
fix1 (:1) !! 10^8
:As you can see, after the transformations
fix1 (1:)
essentially becamemain_x = 1 : main_x
. Note how this definition refers to itself - this is what "tying the knot" means. This self-reference is represented as a simple pointer indirection at runtime:Now let's look at
fix2 (1:) !! 100000000
:Here the
fix2
application is actually preserved:The result is that the second program needs to do allocation for each element of the list (but since the list is immediately consumed, the program still effectively runs in constant space):
Compare that to the behaviour of the first program:
I have perhaps a bit simplified explanation that comes from inlining optimization. If we have
then
fix
is a recursive function and this means it cannot be inlined in places where it is used (anINLINE
pragma will be ignored, if given).However
is not a recursive function - it never calls itself. Only
x
inside is recursive. So when callingthe compiler can inline it into
and then continue simplifying it, for example
which is just as if the function were defined using the standard recursive notation without
fix
:The difference is in sharing vs copying.1
If we substitute the definition into itself, both are reduced as the same infinite application chain
f (f (f (f (f ...))))
. But the first definition uses explicit naming; in Haskell (as in most other languages) sharing is enabled by the ability to name things: one name is more or less guaranteed to refer to one "entity" (here,x
). The 2nd definition does not guarantee any sharing - the result of a callfix2 f
is substituted into the expression, so it might as well be substituted as a value.But a given compiler could in theory be smart about it and use sharing in the second case as well.
The related issue is "Y combinator". In untyped lambda calculus where there is no naming constructs (and thus no self-reference), Y combinator emulates self-reference by arranging for the definition to be copied, so referring to the copy of self becomes possible. But in implementations which use environment model to allow for named entities in a language, direct reference by name becomes possible.
To see a more drastic difference between the two definitions, compare
See also:
(especially try to work out the last two definitions in the last link above).
1 Working from the definitions, for your example
fix (\g x -> let x2 = x+1 in x : g x2)
we getand thus a proper recursive definition for
g
is actually created. (in the above, we write....x.... where {x = ...}
forlet {x = ...} in ....x....
, for legibility).But the second derivation proceeds with a crucial distinction of substituting a value back, not a name, as
so the actual call will proceed as e.g.
and we see that a new binding (for
g2
) is established here, instead of the previous one (forg
) being reused as with thefix1
definition.