Hedgehog generator using IO

208 Views Asked by At

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.

0

There are 0 best solutions below