forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward-port changes from Linker rewrite rules.
Add support for conditional rewrites, simple purity checking as well as constant-checking.
- Loading branch information
1 parent
2283964
commit d7cb648
Showing
4 changed files
with
193 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,25 +1,44 @@ | ||
import dotty.linker.rewrites | ||
import dotty.linker._ | ||
|
||
@rewrites | ||
object rules{ | ||
def twoDropsOnes(x: List[Int]) = | ||
x.drop(1).drop(1) | ||
def twoDropsOnes$opt(x: List[Int]) = | ||
x.drop(2) | ||
object rules/* extends NeedsMeta */ { | ||
def twoDropsOnes(x: List[Int]) = | ||
Rewrite(x.drop(1).drop(1), x.drop(2)) | ||
def twoDropRights(x: List[Int], a: Int, b: Int) = | ||
x.dropRight(a).dropRight(b) | ||
def twoDropRights$opt(x: List[Int], a: Int, b: Int) = | ||
x.dropRight(a + b) | ||
def twoFilters(x: List[Int], a: Int => Boolean, b: Int => Boolean) = | ||
x.filter(a).filter(b) | ||
def twoFilters$opt(x: List[Int], a: Int => Boolean, b: Int => Boolean) = | ||
x.filter(x => a(x) && b(x)) | ||
Rewrite(x.dropRight(a).dropRight(b), x.dropRight(a + b)) | ||
|
||
/************************************/ | ||
def twoFilters(x: List[Int], a: Int => Boolean, b: Int => Boolean)(implicit apure: IsPure[a.type]) = | ||
Rewrite(from = x.filter(a).filter(b), | ||
to = x.filter(x => a(x) && b(x))) | ||
|
||
|
||
def prettyPrint(x: Any)(implicit source: Source[x.type]) = | ||
Rewrite(Test.myPrettyPrint(x), println(source + " = " + x)) | ||
|
||
/* | ||
def twoFilters(x: List[Int], a: Int => Boolean, b: Int => Boolean) = | ||
Rewrite(filter = meta.isPure(a), // restriction: the expressions that are used here are very limited | ||
from = x.filter(a).filter(b), | ||
to = x.filter(x => a(x) && b(x))) | ||
def twoFilters(x: List[Int], a: Int => Boolean, b: Int => Boolean)(implicit meta: Semantics) = | ||
if (meta.isPure(a)) | ||
Rewrite(x.filter(a).filter(b), x.filter(x => a(x) && b(x))) | ||
def twoFilters(x: List[Int], a: Int => Boolean, b: Int => Boolean) = | ||
RewriteMeta(meta{ /* full blown meta */ }) | ||
*/ | ||
} | ||
|
||
object Test{ | ||
def myPrettyPrint(a: Any) = ??? | ||
def main(args: Array[String]): Unit = { | ||
List(1,2,3).drop(1).drop(1) | ||
List(1,2,3).dropRight(1).dropRight(1) | ||
List(1,2,3).filter(_ > 2).filter(_ > 1) | ||
myPrettyPrint(args.length) | ||
} | ||
} |