|
| 1 | +import Macros._ |
| 2 | + |
| 3 | +object Test { |
| 4 | + |
| 5 | + def main(args: Array[String]): Unit = { |
| 6 | + |
| 7 | + println(lift(new Show)(3)) |
| 8 | + println(lift(new Eval)(3)) |
| 9 | + println() |
| 10 | + println(lift(new Show)(if (true) 3 else 4)) |
| 11 | + println(lift(new Eval)(if (true) 3 else 4)) |
| 12 | + println() |
| 13 | + println(lift(new Show)(if (if (true) true else false) 3 else 4)) |
| 14 | + println(lift(new Eval)(if (if (true) true else false) 3 else 4)) |
| 15 | + println() |
| 16 | + println(lift(new Show)(if (3 <= 7) 3 else 4)) |
| 17 | + println(lift(new Eval)(if (3 <= 7) 3 else 4)) |
| 18 | + println() |
| 19 | + println(lift(new Show)(if (3 <= 7) 3 + 4 else 5 * 2)) |
| 20 | + println(lift(new Eval)(if (3 <= 7) 3 + 4 else 5 * 2)) |
| 21 | + println() |
| 22 | + println(lift(new Show)(((x: Int) => x + x) (4))) |
| 23 | + println(lift(new Eval)(((x: Int) => x + x) (4))) |
| 24 | + println() |
| 25 | + println(lift(new Show)(((x: Boolean) => if (x) 3 else 4) (true))) |
| 26 | + println(lift(new Eval)(((x: Boolean) => if (x) 3 else 4) (true))) |
| 27 | + println() |
| 28 | + println(lift(new Show)(if (((x: Int) => x <= x)(4)) 3 else 4)) |
| 29 | + println(lift(new Eval)(if (((x: Int) => x <= x)(4)) 3 else 4)) |
| 30 | + println() |
| 31 | + println(lift(new Show)(if (((b: Boolean) => b)(true)) 3 else 4)) |
| 32 | + println(lift(new Eval)(if (((b: Boolean) => b)(true)) 3 else 4)) |
| 33 | + println() |
| 34 | + println(lift(new Show)(((f: Int => Int) => f(4))((x: Int) => x))) |
| 35 | + println(lift(new Eval)(((f: Int => Int) => f(4))((x: Int) => x))) |
| 36 | + println() |
| 37 | + println(lift(new Show)(((x: Int) => Symantics.fix((self: Int => Int) => ((n: Int) => if (n <= 0) 1 else x * self(n + (-1)) )))(3)(25))) |
| 38 | + println(lift(new Eval)(((x: Int) => Symantics.fix((self: Int => Int) => ((n: Int) => if (n <= 0) 1 else x * self(n + (-1)) )))(3)(5))) |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +class Show extends Symantics { |
| 45 | + type Repr[X] = String |
| 46 | + def int(x: Int): Repr[Int] = x.toString |
| 47 | + def bool(x: Boolean): Repr[Boolean] = x.toString |
| 48 | + def lam[A, B](f: Repr[A] => Repr[B]): Repr[A => B] = { |
| 49 | + val i = nextIndex() |
| 50 | + s"(arg$i => ${f(s"arg$i")})" |
| 51 | + } |
| 52 | + def app[A, B](f: Repr[A => B], arg: Repr[A]): Repr[B] = s"$f($arg)" |
| 53 | + def fix[A, B]: (Repr[A => B] => Repr[A => B]) => Repr[A => B] = f => f("FIX") |
| 54 | + def add(x: Repr[Int], y: Repr[Int]): Repr[Int] = s"$x + $y" |
| 55 | + def mult(x: Repr[Int], y: Repr[Int]): Repr[Int] = s"($x) * ($y)" |
| 56 | + def leq(x: Repr[Int], y: Repr[Int]): Repr[Boolean] = s"$x <= $y" |
| 57 | + def ifThenElse[A](cond: Repr[Boolean], thenp: => Repr[A], elsep: => Repr[A]): Repr[A] = s"if ($cond) $thenp else $elsep" |
| 58 | + |
| 59 | + private[this] var idx: Int = 0 |
| 60 | + private def nextIndex(): Int = { |
| 61 | + idx += 1 |
| 62 | + idx |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class Eval extends Symantics { |
| 67 | + type Repr[X] = X |
| 68 | + def int(x: Int): Repr[Int] = x |
| 69 | + def bool(x: Boolean): Repr[Boolean] = x |
| 70 | + def lam[A, B](f: Repr[A] => Repr[B]): Repr[A => B] = f |
| 71 | + def app[A, B](f: Repr[A => B], arg: Repr[A]): Repr[B] = f(arg) |
| 72 | + def fix[A, B]: (Repr[A => B] => Repr[A => B]) => Repr[A => B] = f => { |
| 73 | + def self(n: A): B = f(self)(n) |
| 74 | + self |
| 75 | + } |
| 76 | + def add(x: Repr[Int], y: Repr[Int]): Repr[Int] = x + y |
| 77 | + def mult(x: Repr[Int], y: Repr[Int]): Repr[Int] = x * y |
| 78 | + def leq(x: Repr[Int], y: Repr[Int]): Repr[Boolean] = x <= y |
| 79 | + def ifThenElse[A](cond: Repr[Boolean], thenp: => Repr[A], elsep: => Repr[A]): Repr[A] = if (cond) thenp else thenp |
| 80 | + |
| 81 | +} |
0 commit comments