Debian Codegraph @

%3a41/streams/primitive.sls

raw
1 #!r6rs
2 ;;; Copyright (C) Philip L. Bewig (2007). All Rights Reserved.
3 ;;; Permission is hereby granted, free of charge, to any person
4 ;;; obtaining a copy of this software and associated documentation files
5 ;;; (the "Software"), to deal in the Software without restriction,
6 ;;; including without limitation the rights to use, copy, modify, merge,
7 ;;; publish, distribute, sublicense, and/or sell copies of the Software,
8 ;;; and to permit persons to whom the Software is furnished to do so,
9 ;;; subject to the following conditions:
10 ;;;
11 ;;; The above copyright notice and this permission notice shall be
12 ;;; included in all copies or substantial portions of the Software.
13 ;;;
14 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 ;;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ;;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 ;;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 ;;; SOFTWARE.
22
23 (library (srfi :41 streams primitive)
24
25 (export stream-null stream-cons stream? stream-null? stream-pair?
26 stream-car stream-cdr stream-lambda)
27
28 (import (rnrs) (rnrs mutable-pairs))
29
30 (define-record-type (stream-type make-stream stream?)
31 (fields (mutable box stream-promise stream-promise!)))
32
33 (define-syntax stream-lazy
34 (syntax-rules ()
35 ((lazy expr)
36 (make-stream
37 (cons 'lazy (lambda () expr))))))
38
39 (define (stream-eager expr)
40 (make-stream
41 (cons 'eager expr)))
42
43 (define-syntax stream-delay
44 (syntax-rules ()
45 ((stream-delay expr)
46 (stream-lazy (stream-eager expr)))))
47
48 (define (stream-force promise)
49 (let ((content (stream-promise promise)))
50 (case (car content)
51 ((eager) (cdr content))
52 ((lazy) (let* ((promise* ((cdr content)))
53 (content (stream-promise promise)))
54 (if (not (eqv? (car content) 'eager))
55 (begin (set-car! content (car (stream-promise promise*)))
56 (set-cdr! content (cdr (stream-promise promise*)))
57 (stream-promise! promise* content)))
58 (stream-force promise))))))
59
60 (define stream-null (stream-delay (cons 'stream 'null)))
61
62 (define-record-type (stream-pare-type make-stream-pare stream-pare?)
63 (fields (immutable kar stream-kar) (immutable kdr stream-kdr)))
64
65 (define (stream-pair? obj)
66 (and (stream? obj) (stream-pare? (stream-force obj))))
67
68 (define (stream-null? obj)
69 (and (stream? obj)
70 (eqv? (stream-force obj)
71 (stream-force stream-null))))
72
73 (define-syntax stream-cons
74 (syntax-rules ()
75 ((stream-cons obj strm)
76 (stream-delay (make-stream-pare (stream-delay obj) (stream-lazy strm))))))
77
78 (define (stream-car strm)
79 (cond ((not (stream? strm)) (error 'stream-car "non-stream"))
80 ((stream-null? strm) (error 'stream-car "null stream"))
81 (else (stream-force (stream-kar (stream-force strm))))))
82
83 (define (stream-cdr strm)
84 (cond ((not (stream? strm)) (error 'stream-cdr "non-stream"))
85 ((stream-null? strm) (error 'stream-cdr "null stream"))
86 (else (stream-kdr (stream-force strm)))))
87
88 (define-syntax stream-lambda
89 (syntax-rules ()
90 ((stream-lambda formals body0 body1 ...)
91 (lambda formals (stream-lazy (let () body0 body1 ...)))))))