-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Term.betaReduce
not working for curried context functions
#17506
Comments
I did some quick experiments and I discovered two things:
My test (mostly just printlns with inline parameters added for readability to the previous minimization): Macro.scala import scala.quoted.*
class Foo
class Bar
inline def fooCurriedExpr(inline f: Foo ?=> Bar ?=> Int): Int = ${ fooCurriedExprImpl('f) }
def fooCurriedExprImpl(f: Expr[Foo ?=> Bar ?=> Int])(using Quotes) =
val applied = '{ ${f}(using new Foo)(using new Bar) }
println("CtxFun curried, initial: " + applied.show)
val reduced = Expr.betaReduce(applied)
println("CtxFun curried, after betaReduce: " + reduced.show)
reduced
inline def fooTupledExpr(inline f: (Foo, Bar) ?=> Int): Int = ${ fooTupledExprImpl('f) }
def fooTupledExprImpl(f: Expr[(Foo, Bar) ?=> Int])(using Quotes) =
val applied = '{ ${f}(using new Foo, new Bar) }
println("CtxFun tupled, initial: " + applied.show)
val reduced = Expr.betaReduce(applied)
println("CtxFun tupled, after betaReduce: " + reduced.show)
reduced
// added non context function
inline def fooCurriedFun(inline f: Foo => Bar => Int): Int = ${fooCurriedFunImpl('f)}
def fooCurriedFunImpl(f: Expr[Foo => Bar => Int])(using Quotes) =
val applied = '{ ${f}(new Foo)(new Bar) }
println("Fun curried, initial: " + applied.show)
val reduced = Expr.betaReduce(applied)
println("Fun curried, after betaReduce: " + reduced.show)
reduced MacroTest.scala @main def run() =
fooCurriedExpr(123)
fooTupledExpr(123)
fooCurriedFun((f: Foo) => (b: Bar) => (123)) Output
|
Interesting. The behavior is consistent and correct, but we do have room for improvement. The core bera reduction logic seems to be able to reduce such expressions. For example the following code is reduced after the bata reduction phase. class Foo
class Bar
val f1: Foo ?=> Bar ?=> Int = ???
def test1 = (123 : Foo ?=> Bar ?=> Int)(using new Foo)(using new Bar)
val f2: (Foo, Bar) ?=> Int = ???
def test2 = (123: (Foo, Bar) ?=> Int)(using new Foo, new Bar) |
Compiler version
3.3.1-RC1-bin-20230514-b0ccf40-NIGHTLY and before
Minimized code
Macro.scala
MacroTest.scala:
Output
After commenting out
println(fooCurriedReflect(123))
the code compiles successfully.Expectation
The code should compile as is.
Term.betaReduce
should successfully reduce an application of a curried context function (returningSome
rather thanNone
) just a it does for a tupled context function.The text was updated successfully, but these errors were encountered: