comp.lang.ada
 help / color / mirror / Atom feed
* Sliding Semantics?
@ 1991-07-03 15:40 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre
  0 siblings, 0 replies; 4+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre @ 1991-07-03 15:40 UTC (permalink / raw)


We have run into a totally unexpected problem involving an obscurity in
Ada called "sliding semantics ". Evidently, when assigning array (or
slices), in most instances the range of indexes on the left side need
not be the same as the range on the right side, as long as the number
of elements match.

	a(1..5) := b(6..10);

In other instance, sliding semantics do not apply and the expression on
the right determines the range of the indexes. If the left side range
does not match, the program will generate a constraint_error exception
at the point of the assignment.

We ran into this problem when looking at the use of what was referred to
in this newsgroup as mutable records for varying string operations. The
little program below shows the exact code that causes the difficulty.


   with text_io; use text_io;
   procedure main is 
      subtype index is natural range 0 .. 80;
      type udr_string(length : index := 0) is record
           text : string (1 .. length);
      end record;
   s1, s2, s3 : udr_string;
   posn, len: natural;

begin
   s1 := (4, "abcz");
   s2 := (5, "defgh");
   posn := 3;
   len := 2;

-- Delete len bytes starting at posn from s3.
-- CONSTRAINT_ERROR when posn is 1.
   s3 := udr_string'(s3.length-len,  s3.text(1..posn-1) &
                                     s3.text(posn+len..s3.length));

-- Place a substring of s2 starting at posn and extending length len into s3.
-- CONSTRAINT_ERROR when posn anything but 1.
   s3 := (len, s2.text(posn .. posn + len -1));

put_line(s1.text);
put_line(s2.text);
put_line(s3.text);
end main;

Substitute posn = 1, and then posn = something else, and you will see
that in each case a situation arises where the text field of the
udr_string value on the right starts with something other than 1 and the
assignment is in error.

Does anyone know the rules or where I can find the rules on when sliding
semantics apply?

There are a couple of workarounds. The best we've been able to invent
has been using type conversions. The substring statement, for instance,
becomes

   declare
      type anchor is new string(1..len);
   begin
      s3 := (len, string(anchor(s2.text(posn .. posn + len -1))));
   end;
		
That "slides" the index range of the result to be from 1 to some value.

Does anyone have a better solution?

Neil Webre
Cal Poly, San Luis Obispo, CA, USA
Department of Computer Science

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

* Re: Sliding Semantics?
@ 1991-07-08 15:49 Jack Dean (713) 283-4008
  0 siblings, 0 replies; 4+ messages in thread
From: Jack Dean (713) 283-4008 @ 1991-07-08 15:49 UTC (permalink / raw)


In article <2871efdf.5c45@polyslo.CalPoly.EDU>, nwebre@polyslo.CalPoly.EDU (Nei
l Webre) writes:
|> 
|> There are a couple of workarounds. The best we've been able to invent
|> has been using type conversions. The substring statement, for instance,
|> becomes
|> 
|>    declare
|>       type anchor is new string(1..len);
|>    begin
|>       s3 := (len, string(anchor(s2.text(posn .. posn + len - 1))));
|>    end;
|> 		
|> That "slides" the index range of the result to be from 1 to some value.
|> 

Very interesting problem.  I was unable to come a significantly better
solution.  One question I do have though, is there any advantage to
using a subtype declaration for anchor, i.e.

    declare
       subtype anchor is string(1..len);
    begin
       s3 := (len, anchor(s2.text(posn .. posn + len - 1)));
    end;

Will the compiler generate less code in this case since there is no
type conversion?  This works as desired on the Rational.  Does it do
the same thing on other machines / other Ada compilers?

--
============================================================================
Jack Dean                               |
McDonnell Douglas Space Systems Co.     |
Houston, Tx.                            |
dean@sweetpea.jsc.nasa.gov              |
============================================================================

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

* Re: Sliding Semantics?
@ 1991-07-08 16:42 cis.ohio-state.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!th
  0 siblings, 0 replies; 4+ messages in thread
From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!th @ 1991-07-08 16:42 UTC (permalink / raw)


> Written Jul  3, 1991 by nwebre@polyslo.CalPoly.EDU in inmet:comp.lang.ada */
> /* ---------- "Sliding Semantics?" ---------- */
> 
> We have run into a totally unexpected problem involving an obscurity in
> Ada called "sliding semantics ". 
> . . .
>    with text_io; use text_io;
>    procedure main is 
>       subtype index is natural range 0 .. 80;
>       type udr_string(length : index := 0) is record
>            text : string (1 .. length);
>       end record;
>    s1, s2, s3 : udr_string;
>    posn, len: natural;
> 
> begin
>    s1 := (4, "abcz");
>    s2 := (5, "defgh");
>    posn := 3;
>    len := 2;
> 
> -- Delete len bytes starting at posn from s3.
> -- CONSTRAINT_ERROR when posn is 1.
>    s3 := udr_string'(s3.length-len,  s3.text(1..posn-1) &
>                                      s3.text(posn+len..s3.length));

No "sliding" takes place for an aggregate component.
This constraint error is due to the result of concatenate
taking the low bound from the right operand (posn+len) when the left
operand is null (posn-1 = 0).

> -- Place a substring of s2 starting at posn and extending length len into s3.
> -- CONSTRAINT_ERROR when posn anything but 1.
>    s3 := (len, s2.text(posn .. posn + len -1));

No "sliding" here either.  Constraint error if posn /= 1.

> . . .
> Does anyone know the rules or where I can find the rules on when sliding
> semantics apply?

Sliding occurs only in assignment statements (see RM 5.2.1(1)), in
array object initializations (see RM 3.2.1(16)), and upon 
explicit array (sub)type conversion (see RM 4.6(11)).

> There are a couple of workarounds. The best we've been able to invent
> has been using type conversions. The substring statement, for instance,
> becomes
> 
>    declare
>       type anchor is new string(1..len);
>    begin
>       s3 := (len, string(anchor(s2.text(posn .. posn + len -1))));
>    end;
> 		
> That "slides" the index range of the result to be from 1 to some value.
> 
> Does anyone have a better solution?

A somewhat simpler solution is to make "anchor" into a constrained subtype:

    declare
       subtype anchor is string(1..len);
    begin
       s3 := (len, anchor(s2.text(posn .. posn + len -1)));
    end;

One could also define a function "Slide":

    function Slide(S : String) return String is
        subtype Anchor is String(1..S'Length);
    begin
        return Anchor(S);
    end Slide;
    pragma Inline(Slide);

This whole area is one of the more annoying little back alleys of Ada 83.

For Ada 9X we have proposed to allow implicit subtype conversion
(aka "sliding") any place where there is an "applicable 
index constraint" (see 4.3.2(4-8)), other than on an OUT or IN OUT parameter.  
We resolve the interaction with aggregates with named 
associations and an others clause (see RM 4.3.2(6)) 
by specifying that implicit subtype conversion is never applied 
to such an aggregate.  This caveat replaces the restriction
in paragraph RM 4.3.2(6) completely.

These new rules would allow your original examples to work as originally
written.

(As usual, this Ada 9X proposal is not yet finalized, so no promises, though
comments are welcome...)

> Neil Webre
> Cal Poly, San Luis Obispo, CA, USA
> Department of Computer Science

S. Tucker Taft         stt@inmet.inmet.com
Ada 9X Mapping/Revision Team
Intermetrics, Inc.
Cambridge, MA  02138

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

* Re: Sliding Semantics?
@ 1991-07-08 18:03 Neil Webre
  0 siblings, 0 replies; 4+ messages in thread
From: Neil Webre @ 1991-07-08 18:03 UTC (permalink / raw)


In article <1991Jul8.154928.24379@aio.jsc.nasa.gov> dean@sweetpea.jsc.nasa.gov 
writes:
>In article <2871efdf.5c45@polyslo.CalPoly.EDU>, nwebre@polyslo.CalPoly.EDU (Ne
il Webre) writes:
>|> 
>|> There are a couple of workarounds. The best we've been able to invent
>|> has been using type conversions. The substring statement, for instance,
>|> becomes
>|> 
>|>    declare
>|>       type anchor is new string(1..len);
>|>    begin
>|>       s3 := (len, string(anchor(s2.text(posn .. posn + len - 1))));
>|>    end;
>|> 		
>|> That "slides" the index range of the result to be from 1 to some value.
>|> 
>
>Very interesting problem.  I was unable to come a significantly better
>solution.  One question I do have though, is there any advantage to
>using a subtype declaration for anchor, i.e.
>
>    declare
>       subtype anchor is string(1..len);
>    begin
>       s3 := (len, anchor(s2.text(posn .. posn + len - 1)));
>    end;
>
>Will the compiler generate less code in this case since there is no
>type conversion?  This works as desired on the Rational.  Does it do
>the same thing on other machines / other Ada compilers?
>
>--

Good point. Your approach avoids a conversion back to type string.

Neil Webre
Cal Poly, San Luis Obispo, CA
Department of Computer Science

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

end of thread, other threads:[~1991-07-08 18:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-07-03 15:40 Sliding Semantics? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre
  -- strict thread matches above, loose matches on Subject: below --
1991-07-08 15:49 Jack Dean (713) 283-4008
1991-07-08 16:42 cis.ohio-state.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!th
1991-07-08 18:03 Neil Webre

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