Permalink
Browse files

checkpoint logic state stuff

  • Loading branch information...
1 parent 3721106 commit 08b5a6d90aa8e7ae9c6cea3cf23d5f4b83719b72 @jaked committed May 24, 2011
@@ -12,32 +12,35 @@ trait Logic { L =>
def or[A](as: List[A]): T[A] =
as.foldRight(fail[A])((a, t) => or(unit(a), t))
+ def seq[A](t1: T[Unit], t2: => T[A]): T[A] =
+ bind(t1, { _: Unit => t2 })
+
def fair_or[A](t1: T[A], t2: => T[A]): T[A] =
- bind(unit(()), { _: Unit =>
+ seq(unit(()), {
split(t1) match {
case None => t2
case Some((a, t)) => or(unit(a), fair_or(t2, t))
}
})
def fair_bind[A,B](t: T[A], f: A => T[B]): T[B] =
- bind(unit(()), { _: Unit =>
+ seq(unit(()), {
split(t) match {
case None => fail
case Some((a, t)) => fair_or(f(a), fair_bind(t, f))
}
})
def ifte[A,B](t: T[A], th: A => T[B], el: T[B]): T[B] =
- bind(unit(()), { _: Unit =>
+ seq(unit(()), {
split(t) match {
case None => el
case Some((a, t)) => or(th(a), bind(t, th))
}
})
def once[A](t: T[A]): T[A] =
- bind(unit(()), { _: Unit =>
+ seq(unit(()), {
split(t) match {
case None => fail
case Some((a, _)) => unit(a)
@@ -12,9 +12,46 @@ trait LogicState { L =>
def get[S]: T[S,S]
def set[S](s: S): T[S, Unit]
+ def seq[S,A](t1: T[S,Unit], t2: => T[S,A]): T[S,A] =
+ bind(t1, { _: Unit => t2 })
+
+ def setUnit[S,A](s: S, a: A): T[S,A] = seq(set(s), unit(a))
+
def or[S,A](as: List[A]): T[S,A] =
as.foldRight(fail[S,A])((a, t) => or(unit(a), t))
+ def fair_or[S,A](t1: T[S,A], t2: => T[S,A]): T[S,A] =
+ bind(get, { s: S =>
+ split(s, t1) match {
+ case None => t2
+ case Some((s, a, t)) => or(setUnit(s, a), fair_or(t2, t))
+ }
+ })
+
+ def fair_bind[S,A,B](t: T[S,A], f: A => T[S,B]): T[S,B] =
+ bind(get, { s: S =>
+ split(s, t) match {
+ case None => fail
+ case Some((s, a, t)) => fair_or(bind(setUnit(s, a), f), fair_bind(t, f))
+ }
+ })
+
+ def ifte[S,A,B](t: T[S,A], th: A => T[S,B], el: T[S,B]): T[S,B] =
+ bind(get, { s: S =>
+ split(s, t) match {
+ case None => el
+ case Some((s, a, t)) => or(bind(setUnit(s, a), th), bind(t, th))
+ }
+ })
+
+ def once[S,A](t: T[S,A]): T[S,A] =
+ bind(get, { s: S =>
+ split(s, t) match {
+ case None => fail
+ case Some((s, a, _)) => setUnit(s, a)
+ }
+ })
+
def run[S,A](s0: S, t: T[S,A], n: Int): List[(S,A)] = {
def runAcc(t: T[S,A], n: Int, acc: List[(S,A)]): List[(S,A)] =
if (n <= 0) acc.reverse else
@@ -16,7 +16,8 @@ object LogicStateSFK extends LogicState {
def or[S,A](t1: T[S,A], t2: => T[S,A]) =
new T[S,A] {
- def apply[R](s: S, sk: SK[S,A,R], fk: FK[R]) = t1(s, sk, { () => t2(s, sk, fk) })
+ def apply[R](s: S, sk: SK[S,A,R], fk: FK[R]) =
+ t1(s, sk, { () => t2(s, sk, fk) })
}
def bind[S,A,B](t: T[S,A], f: A => T[S,B]) =
@@ -33,22 +34,21 @@ object LogicStateSFK extends LogicState {
def filter[S,A](t: T[S,A], p: A => Boolean) =
new T[S,A] {
- def apply[R](s: S, sk: SK[S,A,R], fk: FK[R]) =
- t(s, ({ (s, a, fk) => if (p(a)) sk(s, a, fk) else fk() }: SK[S,A,R]), fk)
+ def apply[R](s: S, sk: SK[S,A,R], fk: FK[R]) = {
+ val sk2 : SK[S,A,R] =
+ { (s, a, fk) => if (p(a)) sk(s, a, fk) else fk() }
+ t(s, sk2, fk)
+ }
}
def split[S,A](s: S, t: T[S,A]) = {
- def stateUnit[S,A](s: S, a: A): T[S,A] =
- new T[S,A] {
- def apply[R](s: S, sk: SK[S,A,R], fk: FK[R]) = sk(s, a, fk)
- }
- def unsplit(r: () => Option[(S,A,T[S,A])]): T[S,A] =
- r() match {
+ def unsplit(fk: FK[Option[(S,A,T[S,A])]]): T[S,A] =
+ fk() match {
case None => fail
- case Some((s, a, t)) => or(stateUnit(s, a), t)
+ case Some((s, a, t)) => or(setUnit(s, a), t)
}
def sk : SK[S,A,Option[(S,A,T[S,A])]] =
- { (s, a, fk) => Some((s, a, unit(fk).flatMap(unsplit))) }
+ { (s, a, fk) => Some((s, a, bind(unit(fk), unsplit))) }
t(s, sk, { () => None })
}
@@ -0,0 +1,94 @@
+object LogicStateSFKDefunc extends LogicState {
+ type O[S,A] = Option[(S,A,T[S,A])]
+
+ sealed trait T[S,A]
+ case class Fail[S,A]() extends T[S,A]
+ case class Unit[S,A](a: A) extends T[S,A]
+ case class Or[S,A](t1: T[S,A], t2: () => T[S,A]) extends T[S,A]
+ case class Bind[S,A,B](t: T[S,A], f: A => T[S,B]) extends T[S,B]
+ case class Apply[S,A,B](t: T[S,A], f: A => B) extends T[S,B]
+ case class Filter[S,A](t: T[S,A], p: A => Boolean) extends T[S,A]
+ case class Unsplit[S,A](fk: FK[O[S,A]]) extends T[S,A]
+ case class Get[S]() extends T[S,S]
+ case class Set[S](s: S) extends T[S,scala.Unit]
+
+ def fail[S,A] = Fail()
+ def unit[S,A](a: A) = Unit(a)
+ def or[S,A](t1: T[S,A], t2: => T[S,A]) = Or(t1, { () => t2 })
+ def bind[S,A,B](t: T[S,A], f: A => T[S,B]) = Bind(t, f)
+ def apply[S,A,B](t: T[S,A], f: A => B) = Apply(t, f)
+ def filter[S,A](t: T[S,A], p: A => Boolean) = Filter(t, p)
+ def get[S] = Get()
+ def set[S](s: S) = Set(s)
+
+ sealed trait FK[R]
+ case class FKOr[S,A,R](t: () => T[S,A], s: S, sk: SK[S,A,R], fk: FK[R]) extends FK[R]
+ case class FKSplit[R](r: R) extends FK[R]
+
+ sealed trait SK[S,A,R]
+ case class SKBind[S,A,B,R](f: A => T[S,B], sk: SK[S,B,R]) extends SK[S,A,R]
+ case class SKApply[S,A,B,R](f: A => B, sk: SK[S,B,R]) extends SK[S,A,R]
+ case class SKFilter[S,A,R](p: A => Boolean, sk: SK[S,A,R]) extends SK[S,A,R]
+ case class SKSplit[S,A,R](r: (S, A, FK[R]) => R) extends SK[S,A,R]
+
+ sealed trait K[R,R2]
+ case class KReturn[R]() extends K[R,R]
+ case class KUnsplit[S,A,R,R2](sk: SK[S,A,R], fk:FK[R], k: K[R,R2]) extends K[O[S,A],R2]
+
+ def split[S,A](s: S, t: T[S,A]) = {
+
+ def applyT[A,R,R2](t: T[S,A], s: S, sk: SK[S,A,R], fk: FK[R], k: K[R,R2]): R2 =
+ t match {
+ case Fail() => applyFK(fk, k)
+ case Unit(a) => applySK(sk, s, a, fk, k)
+ case Or(t1, t2) => applyT(t1, s, sk, FKOr(t2, s, sk, fk), k)
+ case Bind(t, f) => applyT(t, s, SKBind(f, sk), fk, k)
+ case Apply(t, f) => applyT(t, s, SKApply(f, sk), fk, k)
+ case Filter(t, p) => applyT(t, s, SKFilter(p, sk), fk, k)
+ case Unsplit(fk2) => applyFK(fk2, KUnsplit(sk, fk, k))
+ case Get() => applySK(sk.asInstanceOf[SK[S,S,R]], s, s, fk, k)
+ case Set(s) => applySK(sk.asInstanceOf[SK[S,scala.Unit,R]], s, (), fk, k)
+ }
+
+ def applyFK[R,R2](fk: FK[R], k: K[R,R2]): R2 =
+ fk match {
+ case FKOr(t, s, sk, fk) =>
+ applyT(t().asInstanceOf[T[S,Any]],
+ s.asInstanceOf[S],
+ sk.asInstanceOf[SK[S,Any,R]],
+ fk, k)
+ case FKSplit(r) => applyK(k, r)
+ }
+
+ def applySK[A,R,R2](sk: SK[S,A,R], s: S, a: A, fk: FK[R], k: K[R,R2]): R2 =
+ sk match {
+ case SKBind(f, sk) => applyT(f(a), s, sk, fk, k)
+ case SKApply(f, sk) => applySK(sk, s, f(a), fk, k)
+ case SKFilter(p, sk) =>
+ if (p(a)) applySK(sk, s, a, fk, k) else applyFK(fk, k)
+ case SKSplit(rf) => applyK(k, rf(s, a, fk))
+ }
+
+ 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((s, a, t)) =>
+ applyT(or(unit(a), t.asInstanceOf[T[S,Any]]),
+ s.asInstanceOf[S],
+ sk.asInstanceOf[SK[S,Any,Any]],
+ fk, k)
+ }
+ }
+ }
+
+ applyT[A,O[S,A],O[S,A]](
+ t,
+ s,
+ SKSplit((s, a, fk) => Some((s, a, Unsplit(fk)))),
+ FKSplit(None),
+ KReturn())
+ }
+}
@@ -0,0 +1,86 @@
+object LogicStateSFKDefuncTailrec extends LogicState {
+ type O[S,A] = Option[(S,A,T[S,A])]
+
+ type T[S,A] = I
+
+ sealed trait I
+
+ case class Fail() extends I
+ case class Unit(a: Any) extends I
+ case class Or(t1: I, t2: () => I) extends I
+ case class Bind(t: I, f: Any => I) extends I
+ case class Apply(t: I, f: Any => Any) extends I
+ case class Filter(t: I, p: Any => Boolean) extends I
+ case class Unsplit(fk: I) extends I
+ case object Get extends I
+ case class Set(s: Any) extends I
+
+ case class FKOr(t: () => I, s: Any, sk: I, fk: I) extends I
+ case class FKSplit(r: O[Any,Any]) extends I
+
+ case class SKBind(f: Any => I, sk: I) extends I
+ case class SKApply(f: Any => Any, sk: I) extends I
+ case class SKFilter(p: Any => Boolean, sk: I) extends I
+ case class SKSplit(r: (Any, Any, I) => O[Any,Any]) extends I
+
+ case object KReturn extends I
+ case class KUnsplit(sk: I, fk: I, k: I) extends I
+
+ def fail[S,A]: T[S,A] = Fail()
+ def unit[S,A](a: A): T[S,A] = Unit(a)
+ def or[S,A](t1: T[S,A], t2: => T[S,A]): T[S,A] = Or(t1, { () => t2 })
+ def bind[S,A,B](t: T[S,A], f: A => T[S,B]): T[S,B] =
+ Bind(t, f.asInstanceOf[Any => I])
+ def apply[S,A,B](t: T[S,A], f: A => B): T[S,B] =
+ Apply(t, f.asInstanceOf[Any => I])
+ def filter[S,A](t: T[S,A], p: A => Boolean): T[S,A] =
+ Filter(t, p.asInstanceOf[Any => Boolean])
+ def get[S]: T[S,S] = Get
+ def set[S](s: S): T[S,scala.Unit] = Set(s)
+
+ def split[S,A](s: S, t: T[S,A]): O[S,A] = {
+
+ def apply(i: I, s: Any, a: Any, r: O[Any,Any], sk: I, fk: I, k: I): O[Any,Any] =
+ i match {
+ case Fail() => apply(fk, null, null, null, null, null, k)
+ case Unit(a) => apply(sk, s, a, null, null, fk, k)
+ case Or(t1, t2) => apply(t1, s, null, null, sk, FKOr(t2, s, sk, fk), k)
+ case Bind(t, f) => apply(t, s, null, null, SKBind(f, sk), fk, k)
+ case Apply(t, f) => apply(t, s, null, null, SKApply(f, sk), fk, k)
+ case Filter(t, p) => apply(t, s, null, null, SKFilter(p, sk), fk, k)
+ case Unsplit(fk2) => apply(fk2, null, null, null, null, null, KUnsplit(sk, fk, k))
+ case Get => apply(sk, s, s, null, null, fk, k)
+ case Set(s) => apply(sk, s, (), null, null, fk, k)
+
+ case FKOr(t, s, sk, fk) => apply(t(), s, null, null, sk, fk, k)
+ case FKSplit(r) => apply(k, null, null, r, null, null, null)
+
+ case SKBind(f, sk) => apply(f(a), s, null, null, sk, fk, k)
+ case SKApply(f, sk) => apply(sk, s, f(a), null, null, fk, k)
+ case SKFilter(p, sk) =>
+ if (p(a))
+ apply(sk, s, a, null, null, fk, k)
+ else
+ apply(fk, null, null, null, null, null, k)
+ case SKSplit(rf) =>
+ apply(k, null, null, rf(s, a, fk), null, null, null)
+
+ case KReturn => r
+ case KUnsplit(sk, fk, k) => {
+ r match {
+ case None => apply(fk, null, null, null, null, null, k)
+ case Some((s, a, t)) =>
+ apply(or(unit(a), t), s, null, null, sk, fk, k)
+ }
+ }
+ }
+
+ apply(t,
+ s.asInstanceOf[Any],
+ null,
+ null,
+ SKSplit((s, a, fk) => Some((s, a, Unsplit(fk)))),
+ FKSplit(None),
+ KReturn).asInstanceOf[O[S,A]]
+ }
+}
@@ -0,0 +1,34 @@
+trait LogicStateT extends LogicState {
+ val Logic: Logic
+
+ type T[S,A] = S => Logic.T[(S, A)]
+
+ def fail[S,A] = { s: S => Logic.fail }
+ def unit[S,A](a: A) = { s: S => Logic.unit((s, a)) }
+
+ def or[S,A](t1: T[S,A], t2: => T[S,A]) =
+ { s: S => Logic.or(t1(s), t2(s)) }
+
+ def bind[S,A,B](t: T[S,A], f: A => T[S,B]) = {
+ val f2: ((S,A)) => Logic.T[(S,B)] = { case (s, a) => f(a)(s) }
+ { s: S => Logic.bind(t(s), f2) }
+ }
+
+ def apply[S,A,B](t: T[S,A], f: A => B) = {
+ val f2: ((S,A)) => ((S,B)) = { case (s, a) => (s, f(a)) }
+ { s: S => Logic.apply(t(s), f2) }
+ }
+
+ def filter[S,A](t: T[S,A], p: A => Boolean) =
+ { s: S => Logic.filter(t(s), { sa: (S,A) => p(sa._2) }) }
+
+ def split[S,A](s: S, t: T[S,A]) = {
+ Logic.split(t(s)) match {
+ case None => None
+ case Some(((s, a), t)) => Some((s, a, { _ => t }))
+ }
+ }
+
+ def get[S] = { s: S => Logic.unit((s,s)) }
+ def set[S](s: S) = { _: S => Logic.unit((s,())) }
+}
Oops, something went wrong.

0 comments on commit 08b5a6d

Please sign in to comment.