Skip to content

Commit 46a15af

Browse files
authored
Merge pull request #78 from Bodigrim/is-in-range
2 parents f11fefe + 781e516 commit 46a15af

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

src/System/Random/Internal.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,46 @@ class UniformRange a where
597597
--
598598
-- > uniformRM (a, b) = uniformRM (b, a)
599599
--
600+
-- The range is understood as defined by means of 'isInRange', so
601+
--
602+
-- > isInRange (a, b) <$> uniformRM (a, b) gen == pure True
603+
--
604+
-- but beware of
605+
-- [floating point number caveats](System-Random-Stateful.html#fpcaveats).
606+
--
600607
-- @since 1.2.0
601608
uniformRM :: StatefulGen g m => (a, a) -> g -> m a
602609

610+
-- | A notion of (inclusive) ranges prescribed to @a@.
611+
--
612+
-- Ranges are symmetric:
613+
--
614+
-- > isInRange (lo, hi) x == isInRange (hi, lo) x
615+
--
616+
-- Ranges include their endpoints:
617+
--
618+
-- > isInRange (lo, hi) lo == True
619+
--
620+
-- When endpoints coincide, there is nothing else:
621+
--
622+
-- > isInRange (x, x) y == x == y
623+
--
624+
-- Endpoints are endpoints:
625+
--
626+
-- > isInRange (lo, hi) x ==>
627+
-- > isInRange (lo, x) hi == x == hi
628+
--
629+
-- Ranges are transitive relations:
630+
--
631+
-- > isInRange (lo, hi) lo' && isInRange (lo, hi) hi' && isInRange (lo', hi') x
632+
-- > ==> isInRange (lo, hi) x
633+
--
634+
-- @since 1.3.0
635+
isInRange :: (a, a) -> a -> Bool
636+
637+
default isInRange :: Ord a => (a, a) -> a -> Bool
638+
isInRange (a, b) x = min a b <= x && x <= max a b
639+
603640
instance UniformRange Integer where
604641
uniformRM = uniformIntegralM
605642

0 commit comments

Comments
 (0)