Permalink
Browse files

more checkpoint

  • Loading branch information...
1 parent 89cc2bc commit 98d48afa1785fb94ed5073f9a3df76c7f45319cc @jaked committed Apr 26, 2011
@@ -1,4 +1,4 @@
-class Bridge(Logic: Logic) {
+class Bridge(val Logic: Logic) {
import Logic._
object Person extends Enumeration {
@@ -14,11 +14,11 @@ class Bridge(Logic: Logic) {
lightOnLeft: Boolean,
timeRemaining: Int)
- private def chooseTwo(list: List[Person]): T[(Person,Person)] =
+ def chooseTwo(list: List[Person]): T[(Person,Person)] =
for { p1 <- or(list); p2 <- or(list); if p1 < p2 }
yield (p1, p2)
- private def next(state: State): T[State] = {
+ def next(state: State): T[State] = {
if (state.lightOnLeft) {
for {
(p1, p2) <- chooseTwo(state.left)
@@ -40,18 +40,16 @@ class Bridge(Logic: Logic) {
}
}
- private def tree(path: List[State]): T[List[State]] =
+ def tree(path: List[State]): T[List[State]] =
unit(path) |
(for {
state <- next(path.head)
path <- tree(state :: path)
} yield path)
- def search(n: Int): List[List[State]] = {
+ def search: T[List[State]] = {
val start = List(State(Person.all, true, 60))
- val t =
- for { path <- tree(start); if path.head.left == Nil }
- yield path
- run(t, n)
+ for { path <- tree(start); if path.head.left == Nil }
+ yield path
}
}
@@ -16,7 +16,7 @@ trait Logic { L =>
def runAcc(t: T[A], n: Int, acc: List[A]): List[A] =
if (n <= 0) acc.reverse else
split(t) match {
- case None => Nil
+ case None => acc
case Some((a, t)) => runAcc(t, n - 1, a :: acc)
}
runAcc(t, n, Nil)
@@ -33,178 +33,3 @@ trait Logic { L =>
implicit def syntax[A](t: T[A]) = Syntax(t)
}
-
-object LogicList extends Logic {
- type T[A] = List[A]
-
- def fail[A] = Nil
- def unit[A](a: A) = a :: Nil
- def or[A](t1: List[A], t2: => List[A]) = t1 ::: t2
- def apply[A,B](t: List[A], f: A => B) = t.map(f)
- def bind[A,B](t: List[A], f: A => List[B]) = t.flatMap(f)
- def filter[A](t: List[A], p: A => Boolean) = t.filter(p)
- def split[A](t: List[A]) =
- t match {
- case Nil => None
- case h :: t => Some(h, t)
- }
-}
-
-object LogicSFK extends Logic {
- type FK[R] = () => R
- type SK[A,R] = (A, FK[R]) => R
-
- trait T[A] { def apply[R](sk: SK[A,R], fk: FK[R]): R }
-
- def fail[A] =
- new T[A] {
- def apply[R](sk: SK[A,R], fk: FK[R]) = fk()
- }
-
- def unit[A](a: A) =
- new T[A] {
- def apply[R](sk: SK[A,R], fk: FK[R]) = sk(a, fk)
- }
-
- 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 bind[A,B](t: T[A], f: A => T[B]) =
- new T[B] {
- def apply[R](sk: SK[B,R], fk: FK[R]) =
- t(({ (a, fk) => f(a)(sk, fk) }: SK[A,R]), fk)
- }
-
- def apply[A,B](t: T[A], f: A => B) =
- new T[B] {
- def apply[R](sk: SK[B,R], fk: FK[R]) =
- t(({ (a, fk) => sk(f(a), fk) }: SK[A,R]), fk)
- }
-
- 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 split[A](t: T[A]) = {
- def unsplit(fk: FK[Option[(A,T[A])]]): T[A] =
- fk() match {
- case None => fail
- case Some((a, t)) => or(unit(a), t)
- }
- def sk : SK[A,Option[(A,T[A])]] =
- { (a, fk) => Some((a, bind(unit(fk), unsplit))) }
- t(sk, { () => None })
- }
-}
-
-object LogicSKE extends Logic {
- case object Fail extends Exception
-
- type T[A] = (A => Unit) => Unit
-
- def fail[A] = { sk => throw Fail }
-
- def unit[A](a: A) = { sk => sk(a) }
-
- def or[A](t1: T[A], t2: => T[A]) =
- { sk =>
- try { t1(sk) }
- catch { case Fail => t2(sk) }
- }
-
- def bind[A,B](t: T[A], f: A => T[B]) =
- { sk => t(a => f(a)(sk)) }
-
- def apply[A,B](t: T[A], f: A => B) =
- { sk => t(a => sk(f(a))) }
-
- def filter[A](t: T[A], p: A => Boolean) =
- { sk =>
- t(a => if (p(a)) sk(a) else throw Fail)
- }
-
- case object Finish extends Exception
-
- def split[A](t: T[A]) = throw new Exception("unimplemented")
-
- override def run[A](t: T[A], n: Int): List[A] = {
- if (n <= 0) return Nil
- val lb = new scala.collection.mutable.ListBuffer[A]
- def sk(a: A) = {
- lb += a
- throw (if (lb.size < n) Fail else Finish)
- }
- try {
- t(sk)
- throw new Exception("not reached")
- }
- catch { case Fail | Finish => lb.result }
- }
-}
-
-object LogicSKE2 extends Logic {
- case object Fail extends Exception
- class Succeed[A](val a: A, var s: List[() => Unit]) extends Exception
-
- type T[A] = (A => Unit) => Unit
-
- def fail[A] = { sk => throw Fail }
-
- def unit[A](a: A) = { sk => sk(a) }
-
- def or[A](t1: T[A], t2: => T[A]) =
- { sk =>
- try { t1(sk) }
- catch {
- case Fail => t2(sk)
- case e: Succeed[_] => {
- e.s = { () => t2(sk) } :: e.s
- throw e
- }
- }
- }
-
- def bind[A,B](t: T[A], f: A => T[B]) =
- { sk => t(a => f(a)(sk)) }
-
- def apply[A,B](t: T[A], f: A => B) =
- { sk => t(a => sk(f(a))) }
-
- def filter[A](t: T[A], p: A => Boolean) =
- { sk =>
- t(a => if (p(a)) sk(a) else throw Fail)
- }
-
- def split[A](t: T[A]) = {
- def restore(s: List[() => Unit]) {
- s match {
- case Nil => throw Fail
- case cp :: s =>
- try { restore(s) }
- catch {
- case Fail => cp()
- case e: Succeed[_] => {
- e.s = cp :: e.s
- throw e
- }
- }
- }
- }
- def unsplit[A](s: List[() => Unit]): T[A] =
- try { restore(s); throw new Exception("not reached") }
- catch {
- case Fail => fail
- case e: Succeed[A] => or(unit(e.a), bind(unit(e.s), unsplit))
- }
-
- try { t(a => throw new Succeed(a, Nil)); throw new Exception("not reached") }
- catch {
- case Fail => None
- case e: Succeed[A] => Some((e.a, bind(unit(e.s), unsplit)))
- }
- }
-}
@@ -0,0 +1,15 @@
+object LogicList extends Logic {
+ type T[A] = List[A]
+
+ def fail[A] = Nil
+ def unit[A](a: A) = a :: Nil
+ def or[A](t1: List[A], t2: => List[A]) = t1 ::: t2
+ def apply[A,B](t: List[A], f: A => B) = t.map(f)
+ def bind[A,B](t: List[A], f: A => List[B]) = t.flatMap(f)
+ def filter[A](t: List[A], p: A => Boolean) = t.filter(p)
+ def split[A](t: List[A]) =
+ t match {
+ case Nil => None
+ case h :: t => Some(h, t)
+ }
+}
@@ -0,0 +1,50 @@
+object LogicSFK extends Logic {
+ type FK[R] = () => R
+ type SK[A,R] = (A, FK[R]) => R
+
+ trait T[A] { def apply[R](sk: SK[A,R], fk: FK[R]): R }
+
+ def fail[A] =
+ new T[A] {
+ def apply[R](sk: SK[A,R], fk: FK[R]) = fk()
+ }
+
+ def unit[A](a: A) =
+ new T[A] {
+ def apply[R](sk: SK[A,R], fk: FK[R]) = sk(a, fk)
+ }
+
+ 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 bind[A,B](t: T[A], f: A => T[B]) =
+ new T[B] {
+ def apply[R](sk: SK[B,R], fk: FK[R]) =
+ t(({ (a, fk) => f(a)(sk, fk) }: SK[A,R]), fk)
+ }
+
+ def apply[A,B](t: T[A], f: A => B) =
+ new T[B] {
+ def apply[R](sk: SK[B,R], fk: FK[R]) =
+ t(({ (a, fk) => sk(f(a), fk) }: SK[A,R]), fk)
+ }
+
+ 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 split[A](t: T[A]) = {
+ def unsplit(fk: FK[Option[(A,T[A])]]): T[A] =
+ fk() match {
+ case None => fail
+ case Some((a, t)) => or(unit(a), t)
+ }
+ def sk : SK[A,Option[(A,T[A])]] =
+ { (a, fk) => Some((a, bind(unit(fk), unsplit))) }
+ t(sk, { () => None })
+ }
+}
@@ -0,0 +1,63 @@
+object LogicSFKDefunc extends Logic {
+ 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]
+
+ sealed trait 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 SKSplit[A,R](r: (A, FK[R]) => R) extends SK[A,R]
+
+ def fail[A] = Fail()
+ def unit[A](a: A) = Unit(a)
+ def or[A](t1: T[A], t2: => T[A]) = Or(t1, { () => t2 })
+ def bind[A,B](t: T[A], f: A => T[B]) = Bind(t, f)
+ 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](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 applyT[A,R](t: T[A], sk: SK[A,R], fk: FK[R]): R =
+ 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)
+ }
+
+ def applyFK[R](fk: FK[R]): R =
+ fk match {
+ case FKOr(t, sk, fk) => applyT(t(), sk, fk)
+ case FKSplit(r) => r
+ }
+
+ def applySK[A,R](sk: SK[A,R], a: A, fk: FK[R]): R =
+ 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)
+ }
+
+ applyT[A,Option[(A,T[A])]](
+ t,
+ SKSplit((a, fk) => Some((a, bind(unit(fk), unsplit)))),
+ FKSplit(None))
+ }
+}
Oops, something went wrong.

0 comments on commit 98d48af

Please sign in to comment.