Is it possible to write down a sharing fix point-free?

104 Views Asked by At

Edit: fix in this post stands for a fixed-point combination written down in Haskell in general, not just Data.Function.fix.

It is widely known that fix could be non-sharing as GHC does not always eliminate common subexpressions:

Long story short: "If you care about CSE, do it by hand."

fix could be written down point-free pretty easily with the use of the S-combinator:

fix = ($) <*> fix

If we return the points, we would get the well-known non-sharing implementation:

fix f = ($) <*> fix $ f = ($) f (fix f) = f (fix f)

I believe, CSE could not be done by GHC in this case as fix f hinges on f's laziness in its first argument. Indeed, as Daniel Wagner suggested, take a look at the following snippet:

let ones = fix (1:) in (ones !! 10000, ones !! 0)

With fix f = let x = f x in x it used up ~54,5 kB, whereas with fix = ($) <*> fix - ~615 kB.

Is it possible to write a sharing point-free fix down somehow?

1

There are 1 best solutions below

5
Daniel Wagner On

No. let is GHC's cue to share things, and let requires a point.