typelevel / typelevel/cats

trait EuclidianRing should not be a Ring

Open
#4,570 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

The docs give it away EuclideanRing implements a Euclidean domain.

However, there's a problem: a Euclidian domain need not be a Ring.

Approximately & intuitively, euclidean domains defines some set of of integral or whole numbers. These might well be the set of natural numbers (0, 1, 2, 3 etc), which don't have closed subtraction operation.

A ring however, must include a subtraction operation; that's what the n in ring signifies (n for 'negation').

Unfortunately, the trait hierarchy in the algebra package is over-constrained: it makes EuclideanRing extend Ring even though the operations it introduces, based around divmod, dividing whole numbers to yield a quotient and a remainder, do not require the negation aspect of rings.

trait EuclideanRing[@sp(Int, Long, Float, Double) A] extends Any with GCDRing[A] { self =>
  def euclideanFunction(a: A): BigInt
  def equot(a: A, b: A): A
  def emod(a: A, b: A): A
  def equotmod(a: A, b: A): (A, A) = (equot(a, b), emod(a, b))
  def gcd(a: A, b: A)(implicit ev: Eq[A]): A =
    EuclideanRing.euclid(a, b)(ev, self)
  def lcm(a: A, b: A)(implicit ev: Eq[A]): A =
    if (isZero(a) || isZero(b)) zero else times(equot(a, gcd(a, b)), b)

The impact is that it's not possible to implement a complete & correct algebra for natural numbers (non-negative integers) using the Cats hierarchy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with algebra-core/src/main/scala/algebra/ring/EuclideanRing.scala at the trait hierarchy and read the surrounding GCDRing and divmod-related definitions. Trace the existing hierarchy and implementations to determine the compatibility impact of separating Euclidean-domain operations from ring negation. Done means the algebra package can represent non-negative integers without an over-constrained hierarchy and its affected tests or implementations remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.