Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stack overflow in BoundedEnum (Either _ _) #19

Merged
merged 1 commit into from
Nov 10, 2016
Merged

Fix stack overflow in BoundedEnum (Either _ _) #19

merged 1 commit into from
Nov 10, 2016

Conversation

LiamGoodacre
Copy link
Member

The definition of toEnum in the BoundedEnum (Either _ _) instance
eagerly depended on its own instance.

Hid the recursion behind a lambda.
Added a regression test for the instance.

@LiamGoodacre
Copy link
Member Author

Bug spotted by @jacereda

@garyb
Copy link
Member

garyb commented Nov 10, 2016

As part of the work for purescript-contrib/purescript-quickcheck-laws#14 I suppose? Thanks both 😄

@jacereda
Copy link
Contributor

Yes, and more is coming.

@garyb
Copy link
Member

garyb commented Nov 10, 2016

Ah, merging the other PR has made this one conflict, can you update accordingly please @LiamGoodacre?

The definition of `toEnum` in the `BoundedEnum (Either _ _)` instance
eagerly depended on its own instance.

Hid the recursion behind a lambda.
Added a regression test for the instance.
@LiamGoodacre
Copy link
Member Author

@paf31 want to drop the cardinality check here too?

@garyb
Copy link
Member

garyb commented Nov 10, 2016

I don't think you can, since it's used to determine whether the value is in Left or Right?

@LiamGoodacre
Copy link
Member Author

Yeah just realised :P

@garyb
Copy link
Member

garyb commented Nov 10, 2016

In that case, thanks 😉

@garyb garyb merged commit dc6bbed into purescript:master Nov 10, 2016
@LiamGoodacre
Copy link
Member Author

Actually we could drop it from the recursive case?

@LiamGoodacre
Copy link
Member Author

By that I mean, we can get rid of the bit using our own cardinality.

@garyb
Copy link
Member

garyb commented Nov 10, 2016

Like this?

    to :: Cardinality a -> Maybe (Either a b)
    to (Cardinality ca)
      | n >= 0 && n < ca = Left <$> toEnum n
      | otherwise = Right <$> toEnum (n - ca)

@LiamGoodacre
Copy link
Member Author

Yeah like that

@garyb
Copy link
Member

garyb commented Nov 10, 2016

Sure, works for me!

@jacereda
Copy link
Contributor

Would this be a possibility as well?

to :: Cardinality a -> Maybe (Either a b)
to (Cardinality ca)  = Left <$> toEnum n <|> Right <$> toEnum (n - ca)

Don't know how slow it would be though.

@garyb
Copy link
Member

garyb commented Nov 10, 2016

Sure, although yeah I think prefer the other option - we're still making the cardinality check so may as well use that info to avoid the extra toEnum call.

@jacereda
Copy link
Contributor

In the other option I guess you can get rid of Cardinality cab as well, right?

@garyb
Copy link
Member

garyb commented Nov 10, 2016

I edited that out immediately after posting it 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants