On Tue, 30 Dec 2003, Scott wrote:
> Why does Haskell have no continuations?
> (http://www.haskell.org/hawiki/CoMonad)
> If continuations are incompatible with non-strict semantics, I'd 
> appreciate an explanation.

With letrec and unrestricted call/cc you can implement ML-style refs:

  (define (make-cell)           ; Alan Bawden, 1989
      (lambda (return-from-make-cell)
        (letrec ((state
                     (lambda (return-new-state)
                         (lambda (op)
                           (case op
                              (lambda (value)
                                  (lambda (return-from-access)
                                      (list value return-from-access))))))
                             ((get) (car state)))))))))
          ((cadr state) 'done)))))

Unrestricted call/cc seems to be incompatible with referential
transparency in a very fundamental way, and Haskell is nothing without
referential transparency. On the other hand, it doesn't cause any problems
when the evaluation order is fixed by some monad, whence MonadCont.

In practice, the cool things that call/cc makes possible (backtracking,
cooperative multitasking) can be achieved much more easily with custom
monads: e.g. the list monad

  instance Monad [] where
    m >>= k   = concatMap k m
    return x  = [x]
    fail s    = []

versus the amb form in Scheme, which provides essentially the same

  (define amb-fail '())

  (define (initialize-amb-fail)
    (set! amb-fail
          (lambda (x)
            (error #f "amb tree exhausted")))) ;;for petite chez

  (define (fail) (amb))

  (define-syntax amb
    (syntax-rules ()
      ((amb argument ...)
       (let ((old-amb-fail amb-fail))
         (call/cc (lambda (return)
                    (call/cc (lambda (next)
                               (set! amb-fail next)
                               (return argument)))...
                    (set! amb-fail old-amb-fail)
                    (amb-fail #f)))))))


-- Ben

Haskell mailing list

Reply via email to