4

I'm working my way through Real World Haskell (I'm in chapter 4) and to practice a bit off-book I've created the following program to calculate the nth prime.

import System.Environment

isPrime primes test = loop primes test
    where
        loop (p:primes) test
            | test `mod` p == 0 = False
            | p * p > test = True
            | otherwise = loop primes test


primes = [2, 3] ++ loop [2, 3] 5
    where 
        loop primes test
            | isPrime primes test = test:(loop primes' test')
            | otherwise = test' `seq` (loop primes test')
            where
               test' = test + 2
               primes' = primes ++ [test]

main :: IO()
main = do
    args <- getArgs
    print(last  (take (read (head args) :: Int) primes))

Obviously since I'm saving a list of primes this is not a constant space solution. The problem is when I try to get a very large prime say ./primes 1000000 I receive the error:

    Stack space overflow: current size 8388608 bytes.
    Use `+RTS -Ksize -RTS' to increase 

I'm fairly sure that I got the tail recursion right; reading http://www.haskell.org/haskellwiki/Stack_overflow and the various responses here lead me to believe that it's a byproduct of lazy evaluation, and thunks are building up until it overflows, but so far I've been unsuccessful in fixing it. I've tried using seq in various places to force evaluation, but it hasn't had an effect. Am I on the right track? Is there something else I'm not getting?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Walton Hoops
  • 864
  • 4
  • 14
  • 2
    You're right that laziness doesn't mix well with tail-recursion, but when working with lists the solution is usually to remove the tail-recursion, not to remove the laziness. – sepp2k Aug 12 '12 at 22:44
  • @sepp2k although it is usually not to remove the TR outright, but rather turn it into "tail recursion modulo cons" (not just *list* cons, *any* cons), aka guarded recursion. but to say that TRMC is not TR at all, doesn't feel right IMO. Maybe that concept is less known than it ought to be. :) And when guarded rec is not TRMC, well that exactly means thunk pile up. TRMC means, *constant* amount of work between recursive calls, rearranged to be done *before* the call, not *after* it, as in regular recursion schemes. – Will Ness Aug 13 '12 at 06:53

3 Answers3

6

As I said in my comment, you shouldn't be building a list by appending a single element list to the end of a really long list (your line primes' = primes ++ [test]). It is better to just define the infinite list, primes, and let lazy evaluation do it's thing. Something like the below code:

primes = [2, 3] ++ loop 5
    where.
        loop test
            | isPrime primes test = test:(loop test')
            | otherwise = test' `seq` (loop test')
            where
                test' = test + 2

Obviously you don't need to parameterize the isPrime function by primes either, but that's just a nit. Also, when you know all the numbers are positive you should use rem instead of mod - this results in a 30% performance increase on my machine (when finding the millionth prime).

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • Oh my, that solution makes so much sense. I still don't fully understand why my solution was blowing up, but I think I'm kind of getting it. I'll meditate on it a bit and keep studying. Thank you! – Walton Hoops Aug 13 '12 at 00:35
  • You're solution was blowing up because, due to the definition of `(++)`, you were building a computation on the stack equivalent to `prime1 : prime 2 : prime3 : ... : prime1000000 : []`. – Thomas M. DuBuisson Aug 13 '12 at 00:37
  • AH! That just clicked! So ++ actually creates the new list by running prime1:prime2.... so it was ++ that was blowing up when the list got to long, hence why attempting to force evaluation primes' with `seq` didn't help me. – Walton Hoops Aug 13 '12 at 00:52
2

First, you don't have tail recursion here, but guarded recursion, a.k.a. tail recursion modulo cons.

The reason you're getting a stack overflow is, as others commented, a thunk pile-up. But where? One suggested culprit is your use of (++). While not optimal, the use of (++) not necessarily leads to a thunk pileup and stack overflow. For instance, calling

take 2 $ filter (isPrime primes) [15485860..]

should produce [15485863,15485867] in no time, and without any stack overflow. But it is still the same code which uses (++), right?

The problem is, you have two lists you call primes. One (at the top level) is infinite, co-recursively produced through guarded (not tail) recursion. Another (an argument to loop) is a finite list, built by adding each newly found prime to its end, used for testing.

But when it is used for testing, it is not forced through to its end. If that happened there wouldn't be an SO problem. It is only forced through to the sqrt of a test number. So (++) thunks do pile up past that point.

When isPrime primes 15485863 is called, it forces the top-level primes up to 3935, which is 547 primes. The internal testing-primes list too consists of 547 primes, of which only first 19 are forced.

But when you call primes !! 1000000, out of the 1,000,000 primes in the duplicate internal list only 547 are forced. The rest are all in thunks.

If you were adding new primes to the end of testing-primes list only when their square was seen among the candidates, the testing-primes list would be always forced through completely, or nearly to its end, and there wouldn't be a thunk pileup causing the SO. And appending with (++) to the end of a forced list is not that bad when next access forces that list to its end and leaves no thunks behind. (It still copies the list though.)

Of course the top-level primes list can be used directly, as Thomas M. DuBuisson shows in his answer.

But the internal list has its uses. When correctly implemented, adding new primes to it only when their square is seen among the candidates, it may allow your program to run in O(sqrt(n)) space, when compiled with optimizations.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • Thanks that makes it a lot clearer as to why the overflow is occurring. So if I'm understanding the guarded recursion stuff properly, the [implementation of `(++)`](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-Base.html#%2B%2B) is also guarded and as long as it's forced would not overflow, is that correct? – Walton Hoops Aug 13 '12 at 20:50
  • @WaltonHoops I think so, yes. Accessing last element of `xs++[z]` forces thunks out, copying list anew in the process (unless on Very Smart Compiler). See e.g. http://stackoverflow.com/q/11656507 how *not* to build a list (appending with (++) though tail recursion). In quicksort's `qsort small++[x]++qsort big` the `++[x]++` is harmless. When the sorted list's head gets accessed, there are logarithmic number of thunks in place (on non-degenerate data). As the sorted list gets accessed, the number of thunks in place remains logarithmic. – Will Ness Aug 14 '12 at 05:13
  • @WaltonHoops But in your code, adding to whole list (even if wholly/partially forced) creates new thunk over whole list, and the whole list needs to be accessed through to the end *again* to force the thunks out. So as long as only as much primes are added to the internal list as needed, the list will get accessed completely (or nearly so) keeping the number of thunks to the minimum. The problem with your code is, you get a handle (a named var) on the whole list (from its head) and repeatedly add to it through from the start again and again. – Will Ness Aug 14 '12 at 05:32
0

You should probably check these two questions:

  1. How can I increase the stack size with runhaskell?
  2. How to avoid stack space overflows?
Community
  • 1
  • 1
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53
  • 1
    I appreciate the response, but I already know how to increase the stack, my understanding though is I shouldn't need to for this since it's tail recursive. I've tried using seq as the other suggests, but it hasn't had an effect, meaning either I haven't figured out how to use it properly, or I haven't been able to identify where the thunks are piling up. – Walton Hoops Aug 12 '12 at 22:46
  • You think this is ALL tail recursive? How about [this](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-Base.html#%2B%2B). You are consuming O(n) stack where `n` is the number of primes. Placing a single item at the end of the list is a bad idea, it is better to make an expression build the entire infinite list (lazily, of obviously). – Thomas M. DuBuisson Aug 13 '12 at 00:14