-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgImperSem008.scm
436 lines (377 loc) · 11.9 KB
/
AlgImperSem008.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
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
427
428
429
430
431
432
433
434
435
436
@; scheme-r5rs
;; runs only in gambit (gsi AlgImperSem008.smc)
;; (define-syntax bugger-or
;; (syntax-rules ()
;; [(_) #f]
;; [(_ e) e]
;; [(_ e1 e2 e3 ...)
;; (let ([t e1]) (if t t (or e2 e3 ...)))]))
;; (define-syntax echo
;; (syntax-rules ()
;; [(_ e) (begin (print e)
;; (newline)
;; e)]))
(define (separator banner)
(display "----------------------------------------------------------------")
(newline)
(display banner)
(newline))
;; ___ ___ _ _ ___
;; | __/ __| || |/ _ \
;; | _| (__| __ | (_) |
;; |___\___|_||_|\___/
(separator "ECHO")
(define (echo str expr)
(pp str)
(newline)
expr)
(define lambda-test (lambda () 42))
(pp (lambda-test))
;;; Test environment inspection in the Gambit debugger.
;;; https://gambitscheme.org/latest/manual/#index-_002ddebug_002denvironments
;;; Uncomment the following, load the file, and type ",e" in the
;;; REPL to see the binding environment. The chain is not
;;; explicitly revealed. Type ",b" to print the continuation frames.
;; (let ((x 42))
;; (let ((y (- 1 1)))
;; (* (/ x y) 2)))
(define (fact n)
(if (< n 2)
1
(* n (fact (- n 1)))))
;;; After loading this file, type "(trace fact)<ENTER>(fact 5)<ENTER>" in the
;;; REPL.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(pp ((lambda (m)
((lambda (n) (* n n))
m))
42))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(pp ((lambda (m)
((lambda (n) (* n m)) ; DIFFERENT!
m))
42))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(pp
(((lambda (m)
(lambda (n) (* n m))) 42) 42))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define foo
(lambda (m)
(lambda (n) (* n m))))
(pp foo)
(pp (foo 42))
(pp ((foo 42) 42))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define foo
(lambda (n) (* n m)))
(with-exception-handler
pp
(lambda () (pp (foo 43))))
(with-exception-handler
pp
(lambda () (pp ((foo 43) 42))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define foo
(lambda (n)
(lambda (n) (* n n))))
(pp foo)
(pp (foo 43))
(pp ((foo 43) 42))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(((lambda (f) f) square) 42)
(((lambda (f) f) (lambda (x) (* x x))) 42)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ___ ___ _ _ _ ___ ___ ___ ___ ___ _____ ___
;; / __|/ _ \| | | |/_\ | _ \ __| | _ \/ _ \ / _ \_ _/ __|
;; \__ \ (_) | |_| / _ \| / _| | / (_) | (_) || | \__ \
;; |___/\__\_\\___/_/ \_\_|_\___| |_|_\\___/ \___/ |_| |___/
(separator "SQUARE ROOTS")
(print 'A)
(display ": Square a named square root.")
(newline)
(define foo
(lambda (sf)
(lambda (n)
(if (< n 1)
1
(* n ((sf sf) (- n 1)))))))
(pp ((foo foo) 6))
(print 'B)
(display ": Square an anonymous square root.")
(newline)
(pp (((lambda (sf)
(lambda (n)
(if (< n 1)
1
(* n ((sf sf) (- n 1))))))
(lambda (sf)
(lambda (n)
(if (< n 1)
1
(* n ((sf sf) (- n 1)))))))
6))
(print 'C)
(display ": Abstract business code f into
delayed application of the square.")
(newline)
(pp (((lambda (sf)
((lambda (f)
(lambda (n)
(if (< n 1)
1
(* n (f (- n 1))))))
(lambda (m) ((sf sf) m))))
(lambda (sf)
((lambda (f)
(lambda (n)
(if (< n 1)
1
(* n (f (- n 1))))))
(lambda (m) ((sf sf) m)))))
6))
(print 'D)
(display ": Abstract the squaring into function of domain code d.")
(newline)
(pp (((lambda (sf)
((lambda (d)
(d (lambda (m) ((sf sf) m))))
(lambda (f)
(lambda (n)
(if (< n 1)
1
(* n (f (- n 1))))))))
(lambda (sf)
((lambda (d)
(d (lambda (m) ((sf sf) m))))
(lambda (f)
(lambda (n)
(if (< n 1)
1
(* n (f (- n 1)))))))))
6))
(print 'E)
(display ": Involute function of domain code d and squaring.")
(newline)
(pp (((lambda (d)
((lambda (sf)
(d (lambda (m) ((sf sf) m))))
(lambda (sf)
(d (lambda (m) ((sf sf) m))))))
(lambda (f)
(lambda (n)
(if (< n 1)
1
(* n (f (- n 1)))))))
6))
(print 'F)
(display ": Abstract second squaring into function of g.")
(newline)
(pp (((lambda (d)
((lambda (g) (g g))
(lambda (sf)
(d (lambda (m) ((sf sf) m))))))
(lambda (f)
(lambda (n)
(if (< n 1)
1
(* n (f (- n 1)))))))
6))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; _ _ ___ ___ ___ _ ___ _ _ ___
;; | | | | _ \/ __|_ _| | / _ \| \| / __|
;; | |_| | _/\__ \| || |_| (_) | .` \__ \
;; \___/|_| |___/___|____\___/|_|\_|___/
(separator "UPSILONS")
(define (fact-recursive f)
"domain code; Receives from Y or LOOP a recursive version of
itself as parameter f. Domain code must return a business code, b,
of 1 parameter that closes over f as a free variable and may
invoke f, also a function of 1 business parameter."
(lambda (n) (if (< n 1)
1
(* n (f (- n 1))))))
(define (Y1 d)
"d is domain code, a function that receives business code as a
parameter f. Business code, b, is a function of 1 business
parameter, m. Business code may refer to itself through the
parameter f of d, a free variable in the body of b."
((lambda (g) (g g))
(lambda (sf) (d (lambda (m) ((sf sf) m))))))
(display "Y1 on fact-recursive") (newline)
(pp ((Y1 fact-recursive) 6))
(define (fact-iter f)
"domain code; Receives from Y or LOOP a recursive version of
itself as parameter f. Domain code must return a business code, b,
of 3 parameters that closes over f as a free variable and may
invoke f, also a function of 3 business parameters."
(lambda (m c x)
(if (> c x)
m
(f (* m c) (+ c 1) x))))
(define (Y3 d)
"d is domain code, a function that receives business code as a
parameter f. Business code, b, is a function of 3 business
parameters, m, c, x. Business code may refer to itself through the
parameter f of d, a free variable in the body of b."
((lambda (g) (g g))
(lambda (sf) (d (lambda (m c x)
((sf sf) m c x))))))
(display "Y3 on fact-iter") (newline)
(pp ((Y3 fact-iter) 1 1 6))
(define (YN d)
((lambda (g) (g g))
(lambda (sf) (d (lambda L
(apply (sf sf) L))))))
(display "YN on fact-iter") (newline)
(pp ((YN fact-iter) 1 1 6))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; _ ___ ___ ___ ___
;; | | / _ \ / _ \| _ \/ __|
;; | |_| (_) | (_) | _/\__ \
;; |____\___/ \___/|_| |___/
(separator "LOOPS")
(define (LOOP3 d)
;; P3 calls looper with new args:
(define (P3 m+ c+ x+)
(raise (list m+ c+ x+)))
(define (looper m c x)
(with-exception-handler
;; This is how P3 calls looper, indirectly:
(lambda (e)
(apply looper e))
;; Pass P3 to domain code. If domain code calls P3,
;; looper recurses, otherwise returns result below.
(lambda () ; With-exception-handler requires thunk.
((d P3) m c x))))
looper)
(display "exception-based LOOP3 on 3-arg print") (newline)
(pp ((LOOP3 (lambda (f)
(lambda (m c x)
(pp (list m c x)))))
1 2 3))
(display "exception-based LOOP3 on fact-iter") (newline)
(pp ((LOOP3 fact-iter) 1 1 6))
;;; If d calls P3, then loop; else return ((d P3) m c x).
(define (LOOP3 d)
(define (P3 m c x)
(call/cc (lambda (k)
(k ((d P3) m c x)))))
P3)
(display "call/cc-based LOOP3 on fact-iter") (newline)
(pp ((LOOP3 fact-iter) 1 1 6))
(define (LOOPN d)
(define (PN . L)
(call/cc (lambda (k)
(k (apply (d PN) L)))))
PN)
(display "call/cc-based LOOPN on fact-iter") (newline)
(pp ((LOOPN fact-iter) 1 1 6))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; _____ _ _ __
;; |_ _/_\ | |/ /
;; | |/ _ \| ' <
;; |_/_/ \_\_|\_\
(separator "TAK")
(define (itak c x y z)
(let ((itakx (lambda (c) (itak c (- x 1) y z)))
(itaky (lambda (c) (itak c (- y 1) z x)))
(itakz (lambda (c) (itak c (- z 1) x y))))
(if (< y x)
(let* ((lx (itakx (+ c 1)))
(ly (itaky (+ (car lx) 1)))
(lz (itakz (+ (car ly) 1))))
(itak (+ (car lz) 1) (cadr lx) (cadr ly) (cadr lz)))
(list c z))))
(pp (itak 1 3 2 1)) ; (5 2)
(pp (itak 1 12 8 4)) ; (1733 5)
(pp (itak 1 18 12 6)) ; (63609 7)
;; (pp (itak 1 28 20 12)) ; (2493349 13)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ____ ___ ____ ____
;; / ___|_ _/ ___| _ \
;; \___ \| | | | |_) |
;; ___) | | |___| __/
;; |____/___\____|_|
;; ___ _ ___
;; / __| |_ | __|
;; | (__| ' \ |__ \
;; \___|_||_| |___/
;; (define (gcd a b)
;; (if (= b 0)
;; a
;; (gcd b (remainder a b))))
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
;;; Exercise 5.1:
;; +-----+ +--------+ +-----+
;; \ 1 / | | \ 1 /
;; \ / | V \ /
;; | | .--------. |
;; | | \ +1 / |
;; (X) p<-1 | \_____/ (X) c<-1
;; | | | |
;; | | (X) c<-c++ |
;; | | | |
;; v | V |
;; +-----+ | +-----+ | +-----+
;; +->| p | +-----| c |<------+ | n |
;; | +-----+ +-----+ +-----+
;; | | | |
;; | +------+ +------+------+ +------+
;; | | | | |
;; | .--+---+--. .--+---+--.
;; | \ * / \ < /
;; | \_____/ \_____/
;; | | |
;; (X) p<-t (X) t<-pc (X) t<-lt
;; | | |
;; | | V
;; | | +-----+
;; | +------------->| |
;; +-----------------------------| t |
;; | |
;; +-----+
;; |
;; |
;; V
;; ---
;; / \
;; | #t |
;; \___/
;; start
;; |
;; V
;; +-------+
;; | c<-1 |
;; +-------+
;; |
;; V
;; +-------+
;; | p<-1 |
;; +-------+
;; |
;; V
;; +-------+
;; | t<-lt |
;; +-------+
;; |
;; V
;; / \ yes
;; +-------->< < >-----> done (answer in reg. p)
;; | \ /
;; | | no
;; | V
;; | +-------+
;; | | t<-pc |
;; | +-------+
;; | |
;; | V
;; +-------+ +-------+
;; | c<-c++|<--| p<-t |
;; +-------+ +-------+