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 Syntax For Enumerable #4347

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ trait AllSyntax
with ParallelFoldMapASyntax
with ParallelTraverseFilterSyntax
with ParallelReduceMapASyntax
with PartialNextSyntax
with NextSyntax
with PartialPreviousSyntax
with PreviousSyntax
with BoundedEnumerableSyntax

// Note, since we dropped 2.11.x support, we no longer need to use bincompat
// traits. All future syntax additions should be added to AllSyntax
// directly. In 3.x.x we should clean this up.

trait AllSyntaxBinCompat0 extends UnorderedTraverseSyntax with ApplicativeErrorExtension with TrySyntax

Expand Down
78 changes: 78 additions & 0 deletions core/src/main/scala/cats/syntax/enumerable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats
package syntax

import cats.kernel._

trait PartialNextSyntax {
implicit final def catsSyntaxPartialNext[A](a: A): PartialNextOps[A] =
new PartialNextOps(a)
}

final class PartialNextOps[A](private val lhs: A) extends AnyVal {
def partialNext(implicit A: PartialNext[A]): Option[A] =
A.partialNext(lhs)
}

trait NextSyntax extends PartialNextSyntax {
implicit final def catsSyntaxNext[A](a: A): NextOps[A] =
new NextOps(a)
}

final class NextOps[A](private val lhs: A) extends AnyVal {
def next(implicit A: Next[A]): A =
A.next(lhs)
}

trait PartialPreviousSyntax {
implicit final def catsSyntaxPartialPrevious[A](a: A): PartialPreviousOps[A] =
new PartialPreviousOps(a)
}

final class PartialPreviousOps[A](private val lhs: A) extends AnyVal {
def partialPrevious(implicit A: PartialPrevious[A]): Option[A] =
A.partialPrevious(lhs)
}

trait PreviousSyntax extends PartialPreviousSyntax {
implicit final def catsSyntaxPrevious[A](a: A): PreviousOps[A] =
new PreviousOps(a)
}

final class PreviousOps[A](private val lhs: A) extends AnyVal {
def previous(implicit A: Previous[A]): A =
A.previous(lhs)
}

trait BoundedEnumerableSyntax extends NextSyntax with PreviousSyntax {
implicit final def catsSyntaxForBoundedEnumerable[A](a: A): BoundedEnumerableOps[A] =
new BoundedEnumerableOps(a)
}

final class BoundedEnumerableOps[A](private val lhs: A) extends AnyVal {
def cycleNext(implicit A0: PartialNext[A], A1: LowerBounded[A]): A =
A0.partialNext(lhs).getOrElse(A1.minBound)

def cyclePrevious(implicit A0: PartialPrevious[A], A1: UpperBounded[A]): A =
A0.partialPrevious(lhs).getOrElse(A1.maxBound)
Comment on lines +73 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit concerned that the syntax class implements its own logic.
There's BoundedEnumerable typelclass already that implements cycleNext and cyclePrevious.
Usually the syntax should only do

Suggested change
def cycleNext(implicit A0: PartialNext[A], A1: LowerBounded[A]): A =
A0.partialNext(lhs).getOrElse(A1.minBound)
def cyclePrevious(implicit A0: PartialPrevious[A], A1: UpperBounded[A]): A =
A0.partialPrevious(lhs).getOrElse(A1.maxBound)
def cycleNext(implicit A: BoundedEnumerable[A]): A =
A.cycleNext(lhs)
def cyclePrevious(implicit A: BoundedEnumerable): A =
A.cyclePrevious(lhs)

shouldn't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...on the other hand, I think I see what you meant: BoundedEnumerable is too restrictive for that.
And I think this is because the entire hierarchy is sort of not that well-developed yet. Perhaps, there should be two more typeclasses added:

// just an arbitrary name, feel free to suggest a better one
trait CycleableNext extends PartialNext with LowerBounded {
  def cycleNext ...
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there's a couple classes like that: PartialPreviousUpperBounded and PartialNextLowerBounded, but those do not seem to be well-developed either:

trait PartialPreviousUpperBounded[@sp A] extends PartialPrevious[A] with PartialNext[A] with UpperBounded[A] {
/**
* Enumerate the members in descending order.
*/
def membersDescending: LazyList[A] = {
def loop(a: A): LazyList[A] =
partialPrevious(a) match {
case Some(aa) => aa #:: loop(aa)
case _ => LazyList.empty
}
maxBound #:: loop(maxBound)
}
}

I wonder, why does it inherit to PartialNext if it does not use anything from the latter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@satorg Yeah, there are some issues here.

So, first off, RE the syntax classes having logic. I don't want to demand a constraint of BoundedEnumerable because someone might define a PartialNext and LowerBounded as separate definitions and then we won't be able to solve for the instance, e.g. like Decoder Encoder in circe or Get Put in Doobie.

Secondly, I think there is a more complete and less restrictive encoding for what PartialPreviousUpperBounded and PartialNextLowerBouneded are trying to do. I'm prototyping it out here: isomarcte@9debe1c#diff-805a1fb053663f9d27cfcb8586bc2310cbe9a649366549b8d0106a366774e2e3R45

Keep in mind, that is a rough early draft of what I'm calling Countable (should probably just be called Enumerable). It can be defined for both finite and infinite types, unlike PartialPreviousUpperBounded.

I'm trying to work out how the whole hierarchy should look.

The cycleNext and cyclePrevious functions are interesting. Technically, we could define these for unbounded (infinite) types as well, they would just be aliases for next and previous. That is to say, the cycle over an infinite domain, is just a cycle which never wraps. This might be nice if someone wanted to endlessly enumerate over a domain, not caring if they were cycling or just going on forever. 🤷

Countable (or Enumerable) would be really helpful for defining an Interval type. The stdlib Range and cats-collection Range is sort of trying to be both a convenient way to enumerate a series of elements and also a true mathematical interval, with the stdlib version leaning a bit more into the former and cats-collection's version the latter. I think that is why we have some ergonomic issues with those types. However if we define Countable separately then we can solve the "I want to enumerate over some set of values problem" and having that as a constraint also makes writing Interval a lot nicer. Specifically, saying that an Interval is something that maps to the natural numbers opens up a whole lot of optimizations and ergonomic benefits.

Sorry, I got a little off topic there. All that Countable stuff would be another PR once I've sorted out how I think it might all fit together with the existing classes. For now, I just wanted to get the syntax in place for what is already defined, with minimal constraints.

Copy link
Contributor

@satorg satorg Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate you deep involvements into the topic. Actually, I think you already have a suggestion on how the concern could be solved, don't you?

def nextOrMin(a: A)(implicit A: LowerBounded[A]): A =
partialNext(a).getOrElse(A.minBound)

}
66 changes: 66 additions & 0 deletions kernel/src/main/scala/cats/kernel/Enumerable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ trait PartialNext[@sp A] {
def partialNext(a: A): Option[A]
}

object PartialNext {

def apply[A](implicit A: PartialNext[A]): PartialNext[A] =
A

implicit def catsKernelPartialNextForUnit: PartialNext[Unit] =
cats.kernel.instances.unit.catsKernelStdOrderForUnit
implicit def catsKernelPartialNextForBoolean: PartialNext[Boolean] =
cats.kernel.instances.boolean.catsKernelStdOrderForBoolean
implicit def catsKernelPartialNextForByte: PartialNext[Byte] =
cats.kernel.instances.byte.catsKernelStdOrderForByte
implicit def catsKernelPartialNextForInt: PartialNext[Int] =
cats.kernel.instances.int.catsKernelStdOrderForInt
implicit def catsKernelPartialNextForShort: PartialNext[Short] =
cats.kernel.instances.short.catsKernelStdOrderForShort
implicit def catsKernelPartialNextForLong: PartialNext[Long] =
cats.kernel.instances.long.catsKernelStdOrderForLong
implicit def catsKernelPartialNextForChar: PartialNext[Char] =
cats.kernel.instances.char.catsKernelStdOrderForChar
implicit def catsKernelPartialNextForBigInt: PartialNext[BigInt] =
cats.kernel.instances.bigInt.catsKernelStdOrderForBigInt
}

/**
* A typeclass with an operation which returns a member which is
* always greater than the one supplied.
Expand All @@ -42,6 +65,14 @@ trait Next[@sp A] extends PartialNext[A] {
override def partialNext(a: A): Option[A] = Some(next(a))
}

object Next {
def apply[A](implicit A: Next[A]): Next[A] =
A

implicit def catsKernelNextForBigInt: Next[BigInt] =
cats.kernel.instances.bigInt.catsKernelStdOrderForBigInt
}

/**
* A typeclass with an operation which returns a member which is
* smaller or `None` than the one supplied.
Expand All @@ -51,6 +82,28 @@ trait PartialPrevious[@sp A] {
def partialPrevious(a: A): Option[A]
}

object PartialPrevious {
def apply[A](implicit A: PartialPrevious[A]): PartialPrevious[A] =
A

implicit def catsKernelPartialPreviousForUnit: PartialPrevious[Unit] =
cats.kernel.instances.unit.catsKernelStdOrderForUnit
implicit def catsKernelPartialPreviousForBoolean: PartialPrevious[Boolean] =
cats.kernel.instances.boolean.catsKernelStdOrderForBoolean
implicit def catsKernelPartialPreviousForByte: PartialPrevious[Byte] =
cats.kernel.instances.byte.catsKernelStdOrderForByte
implicit def catsKernelPartialPreviousForInt: PartialPrevious[Int] =
cats.kernel.instances.int.catsKernelStdOrderForInt
implicit def catsKernelPartialPreviousForShort: PartialPrevious[Short] =
cats.kernel.instances.short.catsKernelStdOrderForShort
implicit def catsKernelPartialPreviousForLong: PartialPrevious[Long] =
cats.kernel.instances.long.catsKernelStdOrderForLong
implicit def catsKernelPartialPreviousForChar: PartialPrevious[Char] =
cats.kernel.instances.char.catsKernelStdOrderForChar
implicit def catsKernelPartialPreviousForBigInt: PartialPrevious[BigInt] =
cats.kernel.instances.bigInt.catsKernelStdOrderForBigInt
}

/**
* A typeclass with an operation which returns a member which is
* always smaller than the one supplied.
Expand All @@ -61,6 +114,14 @@ trait Previous[@sp A] extends PartialPrevious[A] {
override def partialPrevious(a: A): Option[A] = Some(previous(a))
}

object Previous {
def apply[A](implicit A: Previous[A]): Previous[A] =
A

implicit def catsKernelPreviousForBigInt: Previous[BigInt] =
cats.kernel.instances.bigInt.catsKernelStdOrderForBigInt
}

/**
* A typeclass which has both `previous` and `next` operations
* such that `next . previous == identity`.
Expand All @@ -70,6 +131,11 @@ trait UnboundedEnumerable[@sp A] extends Next[A] with Previous[A] {
override def partialOrder: PartialOrder[A] = order
}

object UnboundedEnumerable {
def apply[A](implicit A: UnboundedEnumerable[A]): UnboundedEnumerable[A] =
A
}

trait BoundedEnumerable[@sp A] extends PartialPreviousUpperBounded[A] with PartialNextLowerBounded[A] {

def order: Order[A]
Expand Down