-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow to beta reduce curried function applications in quotes reflect #18121
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
+183
−21
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
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,80 @@ | ||
class Foo | ||
class Bar | ||
class Baz | ||
|
||
import scala.quoted._ | ||
|
||
def assertBetaReduction(using Quotes)(applied: Expr[Any], expected: String): quotes.reflect.Term = | ||
import quotes.reflect._ | ||
val reducedMaybe = Term.betaReduce(applied.asTerm) | ||
assert(reducedMaybe.isDefined) | ||
val reduced = reducedMaybe.get | ||
assert(reduced.show == expected,s"obtained: ${reduced.show}, expected: ${expected}") | ||
reduced | ||
|
||
inline def regularCurriedCtxFun2BetaReduceTest(inline f: Foo ?=> Bar ?=> Int): Unit = | ||
${regularCurriedCtxFun2BetaReduceTestImpl('f)} | ||
def regularCurriedCtxFun2BetaReduceTestImpl(f: Expr[Foo ?=> Bar ?=> Int])(using Quotes): Expr[Int] = | ||
val expected = | ||
"""|{ | ||
| val contextual$3: Bar = new Bar() | ||
| val contextual$2: Foo = new Foo() | ||
| 123 | ||
|}""".stripMargin | ||
val applied = '{$f(using new Foo())(using new Bar())} | ||
assertBetaReduction(applied, expected).asExprOf[Int] | ||
|
||
inline def regularCurriedFun2BetaReduceTest(inline f: Foo => Bar => Int): Int = | ||
${regularCurriedFun2BetaReduceTestImpl('f)} | ||
def regularCurriedFun2BetaReduceTestImpl(f: Expr[Foo => Bar => Int])(using Quotes): Expr[Int] = | ||
val expected = | ||
"""|{ | ||
| val b: Bar = new Bar() | ||
| val f: Foo = new Foo() | ||
| 123 | ||
|}""".stripMargin | ||
val applied = '{$f(new Foo())(new Bar())} | ||
assertBetaReduction(applied, expected).asExprOf[Int] | ||
|
||
inline def typeParamCurriedFun2BetaReduceTest(inline f: [A] => A => [B] => B => Unit): Unit = | ||
${typeParamCurriedFun2BetaReduceTestImpl('f)} | ||
def typeParamCurriedFun2BetaReduceTestImpl(f: Expr[[A] => (a: A) => [B] => (b: B) => Unit])(using Quotes): Expr[Unit] = | ||
val expected = | ||
"""|{ | ||
| type Y = Bar | ||
| val y: Bar = new Bar() | ||
| type X = Foo | ||
| val x: Foo = new Foo() | ||
| typeParamFun2[Y, X](y, x) | ||
|}""".stripMargin | ||
val applied = '{$f.apply[Foo](new Foo()).apply[Bar](new Bar())} | ||
assertBetaReduction(applied, expected).asExprOf[Unit] | ||
|
||
inline def regularCurriedFun3BetaReduceTest(inline f: Foo => Bar => Baz => Int): Int = | ||
${regularCurriedFun3BetaReduceTestImpl('f)} | ||
def regularCurriedFun3BetaReduceTestImpl(f: Expr[Foo => Bar => Baz => Int])(using Quotes): Expr[Int] = | ||
val expected = | ||
"""|{ | ||
| val i: Baz = new Baz() | ||
| val b: Bar = new Bar() | ||
| val f: Foo = new Foo() | ||
| 123 | ||
|}""".stripMargin | ||
val applied = '{$f(new Foo())(new Bar())(new Baz())} | ||
assertBetaReduction(applied, expected).asExprOf[Int] | ||
|
||
inline def typeParamCurriedFun3BetaReduceTest(inline f: [A] => A => [B] => B => [C] => C => Unit): Unit = | ||
${typeParamCurriedFun3BetaReduceTestImpl('f)} | ||
def typeParamCurriedFun3BetaReduceTestImpl(f: Expr[[A] => A => [B] => B => [C] => C => Unit])(using Quotes): Expr[Unit] = | ||
val expected = | ||
"""|{ | ||
| type Z = Baz | ||
| val z: Baz = new Baz() | ||
| type Y = Bar | ||
| val y: Bar = new Bar() | ||
| type X = Foo | ||
| val x: Foo = new Foo() | ||
| typeParamFun3[Z, Y, X](z, y, x) | ||
|}""".stripMargin | ||
val applied = '{$f.apply[Foo](new Foo()).apply[Bar](new Bar()).apply[Baz](new Baz())} | ||
assertBetaReduction(applied, expected).asExprOf[Unit] |
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 @@ | ||
@main def run() = | ||
def typeParamFun2[A, B](a: A, b: B): Unit = println(a.toString + " " + b.toString) | ||
def typeParamFun3[A, B, C](a: A, b: B, c: C): Unit = println(a.toString + " " + b.toString) | ||
|
||
regularCurriedCtxFun2BetaReduceTest((f: Foo) ?=> (b: Bar) ?=> 123) | ||
regularCurriedCtxFun2BetaReduceTest(123) | ||
regularCurriedFun2BetaReduceTest(((f: Foo) => (b: Bar) => 123)) | ||
typeParamCurriedFun2BetaReduceTest([X] => (x: X) => [Y] => (y: Y) => typeParamFun2[Y, X](y, x)) | ||
|
||
regularCurriedFun3BetaReduceTest((f: Foo) => (b: Bar) => (i: Baz) => 123) | ||
typeParamCurriedFun3BetaReduceTest([X] => (x: X) => [Y] => (y: Y) => [Z] => (z: Z) => typeParamFun3[Z, Y, X](z, y, 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.
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.
I still belive this is to complex for a first-time user. I would call this the general rule.
I would add a trivial beta reduction example before this.
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.
Oh, I see that you did that in the other file. We should have the same example in both documentations. I believe that my new example will be better for new users that are less experienced.