Search code examples
schemesicp

SICP Exercise 4.54


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 uses amb, 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 handles require expressions. Complete the following definition of analyze-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?


Solution

  • (fail2) is the correct one. The procedure conforms to a continuation-passing style, and the correct continuation procedure in this case is fail2.