forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.ss
426 lines (402 loc) · 15.3 KB
/
4.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"4.ss"
;;; 4.ss
;;; Copyright 1984-2016 Cisco Systems, Inc.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
(define apply
(let ()
(define-syntax build-apply
(lambda (x)
(syntax-case x ()
[(_ () cl ...)
#'(case-lambda
[(p r)
(unless (procedure? p)
($oops #f "attempt to apply non-procedure ~s" p))
(let ([n ($list-length r 'apply)])
(case n
[(0) (p)]
[(1) (p (car r))]
[(2) (p (car r) (cadr r))]
[(3) (let ([y1 (cdr r)]) (p (car r) (car y1) (cadr y1)))]
[else ($apply p n r)]))]
cl ...
[(p x . r)
(unless (procedure? p)
($oops #f "attempt to apply non-procedure ~s" p))
(let ([r (cons x ($apply list* ($list-length r 'apply) r))])
($apply p ($list-length r 'apply) r))])]
[(_ (s1 s2 ...) cl ...)
(with-syntax ((m (length #'(s1 s2 ...))))
#'(build-apply
(s2 ...)
[(p s1 s2 ... r)
(unless (procedure? p)
($oops #f "attempt to apply non-procedure ~s" p))
(let ([n ($list-length r 'apply)])
(case n
[(0) (p s1 s2 ...)]
[(1) (p s1 s2 ... (car r))]
[(2) (p s1 s2 ... (car r) (cadr r))]
[(3) (let ([y1 (cdr r)])
(p s1 s2 ... (car r) (car y1) (cadr y1)))]
[else ($apply p (fx+ n m) (list* s1 s2 ... r))]))]
cl ...))])))
(build-apply (x1 x2 x3 x4))))
(define ormap)
(define andmap)
(define map)
(define for-each)
(define fold-left)
(define fold-right)
(let ()
(define length-error
(lambda (who l1 l2)
($oops who "lists ~s and ~s differ in length" l1 l2)))
(define nonprocedure-error
(lambda (who what)
($oops who "~s is not a procedure" what)))
(define length-check
(lambda (who first rest)
(let ([n ($list-length first who)])
(let loop ([rest rest])
(cond
[(null? rest) n]
[(fx= ($list-length (car rest) who) n) (loop (cdr rest))]
[else (length-error who first (car rest))])))))
(define mutation-error
(lambda (who)
($oops who "input list was altered during operation")))
; getcxrs returns the cdrs of ls and their cars
(define getcxrs
(lambda (ls who)
(if (null? ls)
(values '() '())
(let-values ([(cdrs cars) (getcxrs (cdr ls) who)])
(let ([d (cdar ls)])
(unless (pair? d) (mutation-error who))
(values (cons d cdrs) (cons (car d) cars)))))))
(let ()
(define-syntax do-ormap
(syntax-rules ()
[(_ who)
(case-lambda
[(f ls)
(unless (procedure? f) (nonprocedure-error who f))
(and (not (null? ls))
(let ormap ([n ($list-length ls who)] [ls ls])
(if (fx= n 1)
(f (car ls))
(or (f (car ls))
(let ([ls (cdr ls)])
(unless (pair? ls) (mutation-error who))
(ormap (fx- n 1) ls))))))]
[(f ls . more)
(unless (procedure? f) (nonprocedure-error who f))
(let ([n (length-check who ls more)])
(and (not (fx= n 0))
(let ormap ([n n] [ls ls] [more more] [cars (map car more)])
(if (fx= n 1)
(apply f (car ls) cars)
(or (apply f (car ls) cars)
(let ([ls (cdr ls)])
(unless (pair? ls) (mutation-error who))
(let-values ([(cdrs cars) (getcxrs more who)])
(ormap (fx- n 1) ls cdrs cars))))))))])]))
(set-who! ormap (do-ormap who))
(set-who! exists (do-ormap who)))
(let ()
(define-syntax do-andmap
(syntax-rules ()
[(_ who)
(case-lambda
[(f ls)
(unless (procedure? f) (nonprocedure-error who f))
(or (null? ls)
(let andmap ([n ($list-length ls who)] [ls ls])
(if (fx= n 1)
(f (car ls))
(and (f (car ls))
(let ([ls (cdr ls)])
(unless (pair? ls) (mutation-error who))
(andmap (fx- n 1) ls))))))]
[(f ls . more)
(unless (procedure? f) (nonprocedure-error who f))
(let ([n (length-check who ls more)])
(or (fx= n 0)
(let andmap ([n n] [ls ls] [more more] [cars (map car more)])
(if (fx= n 1)
(apply f (car ls) cars)
(and (apply f (car ls) cars)
(let ([ls (cdr ls)])
(unless (pair? ls) (mutation-error who))
(let-values ([(cdrs cars) (getcxrs more who)])
(andmap (fx- n 1) ls cdrs cars))))))))])]))
(set-who! andmap (do-andmap who))
(set-who! for-all (do-andmap who)))
(set! map
(case-lambda
[(f ls)
(unless (procedure? f) (nonprocedure-error 'map f))
($list-length ls 'map)
; library map cdrs first to avoid getting sick if f mutates input
(#3%map f ls)]
[(f ls1 ls2)
(unless (procedure? f) (nonprocedure-error 'map f))
(unless (fx= ($list-length ls1 'map) ($list-length ls2 'map))
(length-error 'map ls1 ls2))
; library map cdrs first to avoid getting sick if f mutates input
(#3%map f ls1 ls2)]
[(f ls . more)
(unless (procedure? f) (nonprocedure-error 'map f))
(length-check 'map ls more)
(let map ([f f] [ls ls] [more more])
(if (null? ls)
'()
; cdr first to avoid getting sick if f mutates input
(let ([tail (map f (cdr ls) (#3%map cdr more))])
(cons (apply f (car ls) (#3%map car more)) tail))))]))
(set! $map
; same as map but errors are reported as coming from who
(case-lambda
[(who f ls)
(unless (procedure? f) (nonprocedure-error who f))
($list-length ls who)
; library map cdrs first to avoid getting sick if f mutates input
(#3%map f ls)]
[(who f ls1 ls2)
(unless (procedure? f) (nonprocedure-error who f))
(unless (fx= ($list-length ls1 who) ($list-length ls2 who))
(length-error who ls1 ls2))
; library map cdrs first to avoid getting sick if f mutates input
(#3%map f ls1 ls2)]
[(who f ls . more)
(unless (procedure? f) (nonprocedure-error who f))
(length-check who ls more)
(let map ([f f] [ls ls] [more more])
(if (null? ls)
'()
; cdr first to avoid getting sick if f mutates input
(let ([tail (map f (cdr ls) (#3%map cdr more))])
(cons (apply f (car ls) (#3%map car more)) tail))))]))
(set! for-each
(case-lambda
[(f ls)
(unless (procedure? f) (nonprocedure-error 'for-each f))
(unless (null? ls)
(let for-each ([n ($list-length ls 'for-each)] [ls ls])
(if (fx= n 1)
(f (car ls))
(begin
(f (car ls))
(let ([ls (cdr ls)])
(unless (pair? ls) (mutation-error 'for-each))
(for-each (fx- n 1) ls))))))]
[(f ls . more)
(unless (procedure? f) (nonprocedure-error 'for-each f))
(let ([n (length-check 'for-each ls more)])
(unless (fx= n 0)
(let for-each ([n n] [ls ls] [more more] [cars (map car more)])
(if (fx= n 1)
(apply f (car ls) cars)
(begin
(apply f (car ls) cars)
(let ([ls (cdr ls)])
(unless (pair? ls) (mutation-error 'for-each))
(let-values ([(cdrs cars) (getcxrs more 'for-each)])
(for-each (fx- n 1) ls cdrs cars))))))))]))
(set! fold-left
(case-lambda
[(combine nil ls)
(unless (procedure? combine) (nonprocedure-error 'fold-left combine))
(cond
[(null? ls) nil]
[else
($list-length ls 'fold-left)
(let fold-left ([ls ls] [acc nil])
(let ([cdrls (cdr ls)])
(if (pair? cdrls)
(fold-left cdrls (combine acc (car ls)))
(if (null? cdrls)
(combine acc (car ls))
(mutation-error 'fold-left)))))])]
[(combine nil ls . more)
(unless (procedure? combine) (nonprocedure-error 'fold-left combine))
(length-check 'fold-left ls more)
(if (null? ls)
nil
(let fold-left ([ls ls] [more more] [cars (map car more)] [acc nil])
(let ([cdrls (cdr ls)])
(if (null? cdrls)
(apply combine acc (car ls) cars)
(let ([acc (apply combine acc (car ls) cars)])
(unless (pair? cdrls) (mutation-error 'fold-left))
(let-values ([(cdrs cars) (getcxrs more 'fold-left)])
(fold-left cdrls cdrs cars acc)))))))]))
(set! fold-right
(case-lambda
[(combine nil ls)
(unless (procedure? combine) (nonprocedure-error 'fold-right combine))
($list-length ls 'fold-right)
; #3%fold-right naturally does cdrs first to avoid mutation sickness
(#3%fold-right combine nil ls)]
[(combine nil ls1 ls2)
(unless (procedure? combine) (nonprocedure-error 'fold-right combine))
(unless (fx= ($list-length ls1 'map) ($list-length ls2 'map))
(length-error 'fold-right ls1 ls2))
; #3%fold-right naturally does cdrs first to avoid mutation sickness
(#3%fold-right combine nil ls1 ls2)]
[(combine nil ls . more)
(unless (procedure? combine) (nonprocedure-error 'fold-right combine))
(length-check 'fold-right ls more)
(let fold-right ([combine combine] [nil nil] [ls ls] [more more])
(if (null? ls)
nil
(apply combine (car ls)
(#3%fold-right cons
(list (fold-right combine nil (cdr ls) (map cdr more)))
(map car more)))))]))
)
(let ()
(define disable/enable (make-winder #f disable-interrupts enable-interrupts))
(define (dwind in body out)
(let ((old-winders ($current-winders)))
(in)
($current-winders (cons (make-winder #f in out) old-winders))
(call-with-values
body
(case-lambda
[(x)
($current-winders old-winders)
(out)
x]
[args
($current-winders old-winders)
(out)
(apply values args)]))))
(define (cwind in body out)
(let* ((old-winders ($current-winders))
[d/e+old-winders (cons disable/enable old-winders)])
(disable-interrupts)
($current-winders d/e+old-winders)
(in)
($current-winders (cons (make-winder #t in out) old-winders))
(enable-interrupts)
(call-with-values
body
(case-lambda
[(x)
(disable-interrupts)
($current-winders d/e+old-winders)
(out)
($current-winders old-winders)
(enable-interrupts)
x]
[args
(disable-interrupts)
($current-winders d/e+old-winders)
(out)
($current-winders old-winders)
(enable-interrupts)
(apply values args)]))))
(define (check-args in body out)
(unless (procedure? in)
($oops 'dynamic-wind "~s is not a procedure" in))
(unless (procedure? body)
($oops 'dynamic-wind "~s is not a procedure" body))
(unless (procedure? out)
($oops 'dynamic-wind "~s is not a procedure" out)))
(set! dynamic-wind
(case-lambda
[(in body out)
(check-args in body out)
(dwind in body out)]
[(critical? in body out)
(check-args in body out)
(if critical?
(cwind in body out)
(dwind in body out))]))
(set-who! #(r6rs: dynamic-wind)
(lambda (in body out)
(#2%dynamic-wind in body out)))
(set! $do-wind
(lambda (old new)
(define common-tail
(lambda (x y)
(let ([lx (length x)] [ly (length y)])
(do ([x (if (fx> lx ly) (list-tail x (fx- lx ly)) x) (cdr x)]
[y (if (fx> ly lx) (list-tail y (fx- ly lx)) y) (cdr y)])
((eq? x y) x)))))
(let ([tail (common-tail old new)])
(let f ((old old))
(unless (eq? old tail)
(let ([w (car old)] [old (cdr old)])
(if (winder-critical? w)
(begin
(disable-interrupts)
($current-winders (cons disable/enable old))
((winder-out w))
($current-winders old)
(enable-interrupts))
(begin
($current-winders old)
((winder-out w))))
(f old))))
(let f ([new new])
(unless (eq? new tail)
(let ([w (car new)])
(f (cdr new))
(if (winder-critical? w)
(begin
(disable-interrupts)
($current-winders (cons disable/enable (cdr new)))
((winder-in w))
($current-winders new)
(enable-interrupts))
(begin
((winder-in w))
($current-winders new)))))))))
)
;;; make-promise and force
(define $make-promise
(lambda (thunk)
(unless (procedure? thunk)
($oops '$make-promise "~s is not a procedure" thunk))
(let ([value (void)] [set? #f])
(lambda ()
(case set?
[(single) value]
[(multiple) (apply values value)]
[else
(call-with-values
thunk
(case-lambda
[(x)
(case set?
[(single) value]
[(multiple) (apply values value)]
[(#f) (set! value x)
(set! set? 'single)
x])]
[x
(case set?
[(single) value]
[(multiple) (apply values value)]
[(#f) (set! value x)
(set! set? 'multiple)
(apply values x)])]))])))))
(define force
(lambda (promise)
(unless (procedure? promise)
($oops 'force "~s is not a procedure" promise))
(promise)))