Lazarus

Free Pascal => Beginners => Topic started by: Joanna on April 22, 2025, 08:00:30 am

Title: [solved] macro for code ?
Post by: Joanna on April 22, 2025, 08:00:30 am
im not sure how to do in pascal.. is there a way to substitute
Code: Pascal  [Select][+][-]
  1. FOR X:= LOW(AR_CONTROLS) TO HIGH(AR_CONTROLS) DO
with something shorter that references it such as ar_loop or something. like i do with resource strings keys ?
Title: Re: macro for code ?
Post by: H₂SO₄ on April 22, 2025, 08:07:03 am
Code: Pascal  [Select][+][-]
  1. {$macro on}
  2.  
  3. {$define AR_LOOP := FOR X := LOW(AR_CONTROLS) TO HIGH(AR_CONTROLS) DO}
  4.  
  5. AR_LOOP BEGIN
  6.   // ...
  7. END;

I'd strongly prefer a  for..in  loop, though.
Title: Re: macro for code ?
Post by: Thaddy on April 22, 2025, 09:07:02 am
Joanna, you are probably missing parameterized macro's, something like
Code: Pascal  [Select][+][-]
  1. THELOOP(AR_CONTROLS);
which would iterate over any array. That is not possible in Freepascal, but what is possible is to use for in do which is a shorter notation..
Title: Re: macro for code ?
Post by: Joanna on April 22, 2025, 11:37:25 am
Thanks for the answers,
Also I’m curious what the scope of the macro is. Is it accessible from other files? Where is it declared usually?
Title: Re: macro for code ?
Post by: Thaddy on April 22, 2025, 02:18:47 pm
Macros are unit local afaik. It requires an inc file to use it in all units that want to use them in. In FreePascal macro's are by no means like something like C macro's, but depending on how you look at that it may actually be a good thing, given that C style macros are untyped and that is not very Pascallish.
That does not mean the feature is not useful, it means it is only very basic. A bit like your capslock problem... :P
(I have a DisJoannafy tool)
Title: Re: macro for code ?
Post by: Joanna on April 23, 2025, 12:46:01 am
Another question, do for in loops ever iterate backwards or are they only one way?
Title: Re: macro for code ?
Post by: Fibonacci on April 23, 2025, 01:09:32 am
Another question, do for in loops ever iterate backwards or are they only one way?

Use tricks

Code: Pascal  [Select][+][-]
  1. {$modeswitch typehelpers}
  2. {$modeswitch anonymousfunctions}
  3. {$modeswitch functionreferences}
  4.  
  5. type
  6.   TSomeTypeArray = Array of String;
  7.  
  8.   THelperCallback = reference to procedure(s: String);
  9.  
  10.   THelper = type helper for TSomeTypeArray
  11.     procedure forEach(cb: THelperCallback; reversed: Boolean=false);
  12.   end;
  13.  
  14. procedure THelper.forEach(cb: THelperCallback; reversed: Boolean=false);
  15. var
  16.   i: Integer;
  17. begin
  18.   if not reversed then
  19.     for i := 0 to high(Self) do cb(Self[i])
  20.   else
  21.     for i := high(Self) downto 0 do cb(Self[i])
  22. end;
  23.  
  24. var
  25.   a: TSomeTypeArray;
  26.  
  27. begin
  28.   a := ['first', 'second', 'third'];
  29.  
  30.   // forward
  31.   writeln('Forward:');
  32.   a.forEach(procedure(s: String)
  33.   begin
  34.     writeln(' * s = ', s);
  35.   end);
  36.  
  37.   writeln;
  38.  
  39.   // backward
  40.   writeln('Backward:');
  41.   a.forEach(procedure(s: String)
  42.   begin
  43.     writeln(' * s = ', s);
  44.   end, True{reversed});
  45.  
  46.   readln;
  47. end.

Code: [Select]
Forward:
 * s = first
 * s = second
 * s = third

Backward:
 * s = third
 * s = second
 * s = first
Title: Re: macro for code ?
Post by: ASerge on April 23, 2025, 02:55:08 am
Another question, do for in loops ever iterate backwards or are they only one way?
By default, the for in loop runs in the forward order. But it can be customized.
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$IFDEF WINDOWS}
  3.   {$APPTYPE CONSOLE}
  4. {$ENDIF}
  5. {$LONGSTRINGS ON}
  6. {$MODESWITCH TYPEHELPERS}
  7.  
  8. type
  9.   TStringArray = array of string;
  10.  
  11.   TStringArrayReverseEnumerator = class(TObject)
  12.   strict private
  13.     FOwner: TStringArray;
  14.     FIndex: SizeInt;
  15.     function GetCurrent: string;
  16.   public
  17.     constructor Create(const AOwner: TStringArray);
  18.     function GetEnumerator: TStringArrayReverseEnumerator; inline;
  19.     function MoveNext: Boolean; inline;
  20.     property Current: string read GetCurrent;
  21.   end;
  22.  
  23.   TStringArrayHelper = type helper for TStringArray
  24.     function Reverse: TStringArrayReverseEnumerator;
  25.   end;
  26.  
  27.  
  28. function TStringArrayHelper.Reverse: TStringArrayReverseEnumerator;
  29. begin
  30.   Result := TStringArrayReverseEnumerator.Create(Self);
  31. end;
  32.  
  33. constructor TStringArrayReverseEnumerator.Create(const AOwner: TStringArray);
  34. begin
  35.   FOwner := AOwner;
  36.   FIndex := Length(FOwner);
  37. end;
  38.  
  39. function TStringArrayReverseEnumerator.GetCurrent: string;
  40. begin
  41.   Result := FOwner[FIndex];
  42. end;
  43.  
  44. function TStringArrayReverseEnumerator.GetEnumerator: TStringArrayReverseEnumerator;
  45. begin
  46.   Result := Self;
  47. end;
  48.  
  49. function TStringArrayReverseEnumerator.MoveNext: Boolean;
  50. begin
  51.   Result := FIndex > 0;
  52.   if Result then
  53.     Dec(FIndex);
  54. end;
  55.  
  56. var
  57.   A: TStringArray;
  58.   S: string;
  59. begin
  60.   A := ['first', 'second', 'third'];
  61.   Writeln('Forward:');
  62.   for S in A do
  63.     Writeln(' * s = ', S);
  64.   Writeln;
  65.   Writeln('Backward:');
  66.   for S in A.Reverse do
  67.     Writeln(' * s = ', S);
  68.   Readln;
  69. end.
Title: Re: macro for code ?
Post by: Joanna on April 23, 2025, 11:22:49 am
Thanks that’s complicated  :D
Im not sure I think once heard that sets are Not really in any particular order.. I only use for in loops for Iterating sets usually.
Title: Re: [solved] macro for code ?
Post by: Thaddy on April 23, 2025, 03:23:41 pm
In set theory you are right, no particular order.
In Pascal implementations the default set construct is always in order, though.
But other sets, written in pascal, like in generics.collections need not be in order. Only the values need to be unique.
Title: Re: [solved] macro for code ?
Post by: Zoran on April 23, 2025, 05:45:01 pm

In set theory you are right, no particular order.
In Pascal implementations the default set construct is always in order, though.

No, that's plain wrong! A Pascal set is not ordered.
Arrays are ordered, sets are not.
What you probably meant to say is that the underlying type for which the set is declared has to be an ordinal type.
But the set itself has no order. It only contains some element or not.

Let's take a look at set theory - you said they are not ordered, and Pascal sets are - let's take a set of integral numbers A={1, 2, 5}.
What can we say about this set?
We can say that number 2 belongs to it, we can say that number 3 doesn't belong to it. We can notice that its elements can be compared (1<2, 1<5, 2<5), as this is a set of integral numbers, but it has nothing to do with the set itself. The set itself is not ordered, it can be written {2, 5, 1} and this is the same set.
Now, Thaddy, what is different in Pascal sets?
Title: Re: [solved] macro for code ?
Post by: TBMan on April 23, 2025, 06:06:34 pm

In set theory you are right, no particular order.
In Pascal implementations the default set construct is always in order, though.

No, that's plain wrong! A Pascal set is not ordered.
Arrays are ordered, sets are not.
What you probably meant to say is that the underlying type for which the set is declared has to be an ordinal type.
But the set itself has no order. It only contains some element or not.

Let's take a look at set theory - you said they are not ordered, and Pascal sets are - let's take a set of integral numbers A={1, 2, 5}.
What can we say about this set?
We can say that number 2 belongs to it, we can say that number 3 doesn't belong to it. We can notice that its elements can be compared (1<2, 1<5, 2<5), as this is a set of integral numbers, but it has nothing to do with the set itself. The set itself is not ordered, it can be written {2, 5, 1} and this is the same set.
Now, Thaddy, what is different in Pascal sets?



What's the difference between "construct" and elements within the set?  I think Zoran and Thaddy are discussing  two different things.
Title: Re: [solved] macro for code ?
Post by: Zoran on April 23, 2025, 11:04:56 pm
What's the difference between "construct" and elements within the set?  I think Zoran and Thaddy are discussing  two different things.

You mean that "construct" there stands for "elements within the set"?
I'd say that in cited context "default set construct" should be understood like "set as a standard structured type (https://www.freepascal.org/docs-html/current/ref/refse14.html#refsu16.html) in Pascal".


Title: Re: [solved] macro for code ?
Post by: TBMan on April 24, 2025, 12:22:24 am
What's the difference between "construct" and elements within the set?  I think Zoran and Thaddy are discussing  two different things.

You mean that "construct" there stands for "elements within the set"?
I'd say that in cited context "default set construct" should be understood like "set as a standard structured type (https://www.freepascal.org/docs-html/current/ref/refse14.html#refsu16.html) in Pascal".

Discuss it with your buddy Thaddy. I'm out.
Title: Re: [solved] macro for code ?
Post by: Joanna on April 24, 2025, 11:38:42 am
I wonder how it’s implemented though. ..
Has anyone experimented with adding numbers to a set in a particular order and then using
Code: Pascal  [Select][+][-]
  1.  for x in theSet do
Even if they come out in the same order they were put in it might be considered undefined behavior.
Title: Re: [solved] macro for code ?
Post by: Zoran on April 24, 2025, 04:12:35 pm
I wonder how it’s implemented though. ..
Has anyone experimented with adding numbers to a set in a particular order and then using
Code: Pascal  [Select][+][-]
  1.  for x in theSet do
Even if they come out in the same order they were put in it might be considered undefined behavior.

The particular order in which the elements were added to the set has surely no influence on the order which the "for in" loop chooses.

They will probably come out in ascending order -- as if you used:
Code: Pascal  [Select][+][-]
  1. var
  2.   x: TElementType;
  3. for x := low(TElementType) to high(TElementType) do begin
  4.   if x in theSet then begin
  5.     // ...
  6.   end;
  7. end;
  8.  

Because, that is the most probably how the "for in" is internally implemented.
But I would NOT rely on it.
And in my opinion, even if this order were guaranteed, it would still be better practice to use the classic for loop explicitly, because I really think that by using "for in", the programmer implicitly suggests that the order is not important.

If you need a particular order, which is neither ascending or descending (if you need the latter you can simply use "for x = High(TElementType) downto Low(TElementType)"), then you need some other approach.

One way would be to declare an array and add elements in the needed order. If the number of elements is known and constant, you can use a static array, otherwise dynamic in which case you have to take care that the needed size has been allocated before you add each element. Then some container class, such as TFPGList (https://www.freepascal.org/docs-html/current/rtl/fgl/tfpglist.html) can make things easier, or some other class from fgl unit (https://www.freepascal.org/docs-html/current/rtl/fgl/index-4.html) might be more appropriate for your needs.
Title: Re: [solved] macro for code ?
Post by: Thaddy on April 24, 2025, 05:17:16 pm
No, that's plain wrong! A Pascal set is not ordered.
A Pascal set is ordered. In all pascal dialects. A pascal set is ordered, and  behaves as an ordinal
Mathemetically a set is not ordered.
You misunderstood me.
Wrong again, in only two days..... Check your sources.....
Title: Re: [solved] macro for code ?
Post by: Zoran on April 24, 2025, 06:19:04 pm
No, that's plain wrong! A Pascal set is not ordered.
A Pascal set is ordered. In all pascal dialects. A pascal set is ordered, and  behaves as an ordinal
Mathemetically a set is not ordered.
You misunderstood me.
Wrong again, in only two days..... Check your sources.....

How is it ordered?
Of course I might be wrong, but can you take a look at what I wrote earlier (https://forum.lazarus.freepascal.org/index.php/topic,70887.msg553001.html#msg553001) and answer:
Regarding order, how does the Pascal set differs from mathematical set which you claim is not ordered (I certainly agree with that)? What makes the difference? А Pascal set has some limits which do not exist in mathematical sets -- it is always finite and cannot have more than 256 elements, the underlying type must be an ordinal type. But nothing that would make a difference regarding order.

, and  behaves as an ordinal

This is a particularly strange claim! Perhaps you can make an example which shows what do you mean, how a set behaves like an ordinal!?

The ordinals can be put in a unique order, as they are always comparable, (a<=b) or (b<=a) is always true, for any a and for any b.
For instance, let's look at numbers 2 and 5 - one of these expressions is true: 2<=5 or 5<=2.

Lets look at the Pascal sets A=[2, 3, 5]; B=[1, 2, 3, 5, 7, 9], C = [3, 5, 9].
A and B are indeed comparable (A <= B equals True), so are B and C (C <=B), but A and C are not (both A<=C and C<=A are False).
Title: Re: [solved] macro for code ?
Post by: 440bx on April 24, 2025, 06:36:39 pm
A set is unordered, for instance [a, b] = [b, a], the fact that the order of the elements differs does not affect equality.

That said, in Pascal (and likely other compilers that implement sets), every set element is enumerated.  Using that enumeration, the compiler selects the bit that represents the set element.  That is a form of ordering but, that ordering does NOT affect set equality (among other possible operations applicable to sets.)  IOW, the internal implementation does not affect the mathematical properties of sets, among them, that element order does not affect the set properties.



@Zoran,

Because Pascal enumerates the set elements and selects a bit based on that enumeration, it is fair to say that sets are internally ordered, i.e, bit 0 is always the first element, bit 1 the second and so on but, that internal ordering  does not impose an external ordering.  i.e., [a, b] still equals [b, a]



In Mathematics a set is an unordered collection of elements and FPC's implementation is true to all mathematical properties of sets. 

It's internal implementation, so far, imposes only a few limits, among them the number of elements that a set can contain.  IOW, a Pascal set has a limit (currently 256), a Mathematical one does not (e.g, set of primes.)

The current implementation also imposes limits on the values of the elements that can be members of a set, e.g, defining a set of [32767..32800] is asking for trouble because FPC does not handle a lower or upper) bound outside the 0 through 255 range. 



Title: Re: [solved] macro for code ?
Post by: alpine on April 24, 2025, 06:52:00 pm
The ordinals can be put in a unique order, as they are always comparable, (a<=b) or (b<=a) is always true, for any a and for any b.
For instance, let's look at numbers 2 and 5 - one of these expressions is true: 2<=5 or 5<=2.

Lets look at the Pascal sets A=[2, 3, 5]; B=[1, 2, 3, 5, 7, 9], C = [3, 5, 9].
A and B are indeed comparable (A <= B equals True), so are B and C (C <=B), but A and C are not (both A<=C and C<=A are False).
There is a thing called "partial order" and the inclusion operator (<=) defines such a partial order in a set, but that is true both for math and the FPC.
I'm just amazed by the topic and the debate.
Title: Re: [solved] macro for code ?
Post by: Zoran on April 24, 2025, 08:12:29 pm
There is a thing called "partial order" and the inclusion operator (<=) defines such a partial order in a set, but that is true both for math and the FPC.

Yes, each set type in Pascal is partially ordered by operation "<=" (subset, or whatever the correct term in English is), and yes that is so in both math and fpc. So we can say that sets in Pascal are ordered just as much as the sets are ordered in math - that was what I've been trying to say from the start.

Unlike this, ordinal types in Pascal are totally ordered (each two elements are comparable).

I'm just amazed by the topic and the debate.
:)

Because Pascal enumerates the set elements and selects a bit based on that enumeration, it is fair to say that sets are internally ordered, i.e, bit 0 is always the first element, bit 1 the second and so on but,

I disagree that it is fair to say. It is misleading - sets are not internally ordered.
The set is not internally represented by any particular bit. It is the members which are represented by the bits, not sets. So the members are ordered (and that's the reason why the elements' type must be ordinal), sets are not internally ordered.
How are these three sets internally ordered (shown here by the underlying bit representation):
A=10010100, B=11010111, C=00010110 ?
The answer for these is A<=B, C<=B, A and C are not comparable.

By the way, I really wanted to avoid the talk about the internal representation (because it shouldn't be relevant), but I should have known it was sooner or later unavoidable.
Title: Re: [solved] macro for code ?
Post by: TRon on April 24, 2025, 08:49:19 pm
By the way, I really wanted to avoid the talk about the internal representation (because it shouldn't be relevant), but I should have known it was sooner or later unavoidable.
+1

You should not care about that for even a split second. Imagine having to do that for every programing language in use... such a waste of time with absolutely no gain whatsoever that is unless you are compiler developer actually implementing as such.
Title: Re: [solved] macro for code ?
Post by: 440bx on April 24, 2025, 08:51:15 pm
I disagree that it is fair to say. It is misleading - sets are not internally ordered.
Not only it is fair to say, it is 100% accurate. 

The compiler has to choose a bit to represent a set element.  It is internally ordering the elements, each element is assigned a different bit, which consequently orders the elements, without that order it simply cannot represent the set.

The set is not internally represented by any particular bit.
read slowly, the sentence referred to set elements. 

It is the members which are represented by the bits, not sets.
Really ?...  you're so smart.

Good luck!
Title: Re: [solved] macro for code ?
Post by: Zoran on April 24, 2025, 09:27:22 pm

It is the members which are represented by the bits, not sets.
Really ?...  you're so smart.

Or not.

Anyway, by what you said, it is obvious that you do understand well how sets work.
That is why I find it quite strange that you said that "it's fair to say that the sets are internally ordered", when it seems quite clear that you understand that only their elements are ordered... :o

And the fact that the elements are ordered and that the type on which a set is declared must be an ordinal type is what makes such statements even more problematic, because it can lead to confusion. That is why I believe that such a statement can only be misleading, not fair.
Title: Re: [solved] macro for code ?
Post by: 440bx on April 24, 2025, 10:49:51 pm
That is why I find it quite strange that you said that "it's fair to say that the sets are internally ordered", when it seems quite clear that you understand that only their elements are ordered... :o
Looks like we're getting off track due to semantics. what's "inside" a set are its elements, my saying that sets are internally ordered means that their elements are ordered (the enumeration imposes the order.)

One thing I find "less than ideal" is the assumption that the internal representation shouldn't be taken into account or the programmer shouldn't have any knowledge of.  Nothing could be farther from reality.  It is understanding the compiler's internal representation that explains the behavior of this small program:
Code: Pascal  [Select][+][-]
  1. program _sets;
  2.  
  3. var
  4.   a, b, c : integer;
  5.  
  6. begin
  7.   a := 500;
  8.   b := 550;
  9.   c := 525;
  10.  
  11.   if c in [a..b] then writeln('c is in the set') else writeln('c is NOT in the set');
  12.  
  13.   a := 50;
  14.   b := 55;
  15.   c := 52;
  16.  
  17.   if c in [a..b] then writeln('c is in the set') else writeln('c is NOT in the set');
  18.  
  19.   readln;
  20. end.
  21.  
Of course, the code above is purposely written to demonstrate what can happen to a programmer who doesn't understand (or ignores) the internal representation of sets.

Currently, while the compiler enumerates the elements of a set, it _needs_ a direct correspondence between the enumeration and the value of the element.  That's why the first case doesn't yield the value an unsuspecting programmer would expect.

Lastly, that's just one of the traps a programmer who doesn't understand (or ignores) how sets are implemented can fall into.
Title: Re: [solved] macro for code ?
Post by: TRon on April 24, 2025, 11:15:43 pm
First rule of documentation: comprehending what it reads  :)




Title: Re: [solved] macro for code ?
Post by: Zoran on April 25, 2025, 01:14:26 am
Looks like we're getting off track due to semantics. what's "inside" a set are its elements, my saying that sets are internally ordered means that their elements are ordered (the enumeration imposes the order.)

I wouldn't say the elements are ordered in the set - the set to which the elements belong has nothing to do with their order. This order is not a property of the set. These elements got this order in their ordinal type, and it's their property which doesn't change and which they have regardless of the set.

The array orders its elements, which may or may not be of an ordinal type. Their place in the array is independent of their own type, its something they get when they are included in the array.
That's why we say that an array is an ordered structure, unlike a set.
Title: Re: [solved] macro for code ?
Post by: alpine on April 25, 2025, 09:00:25 am
There is a thing called "partial order" and the inclusion operator (<=) defines such a partial order in a set, but that is true both for math and the FPC.

Yes, each set type in Pascal is partially ordered by operation "<=" (subset, or whatever the correct term in English is), and yes that is so in both math and fpc. So we can say that sets in Pascal are ordered just as much as the sets are ordered in math - that was what I've been trying to say from the start.

Unlike this, ordinal types in Pascal are totally ordered (each two elements are comparable).
Strictly speaking, the total order requires relation to be reflexive, transitive, asymmetric and, what you have mentioned earlier, strongly connected (x<=y or y<=x). https://en.wikipedia.org/wiki/Total_order
As long as in the computer memory everything is represented by a whole numbers (exclude the recent quantum delirium) a secondary total order can be always defined by considering the N injection, i.e. everything is countable. But this is just a rethought theory, where is the practical benefit in the end?

I'm just amazed by the topic and the debate.
:)
And increasingly puzzled, we should be aware of the limitations of the sets in Pascal and ultimately use them for their intended purpose/convenience, without pointless debates. (Not a personal remark)

Really a lot of pointless pages have been written lately full of nagging on topics regarding types in FPC (to mention the Boolean). Unfortunately this forum has become quite repulsive lately.

Title: Re: [solved] macro for code ?
Post by: Zoran on April 25, 2025, 06:47:08 pm
There is a thing called "partial order" and the inclusion operator (<=) defines such a partial order in a set, but that is true both for math and the FPC.

Yes, each set type in Pascal is partially ordered by operation "<=" (subset, or whatever the correct term in English is), and yes that is so in both math and fpc. So we can say that sets in Pascal are ordered just as much as the sets are ordered in math - that was what I've been trying to say from the start.

Unlike this, ordinal types in Pascal are totally ordered (each two elements are comparable).
Strictly speaking, the total order requires relation to be reflexive, transitive, asymmetric and, what you have mentioned earlier, strongly connected (x<=y or y<=x). https://en.wikipedia.org/wiki/Total_order
As long as in the computer memory everything is represented by a whole numbers (exclude the recent quantum delirium) a secondary total order can be always defined by considering the N injection, i.e. everything is countable.
Yes, that is of course so, but...

But this is just a rethought theory, where is the practical benefit in the end?
;)

I'm just amazed by the topic and the debate.
:)
And increasingly puzzled, we should be aware of the limitations of the sets in Pascal and ultimately use them for their intended purpose/convenience, without pointless debates. (Not a personal remark)

Really a lot of pointless pages have been written lately full of nagging on topics regarding types in FPC (to mention the Boolean). Unfortunately this forum has become quite repulsive lately.

The original subject of this topic got polluted. I am sorry for taking big part in this, but I couldn't just leave Thaddy's claim ("Pascal sets are ordered") unanswered, and then "Pascal set type behaves as an ordinal type". I still think that, had this topic been left finishing with such statements, it would have been more damaged than it is now.

Title: Re: [solved] macro for code ?
Post by: alpine on April 25, 2025, 07:18:25 pm
The original subject of this topic got polluted. I am sorry for taking big part in this, but I couldn't ...
No need to be sorry, I also fall for this bait from time to time. Others have pointed out the usual pollutants here more than once, so I won't. :-[
Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 25, 2025, 08:44:48 pm
No need to be sorry, I also fall for this bait from time to time. Others have pointed out the usual pollutants here more than once, so I won't. :-[

[GUFFAW]

Actually, there's one thing I think I can usefully tack in for completeness.

Shewhosenameshallnotbespoken asked about where a (parameterless) macro can be defined: a year or so ago I experimented with putting it in the Lazarus IDE project options and was able to report that that didn't work, so macros /have/ to be defined in the context of the current unit (including included .inc files).

I'd also comment that nested macros are expanded usefully. Recursion is left as an exercise...

MarkMLl
Title: Re: [solved] macro for code ?
Post by: Joanna on April 26, 2025, 12:31:19 pm
This has certainly been an interesting discussion  :D
I suppose there has to be a way for the computer to store set elements behind the scenes. I am surprised to be told that I can iterate from lowest to highest element though. Is that true? All this time I’ve thought that only for in loop can be used for sets.
Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 26, 2025, 12:58:46 pm
This has certainly been an interesting discussion  :D
I suppose there has to be a way for the computer to store set elements behind the scenes.

Yes, and as an implementation detail- not a part of the language per se- there can be (a) a restriction on the number of elephants in a set and (b) a point below which they can be assumed to be stored sequentially (e.g. in a machine word) but above which they are not (e.g. in a hashed tree of buckets).

It is not uncommon to find CS texts that advocate wildly inefficient storage in an attempt to avoid any size limitation.

Quote
I am surprised to be told that I can iterate from lowest to highest element though. Is that true? All this time I’ve thought that only for in loop can be used for sets.

Look at it like this. Assuming something like your earlier example

Code: Pascal  [Select][+][-]
  1. for x in theSet do
  2. ...
  3.  

assume that you're actually iterating over all possible values of the (type associated with) the variable x, and then testing to see whether the value is a member of theSet.

That's OK if x is declared as a reasonably small enumeration or subrange, not at all OK if it's a large numeric type.

Finally, the ordering issue also affects databases: a lot of people are content to assume that an unindexed database will always return rows in the same order, but this is not safe and will depend both on decisions made by the query engine and on the storage mechanism being used.

MarkMLl
Title: Re: [solved] macro for code ?
Post by: 440bx on April 26, 2025, 01:20:40 pm
It's interesting that code like this:
Code: Pascal  [Select][+][-]
  1. for x in theSet do
  2. ...
  3.  
would not be possible if the set wasn't internally ordered.

but.. why would  anyone care about internals ???  ... ignorance is such a bliss!...  I hope I didn't hurt somebody's fragile feelings again...
Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 26, 2025, 01:36:31 pm
It's interesting that code like this:
Code: Pascal  [Select][+][-]
  1. for x in theSet do
  2. ...
  3.  
would not be possible if the set wasn't internally ordered.

It would be entirely possible, based on the ordering of the base type... as I said, and as Alpine said earlier.

Application of <= etc. to a set is not arithmetic, and does not imply any ordering.

MarkMLl
Title: Re: [solved] macro for code ?
Post by: Joanna on April 26, 2025, 01:53:36 pm
Thanks for the answers. It seems like there is contention About the true nature of a set. I am more concerned with actually using it as set although the inner workings of it are interesting too I don’t think it should be misused.
Title: Re: [solved] macro for code ?
Post by: 440bx on April 26, 2025, 02:23:48 pm
It would be entirely possible, based on the ordering of the base type... as I said, and as Alpine said earlier.
NO, it would not.   The base type order isn't sufficient. 

It is necessary to order the set elements the same way they are ordered in the base type.  That's what makes it work.  IOW, the compiler has to assign a specific bit to represent a specific element.  That's the ordering and, of course, the simplest and most straightforward implementation is to use the ordinal of the base as the bit index.

How often does the obvious have to be explained ????

I even posted some code somewhere that shows how the compiler orders the elements.



Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 26, 2025, 02:36:08 pm
Thanks for the answers. It seems like there is contention About the true nature of a set. I am more concerned with actually using it as set although the inner workings of it are interesting too I don’t think it should be misused.

By and large, it's safe to assume that any Pascal implementation will permit "set of char", at least until that's broken by somebody defining char to map onto some ridiculous Unicode type rather than ansichar.

I'd throw in that Modula-2 has a bitset type which is defined as mapping onto the bits of a machine word, you can rely on that being ordered but not necessarily in the direction you'd expect (some implementations call the LSB 0, others call the MSB 0).

MarkMLl
Title: Re: [solved] macro for code ?
Post by: PascalDragon on April 26, 2025, 03:34:45 pm
Thanks for the answers. It seems like there is contention About the true nature of a set. I am more concerned with actually using it as set although the inner workings of it are interesting too I don’t think it should be misused.

By and large, it's safe to assume that any Pascal implementation will permit "set of char", at least until that's broken by somebody defining char to map onto some ridiculous Unicode type rather than ansichar.

Fun fact: Current Delphi implicitly converts set of Char with Char = WideChar to set of AnsiChar together with a warning that it did this (and set of WideChar triggers an error).
Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 26, 2025, 03:38:38 pm
Fun fact: Current Delphi implicitly converts set of Char with Char = WideChar to set of AnsiChar together with a warning that it did this (and set of WideChar triggers an error).

Ouch!

MarkMLl
Title: Re: [solved] macro for code ?
Post by: TRon on April 26, 2025, 05:04:41 pm
but.. why would  anyone care about internals ???  ... ignorance is such a bliss!...  I hope I didn't hurt somebody's fragile feelings again...
Which is why real programmers read and comprehend documentation. Ignorance must indeed be such a bliss. Better is ofc to waste time with those indirect insults.

Title: Re: [solved] macro for code ?
Post by: 440bx on April 26, 2025, 05:30:51 pm
Which is why real programmers read and comprehend documentation. Ignorance must indeed be such a bliss. Better is ofc to waste time with those indirect insults.
It's not an insult, it's worse, it's a fact.  Anyone who has read documentation knows that it very rarely covers every detail, it's up to the programmer (well... I should say, some programmers) to go beyond what is documented.  Again, that's not an insult, that's a fact.

Just like, internally, FPC orders the elements that make up a set (can't say that sets are ordered because apparently it isn't obvious that it is their elements that are ordered) and it is at least good to be aware of that, because "for" loops absolutely need to have the elements they act on to be ordered for them to operate properly.

Is it really that hard to gather knowledge beyond what is documented ?  Just for the record, a lot of programmers don't find it difficult and in addition to that, some even find it rewarding. 

Let's state the obvious again, by definition sets are collections of unordered elements, HOWEVER, for performance and implementation simplicity reasons, Pascal DOES order set elements.   Is it really that hard to comprehend that ?   
Title: Re: [solved] macro for code ?
Post by: Thaddy on April 26, 2025, 07:28:21 pm
ISO 7185:
"Set types
Set types are perhaps the most radical feature of Pascal. A set type can be thought of as an array of bits indicating the presence or absence of each value in the base type:

var s: set of char;
Would declare a set containing a yes/present or no/not present indicator for each character in the computer's character set. The base type of a set must be ordinal."

Pascal sets are therefor ordered as opposed to the mathematical definition of sets. It is NOT,  as often suggested, implementation detail.
Freepascal throws an error if an attempt is made to make the set not ordered because of that. Technically it is a bitset.
At compile time it renders a note, at runrime it throws a runtime error:
Code: Pascal  [Select][+][-]
  1. program setdemo1;
  2. type
  3.   TTest1 = (a,b=4,c=3);// unordered, can't be used for sets.
  4.   TTest2 = (aa,bb,cc); // ordered
  5.   TTestSet1 = set of TTest1;
  6.   TTestSet2 = set of TTest2;
  7. var
  8.   t1:TTest1;
  9.   t2:TTest2;  
  10. begin
  11.   for t2 in TTestSet2 do writeln(t2);// OK
  12.   for t1 in TTestSet1 do writeln(T1);// runerror 107: invalid enumeration
  13. end.
Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 26, 2025, 08:47:00 pm
Yes, but the ordering is by virtue of the ordinal base type. It says nothing about the ordering of the storage.

MarkMLl
Title: Re: [solved] macro for code ?
Post by: Thaddy on April 26, 2025, 08:59:54 pm
Yes, but the ordering is by virtue of the ordinal base type. It says nothing about the ordering of the storage.

MarkMLl
The ordering is mandatory for sets and documented in ISO 7185.
As per my demo, the compiler is aware of that.
Title: Re: [solved] macro for code ?
Post by: MarkMLl on April 26, 2025, 09:16:13 pm
The ordering is mandatory for sets and documented in ISO 7185.
As per my demo, the compiler is aware of that.

Yes, but the ordering is by virtue of the ordinal base type. It says nothing about the ordering of the storage.

MarkMLl
Title: Re: [solved] macro for code ?
Post by: 440bx on April 26, 2025, 09:37:33 pm
Yes, but the ordering is by virtue of the ordinal base type. It says nothing about the ordering of the storage.

MarkMLl
It is NOT by virtue of the ordinal base type.  ONLY an ordinal type can be used because there must be a 1 to 1 correspondence between a set element and a bit that represents the set element.

For instance, the range TRANGE = 500..550 is most definitely an ordinal base type but, it is not "virtuous" enough to be used in a Pascal set (at least not an FPC one.)  Therefore: that the base type is an ordinal type is NOT SUFFICIENT, which means, there are countless ordinal base types that cannot be used in the definition of a set because for one reason or another the compiler cannot establish a 1 to 1 correspondence between the type's ordinal values and the bit indexes.

In the case of 500..550, the problem is that FPC cannot use a base other than zero.  in a range such as 0..299, the problem is that while the starting ordinal is fine, the ending one exceeds the number of elements that FPC can have in a set.  I hope that this makes it crystal clear that "ordinal base type" is totally insufficient.

In the case of FPC, the only ordinal ranges that can be used must be in the range 0..255. if it's not in there, it's not usable because FPC enumerates the set elements 0..255.  In case it is not painfully obvious yet, that means FPC orders the elements of the set.

It is the internal implementation of sets in Pascal (and FPC) that puts strict limits on the ordinal types that can be used in a set definition.

In FPC, the ordering of the set elements will always be the same 0..255 (or less), therefore the ordering is not dependent on the base type, the only thing that is dependent on the base type is the number of elements in the set.   

all this is made patently obvious by the following definitions and the corresponding compiler behavior.
Code: Pascal  [Select][+][-]
  1.  
  2. type
  3.   EnumA = (aa = 5, ab = 10, ad = 255);
  4.   EnumB = (ba = 5, bb = 10, bd = 256);  { <-- this is an ordinal type }
  5.   EnumC = (ca = 400, cb = 410);         { <-- also an ordinal type    }
  6.  
  7. var
  8.   x : set of EnumA;  { 0..255 -> no problem }
  9.  
  10.   y : set of EnumB;  { <-- no can do        }
  11.   z : set of EnumC;  { <-- no can do either }
  12.  
Title: Re: [solved] macro for code ?
Post by: Joanna on April 26, 2025, 11:14:23 pm
Quote
Is it really that hard to gather knowledge beyond what is documented ?  Just for the record, a lot of programmers don't find it difficult and in addition to that, some even find it rewarding. 
This phenomenon is not limited to programmers  :D
Title: Re: [solved] macro for code ?
Post by: TRon on April 27, 2025, 12:05:29 am
It's not an insult, it's worse, it's a fact.
I was not referring to that and you bloody well know that.

Quote
Anyone who has read documentation knows that it very rarely covers every detail, it's up to the programmer (well... I should say, some programmers) to go beyond what is documented.  Again, that's not an insult, that's a fact.
In general, yes. In this particular case, no as it is documented and the compiler has provision for it to prevent the user from making that mistake. That someone chooses to ignore it is not on my account.

Quote
Just like, internally, FPC orders the elements that make up a set (can't say that sets are ordered because apparently it isn't obvious that it is their elements that are ordered) and it is at least good to be aware of that, because "for" loops absolutely need to have the elements they act on to be ordered for them to operate properly.
The obsession with wanting to know how a byte is ordered in memory is a basic understanding of how computers work. Can be ignored and then blame the compiler for behaving exactly as described but I find it silly with a capital S.

Quote
Is it really that hard to gather knowledge beyond what is documented ?  Just for the record, a lot of programmers don't find it difficult and in addition to that, some even find it rewarding. 
No idea why you would want to gain knowledge on a topic that is behaving exactly as described. Must be silly me to just accept what is documented and expect for this example of yours to fail. But you already knew that as well, which is why these exact numbers where chosen. Still that seems to not have been enough to make you understand, instead redirect to something silly as how things are internally stored while the internal storage has nothing to do with the topic.

Quote
Let's state the obvious again, by definition sets are collections of unordered elements, HOWEVER, for performance and implementation simplicity reasons, Pascal DOES order set elements.   Is it really that hard to comprehend that ?
And yet you seem to mistake sets for ranges. And nobody should care about internal storage whatsoever, again with the exception being a compiler developer, writing a debugger or are a reverse engineer. All of which are not of any concern for an ordinary developer.

riot is an option, not a verb  :)
Title: Re: [solved] macro for code ?
Post by: 440bx on April 27, 2025, 12:19:09 am
"ordinary"... yes, that's a good description.
Title: Re: [solved] macro for code ?
Post by: TBMan on April 27, 2025, 02:34:14 am
I'm a new guy here, but how did the forum go from a sharing information for the betterment of all forum, to a "I know more than you, nah nah nah" forum?
Title: Re: [solved] macro for code ?
Post by: Joanna on April 27, 2025, 03:09:00 pm
I'm a new guy here, but how did the forum go from a sharing information for the betterment of all forum, to a "I know more than you, nah nah nah" forum?
the simple answer would be because they are allowed to.. the more complicated answer would be that they have character flaws that make them feel entitled to sneer at any code that isn’t theirs. I’ve seen the same behavior as this on a dying gaming server. Small group of not so fun people looking for fights.. something really should be done about this....
Title: Re: [solved] macro for code ?
Post by: 440bx on April 27, 2025, 04:06:39 pm
... they have character flaws that make them feel entitled to sneer at any code that isn’t theirs. I’ve seen the same behavior as this on a dying gaming server. Small group of not so fun people looking for fights.. something really should be done about this....
LOL... at least you don't hide where you're coming from.   Good one!
Title: Re: [solved] macro for code ?
Post by: Tomas Hajny on April 27, 2025, 04:24:09 pm
It seems that the discussion of the original topic is finished now, let's not continue with something not belonging here... The topic is locked now.
TinyPortal © 2005-2018