source: project/release/3/amb/doc.scm @ 7753

Last change on this file since 7753 was 112, checked in by Thomas Chust, 15 years ago

v1.2.0: Non-determinism now optional, better controllability of scope

  • amb is now called amb/random
  • amb does the same as before except it doesn't shuffle the list of expressions any more
  • an amb-find macro has been added
File size: 7.0 KB
Line 
1(use eggdoc htmlprag sxml-tools)
2
3(define (pull-demo-code-in)
4  (write-shtml-as-html
5   (car
6    ((sxpath '(// pre))
7     (call-with-input-file "amb-demo.html" html->sxml)))))
8
9(define doc
10  `((eggdoc:begin
11     (name "amb")
12     (description (p "The non-deterministic backtracking ambivalence operator"))
13     (author (url "http://www.chust.org/" "Thomas Chust"))
14     (history
15      (version "1.2.0" "Non-determinism now optional, better controllability of scope")
16      (version "1.1.1" "Fixed bug in amb.setup [Thanks to Kon Lovett]")
17      (version "1.1.0" "Renamed " (tt "bag-of") " to " (tt "amb-collect") " and added " (tt "amb-assert"))
18      (version "1.0.0" "Initial release"))
19     
20     (usage)
21     (download "amb.egg")
22     
23     (documentation
24      (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.")
25
26      (subsection
27       "Normal interface"
28       (group
29        (macro
30         "(amb expr ...) => <top>"
31         (p "In the form " (tt "(amb)") " the expression always fails.")
32         (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.")
33         (p "The backtracking mechanism is described along with the " (tt "amb-failure-continuation") " parameter below."))
34        (macro
35         "(amb/random expr ...) => <top>"
36         (p "Works like " (tt "amb") " but the parameters are not selected in sequence but randomly. None of them is selected more than once, though."))
37        (macro
38         "(amb-find expr #!optional failure-value) => <top>"
39         (p "Evaluates " (tt "expr") " returning its value if successful (possibly after backtracking).")
40         (p "If " (tt "expr") " cannot be evaluated successfully and the expression tree is exhausted, " (tt "failure-value") " is evaluated and the result is returned instead. If no " (tt "failure-value") " is specified, an exception occurs. See the " (tt "amb-failure-continuation") " parameter below for a description of the exception."))
41        (macro
42         "(amb-collect expr) => <list>"
43         (p "Evaluates " (tt "expr") " and performs backtracking repeatedly until all possible values for it have been accumulated in a list, which is returned."))
44        (macro
45         "(amb-assert ok?) => <void>"
46         (p "Evaluates " (tt "ok?") " and fails if it is " (tt "#f") "."))))
47
48      (subsection
49       "Additional stuff exported by amb-base"
50       (group
51        (definition
52          (signatures
53           (signature "parameter" "amb-failure-continuation"))
54          (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.")
55          (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.")
56          (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 throws an exception of the composite " (tt "(exn amb)") " kind (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).")
57          (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 \"tree\".")
58          (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."))
59        (procedure
60         "(amb-thunks <list of procedures>) => <top>"
61         (p "The backend of " (tt "amb") ". " (tt "amb") " wraps all its parameters into thunks and passes a list of them into this procedure, " (tt "amb/random") " shuffles the list first."))
62        (procedure
63         "(amb-find-thunk <procedure> #!optional <procedure>) => <top>"
64         (p "The backend of " (tt "amb-find") ". " (tt "amb-find") " wraps its parameters into thunks and passes them into this procedure."))
65        (procedure
66         "(amb-collect-thunk <procedure>) => <list>"
67         (p "The backend of " (tt "amb-collect") ". " (tt "amb-collect") " wraps its parameter into a thunk and passes it into this procedure."))))
68
69      (examples
70       (p "The following code is a rewrite of an example from the book \"Teach Yourself Scheme in Fixnum Days\" by Dorai Sitaram. The book gives the following problem setting:")
71       (em
72        (p "The Kalotans are a tribe with a peculiar quirk. Their males always tell the truth. Their females never make two consecutive true statements, or two consecutive untrue statements.")
73        (p "An anthropologist (let's call him Worf) has begun to study them. Worf does not yet know the Kalotan language. One day, he meets a Kalotan (heterosexual) couple and their child Kibi. Worf asks Kibi: \"Are you a boy?\" Kibi answers in Kalotan, which of course Worf doesn't understand.")
74        (p "Worf turns to the parents (who know English) for explanation. One of them says: \"Kibi said: 'I am a boy.'\" The other adds: \"Kibi is a girl. Kibi lied.\"")
75        (p "Solve for the sex of the parents and Kibi."))
76
77       (p "So here is the solution:")
78       ,pull-demo-code-in)
79     
80      (license
81       "Copyright (c) 2005, Thomas Chust <chust@web.de>.  All rights reserved.
82
83Redistribution and use in source and binary forms, with or without
84modification, are permitted provided that the following conditions are met:
85
86  Redistributions of source code must retain the above copyright notice,
87  this list of conditions and the following disclaimer. Redistributions in
88  binary form must reproduce the above copyright notice, this list of
89  conditions and the following disclaimer in the documentation and/or
90  other materials provided with the distribution. Neither the name of the
91  author nor the names of its contributors may be used to endorse or
92  promote products derived from this software without specific prior
93  written permission.
94
95THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS
96IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
97THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
98PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
99CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
100EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
101PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
102PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
103LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
104NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
105SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.")))))
106
107(eggdoc->html doc (eggdoc:make-stylesheet doc))
Note: See TracBrowser for help on using the repository browser.