20-CS-4003-001 Organization of Programming Languages Fall 2017
Call-with-current-continuation examples

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code

Can't nest the amb values

(define call/cc call-with-current-continuation)

(define amb-fail '())

(define-syntax amb
  (syntax-rules ()
    ((amb x ...)
       (lambda (sk)
           (lambda (fk1)
             (set! amb-fail (lambda () (fk1 'fail)))
             (sk x)))

(define back (lambda () (amb-fail)))

(define assert (lambda (p) (if (not p) (amb-fail))))

(define f
  (lambda () 
    (let ((a (amb 1 2 3 4 5 6 7))
          (b (amb 11 12 13 14 15)))
      (display 'try:)(display a)
      (display "-")(display b)(display " ")
      (assert (and (eq? a 6) (eq? b 14)))
      (display "success: a=")(display a)
      (display " b=")(display b)(newline)
      (cons a b))))
 -  The obvious first attempt to nest calls to amb fails because the code does not save the continuation of the next-higher level so that it can fail back to that level.