Question

I am writing a program that needs to recursively analyze a list of "commands" and "programs" (it is an interpreter of some "robotic language" invented by our professor for a robot living in a maze). Because my initial implementation was too slow, I decided to use call-with-current-continuation.

I know how call/cc works, I have read this and this as an explanation.

My call/cc is based on this part of tutorial:

Often we want to use call-with-current-continuation to call some procedure that takes arguments other than an escape procedure. For example, we might have a procedure that takes two arguments besides the escape procedure, thus:

(define (foo x y escape) ... (if (= x 0) (escape 'ERROR)) ...)) We can fix this by currying the procedure, making it a procedure of one argument.

[ An earlier chapter should have a discussion of currying! ]

Suppose we want to pass 0 and 1 as the values of x and y, as well as handing foo the escape procedure. Rather than saying

(call-with-current-continuation foo) which doesn't pass enough arguments to the call to foo, we say

(call-with-current-continuation (lambda (escape) (foo 0 1 escape))) The lambda expression creates a closure that does exactly what we want. It will call foo with arguments 0, 1, and the escape procedure created by call-with-current-continuation.

However, for some reason it doesn't work and throws this exception:

call-with-current-continuation: contract violation
  expected: (any/c . -> . any)
  given: #<procedure:...mazesimulator.ss:301:34>

I would like you to help me find my mistake and explain why it occurs...

Here is the part of code relevant to this question:

; main program 
(define (simulate state expression-list program limit)

  ; read the input and set global variables
  (set! current-orientation (list-ref state 2))
  (set! current-coordinates (list-ref state 1))
  (set! current-maze (list-ref state 0))

  ; call the inner function
  (call-with-current-continuation (lambda (exit)
                                    (command state expression-list program limit exit)))
  ; this is the output
   (list list-of-executed-commands (list current-maze current-coordinates current-orientation))
  )


;; main recursive function
;; analyses expression-list parameter
;; evaluates its elements 
;; and calls itself on the cdr of the espression-list
 (define (command state expression-list program limit exit)

   (if (and (not (null? expression-list))(equal? stop-command #f))          

      ; recursion end condition, the whole procedure will be done only 
                                              ; if the list is still not empty 

      (if (atom? expression-list)              ;if the list consists of only one command 
         (if (equal? stop-command #f)          ;positive branch - if there were no erros before
             (atomic-command state expression-list program limit exit)            ;call atomic-command on this element 
             ;when flag is set to #t
             (exit))
         ; here comes a problem with "inner ifs"
         (if (atom? (car expression-list))                                         ;negative branch - if the first element is "if"
              (if (equal? (car expression-list) 'if)                               ;if the list consisits only of if-clause, no other commands ((if ...))
                    (if ((name->function (list-ref expression-list 1)))            ;evaluate the boolean - wall? north? and choose corresponding branch
                             (command state (list-ref expression-list 2) program limit exit)
                             (command state (list-ref expression-list 3) program limit exit))
                    (evaluate-first-and-call-command-on-rest expression-list program limit exit))


              (if (equal? (car(car expression-list)) 'if)                          ;if the if-clause is not the only element in list - "inner if" ((if ...) turn-left put-mark)
                  (begin                                                           ;not only evaluate if-clause,
                    (if ((name->function (list-ref (car expression-list) 1)))
                             (command state (list-ref (car expression-list) 2) program limit exit)
                             (command state (list-ref (car expression-list) 3) program limit exit))
                    (command state (cdr expression-list) program limit exit))                 ;but also call command on cdr! 
                  (evaluate-first-and-call-command-on-rest expression-list program limit exit))))

      ;when limit is exceeded or when the flag is set to #t
      (exit) ))
Was it helpful?

Solution

New follow-up: So at this point I am puzzled by the poster's question. GoZoner has pointed out that the continuation passed by call/cc may require an actual parameter when invoked, but this is not generally true in Racket (which, based on the poster's error message, is the language implementation that I assume is under discussion).

Furthermore, the code snippet that the original poster put in the question is incomplete, so one cannot directly execute the code in an attempt to replicate the problem. (My informal analysis hasn't revealed an obvious bug in the use of call-with-current-continuation that was presented by the original poster.) It would be useful if the original poster could derive a minimal (or at least smaller) test case that exposes the same issue.

It could be that one of the specific languages or language levels within Racket uses a more restrictive form of call/cc, but I have not found evidence of such a language level. I will pose the question to the original poster.


Edit: Chris-Jester Young has pointed out that my commentary may not apply properly here (see comments on this answer). I need to more carefully investigate the Racket's handling of continuations in imperative code; the notes below may be leading the asker down an incorrect path. (I plan to delete this answer if I can confirm that my notes below are bogus.)

Follow-up edit: It appears that Racket's handling of call/cc does indeed pass along a continuation that will accept zero values when it is invoked from a context that discards the value. For example:

(define global 7)

(define (goner exit)
  (set! global 11)
  (exit)
  (set! global 13)
  (* 2 3))

(define (hi)
  (define x global)
  (call-with-current-continuation goner)
  (* 5 x global))

(* 5 7 11)

(hi)

The above prints 385 (twice); once for (* 5 7 11), and once for (hi).


(Original commentary follows)

The fact that you are invoking (exit) with zero arguments leads me to think that you do not completely understand how call/cc works (as in its observable behavior), despite your claim to the contrary.

I recommend you play with some small examples, completely independently of your professor's robot maze infrastructure, before you try to incorporate call/cc into your solution.

For example, I can readily reproduce your error message this way:

(define (goner)
  (* 2 3))

(define (hi)
  (let ((x (call-with-current-continuation goner)))
    (* 5 x)))

(hi)

From the above, I get:

call-with-current-continuation: contract violation
  expected: (any/c . -> . any)
  given: #<procedure:goner>

which is quite similar to your error message, no? (Though to be honest, that might just be a coincidence).

Compare the output from the run above with the outputs from runs of:

(define (goner exit)
  (* 2 3))

(define (hi)
  (let ((x (call-with-current-continuation goner)))
    (* 5 x)))

(hi)

and:

(define (goner exit)
  (* 2 (exit)))

(define (hi)
  (let ((x (call-with-current-continuation goner)))
    (* 5 x)))

(hi)

and:

(define (goner exit)
  (* 2 (exit 3)))

(define (hi)
  (let ((x (call-with-current-continuation goner)))
    (* 5 x)))

(hi)

and:

(define (goner exit)
  (* (exit 2) 3))

(define (hi)
  (let ((x (call-with-current-continuation goner)))
    (* 5 x)))

(hi)

Give some careful thought to what each is illustrating, and think about how it might matter in the case of your program.

OTHER TIPS

The call/cc 'escape procedure' expects a single argument. You are calling it as (exit) without the required argument. Different Scheme implementations will handle this differently. Here is one that complains:

1 ]=> (define (doit exit)
   (display "DOIT2\n")
   (exit))                      ;; no argument, expect error
;Value: doit

1 ]=> (define (try)
   (call-with-current-continuation
    (lambda (exit) (display "TRY\n") (doit exit)))
   'done)
;Value: try

1 ]=> (try)
TRY
DOIT2
;The procedure #[continuation 13] has been called with 0 arguments; it requires exactly 1 argument.

The reason that the 'escape procedure' expects a value is that call/cc, like all Scheme functions, needs to produce a value. The argument that is provided is the return value of call/cc when the escape procedure is invoked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top