I have a huge haskell file which compiles and runs without any problem. I want to put some functions and type definitions in a separate module in a generic hs file and then import it in my main module. While the main program compiles without any error (it also compiles the imported module), I get a stack space overflow when I try to run it.
I tried:
ghc --make -O2 Main.hs
./Main -- stack space overflow
Also:
ghc --make -O2 Main.hs Other.hs -o RunMe
./RunMe -- again, stack space overflow
Is it the right way to compile or am I missing anything?
You're compiling it correctly. The problem must be in the code itself. Splitting it into different modules likely caused GHC to apply optimizations differently which caused this problem to surface.
A likely reason is that GHC was previously able to use strictness analysis to generate a program that ran in constant stack space. Splitting the module in two then caused GHC to no longer be able to make the same strictness assumptions, so it was unable to guarantee that making the function strict was safe.
The solution will likely be to add your own strictness annotations or use a strict version of whichever function is causing this.
I could imagine that GHC is able to optimize the used stack of the functions better (by doing strictness analysis) when the functions are called from the same module they are defined in. It sounds like you have a space leak in at least one function of yours, and GHC is unable to optimize it away when it doesn't know how the function is called.
There are a lot of explanations around the net for finding and fixing stack overflows. See, for example, the Haskell Wiki and RWH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With