-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Quote constant extraction #3697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nicolasstucki
merged 1 commit into
scala:master
from
dotty-staging:quote-constant-extraction
Jan 22, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
package dotty.tools.dotc.quoted | ||
|
||
import java.io.PrintStream | ||
|
||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Compiler that takes the contents of a quoted expression `expr` and produces outputs | ||
* the pretty printed code. | ||
*/ | ||
class ExprDecompiler(out: PrintStream) extends ExprCompiler(null) { | ||
/** Compiler that takes the contents of a quoted expression `expr` and outputs it's tree. */ | ||
class ExprDecompiler(output: tpd.Tree => Context => Unit) extends ExprCompiler(null) { | ||
override def phases: List[List[Phase]] = List( | ||
List(new ExprFrontend(putInClass = false)), // Create class from Expr | ||
List(new QuotePrinter(out)) // Print all loaded classes | ||
List(new QuoteTreeOutput(output)) | ||
) | ||
|
||
class QuoteTreeOutput(output: tpd.Tree => Context => Unit) extends Phase { | ||
override def phaseName: String = "quotePrinter" | ||
override def run(implicit ctx: Context): Unit = output(ctx.compilationUnit.tpdTree)(ctx) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package scala.quoted | ||
|
||
import scala.runtime.quoted.Runner | ||
|
||
object Constant { | ||
def unapply[T](expr: Expr[T])(implicit runner: Runner[T]): Option[T] = runner.toConstantOpt(expr) | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
3 | ||
4 | ||
abc | ||
null | ||
OK | ||
{ | ||
{ | ||
val y: Double = 3.0.*(3.0) | ||
y | ||
} | ||
} | ||
9.0 | ||
{ | ||
{ | ||
val y: Double = 4.0.*(4.0) | ||
y | ||
} | ||
} | ||
16.0 | ||
{ | ||
{ | ||
val y: Double = 5.0.*(5.0) | ||
y | ||
} | ||
} | ||
25.0 | ||
{ | ||
Test.dynamicPower( | ||
{ | ||
println("foo") | ||
2 | ||
} | ||
, 6.0) | ||
} | ||
foo | ||
36.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import scala.quoted._ | ||
|
||
import dotty.tools.dotc.quoted.Runners._ | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
(3: Expr[Int]) match { case Constant(n) => println(n) } | ||
'(4) match { case Constant(n) => println(n) } | ||
'("abc") match { case Constant(n) => println(n) } | ||
'(null) match { case Constant(n) => println(n) } | ||
|
||
'(new Object) match { case Constant(n) => println(n); case _ => println("OK") } | ||
|
||
|
||
// 2 is a lifted constant | ||
println(power(2, 3.0).show) | ||
println(power(2, 3.0).run) | ||
|
||
// n is a lifted constant | ||
val n = 2 | ||
println(power(n, 4.0).show) | ||
println(power(n, 4.0).run) | ||
|
||
// n is a constant in a quote | ||
println(power('(2), 5.0).show) | ||
println(power('(2), 5.0).run) | ||
|
||
// n2 is clearly not a constant | ||
val n2 = '{ println("foo"); 2 } | ||
println(power(n2, 6.0).show) | ||
println(power(n2, 6.0).run) | ||
} | ||
|
||
def power(n: Expr[Int], x: Expr[Double]): Expr[Double] = { | ||
n match { | ||
case Constant(n1) => powerCode(n1, x) | ||
case _ => '{ dynamicPower(~n, ~x) } | ||
} | ||
} | ||
|
||
private def powerCode(n: Int, x: Expr[Double]): Expr[Double] = | ||
if (n == 0) '(1.0) | ||
else if (n == 1) x | ||
else if (n % 2 == 0) '{ { val y = ~x * ~x; ~powerCode(n / 2, '(y)) } } | ||
else '{ ~x * ~powerCode(n - 1, x) } | ||
|
||
def dynamicPower(n: Int, x: Double): Double = | ||
if (n == 0) 1.0 | ||
else if (n % 2 == 0) dynamicPower(n / 2, x * x) | ||
else x * dynamicPower(n - 1, x) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should leave design space for other extractors as well. Some thoughts:
runner
doesn't make sense to programmers.Toolbox
, in macros it's calledUniverse
.ConstantExpr
can be removed, and Liftables could be implemented with an implicitRunner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needing an implicit
Runner
is something that should be avoided. That would create an unnecessary dependency on the compiler/toolbox. In the current design, if you only quote, splice and inline the user never needs to add the runner.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. I just want to point out another logically consistent design. As long as the design tradeoffs are clear, I think it's fine to optimise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In essence, the runner is equivalent to the toolbox except that we only use it to interpret expression at runtime (compile and run, show and deconstruction). The constructor are the quotes themselves,
'(if (~cond) ~e1 else ~e2)
is the constructor for an if expression. The only missing constructor is for literal constants from runtime primitive values, which we special case inConstantExpr
. It is true that havingConstantExpr
is more performant but it is not an optimization. Thought we could decide to compile'(5)
to aConstantExpr
to avoid the pickling memory and runtime costs.