comp.lang.ada
 help / color / mirror / Atom feed
From: Victor Porton <porton@narod.ru>
Subject: Re: Subtypes and type invariants in Ada2012
Date: Mon, 21 Aug 2017 20:41:29 +0300
Date: 2017-08-21T20:41:29+03:00	[thread overview]
Message-ID: <onf60j$j60$1@gioia.aioe.org> (raw)
In-Reply-To: onf342$dsg$1@gioia.aioe.org

Dmitry A. Kazakov wrote:

> On 2017-08-21 18:29, Victor Porton wrote:
>> Dmitry A. Kazakov wrote:
>> 
>>> On 2017-08-21 17:38, Victor Porton wrote:
>>>
>>>> So, my question: Is it possible to restrict a subtype with something
>>>> like a type invariant?
>>>
>>> Type invariant is not a restriction of public interface/properties, it
>>> is an implementation detail. So it should never produce a [sub]type.
>>>
>>>> I want a subtype restricted by an arbitrary condition. Is it possible?
>>>
>>> Arbitrary condition breaks type operations. E.g. condition Is_Prime (X)
>>> breaks more or less everything (assuming covariance). All restrictions
>>> do. A few don't break much, e.g. range constraints.
>> 
>> I don't get. What do you mean saying "breaks"?
> 
>     Prime + Prime not in Prime
> 
> Properties of the set of prime numbers are not ones of the set of
> integer numbers.
> 
>> For me the condition Is_Prime added to a subtype of Natural seems quite
>> natural.
> 
> The new set loses properties of Natural.
> 
>     Natural + 1 in Natural
> 
> is false for Prime.
> 
>> It is quite natural to have a variable which can hold only prime
>> numbers. This may appear in some math software.
> 
> Subtyping is not about individual values it is about sets of values as a
> whole. Properties of prime numbers require a new type with other
> operations. E.g.
> 
>     Prime + Prime return Natural (contra-variant)
> 
> Then
> 
>     X : Prime := 13;
>     Y : Prime := X + 1; -- Type error (not a subtype!)
>     Z : Natural := X + 1; -- OK
> 
>> I would propose to add public subtype invariants (not only private tagged
>> type invariants as now) to Ada2020. Anything particular against this?
> 
> It would not make sense.

Maybe it makes sense to allow subtype predicates, but make the subtype with 
a predicate no more a scalar (even if its base is a scalar)?

We could define "prescalars". Prime numbers would be an example of 
prescalars.

Prescalars would have some but not all properties of scalars. For example 
prescalars would have base range but no range.

The hardest part here seems to define such operations as

Prime + Prime return Natural

Let us discuss it. I think, something can be invented.

For example, the operations returning the base type of a prescalar could be 
automatically defined.

-- 
Victor Porton - http://portonvictor.org


  reply	other threads:[~2017-08-21 17:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-21 15:38 Subtypes and type invariants in Ada2012 Victor Porton
2017-08-21 16:10 ` J-P. Rosen
2017-08-21 16:18 ` Dmitry A. Kazakov
2017-08-21 16:29   ` Victor Porton
2017-08-21 16:52     ` Dmitry A. Kazakov
2017-08-21 17:41       ` Victor Porton [this message]
2017-08-21 19:55         ` Dmitry A. Kazakov
replies disabled

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