-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesignRecipesEDX.html
1669 lines (1461 loc) · 88.3 KB
/
DesignRecipesEDX.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
<html style><!--
Page saved with SingleFile
url: https://s3.amazonaws.com/edx-course-spdx-kiczales/HTC/Pages/design-recipes.html#2One-of
saved date: Mon Apr 03 2023 19:59:50 GMT+0000 (Coordinated Universal Time)
--><meta charset=utf-8><link rel=canonical href=https://s3.amazonaws.com/edx-course-spdx-kiczales/HTC/Pages/design-recipes.html#2One-of><meta http-equiv=content-security-policy content="default-src 'none'; font-src 'self' data:; img-src 'self' data:; style-src 'unsafe-inline'; media-src 'self' data:; script-src 'unsafe-inline' data:; object-src 'self' data:; frame-src 'self' data:;"><style>img[src="data:,"],source[src="data:,"]{display:none!important}</style><body><p><a name=top></a></p>
<h2>DESIGN RECIPES</h2>
<p>In this course, we teach an approach to program design based on design recipes. Each recipe is applicable to certain problems, and systematizes the process of designing solutions to those problems.</p>
<p>There are three core recipes that are used most frequently. The templating recipes are used as part of the design of every data definition and function. Abstraction recipes are used to reduce redundancy in code.</p>
<p>There is also a <a href=https://s3.amazonaws.com/edx-course-spdx-kiczales/HTC/recipe-checklist.pdf>checklist</a> to help you remember the recipe steps.</p>
<table title="Design Recipes" style="border:1px solid #000000;padding:8px;text-align:left;horizontal-align:left;vertical-align:top;max-width:90%" cellpadding=8 border=1>
<tbody></tbody>
<thead>
<tr><th rowspan=2 scope=col width=28%>Core Recipes<th rowspan=1 colspan=2 scope=col style=text-align:center width=50%>Templating<th rowspan=2 scope=col>Abstraction</tr>
<tr><th scope=col width=25%>Data Driven<th scope=col width=25%>Control Driven</tr>
</thead>
<tbody>
<tr>
<td><a href=#HtDF>How to Design Functions (HtDF)</a> <br> <small>Design any function.</small></td>
<td><a href=#Data>Data Driven Templates</a><br> <small> Produce template for a data definition based on the form of the type comment. </small></td>
<td><a href=#FuncComp>Function Composition</a></td>
<td><a href=#Examples>From Examples</a> <br> <small> Produce an abstract function given two similar functions. </small></td>
</tr>
<tr>
<td><a href=#HtDD>How to Design Data (HtDD)</a> <br> <small> Produce data definitions based on structure of the information to be represented. </small></td>
<td><a href=#2One-of>2 One-of Data</a> <br> <small> Functions where 2 arguments have a one-of in their type comments. </small></td>
<td><a href=#Backtrack>Backtracking Search</a></td>
<td><a href=#Type>From Type Comments</a> <br> <small> Produce a fold function given type comments. </small></td>
</tr>
<tr>
<td><a href=#HtDW>How to Design Worlds (HtDW)</a> <br> <small> Produce interactive programs that use <tt>big-bang</tt>.</small></td>
<td rowspan=2></td>
<td><a href=#GenRec>Generative Recursion</a></td>
<td></td>
</tr>
<tr>
<td rowspan=2></td>
<td><a href=#Accumulators>Accumulators</a></td>
<td><a href=#Using>Using Abstract Functions</a></td>
</tr>
<tr>
<td colspan=2 scope=col style=text-align:center><a href=#TempBlend>Template Blending</a></td>
<td></td>
</tr>
</tbody>
</table>
<p><br> </p>
<p></p>
<p><a name=HtDF></a></p>
<hr><hr><hr>
<div align=right><a href=#top>Back to Design Recipes Table</a></div>
<h2>How To Design Functions (HtDF)</h2>
<p>The How to Design Functions (HtDF) recipe is a <strong>design method</strong> that enables systematic design of functions. We will use this recipe throughout the term, although we will enhance it as we go to solves more complex problems.</p>
<table summary="Steps of the HtDF Recipe." style="border:1px solid #000000;padding:5px;text-align:left;horizontal-align:left;vertical-align:top;max-width:90%" cellpadding=5 border=1>
<tbody>
<tr style=text-align:left>
<td>
<p>The HtDF recipe consists of the following steps:</p>
<ol>
<li><a href=#S1>Signature, purpose and stub.</a></li>
<li><a href=#S2>Define examples, wrap each in <tt>check-expect</tt>.</a></li>
<li><a href=#S3>Template and inventory.</a></li>
<li><a href=#S4>Code the function body.</a></li>
<li><a href=#S5>Test and debug until correct</a></li>
</ol>NOTE:
<ul>
<li>Each of these steps build on the ones that precede it. The signature helps write the purpose, the stub, and the check-expects; it also helps code the body. The purpose helps write the check-expects and code the body. The stub helps to write the check-expects. The check-expects help to code the body as well as to test the complete design.</li>
<li>It is sometimes helpful to do the steps in a different order. Sometimes it is easier to write examples first, then do signature and purpose. Often at some point during the design you may discover an issue or boundary condition you did not anticipate, at that point go back and update the purpose and examples accordingly. But you should never write the function definition first and then go back and do the other recipe elements -- for some of you that will work for simple functions, but you will not be able to do that for the complex functions later in the course!</li>
<li>Throughout the HtDF process be sure to "run early and run often". Run your program whenever it is well-formed. The more often you press run the sooner you can find mistakes. Finding mistakes one at a time is much easier than waiting until later when the mistakes can compound and be more confusing. Run, run, run!</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p></p>
<p><a name=S1></a></p>
<h4>Signature, purpose and stub.</h4>
<p>Write the function signature, a one-line purpose statement and a function stub.</p>
<p>A signature has the type of each argument, separated by spaces, followed by <tt>-></tt>, followed by the type of result. So a function that consumes an image and produces a number would have the signature <tt>Image -> Number</tt>.</p>
<p>Note that the stub is a syntactically complete function definition that produces a value of the right type. If the type is Number it is common to use 0, if the type is String it is common to use "a" and so on. The value will not, in general, match the purpose statement. In the example below the stub produces 0, which is a Number, but only matches the purpose when double happens to be called with 0.</p>
<pre>;; Number -> Number
;; produces n times 2
(define (double n) 0) ; this is the stub</pre>
<p>The purpose of the stub is to serve as a kind of scaffolding to make it possible to run the examples even before the function design is complete. With the stub in place check-expects that call the function can run. Most of them will fail of course, but the fact that they can run at all allows you to ensure that they are at least well-formed: parentheses are balanced, function calls have the proper number of arguments, function and constant names are correct and so on. This is very important, the sooner you find a mistake -- even a simple one -- the easier it is to fix.</p>
<p><a name=S2></a></p>
<h4>Define examples, wrap each one in check-expect.</h4>
<p>Write at least one example of a call to the function and the expected result the call should produce.</p>
<p>You will often need more examples, to help you better understand the function or to properly test the function. (If once your function works and you run the program some of the code is highlighted in black it means you definitely do not have enough examples.) If you are unsure how to start writing examples use the combination of the function signature and the data definition(s) to help you generate examples. Often the example data from the data definition is useful, but it does not necessarily cover all the important cases for a particular function.</p>
<p>The first role of an example is to help you understand what the function is supposed to do. If there are boundary conditions be sure to include an example of them. If there are different behaviours the function should have, include an example of each. Since they are examples first, you could write them in this form:</p>
<pre>;; (double 0) should produce 0
;; (double 1) should produce 2
;; (double 2) should produce 4</pre>
<p>When you write examples it is sometimes helpful to write not just the expected result, but also how it is computed. For example, you might write the following instead of the above:</p>
<pre>;; (double 0) should produce (* 0 2)
;; (double 1) should produce (* 1 2)
;; (double 2) should produce (* 2 2)</pre>
<p>While the above form satisfies our need for examples, DrRacket gives us a better way to write them, by enclosing them in check-expect. This will allow DrRacket to check them automatically when the function is complete. (In technical terms it will turn the examples into unit tests.)</p>
<pre>;; Number -> Number
;; produces n times 2
(check-expect (double 0) (* 0 2))
(check-expect (double 1) (* 1 2))
(check-expect (double 3) (* 3 2))
(define (double n) 0) ; this is the stub</pre>
<p>The existence of the stub will allow you to run the tests. Most (or even all) of the tests will fail since the stub is returning the same value every time. But you will at least be able to check that the parentheses are balanced, that you have not misspelled function names etc.</p>
<p><a name=S3></a></p>
<h4>Template and inventory</h4>
<p>Before coding the function body it is helpful to have a clear sense of what the function has to work with -- what is the contents of your bag of parts for coding this function? The template provides this.</p>
<p> </p>
<p>Once the How to Design Data Definitions (HtDD) recipe in introduced, templates are produced by following the rules on the <a href=#Data>Data Driven Templates</a> web page. You should copy the template from the data definition to the function design, rename the template, and write a comment that says where the template was copied from. Note that the template is copied from the data definition for the consumed type, not the produced type.</p>
<p>For primitive data like numbers, strings and images the body of the template is simply <tt>(... x)</tt> where x is the name of the parameter to the function.</p>
<p>Once the template is done the stub should be commented out.</p>
<pre>;; Number -> Number
;; produces n times 2
(check-expect (double 0) (* 0 2))
(check-expect (double 1) (* 1 2))
(check-expect (double 3) (* 3 2))
;(define (double n) 0) ; this is the stub
(define (double n) ; this is the template
(... n))</pre>
<p>It is also often useful to add constant values which are extremely likely to be useful to the template body at this point. For example, the template for a function that renders the state of a world program might have an MTS constant added to its body. This causes the template to include an inventory of useful constants.</p>
<p><a name=S4></a></p>
<h4>Code the function body</h4>
<p>Now complete the function body by filling in the template.</p>
<p>Note that:</p>
<ul>
<li>the signature tells you the type of the parameter(s) and the type of the data the function body must produce</li>
<li>the purpose describes what the function body must produce in English</li>
<li>the examples provide several concrete examples of what the function body must produce</li>
<li>the template tells you the raw material you have to work with</li>
</ul>
<p>You should use all of the above to help you code the function body. In some cases further rewriting of examples might make it more clear how you computed certain values, and that may make it easier to code the function.</p>
<pre>;; Number -> Number
;; produces n times 2
(check-expect (double 0) (* 0 2))
(check-expect (double 1) (* 1 2))
(check-expect (double 3) (* 3 2))
;(define (double n) 0) ; this is the stub
;(define (double n) ; this is the template
; (... n))
(define (double n)
(* n 2))</pre>
<p><a name=S5></a></p>
<h4>Test and debug until correct</h4>
<p>Run the program and make sure all the tests pass, if not debug until they do. Many of the problems you might have had will already have been fixed because of following the "run early, run often" approach. But if not, debug until everything works.</p>
<p><a name=HtDD></a> </p>
<hr><hr><hr>
<div align=right><a href=#top>Back to Design Recipes Table</a></div>
<h2>How To Design Data (HTDD)</h2>
<p>Data definitions are a driving element in the design recipes.</p>
<p>A data definition establishes the represent/interpret relationship between information and data:</p>
<ul>
<li>Information in the program's domain is represented by data in the program.</li>
<li>Data in the program can be interpreted as information in the program's domain.</li>
</ul>
<p>A data definition must describe how to form (or make) data that satisfies the data definition and also how to tell whether a data value satisfies the data definition. It must also describe how to represent information in the program's domain as data and interpret a data value as information.</p>
<p>So, for example, one data definition might say that numbers are used to represent the <tt>Speed</tt> of a ball. Another data definition might say that numbers are used to represent the <tt>Height</tt> of an airplane. So given a number like 6, we need a data definition to tell us how to interpret it: is it a <tt>Speed</tt>, or a <tt>Height</tt> or something else entirely. Without a data definition, the 6 could mean anything.</p>
<table title="Data Definitions." style="border:1px solid #000000;padding:15px;text-align:left;horizontal-align:left;vertical-align:top;max-width:90%" cellpadding=15 border=1>
<tbody>
<tr>
<td>
<p>The first step of the recipe is to identify the inherent structure of the information.</p>
<p>Once that is done, a data definition consists of four or five elements:</p>
<ol>
<li>A possible <strong>structure definition</strong> (not until compound data)</li>
<li>A <strong>type comment</strong> that defines a new type name and describes how to form data of that type.</li>
<li>An <strong>interpretation</strong> that describes the correspondence between information and data.</li>
<li>One or more <strong>examples</strong> of the data.</li>
<li>A <strong>template</strong> for a 1 argument function operating on data of this type.</li>
</ol>
<p><emph>In the first weeks of the course we also ask you to include a list of the <strong>template rules</strong> used to form the template.</emph></p>
<p></p>
</td>
</tr>
</tbody>
</table>
<p></p>
<hr><hr>
<h3>What is the Inherent Structure of the Information?</h3>
<p>One of the most important points in the course is that:</p>
<ul>
<li>the <strong>structure of the information</strong> in the program's domain determines the kind of data definition used,</li>
<li>which in turn determines <strong>the structure of the templates</strong> and helps determine the function examples (<tt>check-expect</tt>s),</li>
<li>and therefore the <strong>structure of much of the final program design</strong>.</li>
</ul>
<p>The remainder of this page lists in detail different kinds of data definition that are used to represent information with different structures. The page also shows in detail how to design a data definition of each kind. This summary table provides a quick reference to which kind of data definition to use for different information structures.</p>
<table frame=box summary=Contents rules=all style="border:1px solid #000000;horizontal-align:left;text-align:left;vertical-align:top" cellpadding=5 border=1>
<tbody>
<tr style=text-align:left></tr>
</tbody>
<thead>
<tr><th>When the form of the information to be represented...<th>Use a data definition of this kind</tr>
</thead>
<tbody>
<tr>
<td>is atomic</td>
<td><a href=#Atomic>Simple Atomic Data</a></td>
</tr>
<tr>
<td>is numbers within a certain range</td>
<td><a href=#Interval>Interval</a></td>
</tr>
<tr>
<td>consists of a fixed number of distinct items</td>
<td><a href=#Enumeration>Enumeration</a></td>
</tr>
<tr>
<td>is comprised of 2 or more subclasses, at least one of which is not a distinct item</td>
<td><a href=#Itemization>Itemization</a></td>
</tr>
<tr>
<td>consists of two or more items that naturally belong together</td>
<td><a href=#Compound>Compound data</a></td>
</tr>
<tr>
<td>is naturally composed of different parts</td>
<td><a href=#Reference title="Link: #S6">References to other defined type</a></td>
</tr>
<tr>
<td>is of arbitrary (unknown) size</td>
<td><a href=#Self-Reference>self-referential or mutually referential</a></td>
</tr>
</tbody>
</table>
<p></p>
<hr><hr>
<h3>Simple Atomic Data</h3>
<p>Use simple atomic data <strong>when the information to be represented is itself atomic in form</strong>, such as the elapsed time since the start of the animation, the x coordinate of a car or the name of a cat.</p>
<table title="Time Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>;; Time is Natural
;; interp. number of clock ticks since start of game
(define START-TIME 0)
(define OLD-TIME 1000)
#;
(define (fn-for-time t)
(... t))
;; Template rules used:
;; - atomic non-distinct: Natural
</pre>
</td>
</tr>
</tbody>
</table>
<h4>Forming the Template</h4>
<p>As noted below the template, it is formed according to the <a href=#Data>Data Driven Templates recipe</a> using the right hand column of the atomic non-distinct rule.</p>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>One or two data examples are usually sufficient for simple atomic data.</p>
<p>When creating example/tests for a specific function operating on simple atomic data at least one test case will be required. Additional tests are required if there are multiple cases involved. If the function produces <tt>Boolean</tt> there needs to be at least a <tt>true</tt> and <tt>false</tt> test case. Also be on the lookout for cases where a number of some form is an <a href=#Interval>interval</a> in disguise, for example given a type comment like <tt>Countdown is Natural</tt>, in some functions <tt>0</tt> is likely to be a special case.</p>
<p><br> <a id=Interval></a></p>
<h3>Intervals</h3>
<p>Use an interval when the information to be represented is numbers within a certain range. <tt>Integer[0, 10]</tt> is all the integers from 0 to 10 inclusive; <tt>Number[0, 10)</tt> is all the numbers from 0 inclusive to 10 exclusive. (The notation is that [ and ] mean that the end of the interval includes the end point; ( and ) mean that the end of the interval does not include the end point.)</p>
<p>Intervals often appear in <a href=#Itemization>itemizations</a>, but can also appear alone, as in:</p>
<table title="Countdown Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>;; Countdown is Integer[0, 10]
;; interp. the number of seconds remaining to liftoff
(define C1 10) ; start
(define C2 5) ; middle
(define C3 0) ; end
#;
(define (fn-for-countdown cd)
(... cd))
;; Template rules used:
;; - atomic non-distinct: Integer[0, 10]
</pre>
</td>
</tr>
</tbody>
</table>
<h4>Forming the Template</h4>
<p>As noted below the template, it is formed according to the <a href=#Data>Data Driven Templates recipe</a> using the right hand column of the atomic non-distinct rule.</p>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>For data examples provide sufficient examples to illustrate how the type represents information. The three data examples above are probably more than is needed in that case.</p>
<p>When writing tests for functions operating on intervals be sure to test closed boundaries as well as midpoints. As always, be sure to include enough tests to check all other points of variance in behaviour across the interval.</p>
<p><br> <a id=Enumeration></a></p>
<h3>Enumerations</h3>
<p>Use an enumeration <strong>when the information to be represented consists of a fixed number of distinct items</strong>, such as colors, letter grades etc. The data used for an enumeration could in principle be anything - strings, integers, images even. But we always use strings. In the case of enumerations it is sometimes redundant to provide an interpretation and nearly always redundant to provide examples. The example below includes the interpretation but not the examples.</p>
<table title="TLColor Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>;; LightState is one of:
;; - "red"
;; - "yellow"
;; - "green"
;; interp. the color of a traffic light
;; <examples are redundant for enumerations>
#;
(define (fn-for-light-state ls)
(cond [(string=? "red" ls) (...)]
[(string=? "yellow" ls) (...)]
[(string=? "green" ls) (...)]))
;; Template rules used:
;; - one of: 3 cases
;; - atomic distinct: "red"
;; - atomic distinct: "yellow"
;; - atomic distinct: "green"
</pre>
</td>
</tr>
</tbody>
</table>
<h4>Forming the Template</h4>
<p>As noted below the template, it is formed according to the <a href=#Data>Data Driven Templates recipe</a> as follows:</p>
<p>First, <tt>LightState</tt> is an enumeration with 3 cases, so the <i>one of rule</i> says to use a <tt>cond</tt> with 3 cases:</p>
<table title="TLColor Template" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>(define (fn-for-tlcolor ls)
(cond [Q1 A1]
[Q2 A2]
[Q3 A3]))
</pre>
</td>
</tr>
</tbody>
</table>
<p>In the first clause, <tt>"red"</tt> is a distinct atomic value, so the <tt>cond</tt> question column of the <i>atomic distinct rule</i> says Q1 should be <tt>(string=? ls "red")</tt>. The cond answer column says A1 should be <tt>(...)</tt>. So we have:</p>
<table title="TLColor Template" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>(define (fn-for-light-state ls)
(cond [(string=? "red" ls) (...)]
[Q2 A2]
[Q3 A3]))
</pre>
</td>
</tr>
</tbody>
</table>
<p>Then <tt>"yellow"</tt> and <tt>"green"</tt> are also distinct atomic values, so the final template is:</p>
<table title="TLColor Template" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>(define (fn-for-light-state ls)
(cond [(string=? "red" ls) (...)]
[(string=? "yellow" ls) (...)]
[(string=? "green" ls) (...)]))
</pre>
</td>
</tr>
</tbody>
</table>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>Data examples are redundant for enumerations.</p>
<p>Functions operating on enumerations should have (at least) as many tests as there are cases in the enumeration.</p>
<h4>Large Enumerations</h4>
<p>Some enumerations contain a large number of elements. A canonical example is <tt>KeyEvent</tt>, which is provided as part of big-bang. <tt>KeyEvent</tt> includes all the letters of the alphabet as well as other keys you can press on the keyboard. It is not necessary to write out all the cases for such a data definition. Instead write one or two, as well as a comment saying what the others are, where they are defined etc.</p>
<p>Defer writing templates for such large enumerations until a template is needed for a specific function. At that point include the specific cases that function cares about. Be sure to include an else clause in the template to handle the other cases. As an example, some functions operating on <tt>KeyEvent</tt> may only care about the space key and just ignore all other keys, the following would be an appropriate template for such functions.</p>
<table title="KeyEvent template" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>#;
(define (fn-for-key-event kevt)
(cond [(key=? " " kevt) (...)]
[else
(...)]))
;; Template formed using the large enumeration special case
</pre>
</td>
</tr>
</tbody>
</table>
<p>The same is true of writing tests for functions operating on large enumerations. All the specially handled cases must be tested, in addition one more test is required to check the else clause.</p>
<p><br> <a id=Itemization></a></p>
<h3>Itemizations</h3>
<p>An itemization describes <strong>data comprised of 2 or more subclasses, at least one of which is not a distinct item</strong>. (C.f. enumerations, where the subclasses are <strong>all</strong> distinct items.) In an itemization the template is similar to that for enumerations: a cond with one clause per subclass. In cases where the subclass of data has its own data definition the answer part of the cond clause includes a call to a helper template, in other cases it just includes the parameter.</p>
<table title="Bird Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>;; Bird is one of:
;; - false
;; - Number
;; interp. false means no bird, number is x position of bird
(define B1 false)
(define B2 3)
#;
(define (fn-for-bird b)
(cond [(false? b) (...)]
[(number? b) (... b)]))
;; Template rules used:
;; - one of: 2 cases
;; - atomic distinct: false
;; - atomic non-distinct: Number
</pre>
</td>
</tr>
</tbody>
</table>
<h4>Forming the Template</h4>
<p>As noted below the template, it is formed according to the <a href=#Data>Data Driven Templates recipe</a> using the <i>one-of rule</i>, the <i>atomic distinct rule</i> and the <i>atomic non-distinct rule</i> in order.</p>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>As always, itemizations should have enough data examples to clearly illustrate how the type represents information.</p>
<p>Functions operating on itemizations should have at least as many tests as there are cases in the itemizations. If there are intervals in the itemization, then there should be tests at all points of variance in the interval. In the case of adjoining intervals it is critical to test the boundaries.</p>
<h4>Itemization of Intervals</h4>
<p>A common case is for the itemization to be comprised of 2 or more intervals. In this case functions operating on the data definition will usually need to be tested at all the boundaries of closed intervals and points between the boundaries.</p>
<table title="Reading Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>;;; Reading is one of:
;; - Number[> 30]
;; - Number(5, 30]
;; - Number[0, 5]
;; interp. distance in centimeters from bumper to obstacle
;; Number[> 30] is considered "safe"
;; Number(5, 30] is considered "warning"
;; Number[0, 5] is considered "dangerous"
(define R1 40)
(define R2 .9)
(define (fn-for-reading r)
(cond [(< 30 r) (... r)]
[(and (< 5 r) (<= r 30)) (... r)]
[(<= 0 r 5) (... r)]))
;; Template rules used:
;; one-of: 3 cases
;; atomic non-distinct: Number[>30]
;; atomic non-distinct: Number(5, 30]
;; atomic non-distinct: Number[0, 5]
</pre>
</td>
</tr>
</tbody>
</table>
<p>As noted below the template, it is formed according to the <a href=#Data>Data Driven Templates recipe</a> using the <i>one-of rule</i>, followed by 3 uses of the <i>atomic non-distinct rule</i>.</p>
<p><br> <a id=Compound></a></p>
<h3>Compound data (structures)</h3>
<p>Use structures when two or more values naturally belong together. The define-struct goes at the beginning of the data definition, before the types comment.</p>
<table title="Ball Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>(define-struct ball (x y))
;; Ball is (make-ball Number Number)
;; interp. a ball at position x, y
(define BALL-1 (make-ball 6 10))
#;
(define (fn-for-ball b)
(... (ball-x b) ;Number
(ball-y b))) ;Number
;; Template rules used:
;; - compound: 2 fields
</pre>
</td>
</tr>
</tbody>
</table>
<p>The template above is formed according to the <a href=#Data>Data Driven Templates recipe</a> using the compound rule. Then for each of the selectors, the result type of the selector (Number in the case of ball-x and ball-y) is used to decide whether the selector call itself should be wrapped in another expression. In this case, where the result types are primitive, no additional wrapping occurs. C.f. cases below when the reference rule applies.</p>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>For compound data definitions it is often useful to have numerous examples, for example to illustrate special cases. For a snake in a snake game you might have an example where the snake is very short, very long, hitting the edge of a box, touching food etc. These data examples can also be useful for writing function tests because they save space in each <tt>check-expect</tt>.</p>
<p><br> <a id=Reference></a></p>
<h3>References to other data definitions</h3>
<p>Some data definitions contain references to other data definitions you have defined (non-primitive data definitions). One common case is for a compound data definition to comprise other named data definitions. (Or, once lists are introduced, for a list to contain elements that are described by another data definition. In these cases the template of the first data definition should contain calls to the second data definition's template function wherever the second data appears. For example:</p>
<table title="Game Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>---assume Ball is as defined above---
(define-struct game (ball score))
;; Game is (make-game Ball Number)
;; interp. the current ball and score of the game
(define GAME-1 (make-game (make-ball 1 5) 2))
#;
(define (fn-for-game g)
(... (fn-for-ball (game-ball g))
(game-score g))) ;Number
;; Template rules used:
;; - compound: 2 fields
;; - reference: ball field is Ball
</pre>
</td>
</tr>
</tbody>
</table>
<p>In this case the template is formed according to the <a href=#Data>Data Driven Templates recipe</a> by first using the <i>compound rule</i>. Then, since the result type of <tt>(game-ball g)</tt> is Ball, the <i>reference rule</i> is used to wrap the selector so that it becomes <tt>(fn-for-ball (game-ball g))</tt>. The call to <tt>game-score</tt> is not wrapped because it produces a primitive type.</p>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>For data definitions involving references to non-primitive types the data examples can sometimes become quite long. In these cases it can be helpful to define well-named constants for data examples for the referred to type and then use those constants in the referring from type. For example:</p>
<pre>...in the data definition for Drop...
(define DTOP (make-drop 10 0)) ;top of screen
(define DMID (make-drop 20 (/ HEIGHT 2))) ;middle of screen
(define DBOT (make-drop 30 HEIGHT)) ;at bottom edge
(define DOUT (make-drop 40 (+ HEIGHT 1))) ;past bottom edge
...in the data definition for ListOfDrop...
(define LOD1 empty)
(define LOD-ALL-ON (cons DTOP (cons DMID )))
(define LOD-ONE-ABOUT-TO-LEAVE (cons DTOP (cons DMID (cons DBOT empty))))
(define LOD-ONE-OUT-ALREADY (cons DTOP (cons DMID (cons DBOT (cons DOUT empty)))))
</pre>
<p>In the case of references to non-primitive types the function operating on the referring type (i.e. <tt>ListOfDrop</tt>) will end up with a call to a helper that operates on the referred to type (i.e. <tt>Drop</tt>). Tests on the helper function should fully test that function, tests on the calling function may assume the helper function works properly.</p>
<p><br> <a id=Self-Reference></a></p>
<h3>Self-referential or mutually referential</h3>
<p>When the <strong>information in the program's domain is of arbitrary size</strong>, a well-formed self-referential (or mutually referential) data definition is needed.</p>
<p>In order to be well-formed, a self-referential data definition must:</p>
<ul>
<li>(i) have at least one case without self reference (the base case(s))</li>
<li>(ii) have at least one case with self reference</li>
</ul>
<p>The template contains a base case corresponding to the non-self-referential clause(s) as well as one or more natural recursions corresponding to the self-referential clauses.</p>
<table title="ListOfString Data Definition" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>;; ListOfString is one of:
;; - empty
;; - (cons String ListOfString)
;; interp. a list of strings
(define LOS-1 empty)
(define LOS-2 (cons "a" empty))
(define LOS-3 (cons "b" (cons "c" empty)))
#;
(define (fn-for-los los)
(cond [(empty? los) (...)] ;BASE CASE
[else (... (first los) ;String
(fn-for-los (rest los)))])) ;NATURAL RECURSION
;; /
;; /
;; COMBINATION
;; Template rules used:
;; - one of: 2 cases
;; - atomic distinct: empty
;; - compound: (cons String ListOfString)
;; - self-reference: (rest los) is ListOfString
</pre>
</td>
</tr>
</tbody>
</table>
<p>In some cases a types comment can have both self-reference and reference to another type.</p>
<table title="Dot and ListOfDot Data Definitions" style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>(define-struct dot (x y))
;; Dot is (make-dot Integer Integer)
;; interp. A dot on the screen, w/ x and y coordinates.
(define D1 (make-dot 10 30))
#;
(define (fn-for-dot d)
(... (dot-x d) ;Integer
(dot-y d))) ;Integer
;; Template rules used:
;; - compound: 2 fields
;; ListOfDot is one of:
;; - empty
;; - (cons Dot ListOfDot)
;; interp. a list of Dot
(define LOD1 empty)
(define LOD2 (cons (make-dot 10 20) (cons (make-dot 3 6) empty)))
#;
(define (fn-for-lod lod)
(cond [(empty? lod) (...)]
[else
(... (fn-for-dot (first lod))
(fn-for-lod (rest lod)))]))
;; Template rules used:
;; - one of: 2 cases
;; - atomic distinct: empty
;; - compound: (cons Dot ListOfDot)
;; - reference: (first lod) is Dot
;; - self-reference: (rest lod) is ListOfDot
</pre>
</td>
</tr>
</tbody>
</table>
<h4>Guidance on Data Examples and Function Example/Tests</h4>
<p>When writing data and function examples for self-referential data definitions always put the base case first. Its usually trivial for data examples, but many function tests don't work properly if the base case isn't working properly, so testing that first can help avoid being confused by a failure in a non base case test. Also be sure to have a test for a list (or other structure) that is at least 2 long.</p>
<p> <a name=HtDW></a></p>
<hr><hr><hr>
<div align=right><a href=#top>Back to Design Recipes Table</a></div>
<h2>How To Design Worlds (HtDW)</h2>
<p>The How to Design Worlds process provides guidance for designing interactive world programs using <tt>big-bang</tt>. While some elements of the process are tailored to <tt>big-bang</tt>, the process can also be adapted to the design of other interactive programs. The wish-list technique can be used in any multi-function program.</p>
<table summary="Steps of the HtDW Recipe." style="border:1px solid #000000;padding:5px;text-align:left;horizontal-align:left;vertical-align:top;max-width:90%" cellpadding=5 border=1>
<tbody>
<tr style=text-align:left>
<td>
<p>World program design is divided into two phases, each of which has sub-parts:</p>
<ol>
<li>Domain analysis (use a piece of paper!)</li>
<ol>
<li>Sketch <emph>program scenarios</emph></li>
<li>Identify <emph>constant information</emph></li>
<li>Identify <emph>changing information</emph></li>
<li>Identify <emph>big-bang options</emph></li>
</ol>
<li>Build the actual program</li>
<ol>
<li>Constants (based on 1.2 above)</li>
<li>Data definitions using <a href=#HtDD>HtDD</a> (based on 1.3 above)</li>
<li>Functions using <a href=#HtDF>HtDF</a></li>
<ol>
<li>main first (based on 1.3, 1.4 and 2.2 above)</li>
<li><emph>wish list entries</emph>for <emph>big-bang handlers</emph></li>
</ol>
<li>Work through wish list until done</li>
</ol></ol></td>
</tr>
</tbody>
</table>
<h4>Phase 1: Domain Analysis</h4>
<p>Do a domain analysis by hand-drawing three or more pictures of what the world program will look like at different stages when it is running.</p>
<p>Use this picture to identify constant information such as the height and width of screen, color of the background, the background image itself, the length of a firework's fuse, the image for a moving cat and so on.</p>
<p>Also identify changing information such as the position of a firework, the color of a light, the number in countdown etc.</p>
<p>Identify which <tt>big-bang</tt> options the program needs.</p>
<table summary="big-bang options." style="border:1px solid #000000;padding:5px;text-align:left;horizontal-align:left;vertical-align:top;max-width:90%" cellpadding=5 border=1>
<thead>
<tr><th>If your program needs to:<th>Then it needs this option:</tr>
</thead>
<tbody>
<tr>
<td>change as time goes by (nearly all do)</td>
<td><tt>on-tick</tt></td>
</tr>
<tr>
<td>display something (nearly all do)</td>
<td><tt>to-draw</tt></td>
</tr>
<tr>
<td>change in response to key presses</td>
<td><tt>on-key</tt></td>
</tr>
<tr>
<td>change in response to mouse activity</td>
<td><tt>on-mouse</tt></td>
</tr>
<tr>
<td>stop automatically</td>
<td><tt>stop-when</tt></td>
</tr>
</tbody>
</table>
<p>(There are several more options to <tt>big-bang</tt>. Look in the DrRacket help desk under <tt>big-bang</tt> for a complete list.)</p>
<h4>Phase 2: Building the actual program</h4>
<p>Structure the actual program in four parts:</p>
<ol>
<li>Requires followed by one line summary of program's behavior</li>
<li>Constants</li>
<li>Data definitions</li>
<li>Functions</li>
</ol>
<p>The program should begin with whatever require declarations are required. For a program using <tt>big-bang</tt> this is usually a require for <tt>2htdp/universe</tt> to get <tt>big-bang</tt> itself and a require for <tt>2htdp/image</tt> to get useful image primitives. This is followed by a <span style=text-decoration:underline>short</span> summary of the program's behavior (ideally 1 line).</p>
<p>The next section of the file should define constants. These will typically come directly from the domain analysis.</p>
<p>This is followed by data definitions. The data definitions describe how the world state - the changing information identified during the analysis - will be represented as data in the program. Simple world programs may have just a single data definition. More complex world programs have a number of data definitions.</p>
<p>The functions section should begin with the <tt>main</tt> function which uses <tt>big-bang</tt> with the appropriate options identified during the analysis. After that put the more important functions first followed by the less important helpers. Keep groups of closely related functions together.</p>
<h5>Template for a World Program</h5>
<p>A useful template for a world program, including a template for the main function and wish list entries for tick-handler and to-draw handler is as follows. To use this template replace WS with the appropriate type for your changing world state. You may want to give the handler functions more descriptive names and you should definitely give them all a more descriptive purpose.</p>
<pre>(require 2htdp/image)
(require 2htdp/universe)
;; My world program (make this more specific)
;; =================
;; Constants:
;; =================
;; Data definitions:
;; WS is ... (give WS a better name)
;; =================
;; Functions:
;; WS -> WS
;; start the world with ...
;; <no tests for main functions>
(define (main ws)
(big-bang ws ; WS
(on-tick tock) ; WS -> WS
(to-draw render) ; WS -> Image
(stop-when ...) ; WS -> Boolean
(on-mouse ...) ; WS Integer Integer MouseEvent -> WS
(on-key ...))) ; WS KeyEvent -> WS
;; WS -> WS
;; produce the next ...
;; !!!
(define (tock ws) ...)
;; WS -> Image
;; render ...
;; !!!
(define (render ws) ...)
</no></pre>
<p>Depending on which other big-bang options you are using you would also end up with wish list entries for those handlers. So, at an early stage a world program might look like this:</p>
<pre>(require 2htdp/universe)
(require 2htdp/image)
;; A cat that walks across the screen.
;; Constants:
(define WIDTH 200)
(define HEIGHT 200)
(define CAT-IMG (circle 10 "solid" "red")) ; a not very attractive cat
(define MTS (empty-scene WIDTH HEIGHT))
;; Data definitions:
;; Cat is Number
;; interp. x coordinate of cat (in screen coordinates)
(define C1 1)
(define C2 30)
#;
(define (fn-for-cat c)
(... c))
;; Functions:
;; Cat -> Cat
;; start the world with initial state c, for example: (main 0)
(define (main c)
(big-bang c ; Cat
(on-tick tock) ; Cat -> Cat
(to-draw render))) ; Cat -> Image
;; Cat -> Cat
;; Produce cat at next position
;!!!
(define (tock c) 1) ;stub
;; Cat -> Image
;; produce image with CAT-IMG placed on MTS at proper x, y position
; !!!
(define (render c) MTS)
</pre>
<p>Note that we are maintaining a <emph>wish list</emph>of functions that need to be designed. The way to maintain the wish list is to just write a signature, purpose and stub for each wished-for function, also label the <emph>wish list entry</emph>with <tt>!!!</tt> or some other marker that is easy to search for. That will help you find your unfilled wishes later.</p>
<p>Forming wish list entries this way is enough for <tt>main</tt> (or other functions that call a wished for function) to be defined without error. But of course <tt>main</tt> (and other such functions) will not run properly until the wished for functions are actually completely designed.</p>
<p>As you design the program remember to run early and run often. The sooner you can run the program after writing anything the sooner you can find any small mistakes that might be in it. Fixing the small mistakes earlier makes it easier to find any harder mistakes later.</p>
<h5>Key and Mouse Handlers</h5>
<p>The <tt>on-key</tt> and <tt>on-mouse</tt> handler function templates are handled specially. The <tt>on-key</tt> function is templated according to its second argument, a <tt>KeyEvent</tt>, using the large enumeration rule. The <tt>on-mouse</tt> function is templated according to its <tt>MouseEvent</tt> argument, also using the large enumeration rule. So, for example, for a key handler function that has a special behaviour when the space key is pressed but does nothing for any other key event the following would be the template:</p>
<pre>(define (handle-key ws ke)
(cond [(key=? ke " ") (... ws)]
[else
(... ws)]))
</pre>
<p>Similarly the template for a mouse handler function that has special behavior for mouse clicks but ignores all other mouse events would be:</p>
<pre>(define (handle-mouse ws x y me)
(cond [(mouse=? me "button-down") (... ws x y)]
[else
(... ws x y)]))
</pre>
<p>For more information on the <tt>KeyEvent</tt> and <tt>MouseEvent</tt> large enumerations see the DrRacket help desk.</p>
<p><a name=Data></a></p>
<hr><hr><hr>
<div align=right><a href=#top>Back to Design Recipes Table</a></div>
<h2>Data Driven Templates</h2>
<p>Templates are the core structure that we know a function must have, independent of the details of its definition. In many cases the template for a function is determined by the type of data the function consumes. We refer to these as data driven templates. The recipe below can be used to produce a data driven template for any type comment.</p>
<p>For a given type <tt>TypeName</tt> the data driven template is:</p>
<table style="border:0px solid #000000;padding:10px" cellpadding=10 border=none>
<tbody>
<tr>
<td>
<pre>(define (fn-for-type-name x)
<body>)
</pre>
</td>
</tr>
</tbody>
</table>
<p>Where <tt>x</tt> is an appropriately chosen parameter name (often the initials of the type name) and the body is determined according to the table below. To use the table, start with the type of the parameter, i.e. TypeName, and select the row of the table that matches that type. The first row matches only primitive types, the later rows match parts of type comments.</p>
<p>(Note that when designing functions that consume additional atomic parameters, the name of that parameter gets added after every <tt>...</tt> in the template. Templates for functions with additional complex parameters are covered in <a href=#2One-of>Functions on 2 One-Of Data</a>.)</p>
<table style=text-align:left;vertical-align:text-top cellpadding=5 border=1>
<thead>
<tr><th style=width:30%>Type of data<th style=width:25%><tt>cond</tt> question (if applicable)<th style=width:45%>Body or <tt>cond</tt> answer (if applicable)</tr>
</thead>
<tbody>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>Atomic Non-Distinct</strong></p>
<ul>
<li><tt>Number</tt></li>
<li><tt>String</tt></li>
<li><tt>Boolean</tt></li>
<li><tt>Image</tt></li>
<li>interval like <tt>Number[0, 10)</tt></li>
<li>etc.</li>
</ul>
</td>
<td>
<p>Appropriate predicate</p>
<ul>
<li><tt>(number? x)</tt></li>
<li><tt>(string? x)</tt></li>
<li><tt>(boolean? x)</tt></li>
<li><tt>(image? x)</tt></li>
<li><tt>(and (<= 0 x)</tt><br> <tt> (< x 10))</tt></li>
<li>etc.</li>
</ul>
</td>
<td>
<p>Expression that operates on the parameter.</p>
<p><tt>(... x)</tt></p>
</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>Atomic Distinct Value</strong></p>
<ul>
<li><tt>"red"</tt></li>
<li><tt>false</tt></li>
<li><tt>empty</tt></li>
<li>etc.</li>
</ul>
</td>
<td>
<p>Appropriate predicate</p>
<ul>
<li><tt>(string=? x "red")</tt></li>
<li><tt>(false? x)</tt></li>
<li><tt>(empty? x)</tt></li>
<li>etc.</li>
</ul>
</td>
<td>
<p>Since value is distinct, parameter does not appear.</p>
<p><tt>(...)</tt></p>
</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>One Of</strong></p>
<ul>
<li>enumerations</li>
<li>itemizations</li>
</ul>
</td>
<td></td>
<td>
<p>Cond with one clause per subclass of one of.</p>
<p><tt>(cond [<question1> <answer1>]<br></tt> <tt> [<question2> <answer2>])</tt></p>
<p>Where each question and answer expression is formed by following the rule in the question or answer column of this table for the corresponding case. A detailed derivation of a template for a one-of type appears below.</p>
<p>It is permissible to use <tt>else</tt> for the last question for itemizations and large enumerations. Normal enumerations should not use else.</p>
<p>Note that in a <i>mixed data itemization</i>, such as</p>
<p><tt>;; Measurement is one of:</tt><br> <tt>;; - Number[-10, 0)</tt><br> <tt>;; - true</tt><br> <tt>;; - Number(0, 10]</tt></p>
<p>the cond questions must be <strong>guarded</strong> with an appropriate type predicate. In particular, the first cond question for <tt>Measurement</tt> must be</p>
<p><tt>(and (number? m)</tt><br> <tt> (<= -10 m)</tt><br> <tt> (< m 0))</tt></p>
<p>where the call to <tt>number?</tt> guards the calls to <tt><=</tt> and <tt><</tt>. This will protect <tt><=</tt> and <tt><</tt> from ever receiving true as an argument.</p>
</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>Compound</strong></p>
<ul>
<li><tt>Position</tt></li>
<li><tt>Firework</tt></li>
<li><tt>Ball</tt></li>
<li>cons</li>
<li>etc.</li>
</ul>
</td>
<td>
<p>Predicate from structure</p>
<ul>
<li><tt>(posn? x)</tt></li>
<li><tt>(firework? x)</tt></li>
<li><tt>(ball? x)</tt></li>
<li><tt>(cons? x)</tt> (often just else)</li>
<li>etc.</li>
</ul>
</td>
<td>
<p>All selectors.</p>
<ul>
<li><tt>(... (posn-x x) (posn-y x))</tt></li>
<li><tt>(... (firework-y x) (firework-color x))</tt></li>
<li><tt>(... (ball-x x) (ball-dx x))</tt></li>
<li><tt>(... (first x) (rest x))</tt></li>
<li>etc.</li>
</ul>
<br>
<p>Then consider the result type of each selector call and wrap the accessor expression appropriately using the table with that type. So for example, if after adding all the selectors you have:</p>
<p><tt>(... (game-ball g) ;produces Ball</tt><br> <tt> (game-paddle g)) ;produces Paddle</tt></p>
<p>Then, because both Ball and Paddle are non-primitive types (types that you yourself defined in a data definition) the reference rule (immediately below) says that you should add calls to those types' template functions as follows:</p>
<p><tt> (... (fn-for-ball (game-ball g))</tt><br> <tt> (fn-for-paddle (game-paddle g)))</tt></p>
</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>Other Non-Primitive Type Reference</strong></p>
</td>
<td>
<p>Predicate, usually from structure definition</p>
<ul>
<li><tt>(firework? x)</tt></li>
<li><tt>(person? x)</tt></li>
</ul>
</td>
<td>
<p>Call to other type's template function</p>
<ul>
<li><tt>(fn-for-firework x)</tt></li>
<li><tt>(fn-for-person x)</tt></li>
</ul>
</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>Self Reference</strong></p>
</td>
<td></td>
<td>
<p>Form natural recursion with call to this type's template function:</p>
<ul>
<li><tt>(fn-for-los (rest los))</tt></li>
</ul>
</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<p><strong>Mutual Reference</strong></p>
<br>
<p>Note: form and group all templates in mutual reference cycle together.</p>
</td>
<td></td>
<td>
<p>Call to other type's template function:</p>
<p><tt>(fn-for-lod (dir-subdirs d)</tt><br> <tt>(fn-for-dir (first lod))</tt></p>
</td>
</tr>
</tbody>
</table>
<p></p>
<h4>Producing the Template for an Example One Of Type</h4>
<p>In many cases more than one of the above rules will apply to a single template. Consider this type comment:</p>
<p><tt>;; Clock is one of:</tt><br> <tt>;; - Natural</tt><br> <tt>;; - false</tt></p>
<p>and the step-by-step construction of the template for a function operating on <tt>Clock</tt>.</p>
<table title="Clock Template Derivation" style="border:0px solid #000000;padding:10px;text-align:left;vertical-align:top" cellpadding=10 border=none>
<tbody>
<tr style=text-align:left;vertical-align:text-top>
<td>
<pre>(define (fn-for-clock c)
(cond [Q A]
[Q A]))
;; Template rules used:
;; - one of: 2 cases
</pre>
</td>
<td style=width:65%><tt>Clock</tt> is a one of type with two subclasses (one of which is not distinct making it an itemization). The one of rule tells us to use a <tt>cond</tt>. The <tt>cond</tt> needs one clause for each subclass of the itemization.</td>
</tr>
<tr style=text-align:left;vertical-align:text-top>
<td>
<pre>(define (fn-for-clock c)
(cond [(number? c) (... c)]
[Q A]))
;; Template rules used:
;; - one of: 2 cases
;; - atomic non-distinct: Natural
</pre>
</td>
<td>
<p>The <tt>cond</tt> questions need to identify each subclass of data. The <tt>cond</tt> answers need to follow templating rules for that subclasses data. In the first subclass, <tt>Natural</tt> is a non-distinct type; the <i>atomic non-distinct rule</i> tells us the question and answer as shown to the left.</p>