I have a piece of Haskell code that computes regular numbers, i.e. positive integers whose only prime factors can be 2, 3 or 5. The algorithm is straightforward and follows what's suggested in the same Wikipedia article.
regularSeq :: [Integer]
regularSeq = 1 : union timesTwo (union timesThree timesFive)
where
timesTwo = map (* 2) regularSeq
timesThree = map (* 3) regularSeq
timesFive = map (* 5) regularSeq
union :: (Ord a) => [a] -> [a] -> [a]
union [] ys = ys
union xs [] = xs
union (x : xs) (y : ys)
| x < y = x : union xs (y : ys)
| x > y = y : union (x : xs) ys
| otherwise = x : union xs ys
Example:
ghci> takeWhile (<= 60) regularSeq
[1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36,40,45,48,50,54,60]
I have a couple questions regarding the performance of this code, specifically the lazy evaluation, "memoization" and memory usage.
Since the computation of a new number in the sequence relies on previous values further back, are older values of
regularSeq
"cached"/"memoized" and reused in the computation oftimesTwo
/timesThree
/timesFive
? Or does the recursive code spawn an inefficient degree-3 tree of computations similar to the naive Fibonacci implementation?fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib (n-2)
During the evlauation of
regularSeq
, is there only a single list of integers present in memory, withtimesTwo
/timesThree
/timesFive
acting as pointers to different elements inside this same list? Or do they point to independent lists, thus not sharing computation?In my mind,
timesTwo
/timesThree
/timesFive
simply "lag behind" and reuse the values already discovered by the evaluation ofregularSeq
, however I'm not entirely sure this is correct.If I were to implement the sequence in an imperative language (say C or Rust) as an endless stream, I would keep in memory only the values from the head of
timesFive
to the current value, as the older ones are no longer needed to compute further elements. Is the Haskell garbage collector able to see that older values are not reachable anymore and does it deallocate them? Or does it deallocate the entire sequence only when it is discarded in its entirety?I find it quite hard to reason about the memory behavior of Haskell programs, and it is often not apparent to me whether the result of computation is shared or something need to be unnecessarily re-evaluated. What are some general principles and a good framework to reason about this problem?
fib 1
in bothfib (n-1)
andfib (n-2)
, whereas I expectregularSeq
to somehow "reuse" the already computed elements in the evaluation of thetimes*
subexpressions. I don't know how to name this fact. I don't even know whether it's true. That's basically the question.