Closed mgajda closed 10 years ago
Unfortunately, because of the way type-class instances work in (all present implementations of) Haskell, instance Enum a => Distrubution Uniform a
is a potentially dangerous instance, because it overlaps and creates ambiguity with every other possible instance Distrubution Uniform t
in ways that can cause weird behavior - especially for types such as Double where the Enum behavior is wildly different from the expected behavior of a uniform distribution. It becomes a bit of "luck of the draw" (unless you know specifically how instances are resolved, in which case it's just "annoyingly inconsistent") whether you end up getting the generic Enum-based instance or the specialized Double instance. This general issue is why I've exposed all the different utility functions to sample uniform values based on class constraints. There's an enumUniform
function which for some reason isn't exported from Data.Random.Distribution.Uniform
. I'll fix that.
randomChoice
is a useful function, and (if I'm reading correctly) is actually equivalent to a probably-poorly-named function already in random-fu: Data.Random.Distribution.Bernoulli.generalBernoulli
A vector-based randomElement
would also be a good thing to include. I'll go ahead and add that.
Hi,
I really like the way
RVar
monad works, and I have found myself making reusable code for some common use cases:Uniform
instance forEnum
types, that generalizes most ofUniform
instances for types that already haveEnum
.randomElement
is more practical withV.Vector
than with lists, for efficiency reasons.RVar
s, I addedrandomChoice
that works asif then else
operator, except that it takes ratio of probabilities of its alternatives instead ofBool
.Please let me know, if you need any corrections in order to include it in the upstream, or have any issues with it.