control.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ;;; Beyond call/cc
  2. ;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (ice-9 control)
  18. #:re-export (call-with-prompt abort-to-prompt
  19. default-prompt-tag make-prompt-tag)
  20. #:export (% abort shift reset shift* reset*
  21. call-with-escape-continuation call/ec
  22. let-escape-continuation let/ec))
  23. (define (abort . args)
  24. (apply abort-to-prompt (default-prompt-tag) args))
  25. (define-syntax %
  26. (syntax-rules ()
  27. ((_ expr)
  28. (call-with-prompt (default-prompt-tag)
  29. (lambda () expr)
  30. default-prompt-handler))
  31. ((_ expr handler)
  32. (call-with-prompt (default-prompt-tag)
  33. (lambda () expr)
  34. handler))
  35. ((_ tag expr handler)
  36. (call-with-prompt tag
  37. (lambda () expr)
  38. handler))))
  39. ;; Each prompt tag has a type -- an expected set of arguments, and an unwritten
  40. ;; contract of what its handler will do on an abort. In the case of the default
  41. ;; prompt tag, we could choose to return values, exit nonlocally, or punt to the
  42. ;; user.
  43. ;;
  44. ;; We choose the latter, by requiring that the user return one value, a
  45. ;; procedure, to an abort to the prompt tag. That argument is then invoked with
  46. ;; the continuation as an argument, within a reinstated default prompt. In this
  47. ;; way the return value(s) from a default prompt are under the user's control.
  48. (define (default-prompt-handler k proc)
  49. (% (default-prompt-tag)
  50. (proc k)
  51. default-prompt-handler))
  52. ;; Kindly provided by Wolfgang J Moeller <wjm@heenes.com>, modelled
  53. ;; after the ones by Oleg Kiselyov in
  54. ;; http://okmij.org/ftp/Scheme/delim-control-n.scm, which are in the
  55. ;; public domain, as noted at the top of http://okmij.org/ftp/.
  56. ;;
  57. (define-syntax-rule (reset . body)
  58. (call-with-prompt (default-prompt-tag)
  59. (lambda () . body)
  60. (lambda (cont f) (f cont))))
  61. (define-syntax-rule (shift var . body)
  62. (abort-to-prompt (default-prompt-tag)
  63. (lambda (cont)
  64. ((lambda (var) (reset . body))
  65. (lambda vals (reset (apply cont vals)))))))
  66. (define (reset* thunk)
  67. (reset (thunk)))
  68. (define (shift* fc)
  69. (shift c (fc c)))
  70. (define (call-with-escape-continuation proc)
  71. "Call PROC with an escape continuation."
  72. (let ((tag (list 'call/ec)))
  73. (call-with-prompt tag
  74. (lambda ()
  75. (proc (lambda args
  76. (apply abort-to-prompt tag args))))
  77. (lambda (_ . args)
  78. (apply values args)))))
  79. (define call/ec call-with-escape-continuation)
  80. (define-syntax-rule (let-escape-continuation k body ...)
  81. "Bind K to an escape continuation within the lexical extent of BODY."
  82. (let ((tag (list 'let/ec)))
  83. (call-with-prompt tag
  84. (lambda ()
  85. (let ((k (lambda args
  86. (apply abort-to-prompt tag args))))
  87. body ...))
  88. (lambda (_ . results)
  89. (apply values results)))))
  90. (define-syntax-rule (let/ec k body ...)
  91. (let-escape-continuation k body ...))