control.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; Delimited control
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Delimited control.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot control)
  21. (export make-prompt-tag
  22. default-prompt-tag
  23. call-with-prompt
  24. abort-to-prompt
  25. %
  26. default-prompt-handler
  27. call-with-current-continuation
  28. call/cc)
  29. (import (hoot parameters)
  30. (rename (hoot primitives)
  31. (%abort-to-prompt abort-to-prompt))
  32. (hoot values)
  33. (hoot cond-expand))
  34. (define* (make-prompt-tag #:optional (stem "prompt"))
  35. (%cons stem '()))
  36. (cond-expand
  37. (guile-vm)
  38. (hoot
  39. (define default-prompt-tag
  40. (%inline-wasm
  41. '(func (result (ref eq))
  42. (global.get $default-prompt-tag))))))
  43. (define-syntax-rule (define-primcall f %f arg ...)
  44. (begin
  45. (define (generic arg ...)
  46. (%f arg ...))
  47. (define-syntax f
  48. (lambda (stx)
  49. (syntax-case stx ()
  50. ((_ . x) #'(%f . x))
  51. (id (identifier? #'id) #'generic))))))
  52. (define-primcall call-with-prompt %call-with-prompt tag body handler)
  53. (define-syntax %
  54. (syntax-rules ()
  55. ((_ expr)
  56. (call-with-prompt (default-prompt-tag)
  57. (lambda () expr)
  58. default-prompt-handler))
  59. ((_ expr handler)
  60. (call-with-prompt (default-prompt-tag)
  61. (lambda () expr)
  62. handler))
  63. ((_ tag expr handler)
  64. (call-with-prompt tag
  65. (lambda () expr)
  66. handler))))
  67. (define (default-prompt-handler k proc) (% (proc k)))
  68. ;; This is an implementation of call/cc in terms of delimited
  69. ;; continuations. It correct except as regards dynamic-wind: capturing
  70. ;; the continuation unwinds all dynamic-winds, then rewinds them; and
  71. ;; invoking the continuation does the same, even if the invoking and
  72. ;; captured continuations overlap. Oh well; call/cc is strictly less
  73. ;; useful than call-with-prompt anyway.
  74. (define (call-with-current-continuation proc)
  75. (define (unwind-and-call handler)
  76. (abort-to-prompt (default-prompt-tag) handler))
  77. (define (rewind-and-continue captured-continuation)
  78. (define-syntax-rule (reinstate expr)
  79. (captured-continuation (lambda () expr)))
  80. (define (k . args)
  81. (define (rewind-and-return-values discarded-continuation)
  82. (reinstate (apply values args)))
  83. (unwind-and-call rewind-and-return-values))
  84. (reinstate (proc k)))
  85. (let ((thunk (unwind-and-call rewind-and-continue)))
  86. (thunk)))
  87. (define call/cc call-with-current-continuation))