Skip to content

Commit

Permalink
minimize-modules: RType descriptors moved to sigma package
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Aug 24, 2023
1 parent f1d0fc7 commit 87682bb
Show file tree
Hide file tree
Showing 33 changed files with 109 additions and 107 deletions.
5 changes: 2 additions & 3 deletions common/shared/src/main/scala/sigma/CollsOverArrays.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package sigma

import sigma.core._
import sigma.util.CollectionUtil
import sigma.core.RType
import debox.Buffer
Expand Down Expand Up @@ -245,8 +244,8 @@ class PairOfCols[@specialized L, @specialized R](val ls: Coll[L], val rs: Coll[R
})

override def hashCode() = ls.hashCode() * 41 + rs.hashCode()
@inline implicit def tL = ls.tItem
@inline implicit def tR = rs.tItem
@inline implicit def tL: RType[L] = ls.tItem
@inline implicit def tR: RType[R] = rs.tItem

override lazy val tItem: RType[(L, R)] = {
RType.pairRType(tL, tR)
Expand Down
21 changes: 6 additions & 15 deletions common/shared/src/main/scala/sigma/core/RType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ object RType {
@inline def asType[T](t: RType[_]): RType[T] = t.asInstanceOf[RType[T]]

def fromClassTag[A](ctA: ClassTag[A]): RType[A] = (ctA match {
case ClassTag.Boolean => BooleanType
case ClassTag.Byte => ByteType
case ClassTag.Short => ShortType
case ClassTag.Int => IntType
case ClassTag.Long => LongType
case ClassTag.Unit => UnitType
case ClassTag.Boolean => sigma.BooleanType
case ClassTag.Byte => sigma.ByteType
case ClassTag.Short => sigma.ShortType
case ClassTag.Int => sigma.IntType
case ClassTag.Long => sigma.LongType
case ClassTag.Unit => sigma.UnitType
case _ => GeneralType[A](ctA)
}).asInstanceOf[RType[A]]

Expand All @@ -62,15 +62,6 @@ object RType {
override def name: String = classTag.toString()
}

val AnyType : RType[Any] = GeneralType[Any] (ClassTag.Any)

implicit val BooleanType : RType[Boolean] = PrimitiveType[Boolean] (ClassTag.Boolean, Array.emptyBooleanArray)
implicit val ByteType : RType[Byte] = PrimitiveType[Byte] (ClassTag.Byte, Array.emptyByteArray)
implicit val ShortType : RType[Short] = PrimitiveType[Short] (ClassTag.Short, Array.emptyShortArray)
implicit val IntType : RType[Int] = PrimitiveType[Int] (ClassTag.Int, Array.emptyIntArray)
implicit val LongType : RType[Long] = PrimitiveType[Long] (ClassTag.Long, Array.emptyLongArray)
implicit val UnitType : RType[Unit] = PrimitiveType[Unit] (ClassTag.Unit, Array[Unit]()(ClassTag.Unit))

implicit case object StringType extends RType[String] {
override def classTag: ClassTag[String] = ClassTag[String](classOf[String])
override def name: String = "String"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sigma
package sigma.core

import sigma.core.RType
import sigma.TupleData
import sigma.core.RType.SomeType
import sigma.util.CollectionUtil

Expand Down
3 changes: 2 additions & 1 deletion common/shared/src/main/scala/sigma/core/package.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package sigma

import scala.annotation.nowarn
import scala.reflect.classTag

/** Contains cores definitions which serves as a basis for [[sigma]] package implementations. */
package object core {
/** Shadow the implicit from sigma package so it doesn't interfere with the resolution
* of ClassTags below.
*/
private def rtypeToClassTag = ???
@nowarn private def rtypeToClassTag = ???

val BigIntClassTag = classTag[BigInt]
val GroupElementClassTag = classTag[GroupElement]
Expand Down
16 changes: 15 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, SomeType}
import sigma.core.RType.{GeneralType, PrimitiveType, SomeType}

import scala.reflect.ClassTag

Expand All @@ -11,6 +11,20 @@ package object sigma {
/** Forces reflection data initialization */
val coreLibReflection = CoreLibReflection

val AnyType: RType[Any] = GeneralType[Any](ClassTag.Any)

implicit val BooleanType: RType[Boolean] = PrimitiveType[Boolean](ClassTag.Boolean, Array.emptyBooleanArray)

implicit val ByteType : RType[Byte] = PrimitiveType[Byte](ClassTag.Byte, Array.emptyByteArray)

implicit val ShortType : RType[Short] = PrimitiveType[Short](ClassTag.Short, Array.emptyShortArray)

implicit val IntType : RType[Int] = PrimitiveType[Int](ClassTag.Int, Array.emptyIntArray)

implicit val LongType : RType[Long] = PrimitiveType[Long](ClassTag.Long, Array.emptyLongArray)

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

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
Expand Up @@ -244,7 +244,7 @@ object Platform {
*/
def isCorrectType[T <: SType](value: Any, tpe: T): Boolean = value match {
case c: Coll[_] => tpe match {
case STuple(items) => c.tItem == RType.AnyType && c.length == items.length
case STuple(items) => c.tItem == sigma.AnyType && c.length == items.length
case tpeColl: SCollection[_] => true
case _ => sys.error(s"Collection value $c has unexpected type $tpe")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.bouncycastle.crypto.ec.CustomNamedCurves
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator
import org.bouncycastle.crypto.params.KeyParameter
import org.bouncycastle.math.ec.{ECCurve, ECFieldElement, ECPoint}
import sigma.core.RType

import java.math.BigInteger
import sigmastate._
Expand Down Expand Up @@ -174,7 +173,7 @@ object Platform {
*/
def isCorrectType[T <: SType](value: Any, tpe: T): Boolean = value match {
case c: Coll[_] => tpe match {
case STuple(items) => c.tItem == RType.AnyType && c.length == items.length
case STuple(items) => c.tItem == sigma.AnyType && c.length == items.length
case _: SCollection[_] => true
case _ => sys.error(s"Collection value $c has unexpected type $tpe")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package sigmastate

import sigma.core.{AVHashMap, Nullable, RType}
import debox.{cfor, sp}
import sigma.core.RType._
import sigmastate.Values.SigmaBoolean
import sigmastate.crypto.DLogProtocol.ProveDlog
import sigmastate.crypto.ProveDHTuple
import sigmastate.eval.SigmaDsl
import sigmastate.crypto.CryptoConstants.EcPointType
import sigmastate.interpreter.{ErgoTreeEvaluator, NamedDesc, OperationCostInfo}
import sigma.{AvlTree, AvlTreeRType, BigInt, BigIntRType, Box, BoxRType, GroupElement, GroupElementRType, Header, HeaderRType, PreHeader, PreHeaderRType, SigmaProp}
import sigma.{Coll, CollOverArray, PairOfCols}
import sigma._

/** Implementation of data equality for two arbitrary ErgoTree data types.
* @see [[DataValueComparer.equalDataValues]]
Expand Down
2 changes: 1 addition & 1 deletion interpreter/shared/src/main/scala/sigmastate/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ object Values {

override lazy val value = {
val xs = items.cast[EvaluatedValue[SAny.type]].map(_.value)
Colls.fromArray(xs.toArray(SAny.classTag.asInstanceOf[ClassTag[SAny.WrappedType]]))(RType.AnyType)
Colls.fromArray(xs.toArray(SAny.classTag.asInstanceOf[ClassTag[SAny.WrappedType]]))(sigma.AnyType)
}

protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
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}
import sigma.core.{CollType, RType, TupleType}
import sigma.core.RType._
import sigmastate.SType._
import sigmastate.Values.SigmaBoolean
Expand Down Expand Up @@ -156,7 +156,7 @@ object Evaluation {
/** Convert SigmaDsl representation of tuple to ErgoTree serializable representation. */
def fromDslTuple(value: Any, tupleTpe: STuple): Coll[Any] = value match {
case t: Tuple2[_,_] => TupleColl(t._1, t._2)
case a: Coll[Any]@unchecked if a.tItem == RType.AnyType => a
case a: Coll[Any]@unchecked if a.tItem == sigma.AnyType => a
case _ =>
sys.error(s"Cannot execute fromDslTuple($value, $tupleTpe)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package object eval {
* Such long tuples are represented as Coll[Any].
* This representaion of tuples is different from representation of pairs (x, y),
* where Tuple2 type is used instead of Coll. */
def TupleColl(items: Any*): Coll[Any] = Colls.fromItems(items:_*)(RType.AnyType)
def TupleColl(items: Any*): Coll[Any] = Colls.fromItems(items:_*)(sigma.AnyType)

trait BaseDigestColl extends TaggedType[Coll[Byte]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ object DataSerializer {
val arr = tuple.items.map { t =>
deserialize(t, r)
}.toArray[Any]
val coll = Colls.fromArray(arr)(RType.AnyType)
val coll = Colls.fromArray(arr)(sigma.AnyType)
Evaluation.toDslTuple(coll, tuple)
case t =>
CheckSerializableTypeCode(t.typeCode)
Expand All @@ -147,7 +147,7 @@ object DataSerializer {
case tTup: STuple if tTup.items.length == 2 =>
Evaluation.stypeToRType(tpeElem)
case _: STuple =>
collRType(RType.AnyType)
collRType(sigma.AnyType)
case _ =>
Evaluation.stypeToRType(tpeElem)
}).asInstanceOf[RType[T#WrappedType]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConstantSerializerSpecification extends TableSerializationSpecification {
implicit val tT = Evaluation.stypeToRType(tpe)
implicit val tag = tT.classTag
forAll { xs: Array[T#WrappedType] =>
implicit val tAny = RType.AnyType
implicit val tAny = sigma.AnyType
roundTripTest(Constant[SCollection[T]](xs.toColl, SCollection(tpe)))
roundTripTest(Constant[SType](xs.toColl.map(x => (x, x)).asWrappedType, SCollection(STuple(tpe, tpe)))) // pairs are special case
val triples = xs.toColl.map(x => TupleColl(x, x, x)).asWrappedType
Expand All @@ -49,7 +49,7 @@ class ConstantSerializerSpecification extends TableSerializationSpecification {
implicit val wWrapped = wrappedTypeGen(tpe)
implicit val tT = Evaluation.stypeToRType(tpe)
@nowarn implicit val tag = tT.classTag
implicit val tAny: RType[Any] = RType.AnyType
implicit val tAny: RType[Any] = sigma.AnyType
forAll { in: (T#WrappedType, T#WrappedType) =>
val (x,y) = (in._1, in._2)
roundTripTest(Constant[SType]((x, y).asWrappedType, STuple(tpe, tpe)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DataSerializerSpecification extends SerializationSpecification {
implicit val wWrapped = wrappedTypeGen(tpe)
implicit val tT = Evaluation.stypeToRType(tpe)
implicit val tagT = tT.classTag
implicit val tAny = RType.AnyType
implicit val tAny = sigma.AnyType
forAll { xs: Array[T#WrappedType] =>
roundtrip[SCollection[T]](xs.toColl, SCollection(tpe))
roundtrip[SType](xs.toColl.map(x => (x, x)).asWrappedType, SCollection(STuple(tpe, tpe)))
Expand All @@ -78,7 +78,7 @@ class DataSerializerSpecification extends SerializationSpecification {
def testTuples[T <: SType](tpe: T) = {
implicit val wWrapped = wrappedTypeGen(tpe)
@nowarn implicit val tag = tpe.classTag[T#WrappedType]
implicit val tAny: RType[Any] = RType.AnyType
implicit val tAny: RType[Any] = sigma.AnyType
forAll { in: (T#WrappedType, T#WrappedType) =>
val (x,y) = (in._1, in._2)
roundtrip[SType]((x, y).asWrappedType, STuple(tpe, tpe))
Expand Down Expand Up @@ -107,7 +107,7 @@ class DataSerializerSpecification extends SerializationSpecification {

property("Should check limits and fail") {
val len = 0xFFFF + 1
val tup = SigmaDsl.Colls.replicate(len, 1.asInstanceOf[Any])(RType.AnyType)
val tup = SigmaDsl.Colls.replicate(len, 1.asInstanceOf[Any])(sigma.AnyType)
assertExceptionThrown({
val w = SigmaSerializer.startWriter()
DataSerializer.serialize(tup.asWrappedType, STuple(Array.fill(len)(SInt)), w)
Expand Down
5 changes: 3 additions & 2 deletions sc/jvm/src/test/scala/scalan/compilation/GraphVizExport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scalan.Scalan
import scalan.core.ScalaNameUtil
import sigma.util.{FileUtil, ProcessUtil, StringUtil}

import scala.annotation.unused
import scala.collection.immutable.StringOps

// TODO implement this outside of the cake
Expand All @@ -21,7 +22,7 @@ class GraphVizExport[Ctx <: Scalan](val scalan: Ctx) {

// TODO it would be better to have nodeColor(elem: Elem[_], optDef: Option[Def[_]]) to
// avoid looking up definition, but this leads to ClassFormatError (likely Scala bug)
protected def nodeColor(td: TypeDesc, d: Def[_])(implicit config: GraphVizConfig): String = d match {
protected def nodeColor(td: TypeDesc, d: Def[_])(implicit @unused config: GraphVizConfig): String = d match {
case _ => nodeColor(td)
}

Expand Down Expand Up @@ -137,7 +138,7 @@ class GraphVizExport[Ctx <: Scalan](val scalan: Ctx) {
case _ => x.toString
}

private def emitDepEdges(sym: Sym, rhs: Def[_])(implicit stream: PrintWriter, config: GraphVizConfig) = {
private def emitDepEdges(sym: Sym, rhs: Def[_])(implicit stream: PrintWriter, @unused config: GraphVizConfig) = {
val (deps, lambdaVars) = rhs match {
case l: Lambda[_, _] => lambdaDeps(l)
case _ => (rhs.deps.toList, Nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class SigmaPPrintSpec extends SigmaDslTesting {
test(SCollectionType(SBoolean), "SBooleanArray")
test(STuple(Vector(SBoolean, SInt)), "SPair(SBoolean, SInt)")

test(RType.BooleanType, "RType.BooleanType")
test(CollType(RType.ByteType), "CollType(RType.ByteType)")
test(sigma.BooleanType, "RType.BooleanType")
test(CollType(sigma.ByteType), "CollType(RType.ByteType)")

// exception handlers
test(new ArithmeticException("msg"), "new ArithmeticException(\"msg\")")
Expand Down
7 changes: 0 additions & 7 deletions sc/jvm/src/test/scala/special/collections/BenchmarkGens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,4 @@ trait BenchmarkGens extends CollGens { suite: Bench[Double] =>
val arrays = ranges.map { case (r, i) => (r.toArray, i) }

val colls = arrays.map { case (arr, i) => (builder.fromArray(arr), i) }

private val config = Seq[KeyValue](
exec.minWarmupRuns -> 5,
exec.maxWarmupRuns -> 10,
exec.benchRuns -> 30,
exec.requireGC -> true
)
}
14 changes: 7 additions & 7 deletions sc/shared/src/main/scala/scalan/TypeDescs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,17 @@ abstract class TypeDescs extends Base { self: Scalan =>
}

/** Type descriptor for `Any`, cannot be used implicitly. */
val AnyElement: Elem[Any] = new BaseElemLiftable[Any](null, AnyType)
val AnyElement: Elem[Any] = new BaseElemLiftable[Any](null, sigma.AnyType)

/** Predefined Lazy value saved here to be used in hotspot code. */
val LazyAnyElement = Lazy(AnyElement)

implicit val BooleanElement: Elem[Boolean] = new BaseElemLiftable(false, BooleanType)
implicit val ByteElement: Elem[Byte] = new BaseElemLiftable(0.toByte, ByteType)
implicit val ShortElement: Elem[Short] = new BaseElemLiftable(0.toShort, ShortType)
implicit val IntElement: Elem[Int] = new BaseElemLiftable(0, IntType)
implicit val LongElement: Elem[Long] = new BaseElemLiftable(0L, LongType)
implicit val UnitElement: Elem[Unit] = new BaseElemLiftable((), UnitType)
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)

/** Implicitly defines element type for pairs. */
Expand Down
6 changes: 4 additions & 2 deletions sc/shared/src/main/scala/scalan/primitives/Equal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package scalan.primitives

import scalan.{Base, Scalan}

import scala.annotation.unused

trait Equal extends Base { self: Scalan =>
/** Binary operation representing structural equality between arguments. */
case class Equals[A: Elem]() extends BinOp[A, Boolean]("==") {
Expand All @@ -13,11 +15,11 @@ trait Equal extends Base { self: Scalan =>
override def applySeq(x: A, y: A): Boolean = !equalValues[A](x, y)
}

protected def equalValues[A](x: Any, y: Any)(implicit eA: Elem[A]) = x == y
protected def equalValues[A](x: Any, y: Any)(implicit @unused eA: Elem[A]) = x == y

/** Extension methods to construct ApplyBinOp nodes */
implicit class EqualOps[A](x: Ref[A]) {
implicit private val eA = x.elem
implicit private val eA: Elem[A] = x.elem

/** Apply Equals binary operation and return Ref to ApplyBinOp node. */
def ===(y: Ref[A]): Ref[Boolean] = Equals[A].apply(x, y)
Expand Down
4 changes: 2 additions & 2 deletions sc/shared/src/test/scala/sigma/SigmaDslSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9716,10 +9716,10 @@ class SigmaDslSpecification extends SigmaDslTesting
},
changedFeature(
{ (x: (Coll[Byte], Int)) =>
SigmaDsl.substConstants(x._1, Coll[Int](x._2), Coll[Any](SigmaDsl.sigmaProp(false))(RType.AnyType))
SigmaDsl.substConstants(x._1, Coll[Int](x._2), Coll[Any](SigmaDsl.sigmaProp(false))(sigma.AnyType))
},
{ (x: (Coll[Byte], Int)) =>
SigmaDsl.substConstants(x._1, Coll[Int](x._2), Coll[Any](SigmaDsl.sigmaProp(false))(RType.AnyType))
SigmaDsl.substConstants(x._1, Coll[Int](x._2), Coll[Any](SigmaDsl.sigmaProp(false))(sigma.AnyType))
},
"{ (x: (Coll[Byte], Int)) => substConstants[Any](x._1, Coll[Int](x._2), Coll[Any](sigmaProp(false))) }",
FuncValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ object SigmaPPrint extends PPrinter {
case Some(v) =>
Tree.Apply("Some", treeifyMany(v))

case coll: Coll[Byte @unchecked] if coll.tItem == RType.ByteType =>
case coll: Coll[Byte @unchecked] if coll.tItem == sigma.ByteType =>
val hexString = ErgoAlgos.encode(coll)
Tree.Apply("Helpers.decodeBytes", treeifyMany(hexString))

Expand Down
Loading

0 comments on commit 87682bb

Please sign in to comment.