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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!fx04.iad.POSTED!not-for-mail From: Shark8 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:30.0) Gecko/20100101 Thunderbird/30.0a1 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: A bad counterintuitive behaviour of Ada about OO References: <932kntuq5rrr.8sumwibqrufn.dlg@40tude.net> <1ohy7vnbntskq$.h139ov04mlxu$.dlg@40tude.net> <1lREv.450693$4n.74225@fx31.iad> <1oj0b4rwma99b$.1iqu11p0ea556$.dlg@40tude.net> <1iyw6q7texwn3$.4mgcck9beqmt.dlg@40tude.net> In-Reply-To: <1iyw6q7texwn3$.4mgcck9beqmt.dlg@40tude.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: X-Complaints-To: abuse@teranews.com NNTP-Posting-Date: Fri, 08 Aug 2014 19:34:38 UTC Organization: TeraNews.com Date: Fri, 08 Aug 2014 13:34:36 -0600 X-Received-Bytes: 5422 X-Received-Body-CRC: 3065705365 Xref: news.eternal-september.org comp.lang.ada:21570 Date: 2014-08-08T13:34:36-06:00 List-Id: On 08-Aug-14 05:20, Dmitry A. Kazakov wrote: > And they [subtypes] break in-operations. As an example consider: > > X : Integer := -1; > > Now substitute Positive for Integer. That's going the wrong way. You're narrowing the set when you move to the subtype, so obviously not all values will be present; this is a Good Thing. For example, we can enforce consistency [and correctness] in a DB with subtypes: Subtype Digit is Character range '0'..'9'; -- The following is a string, of length 9, that has ONLY digits. Subtype Social_Security_Number is String(1..9) with Dynamic_Predicate => (for all C of Social_Security_Number => C in Digit); -- The following function cannot have an invalid invalid SSN parameter. Procedure Save( SSN : Social_Security_Number; ID: User_ID); Function Load(ID: User_ID) return Social_Security_Number; And this is good because we *don't* want any string in the DB's SSN field, we only want strings which are SSNs. Likewise, we may want Ada-like identifiers, say in a mapping: -- Validation rules: -- #1 - Identifier cannot be the empty-string. -- #2 - Identifier must contain only alphanumeric characters + underscore. -- #3 - Identifier cannot begin with a digit. -- #4 - Identifier cannot begin or end with an underscore. -- #5 - Identifier cannot have two consecutive underscores. Function Valid_Identifier(Input : String) return Boolean; -- A string containing an identifier. Subtype Identifier is String with Dynamic_Predicate => Valid_Identifier( Identifier ) or else raise Constraint_Error; -- This package defines a mapping of a name to a type; both of these -- are instances of Identifier. Package Attribute_List is new Ada.Containers.Indefinite_Ordered_Maps( Key_Type => Identifier, Element_Type => Identifier ); -- ...in body. Function Valid_Identifier(Input : String) return Boolean is Subtype Internal_Range is Natural range Input'First+1..Input'Last-1; First : Character renames Input(Input'First); Last : Character renames Input(Input'Last); Use Ada.Characters.Handling; Begin -- Initialize w/ conformance to rule #1. Return Result : Boolean:= Input'Length in Positive do -- Rule 2 Result:= Result and (For all C of Input => Is_Alphanumeric(C) OR C = '_'); -- Rule 3 Result:= Result and not Is_Decimal_Digit(First); -- Rule 4 Result:= Result and First /= '_' and Last /= '_'; -- Rule 5 Result:= Result and (for all Index in Internal_Range => (if Input(Index) = '_' then Input(Index+1) /= '_') ); end return; End Valid_Identifier; And this is good. We *don't* want to substitute STRING for Social_Security_Number or Identifier as the values that STRING can take are outside what we want to deal with... and if we had to deal with validation at every subprogram-call or function-return then Ada would be little better than PHP. > Subsetting means nothing to subtyping and both very little to substitutability. All three are different things. Ridiculous; as shown above subtyping *is* the subsetting of the valid values: Social_Security_Number in particular has only 10**9 values rather than the Σ(n=0..Positive'Last) 256**n values that the STRING type would have. > > Huh, great mathematical problems are about fighting constraints. E.g. > solving x**n + y**n = z**n in real numbers vs. in natural ones. No big > deal? Same applies to programming, it is mostly about working around > constraints. ...that's the most idiotic thing I've *ever* heard you say. Constraints are fundamental for mathematical proofs; they are essential for making robust programs. (HINT: definitions are often constraints.)