10

year.hs:

year = year + 1

main = print year

This is not a tail recursive call:

year = year + 1
year = (year + 1) + 1
year = ((year + 1) + 1) + 1
...

However runhaskell year.hs does not output anything, which indicates that it run into infinite loop.

Does Haskell compiler make optimizations even for non tail recursive calls?

4
  • 2
    Watch your memory usage. It depends on the optimizations, but that's one thing that can happen.
    – luqui
    Dec 31, 2014 at 21:18
  • What is the supposed output? Dec 31, 2014 at 21:19
  • I'd assume the result of this is undefined. Dec 31, 2014 at 21:20
  • @Comm The supposed output is Exception: stack overflow Dec 31, 2014 at 21:39

1 Answer 1

19

Because of laziness (plus the monomorphism restriction). Basically when you say

year = year + 1

and then evaluate year, Haskell saves space for the result, and then when it sees year it tries to re-use the same result. So when year + 1 tries to evaluate year, the code for year doesn't actually get entered.

In GHC, to implement multi-threading, what it actually does is block the current thread when it tries to obtain the value of a variable that's already being evaluated. Then when the evaluation finishes, it resumes execution of the blocked thread. In this case, the thread is being blocked on an evaluation it itself is doing, which is why you get a deadlock.

If you instead say

year () = year () + 1

then running year () does give a stack overflow for me.


The monomorphism restriction comes into play because if you add a type signature

year :: Num a => a
year = year + 1

the compiler is perfectly free to treat the Num a dictionary like the () parameter, yielding a stack overflow. In this case that's not really a problem, but not caching intermediate results is a big problem in real computations. In this case, it looks like GHC is actually putting the recursion inside the abstraction over the dictionary, producing code more like

year () = let y = y + 1 in y

which also doesn't produce a stack overflow.


Compiling the code (with GHC) in single-threaded mode yields

<<loop>>

which means GHC detected the infinite loop and decided to complain about it.

7
  • For clarification: The type of year defaults to Int, or am I mistaken?
    – ThreeFx
    Jan 1, 2015 at 1:36
  • @ThreeFx that sounds reasonable, but I can't check now. It doesn't matter, though, because you can give year any monomorphic type signature (e.g., year :: Int, year :: Integer, year :: Float, etc.) (as long as the type has a Num instance) without changing the behavior (as long as + on that type is strict) Jan 1, 2015 at 1:39
  • 1
    @ThreeFx You can change what type whole-number and decimal number literals default to, but the default default is (Integer, Double).
    – AndrewC
    Jan 1, 2015 at 2:32
  • Just added year :: Num a => a and got the same result. Still no stack overflow Jan 1, 2015 at 6:08
  • @orionll, just tried it and you are correct. It looks like GHC does optimize this case (putting the recursion inside the dictionary abstraction) so you get the lazy behavior even with a polymorphic type signature. I'll edit my answer when I get a chance. Jan 1, 2015 at 16:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.