1

Please define:

instance Arbitrary StdGen where
  arbitrary = undefined
  shrink    = undefined

Why is this not defined in the standard library? Are there some pitfalls I should avoid, is it even possible?

xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
  • What would you want your arbitrary random generator to be? Would `mkStdGen 0` or something like that, with a known seed, work for you? – 9000 Dec 04 '15 at 21:40
  • I don't think mkStdGen 0 would be sufficiently random ... – xiaolingxiao Dec 04 '15 at 21:42
  • 2
    Then how about `instance Arbitrary StdGen where arbitrary = do seed <- arbitrary; return $ mkStdGen seed`? – 9000 Dec 04 '15 at 22:09
  • That works, what about shrink? stdGen not exactly a num ... – xiaolingxiao Dec 04 '15 at 22:20
  • 1
    The default `shrink`, i.e. `const []`, should be OK, I guess. So it's just `arbitrary = mkStdGen <$> arbitrary`. – chi Dec 04 '15 at 22:37
  • 2
    What would be the purpose of having an arbitrary `StdGen`? – Rein Henrichs Dec 04 '15 at 23:28
  • @ReinHenrichs good question, I'm sure if I'm using it the right way. But I like to quickCheck a function that takes in a StdGen as a parameter, I could just explicitly pass in some seed :: StdGen. Does that make more sense than generating an arbitrary instance of it? – xiaolingxiao Dec 05 '15 at 02:24
  • The issue with having an instance n the "standard library" is that there is no such library. If the instance goes in the random package then that package now must depend on quick check. If the instances went in quick check the dep goes the other way. Or you can make an orpaned instance in another library, but those are frowned upon. – Thomas M. DuBuisson Dec 05 '15 at 17:43

0 Answers0