((tags "egg") (section 2 "amb" (p "The Non-Deterministic Backtracking Ambivalence Operator") (toc)) (section 2 "Documentation" (p "The " (tt "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.") (p "Installs the " (i "amb") " and " (i "amb-extras") " extensions.") (section 3 "amb" (def (sig (syntax "(amb EXPRESSION...) => TOP")) (p "If the expression has any parameters, the first one of them is evaluated and the result is returned. If a subsequent occurrence of " (tt "amb") " fails, though, backtracking may cause the second of the given expressions to be selected for evaluation, then the third and so forth until the whole program does not fail if at all possible.") (p "The form " (tt "(amb)") " always fails."))) (section 3 "amb/random" (def (sig (syntax "(amb/random EXPRESSION...) => TOP")) (p "Works like " (tt "amb") " but the parameters are not selected in sequence but randomly. None of them is selected more than once, though."))) (section 3 "amb-find" (def (sig (syntax "(amb-find EXPRESSION [FAILURE-VALUE]) => OBJECT")) (p "Evaluates " (tt "EXPRESSION") " returning its value if successful (possibly after backtracking).") (p "If " (tt "EXPRESSION") " cannot be evaluated successfully and the expression tree is exhausted, " (tt "FAILURE-VALUE") " is evaluated and the result is returned instead.") (p "If no " (tt "FAILURE-VALUE") " is specified, an exception occurs. See the " (tt "amb-failure-continuation") " parameter below for a description of the exception."))) (section 3 "amb-collect" (def (sig (syntax "(amb-collect EXPRESSION) => LIST")) (p "Evaluates " (tt "EXPRESSION") " and performs backtracking repeatedly until all possible values for it have been accumulated in a list, which is returned."))) (section 3 "amb-assert" (def (sig (syntax "(amb-assert OK?)")) (p "Evaluates " (tt "OK?") " and fails if it is " (tt "#f") "."))) (section 3 "amb-failure-continuation" (def (sig (procedure "(amb-failure-continuation) => STATUS-VARIABLE") (procedure "(amb-failure-continuation STATUS-VARIABLE)")) (p "Seen in a global context, the " (tt "amb") " operator transforms the whole program that contains it into a depth first search for return values from " (tt "amb") " forms that will not cause failure.") (p "This is realized using a backtracking system that invokes previously stored continuations whenever an " (tt "amb") " expression fails. The " (tt "amb-failure-continuation") " parameter is the status variable for this system.") (p "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 " (tt "(exn amb)") " (except when a " (tt "amb-collect") " statement is being processed, where the parameter will point to a procedure signalling " (tt "amb-collect") " that there are no more backtracking options available).") (p "In all other cases this parameter is set to a procedure of no arguments that causes backtracking to the next possible alternative in the " (i "tree") ".") (p "If you want to restrict the scope of backtracking to something smaller than the whole past program, use " (tt "amb-find") " or " (tt "amb-collect") " which restore this parameter to its original value when they are done evaluating the expressions they were given."))) (section 3 "amb-thunks" (def (sig (procedure "(amb-thunks THUNKS) => TOP")) (p "The backend of " (tt "amb") ".") (p (tt "amb") " wraps all its parameters into thunks and passes a list of them into this procedure, " (tt "amb/random") " shuffles the list first."))) (section 3 "amb-find-thunk" (def (sig (procedure "(amb-find-thunk THUNK [FAILURE]) => TOP")) (p "The backend of " (tt "amb-find") ".") (p (tt "amb-find") " wraps its parameters into thunks and passes them into this procedure."))) (section 3 "amb-collect-thunk" (def (sig (procedure "(amb-collect-thunk THUNK) => LIST")) (p "The backend of " (tt "amb-collect") ".") (p (tt "amb-collect") " wraps its parameter into a thunk and passes it into this procedure."))) (section 3 ("Extension " (i "amb-extras"))) (section 3 "choose" (def (sig (syntax "(choose LIST) => TOP")) (p (tt "amb/random") " but with a single list argument.")) (section 4 "one-of" (def (sig (syntax "(one-of EXPRESSION) => OBJECT")) (p (tt "amb-find") " synonym."))) (section 4 "all-of" (def (sig (syntax "(all-of EXPRESSION) => LIST")) (p (tt "amb-collect") " synonym.")))) (section 3 "required" (def (sig (syntax "(required EXPRESSION)")) (p (tt "amb-asset") " synonym."))) (section 3 "distinct?" (def (sig (procedure "(distinct? LIST [EQUIVALENCE? equal?]) => BOOLEAN test, 1234")) (p "Is " (tt "LIST") " a list of distinct elements, as determined by " (tt "EQUIVALENCE?") "?")))) (section 2 "Usage" (highlight scheme "(require-library amb) ... (import amb)") (p "or") (highlight scheme "(require-extension amb)")) (section 2 "Examples" (highlight scheme "(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))")) (section 2 "Notes") (section 2 "Requirements" (p (int-link "check-errors" "check-errors"))) (section 2 "Bugs and Limitations") (section 2 "Author123" (p (int-link "/users/thomas chust" "thomas chust"))) (section 2 "Version history" (dl (dt "2.1.0") (dd "Use of \"check-errors\" extension. " (int-link "/users/kon lovett" "kon lovett")) (dt "2.0.0") (dd "Chicken 4 release. " (int-link "/users/kon lovett" "kon lovett")))) (section 2 "License" (p "hai")))