Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
amb
The Non-Deterministic Backtracking Ambivalence Operator
Documentation
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 the amb-extras extension, plus examples.
Usage
(require-library amb) ... (import amb)
or
(require-extension amb)
amb-random-function
- amb-random-functionprocedure
- amb-random-function RANDprocedure
The random function paramerter. Default is ''extras' unit random.
amb-failure-continuation
- amb-failure-continuationprocedure
- amb-failure-continuation STATUS-VARIABLEprocedure
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
- (amb EXPRESSION...) => TOPsyntax
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
- (amb/random EXPRESSION...) => TOPsyntax
Works like amb but the parameters are not selected in sequence but randomly. None of them is selected more than once, though.
amb-find
- (amb-find EXPRESSION [FAILURE-VALUE]) => *syntax
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
- (amb-collect EXPRESSION) => listsyntax
Evaluates EXPRESSION and performs backtracking repeatedly until all possible values for it have been accumulated in a list, which is returned.
amb-assert
- (amb-assert OK?)syntax
Evaluates OK? and fails if it is #f.
amb-thunks
- amb-thunks THUNKSprocedure
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-thunks-shuffled
- amb-thunks-shuffled THUNKSprocedure
As amb-thunks after {{(shuffle THUNKS
amb-find-thunk
- amb-find-thunk THUNK #!optional FAILUREprocedure
The backend of amb-find.
amb-find wraps its parameters into thunks and passes them into this procedure.
amb-collect-thunk
- amb-collect-thunk THUNKprocedure
The backend of amb-collect.
amb-collect wraps its parameter into a thunk and passes it into this procedure.
shuffle
- shuffle LS RANDprocedure
Returns a new list from the randomly-shuffled elements of LS.
RAND is a procedure returning a random integer in [0, N-1].
Extension amb-extras
Usage
(require-library amb-extras) ... (import amb-extras)
or
(require-extension amb-extras)
amb1
- (amb1 LS) => TOPsyntax
amb but with a single list argument LS.
choose
- (choose LS) => TOPsyntax
amb/random but with a single list argument LS.
one-of
- (one-of EXPRESSION) => *syntax
amb-find synonym.
all-of
- (all-of EXPRESSION) => listsyntax
amb-collect synonym.
required
- (required EXPRESSION)syntax
amb-assert synonym.
distinct?
- (distinct? LS [eql? equal?]) => booleanprocedure
Is, using eql? for comparison, LS a list of distinct elements?
count-member
- (count-member OBJECT LS [eql? equal?]) => integerprocedure
Returns, using eql? for comparison, the number of times OBJECT referenced in LS.
list-constantly
- list-constantly LSprocedure
Returns a new list where each element, ELT, of LS is represented by (constantly ELT).
Examples
(use 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))
Notes
- 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".
Requirements
check-errors miscsyntaxs condition-utils
Author
Version history
- 2.3.0
- Add shuffle & list-constantly.
- 2.2.0
- Add amb-thunks-shuffled & count-member.
- 2.1.7
- Use test.
- 2.1.0
- Use of "check-errors" extension.
- 2.0.0
- Chicken 4 release. Kon Lovett
License
Copyright (C) 2009 Thomas Chust <chust@web.de>.. 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.