haskell-numerics / random-fu

A suite of Haskell libraries for representing, manipulating, and sampling random variables
42 stars 21 forks source link

RandomSource lift instances through common monad transformers #42

Closed flip111 closed 2 years ago

flip111 commented 6 years ago

I like to see instance (MonadTrans t, Monad m) => RandomSource (t m) s to be made available. So i can use ReaderT together with RandomSource. Right now RandomSource IO (Gen RealWorld) is available but when i try to use RandomSource (ReaderT Env IO) (Gen RealWorld) it doesn't work.

Doesn't work = No instance for (RandomSource (ReaderT Env IO) (Gen RealWorld)) arising from a use of blabla

idontgetoutmuch commented 5 years ago

I am up to my eyeballs atm - if you can send a PR that would be great otherwise it will take me some time to looking at this.

flip111 commented 5 years ago

I guess this is the code for ReaderT

import Data.Random.Internal.Source (getRandomPrimFrom)
import Data.Random.Source (randomSource)

$(randomSource [d|
  instance RandomSource m s => RandomSource (ReaderT e m) s where
    getRandomPrimFrom a b = lift (getRandomPrimFrom a b)
  |])

Some questions:

  1. should the code use TH or not? (it's advised for user code, but what about library code)
  2. I read on this page that RandomSource has already quite some instances. I can't find these instances, where are they?
  3. where should the additional instance be placed?
idontgetoutmuch commented 3 years ago

I think this should do what you want: https://github.com/haskell-numerics/random-fu/pull/67. Let me know and we can close this. Thanks and sorry for taking so long.