1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 module QuickCheck.Utils where
3 import Control.Arrow (first)
5 import Data.Eq (Eq(..))
6 import Data.Function (($), (.))
7 import Data.Functor (Functor(..))
9 import Data.Ord (Ord(..))
10 import Data.Ratio ((%), Rational, numerator, denominator)
11 import Prelude (Num(..), Integral(..), Bounded(..))
12 import System.Random (Random(..))
14 import Test.QuickCheck
15 import Text.Show (Show(..))
17 import qualified Data.Set as Set
19 -- | Like 'nub', but O(n * log n).
20 nubList :: Ord a => [a] -> [a]
21 nubList = go Set.empty where
23 go s (x:xs) | x`Set.member`s = go s xs
24 | otherwise = x:go (Set.insert x s) xs
26 instance Random Rational where
27 random = randomR (toInteger (minBound::Int)%1, toInteger (maxBound::Int)%1)
28 randomR (minR, maxR) g =
30 then first (% b) $ randomR (a, c) g
31 else first (bd2ac . nat2bd) $ randomR (0, toInteger (maxBound::Int)) g
37 nat2bd x = ((d - b) % toInteger (maxBound::Int)) * (x%1) + (b%1)
38 bd2ac x = alpha * x + beta
41 beta = (a%1) - alpha * (b%1)
42 instance Arbitrary SchoolGrade where
43 arbitrary = arbitraryBoundedEnum
44 instance Arbitrary DanishSchoolGrade where
45 arbitrary = arbitraryBoundedEnum
47 -- * Type 'SameLength'
48 newtype SameLength a = SameLength a
50 instance Functor SameLength where
51 fmap f (SameLength x) = SameLength (f x)