Wiki
Download
Manual
Eggs
API
Tests
Bugs
show
edit
history
You can edit this page using
wiki syntax
for markup.
Article contents:
[[tags: egg]] == amb The Non-Deterministic Backtracking Ambivalence Operator [[toc:]] == 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 <enscript language=scheme> (import amb) </enscript> === amb <syntax>(amb EXPRESSION...) -> TOP</syntax> 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</syntax> 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]) -> *</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 <syntax>(amb-collect EXPRESSION) -> list</syntax> 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?)</syntax> Evaluates {{OK?}} and fails when {{#f}}. === amb-random-function <procedure>(amb-random-function) -> procedure</procedure> <procedure>(amb-random-function RAND)</procedure> The random function {{parameter}}. Default is {{(chicken random) pseudo-random-integer}}. === amb-failure-continuation <procedure>(amb-failure-continuation) -> STATUS-VARIABLE</procedure> <procedure>(amb-failure-continuation STATUS-VARIABLE)</procedure> 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</procedure> The backend of {{amb}}. {{amb}} wraps all its parameters into thunks and passes a list of them into this procedure. === amb-thunks-shuffled <procedure>(amb-thunks-shuffled THUNKS) -> TOP</procedure> The backend of {{amb/random}}. As {{amb-thunks}}, but after shuffling the {{THUNKS}}, using the {{(amb-random-function)}}. === amb-find-thunk <procedure>(amb-find-thunk THUNK [FAILURE]) -> TOP</procedure> 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</procedure> The backend of {{amb-collect}}. {{amb-collect}} wraps its parameter into a thunk and passes it into this procedure. === Extension ''amb-extras'' ==== Usage <enscript language=scheme> (import amb-extras) </enscript> ==== amb1 <syntax>(amb1 LS) -> TOP</syntax> {{amb}} but with a single list argument {{LS}}. ==== choose <syntax>(choose LS) -> TOP</syntax> {{amb/random}} but with a single list argument {{LS}}. ==== one-of <syntax>(one-of EXPRESSION) -> *</syntax> {{amb-find}} synonym. ==== all-of <syntax>(all-of EXPRESSION) -> list</syntax> {{amb-collect}} synonym. ==== required <syntax>(required EXPRESSION ...)</syntax> Conjunction of {{(amb-assert EXPRESSION) ...}}. ==== distinct? <procedure>(distinct? LS [eql? equal?]) -> boolean</procedure> Is, using {{eql?}} for comparison, {{LS}} a {{list}} of distinct elements? ==== count-member <procedure>(count-member OBJECT LS [eql? equal?]) -> integer</procedure> Returns, using {{eql?}} for comparison, the number of times {{OBJECT}} referenced in {{LS}}. ==== list-constantly <procedure>(list-constantly LS) -> list</procedure> Returns a new list where each element, {{ELT}}, of {{LS}} is represented by {{(constantly ELT)}}. == Examples <enscript language=scheme> (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)) </enscript> == 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". * The {{amb-examples}} are found in the egg source; ''chicken-install -r amb''. == Requirements [[srfi-1]] == Author [[/users/thomas-chust|Thomas Chust]] [[/users/kon-lovett|Kon Lovett]] == Repository This egg is hosted on the CHICKEN Subversion repository: [[https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/amb|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 [[/egg-svn-checkout|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. [[/users/kon-lovett|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.
Description of your changes:
I would like to authenticate
Authentication
Username:
Password:
Spam control
What do you get when you add 2 to 19?