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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,21960280f1d61e84 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.teledata-fn.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: How come Ada isn't more popular? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1169531612.200010.153120@38g2000cwa.googlegroups.com> <1mahvxskejxe1$.tx7bjdqyo2oj$.dlg@40tude.net> <2tfy9vgph3.fsf@hod.lan.m-e-leypold.de> <1g7m33bys8v4p.6p9cpsh3k031$.dlg@40tude.net> <14hm72xd3b0bq$.axktv523vay8$.dlg@40tude.net> <4zwt33xm4b.fsf@hod.lan.m-e-leypold.de> <1j7neot6h1udi$.14vp2aos6z9l8.dlg@40tude.net> <1170347180.14376.104.camel@localhost.localdomain> <1nxunq6pci4r1$.1nnigcjicppwy.dlg@40tude.net> <11g5kv93fm3ua.1p5hykfsdax5l$.dlg@40tude.net> <14ejp7cone.fsf@hod.lan.m-e-leypold.de> Date: Sun, 4 Feb 2007 20:33:12 +0100 Message-ID: <1izylgl1pmcxx$.19mkg9x6p946t.dlg@40tude.net> NNTP-Posting-Date: 04 Feb 2007 20:33:12 CET NNTP-Posting-Host: f46c0cf8.newsspool4.arcor-online.net X-Trace: DXC=al]aV]3K4H5kUFX=Y?aLP;4IUKDNcfSJ;bb[5IRnRBaCd3ccZ[9gkJc:Nd9^JR5`Mg6 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:8922 Date: 2007-02-04T20:33:12+01:00 List-Id: On Sat, 03 Feb 2007 15:16:21 +0100, Markus E Leypold wrote: > "Dmitry A. Kazakov" writes: > >> On Fri, 02 Feb 2007 13:34:21 +0100, Markus E Leypold wrote: >> >>> "Dmitry A. Kazakov" writes: >>> >>>> Sure you can: >>>> >>>> type Additive is abstract; -- Should be enough for an interface >>>> function "+" (A, B : Additive) return Additive is abstract; >>>> >>>> Here I am not sure about "increment" variable, because it is not >>>> initialized. Anyway: >>>> >>>> function Increment return Additive is abstract; >>>> -- Better be getter/setter pair. Unfortunately Ada does not have >>>> -- "abstract variable interface" it should have >>>> >>>> function Inc (Arg : Additive'Class) return Additive'Class is >>>> begin >>>> return Increment + Arg; >>>> end Inc; >>>> >>>> There is a very simple rule for all this: >>>> >>>> 1. Formal generic subroutines -> abstract primitive operations >>>> 2. Subroutines in the body -> class-wide operations. >>> >>> So you derive from Additive to get a specific implementation? >>> >>> Like >>> >>> type Vector is Additive with ...; >>> type CountVal is Additive with ...; >>> >>> Right? But then, you should note that with >>> >>> C: Countval; >>> >>> Inc(C). >>> >>> returns an 'Additive', not a 'CountVal'. >> >> No, it does Additive'Class! Thus, no problem. > > I've been expressing myself sloppily. It should return a CountVal, not > an Additive'Class. CountVal(s) could not be added to Vectors -- there > is a difference. When Inc returns CountVal then it is not a member of Additive in the result. So you cannot reuse the result for other members of Additive. >> (note that the goal was polymorphic Inc, if you wanted it covariant that > > No. The goal was to have mechanism to write the algorithms once and > being able to pull various actual operators/procedures from that > without replicating the specification of the algorithm. If you want to reuse Inc's algorithm in various specific Inc, then you would use wrappers: function Common_Inc (Arg : Additive'Class) return Additive'Class is begin return Increment + Arg; end Common_Inc; function Inc (Arg : Additive) return Additive is abstract; function Inc (Arg : CountVal) return CountVal is begin return CountVal (Common_Inc (Arg)); end Inc; However I don't understand the reason, why. > Let me suggest 1 other example for the usefulness of generics and than > 1 example where generics (Ada-style) help, but parametric polymorphism > (what I have been talking about as parametrized types) would actually > bring an advantage. > > Example 1: > > An abstract vector space is defined as a sets of vectors u,v,w,... > and scalars a,b,c, ... with a number of operations: > > - scalars are a field, i.e. have additions an multiplication with certain properties > - there is amultiplication between scalars and vectors: a*v > - there is an addition between vectors. > > A normalized vector space introduces the idea of distance between > verctors, Again certain laws apply. For the programmer: There is a > dist-functions which takes 2 vectors a parameters. > > You can do a really huge amount of mathematics with this > axioms. Examples of vector spaces with thos properties would be > certain subsets of functions, finite vectors (i.e. arrays of N > components), polynomes, etc. > > One application would be, given a function on the vector space f : V > -> Real_Numbers to find minima of this function. > > There is an algorithm that is not very efficient but works in a vast > number of cases without having to use specific knowledge of the > underlying vector space. > > With Ada generics I'd define the vector space interface a generic and > instantiate accordingly. The algorithm to fin minima would be > implemented as generic and take a vector space package as generic > parameter. I am deeply unsatisfied with generics here, because this example is almost exactly what I am doing. I have: Over a real domain I have: 1. Scalars 2. Intervals 3. Fuzzy numbers with a partly linear membership function 4. Fuzzy numbers with membership function of a nested intervals + algebra of 1, 2, 3, 4 and between them But that is not all: 5. Sets of 3 + lattice over it Not yet: 6. Dimensioned scalars 7. Dimensioned intervals + algebra ... n. Sets of dimensioned numbers + lattice over it + algebra between dimensioned things and scalars ... n+1 String I/O for all of them + names for the members of the sets ... n+m+1 GTK widgets for them ... Should I show you generic specifications of this mess? Can you imagine the instantiations chain from Float down to GTK+ widget (provided your GNAT would be able to eat it (:-()? I can give you a link... I DON"T want it. I need a clean way to describe an algebra and a lattice and then mix and bend them as I want. I want to be able to derive everything from a scalar. There is always much talk about how mathematical are generics and FP, it is amazing to me how readily people believe in that. > Example 2 > > Write a quicksort algorithm on arrays which can be reused for arrays > of almost arbitrary elements, if a suitable order relationship is > defined. Note that I might want to sort _the same_ elements with > different orders (like insurance number or alphabetically ...). No problem [with some extensions to Ada types system]. You need two interfaces: Array and Weakly_Ordered. You could also sort same elements of same container object using different orders. [However usually in such cases one better uses a sorted view, rather than physically sorts. > Yes. But Georg used generics, so it worked. You said, you don't want > generics, but it doesn't work. I fail to see, how George has to use > select an example that makes your misguided hypothesis -- that > generics are not neeeded -- hold up. Quite the opposite. generic type Element is private; with function "<" (Left, Right : Element) return Boolean; with function "=" (Left, Right : Element) return Boolean; type Container ... procedure Quick_Sort (...); How can this can sort the same container? This is a clear case where generics don't work. You need "<" and "=" be passed as proper functions rather than formal generic functions. [It is not static polymorphism anymore.] > As an software engineer I'd always prefer > generics, since they avoid polymorphism where no polymorphism is > required or intended. ? Generics = static polymorphism. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de