-- Lecture 3B import Sample -- to observe QuickCheck random generators import Test.QuickCheck import Data.List(nub) doTwice i = do a <- i b <- i return (a,b) oneR :: Gen Int oneR = return 1 natural :: Gen Int natural = do i <- arbitrary :: Gen Int return (abs i) digit = oneof [return 1, return 100] --------------------------------------- data Suit = Spades | Hearts | Diamonds | Clubs deriving (Show,Eq) data Rank = Numeric Integer | Jack | Queen | King | Ace deriving (Show,Eq) data Card = Card Rank Suit deriving (Show,Eq) data Hand = Empty | Add Card Hand deriving (Show,Eq) --------------------------------------- rSuit :: Gen Suit rSuit = oneof [return s | s <- [Spades,Hearts, Diamonds,Clubs] ] rRank :: Gen Rank rRank = frequency [(4,rRoyal),(9,rNumeric)] where -- oneof [rRoyal,rNumeric] where rRoyal = oneof [return r | r <- [Jack, Queen, King, Ace] ] rNumeric = do n <- choose (2,10) return (Numeric n) {- rRank2 = oneof [return Jack, return ..., return (Numeric 2), return (Numeric 3)... -} rCard = do r <- rRank s <- rSuit return (Card r s) rHand = frequency [ (1, return Empty), (4, do c <- rCard h <- rHand return (Add c h)) ] {- rHand = oneof [ return Empty, do c <- rCard h <- rHand return (Add c h) ] -} validRank :: Rank -> Bool validRank (Numeric r) = r `elem` [2..10] validRank _ = True instance Arbitrary Rank where arbitrary = rRank coarbitrary = undefined instance Arbitrary Suit where arbitrary = rSuit coarbitrary = undefined instance Arbitrary Card where arbitrary = rCard coarbitrary = undefined instance Arbitrary Hand where arbitrary = rHand' coarbitrary = undefined prop_Rank r = collect r (validRank r) numCards :: Hand -> Integer numCards Empty = 0 numCards (Add _ h) = 1 + numCards h prop_Hand h = collect (numCards h) (l == nub l) where l = toList h toList :: Hand -> [Card] toList Empty = [] toList (Add c h) = c : toList h rHand' = do cardlist <- arbitrary :: Gen [Card] return (toHand (nub cardlist)) where toHand [] = Empty toHand (c:cs) = Add c (toHand cs)