samedi 22 août 2020

making an Arbitrary instance for a complicated type

I'm trying to check my code using QuickCheck so I need to make Arbitrary instances for my types. One type data Schedule is complicated, and I don't know how to treat it properly. Three instances (for Date, Activity, TimeStart) work fine. But the last instance (for Schedule) is incorrect, and I seek for help with fixing it.

data Schedule = Schedule [(Date,[Activity])]
data Date = Date Year Month Day
data Activity = Activity (ActivityName,TimeStart)
data TimeStart = TimeStart Hour Minute

type Year = Integer
type Month = Integer
type Day = Integer
type ActivityName = String
type Hour = Integer
type Minute = Integer

instance Arbitrary Date where
  arbitrary = do
    year <- choose (2000, 2050)
    month <- choose (1, 12)
    day <- choose (1, 28)
    return $ Date year month day

instance Arbitrary Activity where
  arbitrary = do
    size <- choose (5, 15 :: Integer)
    activityName <- sequence [elements ['a'..'z'] | _ <- [1..size]]
    timeStart <- arbitrary
    return $ Activity (activityName, timeStart)

instance Arbitrary TimeStart where
  arbitrary = do
    hour <- choose (1, 24)
    minute <- choose (1, 60)
    return $ TimeStart hour minute

instance Arbitrary Schedule where
  arbitrary = do
    size <- choose (5, 50 :: Integer)
    schedule <- sequence [(arbitrary, arbitraryActivity) | _ <- [1..size]]
    return $ Schedule schedule where
      arbitraryActivity = do
        size <- choose (5, 50 :: Integer)
        activityName <- sequence [arbitrary | _ <- [1..size]]
        return $ ActivityName activityname

First error is in this line: schedule <- sequence [(arbitrary, arbitraryActivity) | _ <- [1..size]] Expected type: Gen [Gen b0] Actual type: (Gen a0, [Gen b0]) But how to make an arbitrary for such a construction [(x,y)] properly?

Second error is in the last line: return $ ActivityName activityname Data constructor not in scope: ActivityName Is it not allowed to use where in do-notation the way I did?

Aucun commentaire:

Enregistrer un commentaire