comp.lang.ada
 help / color / mirror / Atom feed
From: "Peter C. Chapin" <pchapin@sover.net>
Subject: Re: Subtype conformance... not what I was expecting.
Date: Wed, 26 Jul 2006 10:32:20 -0400
Date: 2006-07-26T10:32:20-04:00	[thread overview]
Message-ID: <44c77cf0$0$2917$4d3efbfe@news.sover.net> (raw)
In-Reply-To: <mo3rtwrhvvqv$.10l5wotai1a7h$.dlg@40tude.net>

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



  reply	other threads:[~2006-07-26 14:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-26  3:02 Subtype conformance... not what I was expecting Peter C. Chapin
2006-07-26  6:58 ` Craig Carey
2006-07-26  7:54 ` Dmitry A. Kazakov
2006-07-26 14:32   ` Peter C. Chapin [this message]
2006-07-26 15:38     ` Georg Bauhaus
2006-07-27  0:02       ` Peter C. Chapin
2006-07-26 18:40     ` Dmitry A. Kazakov
2006-07-27  0:00       ` Peter C. Chapin
2006-07-26  8:06 ` Georg Bauhaus
2006-07-26 14:15   ` Peter C. Chapin
2006-07-26 21:23 ` Adam Beneschan
replies disabled

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