control.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 apply)
  30. (hoot cond-expand)
  31. (hoot inline-wasm)
  32. (hoot parameters)
  33. (rename (only (hoot primitives)
  34. %cons %abort-to-prompt
  35. %call-with-prompt)
  36. (%abort-to-prompt abort-to-prompt))
  37. (hoot syntax)
  38. (hoot values))
  39. (define* (make-prompt-tag #:optional (stem "prompt"))
  40. (%cons stem '()))
  41. (cond-expand
  42. (guile-vm)
  43. (hoot
  44. (define default-prompt-tag
  45. (%inline-wasm
  46. '(func (result (ref eq))
  47. (global.get $default-prompt-tag))))))
  48. (define-syntax-rule (define-primcall f %f arg ...)
  49. (begin
  50. (define (generic arg ...)
  51. (%f arg ...))
  52. (define-syntax f
  53. (lambda (stx)
  54. (syntax-case stx ()
  55. ((_ . x) #'(%f . x))
  56. (id (identifier? #'id) #'generic))))))
  57. (define-primcall call-with-prompt %call-with-prompt tag body handler)
  58. (define-syntax %
  59. (syntax-rules ()
  60. ((_ expr)
  61. (call-with-prompt (default-prompt-tag)
  62. (lambda () expr)
  63. default-prompt-handler))
  64. ((_ expr handler)
  65. (call-with-prompt (default-prompt-tag)
  66. (lambda () expr)
  67. handler))
  68. ((_ tag expr handler)
  69. (call-with-prompt tag
  70. (lambda () expr)
  71. handler))))
  72. (define (default-prompt-handler k proc) (% (proc k)))
  73. ;; This is an implementation of call/cc in terms of delimited
  74. ;; continuations. It correct except as regards dynamic-wind: capturing
  75. ;; the continuation unwinds all dynamic-winds, then rewinds them; and
  76. ;; invoking the continuation does the same, even if the invoking and
  77. ;; captured continuations overlap. Oh well; call/cc is strictly less
  78. ;; useful than call-with-prompt anyway.
  79. (define (call-with-current-continuation proc)
  80. (define (unwind-and-call handler)
  81. (abort-to-prompt (default-prompt-tag) handler))
  82. (define (rewind-and-continue captured-continuation)
  83. (define-syntax-rule (reinstate expr)
  84. (captured-continuation (lambda () expr)))
  85. (define (k . args)
  86. (define (rewind-and-return-values discarded-continuation)
  87. (reinstate (apply values args)))
  88. (unwind-and-call rewind-and-return-values))
  89. (reinstate (proc k)))
  90. (let ((thunk (unwind-and-call rewind-and-continue)))
  91. (thunk)))
  92. (define call/cc call-with-current-continuation))