Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basvandijk/6032630 to your computer and use it in GitHub Desktop.
Save basvandijk/6032630 to your computer and use it in GitHub Desktop.
From 44ddcb44e675fa0979c94efe5364b4c5fc83fab0 Mon Sep 17 00:00:00 2001
From: Bas van Dijk <v.dijk.bas@gmail.com>
Date: Fri, 19 Apr 2013 21:04:35 +0200
Subject: [PATCH 1/2] Fixed a stack-overflow in randomRs The following
generated a stack-overflow exception:
g <- getStdGen
randomRs (0,10::Int) g !! 1000000
Because randomRs generated a list without forcing the generator. This
caused the generator to become a large chain of thunks which when
eventually forced caused the stack to overflow.
Thanks to Utkarsh Upadhyay and Thomas Schilling for assisting me in
finding and fixing this bug.
---
System/Random.hs | 4 +++-
random.cabal | 4 +---
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/System/Random.hs b/System/Random.hs
index 9a970c4..f2e8d25 100644
--- a/System/Random.hs
+++ b/System/Random.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE BangPatterns #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
@@ -280,7 +282,7 @@ class Random a where
-- | Plural variant of 'randomR', producing an infinite list of
-- random values instead of returning a new generator.
randomRs :: RandomGen g => (a,a) -> g -> [a]
- randomRs ival g = x : randomRs ival g' where (x,g') = randomR ival g
+ randomRs ival !g = case randomR ival g of (x, g') -> x : randomRs ival g'
-- | Plural variant of 'random', producing an infinite list of
-- random values instead of returning a new generator.
diff --git a/random.cabal b/random.cabal
index 9c7b63f..66b9688 100644
--- a/random.cabal
+++ b/random.cabal
@@ -22,11 +22,9 @@ Cabal-Version: >= 1.6
Library
exposed-modules:
System.Random
- extensions: CPP
- GHC-Options: -O2
+ GHC-Options: -O2
build-depends: base >= 3 && < 5, time
source-repository head
type: git
location: http://darcs.haskell.org/packages/random.git/
-
--
1.7.10.4
From 623cd94f69cbb1bcf0d578e6f8e3ee419b0f8e2b Mon Sep 17 00:00:00 2001
From: Bas van Dijk <v.dijk.bas@gmail.com>
Date: Fri, 19 Apr 2013 21:15:22 +0200
Subject: [PATCH 2/2] UNPACK the Int32 fields into the StdGen constructor
---
System/Random.hs | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/System/Random.hs b/System/Random.hs
index f2e8d25..a4d7f2c 100644
--- a/System/Random.hs
+++ b/System/Random.hs
@@ -190,8 +190,9 @@ instance of 'StdGen' has the following properties:
-}
-data StdGen
- = StdGen Int32 Int32
+data StdGen
+ = StdGen {-# UNPACK #-} !Int32
+ {-# UNPACK #-} !Int32
instance RandomGen StdGen where
next = stdNext
--
1.7.10.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment