From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Subtypes and type invariants in Ada2012 Date: Mon, 21 Aug 2017 18:52:16 +0200 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: MajGvm9MbNtGBKE7r8NgYA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:47770 Date: 2017-08-21T18:52:16+02:00 List-Id: 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. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de