Debian Codegraph @

tutorial/examples_generics.html

raw symbols SCIP
1
2 <HTML>
3 <!--
4 ***********************************************************************
5 FUNNELWEB MANUAL WEB PAGE
6 =========================
7 Copyright (c) Ross N. Williams 1992,1999. All rights reserved.
8
9 Permission is granted to redistribute and use this manual in
10 any medium, with or without modification, provided that all
11 notices (including, without limitation, the copyright
12 notice, this permission notice, any record of modification,
13 and all legal notices) are preserved on all copies, that all
14 modifications are clearly marked, and that modified versions
15 are not represented as the original version unless all the
16 modifications since the manual's original release by Ross N.
17 Williams (www.ross.net) consist of translations or other
18 transformations that alter only the manual's form, not its
19 content. THIS MANUAL IS PROVIDED "AS IS" AND WITHOUT ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
21 LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
22 FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT PERMITTED BY
23 LAW THERE IS ABSOLUTELY NO WARRANTY.
24
25 ***********************************************************************
26 -->
27
28 <HEAD>
29 <TITLE>6.7 Generics</TITLE>
30 <STYLE TYPE="text/css"> <!-- A {text-decoration: none} // --> </STYLE>
31 </HEAD>
32 <BODY BACKGROUND="binary/background.gif"
33 BGCOLOR="#FFFFFF"
34 TEXT="#000000"
35 VLINK="#660000"
36 LINK="#FF0000"
37 ALINK="#CC0000">
38
39 <TABLE WIDTH="490">
40 <TR>
41 <TD WIDTH="130" VALIGN="top">
42 <IMG SRC="binary/d_clear.gif" ALT="" WIDTH="130" HEIGHT="1"><BR>
43
44 <FONT SIZE="2">
45 <BR>
46
47 <A HREF="http://www.ross.net/"
48 TARGET="rosshome"
49 onClick="window.open('','rosshome','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
50 >
51 <IMG SRC="binary/rossnet_logo.gif"
52 WIDTH="64" HEIGHT="32"
53 BORDER="0" ALT="RossNet"
54 HSPACE="0" VSPACE="1"></A><BR>
55 <BR>
56
57 <A HREF="../index.shtml"
58 TARGET="funnelweb"
59 onClick="window.open('','funnelweb','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
60 >
61 <IMG SRC="binary/linklogo.gif"
62 WIDTH="64" HEIGHT="32"
63 BORDER="0" ALT="FunnelWeb"
64 HSPACE="0" VSPACE="1"></A><BR>
65 <BR>
66 <TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0><TR><TD BGCOLOR="#000000">
67 <A HREF="../reference/index.html"
68 TARGET="funnelwebreference"
69 onClick="window.open('','funnelwebreference','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
70 ><FONT COLOR="#FFFFFF"><B>Reference</B></FONT></A><BR>
71 <BR>
72 <A HREF="../developer/index.html"
73 TARGET="funnelwebdeveloper"
74 onClick="window.open('','funnelwebdeveloper','location,status,menubar,scrollbars,resizable',false).focus(); return true;"
75 ><FONT COLOR="#FFFFFF"><B>Developer</B></FONT></A><BR>
76 <BR>
77 <A HREF="index.html"><FONT COLOR="#FFFFFF"><B>Tutorial</B></FONT></A><BR>
78 <A HREF="intro.html"><FONT COLOR="#FFFFFF">1 Introduction</FONT></A><BR>
79 <A HREF="macro.html"><FONT COLOR="#FFFFFF">2 Macros</FONT></A><BR>
80 <A HREF="type.html"><FONT COLOR="#FFFFFF">3 Typesetting</FONT></A><BR>
81 <A HREF="example.html"><FONT COLOR="#FFFFFF">4 Example</FONT></A><BR>
82 <A HREF="hints.html"><FONT COLOR="#FFFFFF">5 Hints</FONT></A><BR>
83 <A HREF="examples.html"><FONT COLOR="#FFFFFF">6 Examples</FONT></A><BR>
84 <A HREF="web.html"><FONT COLOR="#FFFFFF">7 Webmaking</FONT></A><BR>
85
86 <BR>
87 <A HREF="search.html"><FONT COLOR="#FFFFFF"><B>SEARCH</B></FONT></A><BR>
88 </FONT>
89 </TD></TR></TABLE>
90
91
92 </TD>
93 <TD WIDTH="360" VALIGN="top">
94 <FONT SIZE="3">
95
96
97 <A HREF="../reference/index.html"><IMG SRC="binary/title.gif"
98 WIDTH="302" HEIGHT="24"
99 BORDER="0" ALT="FunnelWeb Tutorial Manual"
100 HSPACE="0" VSPACE="0"></A>
101 <P><FONT SIZE="5">6.7 Generics</FONT><BR>
102
103
104
105 <P>It is well known that generics in programming languages
106 are closely aligned with textual substitution. In fact, a
107 good way to understand the generic facility of a new
108 programming language is to ask oneself the question
109 "In what way does this generic facility differ from
110 simple text substitution?" The differences, if any,
111 typically have to do with the difference in scoping between
112 textual and intelligent substitution and whether the generic
113 code is shared or copied by the implementation. In most
114 cases the differences are quite minor.
115
116 <P>Because generic facilities are so closely aligned with
117 text substitution, it is possible to use FunnelWeb's
118 parameterized macros to provide generics in programming
119 languages that do not support generics. Simply write a
120 FunnelWeb macro whose parameters are the parameters of the
121 generic and whose body is the generic object.
122
123 <P>The following FunnelWeb file gives an example of a
124 fully worked Vax Pascal generic set package
125 implemented using FunnelWeb parameterized macros. The
126 package was written by Barry Dwyer of
127 the Computer Science Department of the University of
128 Adelaide in 1987 and was
129 emailed to me on 11&nbsp;November 1987. The generic package
130 provides a set abstraction
131 implemented using linked lists. Note the clever use of the
132 instantiation parameters in type, function, and procedure
133 names.
134
135 <P>
136 <PRE>
137 @$@&lt;Generic Set Module@&gt;@(@2@)==@{@-
138 @! @1 is the base type, @2 is the set type.
139 [inherit ('@1'), environment ('@2')]
140
141 module @2;
142
143 type @2 = ^@2Record;
144 @2Record = record
145 Member: @1;
146 Next: @2;
147 end;
148
149 procedure Null@2 (var Result: @2);
150 begin new (Result);
151 Result^.Member := (- MaxInt)::@1;
152 Result^.Next := nil end;
153
154 function IsNull@2 (S: @2): boolean;
155 begin
156 IsNull@2 := S^.Member::integer = - MaxInt
157 end;
158
159 procedure ForEach@1 (S: @2; procedure DoIt (i: @1));
160 var ThisS, NextS: @2;
161 begin ThisS := S;
162 while ThisS^.Member::integer &lt;&gt; - MaxInt do
163 begin NextS := ThisS^.Next;
164 DoIt (ThisS^.Member);
165 ThisS := NextS end;
166 end;
167
168 function First@1 (S: @2): @1;
169 begin First@1 := S^.Member end;
170
171 function Is@1InSet (i: @1; S: @2): boolean;
172 procedure TestEquals (j: @1);
173 begin if Equal@1 (i, j) then Is@1InSet := true; end;
174 begin
175 Is@1InSet := false;
176 ForEach@1 (S, TestEquals);
177 end;
178
179 function Includes@2 (S1, S2: @2): boolean;
180 var Result: boolean;
181 procedure TestIfInS1 (i: @1);
182 begin
183 if Result then
184 if not Is@1InSet (i, S1) then
185 Result := false;
186 end;
187 begin Result := true;
188 ForEach@1 (S2, TestIfInS1);
189 Includes@2 := Result end;
190
191 function Disjoint@2s (S1, S2: @2): boolean;
192 var Result: boolean;
193 procedure TestIfInS1 (i: @1);
194 begin
195 if Result then
196 if Is@1InSet (i, S1) then
197 Result := false;
198 end;
199 begin Result := true;
200 ForEach@1 (S2, TestIfInS1);
201 Disjoint@2s := Result end;
202
203 function Equal@2 (S1, S2: @2): boolean;
204 begin
205 Equal@2 := Includes@2 (S1, S2) and
206 Includes@2 (S2, S1);
207 end;
208
209 procedure Insert@1 (i: @1; var S: @2);
210 var This, Pred, Succ: @2;
211 begin
212 if not Is@1InSet (i, S) then
213 begin
214 Pred := nil; Succ := S;
215 while Succ^.Member::integer &gt; i::integer do
216 begin Pred := Succ; Succ := Succ^.Next end;
217 if Succ^.Member::integer &lt; i::integer then
218 begin
219 new (This);
220 This^.Next := Succ;
221 This^.Member := i;
222 if Pred &lt;&gt; nil then
223 Pred^.Next := This
224 else
225 S := This;
226 end;
227 end;
228 end;
229
230 procedure Insert@1s (S1: @2; var S2: @2);
231 var This, Pred, Succ: @2;
232 procedure Add@1 (i: @1);
233 begin Insert@1 (i, S2) end;
234 begin
235 ForEach@1 (S1, Add@1);
236 end;
237
238 procedure Remove@1 (i: @1; var S: @2);
239 var Pred, This: @2;
240 begin
241 Pred := nil; This := S;
242 while not Equal@1 (This^.Member, i) do begin
243 Pred := This; This := This^.Next end;
244 if Pred &lt;&gt; nil then
245 Pred^.Next := This^.Next
246 else
247 S := This^.Next;
248 Dispose (This);
249 end;
250
251 procedure Dispose@2 (var S: @2);
252 var Old: @2;
253 begin
254 while S &lt;&gt; nil do
255 begin
256 Old := S;
257 S := S^.Next;
258 Dispose (Old)
259 end;
260 end;
261
262 end.
263 @}
264
265 @O@&lt;NaryTreeSet.pas@&gt;==@{@-
266 @&lt;Generic Set Module@&gt;@-
267 @(@"NaryTree@"@,@"NaryTreeSet@"@)@}
268 @O@&lt;NaryTreeSetSet.pas@&gt;==@{@-
269 @&lt;Generic Set Module@&gt;@-
270 @(@"NaryTreeSet@"@,@"NaryTreeSetSet@"@)@}
271 </PRE>
272
273 <P>A great advantage of the approach reflected in the
274 above example is that it allows the programmer to construct
275 a generic object in a language that does not supply
276 generics, <I>with complete
277 typesafety.</I>&nbsp; This contrasts to
278 the approach that might be used in a language such as C
279 where the programmer might choose to construct a
280 "generic" package by parameterizing a package with
281 pointers to <SAMP>void</SAMP>. The resulting package is
282 powerful but extremely untypesafe. Such a generic list
283 package is used in the code of FunnelWeb itself and caused
284 no end of problems, as the compiler had no way of telling if
285 pointers to the correctly typed object were being handed to
286 the correct list-object/function combination.
287
288 <P>The major disadvantage of the text generic approach is
289 that it causes the code of the generic object to be
290 duplicated once for each instantiation. Depending on the
291 number and size of the instantiations, this may or may not
292 be acceptable.
293
294 <P>Where the duplication of code is unacceptable, a hybrid
295 approach may be taken. As in the C&nbsp;example, the programmer
296 could write a single generic package using pointers to
297 <SAMP>void</SAMP> or some other untypesafe mechanism. Then the
298 programmer creates a FunnelWeb generic package whose
299 functions do nothing more than call the functions of the
300 untypesafe package, and whose types do nothing more than
301 contain the types of the untypesafe package. This solution
302 involves the use of untypesafe programming, but this is a
303 one-off and if done carefully and correctly, the result can
304 be a typesafe generic package involving minimal code
305 duplication.
306
307
308 <P>
309 <TABLE WIDTH="100%">
310 <TR>
311 <TD ALIGN="left" VALIGN="bottom"><A HREF="examples_sharing.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD>
312 <TD ALIGN="center" VALIGN="bottom"><A HREF="examples.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD>
313 <TD ALIGN="right" VALIGN="bottom"><A HREF="examples.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD>
314 </TR>
315 </TABLE>
316
317
318
319 <HR>
320 <FONT SIZE="2">
321 <A HREF="mailto:[email protected]">Webmaster</A>&nbsp;&nbsp;&nbsp;
322 <A HREF="copyright.html">Copyright &copy; Ross N. Williams 1992,1999. All rights reserved.</A><BR>
323 </FONT>
324
325 </FONT>
326 </TD>
327 </TR>
328 </TABLE>
329
330
331 </BODY>
332
333 <!-- *********************************************************************** -->
334 <!-- End Of A FunnelWeb Manual Web Page (www.ross.net/funnelweb/) -->
335 <!-- *********************************************************************** -->
336
337 </HTML>