[Haskell-cafe] random colors, stack space overflow, mersenne and mersenne.pure64

Cetin Sert cetin.sert at gmail.com
Fri Sep 12 21:55:02 EDT 2008


Hi,

The following piece of code runs just fine, if I say:

instance Random RGB where
  random  = color
  randomR = colorR

instead of:

instance Random RGB where
  random  = color2
  randomR = colorR

When I use random  = color2 I encounter a stack space overflow:

cetin at linux-d312:~/lab/test/colors> ./var2 +RTS -K30000000
Stack space overflow: current size 30000000 bytes.
Use `+RTS -Ksize' to increase it.

I think I'm doing something wrong with the definition of colorR.

Can anyone explain me what's wrong?

import GHC.Word
import Data.Word
import System.Random
import System.Random.Mersenne.Pure64

type RGB = (Int,Int,Int)

instance Bounded RGB where
  minBound = minRGB
  maxBound = maxRGB

minRGB = (0  ,0  ,0  )
maxRGB = (255,255,255)

instance Random RGB where
  random  = color2
  randomR = colorR

color2 :: RandomGen g ⇒ g → (RGB,g)
color2 = colorR (minRGB,maxRGB)

color :: RandomGen g ⇒ g → (RGB,g)
color s0 = ((r,g,b),s3)
  where
    (r,s1) = q s0
    (g,s2) = q s1
    (b,s3) = q s2
    q = randomR (0,255)

colorR :: RandomGen g ⇒ (RGB,RGB) → g → (RGB,g)
colorR ((a,b,c),(x,y,z)) s0 = ((r,g,b),s3)
  where
    (r,s1) = q (a,x) s0
    (g,s2) = q (b,y) s1
    (b,s3) = q (c,z) s2
    q = randomR

main :: IO ()
main = do
  mt ← newPureMT
  let cs = randoms mt :: [RGB]
  print cs

------------------------------------------

This one also just works fine:

import Data.Word
import System.Random.Mersenne

type RGB = (Word8,Word8,Word8)

instance MTRandom RGB where
  random m = do
    r ← random m :: IO Word8
    g ← random m :: IO Word8
    b ← random m :: IO Word8
    return (r,g,b)

main :: IO ()
main = do
  g  ← newMTGen Nothing
  cs ← randoms g :: IO [RGB]
  print cs

but I really need the range constraints colorR might provide me and would
greatly appreciate any help to understand/solve the issue.

Best Regards,
Cetin

P/s:
uname -a
Linux linux-d312 2.6.25.16-0.1-default #1 SMP 2008-08-21 00:34:25 +0200
x86_64 x86_64 x86_64 GNU/Linux
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080913/b6271a65/attachment.htm


More information about the Haskell-Cafe mailing list