fix -is:module

fix f is the least fixed point of the function f, i.e. the least defined x such that f x = x. When f is strict, this means that because, by the definition of strictness, f ⊥ = ⊥ and such the least defined fixed point of any strict function is .

Examples

We can write the factorial function using direct recursion as
>>> let fac n = if n <= 1 then 1 else n * fac (n-1) in fac 5
120
This uses the fact that Haskell’s let introduces recursive bindings. We can rewrite this definition using fix, Instead of making a recursive call, we introduce a dummy parameter rec; when used within fix, this parameter then refers to fix’s argument, hence the recursion is reintroduced.
>>> fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5
120
Using fix, we can implement versions of repeat as fix . (:) and cycle as fix . (++)
>>> take 10 $ fix (0:)
[0,0,0,0,0,0,0,0,0,0]
>>> map (fix (\rec n -> if n < 2 then n else rec (n - 1) + rec (n - 2))) [1..10]
[1,1,2,3,5,8,13,21,34,55]

Implementation Details

The current implementation of fix uses structural sharing
fix f = let x = f x in x
A more straightforward but non-sharing version would look like
fix f = f (fix f)
fix f is the least fixed point of the function f, i.e. the least defined x such that f x = x. For example, we can write the factorial function using direct recursion as
>>> let fac n = if n <= 1 then 1 else n * fac (n-1) in fac 5
120
This uses the fact that Haskell’s let introduces recursive bindings. We can rewrite this definition using fix,
>>> fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5
120
Instead of making a recursive call, we introduce a dummy parameter rec; when used within fix, this parameter then refers to fix’s argument, hence the recursion is reintroduced.
The fix function applies another function to matching events in a pattern of controls. fix is contrast where the false-branching function is set to the identity id. It is like contrast, but one function is given and applied to events with matching controls. For example, the following only adds the crush control when the n control is set to either 1 or 4:
d1 $ slow 2
$ fix (# crush 3) (n "[1,4]")
$ n "0 1 2 3 4 5 6"
# sound "arpy"
You can be quite specific; for example, the following applies the function hurry 2 to sample 1 of the drum sample set, and leaves the rest as they are:
fix (hurry 2) (s "drum" # n "1")
In fix $ go a -> do ...; go xy any action after a go is ignored.
If you turn OverloadedStrings extension on, GHC can't deduce the type of string literal. This function fix the type to Builder Builder without type annotation.
Greatest fixpoint of a Bifunctor (a Functor over the first argument with zipping).
A fix-point type.
A fixed-point constructor that uses Haskell's built-in recursion. This is strict/recursive.
Fixed point newtype. Ideally we should use the data-fix package, but right now we're rolling our own due to an initial idea to avoid dependencies to be easier to upstream into GHC (for improvements to the pattern match checker involving equality graphs). I no longer think we can do that without vendoring in some part of just e-graphs, but until I revert the decision we use this type.
A fix-point type.
A fix-point type.
A basic Functor fixpoint like you'd see anywhere.
Allow the result of an ST computation to be used (lazily) inside the computation. Note that if f is strict, fixST f = _|_.
Allow the result of an ST computation to be used (lazily) inside the computation. Note that if f is strict, fixST f = _|_.
The implementation of mfix for IO. This operation may fail with:

Examples

the IO-action is only executed once. The recursion is only on the values.
>>> take 3 <$> fixIO (\x -> putStr ":D" >> (:x) <$> readLn @Int)
:D
2
[2,2,2]
If we are strict in the value, just as with fix, we do not get termination:
>>> fixIO (\x -> putStr x >> pure ('x' : x))
* hangs forever *
We can tie the knot of a structure within IO using fixIO:
data Node = MkNode Int (IORef Node)

foo :: IO ()
foo = do
p <- fixIO (p -> newIORef (MkNode 0 p))
q <- output p
r <- output q
_ <- output r
pure ()

output :: IORef Node -> IO (IORef Node)
output ref = do
MkNode x p <- readIORef ref
print x
pure p
>>> foo
0
0
0