Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / HTML / MicroMan / Prefixin.htm
1 <!doctype html public "-//IETF//DTD HTML//EN">\r
2 <HTML>\r
3 \r
4 <HEAD>\r
5 \r
6 <TITLE>Prefixing</TITLE>\r
7 \r
8 <META NAME="GENERATOR" CONTENT="Internet Assistant for Word 1.0Z">\r
9 <META NAME="AUTHOR" CONTENT="NOM">\r
10 </HEAD>\r
11 \r
12 <BODY>\r
13 \r
14 <P>\r
15 <U><I>Loglan 82, A micro-manual of the programming language -\r
16 Basic constructs and facilities</I></U>\r
17 <H1><IMG ALIGN=MIDDLE SRC="gifs/logo2.gif"> 8) Prefixing</H1>\r
18 <HR>\r
19 \r
20 <P>\r
21 Classes and prefixing are ingenius inventions of Simula-67(cf\r
22 [1]). Unfortunately they were hardly ever known and, perhaps,\r
23 by this have not been introduced into many programming language\r
24 that gained certain popularity. Moreover, implementation constraints\r
25 of Simula-67 bind prefixing and classes workableness to such a\r
26 degree that both facilities cannot be used in all respects. We\r
27 hope that LOGLAN-82, adopting merits and rooting up deficiencies\r
28 of these constructs, will smooth their variations and vivify theirs\r
29 usefulness.\r
30 <P>\r
31 What is prefixing ? First of all it is a method for unit extending.\r
32 Consider the simplest example:\r
33 <P>\r
34 <IMG SRC="gifs/pr01.gif"> \r
35 <P>\r
36 Assume the user desires to extend this class with new attributes.\r
37 Instead of writing a completely new class, he may enlarge the\r
38 existing one:\r
39 <P>\r
40 <IMG SRC="gifs/pr02.gif"> \r
41 <P>\r
42 Class gas_bill is prefixed by class bill. This new declaration\r
43 may appear anywhere within the scope of declaration of class bill.\r
44 (In Simula-67 such a prefixing is forbidden in nested units.)\r
45 Class gas_bill has all the attributes of class bill and additionally\r
46 its own attributes (in this case the only one: cube_meters). The\r
47 generation statement of this class has the form:\r
48 <P>\r
49 <IMG SRC="gifs/pr03.gif"> \r
50 <P>\r
51 where z is a reference variable of type gas_bill. Remote access\r
52 to the attributes of prefixed class is standard:\r
53 <P>\r
54 <IMG SRC="gifs/pr04.gif"> \r
55 <P>\r
56 Consider now the example of a class with parameters.\r
57 <P>\r
58 Assume that in a program a class:\r
59 <P>\r
60 <IMG SRC="gifs/pr05.gif"> \r
61 <P>\r
62 and its extension:\r
63 <P>\r
64 <IMG SRC="gifs/pr06.gif"> \r
65 <P>\r
66 are declared.\r
67 <P>\r
68 Then for variable z of type id_card and variable t of type idf_card\r
69 the corresponding generation statement may be the following:\r
70 <P>\r
71 <IMG SRC="gifs/pr07.gif"> \r
72 <P>\r
73 Thus the formal parameters of a class are concatenated with the\r
74 formal parameters of its prefix.\r
75 <P>\r
76 One can still extend class idf_card. For instance:\r
77 <P>\r
78 <IMG SRC="gifs/pr08.gif"> \r
79 <P>\r
80 Prefixing allows to build up hierarchies of classes. Each one\r
81 hierarchy has a tree structure. A root of such a tree is a class\r
82 without prefix. One class is a successor of another class iff\r
83 the first is prefixed by the latter one.\r
84 <P>\r
85 Consider the prefix structure:\r
86 <P>\r
87 <IMG SRC="gifs/Fig0701.gif"> \r
88 <P>\r
89 Class H has a prefix sequence A, B, E, F, H. Let a, b, ... , h\r
90 denote the corresponding unique attributes of classes A, B, ...\r
91 , H, respectively. The objects of these classes have the following\r
92 forms:\r
93 <P>\r
94 <IMG SRC="gifs/Fig0702.gif"> \r
95 <P>\r
96 Let Ra, Rb,..., Rh denote reference variables of types A, B,...,\r
97 H, respectively. Then the following expressions are correct:\r
98 <P>\r
99 <IMG SRC="gifs/pr09.gif"> \r
100 <P>\r
101 Variable Ra may designate the object of class B (or C,..., H),\r
102 i.e. the statement:\r
103 <P>\r
104 <IMG SRC="gifs/pr10.gif"> \r
105 <P>\r
106 is legal. But then attribute b is not accessible through dot via\r
107 Ra, i.e. Ra.b is incorrect. This follows from insecurity of such\r
108 a remote access. In fact, variable Ra may point any object of\r
109 a class prefixed by A, in particular, Ra may point the object\r
110 of A itself, which has no attribute b. If Ra.b had been correct,\r
111 a compiler should have distiguish the cases Ra points to the object\r
112 of A or not. But this, of course, is undistinguishable at compilation\r
113 time.\r
114 <P>\r
115 To allow, however, the user's access to attribute b (after instruction\r
116 Ra:=<B>new</B> B), the instantaneous type modification is provided\r
117 within the language:\r
118 <P>\r
119 <IMG SRC="gifs/pr11.gif"> \r
120 <P>\r
121 The correctness of this expression is checked at run time. If\r
122 Ra designates an object of B or prefixed ba B, the type of the\r
123 expression is B. Otherwise the expression is erroneous. Thus,\r
124 for instance, the expressions:\r
125 <P>\r
126 <IMG SRC="gifs/pr12.gif"> \r
127 <P>\r
128 enable remote access to the attributes b, c, ... via Ra.\r
129 <P>\r
130 So far the question of attribute concatenation was merely discussed.\r
131 However the sequences of statements can be also concatenated.\r
132 <P>\r
133 Consider class B prefixed with class A. In the sequence of statements\r
134 of class A the keyword inner may occur anywhere, but only once.\r
135 The sequence of statements of class B consists of the sequence\r
136 of statements of class A with inner replaced by the sequence of\r
137 statements of class B.\r
138 <P>\r
139 <IMG SRC="gifs/Fig0703.gif"> \r
140 <P>\r
141 In this case inner in class B is equivalent to the empty statement.\r
142 If class B prefixes another class, say C, then inner in B is replaced\r
143 by the sequence of statements of class C, and so on. If inner\r
144 does not occur explicitly, an implicit occurrence of inner before\r
145 the final end of a class is assumed.\r
146 <P>\r
147 Example\r
148 <P>\r
149 Let class complex be declared as usual:\r
150 <P>\r
151 <IMG SRC="gifs/pr13.gif"> \r
152 <P>\r
153 and assume one desires to declare a class mcomplex with the additional\r
154 attribute module. In order the generation of class mcomplex define\r
155 the value of attribute module, one can declare a class:\r
156 <P>\r
157 <IMG SRC="gifs/pr14.gif"> \r
158 <P>\r
159 Class mcomplex may be still extended:\r
160 <P>\r
161 <IMG SRC="gifs/pr15.gif"> \r
162 <P>\r
163 For these declarations each generation of class mcomplex defines\r
164 the value of attribute module, each generation of class pcomplex\r
165 defines the values of attributes module and alfa.\r
166 <P>\r
167 For reference variables z1, z2 z3 of type complex, the following\r
168 sequence of statements illustrates the presented constructs:\r
169 <P>\r
170 <IMG SRC="gifs/pr16.gif"> \r
171 <P>\r
172 Example:\r
173 <P>\r
174 Binary search tree (Bst) is a binary tree where for each node\r
175 x the nodes in the left subtree are less than x, the nodes in\r
176 the right subtree are greater than x. It is the well-known exercise\r
177 to program the algorithms for the following operations on Bst:\r
178 <P>\r
179 member(x) = true iff x belongs to Bst\r
180 <P>\r
181 insert(x), enlarge Bst with x, if x does not yet belong to Bst\r
182 <P>\r
183 We define both these operations in a class:\r
184 <P>\r
185 <IMG SRC="gifs/pr17.gif"> \r
186 <P>\r
187 In the example the common actions of member and insert are programmed\r
188 in class help. Then it suffices to use class help as a prefix\r
189 of function member and procedure insert, instead of redundant\r
190 occurrences of the corresponding sequence of statements in both\r
191 units.\r
192 <P>\r
193 Class Bst may be applied as follows:\r
194 <P>\r
195 <IMG SRC="gifs/pr18.gif"> \r
196 <P>\r
197 As shown in the declaration of Bst, class may prefix not only\r
198 other classes but also procedures and functions. Class may prefix\r
199 blocks as well.\r
200 <P>\r
201 Example:\r
202 <P>\r
203 Let class push_down (p. 5) prefix a block:\r
204 <P>\r
205 <IMG SRC="gifs/pr19.gif"> \r
206 <P>\r
207 In the above block prefixed with class push_down one can use pop\r
208 and push as local attributes. (They are local since the block\r
209 is embedded in the prefix push down.)\r
210 <P>\r
211 Example:\r
212 <P>\r
213 <IMG SRC="gifs/pr20.gif"> \r
214 <P>\r
215 In place where classes push_down and Bst are visible together\r
216 a block prefixed with Bst may be nested in a block prefixed with\r
217 push_down (or vice versa). In the inner block both data structures\r
218 are directly accessible. Note that this construct is illegal in\r
219 Simula 67. <HR>\r
220 \r
221 <P><hr>\r
222 <A HREF="coroutin.htm"><IMG SRC="gifs/PrevPage.gif"></A>\r
223 <A HREF="HomePage.htm"><IMG SRC="gifs/HomePage.gif"></A> \r
224 <A HREF="FormalTy.htm"><IMG SRC="gifs/NextPage.gif"></A> <HR>\r
225 \r
226 <ADDRESS>\r
227 Last update 02/07/95 \r
228 </ADDRESS>\r
229 \r
230 <ADDRESS>\r
231 Comments, suggestions and critiques are welcome to : <A HREF="mailto:linfo062@crisv2.univ-pau.fr">linfo062@crisv2.univ-pau.fr</A>\r
232 \r
233 </ADDRESS>\r
234 \r
235 </BODY>\r
236 \r
237 </HTML>\r