control.scm 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; Beyond call/cc
  2. ;; Copyright (C) 2010, 2011 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. (define (abort . args)
  22. (apply abort-to-prompt (default-prompt-tag) args))
  23. (define-syntax %
  24. (syntax-rules ()
  25. ((_ expr)
  26. (call-with-prompt (default-prompt-tag)
  27. (lambda () expr)
  28. default-prompt-handler))
  29. ((_ expr handler)
  30. (call-with-prompt (default-prompt-tag)
  31. (lambda () expr)
  32. handler))
  33. ((_ tag expr handler)
  34. (call-with-prompt tag
  35. (lambda () expr)
  36. handler))))
  37. ;; Each prompt tag has a type -- an expected set of arguments, and an unwritten
  38. ;; contract of what its handler will do on an abort. In the case of the default
  39. ;; prompt tag, we could choose to return values, exit nonlocally, or punt to the
  40. ;; user.
  41. ;;
  42. ;; We choose the latter, by requiring that the user return one value, a
  43. ;; procedure, to an abort to the prompt tag. That argument is then invoked with
  44. ;; the continuation as an argument, within a reinstated default prompt. In this
  45. ;; way the return value(s) from a default prompt are under the user's control.
  46. (define (default-prompt-handler k proc)
  47. (% (default-prompt-tag)
  48. (proc k)
  49. default-prompt-handler))
  50. ;; Kindly provided by Wolfgang J Moeller <wjm@heenes.com>, modelled
  51. ;; after the ones by Oleg Kiselyov in
  52. ;; http://okmij.org/ftp/Scheme/delim-control-n.scm, which are in the
  53. ;; public domain, as noted at the top of http://okmij.org/ftp/.
  54. ;;
  55. (define-syntax-rule (reset . body)
  56. (call-with-prompt (default-prompt-tag)
  57. (lambda () . body)
  58. (lambda (cont f) (f cont))))
  59. (define-syntax-rule (shift var . body)
  60. (abort-to-prompt (default-prompt-tag)
  61. (lambda (cont)
  62. ((lambda (var) (reset . body))
  63. (lambda vals (reset (apply cont vals)))))))
  64. (define (reset* thunk)
  65. (reset (thunk)))
  66. (define (shift* fc)
  67. (shift c (fc c)))