|
| 1 | +import scala.quoted._ |
| 2 | + |
| 3 | +def assertBetaReduction(using Quotes)(applied: Expr[Any], expected: String): quotes.reflect.Term = |
| 4 | + import quotes.reflect._ |
| 5 | + val reducedMaybe = Term.betaReduce(applied.asTerm) |
| 6 | + assert(reducedMaybe.isDefined) |
| 7 | + val reduced = reducedMaybe.get |
| 8 | + assert(reduced.show == expected) |
| 9 | + reduced |
| 10 | + |
| 11 | +inline def regularCurriedCtxFun2BetaReduceTest(inline f: Foo ?=> Bar ?=> Int): Unit = |
| 12 | + ${regularCurriedCtxFun2BetaReduceTestImpl('f)} |
| 13 | +def regularCurriedCtxFun2BetaReduceTestImpl(f: Expr[Foo ?=> Bar ?=> Int])(using Quotes): Expr[Int] = |
| 14 | + val expected = |
| 15 | + """|{ |
| 16 | + | val evidence$3: Bar = new Bar() |
| 17 | + | val evidence$2: Foo = new Foo() |
| 18 | + | 123 |
| 19 | + |}""".stripMargin |
| 20 | + val applied = '{$f(using new Foo())(using new Bar())} |
| 21 | + assertBetaReduction(applied, expected).asExprOf[Int] |
| 22 | + |
| 23 | +inline def regularCurriedFun2BetaReduceTest(inline f: Foo => Bar => Int): Int = |
| 24 | + ${regularCurriedFun2BetaReduceTestImpl('f)} |
| 25 | +def regularCurriedFun2BetaReduceTestImpl(f: Expr[Foo => Bar => Int])(using Quotes): Expr[Int] = |
| 26 | + val expected = |
| 27 | + """|{ |
| 28 | + | val b: Bar = new Bar() |
| 29 | + | val f: Foo = new Foo() |
| 30 | + | 123 |
| 31 | + |}""".stripMargin |
| 32 | + val applied = '{$f(new Foo())(new Bar())} |
| 33 | + assertBetaReduction(applied, expected).asExprOf[Int] |
| 34 | + |
| 35 | +inline def typeParamCurriedFun2BetaReduceTest(inline f: [A] => A => [B] => B => Unit): Unit = |
| 36 | + ${typeParamCurriedFun2BetaReduceTestImpl('f)} |
| 37 | +def typeParamCurriedFun2BetaReduceTestImpl(f: Expr[[A] => (a: A) => [B] => (b: B) => Unit])(using Quotes): Expr[Unit] = |
| 38 | + val expected = |
| 39 | + """|{ |
| 40 | + | type Y = Bar |
| 41 | + | val y: Bar = new Bar() |
| 42 | + | type X = Foo |
| 43 | + | val x: Foo = new Foo() |
| 44 | + | typeParamFun2[Y, X](y, x) |
| 45 | + |}""".stripMargin |
| 46 | + val applied = '{$f.apply[Foo](new Foo()).apply[Bar](new Bar())} |
| 47 | + assertBetaReduction(applied, expected).asExprOf[Unit] |
| 48 | + |
| 49 | +inline def regularCurriedFun3BetaReduceTest(inline f: Foo => Bar => Baz => Int): Int = |
| 50 | + ${regularCurriedFun3BetaReduceTestImpl('f)} |
| 51 | +def regularCurriedFun3BetaReduceTestImpl(f: Expr[Foo => Bar => Baz => Int])(using Quotes): Expr[Int] = |
| 52 | + val expected = |
| 53 | + """|{ |
| 54 | + | val i: Baz = new Baz() |
| 55 | + | val b: Bar = new Bar() |
| 56 | + | val f: Foo = new Foo() |
| 57 | + | 123 |
| 58 | + |}""".stripMargin |
| 59 | + val applied = '{$f(new Foo())(new Bar())(new Baz())} |
| 60 | + assertBetaReduction(applied, expected).asExprOf[Int] |
| 61 | + |
| 62 | +inline def typeParamCurriedFun3BetaReduceTest(inline f: [A] => A => [B] => B => [C] => C => Unit): Unit = |
| 63 | + ${typeParamCurriedFun3BetaReduceTestImpl('f)} |
| 64 | +def typeParamCurriedFun3BetaReduceTestImpl(f: Expr[[A] => A => [B] => B => [C] => C => Unit])(using Quotes): Expr[Unit] = |
| 65 | + val expected = |
| 66 | + """|{ |
| 67 | + | type Z = Baz |
| 68 | + | val z: Baz = new Baz() |
| 69 | + | type Y = Bar |
| 70 | + | val y: Bar = new Bar() |
| 71 | + | type X = Foo |
| 72 | + | val x: Foo = new Foo() |
| 73 | + | typeParamFun3[Z, Y, X](z, y, x) |
| 74 | + |}""".stripMargin |
| 75 | + val applied = '{$f.apply[Foo](new Foo()).apply[Bar](new Bar()).apply[Baz](new Baz())} |
| 76 | + assertBetaReduction(applied, expected).asExprOf[Unit] |
0 commit comments