-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement quoted Lambda extractor #8457
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 4 commits into
scala:master
from
dotty-staging:add-lambda-extractor
Mar 8, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
055132d
Implement quoted Lambda extractor
nicolasstucki a3b0e2b
Fix #8290: Make Expr.betaReduce give up when it sees a non-function t…
nicolasstucki 7edbe0f
Update library/src/scala/quoted/matching/Lambda.scala
nicolasstucki 7446c65
Avoid missing classtag for map
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package scala.quoted | ||
package matching | ||
|
||
/** Lambda expression extractor */ | ||
object Lambda { | ||
|
||
/** `case Lambda(body)` matche a lambda and extract the body. | ||
* As the body may (will) contain references to the paramter, `body` is a function that recieves those arguments as `Expr`. | ||
* Once this function is applied the result will be the body of the lambda with all references to the parameters replaced. | ||
* If `body` is of type `(T1, T2, ...) => R` then body will be of type `(Expr[T1], Expr[T2], ...) => Expr[R]`. | ||
* | ||
* ``` | ||
* '{ (x: Int) => println(x) } match | ||
* case Lambda(body) => | ||
* // where `body` is: (x: Expr[Int]) => '{ println($x) } | ||
* body('{3}) // returns '{ println(3) } | ||
* ``` | ||
*/ | ||
def unapply[F, Args <: Tuple, Res, G](expr: Expr[F])(using qctx: QuoteContext, tf: TupledFunction[F, Args => Res], tg: TupledFunction[G, Tuple.Map[Args, Expr] => Expr[Res]], functionType: Type[F]): Option[/*QuoteContext ?=>*/ G] = { | ||
import qctx.tasty.{_, given _ } | ||
val argTypes = functionType.unseal.tpe match | ||
case AppliedType(_, functionArguments) => functionArguments.init.asInstanceOf[List[Type]] | ||
qctx.tasty.internal.lambdaExtractor(expr.unseal, argTypes).map { fn => | ||
def f(args: Tuple.Map[Args, Expr]): Expr[Res] = | ||
fn(args.toArray.map(_.asInstanceOf[Expr[Any]].unseal).toList).seal.asInstanceOf[Expr[Res]] | ||
tg.untupled(f) | ||
} | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -3,3 +3,6 @@ run-time: 4 | |
compile-time: 1 | ||
run-time: 1 | ||
run-time: 5 | ||
run-time: 7 | ||
run-time: -1 | ||
run-time: 9 |
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,6 @@ | ||
scala.Predef.identity[scala.Int](1) | ||
1 | ||
{ | ||
scala.Predef.println(1) | ||
1 | ||
} |
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,11 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def test(inline f: Int => Int): String = ${ impl('f) } | ||
|
||
def impl(using QuoteContext)(f: Expr[Int => Int]): Expr[String] = { | ||
Expr(f match { | ||
case Lambda(body) => body('{1}).show | ||
case _ => f.show | ||
}) | ||
} |
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,6 @@ | ||
|
||
@main def Test = { | ||
println(test(identity)) | ||
println(test(x => x)) | ||
println(test(x => { println(x); x })) | ||
} |
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,5 @@ | ||
1.+(2) | ||
{ | ||
scala.Predef.println(1) | ||
2 | ||
} |
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,11 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def test(inline f: (Int, Int) => Int): String = ${ impl('f) } | ||
|
||
def impl(using QuoteContext)(f: Expr[(Int, Int) => Int]): Expr[String] = { | ||
Expr(f match { | ||
case Lambda(body) => body('{1}, '{2}).show | ||
case _ => f.show | ||
}) | ||
} |
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,5 @@ | ||
|
||
@main def Test = { | ||
println(test((x, y) => x + y)) | ||
println(test((x, y) => { println(x); y })) | ||
} |
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 |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
|
||
(f: scala.Function1[scala.Int, scala.Int] { | ||
def apply(x: scala.Int): scala.Int | ||
}).apply(3) | ||
} | ||
}) | ||
}.apply(3) |
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.