-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add rewrite prototype and couple of fixes #7506
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 5 commits into
scala:master
from
dotty-staging:add-rewrite-prototype-and-fixes
Nov 7, 2019
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe80b80
Add rewrite prototype and couple of fixes
nicolasstucki 8a90e45
Add type safe rewrite prototype
nicolasstucki 6558b34
Add a unified prototype
nicolasstucki 58477a2
Add comments
nicolasstucki 845fe93
Allow all tree copy methods to receive any tree
nicolasstucki 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
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
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,18 @@ | ||
Macro_1$package.plus(1, 4) | ||
5 | ||
|
||
Macro_1$package.plus(0, a) | ||
a | ||
|
||
Macro_1$package.plus(a, b) | ||
a.+(b) | ||
|
||
Macro_1$package.plus(Macro_1$package.plus(a, 0), Macro_1$package.plus(0, b)) | ||
0.+(a).+(b) | ||
|
||
Macro_1$package.power(4, 5) | ||
1024 | ||
|
||
Macro_1$package.power(a, 5) | ||
a.*(a.*(a.*(a.*(1.*(a))))) | ||
|
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,101 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def rewrite[T](x: => T): T = ${ rewriteMacro('x) } | ||
|
||
def plus(x: Int, y: Int): Int = x + y | ||
def times(x: Int, y: Int): Int = x * y | ||
def power(x: Int, y: Int): Int = if y == 0 then 1 else times(x, power(x, y - 1)) | ||
|
||
private def rewriteMacro[T: Type](x: Expr[T])(given QuoteContext): Expr[T] = { | ||
val rewriter = Rewriter( | ||
postTransform = List( | ||
Transformation[Int] { | ||
case '{ plus($x, $y) } => | ||
(x, y) match { | ||
case (Const(0), _) => y | ||
case (Const(a), Const(b)) => Expr(a + b) | ||
case (_, Const(_)) => '{ $y + $x } | ||
case _ => '{ $x + $y } | ||
} | ||
case '{ times($x, $y) } => | ||
(x, y) match { | ||
case (Const(0), _) => '{0} | ||
case (Const(1), _) => y | ||
case (Const(a), Const(b)) => Expr(a * b) | ||
case (_, Const(_)) => '{ $y * $x } | ||
case _ => '{ $x * $y } | ||
} | ||
case '{ power(${Const(x)}, ${Const(y)}) } => | ||
Expr(power(x, y)) | ||
case '{ power($x, ${Const(y)}) } => | ||
if y == 0 then '{1} | ||
else '{ times($x, power($x, ${Expr(y-1)})) } | ||
}), | ||
fixPoint = true | ||
) | ||
|
||
val x2 = rewriter.rewrite(x) | ||
|
||
'{ | ||
println(${Expr(x.show)}) | ||
println(${Expr(x2.show)}) | ||
println() | ||
$x2 | ||
} | ||
} | ||
|
||
object Transformation { | ||
def apply[T: Type](transform: PartialFunction[Expr[T], Expr[T]]) = | ||
new Transformation(transform) | ||
} | ||
class Transformation[T: Type](transform: PartialFunction[Expr[T], Expr[T]]) { | ||
def apply[U: Type](e: Expr[U])(given QuoteContext): Expr[U] = { | ||
e match { | ||
case '{ $e: T } => transform.applyOrElse(e, identity) match { case '{ $e2: U } => e2 } | ||
case e => e | ||
} | ||
} | ||
} | ||
|
||
private object Rewriter { | ||
def apply(preTransform: List[Transformation[_]] = Nil, postTransform: List[Transformation[_]] = Nil, fixPoint: Boolean = false): Rewriter = | ||
new Rewriter(preTransform, postTransform, fixPoint) | ||
} | ||
|
||
private class Rewriter(preTransform: List[Transformation[_]] = Nil, postTransform: List[Transformation[_]] = Nil, fixPoint: Boolean) { | ||
def rewrite[T](e: Expr[T])(given QuoteContext, Type[T]): Expr[T] = { | ||
val e2 = preTransform.foldLeft(e)((ei, transform) => transform(ei)) | ||
val e3 = rewriteChildren(e2) | ||
val e4 = postTransform.foldLeft(e3)((ei, transform) => transform(ei)) | ||
if fixPoint && e4 != e then rewrite(e4) | ||
else e4 | ||
} | ||
|
||
def rewriteChildren[T: Type](e: Expr[T])(given qctx: QuoteContext): Expr[T] = { | ||
import qctx.tasty.{_, given} | ||
class MapChildren extends TreeMap { | ||
override def transformTerm(tree: Term)(given ctx: Context): Term = tree match { | ||
case IsClosure(_) => | ||
tree | ||
case IsInlined(_) | IsSelect(_) => | ||
transformChildrenTerm(tree) | ||
case _ => | ||
tree.tpe.widen match { | ||
case IsMethodType(_) | IsPolyType(_) => | ||
transformChildrenTerm(tree) | ||
case _ => | ||
tree.seal match { | ||
case '{ $x: $t } => rewrite(x).unseal | ||
} | ||
} | ||
} | ||
def transformChildrenTerm(tree: Term)(given ctx: Context): Term = | ||
super.transformTerm(tree) | ||
} | ||
(new MapChildren).transformChildrenTerm(e.unseal).seal.cast[T] // Cast will only fail if this implementation has a bug | ||
} | ||
|
||
} | ||
|
||
|
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,14 @@ | ||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
val a: Int = 5 | ||
val b: Int = 6 | ||
rewrite(plus(1, 4)) | ||
rewrite(plus(0, a)) | ||
rewrite(plus(a, b)) | ||
rewrite(plus(plus(a, 0), plus(0, b))) | ||
rewrite(power(4, 5)) | ||
rewrite(power(a, 5)) | ||
} | ||
|
||
} |
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,9 @@ | ||
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)) | ||
scala.Nil | ||
|
||
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Nothing](scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x))) | ||
scala.Nil | ||
|
||
scala.Nil.map[scala.Nothing](((x: scala.Nothing) => x)).++[scala.Int](scala.List.apply[scala.Int]((3: scala.<repeated>[scala.Int]))).++[scala.Int](scala.Nil) | ||
scala.List.apply[scala.Int]((3: scala.<repeated>[scala.Int])) | ||
|
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,83 @@ | ||
import scala.quoted._ | ||
|
||
inline def rewrite[T](x: => T): T = ${ rewriteMacro('x) } | ||
|
||
private def rewriteMacro[T: Type](x: Expr[T])(given QuoteContext): Expr[T] = { | ||
val rewriter = Rewriter( | ||
postTransform = { | ||
case '{ Nil.map[$t]($f) } => '{ Nil } | ||
case '{ Nil.filter($f) } => '{ Nil } | ||
case '{ Nil.++[$t]($xs) } => xs | ||
case '{ ($xs: List[$t]).++(Nil) } => xs | ||
case x => x | ||
} | ||
) | ||
|
||
val x2 = rewriter.rewrite(x) | ||
|
||
'{ | ||
println(${Expr(x.show)}) | ||
println(${Expr(x2.show)}) | ||
println() | ||
$x2 | ||
} | ||
} | ||
|
||
private object Rewriter { | ||
def apply(preTransform: Expr[Any] => Expr[Any] = identity, postTransform: Expr[Any] => Expr[Any] = identity, fixPoint: Boolean = false): Rewriter = | ||
new Rewriter(preTransform, postTransform, fixPoint) | ||
} | ||
|
||
private class Rewriter(preTransform: Expr[Any] => Expr[Any], postTransform: Expr[Any] => Expr[Any], fixPoint: Boolean) { | ||
def rewrite[T](e: Expr[T])(given QuoteContext, Type[T]): Expr[T] = { | ||
val e2 = checkedTransform(e, preTransform) | ||
val e3 = rewriteChildren(e2) | ||
val e4 = checkedTransform(e3, postTransform) | ||
if fixPoint && e4 != e then rewrite(e4) | ||
else e4 | ||
} | ||
|
||
private def checkedTransform[T: Type](e: Expr[T], transform: Expr[T] => Expr[Any])(given QuoteContext): Expr[T] = { | ||
transform(e) match { | ||
case '{ $x: T } => x | ||
case '{ $x: $t } => throw new Exception( | ||
s"""Transformed | ||
|${e.show} | ||
|into | ||
|${x.show} | ||
| | ||
|Expected type to be | ||
|${summon[Type[T]].show} | ||
|but was | ||
|${t.show} | ||
""".stripMargin) | ||
} | ||
} | ||
|
||
def rewriteChildren[T: Type](e: Expr[T])(given qctx: QuoteContext): Expr[T] = { | ||
import qctx.tasty.{_, given} | ||
class MapChildren extends TreeMap { | ||
override def transformTerm(tree: Term)(given ctx: Context): Term = tree match { | ||
case IsClosure(_) => | ||
tree | ||
case IsInlined(_) | IsSelect(_) => | ||
transformChildrenTerm(tree) | ||
case _ => | ||
tree.tpe.widen match { | ||
case IsMethodType(_) | IsPolyType(_) => | ||
transformChildrenTerm(tree) | ||
case _ => | ||
tree.seal match { | ||
case '{ $x: $t } => rewrite(x).unseal | ||
} | ||
} | ||
} | ||
def transformChildrenTerm(tree: Term)(given ctx: Context): Term = | ||
super.transformTerm(tree) | ||
} | ||
(new MapChildren).transformChildrenTerm(e.unseal).seal.cast[T] // Cast will only fail if this implementation has a bug | ||
} | ||
|
||
} | ||
|
||
|
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,9 @@ | ||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
rewrite(Nil.map(x => x)) | ||
rewrite(Nil.map(x => x) ++ Nil.map(x => x)) | ||
rewrite(Nil.map(x => x) ++ List(3) ++ Nil) | ||
} | ||
|
||
} |
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.