Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!zephyr.ens.tek.com!tektronix!sequent!mntgfx!plogan From: plogan@mentor.com (Patrick Logan) Newsgroups: comp.object Subject: Re: Continuations Message-ID: <1989Nov28.224937.4183@mentor.com> Date: 28 Nov 89 22:49:37 GMT Organization: engr Lines: 82 Jonathan Shapiro (shap@delrey.sgi.com) is interested in the utility of continuations in object-oriented programming. My response is they have great utility. He mentioned an example of defining an exception handling system using continuations. If you're unfamiliar with continuations, consider another, unrelated example and you may begin to realize the power of continuations. ASIDE... Before I continue with the example, I think it's important to point out that continuations exist conceptually in any programming language that provides sequential execution. For example C, Pascal, and C++ have sequential statements that can be described using the concept of continuations. Even Prolog messes with sequential execution with the concepts of backtracking and cut. The real *power* of continuations comes with languages such as Scheme that provide continuations as full-fledged objects in the language. END OF ASIDE My example of an object that uses continuations is a discrete event simulator. Let's say your simulating (very simplistically**) a digital electronic circuit. You could have an object representing a two-input AND gate. This object has (among others) a method for setting the value of an input. Here is some pseudo code describing that method: When one of the input pins is assigned a new value perform... (1) Assign the new value to the input pin. (2) Notify the discrete event simulator that the next step should execute two simulation time units from the current time. This models the delay of the real AND gate. (3) Assign to the output pin the result of AND'ing the input pins. In a Scheme-based object system this might look like: (define-method (set-input-pin! and-gate pin-number new-value) ;; Step #1. (case pin-number (1 (set! input-pin-one new-value)) (2 (set! input-pin-one new-value)) (else (error "Invalid pin number." pin-number))) ;; Step #2. (simulator 'wait-for 2) ;; Step #3. (self 'set-output-pin! (and input-pin-one input-pin-two))) The wait-for message to the simulator object implements step #2 above and uses continuations. The pseudo code may be: When the wait-for message is sent perform... (1) Capture the current continuation. (2) Schedule it to be executed at the current time plus the value sent in the message. (3) Choose the next scheduled continuation. (4) Set the current time to be the time of that continuation. (5) Execute that continuation. The Scheme-like code may be: (define-method (wait-for simulator relative-time) ;; Step #1. (call-with-current-continuation (lambda (k) ;; Step #2. (scheduler 'schedule k (+ (self 'current-time) relative-time)) (receive (next-scheduled-k new-time) ;; Bind these two names (scheduler 'next) ;; to the two values returned by this message. Step #3. (self 'set-time! new-time) ;; Then set the new time (Step #4.) (next-scheduled-k #t) ;; and execute the continuation with a meaningless argument. Step #5. So you can see that continuations are used to schedule future events in a neat way. They allow control to be handed over to other events that are scheduled to occur sooner. By embedding the capturing of continuations in the simulator object, the details are abstracted so that the description of the model is also very clean. ** This algorithm does not correspond to Mentor Graphics' products! 8) -- Patrick Logan | ...!{decwrl,sequent,tessi}!mntgfx!plogan Mentor Graphics Corporation | plogan@pdx.MENTOR.COM Beaverton, Oregon | Brought to you by Super Global Mega Corp .com