chickadee » amb

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.

Usage

(import amb)

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 when #f.

amb-random-function

amb-random-functionprocedure
amb-random-function RANDprocedure

The random function parameter. Default is (chicken random) pseudo-random-integer.

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-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-thunks-shuffled

amb-thunks-shuffled THUNKSprocedure

The backend of amb/random.

As amb-thunks, but after shuffling the THUNKS, using the (amb-random-function).

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.

Extension amb-extras

Usage

(import 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

Conjunction of (amb-assert EXPRESSION) ....

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

(import 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)) )

    (required
      ;
      ; They live on different floors.
      (distinct? (list baker cooper fletcher miller smith))
      ;
      ; Baker does not live on the top floor.
      (not (= baker 5))
      ;
      ; Cooper does not live on the bottom floor.
      (not (= cooper 1))
      ;
      ; Fletcher does not live on either the top or the bottom floor.
      (not (= fletcher 5))
      (not (= fletcher 1))
      ;
      ; Miller lives on a higher floor than does Cooper.
      (> miller cooper)
      ;
      ; Smith does not live on a floor adjacent to Fletcher's.
      (not (= (abs (- smith fletcher)) 1))
      ;
      ; Fletcher does not live on a floor adjacent to Cooper's.
      (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

Requirements

srfi-1

Author

Thomas Chust Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/amb

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Version history

3.0.9
No installation of amb-examples.
3.0.8
Fix installation of amb-examples in (chicken-home).
3.0.7
.
3.0.6
More specific procedure types.
3.0.5
.
3.0.4
.
3.0.3
Remove shuffle export.
3.0.2
Remove dependencies, add srfi-1.
3.0.1
.
3.0.0
CHICKEN 5 release.
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.

Contents »