Questions tagged [fixpoint-combinators]

Questions about fixed-point combinators, used to encode recursion. For fixed-point arithmetic, use [fixed-point] instead. For the numerical method, used [fixed-point-iteration] instead.

A fixed-point (or fixpoint) combinator produces fixed points of functions given to it. If fix is such a combinator, we have fix f = f (fix f).

The best known fixpoint combinator is the Y combinator, which makes it possible to encode recursive definitions in the untyped lambda calculus. It has its own tag, . Another example is Haskell's fix function, which is of necessity different from the Y combinator (see Y Combinator in Haskell).

There are also type-level fixpoint combinators, such as Haskell's Fix type constructor, which also fall under the scope of this tag. When these are concerned, depending on the focus of the question it may be appropriate to use or in addition to, or instead of, .

77 questions
92
votes
4 answers

How do I use fix, and how does it work?

I was a bit confused by the documentation for fix (although I think I understand what it's supposed to do now), so I looked at the source code. That left me more confused: fix :: (a -> a) -> a fix f = let x = f x in x How exactly does this return…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
36
votes
2 answers

Difference between free monads and fixpoints of functors?

I was reading http://www.haskellforall.com/2013/06/from-zero-to-cooperative-threads-in-33.html where an abstract syntax tree is derived as the free monad of a functor representing a set of instructions. I noticed that the free monad Free is not much…
Daniel
  • 24,697
  • 12
  • 57
  • 88
27
votes
1 answer

What is the difference between Fix, Mu and Nu in Ed Kmett's recursion scheme package

In Ed Kmett's recursion-scheme package, there are three declarations: newtype Fix f = Fix (f (Fix f)) newtype Mu f = Mu (forall a. (f a -> a) -> a) data Nu f where Nu :: (a -> f a) -> a -> Nu f What is the difference between those three data…
19
votes
3 answers

Why is this version of 'fix' more efficient in Haskell?

In Haskell, this is a simple (naive) definition of a fixed point fix :: (a -> a) -> a fix f = f (fix f) But, here is how Haskell actually implements it (more efficient) fix f = let x = f x in x My question is why is the second one more efficient…
Vijaya Rani
  • 195
  • 1
  • 7
18
votes
4 answers

Haskell: to fix or not to fix

I recently learned about Data.Function.fix, and now I want to apply it everywhere. For example, whenever I see a recursive function I want to "fix" it. So basically my question is where and when should I use it. To make it more specific: 1) Suppose…
Vadim
  • 689
  • 5
  • 9
15
votes
4 answers

Why does GHC make fix so confounding?

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…
Vanson Samuel
  • 1,901
  • 1
  • 16
  • 29
14
votes
2 answers

The fixed point functors of Free and Cofree

To make that clear, I'm not talking about how the free monad looks a lot like a fixpoint combinator applied to a functor, i.e. how Free f is basically a fixed point of f. (Not that this isn't interesting!) What I'm talking about are fixpoints of…
leftaroundabout
  • 101,764
  • 3
  • 156
  • 291
13
votes
5 answers

Fixed point combinator in Haskell

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…
Chao Xu
  • 2,028
  • 2
  • 20
  • 29
13
votes
3 answers

Fixed point combinator for mutually recursive functions?

Is there a fixed point combinator for creating tuples of mutually recursive functions? I.e. I'm looking for something like the Y-Combinator but which takes multiple "recursive"* functions, and will return a tuple of functions? *: not really…
10
votes
2 answers

Transforming a function that computes a fixed point

I have a function which computes a fixed point in terms of iterate: equivalenceClosure :: (Ord a) => Relation a -> Relation a equivalenceClosure = fst . List.head -- "guaranteed" to exist . List.dropWhile (uncurry…
nomen
  • 3,428
  • 1
  • 21
  • 35
10
votes
3 answers

Useful instantiations of “fix” on non-function types?

Every time I’ve used fix :: (a -> a) -> a, it’s been at the type ((a -> b) -> a -> b) -> a -> b for some a and b. Is there actually some application of fix where its type parameter is not instantiated to a function type, other than a trivial thing…
Jon Purdy
  • 49,516
  • 7
  • 90
  • 154
10
votes
1 answer

Writing generic instances for Fix/Mu in F-algebras

After reading Milewski's F-algebra article, I tried to implement it and use for a real-world problem. However, I can't seem to figure out how to write instances for Fix, newtype Fix f = Fx { unFix :: f (Fix f) } cata :: Functor f => (f a -> a) ->…
10
votes
2 answers

haskell - flip fix / fix

>>>flip fix (0 :: Int) (\a b -> putStrLn "abc") Output: "abc" This is a simplified version of using flip fix. I saw this way of using it in some youtube video which are probably from google tech talk or some other talks. Can somebody give me some…
pochen
  • 855
  • 10
  • 21
9
votes
1 answer

What order do "least" and "greatest" refer to when talking about fixed point?

In texts about fixed points in Haskell there is often mention of least and greatest fixed points. E.g. in the Data.Functor.Fixedpoint documentation or here. Least and greatest imply an order on the involved types (or is it enough to solely define…
michid
  • 8,478
  • 3
  • 28
  • 49
9
votes
1 answer

Understanding the Fix datatype in Haskell

In this article about the Free Monads in Haskell we are given a Toy datatype defined by: data Toy b next = Output b next | Bell next | Done Fix is defined as follows: data Fix f = Fix (f (Fix f)) Which allows to nest Toy expressions by…
Jesuspc
  • 1,242
  • 7
  • 18
1
2 3 4 5 6