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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6689542a60dbcc55 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news4.google.com!feeder.news-service.com!news.mixmin.net!aioe.org!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Re: Discriminated records are not the most efficient, but ... Date: Sun, 22 Aug 2010 07:39:45 +0200 Organization: Ada At Home Message-ID: References: NNTP-Posting-Host: qtPKc8dS40PCH9kQ+vAdng.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 User-Agent: Opera Mail/10.61 (Win32) Xref: g2news1.google.com comp.lang.ada:13590 Date: 2010-08-22T07:39:45+02:00 List-Id: Le Sat, 21 Aug 2010 23:57:19 +0200, Yannick Duch=C3=AAne (Hibou57) = a =C3=A9crit: > An opportunity to notice how Ada provides the concept to map this = > element of functional programming to its imperative language. And ther= e = > may be others which can be easily mapped to Ada. Model in ML or SML = > (prototyping at a very abstract level) and implement in Ada (get safet= y = > and efficiency) ? May be an option for *some* applications. A new episode in the great series =E2=80=9CBeyond The Type=E2=80=9D :) Let me first give you a short (S)ML excerpt: datatype Tree =3D Leaf of int | Node of Tree * Tree; Think of it as an Ada discriminated record, like type Tree_Kind is (A_Leaf, A_Node); type Tree_Type (Kind : Tree_Kind); type Tree_Access is access all Tree_Type; type Tree_Type (Kind : Tree_Kind) is record case Kind is when A_Leaf =3D> Leaf : Integer; when A_Node =3D> Left, Right : Tree_Access; end case; end record; In datatype Tree =3D Leaf of int | Node of Tree * Tree; =E2=80=9CTree=E2=80=9D is an SML type, =E2=80=9CLeaf=E2=80=9D and =E2=80= =9CNode=E2=80=9D are SML constructors. Think of = these latters as two primitives functions returning each a subtype of = Tree_Type: Now, an SML function defined as fun Foo (Leaf (Value)) =3D Value | Foo (Node (Leaf (_), Leaf (Value))) =3D Value; (here, the underscore =E2=80=9C_=E2=80=9D has the same meaning as with P= rolog) Think of it as the equivalent Ada function: function Foo (Tree : Tree_Type) return Integer is begin case Tree.Kind is when A_Leaf =3D> return Tree.Leaf; when A_Node =3D> return Tree.Left.Leaf; end case; end Foo; OK. You will enjoy one more the awesome Ada case statement and its heavy= = reliability which will help you avoid most errors. But things might go = wrong as you may guess. What if Tree.Left is not actually a =E2=80=9CTre= e_Type = (A_Leaf)=E2=80=9D ? Bam!, Run-Time Error! Eh, logic error, compiler cann= ot = statically catch it. I may guess you may like to say =E2=80=9Cwhere does it drive us, his sil= ly = 'untyped' interpreted language will be far more worst=E2=80=9D. Wait a minute boys and girls, and let the magic play. First of all, I know you already have a Janus or GNAT (aren't you ?), so= I = will not bother about it. But you may not already have either an SML-SJ = or = MLTon. Here are links (we will resume right after, don't go away right = now): http://www.smlnj.org/ (dated as far as October 2000, but still works as = = much fine as it was doing ten years ago) http://mlton.org/ (more like an alive app, dated as near as 2008) Open SML-NJ (an interactive interpreter, MLTon on the other side is a = native code optimizing with global analysis compiler) first, at the SML-NJ prompt, type: datatype Tree =3D Leaf of (int) | Node of (Tree * Tree); (same as the one at the message opening, except standing on a single lin= e) Now, type fun Foo (Leaf (Value)) =3D Value | Foo (Node (Leaf (_), Leaf (Value)= )) =3D = Value; What happens ? You should get a stdIn:28.1-28.77 Warning: match nonexhaustive Leaf Value =3D> ... Node (Leaf _,Leaf Value) =3D> ... =E2=80=9CWarning: match nonexhaustive=E2=80=9D. This one is as cool as a= n error message = from an Ada compiler. What does it means ? You will see (or you may gue= ss = straight away). Now, type the following: fun Foo (Leaf (Value)) =3D Value | Foo (Node (_, Right)) =3D Foo (Ri= ght); Now you should get.... no warning any more. What Foo is supposed to stand for ? I give you the name of the function = I = was playing with : Last_Leaf. Now, you see why the first one was wrong, = = and why the second one is OK. The first one exactly match the one define= d = in Ada above, which would have passed compilation stage without hearing = a = fly bzzz. But this same, did not avoided a warning from SML-NJ. So, what is the difference ? Abstraction : SML sees the constructors as = = part of the structure definition, while an Ada compiler would be unlikel= y = so attempt such an analysis (the construction is a function for an Ada = compiler, not part of of any type definition). Types and constructors ar= e = tied to each others. Time to say also, SML is not =E2=80=9Cuntyped=E2=80=9D, its type-inferen= ce oriented. I had = an adventure with such a thing, needless to say it is unmaintainable wit= h = an application as soon as it goes beyond 5_000 or 10_000 lines of source= = (Ada is really nicer there and even above, it will keep going nice). But= = this is nice at smaller level, as it is terse, which is mostly welcome = some time, just like when you are writing down something on a paper. So my purpose is not to say it is like Ada, not at all: it is different,= = while also formal enough to be usable. It has a different point of view,= = which makes it able to see what an Ada compiler will not see in an = algorithm, simply because type and constructors are not opaque to each = others. What would be the best here, with the possibility of an human error in = mind ? Abstraction designed in SML (caught the algorithmic design error)= = and implementation design in Ada which is the only one which can handle = = things in the large (and efficiently with sharper designed types) as it = is = unlikely your application will be such a tiny thing. What about that last episode of =E2=80=9CBeyond The Type=E2=80=9D ? Did = you liked it ? P.S. I really did have this error while playing with SML-NJ. And when I = = understood what the error standed for and how I could fix it, I said =E2= =80=9CWas, = great, I love this waring message=E2=80=9D.