-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.scm
264 lines (231 loc) · 6.21 KB
/
tests.scm
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
;;; SPDX-FileCopyrightText: 2025 Marc Nieper-Wißkirchen
;;; SPDX-License-Identifier: MIT
(import (srfi :248))
(import (srfi :64))
(import (rnrs io simple))
(import (rnrs records syntactic))
(import (rnrs conditions))
(test-begin "SRFI 248")
(test-equal 1
(with-unwind-handler
(lambda (obj k)
obj)
(lambda ()
(raise 1))))
(test-equal 2
(with-unwind-handler
(lambda (obj k)
obj)
(lambda ()
(raise-continuable 2))))
(test-equal 3
(with-unwind-handler
(lambda (obj k)
(+ 1 obj))
(lambda ()
(with-unwind-handler
(lambda (obj k)
(raise (+ 1 obj)))
(lambda ()
(raise 1))))))
(test-equal 4
(with-unwind-handler
(lambda (obj k)
(and (non-continuable-violation? obj)
4))
(lambda ()
(with-unwind-handler
(lambda (obj k)
(k 2))
(lambda ()
(raise 1))))))
(test-equal 5
(with-unwind-handler
(lambda (obj k)
(+ 2 (k (+ 3 obj))))
(lambda ()
(+ 1 (raise-continuable -1)))))
(test-equal 6
(with-unwind-handler
(lambda (obj k)
(+ 2 (k (k (+ 3 obj)))))
(lambda ()
(+ 1 (raise-continuable -1)))))
(test-equal 7
(guard (c [(number? c) (+ 1 c)])
(raise 6)))
(test-equal 8
(guard (c [(symbol? c) #f]
[else (+ 2 c)])
(raise 6)))
(test-equal 9
(guard (c [else c])
(guard (c [(symbol? c) #f])
(raise 9))))
(define (call-with-state proc init)
(define-condition-type &get &condition
make-get-condition get-condition?)
(define-condition-type &put &condition
make-put-condition put-condition?
(value condition-value))
(define (get) (raise-continuable (make-get-condition)))
(define (put val) (raise-continuable (make-put-condition val)))
((guard (obj k
[(get-condition? obj)
(lambda (s)
((k s) s))]
[(put-condition? obj)
(lambda (s)
((k) (condition-value obj)))])
(call-with-values (lambda () (proc get put))
(lambda arg*
(lambda (s)
(apply values arg*)))))
init))
(test-equal 10
(call-with-state
(lambda (get put)
(+ 1 (get)))
9))
(test-equal 11
(call-with-state
(lambda (get put)
(put (+ 1 (get)))
(+ 1 (get)))
9))
(test-equal 12
(call-with-state
(lambda (get put)
(call/cc
(lambda (c)
(put 13)
(c)))
(get))
12))
(test-equal (list 13 '(exit enter exit enter exit enter))
(let ([trace* '()])
(guard (obj k
[else
(let ([v (k (k obj))])
(list v trace*))])
(dynamic-wind
(lambda ()
(set! trace* (cons 'enter trace*)))
(lambda ()
(+ 1 (raise-continuable 11)))
(lambda ()
(set! trace* (cons 'exit trace*)))))))
(define make-coroutine-generator
(lambda (proc)
(define-condition-type &yield &condition
make-yield-condition yield-condition?
(value condition-value))
(define yield
(lambda (val)
(raise-continuable (make-yield-condition val))))
(define thunk
(lambda ()
(guard (c k
[(yield-condition? c)
(set! thunk k)
(condition-value c)])
(proc yield)
(eof-object))))
(lambda ()
(thunk))))
(define generator->list
(lambda (g)
(let f ()
(let ([x (g)])
(if (eof-object? x)
'()
(cons x (f)))))))
(test-equal '(1 2)
(let ([g (make-coroutine-generator
(lambda (yield)
(yield 1)
(yield 2)))])
(generator->list g)))
(define for-each->fold
(lambda (for-each)
(define-condition-type &yield &condition
make-yield-condition yield-condition?
(value condition-value))
(lambda (proc nil)
((guard (c k
[(yield-condition? c)
(lambda (s)
((k) (proc s (condition-value c))))])
(for-each
(lambda (x)
(raise-continuable (make-yield-condition x))))
values)
nil))))
(define (xcons x y) (cons y x))
(test-equal '(3 2 1)
((for-each->fold
(lambda (f)
(for-each f '(1 2 3))))
xcons '()))
;; prompt0 and control0
(define (empty-continuation? k) #f) ;not supported by Guile, so conservative approximation
(define-condition-type &control-condition &condition
make-control-condition control-condition?
(control condition-control))
(define-record-type prompt
(fields first second))
(define-syntax first
(syntax-rules ()
((first e)
(call-with-values (lambda () e)
(case-lambda
((p)
(if (prompt? p)
((prompt-first p))
p))
(val* (apply values val*)))))))
(define-syntax second
(syntax-rules ()
((second e)
(call-with-values (lambda () e)
(case-lambda
((p) (if (prompt? p)
((prompt-second p))
p))
(val* (apply values val*)))))))
(define-syntax prompt0
(syntax-rules ()
((prompt0 e)
(second
(guard
(c k
((control-condition? c)
(if (empty-continuation? k)
(make-prompt
(lambda ()
(raise-continuable c))
(lambda ()
((condition-control c) values)))
(let ((k (lambda arg* (first (apply k arg*)))))
(make-prompt
(lambda ()
(call-with-values
(lambda () (raise-continuable c))
k))
(lambda ()
((condition-control c) k)))))))
e)))))
(define-syntax control0
(syntax-rules ()
((control0 k e)
(raise-continuable
(make-control-condition
(lambda (k) e))))))
(test-eqv 1 (prompt0 1))
(test-eqv 2 (prompt0 (+ 1 (control0 k 2))))
(test-eqv 4 (prompt0 (+ 1 (control0 k (k (k 2))))))
(test-eqv 0 (prompt0 (+ 4 (prompt0 (+ 1 (let ((x (control0 k (k 0)))) (control0 l x)))))))
(test-end)
;; Local Variables:
;; eval: (put 'with-unwind-handler 'scheme-indent-function 1)
;; End: