5

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?

0

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Gratuitous use of INLINE pragmas would probably fix this too.
Adding strictness annotations indeed fixed the problem. This was not needed in a single module.
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.