comp.lang.ada
 help / color / mirror / Atom feed
* Question about default discriminants and mutable objects.
@ 2010-05-06  0:41 Peter C. Chapin
  2010-05-06  1:26 ` Randy Brukardt
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Peter C. Chapin @ 2010-05-06  0:41 UTC (permalink / raw)


Hopefully I can ask this question clearly...

I understand that an instance of a type with default discriminants can be
mutated (here I mean have its discriminant changed) via assignment. It seems
like the most effective way for a compiler to support that ability is to
always allocate enough memory for the object to account for all possible
discriminant values. Mordechai Ben-Ari pretty much says this explicitly in
his book "Ada for Software Engineers."

I also understand that an instance of a type without default discriminants
can't be mutated in this way (that is, by assignment). If a new value is
assigned to the object with the wrong discriminant the result is
Constraint_Error. This would allow the compiler to allocate just the memory
necessary for that particular discriminant value used to initialize the
object since there would never be a (successful) attempt to stuff a larger
object into that space.

It seems like conceptually the issue of default discriminants and mutability
(in the sense I mean here) are independent. One could imagine some currently
non-existent syntax that would allow the programmer to mark a type
declaration so that the compiler allowed discriminant values to be changed
via assignment without leaning on the mechanism of default discriminants.
Furthermore one could imagine treating default discriminants as 100%
syntactic sugar and not endowing them with any special semantics regarding
mutability.

Okay, so my question is: what was the rationale behind combining the notion of
default discriminants with the notion of mutability? Is there some subtle
technical reason why they must go together? I don't have pressing need to
know... I'm curious and I'm trying to deepen my understanding of this topic.

Thanks!

Peter




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

* Re: Question about default discriminants and mutable objects.
  2010-05-06  0:41 Question about default discriminants and mutable objects Peter C. Chapin
@ 2010-05-06  1:26 ` Randy Brukardt
  2010-05-06  4:07 ` Gene
  2010-05-06 15:15 ` Dmitry A. Kazakov
  2 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2010-05-06  1:26 UTC (permalink / raw)


"Peter C. Chapin" <pcc482719@gmail.com> wrote in message 
news:4be20f28$0$2431$4d3efbfe@news.sover.net...
...
> Okay, so my question is: what was the rationale behind combining the 
> notion of
> default discriminants with the notion of mutability? Is there some subtle
> technical reason why they must go together? I don't have pressing need to
> know... I'm curious and I'm trying to deepen my understanding of this 
> topic.

I don't know; I know the idea come from Ada 83, but I have no idea why they 
chose that.

It's been a long-time annoyance in Ada that these two concepts are 
intertwined. (It's very much like the annoying requirement that a protected 
function do only read-only things to the protected object, while a protected 
procedure has to be assumed to modify the protected object (neither are true 
in general, of course).)

> I understand that an instance of a type with default discriminants can be
> mutated (here I mean have its discriminant changed) via assignment. It 
> seems
> like the most effective way for a compiler to support that ability is to
> always allocate enough memory for the object to account for all possible
> discriminant values. Mordechai Ben-Ari pretty much says this explicitly in
> his book "Ada for Software Engineers."

I personally think it was a mistake that this implementation was allowed, 
since it makes many useful discriminated types impossible or excessively 
expensive in space. Janus/Ada allocates discriminant-dependent components to 
size, and thus reallocates the components if a discriminant is changed. That 
of course has other problems (mostly in overhead if the objects are assigned 
often).

Most everyone disagrees with my position on this, the reasoning being that 
the extra memory allocation overhead isn't justified on real-time systems.

Probably there is some happy medium using a combination of both 
implementations (clearly some real-time systems couldn't allow a 
reallocating implementation, but that is what we have pragma Restrictions 
for; for other uses, a more flexible implementation is better, IMHO).

                                    Randy.





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

* Re: Question about default discriminants and mutable objects.
  2010-05-06  0:41 Question about default discriminants and mutable objects Peter C. Chapin
  2010-05-06  1:26 ` Randy Brukardt
@ 2010-05-06  4:07 ` Gene
  2010-05-06  4:56   ` AdaMagica
  2010-05-06 15:15 ` Dmitry A. Kazakov
  2 siblings, 1 reply; 9+ messages in thread
From: Gene @ 2010-05-06  4:07 UTC (permalink / raw)


On May 5, 8:41 pm, "Peter C. Chapin" <pcc482...@gmail.com> wrote:
> Hopefully I can ask this question clearly...
>
> I understand that an instance of a type with default discriminants can be
> mutated (here I mean have its discriminant changed) via assignment. It seems
> like the most effective way for a compiler to support that ability is to
> always allocate enough memory for the object to account for all possible
> discriminant values. Mordechai Ben-Ari pretty much says this explicitly in
> his book "Ada for Software Engineers."
>
> I also understand that an instance of a type without default discriminants
> can't be mutated in this way (that is, by assignment). If a new value is
> assigned to the object with the wrong discriminant the result is
> Constraint_Error. This would allow the compiler to allocate just the memory
> necessary for that particular discriminant value used to initialize the
> object since there would never be a (successful) attempt to stuff a larger
> object into that space.
>
> It seems like conceptually the issue of default discriminants and mutability
> (in the sense I mean here) are independent. One could imagine some currently
> non-existent syntax that would allow the programmer to mark a type
> declaration so that the compiler allowed discriminant values to be changed
> via assignment without leaning on the mechanism of default discriminants.
> Furthermore one could imagine treating default discriminants as 100%
> syntactic sugar and not endowing them with any special semantics regarding
> mutability.
>
> Okay, so my question is: what was the rationale behind combining the notion of
> default discriminants with the notion of mutability? Is there some subtle
> technical reason why they must go together? I don't have pressing need to
> know... I'm curious and I'm trying to deepen my understanding of this topic.
>
I don't have any special knowledge, and I also believe there is no
really good reason for the connection.

But I've always satisfied my own curiosity about this with the
rationale that it's all about declarations that look like

x : Foo;

where Foo is a discriminated type.

For Foo to be well-defined, it must have a default descriminant
value.  Otherwise it is no type at all.

Yet the syntax here connotes that any value of type Foo ought to be
assignable to x in the future.  It would be an opaque gotcha if a
future assignment to x with a value having other than the default
discriminant caused a run-time exception.

The conclusion is that x ought to be mutable.  Viola!  Discriminated
types with default discriminant values are mutable.

And if you're going to admit non-mutable discriminant types at all,
then it's something like logical for all the rest to be these.

Tortured, I agree...  One can almost hear the design-by-committee in
progress.




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

* Re: Question about default discriminants and mutable objects.
  2010-05-06  4:07 ` Gene
@ 2010-05-06  4:56   ` AdaMagica
  2010-05-06 14:59     ` Adam Beneschan
  2010-05-07  0:47     ` Peter C. Chapin
  0 siblings, 2 replies; 9+ messages in thread
From: AdaMagica @ 2010-05-06  4:56 UTC (permalink / raw)


Gene,
you guessed quite well. See Ada 83 Rationale

http://archive.adaic.com/standards/83rat/html/ratl-04-07.html#4.7

and especially 4.7.4.



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

* Re: Question about default discriminants and mutable objects.
  2010-05-06  4:56   ` AdaMagica
@ 2010-05-06 14:59     ` Adam Beneschan
  2010-05-07  0:47     ` Peter C. Chapin
  1 sibling, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2010-05-06 14:59 UTC (permalink / raw)


On May 5, 9:56 pm, AdaMagica <christoph.gr...@eurocopter.com> wrote:
> Gene,
> you guessed quite well. See Ada 83 Rationale
>
> http://archive.adaic.com/standards/83rat/html/ratl-04-07.html#4.7
>
> and especially 4.7.4.

I don't think that's a complete explanation, though.  It doesn't
explain why you couldn't have (1) a discriminant type without a
default, in which the initial value of the discriminant must be
specified for each object but the discriminant could still be changed
by assigning the whole object; or (2) a discriminant type with a
default discriminant but where all objects of the type are still
constrained [thus making an object declaration X : T; equivalent to
X : T(default);].  It's probably the inability to do one or both of
those two things that causes the "annoyance" Randy mentioned.

As for why those possibilities couldn't be provided, it's probably
just because they would have had to come up with some funky syntax for
it.  That's not a trivial consideration.  Designing syntax is not
always easy.

                                 -- Adam



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

* Re: Question about default discriminants and mutable objects.
  2010-05-06  0:41 Question about default discriminants and mutable objects Peter C. Chapin
  2010-05-06  1:26 ` Randy Brukardt
  2010-05-06  4:07 ` Gene
@ 2010-05-06 15:15 ` Dmitry A. Kazakov
  2 siblings, 0 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2010-05-06 15:15 UTC (permalink / raw)


On Wed, 05 May 2010 20:41:57 -0400, Peter C. Chapin wrote:

> I also understand that an instance of a type without default discriminants
> can't be mutated in this way (that is, by assignment).

You can assign it as a whole as well as its components individually. The
object is mutable, but you cannot change its constraint. Compare it with
similar cases::

   S : String := "abc";

Here the constraint is the array bounds. They cannot be changed, yet S is
mutable.

   T : Foo'Class := ...;

Here the constraint is the type tag. It cannot be changed, but T is
mutable.

> One could imagine some currently
> non-existent syntax that would allow the programmer to mark a type
> declaration so that the compiler allowed discriminant values to be changed
> via assignment without leaning on the mechanism of default discriminants.

That is not a property of the type. It is of a type constraint. Type +
constraint = subtype. Ada is a bit sloppy in the object's subtype
specification. When you declare S as String you write a type, but the
compiler reads it as a constrained subtype of String. That is not good, but
it is difficult to propose a cure. Probably:

   S : String (<>);  -- Any subtype of String
   T : Foo'Class (<>); -- Any type from the class

etc.

> Furthermore one could imagine treating default discriminants as 100%
> syntactic sugar and not endowing them with any special semantics regarding
> mutability.

IMO, discriminant's defaults should imply nothing like what they do now.

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



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

* Re: Question about default discriminants and mutable objects.
  2010-05-06  4:56   ` AdaMagica
  2010-05-06 14:59     ` Adam Beneschan
@ 2010-05-07  0:47     ` Peter C. Chapin
  2010-05-07  2:07       ` Randy Brukardt
  1 sibling, 1 reply; 9+ messages in thread
From: Peter C. Chapin @ 2010-05-07  0:47 UTC (permalink / raw)


AdaMagica wrote:

> Gene,
> you guessed quite well. See Ada 83 Rationale
> 
> http://archive.adaic.com/standards/83rat/html/ratl-04-07.html#4.7
> 
> and especially 4.7.4.

Thanks... that was interesting. I suppose the next time I find myself
wondering about the rationale for a design decision in Ada I should maybe
read the rationale. :)

Peter




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

* Re: Question about default discriminants and mutable objects.
  2010-05-07  0:47     ` Peter C. Chapin
@ 2010-05-07  2:07       ` Randy Brukardt
  2010-05-07 12:35         ` Robert A Duff
  0 siblings, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2010-05-07  2:07 UTC (permalink / raw)


"Peter C. Chapin" <pcc482719@gmail.com> wrote in message 
news:4be361f3$0$2430$4d3efbfe@news.sover.net...
> AdaMagica wrote:
>
>> Gene,
>> you guessed quite well. See Ada 83 Rationale
>>
>> http://archive.adaic.com/standards/83rat/html/ratl-04-07.html#4.7
>>
>> and especially 4.7.4.
>
> Thanks... that was interesting. I suppose the next time I find myself
> wondering about the rationale for a design decision in Ada I should maybe
> read the rationale. :)

Remember that there are three of them: one each for Ada 83, Ada 95, and Ada 
2005. Depending on the feature, you might need to look at all three. (Not 
sure if there will be one for Ada 2012.)

                                  Randy. 





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

* Re: Question about default discriminants and mutable objects.
  2010-05-07  2:07       ` Randy Brukardt
@ 2010-05-07 12:35         ` Robert A Duff
  0 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 2010-05-07 12:35 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> Remember that there are three of them: one each for Ada 83, Ada 95, and Ada 
> 2005. Depending on the feature, you might need to look at all three. (Not 
> sure if there will be one for Ada 2012.)

Right.  Also, for very detailed design decisions, look at the AARM
for rationale.  That's not available for Ada 83 -- I think
Jean Ichbiah was not a "detail man".  ;-)

As to default discriminants causing mutable discriminants,
despite the rationale, it's a bad design.  It's confusing.
And it means you can't use defaults just as defaults when
you want immutable discriminants.  Except you CAN do
that for limited types (more confusion).  And for '[in] out'
parameters, you don't know at compile time whether the
thing is mutable, which is just a tripping hazard.

Also, some compilers chose the deallocate/reallocate
strategy, and others chose the allocate-the-max
strategy.  That's bad; it means you can't use the
feature portably.  Standards are supposed to promote
uniformity.

- Bob



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

end of thread, other threads:[~2010-05-07 12:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-06  0:41 Question about default discriminants and mutable objects Peter C. Chapin
2010-05-06  1:26 ` Randy Brukardt
2010-05-06  4:07 ` Gene
2010-05-06  4:56   ` AdaMagica
2010-05-06 14:59     ` Adam Beneschan
2010-05-07  0:47     ` Peter C. Chapin
2010-05-07  2:07       ` Randy Brukardt
2010-05-07 12:35         ` Robert A Duff
2010-05-06 15:15 ` 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