comp.lang.ada
 help / color / mirror / Atom feed
From: ka@socrates.hr.att.com (Kenneth Almquist)
Subject: Re: Redefinition of Equality (Long)
Date: 1996/10/07
Date: 1996-10-07T00:00:00+00:00	[thread overview]
Message-ID: <539rf2$mbh@nntpa.cb.lucent.com> (raw)
In-Reply-To: 533aebINNfat@cayman.cis.ohio-state.edu


dgibson@cayman.cis.ohio-state.edu (david scott gibson) asks:
> Would you allow an "in" mode formal of a mutable type to be used as an
> actual corresponding to another "in" mode parameter in a call to an
> operation declared outside the package?  That is, could the (concrete)
> value indeed be modified by an outside operation, but only if that
> operation also saw that the type in question was marked as mutable?

If I understand the question, the answer is no for private types.  In
other words, if you have:

    package Set_Package is
       type Set_Type is limited private;
       procedure Insert(Item : Integer; Set : Set_Type);
       function Is_Member(Item : Integer; Set : Set_Type) return Boolean;
    private
       pragma Mutable(Set_Type);
       type Tree_Entry;
       type Tree_Entry_Ptr is access Tree_Entry;
       type Tree_Entry is record
          Left, Right : Tree_Entry_Ptr;
          Value : Integer;
       type Set is 
    end Set_Package;

then the body of Set_Package can include

    procedure Move_To_Root(Key : Integer; Set : in out Set_Type);
       -- Moves the item containing Key to the root of the tree
       -- if it is present in the tree.

    function Is_Member(Item : Integer; Set : Set_Type) return Boolean is
    begin
       Move_To_Root(Item, Set);
       return Set /= null and then Set.Value = Item;
    end Is_Member;

Inside the body of the package there is no semantic difference between
"in" parameters and "in out" parameters, so we could equally well have
written:

    procedure Move_To_Root(Key : Integer; Set : in Set_Type);


Outside of the package, the legality of various operations is not
affected by the presence of pragma Mutable:

    procedure P(Set : in Set_Type) is
    begin
       if Is_Member(1, Set) then -- Legal
          Insert(2, Set);        -- Not legal
       end if;
    end P;

The point is that the abstract value of an "in" parameter should not be
modified.  Inside the body of the package, it is the responsibility of
the programmer to ensure this.  Outside the package the compiler will
check this.

Note that when code is generated for P, Set must be treated as an "in
out" parameter because Is_Member may modify the concrete value of Set.
This is why the pragma applies to the type, as opposed to applying to
particular operations provided by the package.

In my description, I allowed pragma Mutable to be applied to types
which are not private.  In this case there is no distinction between
inside and outside the package; an "in" parameter of the type can be
modified anywhere.  I cannot think of any valid use for this, so I'm
inclined to prohibit it.
					Kenneth Almquist




      reply	other threads:[~1996-10-07  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-27  0:00 Redefinition of Equality (Long) david scott gibson
1996-09-29  0:00 ` Robert A Duff
1996-09-29  0:00   ` Robert Dewar
1996-09-30  0:00   ` Dave Gibson
1996-09-30  0:00     ` Robert A Duff
1996-10-02  0:00       ` Robb Nebbe
1996-10-04  0:00       ` Redefinition of Equality david scott gibson
1996-10-04  0:00       ` Redefinition of Equality (Long) Kenneth Almquist
1996-10-04  0:00         ` david scott gibson
1996-10-07  0:00           ` Kenneth Almquist [this message]
replies disabled

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