From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 3 Jul 91 15:40:15 GMT From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!petunia!nwebre@ucbvax.Be rkeley.EDU (Neil Webre) Subject: Sliding Semantics? Message-ID: <2871efdf.5c45@polyslo.CalPoly.EDU> List-Id: 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