I’ve been trying to extract a random element from a sequence. The function I’m trying to define is below:
getRandomInts :: Seq Int -> PureMT -> ((Seq Int, Int), PureMT)
getRandomInts ints gen = sampleState (fromJust $ choiceExtractSeq ints) gen
This is the error I get:
Overlapping instances for Data.Random.Lift.Lift
transformers-0.3.0.0:Data.Functor.Identity.Identity
(StateT PureMT transformers-0.3.0.0:Data.Functor.Identity.Identity)
arising from a use of `sampleState'
Matching instances:
instance [incoherent] (Monad m, MonadTrans t) =>
Data.Random.Lift.Lift m (t m)
-- Defined in `Data.Random.Lift'
instance [incoherent] Monad m =>
Data.Random.Lift.Lift
transformers-0.3.0.0:Data.Functor.Identity.Identity m
-- Defined in `Data.Random.Lift'
In the expression:
sampleState (fromJust $ choiceExtractSeq ints) gen
In an equation for `getRandomInt':
getRandomInt ints gen
= sampleState (fromJust $ choiceExtractSeq ints) gen
Does anyone know how to fix this, or, alternatively, have a suggestion for an efficient way to take a random element (without replacement) from a sequence?
I had a similar problem with this program:
This results in almost the same error message. The problem here is that there are overlapping instances in
Data.Random.Lift, but there is no most specific instance so ghc rejects the program (which is described in the user’s guide).The way I fixed this was to provide an instance that’s more specific than the others, so simply dropping the following instance makes everything compile fine for me (I’ve included full language pragmas and imports):
In short, try guiding the compiler by writing a more specific instance for
Data.Random.Lift.Lift.Update
I’ve created a pull request to the random-fu author with a patch that fixes this up in the library. Until that’s accepted, you can pull from my fork, and use
cabal installto have this work for you.