Skip to content

Commit

Permalink
Clean up code a little
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Sep 5, 2024
1 parent 8ac5ac8 commit 1618320
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ instance IsTest QCWithSeed where
notice normal $ "Using --quickcheck-replay=" ++ show replay
run (setOption (QuickCheckReplayLegacy replay) options) test progress

-- | Typeclass for doing arbitrary (but law-abiding) comparisons. See also
-- 'ArbitraryOrd', this is the version that works with 'GHC.Generics'.
class GArbitraryOrd f where
garbitraryCompare :: QC.Gen (f p -> f p -> Ordering)

Expand Down Expand Up @@ -83,20 +85,19 @@ instance GArbitraryOrd f => GArbitraryOrd (M1 i t f) where
instance ArbitraryOrd c => GArbitraryOrd (K1 i c) where
garbitraryCompare = (\c (K1 l) (K1 r) -> c l r) <$> arbitraryCompare

-- | Typeclass for doing arbitrary (but law-abiding) comparisons.
class ArbitraryOrd a where
arbitraryCompare :: QC.Gen (a -> a -> Ordering)
default arbitraryCompare
:: (Generic a, GArbitraryOrd (Rep a)) => QC.Gen (a -> a -> Ordering)
arbitraryCompare = (\c l r -> c (from l) (from r)) <$> garbitraryCompare

instance ArbitraryOrd Int where
arbitraryCompare = do
bias <- QC.arbitrary
pure $ \l r -> if bias then compare l r else compare r l

instance ArbitraryOrd Char where
arbitraryCompare = arbitraryCompareReverseSection

-- | Construct an arbitrary comparison by (conceptually) laying out all values
-- in a list, picking two values (since we are using arbitrary these should
-- be "good" values), and then reversing the section between these two values.
arbitraryCompareReverseSection
:: (QC.Arbitrary a, Ord a) => QC.Gen (a -> a -> Ordering)
arbitraryCompareReverseSection = do
Expand Down

0 comments on commit 1618320

Please sign in to comment.