Skip to content

Commit c650b93

Browse files
committed
Redo 14.6 and write some instances of the Arbitrary typeclass
1 parent a93259a commit c650b93

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

ch14/qc/CoArbitrary.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
3+
module CoArbitrary where
4+
5+
import GHC.Generics
6+
import Test.QuickCheck
7+
8+
data Bool' = True' | False' deriving (Generic)
9+
10+
instance CoArbitrary Bool'
11+
12+
-- coarbitrary :: CoArbitrary a => a -> Gen b -> Gen b
13+
14+
trueGen :: Gen Int
15+
trueGen = coarbitrary True' arbitrary
16+
17+
falseGen :: Gen Int
18+
falseGen = coarbitrary False' arbitrary

ch14/qc/First.hs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

ch14/qc/qc.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ cabal-version: >=1.10
1414

1515
library
1616
hs-source-dirs: .
17-
exposed-modules: Examples
17+
exposed-modules: CoArbitrary
18+
, Examples
19+
, First
1820
ghc-options: -Wall -fwarn-tabs
1921
default-language: Haskell2010
2022
build-depends: base >= 4.7 && < 5

0 commit comments

Comments
 (0)