Proposal: Generalize the RandomGen and Random classes
Milan Straka
fox at ucw.cz
Tue Sep 14 23:26:53 EDT 2010
Hi,
as far as I know, a random generator usually creates an int or
a sequence of bits, right? Or do you have some specific random
generators in mind?
I personally do not think it is worth to modify the Random module, which
dates back to at least Haskell 98. Maybe a new package on the Hackage?
Cheers,
Milan
> Hello,
> RandomGen and Random classes assume generators produce Int values.
> This is non-ideal as many high speed generators produce special values
> (ex: doubles) or generic values (bit streams / bytestrings) that can
> be converted directly to types easier than coercing to Int then to an
> 'a' via the Random class.
>
> See 4315 [1] for the patch.
>
> Period of discussion: Till October 8 (3.5 weeks, giving a little time
> after ICFP for last minute debate)
>
> Specifically, this proposal:
>
> 1) Alters RandomGen:
>
> from:
> class RandomGen g where
> next :: g -> (Int, g)
> genRange :: g -> (Int, Int)
>
> to
>
> class RandomGen g v | g -> v where
> next :: g -> (v, g)
> genRange :: g-> (v,v)
>
> 2) Alters Random:
>
> From:
>
> class Random a where
> randomR :: RandomGen g => (a,a) -> g -> (a,g)
> random :: RandomGen g => g -> (a, g)
> randomRs :: RandomGen g => (a,a) -> g -> [a]
> randoms :: RandomGen g => g -> [a]
> randomRIO :: (a,a) -> IO a
> randomIO :: IO a
>
> to
>
> class Random a where
> randomR :: RandomGen g v => (a,a) -> g -> (a,g)
> random :: RandomGen g v => g -> (a, g)
> randomRs :: RandomGen g v => (a,a) -> g -> [a]
> randoms :: RandomGen g v => g -> [a]
>
>
> Additional Points of Debate
> 1) Because random[R]IO can not be part of the new Random instance with
> a sensible default, these have been moved to top-level functions:
>
> randomRIO :: (Random a Int) => (a,a) -> IO a
> randomIO :: (Random a Int) => IO a
>
> Other options exist and I'm open to them. I'm just being upfront
> about what the patch currently does.
>
> 2) All pre-existing instances of "Random x" for some concrete 'x' have
> been modified to be "instance Random x Int". As 'Int' was the
> previous (hardcoded) default for RandomGen this is simply matching the
> behavior. More instances are possible and probably make sense now.
> Alternatively, one could argue for zero default instance as they can
> collide with how a particular user wishes types to be coerced.
>
> 3) Not-so-new extensions are used to enable these changes. Extensions
> include MultiParamTypeClasses, FlexibleContexts, and FunDeps.
>
> 4) A patch is included bumping the version from 1.0.0.x to 1.1.0.0.
>
> Cheers,
> Thomas
>
> [1] http://hackage.haskell.org/trac/ghc/ticket/4315
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>
More information about the Libraries
mailing list