
On Wednesday 15 September 2010 22:38:48, Tim Chevalier wrote:
On the other hand, if you wrote:
let fib50 = slowFib 50 in fib50 + (slowFib 50)
then (slowFib 50) would be evaluated twice, because there's no principle requiring the compiler to notice that (slowFib 50) is the same expression as the one bound to fib50.
If you compile your module with ghc -O[2], ghc does notice, whether you give it a name once or not - without optimisations, it doesn't, however. And if the two expressions appear far enough apart, it will probably not notice with optimisations either. The upshot is, if you don't name an expression, the compiler *might* notice repeated use and share it, but you'd better not rely on it. If you want an expression to be shared, name it - then a decent implementation will share it.