diff --git a/sc/jvm/src/test/scala/sigmastate/InterpreterReflectionGeneratorTests.scala b/sc/jvm/src/test/scala/sigmastate/InterpreterReflectionGeneratorTests.scala index aaa1d6c424..5d81e0d19a 100644 --- a/sc/jvm/src/test/scala/sigmastate/InterpreterReflectionGeneratorTests.scala +++ b/sc/jvm/src/test/scala/sigmastate/InterpreterReflectionGeneratorTests.scala @@ -37,7 +37,7 @@ class InterpreterReflectionGeneratorTests extends AnyPropSpec with Matchers { } property("inner class") { - val ctx = null.asInstanceOf[scalan.Library] // ok! type level only + val ctx = null.asInstanceOf[scalan.Scalan] // ok! type level only val clazz = classOf[ctx.Coll.CollElem[_, _]] registerClassEntry(clazz, constructors = Array( diff --git a/sc/shared/src/main/scala/scalan/GraphIRReflection.scala b/sc/shared/src/main/scala/scalan/GraphIRReflection.scala index 0eaba9d8a3..787873035d 100644 --- a/sc/shared/src/main/scala/scalan/GraphIRReflection.scala +++ b/sc/shared/src/main/scala/scalan/GraphIRReflection.scala @@ -24,7 +24,7 @@ object GraphIRReflection { val reflection = ReflectionData { val clazz = classOf[wrappers.scala.WOptions#WOption[_]] - val ctx = null.asInstanceOf[scalan.Library] // ok! type level only + val ctx = null.asInstanceOf[scalan.Scalan] // ok! type level only registerClassEntry(clazz, methods = Map( mkMethod(clazz, "filter", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) => @@ -123,7 +123,7 @@ object GraphIRReflection { } { val clazz = classOf[Colls#CollBuilder] - val ctx = null.asInstanceOf[Library] // ok! type level only + val ctx = null.asInstanceOf[Scalan] // ok! type level only registerClassEntry(clazz, methods = Map( mkMethod(clazz, "xor", Array[Class[_]](classOf[Base#Ref[_]], classOf[Base#Ref[_]])) { (obj, args) => @@ -145,7 +145,7 @@ object GraphIRReflection { { val clazz = classOf[Colls#Coll[_]] - val ctx = null.asInstanceOf[Library] // ok! type level only + val ctx = null.asInstanceOf[Scalan] // ok! type level only registerClassEntry(clazz, methods = Map( mkMethod(clazz, "append", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) => diff --git a/sc/shared/src/main/scala/scalan/Library.scala b/sc/shared/src/main/scala/scalan/Library.scala deleted file mode 100644 index 43f20813f0..0000000000 --- a/sc/shared/src/main/scala/scalan/Library.scala +++ /dev/null @@ -1,120 +0,0 @@ -package scalan - -import scala.language.implicitConversions -import sigma._ -import sigma.data.{Nullable, RType} -import special.wrappers.WrappersModule -import sigma.util.MemoizedFunc - -trait Library extends Scalan - with WrappersModule - with CollsModule -{ - import WOption._ - import WRType._ - import Coll._; import CollBuilder._; - import WSpecialPredef._ - - type LazyRep[T] = MutableLazy[Ref[T]] - - private val _liftElemMemo = new MemoizedFunc({ - case eT: Elem[t] => - val lT = Liftables.asLiftable[Any, t](eT.liftable) - liftableRType(lT).lift(eT.sourceType.asInstanceOf[RType[Any]]) - }) - implicit def liftElem[T](eT: Elem[T]): Ref[WRType[T]] = { - _liftElemMemo(eT).asInstanceOf[Ref[WRType[T]]] // asRep cannot be used for AnyRef - } - - private val _specialPredef: LazyRep[WSpecialPredefCompanionCtor] = MutableLazy(RWSpecialPredef.value) - def specialPredef: Ref[WSpecialPredefCompanionCtor] = _specialPredef.value - - override protected def onReset(): Unit = { - _specialPredef.reset() - _liftElemMemo.reset() - super.onReset() - } - - val CM = CollMethods - private val CBM = CollBuilderMethods - private val WOptionM = WOptionMethods - private val SPCM = WSpecialPredefCompanionMethods - - def colBuilder: Ref[CollBuilder] - - object IsNumericToInt { - def unapply(d: Def[_]): Nullable[Ref[A] forSome {type A}] = d match { - case ApplyUnOp(_: NumericToInt[_], x) => Nullable(x.asInstanceOf[Ref[A] forSome {type A}]) - case _ => Nullable.None - } - } - object IsNumericToLong { - def unapply(d: Def[_]): Nullable[Ref[A] forSome {type A}] = d match { - case ApplyUnOp(_: NumericToLong[_], x) => Nullable(x.asInstanceOf[Ref[A] forSome {type A}]) - case _ => Nullable.None - } - } - - override def rewriteDef[T](d: Def[T]) = d match { - case CM.length(ys) => ys.node match { - // Rule: xs.map(f).length ==> xs.length - case CM.map(xs, _) => - xs.length - // Rule: replicate(len, v).length => len - case CBM.replicate(_, len, _) => - len - // Rule: Const[Coll[T]](coll).length => - case CollConst(coll, _) => - coll.length - // Rule: Coll(items @ Seq(x1, x2, x3)).length => items.length - case CBM.fromItems(_, items, _) => - items.length - case _ => super.rewriteDef(d) - } - - // Rule: replicate(l, x).zip(replicate(l, y)) ==> replicate(l, (x,y)) - case CM.zip(CBM.replicate(b1, l1, v1), CBM.replicate(b2, l2, v2)) if b1 == b2 && l1 == l2 => - b1.replicate(l1, Pair(v1, v2)) - - case CM.map(xs, _f) => _f.node match { - case IdentityLambda() => xs - case _ => xs.node match { - // Rule: replicate(l, v).map(f) ==> replicate(l, f(v)) - case CBM.replicate(b, l, v: Ref[a]) => - val f = asRep[a => Any](_f) - b.replicate(l, Apply(f, v, false)) - - // Rule: xs.map(f).map(g) ==> xs.map(x => g(f(x))) - case CM.map(_xs, f: RFunc[a, b]) => - implicit val ea = f.elem.eDom - val xs = asRep[Coll[a]](_xs) - val g = asRep[b => Any](_f) - xs.map[Any](fun { x: Ref[a] => g(f(x)) }) - - case _ => super.rewriteDef(d) - } - } - - case WOptionM.getOrElse(opt, _) => opt.node match { - // Rule: Some(x).getOrElse(_) ==> x - case SPCM.some(x) => x - case WOptionConst(Some(x), lA) => lA.lift(x) - case _ => super.rewriteDef(d) - } - - case _ => super.rewriteDef(d) - } - - override def invokeUnlifted(e: Elem[_], mc: MethodCall, dataEnv: DataEnv): Any = e match { - case _: CollElem[_,_] => mc match { - case CollMethods.map(_, f) => - val newMC = mc.copy(args = mc.args :+ f.elem.eRange)(mc.resultType, mc.isAdapterCall) - super.invokeUnlifted(e, newMC, dataEnv) - case _ => - super.invokeUnlifted(e, mc, dataEnv) - } - case _ => - super.invokeUnlifted(e, mc, dataEnv) - } - -} diff --git a/sc/shared/src/main/scala/scalan/Scalan.scala b/sc/shared/src/main/scala/scalan/Scalan.scala index c2e9359261..a466df29fa 100644 --- a/sc/shared/src/main/scala/scalan/Scalan.scala +++ b/sc/shared/src/main/scala/scalan/Scalan.scala @@ -2,6 +2,10 @@ package scalan import scalan.primitives._ import scalan.staged.Transforming +import sigma.CollsModule +import sigma.data.{Nullable, RType} +import sigma.util.MemoizedFunc +import special.wrappers.WrappersModule /** Aggregate cake with all inter-dependent modules assembled together. * Each instance of this class contains independent IR context, thus many @@ -16,7 +20,7 @@ import scalan.staged.Transforming * in classed derived from `Scalan`, this is significant benefit over * *everything is global* design. */ -class Scalan + trait Scalan extends TypeDescs with MethodCalls with Tuples @@ -33,4 +37,116 @@ class Scalan with Entities with Modules with DefRewriting + with WrappersModule + with CollsModule { + + import WOption._ + import WRType._ + import Coll._; import CollBuilder._; + import WSpecialPredef._ + + type LazyRep[T] = MutableLazy[Ref[T]] + + private val _liftElemMemo = new MemoizedFunc({ + case eT: Elem[t] => + val lT = Liftables.asLiftable[Any, t](eT.liftable) + liftableRType(lT).lift(eT.sourceType.asInstanceOf[RType[Any]]) + }) + implicit def liftElem[T](eT: Elem[T]): Ref[WRType[T]] = { + _liftElemMemo(eT).asInstanceOf[Ref[WRType[T]]] // asRep cannot be used for AnyRef + } + + private val _specialPredef: LazyRep[WSpecialPredefCompanionCtor] = MutableLazy(RWSpecialPredef.value) + def specialPredef: Ref[WSpecialPredefCompanionCtor] = _specialPredef.value + + override protected def onReset(): Unit = { + _specialPredef.reset() + _liftElemMemo.reset() + super.onReset() + } + + val CM = CollMethods + private val CBM = CollBuilderMethods + private val WOptionM = WOptionMethods + private val SPCM = WSpecialPredefCompanionMethods + + def colBuilder: Ref[CollBuilder] + + object IsNumericToInt { + def unapply(d: Def[_]): Nullable[Ref[A] forSome {type A}] = d match { + case ApplyUnOp(_: NumericToInt[_], x) => Nullable(x.asInstanceOf[Ref[A] forSome {type A}]) + case _ => Nullable.None + } + } + object IsNumericToLong { + def unapply(d: Def[_]): Nullable[Ref[A] forSome {type A}] = d match { + case ApplyUnOp(_: NumericToLong[_], x) => Nullable(x.asInstanceOf[Ref[A] forSome {type A}]) + case _ => Nullable.None + } + } + + override def rewriteDef[T](d: Def[T]) = d match { + case CM.length(ys) => ys.node match { + // Rule: xs.map(f).length ==> xs.length + case CM.map(xs, _) => + xs.length + // Rule: replicate(len, v).length => len + case CBM.replicate(_, len, _) => + len + // Rule: Const[Coll[T]](coll).length => + case CollConst(coll, _) => + coll.length + // Rule: Coll(items @ Seq(x1, x2, x3)).length => items.length + case CBM.fromItems(_, items, _) => + items.length + case _ => super.rewriteDef(d) + } + + // Rule: replicate(l, x).zip(replicate(l, y)) ==> replicate(l, (x,y)) + case CM.zip(CBM.replicate(b1, l1, v1), CBM.replicate(b2, l2, v2)) if b1 == b2 && l1 == l2 => + b1.replicate(l1, Pair(v1, v2)) + + case CM.map(xs, _f) => _f.node match { + case IdentityLambda() => xs + case _ => xs.node match { + // Rule: replicate(l, v).map(f) ==> replicate(l, f(v)) + case CBM.replicate(b, l, v: Ref[a]) => + val f = asRep[a => Any](_f) + b.replicate(l, Apply(f, v, false)) + + // Rule: xs.map(f).map(g) ==> xs.map(x => g(f(x))) + case CM.map(_xs, f: RFunc[a, b]) => + implicit val ea = f.elem.eDom + val xs = asRep[Coll[a]](_xs) + val g = asRep[b => Any](_f) + xs.map[Any](fun { x: Ref[a] => g(f(x)) }) + + case _ => super.rewriteDef(d) + } + } + + case WOptionM.getOrElse(opt, _) => opt.node match { + // Rule: Some(x).getOrElse(_) ==> x + case SPCM.some(x) => x + case WOptionConst(Some(x), lA) => lA.lift(x) + case _ => super.rewriteDef(d) + } + + case _ => super.rewriteDef(d) + } + + override def invokeUnlifted(e: Elem[_], mc: MethodCall, dataEnv: DataEnv): Any = e match { + case _: CollElem[_,_] => mc match { + case CollMethods.map(_, f) => + val newMC = mc.copy(args = mc.args :+ f.elem.eRange)(mc.resultType, mc.isAdapterCall) + super.invokeUnlifted(e, newMC, dataEnv) + case _ => + super.invokeUnlifted(e, mc, dataEnv) + } + case _ => + super.invokeUnlifted(e, mc, dataEnv) + } + + +} diff --git a/sc/shared/src/main/scala/sigmastate/eval/SigmaLibrary.scala b/sc/shared/src/main/scala/sigmastate/eval/SigmaLibrary.scala index 7745873620..ea0329e3db 100644 --- a/sc/shared/src/main/scala/sigmastate/eval/SigmaLibrary.scala +++ b/sc/shared/src/main/scala/sigmastate/eval/SigmaLibrary.scala @@ -1,10 +1,10 @@ package sigmastate.eval -import scalan.Library +import scalan.Scalan import sigma.SigmaDslModule /** Main trait which represents sigma operations in graph IR cake. */ -trait SigmaLibrary extends Library +trait SigmaLibrary extends Scalan with sigma.wrappers.WrappersModule with SigmaDslModule { diff --git a/sc/shared/src/main/scala/special/collection/CollsUnit.scala b/sc/shared/src/main/scala/special/collection/CollsUnit.scala index 90f0dca99e..db9922b82f 100644 --- a/sc/shared/src/main/scala/special/collection/CollsUnit.scala +++ b/sc/shared/src/main/scala/special/collection/CollsUnit.scala @@ -8,7 +8,7 @@ package sigma { * The semantics of each method is the same as in the original class, please look there * for details. */ - trait Colls extends Base { self: Library => + trait Colls extends Base { self: Scalan => trait Coll[A] extends Def[Coll[A]] { implicit def eA: Elem[A]; def length: Ref[Int]; diff --git a/sc/shared/src/main/scala/special/collection/impl/CollsImpl.scala b/sc/shared/src/main/scala/special/collection/impl/CollsImpl.scala index 239bfff4e5..382f5fe763 100644 --- a/sc/shared/src/main/scala/special/collection/impl/CollsImpl.scala +++ b/sc/shared/src/main/scala/special/collection/impl/CollsImpl.scala @@ -9,8 +9,8 @@ package impl { import sigma.reflection.{RClass, RMethod} // Abs ----------------------------------- -trait CollsDefs extends scalan.Scalan with Colls { - self: Library => +trait CollsDefs extends Base with Colls { + self: Scalan => registerModule(CollsModule) @@ -639,4 +639,4 @@ object CollsModule extends scalan.ModuleInfo("sigma", "Colls") { } } -trait CollsModule extends sigma.impl.CollsDefs {self: Library =>} +trait CollsModule extends sigma.impl.CollsDefs {self: Scalan =>} diff --git a/sc/shared/src/main/scala/special/sigma/wrappers/WrappersModule.scala b/sc/shared/src/main/scala/special/sigma/wrappers/WrappersModule.scala index 6696788621..50869cc4e1 100644 --- a/sc/shared/src/main/scala/special/sigma/wrappers/WrappersModule.scala +++ b/sc/shared/src/main/scala/special/sigma/wrappers/WrappersModule.scala @@ -1,4 +1,6 @@ package sigma.wrappers +import scalan.Scalan + trait WrappersModule - extends special.wrappers.WrappersModule + extends special.wrappers.WrappersModule { self: Scalan => } diff --git a/sc/shared/src/main/scala/special/wrappers/WrappersModule.scala b/sc/shared/src/main/scala/special/wrappers/WrappersModule.scala index b74bec314e..745413569a 100644 --- a/sc/shared/src/main/scala/special/wrappers/WrappersModule.scala +++ b/sc/shared/src/main/scala/special/wrappers/WrappersModule.scala @@ -1,5 +1,6 @@ package special.wrappers +import scalan.Scalan import wrappers.scala.WOptionsModule import wrappers.scalan.WRTypesModule import wrappers.special.WSpecialPredefsModule @@ -7,4 +8,4 @@ import wrappers.special.WSpecialPredefsModule trait WrappersModule extends WSpecialPredefsModule with WOptionsModule - with WRTypesModule \ No newline at end of file + with WRTypesModule { self: Scalan => } \ No newline at end of file diff --git a/sc/shared/src/main/scala/wrappers/scala/WOptions.scala b/sc/shared/src/main/scala/wrappers/scala/WOptions.scala index 4bd314c1de..445da52006 100644 --- a/sc/shared/src/main/scala/wrappers/scala/WOptions.scala +++ b/sc/shared/src/main/scala/wrappers/scala/WOptions.scala @@ -3,7 +3,7 @@ package wrappers.scala { import special.wrappers.WrappersModule - trait WOptions extends Base { self: WrappersModule => + trait WOptions extends Base { self: Scalan => trait WOption[A] extends Def[WOption[A]] { implicit def eA: Elem[A]; def isDefined: Ref[Boolean]; diff --git a/sc/shared/src/main/scala/wrappers/scala/impl/WOptionsImpl.scala b/sc/shared/src/main/scala/wrappers/scala/impl/WOptionsImpl.scala index 45d0a91ad6..62a0e0c10f 100644 --- a/sc/shared/src/main/scala/wrappers/scala/impl/WOptionsImpl.scala +++ b/sc/shared/src/main/scala/wrappers/scala/impl/WOptionsImpl.scala @@ -11,8 +11,8 @@ package impl { import sigma.reflection.{RClass, RMethod} // Abs ----------------------------------- -trait WOptionsDefs extends scalan.Scalan with WOptions { - self: WrappersModule => +trait WOptionsDefs extends Base with WOptions { + self: Scalan => class WOptionCls extends EntityObject("WOption") { // entityConst: single const for each entity @@ -240,4 +240,4 @@ object WOptionsModule extends scalan.ModuleInfo("wrappers.scala", "WOptions") { } } -trait WOptionsModule extends wrappers.scala.impl.WOptionsDefs {self: WrappersModule =>} +trait WOptionsModule extends wrappers.scala.impl.WOptionsDefs {self: Scalan =>} diff --git a/sc/shared/src/main/scala/wrappers/scalan/WRTypes.scala b/sc/shared/src/main/scala/wrappers/scalan/WRTypes.scala index 8c6bc48f8e..bc71b8ad26 100644 --- a/sc/shared/src/main/scala/wrappers/scalan/WRTypes.scala +++ b/sc/shared/src/main/scala/wrappers/scalan/WRTypes.scala @@ -1,9 +1,8 @@ package wrappers.scalan { import scalan._ - import special.wrappers.WrappersModule - trait WRTypes extends Base { self: WrappersModule => + trait WRTypes extends Base { self: Scalan => trait WRType[A] extends Def[WRType[A]] { implicit def eA: Elem[A]; def name: Ref[String] diff --git a/sc/shared/src/main/scala/wrappers/scalan/impl/WRTypesImpl.scala b/sc/shared/src/main/scala/wrappers/scalan/impl/WRTypesImpl.scala index 9b2676f70d..c78a2268bf 100644 --- a/sc/shared/src/main/scala/wrappers/scalan/impl/WRTypesImpl.scala +++ b/sc/shared/src/main/scala/wrappers/scalan/impl/WRTypesImpl.scala @@ -1,18 +1,20 @@ package wrappers.scalan -import scala.language.{existentials,implicitConversions} +import scalan.Scalan + +import scala.language.{existentials, implicitConversions} import sigma.data.RType -import special.wrappers.WrappersModule import special.wrappers.RTypeWrapSpec + import scala.collection.compat.immutable.ArraySeq package impl { - import scalan.GraphIRReflection + import scalan.{Base, GraphIRReflection, Scalan} import sigma.reflection.{RClass, RMethod} // Abs ----------------------------------- -trait WRTypesDefs extends scalan.Scalan with WRTypes { - self: WrappersModule => +trait WRTypesDefs extends Base with WRTypes { + self: Scalan => registerModule(WRTypesModule) @@ -116,4 +118,4 @@ object WRTypesModule extends scalan.ModuleInfo("wrappers.scalan", "WRTypes") { } } -trait WRTypesModule extends wrappers.scalan.impl.WRTypesDefs {self: WrappersModule =>} +trait WRTypesModule extends wrappers.scalan.impl.WRTypesDefs {self: Scalan =>} diff --git a/sc/shared/src/main/scala/wrappers/special/WSpecialPredefs.scala b/sc/shared/src/main/scala/wrappers/special/WSpecialPredefs.scala index 5abbab95e2..d1c1260391 100644 --- a/sc/shared/src/main/scala/wrappers/special/WSpecialPredefs.scala +++ b/sc/shared/src/main/scala/wrappers/special/WSpecialPredefs.scala @@ -1,9 +1,7 @@ package wrappers.special { import scalan._ - import special.wrappers.WrappersModule - - trait WSpecialPredefs extends Base { self: WrappersModule => + trait WSpecialPredefs extends Base { self: Scalan => trait WSpecialPredef extends Def[WSpecialPredef]; trait WSpecialPredefCompanion { def some[A](x: Ref[A]): Ref[WOption[A]]; diff --git a/sc/shared/src/main/scala/wrappers/special/impl/WSpecialPredefsImpl.scala b/sc/shared/src/main/scala/wrappers/special/impl/WSpecialPredefsImpl.scala index 1f80e71676..697de01802 100644 --- a/sc/shared/src/main/scala/wrappers/special/impl/WSpecialPredefsImpl.scala +++ b/sc/shared/src/main/scala/wrappers/special/impl/WSpecialPredefsImpl.scala @@ -9,8 +9,8 @@ package impl { import sigma.reflection.RClass // Abs ----------------------------------- -trait WSpecialPredefsDefs extends scalan.Scalan with WSpecialPredefs { - self: WrappersModule => +trait WSpecialPredefsDefs extends Base with WSpecialPredefs { + self: Scalan => registerModule(WSpecialPredefsModule) @@ -66,4 +66,4 @@ object WSpecialPredefsModule extends scalan.ModuleInfo("wrappers.special", "WSpe } } -trait WSpecialPredefsModule extends wrappers.special.impl.WSpecialPredefsDefs {self: WrappersModule =>} +trait WSpecialPredefsModule extends wrappers.special.impl.WSpecialPredefsDefs {self: Scalan =>} diff --git a/sc/shared/src/test/scala/scalan/TestLibrary.scala b/sc/shared/src/test/scala/scalan/TestLibrary.scala index efbce25e68..d78312317f 100644 --- a/sc/shared/src/test/scala/scalan/TestLibrary.scala +++ b/sc/shared/src/test/scala/scalan/TestLibrary.scala @@ -1,6 +1,6 @@ package scalan -trait TestLibrary extends Library { +trait TestLibrary extends Scalan { import CollBuilder._ val reflection = (GraphIRReflection) lazy val colBuilder: Ref[CollBuilder] = variable[CollBuilder] diff --git a/sc/shared/src/test/scala/sigma/CollsStagingTests.scala b/sc/shared/src/test/scala/sigma/CollsStagingTests.scala index 3b56920c8d..332c5e300c 100644 --- a/sc/shared/src/test/scala/sigma/CollsStagingTests.scala +++ b/sc/shared/src/test/scala/sigma/CollsStagingTests.scala @@ -41,7 +41,7 @@ class CollsStagingTests extends WrappersTests { } test("invokeUnlifted for Col") { - val ctx = new WrappersCtx with Library + val ctx = new WrappersCtx with Scalan import ctx._ import Coll._ import CollBuilder._ @@ -59,7 +59,7 @@ class CollsStagingTests extends WrappersTests { } test("invokeUnlifted for method of Ctor") { - val ctx = new WrappersCtx with Library + val ctx = new WrappersCtx with Scalan import ctx._ import Coll._ import CollBuilder._ diff --git a/sc/shared/src/test/scala/special/wrappers/WOptionTests.scala b/sc/shared/src/test/scala/special/wrappers/WOptionTests.scala index 7e046739f0..e952ba7c00 100644 --- a/sc/shared/src/test/scala/special/wrappers/WOptionTests.scala +++ b/sc/shared/src/test/scala/special/wrappers/WOptionTests.scala @@ -1,7 +1,6 @@ package special.wrappers import scala.language.reflectiveCalls -import scalan.Library class WOptionTests extends WrappersTests {