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.