Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3229174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:46:48+00:00 2026-05-17T16:46:48+00:00

I’m trying to define a generic residue class ring in Scala. A residue class

  • 0

I’m trying to define a generic residue class ring in Scala. A residue class ring is defined by some base ring (e.g. the integers) and a modulus (e.g. two), which is a value from the base ring. Both rings and their elements are objects, hence the type of the modulus would normally be a dependent type, depending on the base ring. I understand this isn’t allowed in Scala (for good reasons), so I’m trying to emulate it by approximating the type and doing a runtime check when the residue class ring is constructed.

The definition of ResidueClassRing is accepted without error, however, Scala doesn’t let me instantiate it, for the argument two I get the error message

type mismatch;
found   : dependenttypetest.DependentTypeTest.two.type 
(with underlying type dependenttypetest.Integers.Integer)  
required: dependenttypetest.EuclideanRing#E

Am I doing something wrong? Could this be a bug in the Scala type checker? Is there a better way to define ResidueClassRing?

This is with Scala 2.8.0 in the Eclipse IDE for Helios. The problem already occurred for 2.7.x. Here is a simplified version of the code:

package dependenttypetest


class EuclideanRing
{
  thisRing =>

  type E <: EuclideanRingElement;

  def one: E;

  trait EuclideanRingElement 
  {
    def ring = thisRing;

    def +(b: E): E;
    def %(b: E): E;
  }
}


object Integers extends EuclideanRing
{
  type E = Integer;

  val one: Integer = new Integer(1);

  class Integer(n: Int) extends EuclideanRingElement
  {
    val intValue: Int = n;
    def +(b: Integer): Integer = new Integer(intValue + b.intValue);
    def %(b: Integer): Integer = new Integer(intValue % b.intValue);
  }
}


class ResidueClassRing (val baseRing : EuclideanRing, m : EuclideanRing#E) 
{
  val modulus: baseRing.E = 
    m match {
    case e: baseRing.E if m.ring == baseRing => e;
    case _ => throw new IllegalArgumentException("modulus not from base ring");
    };

  type E = ResidueClassRingElement;

  def one: E = new ResidueClassRingElement(baseRing.one);

  class ResidueClassRingElement (e : baseRing.E)
  {
    def representative: baseRing.E = e % modulus;

    def +(b: E) = new ResidueClassRingElement(
      this.representative + b.representative); 
  }
}


object DependentTypeTest extends Application
{
  val two = new Integers.Integer(2);
  val mod2ring = new ResidueClassRing(Integers, two);

  println(mod2ring.one + mod2ring.one);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:46:49+00:00Added an answer on May 17, 2026 at 4:46 pm

    This seems to work, but I couldn’t get rid of the cast when calculating representative:

    package dependenttypetest
    
    abstract class EuclideanRing{
      thisRing =>
      type E <: EuclideanRingElement;
      def one: E;
      trait EuclideanRingElement
      {
        def ring = thisRing;
    
        def +(b: E): E;
        def %(b: E): E;
      }
    }
    
    class Integers extends EuclideanRing {
      type E = Integer;
      val one: Integer = new Integer(1);
      class Integer(n: Int) extends EuclideanRingElement
      {
        val intValue: Int = n;
        def +(b: Integer): Integer = new Integer(intValue + b.intValue);
        def %(b: Integer): Integer = new Integer(intValue % b.intValue);
        override def toString = "Int" + intValue
      }
    }
    
    object Integers extends Integers 
    
    class ResidueClassRing[ER <: EuclideanRing] (modulus : ER#E) {
      val baseRing = modulus.ring
      type E = ResidueClassRingElement;
      def one: E = new ResidueClassRingElement(baseRing.one);
    
      class ResidueClassRingElement (e : baseRing.E)
      {
        def representative = e % modulus.asInstanceOf[baseRing.E];
        def +(b: E) = new ResidueClassRingElement(
          this.representative + b.representative);
        override def toString = "RC(" + representative + ")"
      }
    }
    
    object DependentTypeTest extends Application {
      val two =  new Integers.Integer(2);
      val mod2ring = new ResidueClassRing[Integers](two)
    
      println(mod2ring.one + mod2ring.one)
    }
    

    BTW: Be careful with the Application trait, it’s rightfully deprecated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.