From 6fcb4f553176385af8180df08511d5cd1143734c Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Sun, 27 Oct 2024 10:13:48 -0600 Subject: [PATCH] Add `Uniform` instance for `Maybe` and `Either` --- CHANGELOG.md | 1 + src/System/Random/Internal.hs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 122ed4ce..bf876a2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 1.3.0 +* Add `Uniform` instance for `Maybe` and `Either` * Add `SplitGen` and `splitGen` * Add `shuffleList` and `shuffleListM`: [#140](https://github.com/haskell/random/pull/140) * Add `mkStdGen64`: [#155](https://github.com/haskell/random/pull/155) diff --git a/src/System/Random/Internal.hs b/src/System/Random/Internal.hs index a193f438..f6bba9e2 100644 --- a/src/System/Random/Internal.hs +++ b/src/System/Random/Internal.hs @@ -109,7 +109,7 @@ import GHC.ST (ST(..)) import GHC.Word import Numeric.Natural (Natural) import System.IO.Unsafe (unsafePerformIO) -import System.Random.GFinite (Cardinality(..), GFinite(..)) +import System.Random.GFinite (Cardinality(..), GFinite(..), Finite) import qualified System.Random.SplitMix as SM import qualified System.Random.SplitMix32 as SM32 import Data.Kind @@ -1474,6 +1474,10 @@ instance UniformRange Bool where {-# INLINE uniformRM #-} isInRange = isInRangeOrd +instance (Finite a, Uniform a) => Uniform (Maybe a) + +instance (Finite a, Uniform a, Finite b, Uniform b) => Uniform (Either a b) + -- | See [Floating point number caveats](System-Random-Stateful.html#fpcaveats). instance UniformRange Double where uniformRM (l, h) g