Created
February 24, 2012 11:03
-
-
Save adriaanm/1900166 to your computer and use it in GitHub Desktop.
Introducing virtpatmat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait VirtualizeMatch { | |
type Pure[T] = T | |
type Monad[T] = Option[T] | |
object __match { | |
def zero: Monad[Nothing] = None | |
def one[T](x: Pure[T]): Monad[T] = Some(x) | |
def guard[T](cond: Pure[Boolean], then: => Pure[T]): Monad[T] = if(cond) Some(then) else None | |
def runOrElse[T, U](in: Pure[T])(matcher: Pure[T] => Monad[U]): Pure[U] = matcher(in) getOrElse (throw new MatchError(in)) | |
def isSuccess[T, U](x: Pure[T])(f: Pure[T] => Monad[U]): Pure[Boolean] = !f(x).isEmpty | |
} | |
} | |
class Test extends VirtualizeMatch { | |
case class P(a: Int, b: Int) | |
case class Q(a: R, b: S) | |
case class R(x: Int) | |
case class S(y: Int) | |
val x: P = ??? | |
x match { case P(a, b) => a + b } | |
// __match.runOrElse(x)((x1 => P.unapply(x1).flatMap((x2 => __match.one(x2._1 + x2._2))))) | |
val y: Q = ??? | |
y match { case Q(R(x), S(y)) => x + y } | |
// __match.runOrElse(y)((x1 => Q.unapply(x1).flatMap((x4 => R.unapply(x4._1).flatMap((x5 => S.unapply(x4._2).flatMap((x6 => __match.one(x5 + x6))))))))) | |
} | |
val zero9: Option[Nothing] = None | |
val x1: Q = y | |
var matchRes8: Int = 0 | |
var keepGoing7: Boolean = true | |
val o12: Option[(R, S)] = Q.unapply(x1) | |
if (o12.isEmpty) | |
zero9 | |
else { | |
val o11: Option[Int] = R.unapply(o12.get._1) | |
if (o11.isEmpty) | |
zero9 | |
else { | |
val o10: Option[Int] = S.unapply(o12.get._2) | |
if (o10.isEmpty) | |
zero9 | |
else { | |
keepGoing7 = false | |
matchRes8 = o11.get.+(o10.get) | |
zero9 | |
} | |
} | |
} | |
if (keepGoing7) | |
throw new MatchError(x1) | |
else | |
matchRes8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment