You are looking at historical revision 19601 of this page. It may differ significantly from its current revision.
The Non-Deterministic Backtracking Ambivalence Operator
The amb operator is a nice toy and sometimes a useful tool for lightweight logic programming. Its implementation is also a good exercise in the handling of continuations.
Installs the amb and amb-extras extensions.
amb[syntax] (amb EXPRESSION...) => TOP
If the EXPRESSION has any parameters, the first one of them is evaluated and the result is returned. If a subsequent occurrence of amb fails, though, backtracking may cause the second of the given EXPRESSION... to be selected for evaluation, then the third and so forth until the whole program does not fail if at all possible.
The form (amb) always fails.
amb/random[syntax] (amb/random EXPRESSION...) => TOP
Works like amb but the parameters are not selected in sequence but randomly. None of them is selected more than once, though.
amb-find[syntax] (amb-find EXPRESSION [FAILURE-VALUE]) => *
Evaluates EXPRESSION returning its value if successful (possibly after backtracking).
If EXPRESSION cannot be evaluated successfully and the expression tree is exhausted, FAILURE-VALUE is evaluated and the result is returned instead.
If no FAILURE-VALUE is specified, an exception occurs. See the amb-failure-continuation parameter below for a description of the exception.
amb-collect[syntax] (amb-collect EXPRESSION) => list
Evaluates EXPRESSION and performs backtracking repeatedly until all possible values for it have been accumulated in a list, which is returned.
amb-assert[syntax] (amb-assert OK?)
Evaluates OK? and fails if it is #f.
amb-failure-continuation[procedure] (amb-failure-continuation) => STATUS-VARIABLE
[procedure] (amb-failure-continuation STATUS-VARIABLE)
Seen in a global context, the amb operator transforms the whole program that contains it into a depth first search for return values from amb forms that will not cause failure.
This is realized using a backtracking system that invokes previously stored continuations whenever an amb expression fails. The amb-failure-continuation parameter is the status variable for this system.
At the start of the program, or when no further backtracking options are available, this is set to a procedure of no arguments that raises an exception condition (exn amb) (except when a amb-collect statement is being processed, where the parameter will point to a procedure signalling amb-collect that there are no more backtracking options available).
In all other cases this parameter is set to a procedure of no arguments that causes backtracking to the next possible alternative in the tree.
If you want to restrict the scope of backtracking to something smaller than the whole past program, use amb-find or amb-collect which restore this parameter to its original value when they are done evaluating the expressions they were given.
amb-thunks[procedure] (amb-thunks THUNKS) => TOP
The backend of amb.
amb wraps all its parameters into thunks and passes a list of them into this procedure, amb/random shuffles the list first.
amb-find-thunk[procedure] (amb-find-thunk THUNK [FAILURE]) => TOP
The backend of amb-find.
amb-find wraps its parameters into thunks and passes them into this procedure.
amb-collect-thunk[procedure] (amb-collect-thunk THUNK) => list
The backend of amb-collect.
amb-collect wraps its parameter into a thunk and passes it into this procedure.
choose[syntax] (choose LIST) => TOP
amb/random but with a single list argument.
one-of[syntax] (one-of EXPRESSION) => *
all-of[syntax] (all-of EXPRESSION) => list
required[syntax] (required EXPRESSION)
distinct?[procedure] (distinct? LIST [=? equal?]) => boolean
Is LIST a list of distinct elements, as determined by =??
(require-library amb) ... (import amb)
(require-extension amb amb-extras) ;; Baker, Cooper, Fletcher, Miller, and Smith live on different ;; floors of an apartment house that contains only five floors. Baker ;; does not live on the top floor. Cooper does not live on the bottom ;; floor. Fletcher does not live on either the top or the bottom ;; floor. Miller lives on a higher floor than does Cooper. Smith does not ;; live on a floor adjacent to Fletcher's. Fletcher does not live on a ;; floor adjacent to Cooper's. ;; ;; Where does everyone live? (define (solve-dwelling-puzzle) (let ((baker (amb 1 2 3 4 5)) (cooper (amb 1 2 3 4 5)) (fletcher (amb 1 2 3 4 5)) (miller (amb 1 2 3 4 5)) (smith (amb 1 2 3 4 5))) ;; They live on different floors. (required (distinct? (list baker cooper fletcher miller smith))) ;; Baker does not live on the top floor. (required (not (= baker 5))) ;; Cooper does not live on the bottom floor. (required (not (= cooper 1))) ;; Fletcher does not live on either the top or the bottom floor. (required (not (= fletcher 5))) (required (not (= fletcher 1))) ;; Miller lives on a higher floor than does Cooper. (required (> miller cooper)) ;; Smith does not live on a floor adjacent to Fletcher's. (required (not (= (abs (- smith fletcher)) 1))) ;; Fletcher does not live on a floor adjacent to Cooper's. (required (not (= (abs (- fletcher cooper)) 1))) `((baker ,baker) (cooper ,cooper) (fletcher ,fletcher) (miller ,miller) (smith ,smith))) ) (solve-dwelling-puzzle) ;=> ((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1))
- The use of amb, or amb/random, without amb-collect or amb-find can be confusing. See the Chicken-users mailing list for the subject "Using the amb egg".
Copyright (C) 2009 Thomas Chust <email@example.com>.. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.