amb

The Non-Deterministic Backtracking Ambivalence Operator

  1. amb
  2. Documentation
    1. Usage
    2. amb-random-function
    3. amb-failure-continuation
    4. amb
    5. amb/random
    6. amb-find
    7. amb-collect
    8. amb-assert
    9. amb-thunks
    10. amb-thunks-shuffled
    11. amb-find-thunk
    12. amb-collect-thunk
      1. shuffle
    13. Extension amb-extras
      1. Usage
      2. amb1
      3. choose
      4. one-of
      5. all-of
      6. required
      7. distinct?
      8. count-member
      9. list-constantly
  3. Examples
  4. Notes
  5. Requirements
  6. Author
  7. Version history
  8. License

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

[procedure] (amb-random-function) => procedure
[procedure] (amb-random-function RAND)

The random function paramerter. Default is ''extras' unit random.

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

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

[procedure] (amb-thunks-shuffled THUNKS) => TOP

As amb-thunks after {{(shuffle THUNKS

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.

shuffle

[procedure] (shuffle LS RAND) => list

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

[syntax] (amb1 LS) => TOP

amb but with a single list argument LS.

choose

[syntax] (choose LS) => TOP

amb/random but with a single list argument LS.

one-of

[syntax] (one-of EXPRESSION) => *

amb-find synonym.

all-of

[syntax] (all-of EXPRESSION) => list

amb-collect synonym.

required

[syntax] (required EXPRESSION)

amb-assert synonym.

distinct?

[procedure] (distinct? LS [eql? equal?]) => boolean

Is, using eql? for comparison, LS a list of distinct elements?

count-member

[procedure] (count-member OBJECT LS [eql? equal?]) => integer

Returns, using eql? for comparison, the number of times OBJECT referenced in LS.

list-constantly

[procedure] (list-constantly LS) => list

Returns a new list where each element, ELT, of LS is represented by (constantly ELT).

Examples

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

Notes

Requirements

check-errors miscsyntaxs condition-utils

setup-helper

Author

Thomas Chust Kon Lovett

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.