Permalink
Browse files

scala logic part 3

  • Loading branch information...
jaked committed Jun 9, 2011
1 parent 7ece415 commit 6b58f8850abbb424f87f358c630ff10924a2b763
@@ -19,8 +19,10 @@ trait LogicStateT extends LogicState {
{ 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 filter[S,A](t: T[S,A], p: A => Boolean) = {
+ val p2: ((S,A)) => Boolean = { case (_, a) => p(a) }
+ { s: S => Logic.filter(t(s), p2) }
+ }
def split[S,A](s: S, t: T[S,A]) = {
Logic.split(t(s)) match {
@@ -2,10 +2,10 @@ trait Scrolog {
val LogicState: LogicState
import LogicState._
- type P = T[Env,Unit]
+ type G = T[Env,Unit]
class TermSyntax[A](t: Term[A]) {
- def =:=(t2: Term[A]): P =
+ def =:=(t2: Term[A]): G =
for {
env <- get
env2 <- {
@@ -17,13 +17,13 @@ trait Scrolog {
} yield env2
}
- def printEnv: P = for (env <- get) yield Console.println(env)
- def println(m: String): P = unit(Console.println(m))
+ def printEnv: G = for (env <- get) yield Console.println(env)
+ def println(m: String): G = unit(Console.println(m))
implicit def termSyntax[A](t: Term[A]) = new TermSyntax(t)
- implicit def syntax[A](t: P) = LogicState.syntax(t)
+ implicit def syntax[A](t: G) = LogicState.syntax(t)
- def run[A](t: P, n: Int, tm: Term[A]*): List[Seq[Term[A]]] =
+ def run[A](t: G, n: Int, tm: Term[A]): List[Term[A]] =
LogicState.run(Env.empty, t, n)
- .map({ case (e, _) => tm.map(_.subst(e)) })
+ .map({ case (e, _) => tm.subst(e) })
}
@@ -2,10 +2,10 @@ trait Test {
val Scrolog: Scrolog
import Scrolog._
- def member[A](x: Term[A], l: Term[List[A]]): P = {
+ def member[A](x: Term[A], l: Term[List[A]]): G = {
val hd = Evar[A]("hd"); val tl = Evar[List[A]]("tl")
- ConsTerm(hd, tl) =:= l &
- (x =:= hd | member(x, tl))
+ ConsTerm(x, tl) =:= l |
+ (ConsTerm(hd, tl) =:= l & member(x, tl))
}
sealed trait Nat
@@ -48,7 +48,7 @@ trait Test {
case S(n) => STerm(nat2Term(n))
}
- def sum(m: Term[Nat], n: Term[Nat], p: Term[Nat]): P = {
+ def sum(m: Term[Nat], n: Term[Nat], p: Term[Nat]): G = {
val m2 = Evar[Nat]("m"); val p2 = Evar[Nat]("p")
(m =:= Z & n =:= p) |
(m =:= STerm(m2) & p =:= STerm(p2) & sum(m2, n, p2))
@@ -252,7 +252,7 @@ We can rewrite `run` with an accumulator to be tail-recursive:
def runAcc(t: T[A], n: Int, acc: List[A]): List[A] =
if (n <= 0) acc.reverse else
split(t) match {
- case None => acc
+ case None => acc.reverse
case Some((a, t)) => runAcc(t, n - 1, a :: acc)
}
runAcc(t, n, Nil)
Oops, something went wrong.

0 comments on commit 6b58f88

Please sign in to comment.