Question

Perhaps it's a bit silly question, but I'm used to scripting languges, where the question never occurs, as there's no compilation at all. The question is: what is really imported to a Haskell executable: are whole imported libs incorporated or does it rather include in the executable only the code that is really necessary to run the program?

Suppose I have something like that:

import qualified Data.Set as Set

mySet = Set.fromList [1..5]

main = print $ Set.filter (\x -> x `mod` 2 == 0) mySet

Is whole Data.Set module included to the executable? Or does it rather include only Set definition and fromList and filter functions? Does it make any difference if I write:

import qualified Data.Set as Set (Set, fromList, filter)

At least executable size does not seem affected with the change...

Was it helpful?

Solution

A library module is linked as a whole unless it has been compiled with the GHC -split-objs option. However this makes compilation slower, so it is not the default. From GHC documentation:

However, assembling all the sections separately is expensive, so this is slower than compiling normally. We use this feature for building GHC's libraries (warning: don't use it unless you know what you're doing!).

OTHER TIPS

By using import you just bring some definitions into scope.

It is the linker's job to call the functions from the library. The library is still the same, your code only contains calls to it.

I think that the entire library will be included. But as you correctly pointed out some parts of the library will not be called either directly or indirectly. I believe, The code optimization stage of the compiler will remove those parts of the code which are not called. This should result in the final executable containing only those parts which are absolutely necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top