|
| 1 | +import scala.quoted._ |
| 2 | + |
| 3 | +object Test extends App { |
| 4 | + |
| 5 | + sealed trait HList |
| 6 | + sealed trait HNil extends HList |
| 7 | + sealed trait ::[E, T <: HList] extends HList |
| 8 | + |
| 9 | + type STM[A, L <: HList] = L match { |
| 10 | + case HNil => Expr[A] |
| 11 | + case e :: rs => (Expr[A] => STM[e, rs]) => STM[e, rs] |
| 12 | + } |
| 13 | + |
| 14 | + type Stm[A, L <: HList] = L match { |
| 15 | + case HNil => A |
| 16 | + case e :: rs => (A => Stm[e, rs]) => Stm[e, rs] |
| 17 | + } |
| 18 | + |
| 19 | + trait Effects[L <: HList] { |
| 20 | + def reify[A] given Type[A]: STM[A, L] => Expr[Stm[A, L]] |
| 21 | + def reflect[A] given Type[A]: Expr[Stm[A, L]] => STM[A, L] |
| 22 | + } |
| 23 | + delegate empty for Effects[HNil] { |
| 24 | + def reify[A] given Type[A] = m => m |
| 25 | + def reflect[A] given Type[A] = m => m |
| 26 | + } |
| 27 | + // for reify, we need type tags for E and also strangely for L. |
| 28 | + implicit def cons [E, L <: HList] given Effects[L] given Type[E] given Type[L] given QuoteContext: Effects[E :: L] = new Effects[E :: L] { |
| 29 | + def reify[A] given Type[A] = m => '{ k => ${ Effects[L].reify[E] { m( a => Effects[L].reflect[E]('k(a))) } }} |
| 30 | + def reflect[A] given Type[A] = m => k => Effects[L].reflect[E] { m('{ a => ${ Effects[L].reify[E]( k('a)) } })} |
| 31 | + } |
| 32 | + def Effects[L <: HList] given Effects[L]: Effects[L] = the[Effects[L]] |
| 33 | + |
| 34 | + type RS = Boolean :: RS2 |
| 35 | + type RS2 = Int :: String :: HNil |
| 36 | + |
| 37 | + def m given QuoteContext: STM[Int, RS] = k => k('{42}) |
| 38 | + |
| 39 | + implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader) |
| 40 | + |
| 41 | + withQuoteContext { |
| 42 | + println(Effects[RS].reify[Int] { m }.show) |
| 43 | + |
| 44 | + val effects = cons[Boolean, RS2] given (cons[Int, String :: HNil] given (cons[String, HNil] given empty)) |
| 45 | + println(effects.reify[Int] { m }.show) |
| 46 | + |
| 47 | + val res : Expr[Stm[Int, RS]] = '{ k => ${ Effects[RS2].reify[Boolean] { m(a => Effects[RS2].reflect[Boolean]('k(a))) }}} |
| 48 | + println(res.show) |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments