comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre@ucbvax.Be rkeley.EDU  (Neil Webre)
Subject: Sliding Semantics?
Date: 3 Jul 91 15:40:15 GMT	[thread overview]
Message-ID: <2871efdf.5c45@polyslo.CalPoly.EDU> (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

             reply	other threads:[~1991-07-03 15:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-07-03 15:40 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-07-08 15:49 Sliding Semantics? 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
replies disabled

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