Chapter 14

Chapter 14 - Teach Yourself Scheme in Fixnum Days [Go to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Teach Yourself Scheme in Fixnum Days [Go to first , previous , next page; contents ; index ] Chapter 14 Nondeterminism McCarthy's nondeterministic operator amb [ 25 , 4 , 33 ] is as old as Lisp itself, although it is present in no Lisp. amb takes zero or more expressions, and makes a nondeterministic (or ``ambiguous'') choice among them, preferring those choices that cause the program to converge meaningfully. Here we will explore an embedding of amb in Scheme that makes a depth-first selection of the ambiguous choices, and uses Scheme's control operator call/cc to backtrack for alternate choices. The result is an elegant backtracking strategy that can be used for searching problem spaces directly in Scheme without recourse to an extended language. The embedding recalls the continuation strategies used to implement Prolog- style logic programming [ 16 , 7 ], but is sparer because the operator provided is much like a Scheme boolean operator, does not require special contexts for its use, and does not rely on linguistic infrastructure such as logic variables and unification. 14.1 Description of amb An accessible description of amb and many example uses are found in the premier Scheme textbook SICP [ 1 ]. Informally, amb takes zero or more expressions and nondeterministically returns the value of one of them. Thus, (amb 1 2) may evaluate to 1 or 2. amb called with no expressions has no value to return, and is considered to fail . Thus, (amb) -->ERROR!!! amb tree exhausted (We will examine the wording of the error message later.) In particular, amb is required to return a value if at least one its subexpressions converges, ie, doesn't fail. Thus, file:///C|/Documents%20and%20Settings/Linda%20Graue. (1 of 11) [2/6/2008 11:41:43 AM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Teach Yourself Scheme in Fixnum Days (amb 1 (amb)) and (amb (amb) 1) both return 1. Clearly, amb cannot simply be equated to its first subexpression, since it has to return a non-failing value, if this is at all possible. However, this is not all: The bias for convergence is more stringent than a merely local choice of amb 's subexpressions. amb should furthermore return that convergent value that makes the entire program converge. In denotational parlance, amb is an angelic operator. For example, (amb #f #t) may return either #f or #t , but in the program (if (amb #f #t) 1 (amb)) the first amb -expression must return #t . If it returned #f , the if 's ``else'' branch would be chosen, which causes the entire program to fail. 14.2 Implementing amb in Scheme In our implementation of amb , we will favor amb 's subexpressions from left to right. Ie, the first subexpression is chosen, and if it leads to overall failure, the second is picked, and so on. amb s occurring later in the control flow of the program are searched for alternates before backtracking to previous amb s. In other words, we perform a depth-first search of the amb choice tree , and whenever we brush against failure, we backtrack to the most recent node of the tree that offers a further choice. (This
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 4536 taught by Professor Fisler during the Winter '08 term at WPI.

Page1 / 11

Chapter 14 - Teach Yourself Scheme in Fixnum Days [Go to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online