[Haskell-cafe] Preventing sharing

Joachim Breitner mail at joachim-breitner.de
Fri Dec 18 14:36:40 UTC 2015


Hi,

Am Freitag, den 18.12.2015, 09:40 +0000 schrieb David Turner:
> Now I'm a bit stuck: how do you prevent this from happening?
> Obviously here we could just implement fibs differently, but as a
> more general question, how would you prevent unwanted sharing from
> taking place?

floating out (and, relatedly, CSE) is tricky to get a good grip on, and
I wish there were good and easy solutions. This also comes up with
things like "zip xs [0..]" and gets in the way of applying rules.

Note that in your example, depending on the use case, maybe the
programmer did want to share the fibs list. So it is not clear that the
compiler can always do the right thing.

You can pass -fno-full-laziness to GHC (and you can do that with a per-
module pragma), this might work in your case, but you better check.

Another trick is to add an argument to fibs, e.g. the initial starting
values

fib n = fibs 0 1 !! n
  where
   fibs a b = go where go = a : b : zipWith (+) go (tail go)
   {-# NOINLINE fibs #-}

The noinline pragma might be required as otherwise GHC will simplify
the code again to your origin form, and then share the go with a and be
specialized to 0 and 1. Again, check the core if it indeed does what
you want.

Both approaches are unsatisfying.


Greetings,
Joachim

-- 
Joachim “nomeata” Breitner
  mail at joachim-breitner.dehttp://www.joachim-breitner.de/
  Jabber: nomeata at joachim-breitner.de  • GPG-Key: 0xF0FBF51F
  Debian Developer: nomeata at debian.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151218/8fd89d4d/attachment.sig>


More information about the Haskell-Cafe mailing list