Skip to content
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

Poly functions are not beta reduced #15968

Closed
Katrix opened this issue Sep 4, 2022 · 4 comments · Fixed by #16623
Closed

Poly functions are not beta reduced #15968

Katrix opened this issue Sep 4, 2022 · 4 comments · Fixed by #16623
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:enhancement
Milestone

Comments

@Katrix
Copy link
Contributor

Katrix commented Sep 4, 2022

Compiler version

3.2.0

Minimized code

//file a.scala
object betaReduceTest {

  inline def macroPolyFun[A](inline arg: A, inline f: [Z] => Z => String): String =
    ${ macroPolyFunImpl[A]('arg, 'f) }

  def macroPolyFunImpl[A: Type](arg: Expr[A], f: Expr[[Z] => Z => String])(using Quotes): Expr[String] =
    Expr.betaReduce('{ $f($arg) })

  inline def macroFun[A](inline arg: A, inline f: A => String): String =
    ${ macroFunImpl[A]('arg, 'f) }

  def macroFunImpl[A: Type](arg: Expr[A], f: Expr[A => String])(using Quotes): Expr[String] =
    Expr.betaReduce('{ $f($arg) })
}

//file b.scala
object betaReduceTest2 {

  def test: Unit = {
    betaReduceTest.macroPolyFun("foo", [Z] => (arg: Z) => arg.toString)
    betaReduceTest.macroFun("foo", arg => arg.toString)
  }
}

Decompiled code (with Fernflower)

   public void test() {
      String var10000 = (String)((Function1)(new 1())).apply("foo");
      "foo".toString();
   }

Expectation

That both the normal and poly function both beta reduce

@Katrix Katrix added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 4, 2022
@odersky
Copy link
Contributor

odersky commented Sep 5, 2022

You mean, the reducer should do type inference on the argument? That would be a lot to ask!

@prolativ prolativ added itype:enhancement area:metaprogramming:quotes Issues related to quotes and splices and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 5, 2022
@Katrix
Copy link
Contributor Author

Katrix commented Sep 5, 2022

Not sure I understand. Are you saying it does work in some cases already, and I just did it wrong?

@odersky
Copy link
Contributor

odersky commented Sep 5, 2022

No, not at all. Just that it would be a rather big jump from what we have to this, and something that would tear down some important modularity barriers we have in the implementation.

@Katrix
Copy link
Contributor Author

Katrix commented Sep 6, 2022

Managed to work around this for now using value classes and dependent function types instead of poly times. Seems these beta reduce just fine.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 9, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 9, 2023
@nicolasstucki nicolasstucki self-assigned this Feb 9, 2023
odersky added a commit that referenced this issue Feb 12, 2023
Beta-reduce directly applied PolymorphicFunction such as

```scala
([Z] => (arg: Z) => { def a: Z = arg; a }).apply[Int](2)
```
into
```scala
type Z = Int
val arg = 2
def a: Z = arg
a
```

Apply this beta reduction in the `BetaReduce` phase and
`Expr.betaReduce`. Also, refactor the beta-reduce logic to avoid code
duplication.

Fixes #15968
@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants