Skip to content

Lazy handler for Amb #10

@atennapel

Description

@atennapel

Is there a way to implement a handler similar to ambList that returns a stream, lazily returning the results?
My naive attempt did not work, but will result in an infinite loop:

def ambStream[R](prog: Amb => Control[R]): Control[Stream[R]] =
  new Handler[Stream[R]] with Amb {
    def flip() = use { resume =>
      for {
        x <- resume(true)
        y <- resume(false)
      } yield x ++ y
    }
  } handle { amb => prog(amb).map(Stream(_)) }

def flip(implicit amb: Amb): Control[Boolean] = amb.flip()

def from(n: Int)(implicit amb: Amb): Control[Int] = for {
  x <- flip
  y <- if (x) pure(n) else from(n + 1)
} yield y

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions