Skip to content
This repository was archived by the owner on Jun 23, 2020. It is now read-only.
This repository was archived by the owner on Jun 23, 2020. It is now read-only.

cannot cps-transform malformed expression #35

@cristipp

Description

@cristipp

Experimenting on implementing http://www.randomhacks.net/2005/10/11/amb-operator via delimited continuations in Scala. Getting:

Error:(172, 17) cannot cps-transform malformed (possibly in shift/reset placement) expression
          reset {

The code attempt:

"playground" should "eval amb via delimited continuations" in {
    import scala.util.continuations._

    case class Cont[T, U](fn: T => U, arg: T)

    class Evaluator[T, U](init: Cont[T, U]) {
      var queue = Queue.empty[Cont[Any, U]]
      var results = Vector.empty[U]

      lazy val eval: Vector[U] = {
        queue.enqueue(init)
        loop()
        results
      }

      def amb[V](vs: Vector[V]): V @cpsParam[U, Unit] = {
        shift {
          k: (V => U) =>
            vs.foreach {
              v =>
                queue.enqueue(Cont[V, U](k, v))
            }
        }
      }

      @tailrec
      private def loop(): Unit = {
        if (queue.nonEmpty) {
          val (cont, newQueue) = queue.dequeue
          queue = newQueue
          reset {
            val result = cont.fn(cont.arg)
            results = results :+ result
          }
          loop()
        }
      }
    }
  }

Not sure what to do next...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions