From: Georg Bauhaus on
On 10.08.10 15:52, Elias Salom�o Helou Neto wrote:
> On Aug 7, 4:53 am, "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de>
> wrote:
>> On Fri, 6 Aug 2010 13:21:48 -0700 (PDT), Elias Salom�o Helou Neto wrote:
>>
>>> I would like to know how does code generated by Ada compilers compare
>>> to those generated by C++.
>>
>> The short answer is yes, of course.
>>
>> The long answer is that such comparisons are quite difficult to perform
>> accurately.
>
> Yes, I know that. I am, however, writing code within that 1% of
> applications that would be tremendously affected if there is no way to
> access arrays with no range checking. So I am asking very precisely:
> does Ada allow me to do non range-checked access to arrays?

Yes.

The language mandates that checks can be turned off.
See LRM 11.5, for example, of how to give permission.

You can document this is source text by placing a language
defined pragma Suppress in some scope. (There is also
a pragma Unsuppress!) Compilers can be advised to turn
checks on or off, either specific checks, or all checks.

To see the effects, get GNAT or some other compiler;
with GNAT, translate the same source with suitable options
and look at the code generated in each case.


$ gnatmake -c -O2 -funroll-loops -gnatn -gnatp a.adb
vs
$ gnatmake -c -gnato -fstack-check -gnata a.adb

procedure A is
pragma Suppress (Range_Check); -- or All_Checks




type My_Index is range 10 .. 55;
subtype Only_Some is My_Index range 10 .. 12;

type A_Few_Bucks is digits 4;

type Data is array (My_Index range <>) of A_Few_Bucks;
subtype Few_Data is Data (Only_Some);

X, Y, Z: Few_Data;

begin
X := Few_Data'(1.2, 3.4, 5.6);
Y := Few_Data'(3.21, 0.19, 3.3E+12);

-- hand made sum placed in Z:
for K in Z'Range loop
Z(K) := X(K) + Y(K);
end loop;

pragma Assert (Z(10) in 4.4 .. 4.5
and Z(11) in 3.5 .. 3.6
and Z(12) >= 3.3E+12);

end A;
From: Dmitry A. Kazakov on
On Tue, 10 Aug 2010 06:52:21 -0700 (PDT), Elias Salom�o Helou Neto wrote:

> So I am asking very precisely:
> does Ada allow me to do non range-checked access to arrays?

Yes, in many ways. Others have pointed out how to suppress checks. I would
rather advise you to write programs so that the compiler would omit them.
Consider this:

type Vector is array (Positive range <>) of Long_Float;
function Sum (X : Vector) return Long_Float;

Implementation:

function Sum (X : Vector) return Long_Float is
Result : Long_Float := 0.0;
begin
for Index in X'Range loop
Result := Result + X (Index);
end loop
return Result;
end Sum;

Since the compiler knows that Index is in X'Range, you told it that, it
will generate no subscripts checks.

> This is what attracted me, but, as you may guess, I cannot spend
> months learning the language if I am not sure about some very specific
> issues, such non RC array indexing.

Ada arrays are simple and intuitive. And there are multidimensional arrays
too. They certainly do not require months to learn.

>>> Also, I do need to have something similar to C++ "templated
>>> metaprogramming" techniques.
>>
>> Ada has generics which are roughly same as templates. Unlikely to C+
>> generics are contracted and not automatically instantiated.
>
> What exactly does it mean? Is it something like run-time
> instantiation?

It means that the formal parameters of the generics have contracts = typed.
C++ template parameters are untyped. You can substitute anything as long as
the result compiles.

> Hum... I intend to write an efficient n-dimensional matrix.

What's wrong with an n-dimensional array?

> This would
> leave to me the option to individually write element accessing code
> for each possible instance of my generic class if I wish to make it
> through a member function (or whatever is equivalent to that in Ada)
> that takes as many elements as there are dimensions, right?

I cannot imagine generic/templated code for an nD matrix package, because
it would not allow you to unroll the nested loops over n, if we took an
implementation of "+" as an example. It becomes much, much worse for
multiplication. I doubt it were a realistic goal to have it generic over n,
you will need a more powerful preprocessor than templates.

>>> And further, is there any language which is
>>> _truly_ better (regarding code maintainability, readability and
>>> developing ease) than C++ and as overhead-free as it?
>>
>> Maintainability, readability and developing ease are sufficiently dependent
>> on *not* using things like C++ templates. Even more variadic templates!
>
> Could you elaborate on that? I do not agree and I do have lots of
> experience in writing and maintaining template code in C++. They are
> as easy to read, maintain and develop as any C++ code, or should I say
> as difficult?

Honestly, you are the first I met who considered templates easy to read and
maintain. So I am not really prepared to argue. It is like someone said
that warm beer is great. How do you test a template class? How do you make
it work for Borland C++, MSVC 3, 5, 8, 10 and gcc? When you get an error
message whom do you ask what's wrong?

>> Note that for numeric applications templates do not help much. Consider the
>> following problem. Let you have to implement some mathematical function of
>> known algorithm and put it into a library. That latter is not possible with
>> templates anyway is beside the point.
>
> You seem to imply that templated code cannot be part of a library, but
> it definitely can. Just consider the possibility of distributing the
> source, which is what I wish to do. STL does just that.

So, how are you going to test it? We have to maintain our own template
library. It is 10 years old, and errors keep on coming because the number
of combinations needed to check is impossible to cover. So the maintenance
looks like: change the code and commit. If someone gets a problem upon
instantiation let us know. BTW, Ada geneircs are much better, because of
their contracts, they are compiled in true sense of this word.

> Even if you do
> not want to go open source, it is easier to write the code once and
> instantiate it for every type your users are supposed to use, maybe
> wrapped within some overloaded function.

We were talking about a n-D matrix package. You suggest to instantiate it
for each possible combination of types for each n? (:-))

> It sounds like a
> good idea, specially if things like that could be done for user
> defined types, i.e., if I can define my own type that "is digits <>".

Yes you can. E.g.:

type My_Float is digits 8 range -1.0E10..1.0E10;

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Robert A Duff on
"Yannick Duch�ne (Hibou57)" <yannick_duchene(a)yahoo.fr> writes:

> Le Tue, 10 Aug 2010 17:01:10 +0200, Robert A Duff
> <bobduff(a)shell01.theworld.com> a �crit:
>> No, Ada generics work the same way as C++ templates -- the typical
>> implementation is that each instance gets a separate copy of
>> the code
> I wonder If I've understood you correctly. It seems Ada use code sharing
> (which is the reason of some restriction on formal parameters). Code
> duplication is the C++'s template way, not the Ada's generic package
> way. Or am I mistaken somewhere ?

Most Ada compilers, including GNAT, use code duplication. Some Ada
compilers support code-sharing for instance bodies, either in some
cases, or in all cases.

I don't know of any C++ compilers that support sharing of template
instances.

Code sharing is hard in Ada. It is substantially harder in C++,
I think. But note that I am not a C++ compiler expert!

- Bob
From: Randy Brukardt on
"Elias Salom�o Helou Neto" <eshneto(a)gmail.com> wrote in message
news:8349c981-4dca-49dc-9189-8ea726234de3(a)f42g2000yqn.googlegroups.com...
....
> Yes, I know that. I am, however, writing code within that 1% of
> applications that would be tremendously affected if there is no way to
> access arrays with no range checking. So I am asking very precisely:
> does Ada allow me to do non range-checked access to arrays?

Several people have already answered your exact question, so I won't bother
to repeat that. But I'd like to point out that Ada compilers spend a lot of
effort in eliminating unnecessary range checks. So that if your code is
well-written, there will be few if any range checks when you access arrays.
(Dmitry showed you one way that can be accomplished; another is to ensure
that temporaries have appropriate subtypes.)

So I'm suggesting that you try to avoid premature optimization. I can
believe that there will be cases where you'll need to suppress range checks,
but I'd also suggest that those will be far rarer than you are thinking.
And, of course, the problem is that suppressing range checks is essentially
the same as not wearing seat belts when driving. Just remember how many
"security patches" have been caused by buffer overflows, all of which would
have been detected and prevented by having range checking.

Randy.


From: anon on
In <op.vg77swroxmjfy8(a)garhos>, =?iso-8859-15?Q?Yannick_Duch=EAne_=28Hibou57=29?= writes:
>Le Tue, 10 Aug 2010 16:03:13 +0200, Elias Salom=E3o Helou Neto =
>
><eshneto(a)gmail.com> a =E9crit:
>> It is a pity that this post became a technical discussion on array
>> indexing.
>Could I, if you please, talk to Phil without your prior acknowledgment =
>
>please ? ;)
>
>While I understand why you reacted (not the way you do... closed =
>
>parenthesis)
>
>> A simple question that could be asked in a single line is:
>> can Ada access arrays without range checking?
>Sorry, due to a recent trouble with my news reader, I've the original po=
>st.
>
>However, on this sentence basis, I would say: if you do not want range =
>
>check, then just disable it in the compiler option. But be warned you wi=
>ll =
>
>then be unable to catch runtime error. While there is a way to safely dr=
>op =
>
>this compiler option: validation with SPARK checker (just tell if you ne=
>ed =
>
>to learn about it).
>
>If your matter is just about range checking, the answer is as simple as =
> =
>
>that.
>
>If you use GNAT, you may insert "-gnatp" in the command line arguments.
>http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gnat_ugn_unw/Run_002dTime-Checks=
>..html
>
>If you use GNAT from the GPS environment, you may open the "Project" men=
>u, =
>
>then the "Edit project properties" submenu. Then choose the "Switches" =
>
>tab, then the "Ada" tab and check the "Suppress all check" check box or =
> =
>
>uncheck the "Overflow check" check box.
>
>Providing I did not fail to understand what you meant.
>
>-- =
>
>There is even better than a pragma Assert: a SPARK --# check.
>--# check C and WhoKnowWhat and YouKnowWho;
>--# assert Ada;
>-- i.e. forget about previous premises which leads to conclusion
>-- and start with new conclusion as premise.
While the main expression are equal. The code generation by Ada compilers
versus C or C++ is less efficient do to a number of factors.

1. Elaboration: Ada compilers can generate run-time elaboration
routines that must be executed before starting
the main users program. C/C++ compilers do not
perform any run-time elaboration, which cause
the execution and code generation to be more
efficient but cause the less program to be less
reliable.

2. Run-Time Checks: The Ada compiler generates inline run-time checks
which the C/C++ compiler do not. Using the pragma
"suppress" statement can eliminate most checks. The
absent of these checks makes C/C++ less reliable.
Using the following removes all checks the statement
does have other precise options as well.

pragma Suppress ( All_Checks ) ;
-- removes all checks on the program or that package.

pragma Suppress ( All_Checks, ON => <name 1>,
...
ON => <name n> ) ;
-- removes all checks on that set of names only.
-- Names may be an object such as an array or routine
-- Also, the "ON =>" symbols are optional


3. GCC backend: Ada as well as any other language besides C or C++
must translate the core language into the C/C++ like
tree structure. Which forces the core languages to be
limited to C/C++ code generation.