[plt-scheme] Puzzle about amb and memory

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Wed Mar 26 15:28:15 EDT 2008

Hi everyone,


I've isolated and resolved a problem in my project.  But I don't quite 
understand why the problem existed!


I've distilled what I ran into.  I've been using Will Farr's excellent 
amb.plt PLaneT library to write nondeterministic programs.  Here's a small 
program that demonstrates the issue.


###########################################################################
#lang scheme
(require (planet "amb.scm" ("wmfarr" "amb.plt" 1 0)))


(define (tester amb-choose)
   (define (get-number/amb)
     (amb-choose (list 1 2 3 4 5 6 7 8 9 10)))
   (with-amb
    (list (get-number/amb)
          (get-number/amb)
          (get-number/amb)
          (get-number/amb)
          (get-number/amb)
          (get-number/amb)
          (get-number/amb))
    (amb)))

(define (test-1)
   (define (amb-choose elts)
     (call/cc
      (lambda (k)
        (apply amb-thunks k (map (lambda (elt) (lambda () elt)) elts)))))

   (tester amb-choose))

(define (test-2)
   (define (broken-amb-choose elts)
     (cond [(empty? elts)
            (amb)]
           [else
            (amb (first elts)
                 (broken-amb-choose (rest elts)))]))
   (tester broken-amb-choose))
###########################################################################


The problem is on the definition of amb-choose, which ambiguously chooses 
an element out of a list.

test-1 has an implementation that runs in bounded space.  test-2 does not. 
You can guess which one I wrote initially... :)

But why does broken-amb-choose consume unbounded memory?


Posted on the users mailing list.