|
| 1 | +module First where |
| 2 | + |
| 3 | +-- 14.6 Kicking around QuickCheck |
| 4 | + |
| 5 | +import Test.QuickCheck |
| 6 | + |
| 7 | +data Trivial = Trivial deriving (Eq, Show) |
| 8 | + |
| 9 | +trivialGen :: Gen Trivial |
| 10 | +trivialGen = return Trivial |
| 11 | + |
| 12 | +instance Arbitrary Trivial where |
| 13 | + arbitrary = trivialGen |
| 14 | + |
| 15 | +data Identity a = Identity a deriving (Eq, Show) |
| 16 | + |
| 17 | +identityGen :: Arbitrary a => Gen (Identity a) |
| 18 | +identityGen = arbitrary >>= return . Identity |
| 19 | +-- identityGen = do |
| 20 | +-- a <- arbitrary |
| 21 | +-- return (Identity a) |
| 22 | + |
| 23 | +instance Arbitrary a => Arbitrary (Identity a) where |
| 24 | + arbitrary = identityGen |
| 25 | + |
| 26 | +identityGenInt :: Gen (Identity Int) |
| 27 | +identityGenInt = identityGen |
| 28 | + |
| 29 | +data Pair a b = Pair a b deriving (Eq, Show) |
| 30 | + |
| 31 | +pairGen :: (Arbitrary a, Arbitrary b) => Gen (Pair a b) |
| 32 | +pairGen = do |
| 33 | + a <- arbitrary |
| 34 | + b <- arbitrary |
| 35 | + return (Pair a b) |
| 36 | + |
| 37 | +instance (Arbitrary a, Arbitrary b) => Arbitrary (Pair a b) where |
| 38 | + arbitrary = pairGen |
| 39 | + |
| 40 | +pairGenIntString :: Gen (Pair Int String) |
| 41 | +pairGenIntString = pairGen |
| 42 | + |
| 43 | +data Sum a b = First a | Second b deriving (Eq, Show) |
| 44 | + |
| 45 | +-- equal Odds |
| 46 | +sumGenEqual :: (Arbitrary a, Arbitrary b) => Gen (Sum a b) |
| 47 | +sumGenEqual = do |
| 48 | + a <- arbitrary |
| 49 | + b <- arbitrary |
| 50 | + oneof [ return $ First a |
| 51 | + , return $ Second b |
| 52 | + ] |
| 53 | + |
| 54 | +-- instance (Arbitrary a, Arbitrary b) => Arbitrary (Sum a b) where |
| 55 | +-- arbitrary = sumGenEqual |
| 56 | + |
| 57 | +sumGenCharInt :: Gen (Sum Char Int) |
| 58 | +sumGenCharInt = sumGenEqual |
| 59 | + |
| 60 | +-- from the QuickCheck library |
| 61 | +-- instance Arbitrary a => Arbitrary (Maybe a) where |
| 62 | +-- arbitrary = frequency [ (1, return Nothing) |
| 63 | +-- , (3, liftM Just arbitrary) |
| 64 | +-- ] |
| 65 | + |
| 66 | +sumGenFirstPls :: (Arbitrary a, Arbitrary b) => Gen (Sum a b) |
| 67 | +sumGenFirstPls = do |
| 68 | + a <- arbitrary |
| 69 | + b <- arbitrary |
| 70 | + frequency [ (10, return $ First a) |
| 71 | + , (1, return $ Second b) |
| 72 | + ] |
| 73 | + |
| 74 | +sumGenCharIntFirst :: Gen (Sum Char Int) |
| 75 | +sumGenCharIntFirst = sumGenFirstPls |
| 76 | + |
| 77 | +main :: IO () |
| 78 | +main = do |
| 79 | + sample trivialGen |
0 commit comments