But if you try it on ghci you will see that it breaks. (ghc 8.0.1). So it dependes on how you "interpret" the code. `ghc -O` is sophisticated enough to make the right decisions when compiling your code. You probably don't want a space leak, so it doesn't generate one.
GHC will have more trouble making the right choice in more involved computations. It will revert to "just do what the coder says".
Consider this C code for example:
unsigned int sum = 0;
const unsigned int n = 1000;
for(int i = 0; i<n; i++) {
sum += 0;
}
If you look at it, you can tell this is O(1) in space for any valid `n`. A spohisticated enough compiler may replace it by
unsigned int sum = 0;
and be done with it. But even an unsophisticated(but correct) compiler will produce O(1) space wise code.
EDIT: format
EDIT: What I was trying to say in my original comment is that, in haskell, space leaks my appear even in the most innocent looking code. And that it may require a bit of experience to identify and avoid them.