1

There is a post on the official Hedgehog site detailing how to write a Hedgehog generator that uses IO. The relevant code inside it is:

    import           Data.Text (Text)
    import qualified Data.Text as T
    import qualified Data.Text.IO as T

    import           Hedgehog
    import qualified Hedgehog.Gen as Gen
    import qualified Hedgehog.Range as Range

    genWord :: MonadIO m => GenT m Text
    genWord = do
      ws <- T.lines <$> liftIO (T.readFile "/usr/share/dict/words")
      Gen.element ws

...followed by Gen.sample genWord to actually invoke the generator to create some Text values.

The code syntax checks fine. However, when I attempt to run it, I am told:

• Could not deduce (MonadIO Data.Functor.Identity.Identity)
    arising from a use of ‘genWord’
  from the context: MonadIO m
    bound by the inferred type of it :: MonadIO m => m Text
    at <interactive>:62:1-27

I figure that the error happens because the example is just from an old version of the library or GHC, but I'm at a loss as to how to track down the actual problem and fix it. I haven't been able to find an analogous example in the Hedgehog source itself, or elsewhere.

I'd love a solution (of course) but even more some hints about how to go about solving this sort of problem in general.

Jeremy
  • 1,049
  • 1
  • 15
  • 28
  • I think this is a copy and paste error in your GHCi session. Make sure that `genWord` is well aligned with its type declaration – Eric Aug 14 '19 at 14:38
  • 1
    `Gen.sample` has type `Gen a -> IO a`, i.e., `GenT Identity a -> IO a`, so it cannot accept `genWord` as an argument, because of that constraint `MonadIO Identity`. This change seems to have happened in hedgehog-0.4, and otherwise I can't see a way to use a `GenT m` that doesn't use `Identity`... You might want to ask this on the issue tracker (or weigh in on https://github.com/hedgehogqa/haskell-hedgehog/issues/312) – Li-yao Xia Aug 14 '19 at 16:13

0 Answers0