Skip to content

Commit

Permalink
update versions
Browse files Browse the repository at this point in the history
.
  • Loading branch information
Chris Van Vranken committed Jul 7, 2020
1 parent 24de1ae commit 80931a5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 38 deletions.
15 changes: 0 additions & 15 deletions .dotty-ide.json

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*.log

target/

.dotty-ide-artifact
.dotty-ide.json
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val dottyVersion = "0.10.0-RC1"
val dottyVersion = "0.25.0-RC2"

lazy val root = project
.in(file("."))
Expand All @@ -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"
)
)
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.3
sbt.version=1.3.13
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.2.4")
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.1")
33 changes: 18 additions & 15 deletions src/main/scala/Vectors.scala
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
// 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 _ => false
case Apply(Apply(TypeApply(Select(Ident("Array"), "apply"),List(Inferred())),List(Typed(Repeated(Nil,Inferred()),Inferred()))),_) =>
true
case t =>
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
Expand All @@ -61,10 +64,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}")
Expand Down
5 changes: 2 additions & 3 deletions src/test/scala/VectorTests.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.junit.Test
import org.junit.Assert._

import Vectors._
import scala.language.implicitConversions

class VectorTests {

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 80931a5

Please sign in to comment.