blob: aace027496d7126b00d4ab0ef1308d9f30674473 (
plain)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
|
#lang sicp
(#%require (only racket/base print-as-expression print-mpair-curly-braces))
(print-as-expression #f)
(print-mpair-curly-braces #f)
;; Chapter 4
;; Metalinguistic Abstraction
;; 4.3
;; Variations on a Scheme -- Nondeterministic Computing
;; Amb and Search
(#%provide prime?-def)
(define prime?-def
'(define (prime? n)
(define (next n)
(if (= n 2) 3 (+ n 2)))
(define (square x) (* x x))
(define (divides? a b)
(= (remainder b a) 0))
(define (find-divisor n test-divisor)
(cond
((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (smallest-divisor n)
(find-divisor n 2))
(= n (smallest-divisor n))))
(#%provide require-def)
(define require-def
'(define (require p)
(if (not p) (amb))))
(#%provide prime-sum-pair-def)
(define prime-sum-pair-def
'(define (prime-sum-pair list1 list2)
(let
((a (an-element-of list1))
(b (an-element-of list2)))
(require (prime? (+ a b)))
(list a b))))
(#%provide an-element-of-def)
(define an-element-of-def
'(define (an-element-of items)
(require (not (null? items)))
(amb (car items) (an-element-of (cdr items)))))
(#%provide an-integer-starting-from-def)
(define an-integer-starting-from-def
'(define (an-integer-starting-from n)
(amb n (an-integer-starting-from (+ n 1)))))
#| 4.35 |#
(#%provide an-integer-between-def)
(define an-integer-between-def
'(define (an-integer-between low high)
(require (<= low high))
(amb low (an-integer-between (+ low 1) high))))
(#%provide a-pythagorean-triple-between-def)
(define a-pythagorean-triple-between-def
'(define (a-pythagorean-triple-between low high)
(let ((i (an-integer-between low high)))
(let ((j (an-integer-between i high)))
(let ((k (an-integer-between j high)))
(require (= (+ (* i i) (* j j)) (* k k)))
(list i j k))))))
#| 4.36 |#
(#%provide a-pythagorean-triple-bad-def)
(define a-pythagorean-triple-bad-def
'(define (a-pythagorean-triple-bad)
(let ((i (an-integer-starting-from 1)))
(let ((j (an-integer-starting-from i)))
(let ((k (an-integer-starting-from j)))
(require (= (+ (* i i) (* j j)) (* k k)))
(list i j k))))))
;; there is not a valid k for every value of i j
;; the procedure would get stuck trying new values
;; of k forever
(#%provide a-pythagorean-triple-def)
(define a-pythagorean-triple-def
'(define (a-pythagorean-triple)
(let ((k (an-integer-starting-from 1)))
(let ((i (an-integer-between 1 k)))
(let ((j (an-integer-between i k)))
(require (= (+ (* i i) (* j j)) (* k k)))
(list i j k))))))
#| 4.37 |#
(#%provide a-pythagorean-triple-between-fast-def)
(define a-pythagorean-triple-between-fast-def
'(define (a-pythagorean-triple-between-fast low high)
(let
((i (an-integer-between low high))
(hsq (* high high)))
(let ((j (an-integer-between i high)))
(let ((ksq (+ (* i i) (* j j))))
(require (>= hsq ksq))
(let ((k (sqrt ksq)))
(require (integer? k))
(list i j k)))))))
;; this version explores fewer possibilities
;; Examples of Nondeterministic Programs
(#%provide distinct?-def)
(define distinct?-def
'(define (distinct? items)
(cond
((null? items) true)
((null? (cdr items)) true)
((member (car items) (cdr items)) false)
(else (distinct? (cdr items))))))
(#%provide multiple-dwelling-def)
(define multiple-dwelling-def
'(define (multiple-dwelling)
(let
((baker (amb 1 2 3 4 5))
(cooper (amb 1 2 3 4 5))
(fletcher (amb 1 2 3 4 5))
(miller (amb 1 2 3 4 5))
(smith (amb 1 2 3 4 5)))
(require
(distinct? (list baker cooper fletcher miller smith)))
(require (not (= baker 5)))
(require (not (= cooper 1)))
(require (not (= fletcher 5)))
(require (not (= fletcher 1)))
(require (> miller cooper))
(require (not (= (abs (- smith fletcher)) 1)))
(require (not (= (abs (- fletcher cooper)) 1)))
(list
(list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith)))))
#| 4.38 |#
(#%provide multiple-dwelling-mod-def)
(define multiple-dwelling-mod-def
'(define (multiple-dwelling-mod)
(let
((baker (amb 1 2 3 4 5))
(cooper (amb 1 2 3 4 5))
(fletcher (amb 1 2 3 4 5))
(miller (amb 1 2 3 4 5))
(smith (amb 1 2 3 4 5)))
(require
(distinct? (list baker cooper fletcher miller smith)))
(require (not (= baker 5)))
(require (not (= cooper 1)))
(require (not (= fletcher 5)))
(require (not (= fletcher 1)))
(require (> miller cooper))
(require (not (= (abs (- fletcher cooper)) 1)))
(list
(list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith)))))
#| 4.39 |#
(#%provide multiple-dwelling-reorder-def)
(define multiple-dwelling-reorder-def
'(define (multiple-dwelling-reorder)
(let
((baker (amb 1 2 3 4 5))
(cooper (amb 1 2 3 4 5))
(fletcher (amb 1 2 3 4 5))
(miller (amb 1 2 3 4 5))
(smith (amb 1 2 3 4 5)))
(require (> miller cooper))
(require (not (= (abs (- smith fletcher)) 1)))
(require (not (= (abs (- fletcher cooper)) 1)))
(require
(distinct? (list baker cooper fletcher miller smith)))
(require (not (= baker 5)))
(require (not (= cooper 1)))
(require (not (= fletcher 5)))
(require (not (= fletcher 1)))
(list
(list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith)))))
#| 4.40 |#
(#%provide multiple-dwelling-quick-def)
(define multiple-dwelling-quick-def
'(define (multiple-dwelling-quick)
(let
((baker (amb 1 2 3 4 5)))
(require (not (= baker 5)))
(let ((cooper (amb 1 2 3 4 5)))
(require (not (= cooper 1)))
(require (not (= cooper baker)))
(let ((fletcher (amb 1 2 3 4 5)))
(require (not (= (abs (- fletcher cooper)) 1)))
(require (not (= fletcher 1)))
(require (not (= fletcher 5)))
(require (not (= fletcher baker)))
(require (not (= fletcher cooper)))
(let ((miller (amb 1 2 3 4 5)))
(require (> miller cooper))
(require (not (= miller baker)))
(require (not (= miller cooper)))
(require (not (= miller fletcher)))
(let ((smith (amb 1 2 3 4 5)))
(require (not (= (abs (- smith fletcher)) 1)))
(require (not (= smith baker)))
(require (not (= smith cooper)))
(require (not (= smith fletcher)))
(require (not (= smith miller)))
(list
(list 'baker baker)
(list 'cooper cooper)
(list 'fletcher fletcher)
(list 'miller miller)
(list 'smith smith)))))))))
#| 4.41 |#
(define (with-next value prev proc)
(if (null? value)
(prev)
(proc (car value) (cdr value))))
(define (require test this next)
(lambda (x value)
(if (not (test x))
(this value)
(next x value))))
(#%provide multiple-dwelling-scheme)
(define (multiple-dwelling-scheme)
(define (fail) (error "No solution"))
(define (bakerfunc bakers)
(with-next bakers fail
(require (lambda (b) (not (= b 5))) bakerfunc
(lambda (baker bakers)
(define (cooperfunc coopers)
(with-next coopers (lambda () (bakerfunc bakers))
(require (lambda (c) (not (= c 1))) cooperfunc
(require (lambda (c) (not (= c baker))) cooperfunc
(lambda (cooper coopers)
(define (fletcherfunc fletchers)
(with-next fletchers (lambda () (cooperfunc coopers))
(require (lambda (f) (not (= (abs (- f cooper)) 1))) fletcherfunc
(require (lambda (f) (not (= f 1))) fletcherfunc
(require (lambda (f) (not (= f 5))) fletcherfunc
(require (lambda (f) (not (= f baker))) fletcherfunc
(require (lambda (f) (not (= f cooper))) fletcherfunc
(lambda (fletcher fletchers)
(define (millerfunc millers)
(with-next millers (lambda () (fletcherfunc fletchers))
(require (lambda (m) (> m cooper)) millerfunc
(require (lambda (m) (not (= m baker))) millerfunc
(require (lambda (m) (not (= m cooper))) millerfunc
(require (lambda (m) (not (= m fletcher))) millerfunc
(lambda (miller millers)
(define (smithfunc smiths)
(with-next smiths (lambda () (millerfunc millers))
(require (lambda (s) (not (= (abs (- s fletcher)) 1))) smithfunc
(require (lambda (s) (not (= s baker))) smithfunc
(require (lambda (s) (not (= s cooper))) smithfunc
(require (lambda (s) (not (= s fletcher))) smithfunc
(require (lambda (s) (not (= s miller))) smithfunc
(lambda (smith smiths)
(list
(list 'baker baker bakers)
(list 'cooper cooper coopers)
(list 'fletcher fletcher fletchers)
(list 'miller miller millers)
(list 'smith smith smiths))))))))))
(smithfunc (list 1 2 3 4 5)))))))))
(millerfunc (list 1 2 3 4 5))))))))))
(fletcherfunc (list 1 2 3 4 5)))))))
(cooperfunc (list 1 2 3 4 5))))))
(bakerfunc (list 1 2 3 4 5)))
#| 4.42 |#
(#%provide liars-def)
(define liars-def
'(define (liars)
(define (one-lie x y)
(amb
(begin (require x) (require (not y)))
(begin (require (not x)) (require y))))
(let
((betty (amb 1 2 3 4 5))
(ethel (amb 1 2 3 4 5))
(joan (amb 1 2 3 4 5))
(kitty (amb 1 2 3 4 5))
(mary (amb 1 2 3 4 5)))
(require (distinct? (list betty ethel joan kitty mary)))
(one-lie (= kitty 2) (= betty 3))
(one-lie (= ethel 1) (= joan 2))
(one-lie (= joan 3) (= ethel 5))
(one-lie (= kitty 2) (= mary 4))
(one-lie (= mary 4) (= betty 1))
(list
(list 'betty betty)
(list 'ethel ethel)
(list 'joan joan)
(list 'kitty kitty)
(list 'mary mary)))))
#| 4.43 |#
(#%provide map-def)
(define map-def
'(define (map f xs)
(if (null? xs)
'()
(cons (f (car xs)) (map f (cdr xs))))))
(#%provide yachts-def)
(define yachts-def
'(define (yachts)
(define (daughters) (amb 'mary-ann 'gabrielle 'lorna 'rosalind 'melissa))
(define (yachts-) (amb 'mary-ann 'gabrielle 'lorna 'rosalind 'melissa))
(define (name man) (car man))
(define (daughter man) (cadr man))
(define (father men girl)
(cond
((null? men) false)
((eq? (daughter (car men)) girl) (car men))
(else (father (cdr men) girl))))
(define (yacht man) (caddr man))
(let
((mr-moore (list 'mr-moore (daughters) (yachts-)))
(colonel-downing (list 'colonel-downing (daughters) (yachts-)))
(mr-hall (list 'mr-hall (daughters) (yachts-)))
(sir-barnacle-hood (list 'sir-barnacle-hood (daughters) (yachts-)))
(dr-parker (list 'dr-parker (daughters) (yachts-))))
(let
((men
(list
mr-moore
colonel-downing
mr-hall
sir-barnacle-hood
dr-parker)))
(require (eq? (daughter mr-moore) 'mary-ann))
(require (eq? (yacht sir-barnacle-hood) 'gabrielle))
(require (eq? (yacht mr-moore) 'lorna))
(require (eq? (yacht mr-hall) 'rosalind))
(require (eq? (yacht colonel-downing) 'melissa))
(require (eq? (daughter sir-barnacle-hood) 'melissa))
(require (distinct? (map daughter men)))
(require (distinct? (map yacht men)))
(require
(eq? (yacht (father men 'gabrielle)) (daughter dr-parker)))
(name (father men 'lorna))))))
#| 4.44 |#
(#%provide show-row-def)
(define show-row-def
'(define (show-row col board-size)
(if (= board-size 0)
(newline)
(begin
(if (= col 1)
(display " Q")
(display " _"))
(show-row (- col 1) (- board-size 1))))))
(#%provide show-board-def)
(define show-board-def
'(define (show-board board)
(let ((board-size (length board)))
(define (show-rows rows)
(if (null? rows)
(newline)
(begin
(show-row (cadr (car rows)) board-size)
(show-rows (cdr rows)))))
(newline)
(show-rows board))))
(#%provide queens-amb-def)
(define queens-amb-def
'(define (queens-amb board-size)
(define empty-board '())
(define (adjoin-position pos board) (cons pos board))
(define (make-pos r c) (list r c))
(define (row pos) (car pos))
(define (col pos) (cadr pos))
(define (zig pos) (+ (row pos) (col pos)))
(define (zag pos) (- (row pos) (col pos)))
(define (some-col) (an-integer-between 1 board-size))
(define (pick-free proj pos board)
(require (not (memq (proj pos) (map proj board)))))
(define (queen-rows k)
(if (= k 0)
empty-board
(let
((board (queen-rows (- k 1)))
(new-pos (make-pos k (some-col))))
(pick-free row new-pos board)
(pick-free col new-pos board)
(pick-free zig new-pos board)
(pick-free zag new-pos board)
(adjoin-position new-pos board))))
(let ((result (reverse (queen-rows board-size))))
(show-board result)
result)))
(#%provide parse-def)
(define parse-def
'(define (parse input)
(define nouns '(nouns student professor cat class))
(define verbs '(verb studies lectures eats sleeps))
(define articles '(article the a))
(define prepositions '(prep for to in by with))
(define *unparsed* '())
(define (parse-sentence)
(list
'sentence
(parse-noun-phrase)
(parse-verb-phrase)))
(define (parse-noun-phrase)
(define (maybe-extend noun-phrase)
(amb
noun-phrase
(maybe-extend
(list
'noun-phrase
noun-phrase
(parse-prepositional-phrase)))))
(maybe-extend (parse-simple-noun-phrase)))
(define (parse-simple-noun-phrase)
(list
'simple-noun-phrase
(parse-word articles)
(parse-word nouns)))
(define (parse-prepositional-phrase)
(list
'prep-phrase
(parse-word prepositions)
(parse-noun-phrase)))
(define (parse-verb-phrase)
(define (maybe-extend verb-phrase)
(amb
verb-phrase
(maybe-extend
(list
'verb-phrase
verb-phrase
(parse-prepositional-phrase)))))
(maybe-extend (parse-word verbs)))
(define (parse-word word-list)
(require (not (null? *unparsed*)))
(require (memq (car *unparsed*) (cdr word-list)))
(let ((found-word (car *unparsed*)))
(set! *unparsed* (cdr *unparsed*))
(list (car word-list) found-word)))
(set! *unparsed* input)
(let ((sent (parse-sentence)))
(require (null? *unparsed*))
sent)))
#| 4.45 |#
#| '(the professor lectures to the student in the class with the cat) |#
#| '(to the (student in the (class with the cat))) |#
#| '(to the ((student in the class) with the cat)) |#
#| '((to the student) in the (class with the cat)) |#
#| '((to the (student in the class)) with the cat) |#
#| '(((to the student) in the class) with the cat) |#
#| (the professor lectures to (the student in (the class with the cat))) |#
#| there is a class with a cat |#
#| and in the class is a student |#
#| the professor lectures to said student |#
#| '(sentence |#
#| (simple-noun-phrase (article the) (noun professor)) |#
#| (verb-phrase |#
#| (verb lectures) |#
#| (prep-phrase |#
#| (prep to) |#
#| (noun-phrase |#
#| (simple-noun-phrase (article the) (noun student)) |#
#| (prep-phrase |#
#| (prep in) |#
#| (noun-phrase |#
#| (simple-noun-phrase (article the) (noun class)) |#
#| (prep-phrase |#
#| (prep with) |#
#| (simple-noun-phrase (article the) (noun cat))))))))) |#
#| (the professor lectures to ((the student in the class) with the cat)) |#
#| there is a student who |#
#| 1. is in a class |#
#| 2. has a cat |#
#| the professor lectures to that student |#
#| (sentence |#
#| (simple-noun-phrase (article the) (noun professor)) |#
#| (verb-phrase |#
#| (verb lectures) |#
#| (prep-phrase |#
#| (prep to) |#
#| (noun-phrase |#
#| (noun-phrase |#
#| (simple-noun-phrase (article the) (noun student)) |#
#| (prep-phrase |#
#| (prep in) |#
#| (simple-noun-phrase (article the) (noun class)))) |#
#| (prep-phrase |#
#| (prep with) |#
#| (simple-noun-phrase (article the) (noun cat))))))) |#
#| ((the professor lectures to (the student in the class)) with the cat) |#
#| there is a student in a class |#
#| the professor and the cat lecture to that student |#
#| (sentence |#
#| (simple-noun-phrase the professor) |#
#| (verb-phrase |#
#| (verb-phrase |#
#| (verb lectures) |#
#| (prep-phrase |#
#| (prep to) |#
#| (noun-phrase |#
#| (simple-noun-phrase (article the) (noun student)) |#
#| (prep-phrase |#
#| (prep in) |#
#| (simple-noun-phrase (article the) (noun class)))))) |#
#| (prep-phrase |#
#| (prep with) |#
#| (simple-noun (article the) (noun cat))))) |#
#| ((the professor lectures to the student) in the (class with the cat)) |#
#| there is a class with a cat |#
#| the professor lectures to the student |#
#| this occurs in said class |#
#| (sentence |#
#| (simple-noun-phrase the professor) |#
#| (verb-phrase |#
#| (verb-phrase |#
#| (verb lectures) |#
#| (prep-phrase |#
#| (prep to) |#
#| (simple-noun-phrase (article the) (noun student)))) |#
#| (prep-phrase |#
#| (prep in) |#
#| (noun-phrase |#
#| (simple-noun-phrase (article the) (noun class)) |#
#| (prep-phrase |#
#| (prep with) |#
#| (simple-noun-phrase (article the) (noun cat))))))) |#
#| (((the professor lectures to the student) in the class) with the cat) |#
#| the professor and the cat lecture to the student |#
#| this occurs in the class |#
#| (sentence |#
#| (simple-noun-phrase the professor) |#
#| (verb-phrase |#
#| (verb-phrase |#
#| (verb-phrase |#
#| (verb lectures) |#
#| (prep-phrase |#
#| (prep to) |#
#| (simple-noun-phrase (article the) (noun student)))) |#
#| (prep-phrase |#
#| (prep in) |#
#| (simple-noun-phrase (article the) (noun class)))) |#
#| (prep-phrase |#
#| (prep with) |#
#| (simple-noun-phrase (article the) (noun cat))))) |#
#| 4.49 |#
(#%provide parse-gen-def)
(define parse-gen-def
'(define (parse-gen)
(define nouns '(nouns student professor cat class))
(define verbs '(verb studies lectures eats sleeps))
(define articles '(article the a))
(define prepositions '(prep for to in by with))
(define (parse-sentence)
(list
'sentence
(parse-noun-phrase)
(parse-verb-phrase)))
(define (parse-noun-phrase)
(define (maybe-extend noun-phrase)
(ramb
noun-phrase
(maybe-extend
(list
'noun-phrase
noun-phrase
(parse-prepositional-phrase)))))
(maybe-extend (parse-simple-noun-phrase)))
(define (parse-simple-noun-phrase)
(list
'simple-noun-phrase
(parse-word-gen articles)
(parse-word-gen nouns)))
(define (parse-prepositional-phrase)
(list
'prep-phrase
(parse-word-gen prepositions)
(parse-noun-phrase)))
(define (parse-verb-phrase)
(define (maybe-extend verb-phrase)
(ramb
verb-phrase
(maybe-extend
(list
'verb-phrase
verb-phrase
(parse-prepositional-phrase)))))
(maybe-extend (parse-word-gen verbs)))
(define (one-of words)
(if (null? words)
(amb)
(ramb
(car words)
(one-of (cdr words)))))
(define (parse-word-gen word-list)
(let ((found-word (one-of (cdr word-list))))
(list (car word-list) found-word)))
(parse-sentence)))
;; Implementing the Amb Evaluator
(define (tagged-list? exp tag)
(if (pair? exp)
(eq? (car exp) tag)
false))
(define (self-evaluating? exp)
(cond
((number? exp) true)
((string? exp) true)
(else false)))
(define (variable? exp) (symbol? exp))
(define (quoted? exp)
(tagged-list? exp 'quote))
(define (text-of-quotation exp) (cadr exp))
(define (assignment? exp)
(tagged-list? exp 'set!))
(define (assignment-variable exp) (cadr exp))
(define (assignment-value exp) (caddr exp))
(define (definition? exp)
(tagged-list? exp 'define))
(define (definition-variable exp)
(if (symbol? (cadr exp))
(cadr exp)
(caadr exp)))
(#%provide definition-value)
(define (definition-value exp)
(if (symbol? (cadr exp))
(caddr exp)
(make-lambda
(cdadr exp)
(cddr exp))))
(define (lambda? exp) (tagged-list? exp 'lambda))
(define (lambda-parameters exp) (cadr exp))
(#%provide lambda-body)
(define (lambda-body exp) (cddr exp))
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
(define (if? exp) (tagged-list? exp 'if))
(define (if-predicate exp) (cadr exp))
(define (if-consequent exp) (caddr exp))
(define (if-alternative exp)
(if (not (null? (cdddr exp)))
(cadddr exp)
'false))
(define (make-if predicate consequent alternative)
(list 'if predicate consequent alternative))
(define (begin? exp) (tagged-list? exp 'begin))
(define (begin-actions exp) (cdr exp))
(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
(define (sequence->exp seq)
(cond
((null? seq) seq)
((last-exp? seq) (first-exp seq))
(else (make-begin seq))))
(define (make-begin seq) (cons 'begin seq))
(define (application? exp) (pair? exp))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operands ops) (cdr ops))
(define (cond? exp) (tagged-list? exp 'cond))
(define (cond-clauses exp) (cdr exp))
(#%provide cond-predicate)
(define (cond-predicate clause)
(car clause))
(define (cond-else-clause? clause)
(eq? (cond-predicate clause) 'else))
(define (cond-actions clause) (cdr clause))
(define (cond->if exp)
(expand-clauses (cond-clauses exp)))
(define (expand-clauses clauses)
(if (null? clauses)
'false
(let
((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error
"ELSE clause isn't last -- COND->IF"
clauses))
(make-if
(cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))
(#%provide let?)
(define (let? exp) (tagged-list? exp 'let))
(#%provide binding-var)
(define (binding-var binding)
(car binding))
(#%provide binding-exp)
(define (binding-exp binding)
(cadr binding))
(#%provide let-bindings)
(define (let-bindings exp) (cadr exp))
(#%provide let-body)
(define (let-body exp) (cddr exp))
(#%provide let->combination)
(define (let->combination exp)
(if (null? (let-bindings exp))
(if (null? (cdr (let-body exp)))
(car (let-body exp))
(cons 'begin (let-body exp)))
(cons
(cons 'lambda
(cons
(map binding-var (let-bindings exp))
(let-body exp)))
(map binding-exp (let-bindings exp)))))
(define (true? x)
(not (eq? x false)))
(define (false? x)
(eq? x false))
(define (make-procedure parameters body env)
(list 'procedure parameters body env))
(define (compound-procedure? p)
(tagged-list? p 'procedure))
(#%provide procedure-parameters)
(define (procedure-parameters p) (cadr p))
(#%provide procedure-body)
(define (procedure-body p) (caddr p))
(#%provide procedure-environment)
(define (procedure-environment p) (cadddr p))
(#%provide enclosing-environment)
(define (enclosing-environment env) (cdr env))
(#%provide first-frame)
(define (first-frame env) (car env))
(#%provide the-empty-environment)
(define the-empty-environment '())
(#%provide make-frame)
(define (make-frame variables values)
(cons variables values))
(#%provide frame-variables)
(define (frame-variables frame) (car frame))
(#%provide frame-values)
(define (frame-values frame) (cdr frame))
(#%provide add-binding-to-frame!)
(define (add-binding-to-frame! var val frame)
(set-car! frame (cons var (car frame)))
(set-cdr! frame (cons val (cdr frame))))
(#%provide extend-environment)
(define (extend-environment vars vals base-env)
(if (= (length vars) (length vals))
(cons (make-frame vars vals) base-env)
(if (< (length vars) (length vals))
(error "Too many arguments supplied" vars vals)
(error "Too few arguments supplied" vars vals))))
(#%provide lookup-variable-value)
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan vars vals)
(cond
((null? vars)
(env-loop (enclosing-environment env)))
((eq? var (car vars))
(car vals))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan
(frame-variables frame)
(frame-values frame)))))
(env-loop env))
(#%provide set-variable-value!)
(define (set-variable-value! var val env)
(define (env-loop env)
(define (scan vars vals)
(cond
((null? vars)
(env-loop (enclosing-environment env)))
((eq? var (car vars))
(set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan
(frame-variables frame)
(frame-values frame)))))
(env-loop env))
(#%provide define-variable!)
(define (define-variable! var val env)
(let ((frame (first-frame env)))
(define (scan vars vals)
(cond
((null? vars)
(add-binding-to-frame! var val frame))
((eq? var (car vars))
(set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(scan
(frame-variables frame)
(frame-values frame))))
(#%provide ambeval)
(define (ambeval exp env succeed fail)
((analyze exp) env succeed fail))
(#%provide analyze)
(define (analyze exp)
(cond
((self-evaluating? exp)
(analyze-self-evaluating exp))
((quoted? exp) (analyze-quoted exp))
((variable? exp) (analyze-variable exp))
((assignment? exp) (analyze-assignment exp))
((permanent-assignment? exp) (analyze-permanent-assignment exp))
((definition? exp) (analyze-definition exp))
((if? exp) (analyze-if exp))
((if-fail? exp) (analyze-if-fail exp))
((lambda? exp) (analyze-lambda exp))
((begin? exp) (analyze-sequence (begin-actions exp)))
((cond? exp) (analyze (cond->if exp)))
((let? exp) (analyze (let->combination exp)))
((amb? exp) (analyze-amb exp))
((ramb? exp) (analyze-ramb exp))
((require? exp) (analyze-require exp))
((application? exp) (analyze-application exp))
(else
(error "Unknown expression type -- ANALYZE" exp))))
(define (amb? exp) (tagged-list? exp 'amb))
(define (amb-choices exp) (cdr exp))
(define (analyze-self-evaluating exp)
(lambda (env succeed fail) (succeed exp fail)))
(define (analyze-quoted exp)
(let ((qval (text-of-quotation exp)))
(lambda (env succeed fail) (succeed qval fail))))
(define (analyze-variable exp)
(lambda (env succeed fail)
(succeed (lookup-variable-value exp env) fail)))
(define (analyze-lambda exp)
(let
((vars (lambda-parameters exp))
(bproc (analyze-sequence (lambda-body exp))))
(lambda (env succeed fail)
(succeed (make-procedure vars bproc env) fail))))
(define (analyze-assignment exp)
(let
((var (assignment-variable exp))
(vproc (analyze (assignment-value exp))))
(lambda (env succeed fail)
(vproc
env
(lambda (val fail2)
(let ((old-value (lookup-variable-value var env)))
(set-variable-value! var val env)
(succeed
'ok
(lambda ()
(set-variable-value! var old-value env)
(fail2)))))
fail))))
(define (analyze-definition exp)
(let
((var (definition-variable exp))
(vproc (analyze (definition-value exp))))
(lambda (env succeed fail)
(vproc
env
(lambda (val fail2)
(define-variable! var val env)
(succeed 'ok fail2))
fail))))
(define (analyze-if exp)
(let
((pproc (analyze (if-predicate exp)))
(cproc (analyze (if-consequent exp)))
(aproc (analyze (if-alternative exp))))
(lambda (env succeed fail)
(pproc
env
(lambda (pred-value fail2)
(if (true? pred-value)
(cproc env succeed fail2)
(aproc env succeed fail2)))
fail))))
(define (analyze-sequence exps)
(define (sequentially proc1 proc2)
(lambda (env succeed fail)
(proc1
env
(lambda (p1-value fail2)
(proc2 env succeed fail2))
fail)))
(define (loop first-proc rest-procs)
(if (null? rest-procs)
first-proc
(loop
(sequentially first-proc (car rest-procs))
(cdr rest-procs))))
(let
((procs (map analyze exps)))
(if (null? procs)
(error "Empty sequence -- ANALYZE"))
(loop (car procs) (cdr procs))))
(define (analyze-application exp)
(let
((fproc (analyze (operator exp)))
(aprocs (map analyze (operands exp))))
(lambda (env succeed fail)
(fproc
env
(lambda (proc fail2)
(get-args
aprocs
env
(lambda (args fail3)
(execute-application proc args succeed fail3))
fail2))
fail))))
(define (get-args aprocs env succeed fail)
(if (null? aprocs)
(succeed '() fail)
((car aprocs)
env
(lambda (arg fail2)
(get-args
(cdr aprocs)
env
(lambda (args fail3)
(succeed (cons arg args) fail3))
fail2))
fail)))
(define (execute-application proc args succeed fail)
(cond
((primitive-procedure? proc)
(succeed (apply-primitive-procedure proc args) fail))
((compound-procedure? proc)
((procedure-body proc)
(extend-environment
(procedure-parameters proc)
args
(procedure-environment proc))
succeed
fail))
(else
(error
"Unknown procedure type -- EXECUTE-APPLICATION"
proc))))
(define (analyze-amb exp)
(let ((cprocs (map analyze (amb-choices exp))))
(lambda (env succeed fail)
(define (try-next choices)
(if (null? choices)
(fail)
((car choices)
env
succeed
(lambda () (try-next (cdr choices))))))
(try-next cprocs))))
(define input-prompt ";;; Amb-Eval input:")
(define output-prompt ";;; Amb-Eval value:")
(define (setup-environment)
(let
((initial-env
(extend-environment
(primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)))
(define-variable! 'true true initial-env)
(define-variable! 'false false initial-env)
initial-env))
(define (primitive-procedure? proc)
(tagged-list? proc 'primitive))
(define (primitive-implementation proc) (cadr proc))
(define primitive-procedures
(list
(list 'car car)
(list 'cdr cdr)
(list 'cadr cadr)
(list 'caddr caddr)
(list 'cons cons)
(list 'null? null?)
(list 'list list)
(list 'member member)
(list 'memq memq)
(list 'reverse reverse)
(list 'length length)
(list 'not not)
(list 'eq? eq?)
(list 'newline newline)
(list 'display display)
(list 'integer? integer?)
(list 'remainder remainder)
(list 'abs abs)
(list 'sqrt sqrt)
(list 'even? even?)
(list '+ +)
(list '- -)
(list '* *)
(list '/ /)
(list '> >)
(list '< <)
(list '= =)
(list '<= <=)
(list '>= >=)))
(define (primitive-procedure-names)
(map car primitive-procedures))
(define (primitive-procedure-objects)
(map
(lambda (proc) (list 'primitive (cadr proc)))
primitive-procedures))
(define (apply-primitive-procedure proc args)
(apply
(primitive-implementation proc) args))
(define (prompt-for-input string)
(newline)
(newline)
(display string)
(newline))
(define (announce-output string)
(newline)
(display string)
(newline))
(define (user-print object)
(if (compound-procedure? object)
(display
(list
'compound-procedure
(procedure-parameters object)
(procedure-body object)
'<procedure-env>))
(display object)))
(#%provide the-global-environment)
(define the-global-environment (setup-environment))
(#%provide driver-loop)
(define (driver-loop)
(define (internal-loop try-again)
(prompt-for-input input-prompt)
(let ((input (read)))
(if (eq? input 'try-again)
(try-again)
(begin
(newline)
(display ";;; Starting a new problem ")
(ambeval
input
the-global-environment
(lambda (val next-alternative)
(announce-output output-prompt)
(user-print val)
(internal-loop next-alternative))
(lambda ()
(announce-output
";;; There are no more values of")
(user-print input)
(driver-loop)))))))
(internal-loop
(lambda ()
(newline)
(display ";;; There is no current problem")
(driver-loop))))
(#%provide driver-loop-init)
(define (driver-loop-init init-exps)
(cond
((null? init-exps) (driver-loop))
(else
(ambeval
(car init-exps)
the-global-environment
(lambda (val next-alternative)
(driver-loop-init (cdr init-exps)))
(lambda ()
(announce-output ";;; There are no values of")
(display (car init-exps))
(driver-loop-init (cdr init-exps)))))))
(#%provide amb-defs)
(define amb-defs
(list
prime?-def
require-def
prime-sum-pair-def
an-element-of-def
an-integer-starting-from-def
an-integer-between-def
a-pythagorean-triple-between-def
a-pythagorean-triple-bad-def
a-pythagorean-triple-def
a-pythagorean-triple-between-fast-def
distinct?-def
multiple-dwelling-def
multiple-dwelling-mod-def
multiple-dwelling-reorder-def
multiple-dwelling-quick-def
liars-def
map-def
yachts-def
show-row-def
show-board-def
queens-amb-def
parse-def
parse-gen-def
))
#| 4.50 |#
(define (ramb? exp) (tagged-list? exp 'ramb))
(define (ramb-choices exp) (cdr exp))
(#%provide remove)
(define (remove n xs)
(cond
((null? xs) '())
((= n 0) (cdr xs))
(else (cons (car xs) (remove (- n 1) (cdr xs))))))
(define (analyze-ramb exp)
(let ((cprocs (map analyze (amb-choices exp))))
(lambda (env succeed fail)
(define (try-next choices)
(if (null? choices)
(fail)
(let ((i (random (length choices))))
((list-ref choices i)
env
succeed
(lambda () (try-next (remove i choices)))))))
(try-next cprocs))))
#| 4.51 |#
(define (permanent-assignment? exp)
(tagged-list? exp 'permanent-set!))
(define (analyze-permanent-assignment exp)
(let
((var (assignment-variable exp))
(vproc (analyze (assignment-value exp))))
(lambda (env succeed fail)
(vproc
env
(lambda (val fail2)
(set-variable-value! var val env)
(succeed 'ok fail2))
fail))))
(define permanent-set-example
'(begin
(define count 0)
(let
((x (an-element-of '(a b c)))
(y (an-element-of '(a b c))))
(permanent-set! count (+ count 1))
(require (not (eq? x y)))
(list x y count))))
#| 4.52 |#
(define (if-fail? exp) (tagged-list? exp 'if-fail))
(define (if-fail-value exp) (cadr exp))
(define (if-fail-alternative exp) (caddr exp))
(define (analyze-if-fail exp)
(let
((vproc (analyze (if-fail-value exp)))
(aproc (analyze (if-fail-alternative exp))))
(lambda (env succeed fail)
(vproc
env
succeed
(lambda ()
(aproc env succeed fail))))))
(define if-fail-example-1
'(if-fail
(let ((x (an-element-of '(1 3 5))))
(require (even? x))
x)
'all-odd))
(define if-fail-example-2
'(if-fail
(let ((x (an-element-of '(1 3 5 8))))
(require (even? x))
x)
'all-odd))
#| 4.53 |#
(define all-solutions-example
'(let ((pairs '()))
(if-fail
(let ((p (prime-sum-pair '(1 3 5 8) '(20 35 110))))
(permanent-set! pairs (cons p pairs))
(amb))
pairs)))
#| 4.54 |#
(define (require? exp) (tagged-list? exp 'require-))
(define (require-predicate exp) (cadr exp))
(define (analyze-require exp)
(let ((pproc (analyze (require-predicate exp))))
(lambda (env succeed fail)
(pproc
env
(lambda (pred-value fail2)
(if (not pred-value)
(fail)
(succeed 'ok fail2)))
fail))))
|