fix

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.
Like contrast, but one function is given, and applied to events with matching controls.
In fix $ go a -> do ...; go xy any action after a go is ignored.
Monadic fixpoints. For a detailed discussion, see Levent Erkok's thesis, Value Recursion in Monadic Computations, Oregon Graduate Institute, 2002.
Greatest fixpoint of a Bifunctor (a Functor over the first argument with zipping).
Fixed points of a functor. Type f should be a Functor if you want to use simple recursion schemes or Traversable if you want to use monadic recursion schemes. This style allows you to express recursive functions in non-recursive manner. You can imagine that a non-recursive function holds values of the previous iteration. An example: First we define a base functor. The arguments b are recursion points.
>>> data ListF a b = Nil | Cons a b deriving (Show, Functor)
The list is then a fixed point of ListF
>>> type List a = Fix (ListF a)
We can write length function. Note that the function we give to foldFix is not recursive. Instead the results of recursive calls are in b positions, and we need to deal only with one layer of the structure.
>>> :{
let length :: List a -> Int
length = foldFix $ \x -> case x of
Nil      -> 0
Cons _ n -> n + 1
:}
If you already have recursive type, like '[Int]', you can first convert it to `Fix (ListF a)` and then foldFix. Alternatively you can use recursion-schemes combinators which work directly on recursive types.
A fix-point type.
Fix f is a fix point of the Functor f. Note that in Haskell the least and greatest fixed points coincide, so we don't need to distinguish between Mu f and Nu f. This type used to be called Y, hence the naming convention for all the yfoo functions. This type lets us invoke category theory to get recursive types and operations over them without the type checker complaining about infinite types. The Show instance doesn't print the constructors, for legibility.
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.
Monad transformer for evaluating to a fixpoint. The idea is that some transforms need to be run multiple times. Deciding whether to run a transform again can be somewhat tedious though, as you cannot necessarily just run some transform f on x until f x == x. This might not be ideal for a few reasons:
  • x might not implement Eq;
  • x might implement Eq, but could contain floats of NaN, in which case NaN /= NaN; or
  • checking equality can be expensive.
Instead, this provides a function called progress, with the same type as return, that marks the current transform as having "made progress": that is, it should be re-run again. Then you can call fixpoint with a function of type a -> FixT m a, which will be re-run until no progress is made.
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. If the function passed to fixIO inspects its argument, the resulting action will throw FixIOException.