Reputation: 789
I have a question about Exercise 4.54 from Section 4.3.3 of Structure and Interpretation of Computer Programs (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_4.3.3). This exercise concerns the Amb evaluator.
The exercise is the following:
If we had not realized that
require
could be implemented as an ordinary procedure that usesamb
, to be defined by the user as part of a nondeterministic program, we would have had to implement it as a special form. This would require syntax procedures
(define (require? exp) (tagged-list? exp 'require))
(define (require-predicate exp) (cadr exp))
and a new clause in the dispatch in
analyze
((require? exp) (analyze-require exp))
as well the procedure
analyze-require
that handlesrequire
expressions. Complete the following definition ofanalyze-require
.(define (analyze-require exp) (let ((pproc (analyze (require-predicate exp)))) (lambda (env succeed fail) (pproc env (lambda (pred-value fail2) (if <??> <??> (succeed 'ok fail2))) fail))))
I completed it as follows:
(define (analyze-require exp)
(let ((pproc (analyze (require-predicate exp))))
(lambda (env succeed fail)
(pproc env
(lambda (pred-value fail2)
(if (false? pred-value)
(fail2) ;; or (fail)
(succeed 'ok fail2)))
fail))))
My doubt is the following:
I know that, during execution, when the predicate value pred-value
is false, require
should fail; that is, it should call a failure continuation procedure. But I'm a bit confused as to whether it should call (fail)
or (fail2)
. Which one is correct?
Upvotes: 2
Views: 345
Reputation: 1
(fail2)
is correct, well,most of the time it doesn't matter whichever you choose,but for the following conditons:
1.the predicate part contains (amb) such as
(require (amb false true false true true))
2.the predicate part contains (set! <...> <...>)
you can check it out,this is because most of the process's fail-continuation is the same as its caller's except for the above two kinds of processes. Though it sounds some kind of ridiculous.
Upvotes: 0
Reputation: 2741
I think (fail)
is correct.
Here we have got pred-value
, which means pproc
evaluates well for now. But if it is false
value, we treat it as bad as pproc
evaluates wrong. so the continuation here is fail
not fail2
.
A test can be found here:
Upvotes: 0
Reputation: 236140
(fail2)
is the correct one. The procedure conforms to a continuation-passing style, and the correct continuation procedure in this case is fail2
.
Upvotes: 1