Skip to content

Commit

Permalink
minimize-modules: CollType and TupleType moved to RType.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 24, 2023
1 parent 87682bb commit 86c83e0
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 147 deletions.
2 changes: 1 addition & 1 deletion common/shared/src/main/scala/sigma/CollsOverArrays.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigma

import sigma.util.CollectionUtil
import sigma.core.RType
import sigma.core.{PairType, RType}
import debox.Buffer
import sigma.core.RType._
import sigma.util.{MaxArrayLength, safeConcatArrays_v5}
Expand Down
24 changes: 0 additions & 24 deletions common/shared/src/main/scala/sigma/core/CollType.scala

This file was deleted.

190 changes: 118 additions & 72 deletions common/shared/src/main/scala/sigma/core/RType.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package sigma.core

import sigma.{Coll, TupleData}
import sigma.core.RType.{SomeType, ThunkData}
import sigma.util.CollectionUtil

import scala.reflect.ClassTag
import scala.annotation.{implicitNotFound, unused}
import scala.annotation.{implicitNotFound, nowarn, unused}
import scala.language.implicitConversions

/** Base type for all runtime type descriptors. Sigma uses type descriptors to
Expand All @@ -11,7 +15,7 @@ import scala.language.implicitConversions
* @see [[getReg]], [[getVar]]
*/
@implicitNotFound(msg = "No RType available for ${A}.")
abstract class RType[A] {
sealed abstract class RType[A] {
/** Class tag suitable for construct instances of Array[A]. */
def classTag: ClassTag[A]

Expand All @@ -22,6 +26,108 @@ abstract class RType[A] {
def emptyArray: Array[A] = Array.empty[A](classTag)
}

/** Type descriptor for types with limited type information like Any, AnyRef, Nothing.
* It also used to wrap class tags which are GenericClassTag instances
* (also with limited information about type structure).
* To describe structure of the type more precisely use concrete classes
* in the hierarchy of RType.
*/
case class GeneralType[A](classTag: ClassTag[A]) extends RType[A] {
override def name: String = classTag match {
case ClassTag.Any => "Any"
case ClassTag.AnyRef => "AnyRef"
case ClassTag.Nothing => "Nothing"
case ct => ct.runtimeClass.getSimpleName()
}
}

/** Descriptor used to represent primitive types. */
case class PrimitiveType[A](
classTag: ClassTag[A],
override val emptyArray: Array[A]) extends RType[A] {
override def name: String = classTag.toString()
}

case class StringType() extends RType[String] {
override def classTag: ClassTag[String] = ClassTag[String](classOf[String])

override def name: String = "String"
}

/** Descriptor of descriptor to represent type of RType[_] instances.
* This describes any possible descriptor, disregarding its underlying type.
* Thus the underlying type is assumed to be Any. */
case object RTypeType extends RType[RType[_]] {
val classTag: ClassTag[RType[_]] = ClassTag[RType[_]](classOf[RType[_]])

override def name: String = s"RType[Any]"
}

case class PairType[A, B](tFst: RType[A], tSnd: RType[B]) extends RType[(A, B)] {
val classTag: ClassTag[(A, B)] = scala.reflect.classTag[(A, B)]

override def name: String = s"(${tFst.name}, ${tSnd.name})"
}

case class ArrayType[A](tA: RType[A]) extends RType[Array[A]] {
val classTag: ClassTag[Array[A]] = {
@nowarn implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[Array[A]]
}

override def name: String = s"Array[${tA.name}]"
}

case class FuncType[A, B](tDom: RType[A], tRange: RType[B]) extends RType[A => B] {
val classTag: ClassTag[A => B] = scala.reflect.classTag[A => B]

override def name: String = s"${tDom.name} => ${tRange.name}"
}

case class OptionType[A](tA: RType[A]) extends RType[Option[A]] {
val classTag: ClassTag[Option[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[Option[A]]
}

override def name: String = s"Option[${tA.name}]"
}

/** Type descriptor for `Coll[A]` type. */
case class CollType[A](tItem: RType[A]) extends RType[Coll[A]] {
override val classTag: ClassTag[Coll[A]] = ClassTag[Coll[A]](classOf[Coll[A]])

override def name: String = s"Coll[${tItem.name}]"
}

case class ThunkType[A](tA: RType[A]) extends RType[ThunkData[A]] {
val classTag: ClassTag[ThunkData[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[ThunkData[A]]
}

override def name: String = s"Thunk[${tA.name}]"
}

/** Descriptor (in RType family) of tuple types.
*
* @param items types of tuple items
*/
case class TupleType(items: Array[SomeType]) extends RType[TupleData] {
override val classTag: ClassTag[TupleData] = scala.reflect.classTag[TupleData]

override def name: String = items.map(_.name).mkString("(", ", ", ")")

override def hashCode(): Int = CollectionUtil.deepHashCode(items)

override def equals(obj: Any): Boolean = (this eq obj.asInstanceOf[AnyRef]) || (obj match {
case that: TupleType => java.util.Arrays.equals(items.asInstanceOf[Array[AnyRef]], that.items.asInstanceOf[Array[AnyRef]])
case _ => false
})
}

object RType {
/** Helper to request RType instances from an implicit scope.*/
def apply[A](implicit t: RType[A]): RType[A] = t
Expand All @@ -41,91 +147,31 @@ object RType {

type SomeType = RType[_]

/** Type descriptor for types with limited type information like Any, AnyRef, Nothing.
* It also used to wrap class tags which are GenericClassTag instances
* (also with limited information about type structure).
* To describe structure of the type more precisely use concrete classes
* in the hierarchy of RType.
*/
case class GeneralType[A](classTag: ClassTag[A]) extends RType[A] {
override def name: String = classTag match {
case ClassTag.Any => "Any"
case ClassTag.AnyRef => "AnyRef"
case ClassTag.Nothing => "Nothing"
case ct => ct.runtimeClass.getSimpleName()
}
}

/** Descriptor used to represent primitive types. */
case class PrimitiveType[A](classTag: ClassTag[A],
override val emptyArray: Array[A]) extends RType[A] {
override def name: String = classTag.toString()
}

implicit case object StringType extends RType[String] {
override def classTag: ClassTag[String] = ClassTag[String](classOf[String])
override def name: String = "String"
}
/** Underlying type of Thunk[A] values (or by-name values of type A). */
type ThunkData[A] = () => A

/** Descriptor of descriptor to represent type of RType[_] instances.
* This describes any possible descriptor, disregarding its underlying type.
* Thus the underlying type is assumed to be Any. */
case object RTypeType extends RType[RType[_]] {
val classTag: ClassTag[RType[_]] = ClassTag[RType[_]](classOf[RType[_]])
override def name: String = s"RType[Any]"
}
/** Implicitly obtain the single RTypeType descriptor casted to the given type A. */
implicit def rtypeRType[A]: RType[RType[A]] = asType[RType[A]](RTypeType)

implicit def pairRType[A,B](implicit tA: RType[A], tB: RType[B]): RType[(A,B)] = PairType(tA, tB)

case class PairType[A,B](tFst: RType[A], tSnd: RType[B]) extends RType[(A,B)] {
val classTag: ClassTag[(A, B)] = scala.reflect.classTag[(A,B)]
override def name: String = s"(${tFst.name}, ${tSnd.name})"
}
implicit def extendPairType[A,B](pt: RType[(A,B)]): PairType[A,B] = pt.asInstanceOf[PairType[A,B]]

implicit def funcRType[A,B](implicit tDom: RType[A], tRange: RType[B]): RType[A => B] = FuncType(tDom, tRange)

case class FuncType[A,B](tDom: RType[A], tRange: RType[B]) extends RType[A => B] {
val classTag: ClassTag[A => B] = scala.reflect.classTag[A => B]
override def name: String = s"${tDom.name} => ${tRange.name}"
}

implicit def arrayRType[A](implicit tA: RType[A]): RType[Array[A]] = ArrayType(tA)

case class ArrayType[A](tA: RType[A]) extends RType[Array[A]] {
val classTag: ClassTag[Array[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[Array[A]]
}
override def name: String = s"Array[${tA.name}]"
}

implicit def optionRType[A](implicit tA: RType[A]): RType[Option[A]] = OptionType(tA)

case class OptionType[A](tA: RType[A]) extends RType[Option[A]] {
val classTag: ClassTag[Option[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[Option[A]]
}
override def name: String = s"Option[${tA.name}]"
}

/** Underlying type of Thunk[A] values (or by-name values of type A). */
type ThunkData[A] = () => A
/** Conversion to underlying descriptor class.
* Allows syntax like
*
* ```val tColl: RType[Coll[A]] = ...; tColl.tItem```
*
* where `tItem` is a method of `CollType`, but is not defined on `RType`.
*/
implicit def downcastCollType[A](ct: RType[Coll[A]]): CollType[A] = ct.asInstanceOf[CollType[A]]

implicit def thunkRType[A](implicit tA: RType[A]): RType[ThunkData[A]] = ThunkType(tA)

case class ThunkType[A](tA: RType[A]) extends RType[ThunkData[A]] {
val classTag: ClassTag[ThunkData[A]] = {
@unused // avoid warning about unused ctA
implicit val ctA: ClassTag[A] = tA.classTag
scala.reflect.classTag[ThunkData[A]]
}
override def name: String = s"Thunk[${tA.name}]"
}

}
24 changes: 0 additions & 24 deletions common/shared/src/main/scala/sigma/core/TupleType.scala

This file was deleted.

4 changes: 3 additions & 1 deletion common/shared/src/main/scala/sigma/package.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import sigma.core._
import sigma.core.RType.{GeneralType, PrimitiveType, SomeType}
import sigma.core.RType.SomeType

import scala.reflect.ClassTag

Expand All @@ -25,6 +25,8 @@ package object sigma {

implicit val UnitType : RType[Unit] = PrimitiveType[Unit](ClassTag.Unit, Array[Unit]()(ClassTag.Unit))

implicit val StringType : RType[String] = core.StringType()

implicit val BigIntRType: RType[BigInt] = GeneralType(BigIntClassTag)
implicit val GroupElementRType: RType[GroupElement] = GeneralType(GroupElementClassTag)
implicit val SigmaPropRType: RType[SigmaProp] = GeneralType(SigmaPropClassTag)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sigmastate.eval

import org.ergoplatform._
import sigma.core.{CollType, RType, TupleType}
import sigma.core._
import sigma.core.RType._
import sigmastate.SType._
import sigmastate.Values.SigmaBoolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ package object eval {

type Digest32Coll = Digest32Coll.Type
implicit val Digest32CollRType: RType[Digest32Coll] = RType[Coll[Byte]].asInstanceOf[RType[Digest32Coll] ]
implicit val Digest32RType: RType[Digest32] = RType[Array[Byte]].asInstanceOf[RType[Digest32] ]

/** Implicit conversions between Dsl type and the type wrapped by the corresponding type Dsl type.
* Here BigInt is Dsl type and BigInteger is wrapped type.
Expand Down
22 changes: 11 additions & 11 deletions interpreter/shared/src/main/scala/sigmastate/types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.math.BigInteger
import org.ergoplatform._
import org.ergoplatform.validation._
import sigma.core.{Nullable, RType}
import sigma.core.RType.GeneralType
import sigma.core.GeneralType
import sigmastate.SType.TypeCode
import sigmastate.interpreter._
import sigmastate.utils.Overloading.Overload1
Expand Down Expand Up @@ -100,16 +100,16 @@ object SType {

val DummyValue = 0.asWrappedType

implicit val typeByte = SByte
implicit val typeShort = SShort
implicit val typeInt = SInt
implicit val typeLong = SLong
implicit val typeBigInt = SBigInt
implicit val typeBoolean = SBoolean
implicit val typeAvlTree = SAvlTree
implicit val typeGroupElement = SGroupElement
implicit val typeSigmaProp = SSigmaProp
implicit val typeBox = SBox
implicit val typeByte : SByte.type = SByte
implicit val typeShort : SShort.type = SShort
implicit val typeInt : SInt.type = SInt
implicit val typeLong : SLong.type = SLong
implicit val typeBigInt : SBigInt.type = SBigInt
implicit val typeBoolean : SBoolean.type = SBoolean
implicit val typeAvlTree : SAvlTree.type = SAvlTree
implicit val typeGroupElement: SGroupElement.type = SGroupElement
implicit val typeSigmaProp : SSigmaProp.type = SSigmaProp
implicit val typeBox : SBox.type = SBox

/** Costructs a collection type with the given type of elements. */
implicit def typeCollection[V <: SType](implicit tV: V): SCollection[V] = SCollection[V](tV)
Expand Down
12 changes: 6 additions & 6 deletions sc/shared/src/main/scala/scalan/TypeDescs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ abstract class TypeDescs extends Base { self: Scalan =>
val LazyAnyElement = Lazy(AnyElement)

implicit val BooleanElement: Elem[Boolean] = new BaseElemLiftable(false, sigma.BooleanType)
implicit val ByteElement: Elem[Byte] = new BaseElemLiftable(0.toByte, sigma.ByteType)
implicit val ShortElement: Elem[Short] = new BaseElemLiftable(0.toShort, sigma.ShortType)
implicit val IntElement: Elem[Int] = new BaseElemLiftable(0, sigma.IntType)
implicit val LongElement: Elem[Long] = new BaseElemLiftable(0L, sigma.LongType)
implicit val UnitElement: Elem[Unit] = new BaseElemLiftable((), sigma.UnitType)
implicit val StringElement: Elem[String] = new BaseElemLiftable("", StringType)
implicit val ByteElement : Elem[Byte] = new BaseElemLiftable(0.toByte, sigma.ByteType)
implicit val ShortElement : Elem[Short] = new BaseElemLiftable(0.toShort, sigma.ShortType)
implicit val IntElement : Elem[Int] = new BaseElemLiftable(0, sigma.IntType)
implicit val LongElement : Elem[Long] = new BaseElemLiftable(0L, sigma.LongType)
implicit val UnitElement : Elem[Unit] = new BaseElemLiftable((), sigma.UnitType)
implicit val StringElement : Elem[String] = new BaseElemLiftable("", sigma.StringType)

/** Implicitly defines element type for pairs. */
implicit final def pairElement[A, B](implicit ea: Elem[A], eb: Elem[B]): Elem[(A, B)] =
Expand Down
3 changes: 1 addition & 2 deletions sc/shared/src/test/scala/sigma/SigmaDslTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.propspec.AnyPropSpec
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import scalan.Platform.threadSleepOrNoOp
import sigma.core.{CollType, RType}
import sigma.core.RType._
import sigma.core.{CollType, OptionType, PairType, RType}
import sigma.util.BenchmarkUtil
import sigma.util.CollectionUtil._
import sigma.util.Extensions._
Expand Down
Loading

0 comments on commit 86c83e0

Please sign in to comment.