|
From: Georg Bauhaus on 1 Sep 2005 07:48 Florian Weimer wrote: > Even more problematic is the empty array whose index type > is an enumeration type with just one enumeration literal. This looks like a formalist's problem to me. Use subtypes: type With_Off is (Off, Only); -- two literals subtype The_King is With_Off range Only .. Only; -- one literal left_the_building: array (The_King'first .. The_King'base'first) of Boolean;
From: Lutz Donnerhacke on 1 Sep 2005 08:02 * Georg Bauhaus wrote: > Florian Weimer wrote: >> Even more problematic is the empty array whose index type >> is an enumeration type with just one enumeration literal. > > This looks like a formalist's problem to me. Use subtypes: > > type With_Off is (Off, Only); -- two literals > subtype The_King is With_Off range Only .. Only; -- one literal You formally introduces a base type. But even this subtype fails to construct an empty array starting on Off. Without the base type an empty array can't specified: 1 with Ada.Text_IO; 2 use Ada.Text_IO; 3 4 procedure t is 5 type Offset is (Single); 6 type Test_Array is array (Offset range <>) of Integer; 7 empty : Test_Array(Offset'First .. Offset'Base'Pred(Offset'First)); 8 begin 9 Put_Line("empty'First = " & Offset'Image(empty'First)); 10 Put_Line("empty'Last = " & Offset'Image(empty'Last)); 11 end; gcc -c t.adb t.adb:7:49: Pred of "Offset'First" t.adb:7:49: static expression raises "Constraint_Error" gnatmake: "t.adb" compilation error
From: Georg Bauhaus on 1 Sep 2005 09:01 Lutz Donnerhacke wrote: > * Georg Bauhaus wrote: > >>Florian Weimer wrote: >> >>>Even more problematic is the empty array whose index type >>>is an enumeration type with just one enumeration literal. >> >>This looks like a formalist's problem to me. Use subtypes: >> >> type With_Off is (Off, Only); -- two literals >> subtype The_King is With_Off range Only .. Only; -- one literal > > > You formally introduces a base type. But even this subtype fails to > construct an empty array starting on Off. This looks like a formalist's problem to me. Computer types have limited value sets, infinite types are an illusion, useful but not real. If the value is outside the range of values that can be realised inside a computer, you cannot make the value part of a terminating computation that does reference the value. So why not explicitly identify limits, if you work near the limits? One use of 'base'first, where 'base'first < 'first, is in algorithms that never reference the value at index 'base'first. Like in some Ada.Containers algorithms. In Ada.Strings.*, index value 0 is off bounds, but useful, and chosen explicitly. type String is array (Positive range <>) of Character; subtype Empty_String is String (1 .. 0); subtype Empty_String_Verbose is String (Positive'first .. Positive'pred(Positive'first));
From: Dmitry A. Kazakov on 1 Sep 2005 09:59 On Thu, 01 Sep 2005 13:42:37 +0200, Georg Bauhaus wrote: > Dmitry A. Kazakov wrote: > >> A more interesting question is why Empty'First does not raise any >> exception. > > Because 'First does not denote an element? Because it does not exist. Consider a discrete type with one or even no elements. >> After all, there is no any lower bound of an empty index range. > > ? What's the bound of a range? (as opposed to a bounded array) The bound of the index range. Array itself has no any bounds, its index may have them. >> Provided, that empty arrays are all same, of course. If not, then another >> interesting question would appear: how to make an empty array with the >> lower bound Integer'First? > > Carrying the arguement further, how to reference an element > outside the machine's storage? This way: raise Constraint_Error; -- (:-)) -------------- The problem with the current (Ada 83) design is that it tries to abstract away trivial mathematical facts: 1. Not all sets have bounds 2. Even if a set is bounded it does not necessarily contain the bound. Further trivial fact: 3. [a, a-1] is not an empty set. It might be an interval of negative length, whatever meaning that may have. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de
From: Georg Bauhaus on 1 Sep 2005 11:36
Dmitry A. Kazakov wrote: > -------------- > The problem with the current (Ada 83) design is that it tries to abstract > away trivial mathematical facts: As does a computer :-) |