From 03f00fe232c35189682341e39fac487ed2a70a8c Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Wed, 28 Dec 2011 19:06:49 +0100 Subject: [vpm] __match determines match semantics; virtualization determine match strategy by typing `__match` factored out the interface to generate code in this monad, cleaned up codegen a bit no longer solving a context bound to determine the match strategy and the monad's type constructor it's too expensive don't consider implicits looking for __match implicit search causes HUGE slowdowns -- now the overhead is about 4% compared to just assuming there's no __match in scope to support virtualization&staging, we use the type of `__match.one` as the prototype for how to wrap "pure" types and types "in the monad" pure types T are wrapped as P[T], and T goes into the monad as M[T], if one is defined as: def one[T](x: P[T]): M[T] for staging, P will typically be the Rep type constructor, and type M[T] = Rep[Option[T]] furthermore, naive codegen no longer supplies type information -- type inference will have to work it out optimized codegen still does, of course, and that's enough since we only bootstrap that way TODO: improve the test (currently the condition is not represented) --- test/files/run/virtpatmat_staging.scala | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/files/run/virtpatmat_staging.scala (limited to 'test/files/run/virtpatmat_staging.scala') diff --git a/test/files/run/virtpatmat_staging.scala b/test/files/run/virtpatmat_staging.scala new file mode 100644 index 0000000000..c17b45043b --- /dev/null +++ b/test/files/run/virtpatmat_staging.scala @@ -0,0 +1,52 @@ +trait Intf { + type Rep[+T] + type M[+T] = Rep[Maybe[T]] + + val __match: Matcher + abstract class Matcher { + // runs the matcher on the given input + def runOrElse[T, U](in: Rep[T])(matcher: Rep[T] => M[U]): Rep[U] + + def zero: M[Nothing] + def one[T](x: Rep[T]): M[T] + def guard[T](cond: Rep[Boolean], then: => Rep[T]): M[T] + def isSuccess[T, U](x: Rep[T])(f: Rep[T] => M[U]): Rep[Boolean] // used for isDefinedAt + } + + abstract class Maybe[+A] { + def flatMap[B](f: Rep[A] => M[B]): M[B] + def orElse[B >: A](alternative: => M[B]): M[B] + } + + implicit def proxyMaybe[A](m: M[A]): Maybe[A] + implicit def repInt(x: Int): Rep[Int] + implicit def repBoolean(x: Boolean): Rep[Boolean] + implicit def repString(x: String): Rep[String] + + def test = 7 match { case 5 => "foo" case _ => "bar" } +} + +trait Impl extends Intf { + type Rep[+T] = String + + object __match extends Matcher { + def runOrElse[T, U](in: Rep[T])(matcher: Rep[T] => M[U]): Rep[U] = ("runOrElse("+ in +", ?" + matcher("?") + ")") + def zero: M[Nothing] = "zero" + def one[T](x: Rep[T]): M[T] = "one("+x.toString+")" + def guard[T](cond: Rep[Boolean], then: => Rep[T]): M[T] = "guard("+cond+","+then+")" + def isSuccess[T, U](x: Rep[T])(f: Rep[T] => M[U]): Rep[Boolean] = ("isSuccess("+x+", ?" + f("?") + ")") + } + + implicit def proxyMaybe[A](m: M[A]): Maybe[A] = new Maybe[A] { + def flatMap[B](f: Rep[A] => M[B]): M[B] = m + ".flatMap(? =>"+ f("?") +")" + def orElse[B >: A](alternative: => M[B]): M[B] = m + ".orElse("+ alternative +")" + } + + def repInt(x: Int): Rep[Int] = x.toString + def repBoolean(x: Boolean): Rep[Boolean] = x.toString + def repString(x: String): Rep[String] = x +} + +object Test extends Impl with Intf with App { + println(test) +} -- cgit v1.2.3