Skip to content

Add Random instances for tuples #26

Description

@ndmitchell

Looking at the interface, I see no reason tuples can't be instances of the Random class, which would make it more useful.

Activity

  1. cartazio commented on Apr 23, 2015

    @cartazio
    Contributor

    That sounds super reasonable. I'll talk about it with Dominic when we next
    catchup.

    I assume we probably don't need more Than size 6 or 10 tuples? :)

    On Thursday, April 23, 2015, Neil Mitchell [email protected] wrote:

    Looking at the interface, I see no reason tuples can't be instances of the
    Random class, which would make it more useful.

    —
    Reply to this email directly or view it on GitHub
    #26.

  2. ndmitchell commented on Apr 23, 2015

    @ndmitchell
    Author

    My particular use case was for 4 tuples - specifically, I wanted to call seedTFGen with a randomly chosen seed. Agreed, 6 seems sufficient.

  3. added a commit that references this issue on Jan 3, 2017
    f9ccf90
  4. cartazio commented on Feb 25, 2018

    @cartazio
    Contributor

    @ndmitchell this is a good feature, i'll make it happen, see what i can do...

    how would you specify/what to talk about generating samples / intervals?

  5. cartazio commented on Feb 25, 2018

    @cartazio
    Contributor

    (i'm amidst revisiting / generalizing the sampler api, though that wont happen for the next release, i presume you want every tuple value, not an "interval/range"?

  6. ndmitchell commented on Feb 26, 2018

    @ndmitchell
    Author

    @cartazio I didn't really have any idea about what intervals should mean - but I guess between (a,b) and (c,d) should probably be equivalent to the first member in the range a-c and the second in b-d.

  7. samuelpilz commented on Apr 17, 2018

    @samuelpilz

    What is the status on this issue? Is there a chance that the pull-request will be merged and an update is published?

  8. cartazio commented on Apr 17, 2018

    @cartazio
    Contributor
  9. L-TChen commented on Jan 25, 2019

    @L-TChen

    Will this be happening soon? I am really looking forward to this feature. :-)

  10. 9 remaining items

  11. Bodigrim commented on Jun 24, 2020

    @Bodigrim
    Contributor

    @ndmitchell proposed above that "between (a,b) and (c,d) should probably be equivalent to the first member in the range a-c and the second in b-d". Under this interpretation we can define UniformRange, but I'm not quite convinced that this is a right and unambiguous thing to do. (That's basically what I meant to ask in the comment above)

  12. lehins commented on Jun 24, 2020

    @lehins
    Contributor

    I think we can define such instance if we treat a and b completely independent of each other. For example this could work:

    instance (UniformRange a, UniformRange b) => UniformRange (a, b) where
      uniformRM ((al, bl), (ah, bh)) g = do
        a <- uniformRM (al, ah) g
        b <- uniformRM (bl, bh) g
        pure (a, b)

    If we think of the tuples as some related value such complex numbers or something then it definitely doesn't make sense, but above instance (and similar one for Random) I believe could be pretty useful. @Shimuuar What's your take on this?

  13. Shimuuar commented on Jun 24, 2020

    @Shimuuar
    Contributor

    That would be easy to implement. And in some sense it is uniform. But it's wrong is we use interpretation: "sample uniformly every value between a and b". I think it's better to avoid defining weird instances from get-go

  14. lehins commented on Jun 24, 2020

    @lehins
    Contributor

    In that case, I think it would be ok to add such instance for Random, but not for UniformRange, since former already does not promise uniformity as with types like Float and Integer.

  15. Bodigrim commented on Jun 24, 2020

    @Bodigrim
    Contributor

    Defining more instances of Random than of UniformRange will motivate users to use the former and block its eventual deprecation. If we think that this is a handy instance, let's add it to UniformRange as well with a proper comment/clarification.

  16. lehins commented on Jun 24, 2020

    @lehins
    Contributor

    I certainly disagree with Random being deprecated, even over time! After over two decades of people using it, this would be a wrong move. Moreover, many programming languages have a concept like "Random", that produces some standard distribution that does not have a mathematical backing but is useful for programmers. Tuples here is a perfect example.

    Users can choose themselves which instance they want to use. One says Uniform and it promises the uniform distribution, another one is Random, which makes it open to interpretation. Many of the types do coincide and that is fine, IMHO.

  17. Bodigrim commented on Jun 24, 2020

    @Bodigrim
    Contributor

    The problem is that currently Random and randomR are not open to intepretation: they do claim uniformity.

    random/src/System/Random.hs

    Lines 189 to 190 in 11464aa

    -- | The class of types for which uniformly distributed values can be
    -- generated.

    random/src/System/Random.hs

    Lines 197 to 199 in 11464aa

    -- | Takes a range /(lo,hi)/ and a pseudo-random number generator
    -- /g/, and returns a pseudo-random value uniformly distributed over the
    -- closed interval /[lo,hi]/, together with a new generator. It is unspecified

    I do not particularly mind lifting this guarantee for Random, but I think that in future with a wider adoption of UniformRange people would ask for UniformRange for tuples as well. I would rather define either both, or none.

  18. ndmitchell commented on Jun 24, 2020

    @ndmitchell
    Author

    FWIW, I have no particular desire for a uniform range in 99%+ of cases. I've wanted random tuples a handful of times. I'm perfectly happy with Random.

  19. lehins commented on Jun 24, 2020

    @lehins
    Contributor

    @Bodigrim the whole point of the Uniform and UniformRange classes coming into existence was for them to be different from Random type class. So when "people do ask", I'll be happy to tell them such instance cannot exist, but for now let's not worry about what "people might ask".

    I am on the same page with @ndmitchell and @Shimuuar seems to support this idea as well.

    So, let's remove the "uniform" promise from Random class, because it is already a lie and add some tuple instances for Random, but not UniformRange.

  20. Bodigrim commented on Jun 24, 2020

    @Bodigrim
    Contributor

    If we communicate the difference between Random and Uniform clearly and unambiguously, then I'm on board as well.

  21. curiousleo commented on Jun 25, 2020

    @curiousleo
    Contributor

    @lehins wrote:

    add some tuple instances for Random, but not UniformRange.

    I'm on board with this conclusion.

    So, let's remove the "uniform" promise from Random class, because it is already a lie

    I don't understand this premise. Which Random instances do not produce uniform distributions?

  22. lehins commented on Jun 25, 2020

    @lehins
    Contributor

    Which Random instances do not produce uniform distributions?

    Integer, Float and Double. Calling random function for these types will produce values distributed uniformly in a subrange, instead of full range, because as you know full range is infinite

  23. lehins commented on Jun 29, 2020

    @lehins
    Contributor

    There is implementation for this ticket in #72 if anyone feels like giving it a review.

  24. lehins commented on Sep 19, 2021

    @lehins
    Contributor

    Finally this is implemented and is merged into master. These instances will be soon released with random-1.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions