Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Normally, definition of primes stream in Richard Bird's formulation of the sieve of Eratosthenes is self-referential:</p> <pre><code>import Data.List.Ordered (minus, union, unionAll) ps = ((2:) . minus [3..] . foldr (\p r -&gt; p*p : union [p*p+p, p*p+2*p..] r) []) ps </code></pre> <p>The primes <code>ps</code> produced by this definition are used as input to it. To prevent a <em>vicious circle</em> the definition is primed with the initial value, 2. This corresponds to the mathematical definition of the sieve of Eratosthenes as finding primes in the gaps between the composites, enumerated for each prime <em>p</em> by counting up in steps of <em>p</em>, <strong>P</strong> = {2} <strong>U</strong> ({3,4,...} \ <strong>U</strong> {{<em>p<sup>2</sup></em>, <em>p<sup>2</sup>+p</em>, <em>p<sup>2</sup>+2p</em>, ...} | <em>p</em> in <strong>P</strong>}).</p> <p>The produced stream is used as input in its own definition. This causes the retention of the whole stream of primes in memory (or most of it anyway). The fixpoint here is <em>sharing</em>, <em>corecursive</em>:</p> <pre><code>fix f = xs where xs = f xs -- a sharing fixpoint combinator ps = fix ((2:) . minus [3..] . foldr (...) []) -- = xs where xs = 2 : minus [3..] (foldr (...) [] xs) </code></pre> <hr> <p><strong><em>The idea</em></strong> (due to Melissa O'Neill) is, then, to separate this into <em>two</em> streams, with an inner loop feeding into the second stream of primes "above" it:</p> <pre><code>fix2 f = f xs where xs = f xs -- double-staged fixpoint combinator ps2 = fix2 ((2:) . minus [3..] . foldr (...) []) -- = 2 : minus [3..] (foldr (...) [] xs) where -- xs = 2 : minus [3..] (foldr (...) [] xs) </code></pre> <p>Thus, when <code>ps2</code> produces some prime <code>p</code>, its inner stream <code>xs</code> of <em>"core"</em> primes needs only be instantiated up to about <code>sqrt p</code>, and any primes which are produced by <code>ps2</code> can get discarded and garbage-collected by the system immediately afterwards: </p> <pre> \ \ &lt;- ps2 &lt;-. \ \ &lt;- xs &lt;-. / \ \_________/ </pre> <p>Primes produced by the inner loop <code>xs</code> can not be immediately discarded, because they are needed for <code>xs</code> stream itself. When <code>xs</code> has produced a prime <code>q</code>, only its part below <code>sqrt q</code> can be discarded, just after it has been consumed by the <code>foldr</code> part of the computation. In other words this sequence maintains back pointer into itself down to the <code>sqrt</code> of its biggest produced value (as it is being consumed by <em>its</em> consumer, like <code>print</code>).</p> <p>So with one feed loop (with <code>fix</code>) almost the whole sequence would have to be retained in memory, while with the double feed (with <code>fix2</code>) only the inner loop needs to be mostly retained that only reaches up to the square root of the current value produced by the main stream. Thus the overall space complexity is reduced from about <em>O(N)</em> to about <em>O(sqrt(N))</em> -- a drastic reduction.</p> <p>For this to work the code must be compiled with optimizations, i.e. with the <code>-O2</code> switch, and run standalone. You may also have to use <code>-fno-cse</code> switch. And there must be only one reference to <code>ps2</code> in the testing code:</p> <pre><code>main = getLine &gt;&gt;= (read &gt;&gt;&gt; (+(-1)) &gt;&gt;&gt; (`drop` ps2) &gt;&gt;&gt; print . take 5) </code></pre> <p>In fact, when tested at Ideone, <a href="http://ideone.com/p0e81" rel="nofollow noreferrer">it does show</a> a practically constant memory consumption.</p> <hr> <p><strong>And it's the Sieve of Eratosthenes, not Euler's sieve.</strong></p> <p>The initial definitions are:</p> <pre><code>eratos (x:xs) = x : eratos (minus xs $ map (*x) [x..] ) -- ps = eratos [2..] eulers (x:xs) = x : eulers (minus xs $ map (*x) (x:xs)) -- ps = eulers [2..] </code></pre> <p>Both are very inefficient because of the premature handling of the multiples. It is easy to remedy the <em>first</em> definition by fusing the <code>map</code> and the enumeration into one enumeration moved further away (from <code>x</code> to <code>x*x</code>, i.e. <code>[x*x, x*x+x..]</code>), so that its handling can be <strong><em>postponed</em></strong> -- because here each prime's multiples are generated <strong><em>independently</em></strong> (enumerated at fixed intervals): </p> <pre><code>eratos (p:ps) xs | (h,t) &lt;- span (&lt; p*p) xs = -- ps = 2 : eratos ps [2..] h ++ eratos ps (minus t [p*p, p*p+p..]) -- "postponed sieve" </code></pre> <p>which is the same as Bird's sieve at the top of this post, <em>segment-wise</em>:</p> <pre><code>ps = 2 : [n | (r:q:_, px) &lt;- (zip . tails . (2:) . map (^2) &lt;*&gt; inits) ps, n &lt;- [r+1..q-1] `minus` foldr union [] [[s+p, s+2*p..q-1] | p &lt;- px, let s = r`div`p*p]] </code></pre> <p>(<code>(f &lt;*&gt; g) x = f x (g x)</code> is used here as a <a href="/questions/tagged/pointfree" class="post-tag" title="show questions tagged &#39;pointfree&#39;" rel="tag">pointfree</a> shorthand.)</p> <p>There is no easy fix for the second definition, i.e. <code>eulers</code>. </p> <hr> <p><em>addition:</em> you can see the same idea implemented with Python generators, for comparison, <a href="https://stackoverflow.com/a/10733621/849891">here</a>.</p> <p>In fact, that Python code employs a <em>telescopic, multistage</em> recursive production of ephemeral primes streams; in Haskell <a href="http://ideone.com/se0bsB" rel="nofollow noreferrer">we can arrange for it</a> with the non-sharing, multi-staged fixpoint combinator <strong><code>_Y</code></strong>: </p> <pre><code>primes = 2 : _Y ((3:) . sieve 5 . unionAll . map (\p -&gt; [p*p, p*p+2*p..])) where _Y g = g (_Y g) -- == g . g . g . g . .... sieve k s@(x:xs) | k &lt; x = k : sieve (k+2) s -- == [k,k+2..] \\ s, | True = sieve (k+2) xs -- when s ⊂ [k,k+2..] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload