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

Add Bounded typeclass #1536

Closed
julien-truffaut opened this issue Feb 12, 2017 · 21 comments
Closed

Add Bounded typeclass #1536

julien-truffaut opened this issue Feb 12, 2017 · 21 comments

Comments

@julien-truffaut
Copy link
Contributor

trait Bounded[A] extends Order[A]{
  def minValue: A
  def maxValue: A
}

such as min(a, minValue) == minValue and max(a, maxValue) == maxValue

@kevinmeredith
Copy link

Why must Bounded extend Order?

I don't believe that Haskell's Bounded imposes such a constraint:

iPrelude> :i Bounded
class Bounded a where
  minBound :: a
  maxBound :: a

@julien-truffaut
Copy link
Contributor Author

It is correct but in this case Bounded will be lawless isn't it?

@kevinmeredith
Copy link

@julien-truffaut
Copy link
Contributor Author

julien-truffaut commented Mar 31, 2017

@kevinmeredith see my first comment (such as ...)

@edmundnoble
Copy link
Contributor

This seems to not be Bounded any more when you add those laws, but BoundedLattice (https://github.com/typelevel/algebra/blob/master/core/src/main/scala/algebra/lattice/BoundedLattice.scala) seeing as < and > can be implemented using meet/join and Eq.

@johnynek
Copy link
Contributor

johnynek commented Apr 1, 2017

I'm not crazy about Bounded. Would prefer LowerBounded and UpperBounded. String, for instance is LowerBounded. NonNegative numbers are LowerBounded. There are some examples of only UpperBounded as well.

@julien-truffaut
Copy link
Contributor Author

I agree with @johnynek it would be more useful to one typeclass for upper and another for lower bound. Here is an example https://github.com/julien-truffaut/newts/blob/master/core/shared/src/main/scala/newts/Min.scala#L35

@kailuowang
Copy link
Contributor

kailuowang commented Apr 1, 2017

For lawless typeclasses, we might also want to consider adding them to alleycats instead, especially if they are not that "core" and we still intend to keep cats.core lean.

@johnynek
Copy link
Contributor

johnynek commented Apr 1, 2017

I don't see how his is lawless when it has an associated Ordering. So, they could extend Order and everything is fine, no?

@johnynek johnynek closed this as completed Apr 1, 2017
@johnynek johnynek reopened this Apr 1, 2017
@kailuowang
Copy link
Contributor

@johnynek I didn't make it clear. I was referring to LowerBounded and UpperBounded.

@johnynek
Copy link
Contributor

johnynek commented Apr 1, 2017

trait LowerBounded[A] extends Order[A] {
  // law: compare(minValue, a) <= 0 for all a
  def minValue: A
}

I'm probably missing it, but how is that lawless? Maybe I'm missing context?

@kailuowang
Copy link
Contributor

kailuowang commented Apr 1, 2017

Ah for some reason I thought your upper and lower bound isn't order associated, now I have no idea how I got that impression. My bad.

@johnynek
Copy link
Contributor

johnynek commented Apr 1, 2017

No problem. In thinking about this more, we probably want this to extend PartialOrder so it can represent the top and bottom of any Lattice, but then maybe indeed these are equivalent to bounded meet and bounded join semi lattices as @edmundnobel suggests.

@izeigerman
Copy link
Contributor

Hi everyone. I've got the following implementation of Enum and Bounded type classes here: https://github.com/izeigerman/scalanum . Is there any chance for it to be included into Cats? Is it missing anything else beside the integration with PartialOrder?

@Michaelt293
Copy link

I like the idea of adding these type classes to Cats. Just a couple of ideas, I think the laws could be expressed as minValue <= a and maxValue >= a. The implementation provided by @izeigerman looks good. I just wonder whether using IndexededSeq in EnumIndex is the way to go since it would then be possible to provide an empty sequence to an instance. An exception would then be thrown when using head and tail to the get the min and max values.

@LukaJCB
Copy link
Member

LukaJCB commented Jun 24, 2019

Hey @izeigerman are you still interested in getting included into Cats? I'm sorry no one ever got back to you, but I think this is definitely still on the table :)

@izeigerman
Copy link
Contributor

@LukaJCB, yes, I still am! Will create a PR soon.

@LukaJCB
Copy link
Member

LukaJCB commented Jun 24, 2019

Awesome, thank you!

@izeigerman
Copy link
Contributor

@LukaJCB Quick question though: I'm having a hard time coming up with laws for Enum typeclass. It doesn't appear that it has any. What makes the matter even worse is that similarly to its Haskell's counterpart, Enum is expected to return a runtime error under certain condition. Can you suggest the best way to handle this in cats?
I was going to place *Bounded typeclasses into the kernel module, but I'm not that sure about Enum.

@LukaJCB
Copy link
Member

LukaJCB commented Jun 24, 2019

I think we should start with adding *Bounded first and then discussing the specifics of Enum in the other ticket, #2342. :) If you need any help with setting up the laws and everything, please feel free to reach out in the cats-dev gitter channel!

@LukaJCB
Copy link
Member

LukaJCB commented Jul 3, 2019

Closed by #2913

@LukaJCB LukaJCB closed this as completed Jul 3, 2019
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

No branches or pull requests

8 participants