umouse

umouse.git
git clone git://git.lenczewski.org/umouse.git
Log | Files | Refs | Submodules | README

DPANSA13.HTM (10915B)


      1 <HTML><HEAD>
      2 <TITLE>DPANS94</TITLE>
      3 <link disabled rel="stylesheet" href="mpexc6.css">
      4 <style>@import url(mpexc6.css);</style>
      5 </head>
      6 
      7 <BODY>
      8 <table width=100%>
      9 <tr>
     10 <td align=left>
     11 <a href=dpansa12.htm><img src=left.gif
     12  width=26 height=26 align=ALIGN border=0></a>
     13 <a href=dpansa14.htm><img src=right.gif
     14  width=26 height=26 align=ALIGN border=0></a>
     15 </td>
     16 <td align=right>
     17 <a href=dpans.htm#toc><img src=up.gif 
     18  width=26 height=26 align=ALIGN border=0></a>
     19 <a name=A.13>Table of Contents</a>
     20 </td>
     21 </tr>
     22 </table>
     23 <p>
     24 <hr size=4>
     25 
     26 <H2>A.13 The optional Locals word set</H2>
     27 
     28 The Technical Committee has had a problem with locals.  It has been
     29 argued forcefully that ANS Forth should say nothing about locals since:
     30 
     31 <UL>
     32 <LI>there is no clear accepted practice in this area;
     33 <LI>not all Forth programmers use them or even know what they are; and
     34 <LI>few implementations use the same syntax, let alone the same broad
     35 usage rules and general approaches.
     36 </UL>
     37 <P>
     38 
     39 It has also been argued, it would seem equally forcefully, that the lack
     40 of any standard approach to locals is precisely the reason for this lack
     41 of accepted practice since locals are at best non-trivial to implement
     42 in a portable and useful way.  It has been further argued that users who
     43 have elected to become dependent on locals tend to be locked into a
     44 single vendor and have little motivation to join the group that it is
     45 hoped will <B>broadly accept</B> ANS Forth unless the Standard addresses
     46 their problems.
     47 
     48 <P>
     49 
     50 Since the Technical Committee has been unable to reach a strong
     51 consensus on either leaving locals out or on adopting any particular
     52 vendor's syntax, it has sought some way to deal with an issue that it
     53 has been unable to simply dismiss.  Realizing that no single mechanism
     54 or syntax can simultaneously meet the desires expressed in all the
     55 locals proposals that have been received, it has simplified the problem
     56 statement to be to define a locals mechanism that:
     57 
     58 <UL>
     59 <LI>is independent of any particular syntax;
     60 <LI>is user extensible;
     61 <LI>enables use of arbitrary identifiers, local in scope to a single
     62 definition;
     63 <LI>supports the fundamental cell size data types of Forth; and
     64 <LI>works consistently, especially with respect to re-entrancy and
     65 recursion.
     66 </UL>
     67 <P>
     68 
     69 This appears to the Technical Committee to be what most of those who
     70 actively use locals are trying to achieve with them, and it is at
     71 present the consensus of the Technical Committee that if ANS Forth has
     72 anything to say on the subject this is an acceptable thing for it to
     73 say.
     74 
     75 <P>
     76 
     77 This approach, defining 
     78 <a href=dpans13.htm#13.6.1.0086>(LOCAL)</a>, 
     79 is proposed as one that can be used
     80 with a small amount of user coding to implement some, but not all, of
     81 the locals schemes in use.  The following coding examples illustrate how
     82 it can be used to implement two syntaxes.
     83 
     84 <p>
     85 
     86 The syntax defined by this Standard and used in the systems of
     87 Creative Solutions, Inc.:
     88 
     89 <P>
     90 
     91 
     92 <PRE>
     93    : LOCALS|  ( "name...name |" -- )
     94        BEGIN
     95          BL WORD   COUNT OVER C@
     96          [CHAR] | - OVER 1 - OR
     97        WHILE
     98          (LOCAL)
     99        REPEAT 2DROP   0 0 (LOCAL)
    100    ;
    101    IMMEDIATE
    102 
    103    : EXAMPLE  ( n -- n n**2 n**3 )
    104        LOCALS| N |   N  DUP N *  DUP N * ;
    105 </PRE>
    106 
    107 A proposed syntax: ( LOCAL name ) with additional usage rules:
    108 
    109 <P>
    110 
    111 
    112 <PRE>
    113    : LOCAL  ( "name" -- )  BL WORD COUNT (LOCAL) ;  IMMEDIATE
    114 
    115    : END-LOCALS  ( -- )  0 0 (LOCAL) ;  IMMEDIATE
    116 
    117    : EXAMPLE  ( n -- n n**2 n**3 )
    118        LOCAL N  END-LOCALS   N  DUP N *  DUP N * ;
    119 </PRE>
    120 
    121 <P>
    122 
    123 Other syntaxes can be implemented, although some will admittedly require
    124 considerably greater effort or in some cases program conversion.  Yet
    125 other approaches to locals are completely incompatible due to gross
    126 differences in usage rules and in some cases even scope identifiers.
    127 For example, the complete local scheme in use at Johns Hopkins had
    128 elaborate semantics that cannot be duplicated in terms of this model.
    129 
    130 <P>
    131 
    132 To reinforce the intent of section 
    133 <a href=dpans13.htm>13</a>, here are two examples of actual use of
    134 locals.  The first illustrates correct usage:
    135 
    136 <P>
    137 
    138 a)
    139 
    140 <PRE>
    141 : {  ( "name ... }" - )
    142     BEGIN  BL WORD COUNT
    143       OVER C@ [CHAR] }
    144       - OVER 1 -  OR 
    145     WHILE
    146       (LOCAL)
    147     REPEAT 2DROP 0 0 (LOCAL)
    148 ;  IMMEDIATE
    149 </PRE>
    150 
    151 <P>
    152 
    153 b)
    154 
    155 <PRE>
    156 : JOE  ( a b c -- n )
    157     >R 2* R> 2DUP + 0
    158     { ANS 2B+C C 2B A }
    159     2 0 DO  1 ANS + I + TO ANS  ANS . CR  LOOP
    160     ANS . 2B+C . C . 2B . A . CR
    161     ANS
    162 ;
    163 </PRE>
    164 c) 
    165 <PRE>
    166 100 300 10 JOE .
    167 </PRE>
    168 
    169 <P>
    170 
    171 The word { at a) defines a local declaration syntax that surrounds the
    172 list of locals with braces.  It doesn't do anything fancy, such as
    173 reordering locals or providing initial values for some of them, so
    174 locals are initialized from the stack in the default order.  The
    175 definition of JOE at b) illustrates a use of this syntax.  Note that
    176 work is performed at execution time in that definition before locals are
    177 declared.  It's OK to use the return stack as long as whatever is placed
    178 there is removed before the declarations begin.
    179 
    180 <P>
    181 
    182 Note that before declaring locals, B is doubled, a subexpression (2B+C)
    183 is computed, and an initial value (zero) for ANS is provided.  After
    184 locals have been declared, JOE proceeds to use them.  Note that locals
    185 may be accessed and updated within do-loops.  The effect of interpreting
    186 line c) is to display the following values:
    187 
    188 <P>
    189 
    190 1 (ANS the first time through the loop), 
    191 <BR>
    192 3 (ANS the second time), 
    193 <BR>
    194 3 (ANS), 610 (2B+C), 10 (C), 600 (2B), 100 (A), 
    195 <BR>
    196 and 3 (ANS left on the stack by JOE). 
    197 <BR>
    198 
    199 <P>
    200 
    201 The names of the locals vanish after JOE has been compiled.  The storage and
    202 meaning of locals appear when JOE's locals are declared and vanish as JOE
    203 returns to its caller at ; (semicolon).
    204 
    205 <P>
    206 
    207 A second set of examples illustrates various things that break the rules.  We
    208 assume that the definitions of LOCAL and END-LOCALS above are present, along
    209 with { from the preceding example.
    210 <P>
    211 
    212 d)      
    213 <PRE>
    214 : ZERO   0 POSTPONE LITERAL POSTPONE LOCAL ; IMMEDIATE
    215 </PRE>
    216 <P>
    217 
    218 e)
    219 <PRE>
    220 : MOE  ( a b )
    221      ZERO TEMP  LOCAL B  1+ LOCAL A+  ZERO ANSWER ;
    222 </PRE>
    223 
    224 <P>
    225 
    226 f)      
    227 <PRE>
    228 : BOB  ( a b c d )  { D C }  { B A } ;
    229 </PRE>
    230 <P>
    231 
    232 Here are two definitions with various violations of rule 
    233 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>a.  In
    234 e) the declaration of TEMP is legal and creates a local whose initial
    235 value is zero.  It's OK because the executable code that ZERO generates
    236 precedes the first use of (LOCAL) in the definition.  However, the 1+
    237 preceding the declaration of A+ is illegal.  Likewise the use of ZERO to
    238 define ANSWER is illegal because it generates executable code between
    239 uses of (LOCAL).  Finally, MOE terminates illegally (no END-LOCALS).
    240 BOB in f) violates the rule against declaring two sets of locals.
    241 <P>
    242 
    243 g)      
    244 <PRE>
    245 : ANN  ( a b  -- b )  DUP >R  DUP IF { B A } THEN  R> ;
    246 </PRE>
    247 <P>
    248 
    249 h)      
    250 <PRE>
    251 : JANE  ( a b -- n )  { B A }  A B + >R  A B -  R> / ;
    252 </PRE>
    253 <P>
    254 
    255 ANN in g) violates two rules.  The IF ... THEN around the declaration
    256 of its locals violates 
    257 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>b, 
    258 and the copy of B left on the return
    259 stack before declaring locals violates 
    260 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>c.  JANE in h) violates
    261 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>d 
    262 by accessing locals after placing the sum of A and B on the
    263 return stack without first removing that sum.
    264 
    265 <P>
    266 
    267 i)      
    268 <PRE>
    269 : CHRIS  ( a b)
    270      { B A }  ['] A EXECUTE  5 ['] B >BODY !
    271      [ ' A ] LITERAL LEE ;
    272 </PRE>
    273 
    274 <P>
    275 
    276 CHRIS in i) illustrates three violations of 
    277 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>e.  The attempt to
    278 <a href=dpans6.htm#6.1.1370>EXECUTE</a> 
    279 the local called A is inconsistent with some implementations.
    280 The store into B via 
    281 <a href=dpans6.htm#6.1.0550>>BODY</a> 
    282 is likely to cause tragic results with many
    283 implementations; moreover, if locals are in registers they can't be
    284 addressed as memory no matter what is written.
    285 
    286 <P>
    287 
    288 The third violation, in which an execution token for a definition's
    289 local is passed as an argument to the word LEE, would, if allowed, have
    290 the unpleasant implication that LEE could EXECUTE the token and obtain a
    291 value for A from the particular execution of CHRIS that called LEE this
    292 time.
    293 
    294 <P>
    295 
    296 <hr>
    297 <a name=A.13.3>
    298 <H3>A.13.3 Additional usage requirements</H3>
    299 </a>
    300 
    301 Rule 
    302 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>d 
    303 could be relaxed without affecting the integrity of the
    304 rest of this structure.  
    305 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>c could not be.
    306 
    307 <P>
    308 
    309 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>b 
    310 forbids the use of the data stack for local storage because no
    311 usage rules have been articulated for programmer users in such a case.
    312 Of course, if the data stack is somehow employed in such a way that
    313 there are no usage rules, then the locals are invisible to the
    314 programmer, are logically not on the stack, and the implementation
    315 conforms.
    316 
    317 <P>
    318 
    319 The minimum required number of locals can (and should) be adjusted to
    320 minimize the cost of compliance for existing users of locals.
    321 
    322 <P>
    323 
    324 Access to previously declared local variables is prohibited by Section
    325 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>d 
    326 until any data placed onto the return stack by the application
    327 has been removed, due to the possible use of the return stack for
    328 storage of locals.
    329 
    330 <P>
    331 
    332 Authorization for a Standard Program to manipulate the return stack
    333 (e.g., via 
    334 <a href=dpans6.htm#6.1.0580>&gt;R</a> 
    335 <a href=dpans6.htm#6.1.2060>R&gt;</a>) 
    336 while local variables are active overly constrains
    337 implementation possibilities.  The consensus of users of locals was that
    338 Local facilities represent an effective functional replacement for
    339 return stack manipulation, and restriction of standard usage to only one
    340 method was reasonable.
    341 
    342 <P>
    343 
    344 Access to Locals within 
    345 <a href=dpans6.htm#6.1.1240>DO</a>..<a href=dpans6.htm#6.1.1800>LOOP</a>s 
    346 is expressly permitted as an
    347 additional requirement of conforming systems by Section 
    348 <a href=dpans13.htm#13.3.3.2>13.3.3.2</a>g.
    349 Although words, such as 
    350 <a href=dpans13.htm#13.6.1.0086>(LOCALS)</a>, 
    351 written by a System Implementor, may
    352 require inside knowledge of the internal structure of the return stack,
    353 such knowledge is not required of a user of compliant Forth systems.
    354 
    355 <P>
    356 
    357 <hr>
    358 <a name=A.13.6>
    359 <H3>A.13.6 Glossary</H3>
    360 </a>
    361 
    362 
    363 
    364 <hr>
    365 <a name=A.13.6.1.2295>A.13.6.1.2295 TO</A>
    366 <P>
    367 
    368 Typical use:    <code>x TO name</code>
    369 
    370 <P>
    371 <code>
    372 See:  <a href=dpansa6.htm#A.6.2.2295>A.6.2.2295 TO</A>
    373 </code>
    374 <P>
    375 
    376 <hr>
    377 <a name=A.13.6.2.1795>A.13.6.2.1795 LOCALS|</A>
    378 <P>
    379 
    380 A possible implementation of this word and an example of usage is given in
    381 <a href=dpansa13.htm#A.13>A.13</a>, 
    382 above.  It is intended as an example only; any implementation yielding
    383 the described semantics is acceptable.
    384 
    385 <P>
    386 
    387 <hr>
    388 <a href=dpans.htm#toc><IMG   src="up.gif" ></A>    Table of Contents 
    389 <BR>
    390 <a href=dpansa14.htm><IMG   src="right.gif" ></A>
    391 Next Section
    392 <P>
    393 </BODY>
    394 </HTML>