Recent

Author Topic: Ada style "for" loop counters  (Read 59908 times)

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Ada style "for" loop counters
« on: January 07, 2016, 08:50:50 pm »
I'd like to suggest an addition to fpc's "Pascal" definition:

         that for loop indexes could be like those in Ada - that is, do not require a
         variable declaration. They would always be word/integer (longword/longint/
         Qword/int64) as needed and would only "exist" within the for loop block.

(Ada is much more strict on typing than Pascal, yet it allows simple loop control w/o declaring the variable).

(* ---------------------------------------------------------------------------------------- *)
Procedure foobar;
var
        p, q: word;
begin

       for i := 1 to 22 do
          begin                          // variable i (word) exists only between here <<<
               p := i * 2;
               foo (p);
               bar (i);
          end;                            //                   and here<<<

       q := 23 + p + i;     // compiler error:  Error: Identifier not found "i"
       writeln (q);

       for i := -3000000 to 0 do bigfoo (i);      // variable i is a longint this time.

end;
(* ---------------------------------------------------------------------------------------- *)

1) i would not need to be declared.
2) i would be a "word" the first time and a longint the 2nd time.
3) i would only exist within the for statement (including within its begin/end block)
4) thus, above a compile time error would not allow the q := operation (above) as i would be
    unknown.

Optionally, programmer ordinal types could be used ( for f := apple to kiwi do  ...) w/o declaring f (assumes the programmer has defined a type that contains those fruit ..).

Note that such a change has no impact on any existing code but would make future coding less work  (not a lot less, but ....)
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Ada style "for" loop counters
« Reply #1 on: January 07, 2016, 08:59:55 pm »
+1


(I might  suggested this a dozen times by now)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Ada style "for" loop counters
« Reply #2 on: January 07, 2016, 09:07:53 pm »
--1000%

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Ada style "for" loop counters
« Reply #3 on: January 07, 2016, 09:15:48 pm »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Ada style "for" loop counters
« Reply #4 on: January 07, 2016, 09:21:29 pm »
@eny
And don't forget the curly braces thread in day 3 of this year.

People who keep insisting on introducing such non-sense clearly have no idea about the origin and idea's behind pascal programming language.

Such features adds complexity, introduces inconsistency and is plain ugly to debug. On the fly-untyped variables... *barf*

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Ada style "for" loop counters
« Reply #5 on: January 07, 2016, 10:05:51 pm »
Then please all reply with

first check

http://www.freepascal.org/faq.var#extensionselect

before suggesting language features


In this case just change warnings to errors, and you have  the safety part:

Quote
  -Se<x>     Error options. <x> is a combination of the following:
         <n> : Compiler halts after the <n> errors (default is 1)
         w : Compiler also halts after warnings
         n : Compiler also halts after notes
         h : Compiler also halts after hints

Non declaration is shorthand, and the extension faq covers that.
« Last Edit: January 07, 2016, 10:10:15 pm by marcov »

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: Ada style "for" loop counters
« Reply #6 on: January 07, 2016, 10:39:20 pm »

People who keep insisting on introducing such non-sense clearly have no idea about the origin and idea's behind pascal programming language.

Such features adds complexity, introduces inconsistency and is plain ugly to debug. On the fly-untyped variables... *barf*

I likely have more insight than most into what Mr. Wirth meant by his prototype of Pascal.  Indeed he didn't like it enough and went on to new things such as Modula II (which inspired a lot of new features in Pascal as seen in TP from about 4.0 onward [as well as other compilers]).  Pascal today is not what Mr. Wirth envisioned at the start but then many things are not.

Ada's type strictness was in large part inspired by Pascal - for very good reason.  Further, it is MUCH MORE TYPE STRICT than Pascal.

And yet, for the simple purpose of loop control, no strict declaration of for loop control variables is required in Ada (it is, of course, allowed).  This is why I proposed it here.

It is not complexity - it is simplicity.  I'd venture that 90% or more of my loop control variables are named i,j,k,m,n,p,q (in about that order) and that 90% of them, further, are used for 15 lines of code, or less (maybe more if nested deep enough).  They are ephemeral by their nature.

Such are certainly well taken care of when debugging Ada programs.

Finally, Pascal now allows the abomination of C operations like       a+ = c      albeit permitted by compiler directive (relief - may it never see my machine).

Now that is awful.  Stupid.  Insane bad style.  Just as bad as curly braces.  Of course that's not at all what I've suggested.

What I suggest is very natural however.  In context, any programmer can see that a for loop needs an index.  Needing to declare a type for it is quite redundant.  Using the correct type for it is something the compiler can divine easily enough from the range or type of the begin/end values of the loop.

Further, it lends itself very well to register optimization of loop control variables.  (May be redundant).

Finally, it was a suggestion.  It certainly doesn't merit the ridicule you heap on it via a ridiculous comparison with a ridiculous idea from someone else.

Suggestions should be discussed - not dismissed out of hand because they fail your particular purity test.

Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: Ada style "for" loop counters
« Reply #7 on: January 07, 2016, 11:44:45 pm »

first check

http://www.freepascal.org/faq.var#extensionselect

before suggesting language features[/b][/size]

In this case just change warnings to errors, and you have  the safety part:

Quote
  -Se<x>     Error options. <x> is a combination of the following:
         <n> : Compiler halts after the <n> errors (default is 1)
         w : Compiler also halts after warnings
         n : Compiler also halts after notes
         h : Compiler also halts after hints

Non declaration is shorthand, and the extension faq covers that.

Not sure why you believe massive fonts make your reply better.

As to the provided link.  The first point says:
Compatibility must not be compromised in any way. Existing codebases on at least the Pascal level must keep running. This is often more difficult than most people think.

My suggestion does not invalidate a single line of Pascal ever written in the history of Pascal anywhere.  (ie: Declared variables used in for loops would still exist, compile and function).

The next point says:
The extension must have real value. Anything that is only a shorter notation does not apply, unless it is out of compatibility with an existing Pascal/Delphi codebase. Practically it means it must make something possible that cannot be done otherwise or be a compatibility item

My suggestion does not invalidate that notion at all except that it eliminates the _formal_ declaration of loop control variables.  Of course it would only apply to the fpc compiler versions that incorporate it and therefore to IDE's that use those fpc's.  Some editors/IDE's may flag it.  It could be directive flagged on/off in case there are SQA objections (just as the unholy abomination of C operators (    a+ = c    )).

Then it goes onto:
The change must fit in with the scope of the project: implementing a Pascal compiler with support for RAD and a generic DB system. This excludes features like inline SQL, and large garbage collected object frameworks.

The change is too local in scope (within a line of code or the begin .. end scope it controls).  This should have 0 impact on any DB system, garbage collection, SQL, objects, etc.  As to RAD ... well, it should be obvious that it only helps one to write control loops w/o the maintenance of control loop variables whose types can easily be inferred from the loop control constants or variable type(s).

As to the rest, the desirability for such is obvious (at least to me) - it simplifies and accelerates coding and is a well proven language feature of the most safety/mission-critical oriented language that there ever has been (Ada).  The programmer doesn't have to focus on writing formal declarations (and fixing errors) for one of the most fundamentally used features of most programming languages.  Indeed: because the type of the variable is guaranteed by the compiler to be correct for the type of the begin/end constants or variables of the loop there will be fewer errors in the generated op code due to programmer error - this is most especially valid over the life cycle of the code as things change and perhaps attention is not paid in change to the type declaration of the control variable.

Importantly to READABILITY of the code:   It simplifies and clarifies coding because the VAR block of a procedure or function can be constrained to declare and describe the more critical/important variables and leave implicit the simple things that control for loops.  Further, although this is covered by compiler optimization, it removes to the bottom of the stack those control variables making word/longword alignment simpler.  Also these variables need not even be on the stack at all if they can be done in register (again, perhaps best left to the optimization code).

The compiler can assign symbols that can be used by the debugger.  If a variable name is used for separate control blocks - and has different types then the compiler can name them sequentially within the procedure or function appropriately  (_U_$P$LOCI_$$_I_1, _U_$P$LOCI_$$_I_2 ...).

The test cases are pretty simple.  Put like named variables in the global section of programs/units; passed parameters; and local variables with incompatible types to the for loop.  The compiler should warn or stop appropriately.

"A design is not complete when there is nothing to add, it is complete when there is nothing left to take away."
 -Le Courbousier.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Ada style "for" loop counters
« Reply #8 on: January 08, 2016, 12:25:50 am »
First of all let me apologize for my initial rude response. For me it was a drop that pushes the water out of the bucket, as also indicated by eny.

Although i'm fairly new as in having an account over here, i watch these forums for quite some time now, and believe it or not the proposal of wanting to introduce on the fly or scoped variables is a topic that passes here on the forums as well as on mailing list every couple of months.

It is of course more than allowed to express your wishes, wanting to introduce new features trying to convince others, work on and discuss proposals etc.

I agree that over time Pascal evolved, and indeed not all for the better or what N.W. envisioned. Do keep in mind though that some of the introduced features are part of maintaining compatibility with Delphi.

It is not complexity - it is simplicity.  I'd venture that 90% or more of my loop control variables are named i,j,k,m,n,p,q (in about that order) and that 90% of them, further, are used for 15 lines of code, or less (maybe more if nested deep enough).  They are ephemeral by their nature.
Of course they are ephemeral by their nature. But really, in all those discussion i have seen and what i miss in your arguments as well, what on earth is the benefit of having scoped variables (let alone having them as untyped ones).

Quote
What I suggest is very natural however.  In context, any programmer can see that a for loop needs an index.  Needing to declare a type for it is quite redundant.  Using the correct type for it is something the compiler can divine easily enough from the range or type of the begin/end values of the loop.
That we as humans/programmer see this as natural, i will not disagree with.

However, having untyped variables is in my opinion very unnatural for the Pascal language, and adds complexity to the parser/compiler, while the benefits are next to none.

Maybe it is a personal taste of reading sources, but when i dive into c code i am always amazed how some people are able to actually read and understand the code they've written when scoped variables are used and abused all over the place.

Right now things are pretty much clear, as you declare a variable and its type. And even though you can use and abuse that variable to your likings, you will never have to ask yourself, was that variable a byte, an integer, a char, or no the last iteration it went through made it a real (with all the consequences that comes along with that, especially with a typed language). I truly do not understand how they managed to justify this to allow in ADA.

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: Ada style "for" loop counters
« Reply #9 on: January 08, 2016, 12:50:33 am »

Although i'm fairly new as in having an account over here, i watch these forums for quite some time now, and believe it or not the proposal of wanting to introduce on the fly or scoped variables is a topic that passes here on the forums as well as on mailing list every couple of months.


It is of course more than allowed to express your wishes, wanting to introduce new features trying to convince others, work on and discuss proposals etc.

I agree that over time Pascal evolved, and indeed not all for the better or what N.W. envisioned. Do keep in mind though that some of the introduced features are part of maintaining compatibility with Delphi.

It is not complexity - it is simplicity.  I'd venture that 90% or more of my loop control variables are named i,j,k,m,n,p,q (in about that order) and that 90% of them, further, are used for 15 lines of code, or less (maybe more if nested deep enough).  They are ephemeral by their nature.
Of course they are ephemeral by their nature. But really, in all those discussion i have seen and what i miss in your arguments as well, what on earth is the benefit of having scoped variables (let alone having them as untyped ones).

Quote
What I suggest is very natural however.  In context, any programmer can see that a for loop needs an index.  Needing to declare a type for it is quite redundant.  Using the correct type for it is something the compiler can divine easily enough from the range or type of the begin/end values of the loop.
That we as humans/programmer see this as natural, i will not disagree with.

However, having untyped variables is in my opinion very unnatural for the Pascal language, and adds complexity to the parser/compiler, while the benefits are next to none.

Maybe it is a personal taste of reading sources, but when i dive into c code i am always amazed how some people are able to actually read and understand the code they've written when scoped variables are used and abused all over the place.

Right now things are pretty much clear, as you declare a variable and its type. And even though you can use and abuse that variable to your likings, you will never have to ask yourself, was that variable a byte, an integer, a char, or no the last iteration it went through made it a real (with all the consequences that comes along with that, especially with a typed language). I truly do not understand how they managed to justify this to allow in ADA.


First off thanks for taking the time for a more considered reply.

My suggestion was not on the fly.  I recognized back in about 1990 (when I took an Ada programming course) that implicit control variables was not only a reduction in un-needed formalism (in the most formal of languages of the day), but that it would also contribute to fewer errors in Pascal were it implemented.

I am not suggesting that the control variable in a loop be untyped.  I am suggesting that the type be correctly determined by the compiler according to the constants or variables (or both) that indicate the begin/end of the loop.  As such errors in control loop variable type will be eliminated or inconsistencies in the begin v. end controls of the loop detected and flagged by the compiler (fatal).

Therefore I disagree entirely that there are no benefits:   quicker coding and less "noise" in the VAR section of procedures (no clutter from the added lists of needed control variables).  Again: the "type" of the variable would be strongly set by the compiler.  It is not weak.  Indeed: over the life cycle of the code if those beginning and ending points of the for loop are changed to require a larger type then it would be taken care of automatically and possibly (probably(?)) prevent run time bugs.

What it does for clarity I've put in my more detailed reply to marcov.

Why was it justified in Ada?  Because the compiler imposes simple and clear rules about what that control variable can be.  Indeed, the programmer benefits from the safety of the compiler imposing the correct variable type on the loop.  What I am suggesting is no different.

Note that the new C derived language Swift 2 (from Apple) does things in a similar fashion though I confess I haven't dug into it enough to know how/what it does in this respect.  Apple claim strong safety.

I don't know why you bring up C.  It is an abomination.  It is shit.  Sorry - I prefer shit over C.  I don't know why the c operators (styled   a+ = c,  etc.) were permitted into fpc (I can guess to make importing code easier (?)).  But of course my suggestion has nothing to do with C.  It has to do with syntax from Ada - a most deliberately (the most?) designed programming language.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Ada style "for" loop counters
« Reply #10 on: January 08, 2016, 03:40:38 am »
...
(* ---------------------------------------------------------------------------------------- *)
Procedure foobar;
var
        p, q: word;
begin

       for i := 1 to 22 do
          begin                          // variable i (word) exists only between here <<<
               p := i * 2;
               foo (p);
               bar (i);
          end;                            //                   and here<<<

//....

       for i := -3000000 to 0 do bigfoo (i);      // variable i is a longint this time.

end;
(* ---------------------------------------------------------------------------------------- *)

1) i would not need to be declared.
2) i would be a "word" the first time and a longint the 2nd time.
3) i would only exist within the for statement (including within its begin/end block)
..

Note that such a change has no impact on any existing code but would make future coding less work  (not a lot less, but ....)
I think it shall be SynEditor-Autocomplete's job. Or be CodeTool's job.


Didn't we know?, that pressing Ctrl+Shift+C will automagically write the property getter/setter into "private" class block.
plus write getter/setter methods in "implementation" block too?
So, (no matter AutoComplete or CodeTool) must be easy to able to write "var i : longint" into correct place within the procedure, somehow .
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Amir61

  • New Member
  • *
  • Posts: 19
    • http://Amir.Aavani.net
Re: Ada style "for" loop counters
« Reply #11 on: January 08, 2016, 04:11:42 am »
I like the idea.

The x2nie's suggestion does not resolve the issue!
Some variables do not deserve to be in the VAR list! They just increase the line of code and make the code less readable.

What I am not sure is why one should stop with just the loop variables: Why should not Pascal allow all the variables to be like this?  Essentially it is like the "auto" type in C++.

As I said, I like the idea but I think it needs a deep understanding of its consequences.
 

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Ada style "for" loop counters
« Reply #12 on: January 08, 2016, 04:32:51 am »
I like the idea.

The x2nie's suggestion does not resolve the issue!
Some variables do not deserve to be in the VAR list! They just increase the line of code and make the code less readable.

What I am not sure is why one should stop with just the loop variables: Why should not Pascal allow all the variables to be like this?  Essentially it is like the "auto" type in C++.
Well, there is no such things as bad idea. I respect to the any new idea.
But, if were fpc allowing a variable used without declaring the exact type, it should be in new compiler mode. 
such {$mode AlanTheBest} ,eh?
Even fpc will support this new feature, I will disabled that in my local machine very quickly.


I myself will thankful if I always noticed of "mistake" of undeclared variable. Sure, writing  var i : longint still disturbing me. But it just what I really need.





Anyway, my suggestion is based on what AlanTheBeast described by himself in first post:
Quote
Note that such a change has no impact on any existing code but would make future coding less work
So, if you want to code faster less work, let the SynEdit do that (by using autocomplete / codetool?)



Quote from: AlanTheBest
Finally, Pascal now allows the abomination of C operations like       a+ = c      albeit permitted by compiler directive (relief - may it never see my machine).

Now that is awful.  Stupid.  Insane bad style.  Just as bad as curly braces.  Of course that's not at all what I've suggested.
I didn't know when fpc allow a+=c,
but if fpc did, the variables still must be declared in exact type, aren't they?


@AlanTheBest: if you really dislike to declare variable (is it the stupid awful?), use python!
No, do't be offended. Just maybe the best you can get: never typed-variable at all. it help you less work.
« Last Edit: January 08, 2016, 04:52:54 am by x2nie »
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Ada style "for" loop counters
« Reply #13 on: January 08, 2016, 04:50:27 am »
I'd like to suggest an addition to fpc's "Pascal" definition:

         that for loop indexes could be like those in Ada - that is, do not require a
         variable declaration. They would always be word/integer (longword/longint/
         Qword/int64) as needed and would only "exist" within the for loop block.

I think suggestions are welcome. It means there are interest in improving this old language. I love Pascal, like it's, but I thik it could be better.  :)

What it surprise me, is the bad behavior some people here respond when they hear about a language suggestion. %)

Anyway this is just my humble opinion.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Ada style "for" loop counters
« Reply #14 on: January 08, 2016, 04:59:21 am »
+2 for Edson. (double thumbs).
You got very good point of view


But about people respons, there are people that have limited english vocabulary (like me).
THere is some difficult to picks english words to express the my mind.
if so, just forget and go forward 8-)
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

 

TinyPortal © 2005-2018