#!/usr/bin/env ruby class Amb class ExhaustedError < RuntimeError; end def initialize @back = Array(lambda { raise ExhaustedError, "amb tree exhausted" }) end def choose(*choices) choices.each do |choice| callcc do |current_continuation| @back << current_continuation return choice end end end def assert(cond) failure unless cond end private def failure @back.pop.call end end require 'rubygems'; require 'ruby-debug'; debugger; amb = Amb.new x = amb.choose(1,2,3,4) amb.assert((x % 2) == 0) amb.assert x > 3 puts x