random colors, stack space overflow, mersenne and mersenne.pure64

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@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

cetin.sert:
random = color2 randomR = colorR
color2 :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color2 = colorR (minRGB,maxRGB)
color :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color s0 = ((r,g,b),s3) where
^^^^^^^^^^ There's some corruption in this text. Could you post the file somewhere? -- Don

Oh, hi *^o^*
mersenne.pure64
http://sert.homedns.org/lab/colors/var2.hs
mersenne
http://sert.homedns.org/lab/colors/var.hs
the problem seems to be with the definition of colorR in var2.hs
CS
2008/9/13 Don Stewart
cetin.sert:
random = color2 randomR = colorR
color2 :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color2 = colorR (minRGB,maxRGB)
color :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color s0 = ((r,g,b),s3) where
^^^^^^^^^^
There's some corruption in this text. Could you post the file somewhere?
-- Don

Oh, you've got unicode source. What's the flag to get this to actually compile? (Note to readers, using extensions , you should always include the LANGUAGE pragmas required to build the file when asking for help :) How are you compiling this? -- Don cetin.sert:
Oh, hi *^o^*
mersenne.pure64 [1]http://sert.homedns.org/lab/colors/var2.hs
mersenne [2]http://sert.homedns.org/lab/colors/var.hs
the problem seems to be with the definition of colorR in var2.hs
CS
2008/9/13 Don Stewart <[3]dons@galois.com>
cetin.sert: > random = color2 > randomR = colorR > > color2 :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) > color2 = colorR (minRGB,maxRGB) > > color :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) > color s0 = ((r,g,b),s3) > where
^^^^^^^^^^
There's some corruption in this text. Could you post the file somewhere? -- Don
References
Visible links 1. http://sert.homedns.org/lab/colors/var2.hs 2. http://sert.homedns.org/lab/colors/var.hs 3. mailto:dons@galois.com

Oh, sorry... ^__^"
ghc -fglasgow-exts -O2 --make var
ghc -fglasgow-exts -O2 --make var2
I've not used pragmas in any source file o_O so far... should go fix that
sometime. Thanks for reminding...
Using ghc 6.8.3, mersenne-random-0.1.3 and mersenne-random-pure64-0.2.0.2.
(Cause I could not find out how to check things out with darcs from hackage
o__O google was not very helpful there so I used the latest tar packages.)
@don: sorry I sent my reply twice only to you and then to the list and
you... it's because of gmail... I'll be very careful not to repeat my
mistake.
2008/9/13 Don Stewart
Oh, you've got unicode source. What's the flag to get this to actually compile? (Note to readers, using extensions , you should always include the LANGUAGE pragmas required to build the file when asking for help :)
How are you compiling this?
-- Don
cetin.sert:
Oh, hi *^o^*
mersenne.pure64 [1]http://sert.homedns.org/lab/colors/var2.hs
mersenne [2]http://sert.homedns.org/lab/colors/var.hs
the problem seems to be with the definition of colorR in var2.hs
CS
2008/9/13 Don Stewart <[3]dons@galois.com>
cetin.sert: > random = color2 > randomR = colorR > > color2 :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) > color2 = colorR (minRGB,maxRGB) > > color :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) > color s0 = ((r,g,b),s3) > where
^^^^^^^^^^
There's some corruption in this text. Could you post the file somewhere? -- Don
References
Visible links 1. http://sert.homedns.org/lab/colors/var2.hs 2. http://sert.homedns.org/lab/colors/var.hs 3. mailto:dons@galois.com

On 2008 Sep 12, at 21:57, Don Stewart wrote:
cetin.sert:
random = color2 randomR = colorR
color2 :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color2 = colorR (minRGB,maxRGB)
color :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color s0 = ((r,g,b),s3) where
There's some corruption in this text. Could you post the file somewhere?
It's not corrupt; he used UTF-8 symbols. It shows up properly in Mail.app but doesn't quote right (admittedly I force plaintext). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

main :: IO ()
main = do
as <- getArgs
mt <- newPureMT
let colors = randomRs (lo,hi) mt :: [RGB]
print $ zip tx cs
where
lo = read $ as !! 0
hi = read $ as !! 1
tx = as !! 2
Why is as not visible in the where block?
2008/9/13 Brandon S. Allbery KF8NH
On 2008 Sep 12, at 21:57, Don Stewart wrote:
cetin.sert:
random = color2 randomR = colorR
color2 :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color2 = colorR (minRGB,maxRGB)
color :: RandomGen g .$B"M.(B g .$B"*.(B (RGB,g) color s0 = ((r,g,b),s3) where
There's some corruption in this text. Could you post the file somewhere?
It's not corrupt; he used UTF-8 symbols. It shows up properly in Mail.app but doesn't quote right (admittedly I force plaintext).
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

cetin.sert:
main :: IO () main = do as <- getArgs mt <- newPureMT let colors = randomRs (lo,hi) mt :: [RGB] print $ zip tx cs where lo = read $ as !! 0 hi = read $ as !! 1 tx = as !! 2 Why is as not visible in the where block?
Same as: main = let lo = read $ as !! 0 hi = read $ as !! 1 tx = as !! 2 in do as <- getArgs mt <- newPureMT let colors = randomRs (lo,hi) mt :: [RGB] print $ zip tx cs If that helps think about when things are bound. -- Don

On Fri, Sep 12, 2008 at 8:53 PM, Don Stewart
cetin.sert:
main :: IO () main = do as <- getArgs mt <- newPureMT let colors = randomRs (lo,hi) mt :: [RGB] print $ zip tx cs where lo = read $ as !! 0 hi = read $ as !! 1 tx = as !! 2 Why is as not visible in the where block?
Same as:
main = let lo = read $ as !! 0 hi = read $ as !! 1 tx = as !! 2 in do as <- getArgs mt <- newPureMT let colors = randomRs (lo,hi) mt :: [RGB] print $ zip tx cs
If that helps think about when things are bound.
And you probably want this rewrite (untested): main :: IO () main = do as <- getArgs let lo = read $ as !! 0 hi = read $ as !! 1 tx = as !! 2 mt <- newPureMT let colors = randomRs (lo,hi) mt :: [RGB] print $ zip tx cs My indentation may be a bit off as I didn't use a fixed width font to type it. Jason

Cetin Sert wrote:
main = do as <- getArgs [...] where lo = read $ as !! 0 hi = read $ as !! 1 tx = as !! 2
Why is as not visible in the where block?
The where-block is outside the do-block. (Indentation cannot change that fact.)

Cetin Sert wrote: [snip]
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
Look closely at how you use the variable 'b'. HTH, Bertram

bertram.felgenhauer:
Cetin Sert wrote: [snip]
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
Look closely at how you use the variable 'b'.
:-) fast eyes. Btw, Cetin, this is good practice, along with -funbox-strict-fields: data RGB = RGB !Int !Int !Int deriving Show Much better code than using a lazy triple.

Oh thank you both *^o^*... now works like a charm again.
Btw, Cetin, this is good practice, along with -funbox-strict-fields:
data RGB = RGB !Int !Int !Int
deriving Show
Much better code than using a lazy triple.
Where can I read more of such good practice? Looking forward to Real World
Haskell to read lots of code, hope it'll help lots of people interested in
learning haskell. (Will be released around Christmas here in Germany, I
think.)
CS
2008/9/13 Don Stewart
bertram.felgenhauer:
Cetin Sert wrote: [snip]
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
Look closely at how you use the variable 'b'.
:-) fast eyes.
Btw, Cetin, this is good practice, along with -funbox-strict-fields:
data RGB = RGB !Int !Int !Int deriving Show
Much better code than using a lazy triple. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

cetin.sert:
Where can I read more of such good practice? Looking forward to Real World Haskell to read lots of code, hope it'll help lots of people interested in learning haskell. (Will be released around Christmas here in Germany, I think.)
Yeah, that's right. November some time. There's a chapter that talks about performance and data structures, http://book.realworldhaskell.org/read/profiling-and-optimization.html
participants (6)
-
Albert Y. C. Lai
-
Bertram Felgenhauer
-
Brandon S. Allbery KF8NH
-
Cetin Sert
-
Don Stewart
-
Jason Dagit