-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 2.90 support for both sparse and dense.rkt
1879 lines (1684 loc) · 69.3 KB
/
Exercise 2.90 support for both sparse and dense.rkt
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
#lang racket
; Exercise 2.90. Suppose we want to have a polynomial system that is efficient for both sparse
; and dense polynomials. One way to do this is to allow both kinds of term-list representations in
; our system. The situation is analogous to the complex-number example of section 2.4, where we
; allowed both rectangular and polar representations. To do this we must distinguish different
; types of term lists and make the operations on term lists generic. Redesign the polynomial system
; to implement this generalization. This is a major effort, not a local change.
; S O L U T I O N
; GENERIC PROCEDURES
; Generic Polynomial procedures
; Note: I have designed this with the assumption that the procedures adjoin-term, first-term and rest-terms
; though generic, will still be used only internally by the polynomial procedures.
; These three procedures are generic but not exposed to the outside world.
(define (adjoin-term term term-list) (apply-generic 'adjoin-term term term-list))
(define (first-term term-list) (apply-generic 'first-term term-list))
(define (rest-terms term-list) (apply-generic 'rest-terms term-list))
(define (order term) (apply-generic 'order term))
(define (coeff term) (apply-generic 'coeff term))
(define (negate-term term) (apply-generic 'negate-term term))
; Generic Logical procedures
(define (=zero? x) (apply-generic '=zero? x))
(define (equ? x y) (apply-generic 'equal? x y))
(define (square x) (apply-generic 'square x))
(define (square-root x) (apply-generic 'square-root x))
(define (NEGATE x) (apply-generic 'negate x))
; Generic Arithmetic procedures with two arguments
(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(define (mul x y) (apply-generic 'mul x y))
(define (div x y) (apply-generic 'div x y))
(define (exp x y) (apply-generic 'exp x y))
; Generic Trigonometric procedures
(define (sine x) (apply-generic 'sine x))
(define (cosine x) (apply-generic 'cosine x))
(define (tan-inverse x y) (apply-generic 'tan-inverse x y))
; Generic Contrived procedures
(define (add-four-quantities w x y z) (apply-generic 'add-four-quantities w x y z))
(define (mul-and-scale x y factor) (apply-generic 'mul-and-scale x y factor))
(define (mul-five-quantities v w x y z) (apply-generic 'mul-five-quantities v w x y z))
; Generic operation that 'projects' x one level lower in the tower
(define (project x) (apply-generic 'project x))
; Generic operation that raises x one level in the tower
(define (raise x) (apply-generic 'raise x))
; Generic Constructions of specific types of entities (ordinary, rational, complex etc.)
(define (make-scheme-number n) ((get 'make 'scheme-number) n))
(define (make-natural n) ((get 'make 'natural) n))
(define (make-integer n) ((get 'make 'integer) n))
(define (make-rational n d) ((get 'make 'rational) n d))
(define (make-real r) ((get 'make 'real) r))
(define (make-complex-from-real-imag x y) ((get 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a) ((get 'make-from-mag-ang 'complex) r a))
(define (make-polynomial var terms) ((get 'make 'polynomial) var terms))
(define (make-polynomial-dense-terms terms) ((get 'make 'polynomial-dense-terms) terms))
(define (make-polynomial-sparse-terms terms) ((get 'make 'polynomial-sparse-terms) terms))
(define (make-term order coeff) ((get 'make 'polynomial-term) order coeff))
(define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a))
; POLYNOMIAL PROCEDURES
(define (make-poly variable term-list) (cons variable term-list))
(define (make-dense-terms terms) terms)
(define (make-sparse-terms terms) terms)
(define (variable p) (car p))
(define (term-list p) (cdr p))
(define (the-empty-poly-termlist) '())
; (define (the-empty-poly-termlist) (list 'polynomial-sparse-terms (list 0 0)))
; Term-list manipulation for dense polynomials
(define (adjoin-term-dense term term-list)
; Structure of term-list
; [OrderOfPolynomial, (list of coefficients)]
; Maintaining the order of the polynomial as the first element allows us to avoid repeated
; expensive 'length' calls on the coefficient list
; Reminder: We assume that term lists are represented as lists of terms,
; arranged from highest-order to lowest-order term.
(if (=zero? (coeff-poly-term term))
term-list
(cond
((= (order-poly-term term) (+ (polynomial-order-dense term-list) 1))
; no need to insert a zero in the coefficient list
(cons (+ 1 (polynomial-order-dense term-list)) (cons (coeff-poly-term term) (coefficients-dense term-list)))
)
((and (empty-termlist? term-list) (= 0 (order-poly-term term)))
(list 0 (coeff-poly-term term))
)
((and (empty-termlist? term-list) (> (order-poly-term term) 0))
(adjoin-term-dense term (list 0 0))
)
(else
; we need to supply zero(s) if there are gaps
(adjoin-term-dense term (cons (+ 1 (polynomial-order-dense term-list)) (cons 0 (coefficients-dense term-list))))
)
)
)
)
(define (first-term-dense term-list)
; Since we are using the term-list representation that is appropriate for
; dense polynomials (see SICP text), we need to do some extra processing
; to retrieve both the order and coefficient
(if (pair? term-list)
(if (pair? (coefficients-dense term-list))
(list (polynomial-order-dense term-list) (car (coefficients-dense term-list)))
null
)
null
)
)
(define (rest-terms-dense term-list)
; Since we are using the term-list representation that is appropriate for
; dense polynomials (see SICP text), we need to do some extra processing
; to retrieve both the order and coefficient
(if (> (polynomial-order-dense term-list) 0)
(cons (- (polynomial-order-dense term-list) 1) (cdr (coefficients-dense term-list)))
(the-empty-poly-termlist)
)
)
(define (polynomial-order-dense term-list)
(if (pair? term-list)
(car term-list)
0
)
)
(define (coefficients-dense term-list)
(if (pair? term-list)
(cdr term-list)
(list 0)
)
)
; Term-list manipulation for sparse polynomials
(define (adjoin-term-sparse term term-list)
(if (=zero? (coeff-poly-term term))
term-list
(cons term term-list)
)
)
(define (first-term-sparse term-list)
(cond
((null? term-list) null)
((not (pair? term-list)) (error "Procedure first-term-sparse: term-list is not a pair!"))
(else
(car term-list)
)
)
)
(define (rest-terms-sparse term-list)
(cond
((null? term-list) (the-empty-poly-termlist))
((not (pair? term-list)) (error "Procedure rest-terms-sparse: term-list is not a pair!"))
(else
(cdr term-list)
)
)
)
; Term manipulation (used in both dense and sparse polynomial representations)
(define (make-poly-term order coeff) (list order coeff))
(define (order-poly-term term)
(if (pair? term)
(car term)
0
)
)
(define (coeff-poly-term term)
(if (pair? term)
(cadr term)
0
)
)
; Polynomial Operations
(define (add-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly
(variable p1)
(add-terms (term-list p1) (term-list p2))
)
(error "Polys not in same var -- ADD-POLY"
(list p1 p2)
)
)
)
(define (sub-poly p1 p2)
(add-poly p1 (negate-poly p2))
)
(define (mul-poly p1 p2)
(if (same-variable? (variable p1) (variable p2))
(make-poly
(variable p2)
(mul-terms (term-list p1) (term-list p2))
)
(error "Polys not in same var -- MUL-POLY"
(list p1 p2)
)
)
)
(define (add-terms L1 L2)
(cond
((empty-termlist? L1) L2)
((empty-termlist? L2) L1)
(else
(let ((t1 (first-term L1)) (t2 (first-term L2)))
(cond
((> (order t1) (order t2))
(adjoin-term t1 (add-terms (rest-terms L1) L2))
)
((< (order t1) (order t2))
(adjoin-term t2 (add-terms L1 (rest-terms L2)))
)
(else
(adjoin-term
(make-term (order t1) (add (coeff t1) (coeff t2)))
(add-terms (rest-terms L1) (rest-terms L2))
)
)
)
)
)
)
)
(define (mul-terms L1 L2)
(if (or (empty-termlist? L1) (empty-termlist? L2))
(the-empty-poly-termlist)
(add-terms
(mul-term-by-all-terms (first-term L1) L2)
(mul-terms (rest-terms L1) L2)
)
)
)
(define (mul-term-by-all-terms t1 L)
(if (or (empty-termlist? L) (empty-term? t1))
; (the-empty-poly-termlist)
; Returning an empty list with an explicit tag so that the generic procedure
; 'adjoin-term' below does not fail. Generic procedures expect a data tag for each of their arguments
(list 'polynomial-sparse-terms (list 0 0))
(let ((t2 (first-term L)))
(adjoin-term
(make-term (+ (order t1) (order t2)) (mul (coeff t1) (coeff t2)))
(mul-term-by-all-terms t1 (rest-terms L))
)
)
)
)
(define (=zero-polynomial? p)
; A polynomial is zero if its term-list is empty
(empty-termlist? (term-list p))
)
(define (negate-poly p)
(make-poly
(variable p)
(negate-term-list (term-list p))
)
)
(define (negate-term-list t)
(if (empty-termlist? t)
t
(adjoin-term (negate-term (first-term t)) (negate-term-list (rest-terms t)))
)
)
(define (negate-poly-term t)
(list (order-poly-term t) (* -1 (coeff-poly-term t)))
)
(define (empty-termlist? t)
; consider both with tag and without tag
; This procedure will return true for the following data:
; '()
; ('polynomial-sparse-terms)
; ('polynomial-dense-terms)
; ('polynomial-sparse-terms (0 0))
; ('polynomial-dense-terms 0 0)
(or
(null? t)
(and (symbol? (car t)) (null? (cdr t)))
(and (eq? (car t) 'polynomial-sparse-terms) (eq? (caadr t) 0) (eq? (cadadr t) 0))
(and (eq? (car t) 'polynomial-dense-terms) (eq? (cadr t) 0) (eq? (caddr t) 0))
)
)
(define (empty-term? t)
(cond
((null? t) true)
((not (pair? t)) (error "Procedure empty-term: t not a pair!"))
(else
(eq? (cadr t) 0)
)
)
)
(define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2))
)
(define (variable? x) (symbol? x))
; COMPLEX NUMBER PROCEDURES
(define (magnitude z) (apply-generic 'magnitude z))
(define (angle z) (apply-generic 'angle z))
(define (REAL-PART z) (apply-generic 'REAL-PART z))
(define (IMAG-PART z) (apply-generic 'IMAG-PART z))
(define (add-complex z1 z2)
(make-from-real-imag
(add (REAL-PART z1) (REAL-PART z2))
(add (IMAG-PART z1) (IMAG-PART z2))
)
)
(define (sub-complex z1 z2)
(make-from-real-imag
(sub (REAL-PART z1) (REAL-PART z2))
(sub (IMAG-PART z1) (IMAG-PART z2))
)
)
(define (mul-complex z1 z2)
(make-from-mag-ang
(mul (magnitude z1) (magnitude z2))
(add (angle z1) (angle z2))
)
)
(define (div-complex z1 z2)
(make-from-mag-ang
(div (magnitude z1) (magnitude z2))
(sub (angle z1) (angle z2))
)
)
(define (mul-and-scale-complex z1 z2 factor)
(display "Entered proc mul-and-scale-complex")
(newline)
(let ((prod (mul-complex z1 z2)))
(make-complex-from-real-imag (mul (REAL-PART prod) factor) (mul (IMAG-PART prod) factor))
)
)
(define (add-four-complex-numbers z1 z2 z3 z4)
(display "Entered proc add-four-complex-numbers")
(newline)
(make-from-real-imag
(add (REAL-PART z1) (REAL-PART z2) (REAL-PART z3) (REAL-PART z4))
(add (IMAG-PART z1) (IMAG-PART z2) (IMAG-PART z3) (IMAG-PART z4))
)
)
(define (equal-complex? c1 c2)
(and (equ? (REAL-PART c1) (REAL-PART c2)) (equ? (IMAG-PART c1) (IMAG-PART c2)))
)
(define (=zero-complex? c) (equ? 0 (magnitude c)))
(define (project-complex c) (make-real (REAL-PART c)))
(define (square-complex c)
(mul-complex c c)
)
; Rectangular (Complex) Number procedures
(define (make-from-real-imag-rectangular x y) (cons x y))
(define (make-from-mag-ang-rectangular r a) (cons (mul r (cosine a)) (mul r (sine a))))
(define (magnitude-rectangular z)
(square-root (add (square (real-part-rectangular z)) (square (imag-part-rectangular z))))
)
(define (angle-rectangular z) (tan-inverse (imag-part-rectangular z) (real-part-rectangular z)))
(define (real-part-rectangular z) (car z))
(define (imag-part-rectangular z) (cdr z))
; Polar (Complex) Number procedures
(define (make-from-real-imag-polar x y) (cons (square-root (add (square x) (square y))) (tan-inverse y x)))
(define (make-from-mag-ang-polar r a) (cons r a))
(define (magnitude-polar z) (car z))
(define (angle-polar z) (cdr z))
(define (real-part-polar z) (mul (magnitude-polar z) (cosine (angle-polar z))))
(define (imag-part-polar z) (mul (magnitude-polar z) (sine (angle-polar z))))
; REAL NUMBER PROCEDURES
(define (make-real-specific r)
(if (real? r)
r
(error "Cannot make real with: " r)
)
)
(define (=zero-real? x)
(= 0 x)
)
(define (raise-real r)
(make-complex-from-real-imag r 0)
)
(define (project-real r)
(make-rational r 1)
)
(define (square-root-real r)
(if (>= r 0)
(sqrt r)
(make-complex-from-real-imag 0 (sqrt (abs r)))
)
)
(define (square-real r)
(* r r)
)
(define (cosine-real r)
(cos r)
)
(define (tan-inverse-real r1 r2)
(atan r1 r2)
)
(define (sine-real r)
(sin r)
)
; RATIONAL NUMBER PROCEDURES
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (make-rational-specific n d)
(define (construct-rational n d)
(let ((g (gcd n d)))
(cond
((= d 0) (error "Denominator in a rational number cannot be zero"))
((= n 0) (cons n d))
((and (< n 0) (< d 0)) (cons (/ (abs n) g) (/ (abs d) g)))
((and (< n 0) (> d 0)) (cons (/ n g) (/ d g)))
((and (> n 0) (< d 0)) (cons (/ (* -1 n) g) (/ (abs d) g)))
((and (> n 0) (> d 0)) (cons (/ n g) (/ d g)))
)
)
)
(cond
((and (integer? n) (integer? d))
(construct-rational (exact-round n) (exact-round d))
)
; if both supplied arguments are not integers, try combining them and try to make a rational
; Example: 4 / 0.5 gives us 8 which is rational
((integer? (/ n d))
(construct-rational (exact-round (/ n d)) 1)
)
(else
(error "Rational number cannot be made with: " n d)
)
)
)
(define (add-rational x y) (make-rational-specific (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))
(define (sub-rational x y) (make-rational-specific (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))
(define (mul-rational x y) (make-rational-specific (* (numer x) (numer y)) (* (denom x) (denom y))))
(define (div-rational x y) (make-rational-specific (* (numer x) (denom y)) (* (denom x) (numer y))))
(define (equal-rational? x y)
; (display "Entered equal-rational?")
; (newline)
(and (= (numer x) (numer y)) (= (denom x) (denom y)))
)
(define (=zero-rational? x)
(= 0 (numer x))
)
(define (square-root-rational rat)
(if (>= (numer rat) 0)
(sqrt (/ (numer rat) (denom rat)))
(make-complex-from-real-imag 0 (sqrt (abs (/ (numer rat) (denom rat)))))
)
)
(define (square-rational rat)
(cons (* (numer rat) (numer rat)) (* (denom rat) (denom rat)))
)
(define (cosine-rational rat)
(cos (/ (numer rat) (denom rat)))
)
(define (tan-inverse-rational rat1 rat2)
(atan (/ (numer rat1) (denom rat1)) (/ (numer rat2) (denom rat2)))
)
(define (sine-rational rat)
(sin (/ (numer rat) (denom rat)))
)
(define (raise-rational r)
(make-real (* 1.0 (/ (numer r) (denom r))))
)
(define (project-rational r)
; tries to push this object down one step in the tower
(make-integer (numer r))
)
(define (mul-five-rationals v w x y z)
(mul-rational (mul-rational (mul-rational (mul-rational v w) x) y) z)
)
; INTEGER PROCEDURES
(define (make-integer-specific n)
(if (integer? n)
n
(error "Cannot make integer with: " n)
)
)
(define (raise-int n)
(make-rational n 1)
)
(define (project-int n)
; tries to push this object down one step in the tower
(make-natural n)
)
(define (square-root-integer i)
(if (>= i 0)
(sqrt i)
(make-complex-from-real-imag 0 (sqrt (abs i)))
)
)
(define (square-integer n)
(* n n)
)
(define (cosine-integer i)
(cos i)
)
(define (tan-inverse-integer i1 i2)
(atan i1 i2)
)
(define (sine-integer i)
(sin i)
)
; NATURAL NUMBER PROCEDURES
(define (make-natural-specific n)
(if (and (>= n 0) (or (natural? n) (= n (exact-round n))))
n
(error "Cannot make natural with: " n)
)
)
(define (raise-natural n)
(make-integer n)
)
(define (square-root-natural n)
(sqrt n)
)
(define (square-natural n)
(* n n)
)
(define (cosine-natural n)
(cos n)
)
(define (tan-inverse-natural n1 n2)
(atan n1 n2)
)
(define (sine-natural n)
(sin n)
)
; GENERIC PROCEDURE FRAMEWORK
(define (apply-generic op . args)
(define (apply-generic-internal op position args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(if proc
(apply proc (map contents args))
; proc not found so we need to try coercion (provided the args are not all
; of the same type)
(if (dissimilar? type-tags)
(if (<= position (length args))
; Coerce all arguments to the type of the argument that is in 'position'
; position in the list. If all the coercions obtained are valid,
; then apply the operation on the coerced arguments.
(let ((target-type (find-element type-tags position)))
; type-tags will be something like:
; ('complex 'rational 'real)
; from this we want to generate a list of procedures like:
; (complex->complex rational->complex 'real->complex)
; If all the above procedures are valid, then we will apply them to
; the respective arguments to do the coercion
(let ((coercion-procs (build-coercion-proc-list type-tags target-type)))
(if (allValid? coercion-procs)
; coerce all the arguments
(let ((coerced-args (coerce args coercion-procs)))
(let ((coerced-type-tags (map type-tag coerced-args)))
(let ((new-proc (get op coerced-type-tags)))
(if new-proc
; Found a valid procedure for the coerced args
; So we are done
(apply new-proc (map contents coerced-args))
; Could not find a valid procedure for the coerced args so try the next coercion
(apply-generic-internal op (+ position 1) args)
)
)
)
)
; At least one coercion is not supported so try coercing
; with the type of the next argument
(apply-generic-internal op (+ position 1) args)
)
)
)
; tried all coercions so now try "raising", provided the op itself is not "raise"
; Also, it does not make sense to raise up when we are trying to project down
(if (and (not (equal? op 'raise)) (not (equal? op 'project)))
(apply-raise op args)
(error "Tried all coercions. No procedure was found for these types" (list op type-tags))
)
)
; args are all of the same type so try raising, provided the op itself is not "raise"
; Also, it does not make sense to raise up when we are trying to project down
(if (and (not (equal? op 'raise)) (not (equal? op 'project)))
(apply-raise op args)
(error "(All arguments are of the same type and) no procedure was found for these types" (list op type-tags))
)
)
)
)
)
)
; (display "Starting with args: ")
; (display args)
; (newline)
(if (and (not (equal? op 'raise)) (not (equal? op 'project)))
(drop (apply-generic-internal op 1 args))
(apply-generic-internal op 1 args)
)
)
(define (drop x)
(with-handlers ([exn:fail? (lambda (exn)
; (display "Could not drop beyond: ")
; (display x)
; (newline)
x)])
(if (not (pair? x))
x
(let ((projected-x (project x)))
(cond
((equ? x (raise projected-x)) (drop projected-x))
(else
; can't drop any further
x
)
)
)
)
)
)
(define (apply-raise op args)
(let ((type-tags (map type-tag args)))
(let ((new-args (raise-one-step args)))
; (display "New args: ")
; (display new-args)
; (newline)
(let ((new-type-tags (map type-tag new-args)))
(if (not (equal? new-type-tags type-tags))
; raise worked
(let ((proc (get op new-type-tags)))
(if proc
; valid procedure found, so apply it
(apply proc (map contents new-args))
; valid procedure not found, so raise again
(apply-raise op new-args)
)
)
; we could not raise any more, so give up
(error "Tried all raise options and failed to find a valid procedure for this operation" (list op args))
)
)
)
)
)
(define (raise-one-step args)
; Logic of this procedure:
; If the arguments are dissimilar, find the 'lowest' argument in the list and raise it one step
; If the arguments are all similar (i.e. of the same type), then raise all of them one step
; return the new arguments
(let ((type-tags (map type-tag args)))
(if (dissimilar? type-tags)
(raise-lowest args)
(raise-all args)
)
)
)
(define (raise-lowest args)
(define (raise-lowest-internal arg-list position-of-lowest-type)
(if (= position-of-lowest-type 1)
(cons (try-raise (car arg-list)) (cdr arg-list))
(cons
(car arg-list)
(raise-lowest-internal (cdr arg-list) (- position-of-lowest-type 1))
)
)
)
(let ((position-of-lowest-type (get-position-of-lowest-type args)))
; (display "Raising the argument in position: ")
; (display position-of-lowest-type)
; (newline)
(raise-lowest-internal args position-of-lowest-type)
)
)
(define (get-position-of-lowest-type args)
(define (get-position-of-lowest-type-internal
arg-list
current-candidate
position-of-current-candidate
current-position)
(cond
((null? arg-list) position-of-current-candidate)
(else
(if (lower-in-tower? (car arg-list) current-candidate)
; we found a "lower" candidate
(get-position-of-lowest-type-internal (cdr arg-list) (car arg-list) (+ current-position 1) (+ current-position 1))
; we did not find a "lower" candidate, continue looking with the same current candidate
(get-position-of-lowest-type-internal (cdr arg-list) current-candidate position-of-current-candidate (+ current-position 1))
)
)
)
)
(cond
((null? args) 0)
((null? (cdr args)) 1)
(else
(get-position-of-lowest-type-internal (cdr args) (car args) 1 1)
)
)
)
(define (lower-in-tower? a b)
; tests whether a is lower in the tower than b
; returns true if a is lower, false if not
; Logic: keep raising a till you find b or cannot raise any further
(let ((new-a (try-raise a)))
(if (same-type? new-a a)
; raise failed
false
; raise worked and we got a new type
(if (same-type? new-a b)
; we successfully raised a to b
true
; a has not become b yet so keep looking
(lower-in-tower? new-a b)
)
)
)
)
(define (same-type? x y)
(equal? (type-tag x) (type-tag y))
)
(define (raise-all arg-list)
(cond
((null? arg-list) (list))
(else
(cons
(try-raise (car arg-list))
(raise-all (cdr arg-list))
)
)
)
)
(define (try-raise x)
; Tries to raise the object. If successful, it returns the raised object
; Otherwise it returns the same object
(let ((proc (get 'raise (list (type-tag x)))))
(if proc
(proc (contents x))
x
)
)
)
(define (dissimilar? items)
; evaluates to true if the list contains dissimilar items
; false if all the items are the same
(cond
((not (pair? items)) false)
((null? items) false)
; reached the last item in the list
((null? (cdr items)) false)
((equal? (car items) (cadr items))
; continue looking
(dissimilar? (cdr items))
)
(else
true
)
)
)
(define (find-element items index)
; finds and returns the item in the 'index' position of the list
(cond
((and (> index 0) (<= index (length items)))
(if (= index 1)
(car items)
(find-element (cdr items) (- index 1))
)
)
(else
null
)
)
)
(define (build-coercion-proc-list type-tags target-type)
(cond
((null? type-tags) (list))
((not (pair? type-tags)) (error "type-tags not a pair: " type-tags))
(else
(cons
(get-coercion (car type-tags) target-type)
(build-coercion-proc-list (cdr type-tags) target-type)
)
)
)
)
(define (allValid? procs)
(cond
((null? procs) true)
((not (pair? procs)) (error "procs not a pair: " procs))
((car procs) (allValid? (cdr procs)))
(else
false
)
)
)
(define (coerce args coercion-procs)
(cond
((or (null? args) (null? coercion-procs)) (list))
((not (pair? args)) (error "args not a pair: " args))
((not (pair? coercion-procs)) (error "coercion-procs not a pair: " coercion-procs))
((not (= (length args) (length coercion-procs))) (error "The number of coercion procs needs to be equal to the number of arguments"))
(else
(cons ((car coercion-procs) (car args)) (coerce (cdr args) (cdr coercion-procs)))
)
)
)
(define (type-tag datum)
(cond
((pair? datum) (car datum))
((number? datum) (determine-number-type datum))
(else
(error "Bad tagged datum -- TYPE-TAG" datum)
)
)
)
(define (contents datum)
(cond
((pair? datum) (cdr datum))
((number? datum) datum)
(else
(error "Bad tagged datum -- CONTENTS" datum)
)
)
)
(define (determine-number-type x)
(if (not (= (imag-part x) 0))
; number has a non-zero imaginary part
'complex
; number is real
'real
)
)
; Coercion procedures
(define (complex->complex z) z)
(define (rational->complex x)
(let ((rat (contents x)))
(make-complex-from-real-imag (* 1.0 (/ (numer rat) (denom rat))) 0)
)
)
(define (get-coercion type1 type2)
(define coercion-table
(list
; Since I have re-written "apply-generic", there should be no harm in
; keeping coercion procedures that convert from one type to the same type.
; These should not cause infinite loops any more
(cons
'complex
(list
(cons 'complex complex->complex)
)
)
(cons
'rational
(list
(cons 'complex rational->complex)
)
)
)
)
(define (find-row type-name table)
(cond
((not (pair? table)) false)
((null? table) (error "Type row not found for " type-name))
(else
(if (equal? type-name (car (car table)))
(car table)
(find-row type-name (cdr table))
)
)
)
)
(define (find-type-in-row type-list type-name)
(cond
((not (pair? type-list)) false)
((null? type-list) (error "type not found: " type-name))
(else
(if (equal? type-name (car (car type-list)))
(cdr (car type-list))
(find-type-in-row (cdr type-list) type-name)
)
)
)
)
(if (find-row type1 coercion-table)
(find-type-in-row (cdr (find-row type1 coercion-table)) type2)
false
)
)