home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / scheme / 2177 < prev    next >
Encoding:
Internet Message Format  |  1992-09-10  |  3.3 KB

  1. Path: sparky!uunet!ogicse!mintaka.lcs.mit.edu!zurich.ai.mit.edu!jaffer
  2. From: jaffer@zurich.ai.mit.edu (Aubrey Jaffer)
  3. Newsgroups: comp.lang.scheme
  4. Subject: dynamic-wind
  5. Message-ID: <JAFFER.92Sep10234847@camelot.ai.mit.edu>
  6. Date: 11 Sep 92 04:48:47 GMT
  7. Article-I.D.: camelot.JAFFER.92Sep10234847
  8. Sender: news@mintaka.lcs.mit.edu
  9. Organization: M.I.T. Artificial Intelligence Lab.
  10. Lines: 82
  11.  
  12. I posted the following message 2 weeks ago and received only one
  13. response.  Perhaps no one will use dynamic-wind.  Perhaps everyone was
  14. on vacation.  Specifically:
  15.  
  16. If a continuation is called from within <thunk1> which escapes into
  17. <thunk2>, should <thunk3> and then <thunk1> be called before <thunk2>?
  18.  
  19. If a continuation is called from within <thunk1> which escapes into
  20. <thunk2>, should just <thunk1> be called before <thunk2>?
  21.  
  22. If a continuation is called from within <thunk3> which escapes into
  23. <thunk2>, should <thunk3> and then <thunk1> be called before <thunk2>?
  24.  
  25. If a continuation is called from within <thunk3> which escapes into
  26. <thunk2>, should just <thunk1> be called before <thunk2>?
  27.  
  28. If a continuation is called from within <thunk1> which escapes into
  29. <thunk3>, should any additional thunks be called?
  30.  
  31. If a continuation is called from within <thunk3> which escapes into
  32. <thunk1>, should any additional thunks be called?
  33.  
  34. Here is code which I beleive implements the proposed DYNAMIC-WIND
  35. procedre for any R4RS compliant Scheme.  Is this correct?  If so, I
  36. will add it to SLIB.  Notice that it works for
  37. call-with-current-continuation in <thunk2>.  I have tried to make it
  38. consistent for <thunk1> and <thunk3> as well but I am not clear as to
  39. what should happen in this case.
  40. ======================================================================
  41. ; "dynwind.scm", wind-unwind-protect for Scheme
  42. ; Copyright (c) 1992, Aubrey Jaffer
  43.  
  44. ;This facility is a generalization of Common Lisp `unwind-protect',
  45. ;designed to take into account the fact that continuations produced by
  46. ;CALL-WITH-CURRENT-CONTINUATION may be reentered.
  47.  
  48. ;  (dynamic-wind <thunk1> <thunk2> <thunk3>)        procedure
  49.  
  50. ;The arguments <thunk1>, <thunk2>, and <thunk3> must all be procedures
  51. ;of no arguments (thunks).
  52.  
  53. ;DYNAMIC-WIND calls <thunk1>, <thunk2>, and then <thunk3>.  The value
  54. ;returned of <thunk2> is returned as the result of DYNAMIC-WIND.
  55. ;<thunk3> is also called just before <thunk2> calls any continuations
  56. ;created by CALL-WITH-CURRENT-CONTINUATION.  If <thunk2> captures its
  57. ;continuation as an escape procedure, <thunk1> is invoked just before
  58. ;continuing that continuation.
  59.  
  60. (define *winds* '())
  61.  
  62. (define (dynamic-wind <thunk1> <thunk2> <thunk3>)
  63.   (<thunk1>)
  64.   (set! *winds* (cons (cons <thunk1> <thunk3>) *winds*))
  65.   (let ((ans (<thunk2>)))
  66.     (set! *winds* (cdr *winds*))
  67.     (<thunk3>)
  68.     ans))
  69.  
  70. (define call-with-current-continuation
  71.   (let ((oldcc call-with-current-continuation))
  72.     (lambda (proc)
  73.       (let ((winds *winds*))
  74.     (oldcc
  75.      (lambda (cont)
  76.        (proc (lambda (c2)
  77.            (dynamic:do-winds *winds* winds)
  78.            (cont c2)))))))))
  79.  
  80. (define (dynamic:do-winds from to)
  81.   (set! *winds* from)
  82.   (cond ((eq? from to))
  83.     ((null? from)
  84.      (dynamic:do-winds from (cdr to))
  85.      ((caar to)))
  86.     ((null? to)
  87.      ((cdar from))
  88.      (dynamic:do-winds (cdr from) to))
  89.     (else
  90.      ((cdar from))
  91.      (dynamic:do-winds (cdr from) (cdr to))
  92.      ((caar to))))
  93.   (set! *winds* to))
  94.