Debian Codegraph @

%3a165/implementation.scm

raw symbols SCIP
1 ;; Copyright (C) Marc Nieper-Wißkirchen (2019). All Rights Reserved.
2
3 ;; Permission is hereby granted, free of charge, to any person
4 ;; obtaining a copy of this software and associated documentation
5 ;; files (the "Software"), to deal in the Software without
6 ;; restriction, including without limitation the rights to use, copy,
7 ;; modify, merge, publish, distribute, sublicense, and/or sell copies
8 ;; of the Software, and to permit persons to whom the Software is
9 ;; furnished to do so, subject to the following conditions:
10
11 ;; The above copyright notice and this permission notice (including
12 ;; the next paragraph) shall be included in all copies or substantial
13 ;; portions of the Software.
14
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 ;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 ;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 ;; SOFTWARE.
23
24 (define-record-type Computation-Environment-Variable
25 (make-environment-variable name default immutable? id)
26 environment-variable?
27 (name environment-variable-name)
28 (default environment-variable-default)
29 (immutable? environment-variable-immutable?)
30 (id environment-variable-id))
31
32 (define make-computation-environment-variable
33 (let ((count 0))
34 (lambda (name default immutable?)
35 (set! count (+ count 1))
36 (make-environment-variable name default immutable? (- count)))))
37
38 (define (predefined? var)
39 (not (negative? (environment-variable-id var))))
40
41 (define variable-comparator
42 (make-comparator environment-variable?
43 eq?
44 (lambda (x y)
45 (< (environment-variable-id x)
46 (environment-variable-id y)))
47 (lambda (x . y)
48 (environment-variable-id x))))
49
50 (define default-computation
51 (make-computation-environment-variable 'default-computation #f #f))
52
53 (define (environment-global env)
54 (vector-ref env 0))
55
56 (define (environment-local env)
57 (vector-ref env 1))
58
59 (define (environment-set-global! env global)
60 (vector-set! env 0 global))
61
62 (define (environment-set-local! env local)
63 (vector-set! env 1 local))
64
65 (define (environment-cell-set! env var box)
66 (vector-set! env (+ 2 (environment-variable-id var)) box))
67
68 (define (environment-cell env var)
69 (vector-ref env (+ 2 (environment-variable-id var))))
70
71 (define-syntax define-computation-type
72 (syntax-rules ()
73 ((define-computation-type make-environment run var ...)
74 (%define-computation-type make-environment run (var ...) 0 ()))))
75
76 (define-syntax %define-computation-type
77 (syntax-rules ()
78 ((_ make-environment run () n ((var default e immutable i) ...))
79 (begin
80 (define-values (e ...) (values default ...))
81 (define var (make-environment-variable 'var e immutable i))
82 ...
83 (define (make-environment)
84 (let ((env (make-vector (+ n 2))))
85 (environment-set-global! env (hash-table variable-comparator))
86 (environment-set-local! env (mapping variable-comparator))
87 (vector-set! env (+ i 2) (box e))
88 ...
89 env))
90 (define (run computation)
91 (execute computation (make-environment)))))
92 ((_ make-environment run ((v d) . v*) n (p ...))
93 (%define-computation-type make-environment run v* (+ n 1) (p ... (v d e #f n))))
94 ((_ make-environment run ((v d "immutable") . v*) n (p ...))
95 (%define-computation-type make-environment run v* (+ n 1) (p ... (v d e #t n))))
96 ((_ make-environment run (v . v*) n (p ...))
97 (%define-computation-type make-environment run v* (+ n 1) (p ... (v #f e #f n))))))
98
99 (define-computation-type make-computation-environment computation-run)
100
101 (define (computation-environment-ref env var)
102 (if (predefined? var)
103 (unbox (environment-cell env var))
104 (mapping-ref
105 (environment-local env)
106 var
107 (lambda ()
108 (hash-table-ref/default (environment-global env)
109 var
110 (environment-variable-default var)))
111 unbox)))
112
113 (define (computation-environment-update env . arg*)
114 (let ((new-env (vector-copy env)))
115 (let loop ((arg* arg*)
116 (local (environment-local env)))
117 (if (null? arg*)
118 (begin
119 (environment-set-local! new-env local)
120 new-env)
121 (let ((var (car arg*))
122 (val (cadr arg*)))
123 (if (predefined? var)
124 (begin
125 (environment-cell-set! new-env var (box val))
126 (loop (cddr arg*) local))
127 (loop (cddr arg*) (mapping-set local var (box val)))))))))
128
129 (define (computation-environment-update! env var val)
130 (if (predefined? var)
131 (set-box! (environment-cell env var) val)
132 (mapping-ref (environment-local env)
133 var
134 (lambda ()
135 (hash-table-set! (environment-global env) var val))
136 (lambda (cell)
137 (set-box! cell val)))))
138
139 (define (computation-environment-copy env)
140 (let ((global (hash-table-copy (environment-global env) #t)))
141 (mapping-for-each (lambda (var cell)
142 (hash-table-set! global var (unbox cell)))
143 (environment-local env))
144 (let ((new-env (make-vector (vector-length env))))
145 (environment-set-global! new-env global)
146 (environment-set-local! new-env (mapping variable-comparator))
147 (do ((i (- (vector-length env) 1) (- i 1)))
148 ((< i 2)
149 new-env)
150 (vector-set! new-env i (box (unbox (vector-ref env i))))))))
151
152 (define (execute computation env)
153 (let ((coerce (if (procedure? computation)
154 values
155 (or (computation-environment-ref env default-computation)
156 (error "not a computation" computation)))))
157 ((coerce computation) env)))
158
159 (define (make-computation proc)
160 (lambda (env)
161 (proc (lambda (c) (execute c env)))))
162
163 (define (computation-pure . args)
164 (make-computation
165 (lambda (compute)
166 (apply values args))))
167
168 (define (computation-each a . a*)
169 (computation-each-in-list (cons a a*)))
170
171 (define (computation-each-in-list a*)
172 (make-computation
173 (lambda (compute)
174 (let loop ((a (car a*)) (a* (cdr a*)))
175 (if (null? a*)
176 (compute a)
177 (begin
178 (compute a)
179 (loop (car a*) (cdr a*))))))))
180
181 (define (computation-bind a . f*)
182 (make-computation
183 (lambda (compute)
184 (let loop ((a a) (f* f*))
185 (if (null? f*)
186 (compute a)
187 (loop (call-with-values
188 (lambda () (compute a))
189 (car f*))
190 (cdr f*)))))))
191
192 (define (computation-ask)
193 (lambda (env)
194 env))
195
196 (define (computation-local updater computation)
197 (lambda (env)
198 (computation (updater env))))
199
200 (define-syntax computation-fn
201 (syntax-rules ()
202 ((_ (clause ...) expr ... computation)
203 (%fn (clause ...) () expr ... computation))))
204
205 (define-syntax %fn
206 (syntax-rules ()
207 ((_ () ((id var tmp) ...) expr ... computation)
208 (let ((tmp var) ...)
209 (computation-bind
210 (computation-ask)
211 (lambda (env)
212 (let ((id (computation-environment-ref env tmp)) ...)
213 expr ...
214 computation)))))
215 ((_ ((id var) . rest) (p ...) expr ... computation)
216 (%fn rest (p ... (id var tmp)) expr ... computation))
217 ((_ (id . rest) (p ...) expr ... computation)
218 (%fn rest (p ... (id id tmp)) expr ... computation))))
219
220 (define-syntax computation-with
221 (syntax-rules ()
222 ((_ ((var val) ...) a* ... a)
223 (%with ((var val) ...) () () a* ... a))))
224
225 (define-syntax %with
226 (syntax-rules ()
227 ((_ () ((x u) ...) ((a b) ...))
228 (let ((u x) ... (b a) ...)
229 (computation-local
230 (lambda (env)
231 (computation-environment-update env u ...) )
232 (computation-each b ...))))
233 ((_ ((var val) . rest) (p ...) () a* ...)
234 (%with rest (p ... (var u) (val v)) () a* ...))
235 ((_ () p* (q ...) a . a*)
236 (%with () p* (q ... (a b)) . a*))))
237
238 (define-syntax computation-with!
239 (syntax-rules ()
240 ((_ (var val) ...)
241 (%with! (var val) ... ()))))
242
243 (define-syntax %with!
244 (syntax-rules ()
245 ((_ ((var u val v) ...))
246 (let ((u var) ... (v val) ...)
247 (computation-bind
248 (computation-ask)
249 (lambda (env)
250 (computation-environment-update! env u v) ...
251 (computation-pure (if #f #f))))))
252 ((_ (var val) r ... (p ...))
253 (%with! r ... (p ... (var u val v))))))
254
255 (define (computation-forked a . a*)
256 (make-computation
257 (lambda (compute)
258 (let loop ((a a) (a* a*))
259 (if (null? a*)
260 (compute a)
261 (begin
262 (compute (computation-local
263 (lambda (env)
264 (computation-environment-copy env))
265 a))
266 (loop (car a*) (cdr a*))))))))
267
268 (define (computation-bind/forked computation . proc*)
269 (apply computation-bind
270 (computation-local computation-environment-copy computation)
271 proc*))
272
273 (define (computation-sequence fmt*)
274 (fold-right
275 (lambda (fmt res)
276 (computation-bind
277 res
278 (lambda (vals)
279 (computation-bind
280 fmt
281 (lambda (val)
282 (computation-pure (cons val vals)))))))
283 (computation-pure '()) fmt*))