//设置package包名称以及导入依赖的类
package acumen
package interpreters
package reference2012
import Interpreter._
import acumen.Errors._
import scala.collection.immutable.Set
def promote(f: Store => Store) : Eval[Unit] =
mkEval (s => ((), Set.empty, Set.empty, f(s)))
def pure[A](x:A) : Eval[A] = mkEval(s => (x, Set.empty, Set.empty, s))
def pass = pure(())
def getStore : Eval[Store] = mkEval(s => (s, Set.empty, Set.empty, s))
def modifyStore(f:Store => Store) : Eval[Unit] =
mkEval(s => ((), Set.empty, Set.empty, f(s)))
def logCId(id:CId) : Eval[Unit] =
mkEval (s => ((), Set(id), Set.empty, s))
def logReparent(o:CId, parent:CId) : Eval[Unit] =
mkEval(s => ((), Set.empty, Set((o,parent)), s))
def asks[A](f : Store => A) : Eval[A] =
getStore >>= ((s:Store) => pure(f(s)))
def sequence[A](es:List[Eval[A]]) : Eval[List[A]] = es match {
case Nil => pure(Nil)
case (e::es) => for (x <- e; xs <- sequence(es)) yield x::xs
}
def sequence_[A](es:List[Eval[A]]) : Eval[Unit] =
es match {
case Nil => pure(())
case (e::es) => e >> sequence_(es)
}
def mapM[A,B](f:A => Eval[B], xs:List[A]) : Eval[List[B]] =
sequence(xs map f)
def mapM_[A,B](f:A => Eval[B], xs:List[A]) : Eval[Unit] =
sequence_(xs map f)
def foldLeftM[A,B](a:A, f:(A,B) => Eval[A], l:List[B]) : Eval[A] =
l match {
case Nil => pure(a)
case (x::xs) => f(a,x) >>= (a1 => foldLeftM(a1,f,xs))
}
}
请发表评论