comp.lang.ada
 help / color / mirror / Atom feed
* How to best make a custom range?
@ 2019-11-04 17:26 Andrew Shvets
  2019-11-04 17:56 ` Shark8
  2019-11-04 18:34 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 16+ messages in thread
From: Andrew Shvets @ 2019-11-04 17:26 UTC (permalink / raw)


Lets say I have the following code:

subtype Test_Char is Character range ‘A’ .. ‘Z’;

But what if I wanted to include ‘&’, ‘@‘ and ‘?’ in this custom range of characters as well?  I thought of doing the following, but this obviously failed:

subtype Test_Char is Character range ‘@‘ | ‘&’ | ‘?’ | ‘A’ .. ‘Z’;

Or is this impossible unless I use a different approach?


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 17:26 How to best make a custom range? Andrew Shvets
@ 2019-11-04 17:56 ` Shark8
  2019-11-04 18:49   ` Andrew Shvets
  2019-11-04 19:16   ` Shark8
  2019-11-04 18:34 ` Dmitry A. Kazakov
  1 sibling, 2 replies; 16+ messages in thread
From: Shark8 @ 2019-11-04 17:56 UTC (permalink / raw)


On Monday, November 4, 2019 at 10:26:22 AM UTC-7, Andrew Shvets wrote:
> Lets say I have the following code:
> 
> subtype Test_Char is Character range ‘A’ .. ‘Z’;
> 
> But what if I wanted to include ‘&’, ‘@‘ and ‘?’ in this custom range of characters as well?  I thought of doing the following, but this obviously failed:
> 
> subtype Test_Char is Character range ‘@‘ | ‘&’ | ‘?’ | ‘A’ .. ‘Z’;
> 
> Or is this impossible unless I use a different approach?


The approach I've been going with is splitting things apart into parts:

  Subtype Upper  is Character range 'A'..'Z';
  Subtype Lower  is Character range 'a'..'z';
  Subtype Symbol is Character
    with Static_Predicate => '&' | '@' | '?';

  Subtype Test_Character is Character
    with Static_Predicate => Upper | Lower | Symbol;


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 17:26 How to best make a custom range? Andrew Shvets
  2019-11-04 17:56 ` Shark8
@ 2019-11-04 18:34 ` Dmitry A. Kazakov
  2019-11-04 18:49   ` Andrew Shvets
  1 sibling, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2019-11-04 18:34 UTC (permalink / raw)


On 2019-11-04 18:26, Andrew Shvets wrote:
> Lets say I have the following code:
> 
> subtype Test_Char is Character range ‘A’ .. ‘Z’;
> 
> But what if I wanted to include ‘&’, ‘@‘ and ‘?’ in this custom range of characters as well?

It is not range, it is an arbitrary subset. Differently to a range it 
would lose most of operations, where a range loses only a few.

> Or is this impossible unless I use a different approach?

What do you want from this type? Usually it is is-in test and 
enumeration. For that purpose discrete sets are just OK.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 18:34 ` Dmitry A. Kazakov
@ 2019-11-04 18:49   ` Andrew Shvets
  2019-11-04 18:55     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 16+ messages in thread
From: Andrew Shvets @ 2019-11-04 18:49 UTC (permalink / raw)


Mostly to have a set of values that I can select at random.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 17:56 ` Shark8
@ 2019-11-04 18:49   ` Andrew Shvets
  2019-11-04 19:16   ` Shark8
  1 sibling, 0 replies; 16+ messages in thread
From: Andrew Shvets @ 2019-11-04 18:49 UTC (permalink / raw)


Oh, that is cool.  Thank you!


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 18:49   ` Andrew Shvets
@ 2019-11-04 18:55     ` Dmitry A. Kazakov
  2019-11-04 19:13       ` Shark8
  0 siblings, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2019-11-04 18:55 UTC (permalink / raw)


On 2019-11-04 19:49, Andrew Shvets wrote:
> Mostly to have a set of values that I can select at random.

That is not subtype, which is the point. If it were made a "subtype" as 
was suggested per misuse of dynamic predicate, then that would break 
most of the code by polluting it with Constraint_Error raised in most 
unexpected places. Which is the reason why it should not be a subtype, 
because it is not (see LSP).

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 18:55     ` Dmitry A. Kazakov
@ 2019-11-04 19:13       ` Shark8
  2019-11-04 21:41         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 16+ messages in thread
From: Shark8 @ 2019-11-04 19:13 UTC (permalink / raw)


On Monday, November 4, 2019 at 11:55:42 AM UTC-7, Dmitry A. Kazakov wrote:
> On 2019-11-04 19:49, Andrew Shvets wrote:
> > Mostly to have a set of values that I can select at random.
> 
> That is not subtype, which is the point. If it were made a "subtype" as 
> was suggested per misuse of dynamic predicate,
It's not Dynamic_Predicate, it's Static_Predicate.

> then that would break 
> most of the code by polluting it with Constraint_Error raised in most 
> unexpected places.
Constraint_Error is the normal/expected exception, perhaps you are thinking Assertion_Error?

> Which is the reason why it should not be a subtype, 
> because it is not (see LSP).
Everywhere that Character is requested, a Test_Character is valid -- Just like everywhere Integer is requested, Natural is valid.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 17:56 ` Shark8
  2019-11-04 18:49   ` Andrew Shvets
@ 2019-11-04 19:16   ` Shark8
  2019-11-05 14:02     ` Andrew Shvets
  1 sibling, 1 reply; 16+ messages in thread
From: Shark8 @ 2019-11-04 19:16 UTC (permalink / raw)


On Monday, November 4, 2019 at 10:56:15 AM UTC-7, Shark8 wrote:
> On Monday, November 4, 2019 at 10:26:22 AM UTC-7, Andrew Shvets wrote:
> > Lets say I have the following code:
> > 
> > subtype Test_Char is Character range ‘A’ .. ‘Z’;
> > 
> > But what if I wanted to include ‘&’, ‘@‘ and ‘?’ in this custom range of characters as well?  I thought of doing the following, but this obviously failed:
> > 
> > subtype Test_Char is Character range ‘@‘ | ‘&’ | ‘?’ | ‘A’ .. ‘Z’;
> > 
> > Or is this impossible unless I use a different approach?
> 
> 
> The approach I've been going with is splitting things apart into parts:
> 
>   Subtype Upper  is Character range 'A'..'Z';
>   Subtype Lower  is Character range 'a'..'z';
>   Subtype Symbol is Character
>     with Static_Predicate => '&' | '@' | '?';
> 
>   Subtype Test_Character is Character
>     with Static_Predicate => Upper | Lower | Symbol;
Ah, I forgot the SUBTYPE_NAME IN check, as well as the PREDICATE_FAILURE aspect.
Try:

   Subtype Upper  is Character range 'A'..'Z';
   Subtype Lower  is Character range 'a'..'z';
   Subtype Symbol is Character
     with Static_Predicate => Symbol in '&' | '@' | '?';
 
   Subtype Test_Character is Character
      with Static_Predicate  => Test_Character in Upper | Lower | Symbol,
           Predicate_Failure => Raise Constraint_Error;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 19:13       ` Shark8
@ 2019-11-04 21:41         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 16+ messages in thread
From: Dmitry A. Kazakov @ 2019-11-04 21:41 UTC (permalink / raw)


On 2019-11-04 20:13, Shark8 wrote:
> On Monday, November 4, 2019 at 11:55:42 AM UTC-7, Dmitry A. Kazakov wrote:
>> On 2019-11-04 19:49, Andrew Shvets wrote:
>>> Mostly to have a set of values that I can select at random.
>>
>> That is not subtype, which is the point. If it were made a "subtype" as
>> was suggested per misuse of dynamic predicate,
> It's not Dynamic_Predicate, it's Static_Predicate.
> 
>> then that would break
>> most of the code by polluting it with Constraint_Error raised in most
>> unexpected places.
> Constraint_Error is the normal/expected exception, perhaps you are thinking Assertion_Error?

No difference. So long an unanticipated exception is propagated it is a 
bug. Any new dynamic check is a bug.

>> Which is the reason why it should not be a subtype,
>> because it is not (see LSP).
> Everywhere that Character is requested, a Test_Character is valid

Try

    Lookup : array (Test_Character) of Boolean := (others => False);

or

    procedure Next (X : in out Character) is
    begin
       X := Character'Succ (X);
    end Next;

in case you tried to implement arrays indexed by arbitrary subsets...

> Just like everywhere Integer is requested, Natural is valid.

Consider:

    procedure Negate (Value : in out Integer);

Natural is valid there only when 0.

Yes, the type system will pass it through, and this is the problem. You 
do not known in advance which code will change its behavior. With proper 
ranges it is doable to foresee most of the cases especially because base 
types are ranges too. Arbitrary constraints break arbitrary pieces of code.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-04 19:16   ` Shark8
@ 2019-11-05 14:02     ` Andrew Shvets
  2019-11-05 15:10       ` Shark8
  2019-11-05 17:14       ` Dmitry A. Kazakov
  0 siblings, 2 replies; 16+ messages in thread
From: Andrew Shvets @ 2019-11-05 14:02 UTC (permalink / raw)


This is what I have now.

subtype DNA_Chars is Character range ‘A’ .. ‘Z’;
subtype DNA_Symbols is Character with Static_Predicate => DNA_Symbols in ‘ ‘;
subtype DNA_Char is Character with Static_Predicate => DNA_Char in DNA_Chars | DNA_Symbols;

And this is the warning that I get (one of many, but I can’t copy and paste):

warning: in instantiation at a-nudira.adb:54
type “Result_Subtype” has predicates, attribute “First” not allowed


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-05 14:02     ` Andrew Shvets
@ 2019-11-05 15:10       ` Shark8
  2019-11-08 15:55         ` AdaMagica
  2019-11-05 17:14       ` Dmitry A. Kazakov
  1 sibling, 1 reply; 16+ messages in thread
From: Shark8 @ 2019-11-05 15:10 UTC (permalink / raw)


On Tuesday, November 5, 2019 at 7:02:37 AM UTC-7, Andrew Shvets wrote:
> This is what I have now.
> 
> subtype DNA_Chars is Character range ‘A’ .. ‘Z’;
> subtype DNA_Symbols is Character with Static_Predicate => DNA_Symbols in ‘ ‘;
> subtype DNA_Char is Character with Static_Predicate => DNA_Char in DNA_Chars | DNA_Symbols;
> 
> And this is the warning that I get (one of many, but I can’t copy and paste):
> 
> warning: in instantiation at a-nudira.adb:54
> type “Result_Subtype” has predicates, attribute “First” not allowed

OK, the problem here is that the Ada language *does* discriminate between subtypes with predicates and those without -- mostly because there are arguments about how such attributes should behave. Things like if we have:
   TYPE Digits is range 0..9;
   SUBTYPE Odds is Digits with Static_Predicate => Odds in 1|3|5|7|9;
what should Odds'Pred(1) be? 0? Constraint_Error?
what about Odds'Pos(1) should it be 0, the first item of the subtype? Or 1, the position in the parent-type?


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-05 14:02     ` Andrew Shvets
  2019-11-05 15:10       ` Shark8
@ 2019-11-05 17:14       ` Dmitry A. Kazakov
  2019-11-05 17:28         ` Shark8
  1 sibling, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2019-11-05 17:14 UTC (permalink / raw)


On 2019-11-05 15:02, Andrew Shvets wrote:
> This is what I have now.
> 
> subtype DNA_Chars is Character range ‘A’ .. ‘Z’;
> subtype DNA_Symbols is Character with Static_Predicate => DNA_Symbols in ‘ ‘;
> subtype DNA_Char is Character with Static_Predicate => DNA_Char in DNA_Chars | DNA_Symbols;
> 
> And this is the warning that I get (one of many, but I can’t copy and paste):
> 
> warning: in instantiation at a-nudira.adb:54
> type “Result_Subtype” has predicates, attribute “First” not allowed

Because ordering attributes are ones that get broken either way. Outside 
generics the language threats them contravariant [the result is of the 
base subtype], which breaks ordering but keeps much of other semantics. 
When you pass a subtype as an actual parameter to a generic it suddenly 
becomes covariant [the result of the subtype], which is sometimes worse, 
sometimes quite impossible to implement. Ada plays safe here and just 
does not let you. In other cases you might not be so lucky. The language 
cannot deduce right semantics from the constraint. It is undecidable, 
incomputable etc. In short, do not do that.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-05 17:14       ` Dmitry A. Kazakov
@ 2019-11-05 17:28         ` Shark8
  0 siblings, 0 replies; 16+ messages in thread
From: Shark8 @ 2019-11-05 17:28 UTC (permalink / raw)


On Tuesday, November 5, 2019 at 10:14:57 AM UTC-7, Dmitry A. Kazakov wrote:
> 
> Because ordering attributes are ones that get broken either way.
And I was explaining the how/why.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-05 15:10       ` Shark8
@ 2019-11-08 15:55         ` AdaMagica
  2019-11-08 16:07           ` AdaMagica
  2019-11-08 22:28           ` Randy Brukardt
  0 siblings, 2 replies; 16+ messages in thread
From: AdaMagica @ 2019-11-08 15:55 UTC (permalink / raw)


Am Dienstag, 5. November 2019 16:10:46 UTC+1 schrieb Shark8:
>    TYPE Digits is range 0..9;
>    SUBTYPE Odds is Digits with Static_Predicate => Odds in 1|3|5|7|9;
> what should Odds'Pred(1) be? 0? Constraint_Error?
> what about Odds'Pos(1) should it be 0, the first item of the subtype? Or 1, the position in the parent-type?

Attributes work on the base type, e.g.

Natural'Pred (0) = -1

Constraints on the subtype are ignored, Digit'Pred and Odds'Pred are identical. Thus:

Odds'Pred (8) = 7  -- 8-1
Odds'Pos (2) = 2  -- result is universal_integer

Of course, this will raise an exception:

X: Odds := Odds'Pred (7);  -- 6 not in Odds
Y: Odds := Odds'Pos (8);   -- universal_integer 8 converted into Digits not in Odds

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-08 15:55         ` AdaMagica
@ 2019-11-08 16:07           ` AdaMagica
  2019-11-08 22:28           ` Randy Brukardt
  1 sibling, 0 replies; 16+ messages in thread
From: AdaMagica @ 2019-11-08 16:07 UTC (permalink / raw)


Am Freitag, 8. November 2019 16:55:16 UTC+1 schrieb AdaMagica:
> Attributes work on the base type, e.g.

See RM 3.5(25-27)

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How to best make a custom range?
  2019-11-08 15:55         ` AdaMagica
  2019-11-08 16:07           ` AdaMagica
@ 2019-11-08 22:28           ` Randy Brukardt
  1 sibling, 0 replies; 16+ messages in thread
From: Randy Brukardt @ 2019-11-08 22:28 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:bbdac1c2-53e9-4b26-a9da-580c988b6b1d@googlegroups.com...
> Am Dienstag, 5. November 2019 16:10:46 UTC+1 schrieb Shark8:
>>    TYPE Digits is range 0..9;
>>    SUBTYPE Odds is Digits with Static_Predicate => Odds in 1|3|5|7|9;
>> what should Odds'Pred(1) be? 0? Constraint_Error?
>> what about Odds'Pos(1) should it be 0, the first item of the subtype? Or 
>> 1, the position in the parent-type?
>
> Attributes work on the base type, e.g.
>
> Natural'Pred (0) = -1

Right, but it's a case-by-case thing as to whether an attribute applies to 
the type or to the subtype. The ones you're talking about apply to the type 
(not any subtype), but 'First and 'Last apply to the subtype.

                                             Randy.




^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2019-11-08 22:28 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-04 17:26 How to best make a custom range? Andrew Shvets
2019-11-04 17:56 ` Shark8
2019-11-04 18:49   ` Andrew Shvets
2019-11-04 19:16   ` Shark8
2019-11-05 14:02     ` Andrew Shvets
2019-11-05 15:10       ` Shark8
2019-11-08 15:55         ` AdaMagica
2019-11-08 16:07           ` AdaMagica
2019-11-08 22:28           ` Randy Brukardt
2019-11-05 17:14       ` Dmitry A. Kazakov
2019-11-05 17:28         ` Shark8
2019-11-04 18:34 ` Dmitry A. Kazakov
2019-11-04 18:49   ` Andrew Shvets
2019-11-04 18:55     ` Dmitry A. Kazakov
2019-11-04 19:13       ` Shark8
2019-11-04 21:41         ` Dmitry A. Kazakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox