Skip to content

Gen.elements only returns first or last element. #22

Closed

Description

@niklas

I ran into a problem with Gen.elements. No matter how often I use it with a Random Generator, it always gives me either the first or the last element of the Foldable1 f.

My suspicion is the internal helper fromIndex:

edge cases seem to work

fromIndex 0 $ 10 :| [20,30,40,50,60] -- 10
fromIndex 10 $ 10 :| [20,30,40,50,60] -- 60

but indices within the bounds don't

fromIndex 5 $ 10 :| [20,30,40,50,60] -- 60
fromIndex 1 $ 10 :| [20,30,40,50,60] -- 60

given a negative index, the function crashes for me

fromIndex -10 $ 10 :| [20,30,40,50,60]
Error found:
in module $PSCI
at :1:12 - 1:14 (line 1, column 12 - line 1, column 14)

  Could not match type
       
    Int
       
  with type
                      
    Int -> t0 t1 -> t1
                      

while checking that type Int
  is at least as general as type Int -> t0 t1 -> t1
while checking that expression 10
  has type Int -> t0 t1 -> t1
in value declaration it

where t1 is an unknown type
      t0 is an unknown type

I am still new to Purescript, so I am not sure why a negative Int can cause that.

Luckily, it won't be called with negative numbers as long the provided chooseInt behaves correctly. Nonetheless, above behavior would explain the the uneven distribution of elements.

Activity

  1. niklas commented on Jun 3, 2020

    @niklas
    Author

    If I switch the order of elements g and f in the type class instance, the distribution is more even. The index lookup is done from the back (so calling it with 0 gets the last element). Negative numbers still cause the same crash.

    instance semigroupAtIndex :: Semigroup (AtIndex a) where
      append (AtIndex g) (AtIndex f) = AtIndex \i -> if i <= 0 then f i else g (i - 1)
  2. garyb commented on Jun 3, 2020

    @garyb
    Member

    You need to use parens around the negative number, -10 is an expression rather than a literal.

    What type are you using for your MonadGen instance, maybe the poor distribution is down to that?

  3. garyb commented on Jun 3, 2020

    @garyb
    Member

    Hmm, no, I see it too. That's extremely concerning that it's been like this for a long time!

  4. hdgarrood commented on Jun 3, 2020

    @hdgarrood
    Contributor

    I think we could do with some distribution tests, maybe in quickcheck if not here. I've definitely seen a few cases (separate from this one) where quickcheck is giving an odd distribution of results.

  5. garyb commented on Jun 3, 2020

    @garyb
    Member

    Yeah, I reproduced this by adding a test case. I thought this library had better test coverage than it does.

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