[plt-scheme] problem with backtracking order of nested ambs

From: Thomas Chust (chust at web.de)
Date: Tue Oct 6 09:47:25 EDT 2009

2009/10/5 keydana at gmx.de <keydana at gmx.de>:
> [...]
>  (let ((x (amb 3 5 4)))
>   (let ((result (amb (begin (assert (even? x)) x) 77)))
>     result))
>
> I would need amb to first try all of '(3 5 4) and only after that, take 77
> as last possible way out.
> [...]

Hello Sigrid,

since the evaluation order of amb itself cannot be modified easily,
you have to reverse the dynamic nesting order of the two ambivalent
expressions. This can for example be done by "physically" moving the
inner out outward:

  (let ((result
           (amb
             (let ((x (amb 3 5 4)))
               (assert (even? x))
               x)
             77)))
    result)

or by wrapping the outer one in a procedure:

  (let ((x (lambda () (amb 3 5 4))))
    (let ((result (amb (let ((x (x))) (assert (even? x)) x) 77)))
      result))

Ciao,
Thomas


-- 
All these theories, diverse as they are, have two things in common: They
explain the observed facts, and they are completely and utterly wrong.
                               -- Terry Pratchett, "The Light Fantastic"


Posted on the users mailing list.