From 437533baa50684d1a87fac1cd7600add8ee550f0 Mon Sep 17 00:00:00 2001 From: Chris Van Vranken Date: Mon, 6 Jul 2020 10:49:31 -0400 Subject: [PATCH] update versions --- .dotty-ide.json | 15 --------------- .gitignore | 3 +++ build.sbt | 7 ++++--- project/build.properties | 2 +- project/plugins.sbt | 2 +- src/main/scala/Vectors.scala | 29 +++++++++++++++-------------- src/test/scala/VectorTests.scala | 5 ++--- 7 files changed, 26 insertions(+), 37 deletions(-) delete mode 100644 .dotty-ide.json diff --git a/.dotty-ide.json b/.dotty-ide.json deleted file mode 100644 index 1373e70..0000000 --- a/.dotty-ide.json +++ /dev/null @@ -1,15 +0,0 @@ -[ { - "id" : "root/compile", - "compilerVersion" : "0.10.0-RC1", - "compilerArguments" : [ ], - "sourceDirectories" : [ "/Users/nicolasstucki/Desktop/vec-mult-opimized/src/main/scala-0.10", "/Users/nicolasstucki/Desktop/vec-mult-opimized/src/main/scala", "/Users/nicolasstucki/Desktop/vec-mult-opimized/src/main/java", "/Users/nicolasstucki/Desktop/vec-mult-opimized/target/scala-0.10/src_managed/main" ], - "dependencyClasspath" : [ "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty_0.10/jars/dotty_0.10-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty-compiler_0.10/jars/dotty-compiler_0.10-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty-interfaces/jars/dotty-interfaces-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty-library_0.10/jars/dotty-library_0.10-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/scala-library/jars/scala-library-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.7.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-lang.modules/scala-asm/bundles/scala-asm-6.0.0-scala-1.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-lang.modules/scala-xml_2.12/bundles/scala-xml_2.12-1.1.0.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-sbt/compiler-interface/jars/compiler-interface-1.2.2.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-sbt/util-interface/jars/util-interface-1.2.2.jar", "/Users/nicolasstucki/.ivy2/cache/org.jline/jline-reader/jars/jline-reader-3.9.0.jar", "/Users/nicolasstucki/.ivy2/cache/org.jline/jline-terminal/jars/jline-terminal-3.9.0.jar", "/Users/nicolasstucki/.ivy2/cache/org.jline/jline-terminal-jna/jars/jline-terminal-jna-3.9.0.jar", "/Users/nicolasstucki/.ivy2/cache/net.java.dev.jna/jna/jars/jna-4.2.2.jar" ], - "classDirectory" : "/Users/nicolasstucki/Desktop/vec-mult-opimized/target/scala-0.10/classes" -}, { - "id" : "root/test", - "compilerVersion" : "0.10.0-RC1", - "compilerArguments" : [ ], - "sourceDirectories" : [ "/Users/nicolasstucki/Desktop/vec-mult-opimized/src/test/scala-0.10", "/Users/nicolasstucki/Desktop/vec-mult-opimized/src/test/scala", "/Users/nicolasstucki/Desktop/vec-mult-opimized/src/test/java", "/Users/nicolasstucki/Desktop/vec-mult-opimized/target/scala-0.10/src_managed/test" ], - "dependencyClasspath" : [ "/Users/nicolasstucki/Desktop/vec-mult-opimized/target/scala-0.10/classes", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty_0.10/jars/dotty_0.10-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty-compiler_0.10/jars/dotty-compiler_0.10-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty-interfaces/jars/dotty-interfaces-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/dotty-library_0.10/jars/dotty-library_0.10-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/ch.epfl.lamp/scala-library/jars/scala-library-0.10.0-RC1.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.7.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-lang.modules/scala-asm/bundles/scala-asm-6.0.0-scala-1.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-lang.modules/scala-xml_2.12/bundles/scala-xml_2.12-1.1.0.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-sbt/compiler-interface/jars/compiler-interface-1.2.2.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-sbt/util-interface/jars/util-interface-1.2.2.jar", "/Users/nicolasstucki/.ivy2/cache/org.jline/jline-reader/jars/jline-reader-3.9.0.jar", "/Users/nicolasstucki/.ivy2/cache/org.jline/jline-terminal/jars/jline-terminal-3.9.0.jar", "/Users/nicolasstucki/.ivy2/cache/org.jline/jline-terminal-jna/jars/jline-terminal-jna-3.9.0.jar", "/Users/nicolasstucki/.ivy2/cache/net.java.dev.jna/jna/jars/jna-4.2.2.jar", "/Users/nicolasstucki/.ivy2/cache/com.novocode/junit-interface/jars/junit-interface-0.11.jar", "/Users/nicolasstucki/.ivy2/cache/junit/junit/jars/junit-4.11.jar", "/Users/nicolasstucki/.ivy2/cache/org.hamcrest/hamcrest-core/jars/hamcrest-core-1.3.jar", "/Users/nicolasstucki/.ivy2/cache/org.scala-sbt/test-interface/jars/test-interface-1.0.jar" ], - "classDirectory" : "/Users/nicolasstucki/Desktop/vec-mult-opimized/target/scala-0.10/test-classes" -} ] \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3d554bb..478fb29 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ *.log target/ + +.dotty-ide-artifact +.dotty-ide.json \ No newline at end of file diff --git a/build.sbt b/build.sbt index d1b8def..5783131 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,4 @@ -val dottyVersion = "0.10.0-RC1" +val dottyVersion = "0.25.0-RC2" lazy val root = project .in(file(".")) @@ -9,8 +9,9 @@ lazy val root = project scalaVersion := dottyVersion, libraryDependencies ++= Seq( - "ch.epfl.lamp" % "dotty_0.10" % dottyVersion, - "ch.epfl.lamp" % "dotty_0.10" % dottyVersion % "test->runtime", + "ch.epfl.lamp" % "dotty_0.25" % dottyVersion, + "ch.epfl.lamp" % "dotty_0.25" % dottyVersion % "test->runtime", + "ch.epfl.lamp" %% "dotty-staging" % dottyVersion, "com.novocode" % "junit-interface" % "0.11" % "test" ) ) diff --git a/project/build.properties b/project/build.properties index 0cd8b07..0837f7a 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.2.3 +sbt.version=1.3.13 diff --git a/project/plugins.sbt b/project/plugins.sbt index f488f11..03ee51a 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1 +1 @@ -addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.2.4") +addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.1") diff --git a/src/main/scala/Vectors.scala b/src/main/scala/Vectors.scala index e19c6af..cc1eea4 100644 --- a/src/main/scala/Vectors.scala +++ b/src/main/scala/Vectors.scala @@ -1,43 +1,44 @@ // Import Expr and some extension methods import scala.quoted._ +import scala.quoted.staging._ import scala.tasty._ object Vectors { // Needed to show quotes - implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make + given Toolbox = Toolbox.make(getClass.getClassLoader) /** Compute the dot product of the vectors (represented as arrays) * Returns (v1(0) * v2(0)) + (v1(1) * v2(1)) + ... + (v1(v1.length - 1) * v2(v2.length - 1)) * Or throws an exception if v1.length != v2.length * Both arrays are assumed to be immutable. */ - inline def dot(v1: => Array[Int], v2: => Array[Int]): Int = ~dotImpl('(v1), '(v2))(Tasty.macroContext) + inline def dot(v1: => Array[Int], v2: => Array[Int]): Int = ${dotImpl('{v1}, '{v2})} /** Generates code to compute the dot product. * Will try to partially evaluate any statically available data. */ - def dotImpl(v1: Expr[Array[Int]], v2: Expr[Array[Int]])(reflect: Tasty): Expr[Int] = { - import reflect._ + def dotImpl(v1: Expr[Array[Int]], v2: Expr[Array[Int]])(using qctx: QuoteContext): Expr[Int] = { + import qctx.tasty.{_} object EmptyArray { def unapply(arg: Tree): Boolean = arg match { - case Term.Apply(Term.Apply(Term.TypeApply(Term.Select(Term.Ident("Array"), "apply", _), List(TypeTree.Synthetic())), List(Term.Typed(Term.Repeated(Nil), TypeTree.Synthetic()))), _) => true + case Apply(Apply(TypeApply(Select(Ident("Array"), "apply"),List(Inferred())),List(Typed(Repeated(Nil,Inferred()),Inferred()))),_) => true case _ => false } } // Useful methods // Use i.toExpr to lift an i:Int into an quoted.Expr[Int] - // Use q.toTasty to transform a q:quoted.Expr[_] to a Tasty.Tree + // Use q.unseal to transform a q:quoted.Expr[_] to a Tasty.Tree // Use tree.toExpr[Int] to transform a tree:Tasty.Tree to a quoted.Expr[Int] // Use q.show to show the code of a q:quoted.Expr[_] - // Use tree.show to show the extractors needed to pattern match a tree:Tasty.Tree + // Use tree.showExtractors to show the extractors needed to pattern match a tree:Tasty.Tree - val generatedCode = (v1.toTasty.underlyingArgument, v2.toTasty.underlyingArgument) match { - case (EmptyArray(), EmptyArray()) => '(0) + val generatedCode = (v1.unseal.underlyingArgument, v2.unseal.underlyingArgument) match { + case (EmptyArray(), EmptyArray()) => '{0} // TODO Exercise: optimize more cases - // case (EmptyArray(), _) => '() + // case (EmptyArray(), _) => '{0} // ... case (tv1, tv2) => // Print the extractors of tv1 and tv2 @@ -61,10 +62,10 @@ object Vectors { generatedCode } - /** Staged code that computes the the dot product with a while loop */ - def dynamicDot(v1: Expr[Array[Int]], v2: Expr[Array[Int]]): Expr[Int] = '{ - val vv1 = ~v1 - val vv2 = ~v2 + /** Staged code that computes the dot product with a while loop */ + def dynamicDot(v1: Expr[Array[Int]], v2: Expr[Array[Int]])(using qctx: QuoteContext): Expr[Int] = '{ + val vv1 = $v1 + val vv2 = $v2 val len = vv1.length if (vv2.length != len) throw new Exception(s"Vectors must have the same sizes ($len, ${vv2.length}") diff --git a/src/test/scala/VectorTests.scala b/src/test/scala/VectorTests.scala index 48c47ea..164daf9 100644 --- a/src/test/scala/VectorTests.scala +++ b/src/test/scala/VectorTests.scala @@ -1,7 +1,7 @@ import org.junit.Test import org.junit.Assert._ - import Vectors._ +import scala.language.implicitConversions class VectorTests { @@ -10,7 +10,7 @@ class VectorTests { val empty: Array[Int] = Array() assertEquals(dot(empty, Array()), 0) } - + @Test def zeroVector(): Unit = { assertEquals(dot(Array(1, 2, 3, 4, 5), Array(0, 0, 0, 0, 0)), 0) val zeros: Array[Int] = Array(0, 0, 0, 0, 0) @@ -39,7 +39,6 @@ class VectorTests { assertEquals(dot(vec, Array(10, 20, 30, 40, 50)), 550) } - @Test def mixedVector(): Unit = { val v1 = 1 val z = 0