From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1cb75f0476fe2d1a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Half Constrained Array Types and Slices Date: 02 Mar 2006 15:38:25 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1141331907 14620 192.74.137.71 (2 Mar 2006 20:38:27 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 2 Mar 2006 20:38:27 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:3242 Date: 2006-03-02T15:38:25-05:00 List-Id: "Jeffrey R. Carter" writes: > Robert Duff present a concept from his idea for an Ada-like language > (not called Duff) some time ago, that there was no need for "box" ("<>") > in his language: > > type String (Length : Natural) is array (1 .. Length) of Character; > > (IIRC) Yeah. But I did not invent this idea. Something like this was proposed as a language change during the Ada 9X design. Probably invented by Tucker before I joined the project. (Except my hobby language can use this idea to _simplify_, whereas Ada would have had to add this feature and still keep the "range <>" notation for compatibility.) > In Ada we have constrained and unconstrained array types; I call this a > "half constrained" array type so we have names for all of them. Since > his language doesn't have box, I presume an unconstrained array type > would be: > > type Unconstrained (Lo : Positive; Hi : Natural) is array (Lo .. Hi) of Component; > > while a constrained type would look like an Ada constrained type: > > type Constrained is array (Lo .. Hi) of Component; > > I was wondering how slices would work with this kind of syntax. I would > expect a slice of a half-constrained array to have the fixed lower bound > specified in its type declaration; Definitely. >... that would differ from the others. I'm not so sure about that. I'm inclined to say slices always slide to the lower bound. The idea that X(3..5) should have lower bound 3 seems to make sense at first, until you think about passing that slice as a parameter. The called procedure should not care that it's a slice at all -- much less the particular bounds used to create it. The Ada rule violates abstraction. Same issue for function returns. It's also rather error prone. It's easy to write code that assumes all Strings start at 1 (and such code is more readable than the more-general (correct) code using complicated expressions involving 'First/'Last/'Length). And 99.9% of Strings _do_ have 'First=1, so one doesn't notice the bug until it's too late. > In Ada, we deal with slices of constrained array types by having the 1st > named subtype be a constrained subtype of an unconstrained base type; > that wouldn't seem to work here, since it would have a base type with 2 > discriminants and a subtype with only 1. Yes, I agree that's a problem. Various possible solutions come to mind. Probably the simplest is to outlaw slices of constrained arrays. After all, what is the sense in saying "all arrays of type T go from 1 to 5", and then turn around and create values of type T with bounds 3..4? Even in Ada as is, there is very little use for this feature. You can do: type Bits is array(Int range 0..31) of Boolean; function Rotate(X: Bits) return Boolean Bits is begin return X(31) & X(0..30); end Rotate; But that's not worth much language complexity. If you want that, declare an unconstrained (doubly unconstrained?) array, along with constrained subtype(s). I can talk about more and more exotic ideas, depending on how far away from Ada you're willing to contemplate. ;-) For example, I'd like to be able to do: type T(D1: ...; D2: ...) is ... subtype S is T(D1 => 17); -- Illegal in Ada to partially constrain! Or maybe the syntax should be "T(D1 => 17, D2 => <>)". Also: type Memory_Block(Length: Positive) is array(0..Length-1) of Byte; ^^ That ain't Ada! ;-) Not even close. Ada forbids discriminants as part of an expression like that. I think pure (no side effects) expressions should be OK. But there's a whole lot of language mechanism needed to teach the compiler to know which expressions are pure. - Bob