control.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. suspendable-continuation?))
  24. (load-extension (string-append "libguile-" (effective-version))
  25. "scm_init_ice_9_control")
  26. (define (abort . args)
  27. (apply abort-to-prompt (default-prompt-tag) args))
  28. (define-syntax %
  29. (syntax-rules ()
  30. ((_ expr)
  31. (call-with-prompt (default-prompt-tag)
  32. (lambda () expr)
  33. default-prompt-handler))
  34. ((_ expr handler)
  35. (call-with-prompt (default-prompt-tag)
  36. (lambda () expr)
  37. handler))
  38. ((_ tag expr handler)
  39. (call-with-prompt tag
  40. (lambda () expr)
  41. handler))))
  42. ;; Each prompt tag has a type -- an expected set of arguments, and an unwritten
  43. ;; contract of what its handler will do on an abort. In the case of the default
  44. ;; prompt tag, we could choose to return values, exit nonlocally, or punt to the
  45. ;; user.
  46. ;;
  47. ;; We choose the latter, by requiring that the user return one value, a
  48. ;; procedure, to an abort to the prompt tag. That argument is then invoked with
  49. ;; the continuation as an argument, within a reinstated default prompt. In this
  50. ;; way the return value(s) from a default prompt are under the user's control.
  51. (define (default-prompt-handler k proc)
  52. (% (default-prompt-tag)
  53. (proc k)
  54. default-prompt-handler))
  55. ;; Kindly provided by Wolfgang J Moeller <wjm@heenes.com>, modelled
  56. ;; after the ones by Oleg Kiselyov in
  57. ;; http://okmij.org/ftp/Scheme/delim-control-n.scm, which are in the
  58. ;; public domain, as noted at the top of http://okmij.org/ftp/.
  59. ;;
  60. (define-syntax-rule (reset . body)
  61. (call-with-prompt (default-prompt-tag)
  62. (lambda () . body)
  63. (lambda (cont f) (f cont))))
  64. (define-syntax-rule (shift var . body)
  65. (abort-to-prompt (default-prompt-tag)
  66. (lambda (cont)
  67. ((lambda (var) (reset . body))
  68. (lambda vals (reset (apply cont vals)))))))
  69. (define (reset* thunk)
  70. (reset (thunk)))
  71. (define (shift* fc)
  72. (shift c (fc c)))
  73. (define (call-with-escape-continuation proc)
  74. "Call PROC with an escape continuation."
  75. (let ((tag (list 'call/ec)))
  76. (call-with-prompt tag
  77. (lambda ()
  78. (proc (lambda args
  79. (apply abort-to-prompt tag args))))
  80. (lambda (_ . args)
  81. (apply values args)))))
  82. (define call/ec call-with-escape-continuation)
  83. (define-syntax-rule (let-escape-continuation k body ...)
  84. "Bind K to an escape continuation within the lexical extent of BODY."
  85. (let ((tag (list 'let/ec)))
  86. (call-with-prompt tag
  87. (lambda ()
  88. (let ((k (lambda args
  89. (apply abort-to-prompt tag args))))
  90. body ...))
  91. (lambda (_ . results)
  92. (apply values results)))))
  93. (define-syntax-rule (let/ec k body ...)
  94. (let-escape-continuation k body ...))