module UsingQuickCheck where import Test.QuickCheck -- -- 1 -- half :: (Eq a, Fractional a) => a -> a half x = x / 2 halfIdentity :: (Eq a, Fractional a) => a -> a halfIdentity = (*2) . half -- -- 2 -- -- for any list you apply sort to -- this property should hold listOrdered :: (Ord a) => [a] -> Bool listOrdered xs = snd $ foldr go (Nothing, True) xs where go _ status@(_, False) = status go y (Nothing, t) = (Just y, t) go y (Just x, _) = (Just y, x >= y) -- -- 3 -- plusAssociative :: (Ord a, Integral a) => a -> a -> a -> Bool plusAssociative x y z = x + (y + z) == (x + y) + z plusCommutative :: (Ord a, Integral a) => a -> a -> Bool plusCommutative x y = x + y == y + x -- -- 4 -- mulAssociative :: (Ord a, Integral a) => a -> a -> a -> Bool mulAssociative x y z = x * (y * z) == (x * y) * z mulCommutative :: (Ord a, Integral a) => a -> a -> Bool mulCommutative x y = x * y == y * x data Fool = Fulse | Frue deriving (Eq, Show) data Fool' = Fulse' -- 2/3 | Frue' -- 1/3 deriving (Eq, Show) instance Arbitrary Fool where arbitrary = oneof [ return Fulse , return Frue ] instance Arbitrary Fool' where arbitrary = frequency [ (3, return Fulse') , (1, return Frue')]