
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@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0xF0FBF51F Debian Developer: nomeata@debian.org