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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,628d2a493f1e203d X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news-out.readnews.com!news-xxxfer.readnews.com!not-for-mail Date: Wed, 26 Jul 2006 10:32:20 -0400 From: "Peter C. Chapin" User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Subtype conformance... not what I was expecting. References: <44c6db66$0$2928$4d3efbfe@news.sover.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <44c77cf0$0$2917$4d3efbfe@news.sover.net> Organization: SoVerNet (sover.net) NNTP-Posting-Host: a4e5ade8.news.sover.net X-Trace: DXC=bBhnA7BfNcIb5X7jJYA1FBK6_LM2JZB_C_D`nC@WjE7G:WUUlR<856OZ[5fVo[S`ZMa52[L>OclON X-Complaints-To: abuse@sover.net Xref: g2news2.google.com comp.lang.ada:5939 Date: 2006-07-26T10:32:20-04:00 List-Id: Dmitry A. Kazakov wrote: >> procedure Check is >> subtype Narrow is Integer range -10..10; >> type Function_Ptr is access function(X : Narrow) return Integer; >> >> function F(Para : Integer) return Narrow is >> begin >> return Para; >> end F; >> >> G : Function_Ptr := F'Access; >> Result : Integer; >> begin >> Result := G(0); >> end Check; >> [snip] > The semantic of "subtype" in Ada is "same type." So if you allow Narrow to > appear in place of Integer, you must also allow the reverse: > > subtype Narrow is Integer range -10..10; > type Function_Ptr is access function(X : Narrow) return Narrow; > function F(Para : Integer) return Integer; > -- Constraint_Error-unsafe > > If you wanted a one-way road, you'd need function(X : Narrow) return > Integer be an override of some primitive subprogram of Integer. That would > make you able to legally judge about conformance to *class* (Narrow <: > Integer). But that works for only operations defined on the class. > Unfortunately Ada does not have either Integer'Class or Narrow'Class. Hmmm. I'll have to think about this a little; I don't quite follow you right now. Consider this subtype Narrow is Integer range -10..10; X : Integer; Y : Narrow; ... X := Y; Y := X; -- Might raise Constraint_Error. Even though one statement is Constraint_Error-unsafe, both are legal. However, I don't think this is the point I was trying to make. When I do G := F'Access (in my original example) it is type safe in that I can't do anything with G that would cause a problem for F. I might get a Constraint_Error when G's arguments are evaluated or when G's return value is used but no such error can occur because the actual underlying function is actually F. Using the notation common in functional languages, let F : t1 -> t2 and G : t1' -> t2'. Then t1 -> t2 is a subtype of t1' -> t2' (that is, t1->t2 <: t1'->t2') iff t2 <: t2' and t1' <: t1. In this case I realize that G is an access type but it "feels" like a function when it is used. Since I'm trying to use F where G is expected and since F and G have the proper subtype relationship to each other one might suppose that the above rule would be obeyed. Peter