-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
2077 lines (1911 loc) · 58 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Forth thru a Lisp-y Lens</title>
<meta name="description"
content="Forth thru a Lisp-y Lens - February 23, 2019">
<meta name="author" content="Brad Nelson">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style"
content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../reveal.js/css/reveal.css">
<link rel="stylesheet" href="../reveal.js/css/theme/simple.css" id="theme">
<!-- Code syntax highlighting -->
<!--
<link rel="stylesheet" href="../reveal.js/lib/css/zenburn.css">
-->
<script src="../common/printing.js"></script>
<!--[if lt IE 9]>
<script src="../reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<style>
pre {
background-color: #ddd;
}
pre.lisp {
background-color: #fed;
}
pre.forth {
background-color: #def;
}
span.lisp {
color: #c80;
}
span.forth {
color: #08c;
}
</style>
</head>
<!--
Forth thru a Lisp-y Lens
------------------------
For over two decades, Abelson and Sussman's seminal work,
"Structured Interpretation of Computer Programs",
served as the text for MIT's intro CS course.
It uses Lisp/Scheme as a vehicle to explore programming in general,
and explains this choice of language by proposing that a powerful
programming language should serve as solid framework in which
to organize ideas.
Together we'll hold Forth up to that lens and explore how well
several of the ideas and examples from the book work when expressed
in Forth.
https://mitpress.mit.edu/sites/default/files/sicp/index.html
-->
<body>
<div style="position: fixed; bottom: 0; width: 100%">
<center>
<h2 style="font-family=monospace; color: blue" id="cc1"></h2>
<h2 style="font-family=monospace" id="cc2"></h2>
</center>
</div>
<div class="reveal">
<div class="slides">
<section data-transition="fade-out">
<h1><span class="forth">Forth</span>
thru a
<span class="lisp">Lisp-y</span>
Lens</h1>
<h2>February 23, 2019</h2>
<p>
<small><a href="http://flagxor.com">Brad Nelson</a> /
<a href="http://twitter.com/flagxor">@flagxor</a></small>
</p>
</section>
<section data-transition="fade-out">
<h2>Introduction</h2>
<ul>
<img src="https://upload.wikimedia.org/wikipedia/commons/9/9d/SICP_cover.jpg" style="float: right;" width="300">
<li>Structured Interpretation of Computer Programs</li>
<li>6.001</li>
<li>1980 - 2007</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>Disclaimers</h2>
<ul>
<li>SICP is a big book, I'll cover <b>some</b></li>
<li>There are many Forths, and many Lisps</li>
<li>Reframing from scratch is hard</li>
<li>Trying to illuminate, not stack rank</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>Lisp</h2>
<img src="https://imgs.xkcd.com/comics/lisp_cycles.png">
<br/>
<small>https://imgs.xkcd.com/comics/lisp_cycles.png</small>
</section>
<section data-transition="fade-out">
<h2>Chapters</h2>
<ul>
<li><b>1. Building Abstraction with Procedures</b></li>
<li><b>2. Building Abstraction with Data</b></li>
<li>3. Modularity, Objects, and State</li>
<li><b>4. Metalinguistic Abstraction</b></li>
<li>5. Computing with Register Machines</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>1. Building Abstraction with Procedures</h2>
</section>
<section data-transition="fade-out">
<h2>Elements of Programming</h2>
<ul>
<li>Primitive Expressions</li>
<li>Means of Combination</li>
<li>Means of Abstraction</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>Primitive Expressions</h2>
<div layout="inline">
<pre style="float: left; width: 30%;" class="lisp">
(+ 137 349)
486
(- 1000 334)
666
(/ 10 5)
2
(+ 2.7 10)
12.7
</pre>
<pre style="float: right; width: 30%;" class="forth">
137 349 + .
486
1000 334 - .
666
10 5 / .
2
2.7e 10e f+ f.
12.7
</pre>
</div>
</section>
<section data-transition="fade-out">
<h2>Means of Combination</h2>
<pre class="lisp">
; Nesting
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
</pre>
<pre class="forth">
\ Nesting by way of stacks
2 4 * 3 5 + 3 * 10 7 - 6 + +
</pre>
</section>
<section data-transition="fade-out">
<h2>Means of Abstraction</h2>
<pre class="lisp">
(define size 2)
(* 5 size)
10
</pre>
<pre class="forth">
: size 5 ; \ 5 constant size
size 5 * .
10
</pre>
</section>
<section data-transition="fade-out">
<h2>Compound Procedures</h2>
<pre class="lisp">
(define (square x) (* x x))
(square 21)
441
</pre>
<pre class="forth">
: square dup * ;
21 square .
441
</pre>
</section>
<section data-transition="fade-out">
<h2>Compound Procedures</h2>
<pre class="lisp">
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(sum-of-squares 3 4)
25
</pre>
<pre class="forth">
: square dup * ;
: sum-of-squares square swap square + ;
3 4 sum-of-squares .
25
</pre>
</section>
<section data-transition="fade-out">
<h2>Conditionals</h2>
<pre class="lisp">
(define (abs x)
(if (< x 0) (- x) x))
</pre>
<pre class="forth">
: abs ( n -- n )
dup 0< if negate then ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Conditionals</h2>
<pre class="lisp">
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
</pre>
<pre class="forth">
: abs ( n -- n )
dup 0> if exit then
dup 0= if exit then
dup 0< if negate exit then ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Euclid's Algorithm</h2>
<pre class="lisp">
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
</pre>
<pre class="forth">
: gcd ( a b -- n )
dup 0= if drop else swap over mod recurse then ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Square Root</h2>
<pre class="lisp">
(define (square x) (* x x))
(define (average x y) (/ (+ x y) 2))
(define (improve guess x) (average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (sqrt x) (sqrt-iter 1.0 x))
</pre>
</section>
<section data-transition="fade-out">
<h2>Square Root</h2>
<pre class="forth">
: fsquare ( n -- n2 ) fdup f* ;
: faverage ( a b -- mid ) f+ 2e f/ ;
: fgood-enough? ( x guess ) fsquare f- fabs 0.001e f< ;
: fimprove ( guess x ) fover f/ faverage ;
: f2dup ( a b -- a b a b ) fover fover ;
: fsqrt-iter ( x guess )
f2dup fgood-enough? if fnip else fover fimprove recurse then ;
: fsqrt ( x -- rx ) 1.0e fsqrt-iter ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Square Root</h2>
<pre class="forth">
: square ( n -- n2 ) dup * ;
: average ( a b -- mid ) + 2/ ;
: improve ( guess x ) over / average ;
: good-enough? ( x guess ) dup >r square - abs r> < ;
: sqrt-iter ( x guess )
2dup good-enough? if nip else over improve recurse then ;
: sqrt ( x -- rx ) 1 sqrt-iter ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Recursive Factorial</h2>
<pre class="lisp">
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
</pre>
<pre class="forth">
: factorial ( n -- n! )
dup 1 <> if dup 1- recurse * then ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Recursive Factorial</h2>
<pre class="lisp">
(factorial 4)
(* 4 (factorial 3))
(* 4 (* 3 (factorial 2)))
(* 4 (* 3 (* 2 (factorial 1))))
(* 4 (* 3 (* 2 1)))
(* 4 (* 3 2))
(* 4 6)
24
</pre>
<pre class="forth">
4 factorial
4 3 factorial *
4 3 2 factorial * *
4 3 2 1 factorial * * *
4 3 2 1 * * *
4 3 2 * *
4 6 *
24
</pre>
</section>
<section data-transition="fade-out">
<h2>Iterative Factorial</h2>
<pre class="lisp">
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
</pre>
<pre class="forth">
: factorial ( n -- n! )
1+ 1 swap 1 ?do i * loop ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Iterative Factorial</h2>
<pre class="lisp">
(factorial 4)
(fact-iter 1 1 4)
(fact-iter 1 2 4)
(fact-iter 2 3 4)
(fact-iter 6 4 4)
(fact-iter 24 5 4)
24
</pre>
<pre class="forth">
4 factorial
1 ( i = 1 )
1 2 * ( i = 2 )
2 3 * ( i = 3 )
6 4 * ( i = 4 )
24
</pre>
</section>
<section data-transition="fade-out">
<h2>Recursive Fibonacci</h2>
<pre class="lisp">
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
</pre>
<pre class="forth">
: fib ( n -- nfib )
dup 1 > if dup 1- recurse swap 2 - recurse + then ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Tree Recursion</h2>
<img src="https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/ch1-Z-G-13.gif">
<small>https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/ch1-Z-G-13.gif</small>
</section>
<section data-transition="fade-out">
<h2>Iterative Fibonacci</h2>
<pre class="lisp">
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
</pre>
<pre class="forth">
: fib-iter ( n -- nfib )
0 1 rot 0 ?do swap over + loop drop ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Counting Change</h2>
<pre class="lisp">
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
</pre>
</section>
<section data-transition="fade-out">
<h2>Counting Change</h2>
<pre class="forth">
create denominations 1 , 5 , 10 , 25 , 50 ,
: first-denomination ( kinds-of-coins -- n )
1- cells denominations + @ ;
: cc ( amount kinds-of-coins )
recursive
over 0= if 2drop 1 exit then
2dup 0= swap 0< or if 2drop 0 exit then
2dup 1- cc >r dup >r first-denomination - r> cc r> + ;
: count-change ( amount -- n ) 5 cc ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Higher Order Procedures</h2>
<ul>
<li>Procedures that manipulate procedures</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>Procedures as Arguments (summing)</h2>
<pre class="lisp">
(define sum-integers a b)
(if > a b)
0
(+ a (sum-integers (+ a 1) b))))
</pre>
<pre class="forth">
: sum-integers ( b a -- sum )
0 -rot ?do i + loop ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Procedures as Arguments (cubes)</h2>
<pre class="lisp">
(define sum-cubes a b)
(if > a b)
0
(+ (cube a) (sum-cubes (+ a 1) b))))
</pre>
<pre class="forth">
: sum-cubes ( b a -- sum )
0 -rot ?do i cube + loop ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Procedures as Arguments (pi-sum)</h2>
<pre class="lisp">
(define pi-sum a b)
(if > a b)
0
(+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))
</pre>
<pre class="forth">
: pi-sum ( b a -- sum )
0 -rot ?do
1000000000 i 2 + i * / +
4 +loop ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Procedures as Arguments</h2>
<pre class="lisp">
(define sum term a next b)
(if > a b)
0
(+ (term a) (sum (next a) next b))))
(define (inc n) (+ n 1))
(define (sum-cubes a b) (sum cube a inc b))
</pre>
<pre class="forth">
: sum ( next term b a -- n )
0 -rot ?do over i swap execute dup . +
>r over r> swap execute dup .
+loop nip nip ;
: one 1 ;
: sum-cubes ( b a -- n )
['] one -rot ['] cube -rot sum ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Lambda (?)</h2>
<pre class="lisp">
(define (adder a) (lambda (b) (+ a b)))
((adder 3) 4) ; 7
</pre>
<pre class="forth">
?
</pre>
</section>
<section data-transition="fade-out">
<h2>Lambda (create does>)</h2>
<pre class="lisp">
(define (adder a) (lambda (b) (+ a b)))
((adder 3) 4) ; 7
</pre>
<pre class="forth">
: adder create , does> @ + ;
3 adder 3plus
4 3plus \ 7
</pre>
</section>
<section data-transition="fade-out">
<h2>Lambda (dynamic)</h2>
<pre class="lisp">
(define (adder a) (lambda (b) (+ a b)))
((adder 3) 4) ; 7
</pre>
<pre class="forth">
: adder create , does> @ + ;
4 3 noname adder latestxt execute \ 7
</pre>
</section>
<section data-transition="fade-out">
<h2>Lambda (closure)</h2>
<pre class="lisp">
(define (adder a) (lambda (b) (+ a b)))
((adder 3) 4) ; 7
</pre>
<pre class="forth">
: adder ( n -- xt ) >s [: s> + ;] sdrop ;
4 3 adder execute
</pre>
</section>
<section data-transition="fade-out">
<h2>Lambda (closure+)</h2>
<pre class="lisp">
(define (adder a) (lambda (b) (+ a b)))
((adder 3) 4) ; 7
</pre>
<pre class="forth">
: adder
>s ( add to scope stack )
[:
s> ( pull out of scope )
+
;]
sdrop ( drop in the parent scope )
;
4 3 adder execute
<small>
<a href="http://bradn123.github.io/literateforth/out/events_0001.html">August 25, 2012 - Event Driven Programming</a>
</small>
</pre>
</section>
<section data-transition="fade-out">
<h2>2. Building Abstraction with Data</h2>
</section>
<section data-transition="fade-out">
<h2>Two Kinds of "Closure"</h2>
<ul>
<li>Combined things can themselves be combined using same ops</li>
<li>Function which has its own environment</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>Abstraction Barriers</h2>
<ul>
<li>Define layered abstractions</li>
<li>Compose them to isolate implementation details</li>
</ul>
</section>
<section data-transition="fade-out">
<h2>Rational Numbers</h2>
<u>Programs that use rational numbers</u><br/>
Rational numbers in problem domain<br/>
<u>add-rat, sub-rat, ...</u><br/>
Rational numbers as numerators and denominators<br/>
<u>make-rat, numer, denom</u><br/>
Rational number as pairs<br/>
<u>cons, car, cdr</u><br/>
However pairs are implemented<br/>
</ul>
</section>
<section data-transition="fade-out">
<h2>Bringing Lisp-y Lists to Forth</h2>
<ul>
<li class="fragment">Store in the dictionary</li>
<li class="fragment">Leak like crazy</li>
<li class="fragment">Zone/Arena allocation works ok</li>
<li class="fragment">Handles / Boehm collector if we're serious</li>
<li class="fragment">We probably aren't</li>
</ul>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Symbols and Pairs )
: cons ( a b -- c ) noname create , , latestxt ;
: car ( c -- a ) execute cell+ @ ;
: cdr ( c -- b ) execute @ ;
: atom create latest , ;
: atom>string ( x -- a n ) @ name>string ;
: atom. ( x -- ) atom>string type space ;
</pre>
</section>
<section data-transition="fade-out">
<h2>Generic Operators</h2>
<ul>
<li>Higher level abstraction</li>
<li>Use the power of the closure property</li>
</ul>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Reinterpret Floats and Numbers )
variable ftemp
: f->n ftemp f! ftemp @ ;
: n->f ftemp ! ftemp f@ ;
( Utility )
: private[[ get-order wordlist swap 1+ set-order definitions ;
: ]]private previous definitions ;
: fsquare fdup f* ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(put <op> <type> <item>)
; installs the <item> in the table,
; indexed by the <op> and the <type>.
(get <op> <type>)
; looks up the <op>, <type> entry in the table
; and returns the item found there.
; If no item is found, get returns false.
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Type Tags )
: attach-tag swap cons ;
: type-tag car ;
: contents cdr ;
( Type Table )
variable table
: put ( item op type -- )
cons swap cons table @ cons table ! ;
: equiv ( a b -- f )
2dup car swap car = -rot cdr swap cdr = and ;
: get ( op type -- item )
cons table @ begin dup while
2dup car car equiv if nip car cdr exit then
cdr repeat -1 throw ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(if proc
(apply proc (map contents args))
(error
"No method for these types -- APPLY-GENERIC"
(list op type-tags))))))
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
<b>(define (apply-generic op . args)
(let ((type-tags (map type-tag args)))
(let ((proc (get op type-tags)))
(if proc
(apply proc (map contents args))</b>
(if (= (length args) 2)
(let ((type1 (car type-tags))
(type2 (cadr type-tags))
(a1 (car args))
(a2 (cadr args)))
(let ((t1->t2 (get-coercion type1 type2))
(t2->t1 (get-coercion type2 type1)))
(cond (t1->t2<b>
(apply-generic op (t1->t2 a1) a2))
(t2->t1
(apply-generic op a1 (t2->t1 a2)))</b>
(else
(error "No method for these types"
(list op type-tags))))))
(error "No method for these types"
(list op type-tags)))))))
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Applying generic ops )
: apply-generic ( .. op -- .. )
over type-tag get swap contents swap execute ;
: apply-generic2 ( .. op -- .. )
over type-tag get rot contents rot contents rot execute ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(define (real-part z) (apply-generic 'real-part z))
(define (imag-part z) (apply-generic 'imag-part z))
(define (magnitude z) (apply-generic 'magnitude z))
(define (angle z) (apply-generic 'angle z))
(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))
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Generic Complex Ops )
atom 'real-part atom 'imag-part
atom 'magnitude atom 'angle
atom 'make-from-real-imag atom 'make-from-mag-ang
atom 'rectangular atom 'polar
: real-part 'real-part apply-generic ;
: imag-part 'imag-part apply-generic ;
: magnitude 'magnitude apply-generic ;
: angle 'angle apply-generic ;
: rect>z 'make-from-real-imag 'rectangular get execute ;
: polar>z 'make-from-mag-ang 'polar get execute ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(define (add-complex z1 z2)
(make-from-real-imag (+ (real-part z1) (real-part z2))
(+ (imag-part z1) (imag-part z2))))
(define (sub-complex z1 z2)
(make-from-real-imag (- (real-part z1) (real-part z2))
(- (imag-part z1) (imag-part z2))))
(define (mul-complex z1 z2)
(make-from-mag-ang (* (magnitude z1) (magnitude z2))
(+ (angle z1) (angle z2))))
(define (div-complex z1 z2)
(make-from-mag-ang (/ (magnitude z1) (magnitude z2))
(- (angle z1) (angle z2))))
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Complex Math )
: z+ ( z1 z2 -- z )
2dup real-part real-part f+
imag-part imag-part f+ rect>z ;
: z- ( z1 z2 -- z )
2dup real-part real-part fswap f-
imag-part imag-part fswap f- rect>z ;
: z* ( z1 z2 -- z )
2dup magnitude magnitude f*
angle angle f+ polar>z ;
: z/ ( z1 z2 -- z )
2dup magnitude magnitude fswap f/
angle angle fswap f- polar>z ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
: zsquare ( z -- z2 ) dup z* ;
: z. ( z -- )
." ( " dup real-part f. ." + i * " imag-part f. ." ) " ;
: zp. ( z -- )
." ( " dup magnitude f. ." * e^ ( i * " angle f. ." ) ) " ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(define (install-rectangular-package)
;; internal procedures
(define (real-part z) (car z))
(define (imag-part z) (cdr z))
(define (make-from-real-imag x y) (cons x y))
(define (magnitude z)
(sqrt (+ (square (real-part z))
(square (imag-part z)))))
(define (angle z)
(atan (imag-part z) (real-part z)))
(define (make-from-mag-ang r a)
(cons (* r (cos a)) (* r (sin a))))
;; interface to the rest of the system
(define (tag x) (attach-tag 'rectangular x))
(put 'real-part '(rectangular) real-part)
(put 'imag-part '(rectangular) imag-part)
(put 'magnitude '(rectangular) magnitude)
(put 'angle '(rectangular) angle)
(put 'make-from-real-imag 'rectangular
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'rectangular
(lambda (r a) (tag (make-from-mag-ang r a))))
'done)
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Rectangular Complex Numbers )
private[[
: real-part car n->f ;
: imag-part cdr n->f ;
: magnitude
dup real-part fsquare
imag-part fsquare f+ fsqrt ;
: angle
dup imag-part
real-part fatan2 ;
: make-from-real-imag
f->n f->n swap cons 'rectangular attach-tag ;
' real-part 'real-part 'rectangular put
' imag-part 'imag-part 'rectangular put
' magnitude 'magnitude 'rectangular put
' angle 'angle 'rectangular put
' make-from-real-imag 'make-from-real-imag 'rectangular put
]]private
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(define (install-polar-package)
;; internal procedures
(define (magnitude z) (car z))
(define (angle z) (cdr z))
(define (make-from-mag-ang r a) (cons r a))
(define (real-part z)
(* (magnitude z) (cos (angle z))))
(define (imag-part z)
(* (magnitude z) (sin (angle z))))
(define (make-from-real-imag x y)
(cons (sqrt (+ (square x) (square y)))
(atan y x)))
;; interface to the rest of the system
(define (tag x) (attach-tag 'polar x))
(put 'real-part '(polar) real-part)
(put 'imag-part '(polar) imag-part)
(put 'magnitude '(polar) magnitude)
(put 'angle '(polar) angle)
(put 'make-from-real-imag 'polar
(lambda (x y) (tag (make-from-real-imag x y))))
(put 'make-from-mag-ang 'polar
(lambda (r a) (tag (make-from-mag-ang r a))))
'done)
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Polar Complex Numbers )
private[[
: magnitude car n->f ;
: angle cdr n->f ;
: real-part dup magnitude angle fcos f* ;
: imag-part dup magnitude angle fsin f* ;
: make-from-mag-ang
f->n f->n swap cons 'polar attach-tag ;
' real-part 'real-part 'polar put
' imag-part 'imag-part 'polar put
' magnitude 'magnitude 'polar put
' angle 'angle 'polar put
' make-from-mag-ang 'make-from-mag-ang 'polar put
]]private
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(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 (make-scheme-number n)
((get 'make 'scheme-number) n))
(define (make-rational n d)
((get 'make 'rational) n d))
(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))
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Generic Ops )
atom '+ atom '- atom '* atom '/ atom '. atom 'make
atom 'number atom 'float atom 'complex
atom 'rational atom 'polynomial
: g+ '+ apply-generic2 ;
: g- '- apply-generic2 ;
: g* '* apply-generic2 ;
: g/ '/ apply-generic2 ;
: g. '. apply-generic ;
: make-number 'make 'number get execute ;
: make-float 'make 'float get execute ;
: make-complex 'make 'complex get execute ;
: make-rational 'make 'rational get execute ;
: make-poly 'make 'polynomial get execute ;
</pre>
</section>
<section data-transition="fade-out">
<pre class="lisp">
(define (install-scheme-number-package)
(define (tag x)
(attach-tag 'scheme-number x))
(put 'add '(scheme-number scheme-number)
(lambda (x y) (tag (+ x y))))
(put 'sub '(scheme-number scheme-number)
(lambda (x y) (tag (- x y))))
(put 'mul '(scheme-number scheme-number)
(lambda (x y) (tag (* x y))))
(put 'div '(scheme-number scheme-number)
(lambda (x y) (tag (/ x y))))
(put 'make 'scheme-number
(lambda (x) (tag x)))
'done)
</pre>
</section>
<section data-transition="fade-out">
<pre class="forth">
( Simple Numbers )
private[[
: tag 'number attach-tag ;
: add + tag ;
: sub - tag ;
: mul * tag ;
: div / tag ;
: print . ;
: make tag ;
' add '+ 'number put
' sub '- 'number put
' mul '* 'number put