[Haskell-cafe] shared local definitions

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Thu May 18 05:06:47 EDT 2006


On Thu, 2006-05-18 at 11:00 +0200, Alberto Ruiz wrote:

> When I compute (using ghc -O) things like
> 
> map g1 [1 .. 1000]
> 
> the common q is evaluated only once, which is very nice. But the problem 
> is that in some strange cases this kind of optimization is not applied, and 
> the same q is evaluated 1000 times. Curiously, this happens if I add just:
> 
> module Main where

This is explained by how it affects what functions your module exports.
When you have no module header, the default is that it is the Main
module and that nothing other than the function main is exported. When
you add "module Main where" then everything in the module is exported.
The compiler can often optimise things better if it knows that they do
not need to be exported.

For example if a function is not exported and only used once then the
compiler will probably decide that it is beneficial to inline. If it
needs to keep a copy of the function to export from the module then it
may decide that the code duplication is not worth it and not inline.

Duncan



More information about the Haskell-Cafe mailing list