srfi-40.scm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; STREAM -- LIBRARY OF SYNTAX AND FUNCTIONS TO MANIPULATE STREAMS
  2. ;;; A stream is a new data type, disjoint from all other data types, that
  3. ;;; contains a promise that, when forced, is either nil (a single object
  4. ;;; distinguishable from all other objects) or consists of an object
  5. ;;; (the stream element) followed by a stream. Each stream element is
  6. ;;; evaluated exactly once, when it is first retrieved (not when it is
  7. ;;; created); once evaluated its value is saved to be returned by
  8. ;;; subsequent retrievals without being evaluated again.
  9. ;;; Copyright (C) 2003 by Philip L. Bewig of Saint Louis, Missouri,
  10. ;;; United States of America. All rights reserved.
  11. ;;;
  12. ;;; Permission is hereby granted, free of charge, to any person
  13. ;;; obtaining a copy of this software and associated documentation
  14. ;;; files (the "Software"), to deal in the Software without
  15. ;;; restriction, including without limitation the rights to use, copy,
  16. ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
  17. ;;; of the Software, and to permit persons to whom the Software is
  18. ;;; furnished to do so, subject to the following conditions:
  19. ;;;
  20. ;;; The above copyright notice and this permission notice shall be
  21. ;;; included in all copies or substantial portions of the Software.
  22. ;;;
  23. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  27. ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  28. ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. ;;; DEALINGS IN THE SOFTWARE.
  31. ;;; PROMISES A LA SRFI-45:
  32. ;;; A separate implementation is necessary to
  33. ;;; have promises that answer #t to stream?
  34. ;;; This requires lots of complicated type conversions.
  35. (define-record-type stream-promise :stream-promise
  36. (make-s:promise kind content)
  37. s:promise?
  38. (kind s:promise-kind set-s:promise-kind!)
  39. (content s:promise-content set-s:promise-content!))
  40. (define-syntax lazy
  41. (syntax-rules ()
  42. ((lazy exp)
  43. (make-cell (make-s:promise 'lazy (lambda () exp))))))
  44. (define (eager x)
  45. (make-stream (make-cell (make-s:promise 'eager x))))
  46. (define-syntax delay
  47. (syntax-rules ()
  48. ((delay exp) (lazy (eager exp)))))
  49. (define (force promise)
  50. (let ((content (cell-ref promise)))
  51. (case (s:promise-kind content)
  52. ((eager) (s:promise-content content))
  53. ((lazy)
  54. (let* ((promise* (stream-promise ((s:promise-content content))))
  55. (content (cell-ref promise)))
  56. (if (not (eqv? 'eager (s:promise-kind content)))
  57. (begin
  58. (set-s:promise-kind! content (s:promise-kind (cell-ref promise*)))
  59. (set-s:promise-content! content (s:promise-content (cell-ref promise*)))
  60. (cell-set! promise* content)))
  61. (force promise))))))
  62. ;; STREAM-TYPE -- type of streams
  63. ;; STREAM? object -- #t if object is a stream, #f otherwise
  64. (define-record-type stream :stream
  65. (make-stream promise)
  66. stream?
  67. (promise stream-promise))
  68. ;;; UTILITY FUNCTIONS
  69. ;; STREAM-ERROR message -- print message then abort execution
  70. ; replace this with a call to the native error handler
  71. ; if stream-error returns, so will the stream library function that called it
  72. (define stream-error error)
  73. ;;; STREAM SYNTAX AND FUNCTIONS
  74. ;; STREAM-NULL -- the distinguished nil stream
  75. (define stream-null (make-stream (delay '())))
  76. ;; STREAM-CONS object stream -- primitive constructor of streams
  77. (define-syntax stream-cons
  78. (syntax-rules ()
  79. ((stream-cons obj strm)
  80. (make-stream
  81. (delay
  82. (if (not (stream? strm))
  83. (stream-error "attempt to stream-cons onto non-stream")
  84. (cons obj strm)))))))
  85. ;; STREAM-NULL? object -- #t if object is the null stream, #f otherwise
  86. (define (stream-null? obj)
  87. (and (stream? obj) (null? (force (stream-promise obj)))))
  88. ;; STREAM-PAIR? object -- #t if object is a non-null stream, #f otherwise
  89. (define (stream-pair? obj)
  90. (and (stream? obj) (not (null? (force (stream-promise obj))))))
  91. ;; STREAM-CAR stream -- first element of stream
  92. (define (stream-car strm)
  93. (cond ((not (stream? strm)) (stream-error "attempt to take stream-car of non-stream"))
  94. ((stream-null? strm) (stream-error "attempt to take stream-car of null stream"))
  95. (else (car (force (stream-promise strm))))))
  96. ;; STREAM-CDR stream -- remaining elements of stream after first
  97. (define (stream-cdr strm)
  98. (cond ((not (stream? strm)) (stream-error "attempt to take stream-cdr of non-stream"))
  99. ((stream-null? strm) (stream-error "attempt to take stream-cdr of null stream"))
  100. (else (cdr (force (stream-promise strm))))))
  101. ;; STREAM-DELAY object -- the essential stream mechanism
  102. (define-syntax stream-delay
  103. (syntax-rules ()
  104. ((stream-delay expr)
  105. (make-stream
  106. (lazy expr)))))
  107. ;; STREAM object ... -- new stream whose elements are object ...
  108. (define (stream . objs)
  109. (let loop ((objs objs))
  110. (stream-delay
  111. (if (null? objs)
  112. stream-null
  113. (stream-cons (car objs) (loop (cdr objs)))))))
  114. ;; STREAM-UNFOLDN generator seed n -- n+1 streams from (generator seed)
  115. (define (stream-unfoldn gen seed n)
  116. (define (unfold-result-stream gen seed)
  117. (let loop ((seed seed))
  118. (stream-delay
  119. (call-with-values
  120. (lambda () (gen seed))
  121. (lambda (next . results)
  122. (stream-cons results (loop next)))))))
  123. (define (result-stream->output-stream result-stream i)
  124. (stream-delay
  125. (let ((result (list-ref (stream-car result-stream) i)))
  126. (cond ((pair? result)
  127. (stream-cons (car result)
  128. (result-stream->output-stream
  129. (stream-cdr result-stream) i)))
  130. ((not result)
  131. (result-stream->output-stream (stream-cdr result-stream) i))
  132. ((null? result) stream-null)
  133. (else (stream-error "can't happen"))))))
  134. (define (result-stream->output-streams result-stream n)
  135. (let loop ((i 0) (outputs '()))
  136. (if (= i n)
  137. (apply values (reverse outputs))
  138. (loop (+ i 1)
  139. (cons (result-stream->output-stream result-stream i)
  140. outputs)))))
  141. (result-stream->output-streams (unfold-result-stream gen seed) n))
  142. ;; STREAM-MAP func stream ... -- stream produced by applying func element-wise
  143. (define (stream-map func . strms)
  144. (cond ((not (procedure? func)) (stream-error "non-functional argument to stream-map"))
  145. ((null? strms) (stream-error "no stream arguments to stream-map"))
  146. ((not (every stream? strms)) (stream-error "non-stream argument to stream-map"))
  147. (else (let loop ((strms strms))
  148. (stream-delay
  149. (if (any stream-null? strms)
  150. stream-null
  151. (stream-cons (apply func (map stream-car strms))
  152. (loop (map stream-cdr strms)))))))))
  153. ;; STREAM-FOR-EACH proc stream ... -- apply proc element-wise for side-effects
  154. (define (stream-for-each proc . strms)
  155. (cond ((not (procedure? proc)) (stream-error "non-functional argument to stream-for-each"))
  156. ((null? strms) (stream-error "no stream arguments to stream-for-each"))
  157. ((not (every stream? strms)) (stream-error "non-stream argument to stream-for-each"))
  158. (else (let loop ((strms strms))
  159. (if (not (any stream-null? strms))
  160. (begin (apply proc (map stream-car strms))
  161. (loop (map stream-cdr strms))))))))
  162. ;; STREAM-FILTER pred? stream -- new stream including only items passing pred?
  163. (define (stream-filter pred? strm)
  164. (cond ((not (procedure? pred?)) (stream-error "non-functional argument to stream-filter"))
  165. ((not (stream? strm)) (stream-error "attempt to apply stream-filter to non-stream"))
  166. (else (stream-unfoldn
  167. (lambda (s)
  168. (values
  169. (stream-cdr s)
  170. (cond ((stream-null? s) '())
  171. ((pred? (stream-car s)) (list (stream-car s)))
  172. (else #f))))
  173. strm
  174. 1))))