haskell-numerics / random-fu

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

Confusion with sample types #90

Closed samuelrivas closed 1 year ago

samuelrivas commented 1 year ago

I am trying to update some old code that doesn't compile with the latest version of random fu, but I am a bit confused about how is sample working now.

From the docs and the code I see that its type is so I should be able to sample an RVar having a stateful generator

(Random.Sampleable d m t, Random.StatefulGen g m,
   MonadReader g m) =>
  d t -> m t
        -- Defined in `Data.Random.Sample'

Which is what :i also returns in ghci. However :t returns a different type, which makes sampling an RVar a bit more unclear:

> :i Random.sample 
Random.sample ::
  (Random.Sampleable d m t, Random.StatefulGen g m,
   MonadReader g m) =>
  d t -> m t
        -- Defined in `Data.Random.Sample'
> :t Random.sample
Random.sample
  :: (Random.Distribution d t, Random.StatefulGen g m,
      MonadReader g m) =>
     d t -> m t

Can someone explain what is going on here?

idontgetoutmuch commented 1 year ago

Just in case you need an example, I put one here: https://github.com/haskell-numerics/random-fu/issues/83#issuecomment-1114591775

idontgetoutmuch commented 1 year ago

I think the difference between what :i returns and :t returns is a question for ghc.

samuelrivas commented 1 year ago

Great, thanks. I'll take a look.

samuelrivas commented 1 year ago

For the record, I got an answer for the difference between :t and :i. In short, :t runs type inference, and simplifies Sampleable out with the instance for Distribution, :i on the other hand reports the type that is written in the source code, without running inference on it.