Permalink
Browse files

formatting

  • Loading branch information...
1 parent 98d48af commit 1b571e008fb2b6de50719fadb66fcc0b8a0cd5e5 @jaked committed Apr 28, 2011
View
10 _code/scala-logic/LogicSFK.scala
@@ -16,7 +16,8 @@ object LogicSFK extends Logic {
def or[A](t1: T[A], t2: => T[A]) =
new T[A] {
- def apply[R](sk: SK[A,R], fk: FK[R]) = t1(sk, { () => t2(sk, fk) })
+ def apply[R](sk: SK[A,R], fk: FK[R]) =
+ t1(sk, { () => t2(sk, fk) })
}
def bind[A,B](t: T[A], f: A => T[B]) =
@@ -33,8 +34,11 @@ object LogicSFK extends Logic {
def filter[A](t: T[A], p: A => Boolean) =
new T[A] {
- def apply[R](sk: SK[A,R], fk: FK[R]) =
- t(({ (a, fk) => if (p(a)) sk(a, fk) else fk() }: SK[A,R]), fk)
+ def apply[R](sk: SK[A,R], fk: FK[R]) = {
+ val sk2: SK[A,R] =
+ { (a, fk) => if (p(a)) sk(a, fk) else fk() }
+ t(sk2, fk)
+ }
}
def split[A](t: T[A]) = {
View
75 _code/scala-logic/LogicSFKDefunc.scala
@@ -1,22 +1,34 @@
object LogicSFKDefunc extends Logic {
+ type O[A] = Option[(A,T[A])]
+
sealed trait T[A]
case class Fail[A]() extends T[A]
case class Unit[A](a: A) extends T[A]
case class Or[A](t1: T[A], t2: () => T[A]) extends T[A]
case class Bind[A,B](t: T[A], f: A => T[B]) extends T[B]
case class Apply[A,B](t: T[A], f: A => B) extends T[B]
case class Filter[A](t: T[A], p: A => Boolean) extends T[A]
+ case class Unsplit[A](fk: FK[O[A]]) extends T[A]
sealed trait FK[R]
- case class FKOr[A,R](t: () => T[A], sk: SK[A,R], fk: FK[R]) extends FK[R]
+ case class FKOr[A,R](t: () => T[A], sk: SK[A,R], fk: FK[R])
+ extends FK[R]
case class FKSplit[R](r: R) extends FK[R]
sealed trait SK[A,R]
- case class SKBind[A,B,R](f: A => T[B], sk: SK[B,R]) extends SK[A,R]
- case class SKApply[A,B,R](f: A => B, sk: SK[B,R]) extends SK[A,R]
- case class SKFilter[A,R](p: A => Boolean, sk: SK[A,R]) extends SK[A,R]
+ case class SKBind[A,B,R](f: A => T[B], sk: SK[B,R])
+ extends SK[A,R]
+ case class SKApply[A,B,R](f: A => B, sk: SK[B,R])
+ extends SK[A,R]
+ case class SKFilter[A,R](p: A => Boolean, sk: SK[A,R])
+ extends SK[A,R]
case class SKSplit[A,R](r: (A, FK[R]) => R) extends SK[A,R]
+ sealed trait K[R,R2]
+ case class KReturn[R]() extends K[R,R]
+ case class KUnsplit[A,R,R2](sk: SK[A,R], fk:FK[R], k: K[R,R2])
+ extends K[O[A],R2]
+
def fail[A] = Fail()
def unit[A](a: A) = Unit(a)
def or[A](t1: T[A], t2: => T[A]) = Or(t1, { () => t2 })
@@ -25,39 +37,50 @@ object LogicSFKDefunc extends Logic {
def filter[A](t: T[A], p: A => Boolean) = Filter(t, p)
def split[A](t: T[A]) = {
- def unsplit[A](fk: FK[Option[(A, T[A])]]): T[A] =
- applyFK(fk) match {
- case None => fail
- case Some((a, t)) => or(unit(a), t)
+
+ def applyK[R,R2](k: K[R,R2], r: R): R2 =
+ k match {
+ case KReturn() => r.asInstanceOf[R2]
+ case KUnsplit(sk, fk, k) => {
+ r match {
+ case None => applyFK(fk, k)
+ case Some((a, t)) => applyT(or(unit(a), t), sk, fk, k)
+ }
+ }
}
- def applyT[A,R](t: T[A], sk: SK[A,R], fk: FK[R]): R =
+ def applyT[A,R,R2]
+ (t: T[A], sk: SK[A,R], fk: FK[R], k: K[R,R2]): R2 =
t match {
- case Fail() => applyFK(fk)
- case Unit(a) => applySK(sk, a, fk)
- case Or(t1, t2) => applyT(t1, sk, FKOr(t2, sk, fk))
- case Bind(t, f) => applyT(t, SKBind(f, sk), fk)
- case Apply(t, f) => applyT(t, SKApply(f, sk), fk)
- case Filter(t, p) => applyT(t, SKFilter(p, sk), fk)
+ case Fail() => applyFK(fk, k)
+ case Unit(a) => applySK(sk, a, fk, k)
+ case Or(t1, t2) => applyT(t1, sk, FKOr(t2, sk, fk), k)
+ case Bind(t, f) => applyT(t, SKBind(f, sk), fk, k)
+ case Apply(t, f) => applyT(t, SKApply(f, sk), fk, k)
+ case Filter(t, p) => applyT(t, SKFilter(p, sk), fk, k)
+ case Unsplit(fk2) => applyFK(fk2, KUnsplit(sk, fk, k))
}
- def applyFK[R](fk: FK[R]): R =
+ def applyFK[R,R2](fk: FK[R], k: K[R,R2]): R2 =
fk match {
- case FKOr(t, sk, fk) => applyT(t(), sk, fk)
- case FKSplit(r) => r
+ case FKOr(t, sk, fk) => applyT(t(), sk, fk, k)
+ case FKSplit(r) => applyK(k, r)
}
- def applySK[A,R](sk: SK[A,R], a: A, fk: FK[R]): R =
+ def applySK[A,R,R2]
+ (sk: SK[A,R], a: A, fk: FK[R], k: K[R,R2]): R2 =
sk match {
- case SKBind(f, sk) => applyT(f(a), sk, fk)
- case SKApply(f, sk) => applySK(sk, f(a), fk)
- case SKFilter(p, sk) => if (p(a)) applySK(sk, a, fk) else applyFK(fk)
- case SKSplit(r) => r(a, fk)
+ case SKBind(f, sk) => applyT(f(a), sk, fk, k)
+ case SKApply(f, sk) => applySK(sk, f(a), fk, k)
+ case SKFilter(p, sk) =>
+ if (p(a)) applySK(sk, a, fk, k) else applyFK(fk, k)
+ case SKSplit(rf) => applyK(k, rf(a, fk))
}
- applyT[A,Option[(A,T[A])]](
+ applyT[A,O[A],O[A]](
t,
- SKSplit((a, fk) => Some((a, bind(unit(fk), unsplit)))),
- FKSplit(None))
+ SKSplit((a, fk) => Some((a, Unsplit(fk)))),
+ FKSplit(None),
+ KReturn())
}
}
View
30 _code/scala-logic/LogicSFKDefuncTailrec.scala
@@ -1,4 +1,6 @@
object LogicSFKDefuncTailrec extends Logic {
+ type O[A] = Option[(A,T[A])]
+
sealed trait T[A]
case class Fail[A]() extends T[A]
case class Unit[A](a: A) extends T[A]
@@ -8,17 +10,15 @@ object LogicSFKDefuncTailrec extends Logic {
case class Filter[A](t: T[A], p: A => Boolean) extends T[A]
case class Unsplit[A](fk: FK) extends T[A]
- type R = Option[(Any,T[Any])]
-
sealed trait FK
case class FKOr(t: () => T[Any], sk: SK, fk: FK) extends FK
- case class FKSplit(r: R) extends FK
+ case class FKSplit(r: O[Any]) extends FK
sealed trait SK
case class SKBind(f: Any => T[Any], sk: SK) extends SK
case class SKApply(f: Any => Any, sk: SK) extends SK
case class SKFilter(p: Any => Boolean, sk: SK) extends SK
- case class SKSplit(r: (Any, FK) => R) extends SK
+ case class SKSplit(r: (Any, FK) => O[Any]) extends SK
sealed trait K
case object KReturn extends K
@@ -31,11 +31,11 @@ object LogicSFKDefuncTailrec extends Logic {
def apply[A,B](t: T[A], f: A => B) = Apply(t, f)
def filter[A](t: T[A], p: A => Boolean) = Filter(t, p)
- def split[A](t2: T[A]): Option[(A,T[A])] = {
+ def split[A](t2: T[A]): O[A] = {
var app = 1
var t = t2.asInstanceOf[T[Any]]
var a: Any = null
- var r: R = null
+ var r: O[Any] = null
var sk: SK = SKSplit((a, fk) => Some((a, Unsplit(fk))))
var fk: FK = FKSplit(None)
var k: K = KReturn
@@ -44,11 +44,14 @@ object LogicSFKDefuncTailrec extends Logic {
(app: @annotation.switch) match {
case 0 => // applyK
k match {
- case KReturn => return r.asInstanceOf[Option[(A,T[A])]]
+ case KReturn => return r.asInstanceOf[O[A]]
case KUnsplit(sk2, fk2, k2) =>
r match {
case None => { app = 2; fk = fk2; k = k2 }
- case Some((a2, t2)) => { app = 1; t = or(unit(a2), t2); sk = sk2; fk = fk2; k = k2 }
+ case Some((a2, t2)) => {
+ app = 1; t = or(unit(a2), t2); sk = sk2
+ fk = fk2; k = k2
+ }
}
}
@@ -60,20 +63,25 @@ object LogicSFKDefuncTailrec extends Logic {
case Bind(t2, f) => { t = t2; sk = SKBind(f, sk) }
case Apply(t2, f) => { t = t2; sk = SKApply(f, sk) }
case Filter(t2, p) => { t = t2; sk = SKFilter(p, sk) }
- case Unsplit(fk2) => { app = 2; k = KUnsplit(sk, fk, k); fk = fk2 }
+ case Unsplit(fk2) => {
+ app = 2; k = KUnsplit(sk, fk, k); fk = fk2
+ }
}
case 2 => // applyFK
fk match {
- case FKOr(t2, sk2, fk2) => { app = 1; t = t2(); sk = sk2; fk = fk2 }
+ case FKOr(t2, sk2, fk2) => {
+ app = 1; t = t2(); sk = sk2; fk = fk2
+ }
case FKSplit(r2) => { app = 0; r = r2 }
}
case 3 => // applySK
sk match {
case SKBind(f, sk2) => { app = 1; t = f(a); sk = sk2 }
case SKApply(f, sk2) => { sk = sk2; a = f(a) }
- case SKFilter(p, sk2) => if (p(a)) sk = sk2 else app = 2
+ case SKFilter(p, sk2) =>
+ if (p(a)) sk = sk2 else app = 2
case SKSplit(rf) => { app = 0; r = rf(a, fk) }
}
}

0 comments on commit 1b571e0

Please sign in to comment.