Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #33820

Re: uninitialized and implicitly initialized locals

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: uninitialized and implicitly initialized locals
Date 2014-10-10 16:03 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2014Oct10.180311@mips.complang.tuwien.ac.at> (permalink)
References (5 earlier) <V8udnS1fUb2WpqjJnZ2dnUU7-I-dnZ2d@supernews.com> <2014Oct8.173212@mips.complang.tuwien.ac.at> <N_OdnXwI08V58ajJnZ2dnUVZ8nadnZ2d@supernews.com> <2014Oct9.160722@mips.complang.tuwien.ac.at> <FL2dncik_pHsMKvJnZ2dnUU7-RmdnZ2d@supernews.com>

Show all headers | View raw


Andrew Haley <andrew29@littlepinkcloud.invalid> writes:
>Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>> Andrew Haley <andrew29@littlepinkcloud.invalid> writes:
>>>Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>>>> Andrew Haley <andrew29@littlepinkcloud.invalid> writes:
>>>>>Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>>>>>> Andrew Haley <andrew29@littlepinkcloud.invalid> writes:
>>>>>>> On the other hand, if someone really wants to create an
>>>>>>>uninitialized local I can't see much harm in it.
>>>>>> 
>>>>>> The benefit of defining a local where it is properly initialized is
>>>>>> that every read from the local produces a proper value.  You cannot
>>>>>> read the uninitialized value.
>>>>>
>>>>>I'm not going to argue with that advantage, but it's hard to do that
>>>>>if you're only allowed to define locals in one place.
>>>> 
>>>> Lifting that restriction is not that hard.
>>>
>>>Ummm.  I don't think that you can really say so for all
>>>implementations.
>> 
>> Sure I can: For all implementations lifting that restriction is not
>> that hard.
>> 
>> To support that claim, I have outlined how to do it in
>> <2014Oct7.173637@mips.complang.tuwien.ac.at> and
>> <2014Oct8.180623@mips.complang.tuwien.ac.at>.
>
>Hmmm.  I suppose "not that hard" is in the eye of the beholder.
>Locals are not such a big deal that it's worth so much effort.

How much effort is it?  With such a statement, you surely have a
number for that.  To me it seems that implementing this simple
approach is very little effort.  Or do you mean to write that locals
are not worth any effort.

>The part I really object to is the dummy return values (effectively a
>comment) on the right hand side of the "--".  They are misleading.

When you know the notation, they are not misleading, but convenient.

Do you really think that

: strcmp ( addr1 u1 addr2 u2 -- n )
        {: addr1 u1 addr2 u2 :}
  ... ;

is an improvement over

: strcmp {: addr1 u1 addr2 u2 -- n :}
  ... ;

?

>>>>>> So the harm of uninitialized locals to the programmer is that he may
>>>>>> make errors that would be caught (as "undefined word") if the locals
>>>>>> were defined later, where they are properly initialized. 
>>>>>
>>>>>Sure, but I do not have to remind you that this is Forth: your bullet,
>>>>>gun, and foot.  Initialized locals are already provided, and if you
>>>>>prefer to initialize locals, add all of them to the initialized locals
>>>>>list with whatever constant you prefer as the initial value.  The
>>>>>problem is already solved.
>>>> 
>>>> No, by defining a local and initializing it to a dummy value before
>>>> you know the proper initial value, you will not catch a to-early read
>>>> of the local.  This problem is not at all solved in Forth-2012.
>>>
>>>And in what way will an initialized local catch a too-early read of a
>>>local?  (I'm assuming we're not talking about multiple locals blocks,
>>>but the single one that we have.)
>> 
>> It was you who claimed that initialized locals solve the problem that
>> arises from the restriction to one locals definition that you want to
>> uphold.
>
>It does.  It's a way of declaring locals that may be defined later.
>That's the problem it solves.  But that's nothing to do with a
>too-early read of a local.

Yes, that's what I pointed out.  Initialized locals while still
allowing only one locals definition are not a solution to that
problem.

>> I pointed out that it did not.  So you have to answer your question
>> yourself.
>
>I no longer have any idea what you are talking about.

First you claimed "Initialized locals are already provided [...].  The
problem is already solved." (full quote above).  Later you asked "And
in what way will an initialized local catch a too-early read of a
local?"  I.e., problem not already solved.  Everything quoted above.
I am not to lazy to trim quotes, these quotes are included because
they provide the context.

>I don't think so.  Look at this:
>
>: strcmp {: addr1 u1 addr2 u2 -- n :}
> addr1 addr2
> u1 u2 min 0
> ?do {: s1 s2 :}
>   s1 c@ s2 c@ -
>   ?dup-if
>     unloop exit
>   then
>   s1 char+ s2 char+
> loop
> 2drop
> u1 u2 - ;
>
>It is obvious to anyone skilled in the art that this word needs to be
>refactored, and with that refactoring the need for the inner locals
>declaration goes away.
>
>Given a word -STR ( a1 a2 u), which does the inner comparison, this
>becomes:
>
>: @pair ( a1 a2 - a1 a2 c1 c2)   over c@ over c@ ;
>: step ( a1 a2 - a1' a2')   swap 1+ swap 1+ ;
>
>: -str ( a1 a2 u - n)
>   0 do
>      @pair -  ?dup if  unloop exit  then
>      step
>   loop
>   2drop 0 ;
>
>: strcmp {: addr1 u1 addr2 u2 -- n :}
>   addr1 addr2
>   u1 u2 min
>   -str ?dup if  exit  then
>   u1 u2 - ;
>
>And with a bit more work the need for any locals goes away altogether.

I would like to see the result.

Anyway, concerning your factored solution, I find it pretty horrible
in many respects, but there's no accounting for tastes.

Yes, in this example one can do without the inner locals relatively
easily, because there are only two cells permanently alive in the
loop.  No need for any factoring:

: strcmp {: addr1 u1 addr2 u2 -- n :}
 addr1 addr2
 u1 u2 min 0
 ?do ( s1 s2 )
   over c@ over c@ -
   ?dup-if
     nip nip unloop exit
   then
   over char+ over char+
 loop
 2drop
 u1 u2 - ;

But this is actually just an example demonstrating the
single-assignment style over one that uses TO on locals:

: strcmp {: addr1 u1 addr2 u2 -- n :}
 u1 u2 min 0
 ?do
   addr1 c@ addr2 c@ -
   ?dup-if
     unloop exit
   then
   addr1 char+ TO addr1
   addr2 char+ TO addr2
 loop
 u1 u2 - ;

The original purpose was not to demonstrate the need for inner locals.
I could now search for one example where I used it in production code,
but examples from production code have the problem that they are
usually hard to understand without a ton of context.

>The question in my mind is whether there is ever a word not in need of
>refactoring which needs multiple locals declarations.

If there is not, there is no need for uninitialized locals, either.  I
note that you used no uninitialized locals in your version of strcmp,
but you used the "--" feature:-).

I guess if I take the trouble to search for production code with
multiple locals definitions, and challenge you to refactor them, the
result will be equally deafening silence as for my repeated challanges
to refactor locals-containing code in a better way without locals (and
two of the examples are really much too big).  Here's the challenge
from two years ago <2012Oct16.134654@mips.complang.tuwien.ac.at>:

|Concerning factoring, it helps in some cases, but not always.  I put
|up some cases where I could not come up with a factoring that
|eliminates locals here as a challenge
|<2003Jan25.154659@a0.complang.tuwien.ac.at>
|<a3j4aa$a23$1@news.tuwien.ac.at>
|<2009Sep10.122914@mips.complang.tuwien.ac.at>, but nobody has taken up
|the challenge.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2014: http://www.euroforth.org/ef14/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Why smudge? hughaguilar96@yahoo.com - 2014-09-30 19:25 -0700
  Re: Why smudge? november.nihal@gmail.com - 2014-10-01 08:54 -0700
    Re: Why smudge? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-01 19:09 +0000
  Re: Why smudge? november.nihal@gmail.com - 2014-10-01 10:16 -0700
    Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-01 18:54 -0700
  Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-02 15:31 -0700
    Re: Why smudge? Michael Barry <barrym95838@yahoo.com> - 2014-10-02 16:35 -0700
      Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-02 17:02 -0700
        Re: Why smudge? Michael Barry <barrym95838@yahoo.com> - 2014-10-02 17:51 -0700
          Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-02 18:50 -0700
        Re: Why smudge? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-04 00:30 +0100
          Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-03 17:40 -0700
            Re: Why smudge? Raimond Dragomir <raimond.dragomir@gmail.com> - 2014-10-03 23:00 -0700
              Pelc's effort to sabotage cross-compiler writing has a work-around now! hughaguilar96@yahoo.com - 2014-10-04 21:14 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-05 03:42 -0500
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! Enoch <emc.consultants@gmail.com> - 2014-10-05 10:43 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! hughaguilar96@yahoo.com - 2014-10-05 20:21 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-06 03:58 -0500
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! hughaguilar96@yahoo.com - 2014-10-05 17:09 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! Michael Barry <barrym95838@yahoo.com> - 2014-10-06 11:15 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! hughaguilar96@yahoo.com - 2014-10-06 18:52 -0700
                More ranting Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-07 03:48 -0500
                Re: More ranting albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-07 12:43 +0000
                Re: More ranting Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-07 16:23 +0200
                Re: More ranting "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-16 01:03 -0400
                Re: More ranting Just Ian <superian@gmail.com> - 2014-10-16 07:14 -0700
                Re: More ranting Michael Barry <barrym95838@yahoo.com> - 2014-10-16 13:23 -0700
                uninitialized and implicitly initialized locals (was: More ranting) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-07 15:36 +0000
                Re: uninitialized and implicitly initialized locals (was: More ranting) Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-07 22:15 +0200
                Re: uninitialized and implicitly initialized locals (was: More ranting) mhx@iae.nl - 2014-10-07 15:07 -0700
                Re: uninitialized and implicitly initialized locals (was: More ranting) Bernd Paysan <bernd.paysan@gmx.de> - 2014-10-08 03:11 +0200
                Re: uninitialized and implicitly initialized locals (was: More ranting) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-08 13:25 +0000
                Re: uninitialized and implicitly initialized locals (was: More ranting) hughaguilar96@yahoo.com - 2014-10-09 16:22 -0700
                Re: uninitialized and implicitly initialized locals (was: More ranting) "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-17 21:58 -0400
                Re: uninitialized and implicitly initialized locals (was: More ranting) mhx@iae.nl (Marcel Hendrix) - 2014-10-12 11:18 +0200
                Re: uninitialized and implicitly initialized locals (was: More ranting) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-08 12:35 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-07 15:27 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-08 12:56 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-08 08:24 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-08 15:32 +0000
                Simplified automatic scoping (was: uninitialized ...) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-08 16:06 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-08 11:57 -0500
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-08 12:18 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-09 14:19 +0000
                Re: uninitialized and implicitly initialized locals hughaguilar96@yahoo.com - 2014-10-09 14:42 -0700
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-09 14:07 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-09 10:44 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-10 16:03 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-10 13:35 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-11 10:53 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-12 11:34 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-15 12:44 +0000
                Re: uninitialized and implicitly initialized locals albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-15 14:34 +0000
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-15 15:03 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-15 11:10 -0500
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-16 12:19 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-17 03:56 -0500
                Re: uninitialized and implicitly initialized locals "Elizabeth D. Rather" <erather@forth.com> - 2014-10-17 07:48 -1000
                Re: uninitialized and implicitly initialized locals hughaguilar96@yahoo.com - 2014-10-17 19:26 -0700
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-18 12:55 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-18 13:21 -0500
                Re: uninitialized and implicitly initialized locals "Elizabeth D. Rather" <erather@forth.com> - 2014-10-18 08:44 -1000
                Re: uninitialized and implicitly initialized locals albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-10-19 09:53 +0000
                Re: uninitialized and implicitly initialized locals anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2014-10-20 12:48 +0000
                Re: uninitialized and implicitly initialized locals "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-20 16:24 -0400
                Re: uninitialized and implicitly initialized locals "Elizabeth D. Rather" <erather@forth.com> - 2014-10-20 10:30 -1000
                Re: uninitialized and implicitly initialized locals hughaguilar96@yahoo.com - 2014-10-20 16:23 -0700
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 04:16 -0500
                Re: uninitialized and implicitly initialized locals mhx@iae.nl (Marcel Hendrix) - 2014-10-26 11:32 +0200
                Re: uninitialized and implicitly initialized locals stephenXXX@mpeforth.com (Stephen Pelc) - 2014-10-26 14:27 +0000
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 13:56 -0500
                Re: uninitialized and implicitly initialized locals mhx@iae.nl (Marcel Hendrix) - 2014-10-26 20:32 +0200
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 16:18 -0500
                Re: uninitialized and implicitly initialized locals hughaguilar96@yahoo.com - 2014-10-26 15:05 -0700
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 17:53 -0500
                Re: uninitialized and implicitly initialized locals hughaguilar96@yahoo.com - 2014-10-26 16:30 -0700
                Re: uninitialized and implicitly initialized locals "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-27 01:45 -0400
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-27 12:21 -0500
                Re: uninitialized and implicitly initialized locals Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-10-26 17:57 -0500
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-06 20:55 +0100
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! hughaguilar96@yahoo.com - 2014-10-06 16:05 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-09 07:36 +0100
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! stephenXXX@mpeforth.com (Stephen Pelc) - 2014-10-10 01:52 +0000
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! hughaguilar96@yahoo.com - 2014-10-09 19:56 -0700
                Re: Pelc's effort to sabotage cross-compiler writing has a work-around now! "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-17 23:11 -0400
            Re: Why smudge? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-05 11:05 +0100
              Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-05 19:48 -0700
                Re: Why smudge? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-10-06 07:53 +0100
                Re: Why smudge? "Elizabeth D. Rather" <erather@forth.com> - 2014-10-05 21:29 -1000
              Re: Why smudge? hughaguilar96@yahoo.com - 2014-10-09 15:16 -0700
              Re: Why smudge? "Rod Pemberton" <buzzoff@nonamewhichexists.cmm> - 2014-10-16 01:03 -0400

csiph-web