From: Robert A Duff on
Jacob Sparre Andersen <sparre(a)nbi.dk> writes:

> Robert A Duff wrote:
>> Jeffrey Carter <spam.jrcarter.not(a)spam.not.acm.org> writes:
>
>>> type T is range 0 .. 2 ** N - 1;
>>> for T'Size use N;
>
>> Yes. And T'Size = N by default in Ada >= 95, so I would write:
>>
>> pragma Assert(T'Size = N);
>>
>> instead of the Size clause.
>
> But don't you loose the compile time error in case the compiler
> cannot make T'Size = N?

No. Compilers are required to make T'Size = N. See 13.3(54-55).
The Assert is just a hint that we care about that fact.

Suppose N = 32, and I make a mistake, and write:

pragma Assert (T'Size = 33);

GNAT will warn that this is False, which is what I want.
(And in -gnatwe mode, that's an error.)

If I instead wrote "for T'Size use 33;", the compiler
will not catch the error, but will instead use a strange
Size.

That's why I don't like to use rep clauses to assert that
the compiler chose the obviously-correct representation --
the compiler will complain if it's too small, but not
if it's too big.

Consider:

type M is mod 32; -- Oops, I meant 2**32.
for M'Size use 32; -- No error here.

I've seen this bug in real code.

- Bob
From: Yannick Duchêne (Hibou57) on
Le Tue, 10 Aug 2010 14:26:25 +0200, Phil Clayton
<phil.clayton(a)lineone.net> a écrit:
> A(A'Length - I - 1)
>
> (not valid Ada, of course.)

A(A'Last - (I - A'First))

> Perhaps "reverse" would be a better name for the operator "not" when
> the modulus is not a power of two?
Or "wrapped" ? But this would only make sens on an array instance basis,
not on type basis. If the array actual range is not the same as the index
type range, it fails. So this could not be a type primitive (operator).
May be an array attribute ? A'From_Last ? A'Last_Based ?

"reverse" is already there for loop statements. I feel this makes for sens
for loop, as what is reverse, is not the array index: this is the
iteration which goes reversed order.

--
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.
From: Elias Salomão Helou Neto on
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?

>
> > I use C++ for numerical software
> > implementation, but I am trying to find alternatives.
>
> Ada is especially good for this, because it has an elaborated system of
> numeric types with specified accuracy and precision (and behavior), which
> C++ lacks.

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.

> > One thing,
> > however, I cannot trade for convenience is efficiency. Will Ada
> > compiled code possibly be as efficient as that generated by C++
> > compilers?
>
> Certainly yes. Potentially Ada code can be more efficient, because in Ada
> your program usually tells the compiler more than in C++. This information
> can be used in optimization.

All right. This one is easy to believe!

> > 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?

> > In particular, C++0x will introduce
> > variadic templates, which will allow us to write templates that will
> > generate efficient, type-safe, variable-argument functions. Is there
> > anything like that in Ada?
>
> No.

Hum... I intend to write an efficient n-dimensional matrix. 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?

> > If any of the above questions is to be negatively answered, I ask: why
> > does Ada even exist?
>
> Do you mean variadic templates here? Seriously?

I did not mean that, but I am terribly sorry for this paragraph since,
as Gene commented, this is a truly unreasonable question. I am sure
there are plenty of reasons why Ada should exist that goes far beyond
the questions I pointed out :) - no I am not a troll.

> > 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? So, the point in C++ difficulty is not quite templated
code, but rather everything else in the language :) Even if it is
difficult, I like C++, but I have long been feeling that there _must_
be better options. I am right now looking for the right one.

> 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. 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.

> How do you do it to work for say
> float, double etc? Metaprogramming you said. You could try implementing it
> as a template<class number>, but the parameter here is not the actual type
> you need to use in the computations in order to make the result accurate
> within the precision of the type number. In C++ you cannot even learn the
> precision of a template argument.

You can! There is the std::numeric_limits<class number> template that
does allow you to learn nearly everything you need about the numeric
type "number". You can even specialize this template to your own user-
defined type so it will act as a fundamental arithmetic type in most
aspects.

> In Ada at least you can (Ada Reference
> Manual 3.5.8). But considering:
>
> generic
>    type Real is digits <>;  -- To work with any floating-point type
> function Generic_Elliptic_Integral (X : Real; K : Real) return Real;
>
> The following implementation is illegal:
>
> function Generic_Elliptic_Integral (X : Real; K : Real) return Real is
>    type Internal is digits Real'Digits * 2;  -- You cannot do that!
> begin
>    ...

I don't fully understand the code, but it does seem to be very
intuitive. What does

type Real is digits <>;

mean? Is "digits" a keyword of the language? I guess Ada groups
fundamental types in categories and "digits" mean we must use some
floating point type as the template argument, right? 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 <>".

> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de

From: Elias Salomão Helou Neto on
On Aug 10, 9:57 am, Yannick Duchêne (Hibou57)
<yannick_duch...(a)yahoo.fr> wrote:
> Le Tue, 10 Aug 2010 14:26:25 +0200, Phil Clayton  
> <phil.clay...(a)lineone.net> a écrit:
>
> >   A(A'Length - I - 1)
>
> > (not valid Ada, of course.)
>
> A(A'Last - (I - A'First))
>
> > Perhaps "reverse" would be a better name for the operator "not" when
> > the modulus is not a power of two?
>
> Or "wrapped" ? But this would only make sens on an array instance basis,  
> not on type basis. If the array actual range is not the same as the index  
> type range, it fails. So this could not be a type primitive (operator).  
> May be an array attribute ? A'From_Last ? A'Last_Based ?
>
> "reverse" is already there for loop statements. I feel this makes for sens  
> for loop, as what is reverse, is not the array index: this is the  
> iteration which goes reversed order.
>
> --
> 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.

It is a pity that this post became a technical discussion on array
indexing. A simple question that could be asked in a single line is:
can Ada access arrays without range checking? My algorithm needs not
wrapping, neither it needs range checking!

Please, try avoiding pointless discussions on wrapped/non-wrapped
types. Recall that I do not even know Ada's syntax, let alone its
sophisticated type system. Your discussion can only possibly confuse
me.

Elias.
From: Shark8 on
On Aug 10, 7:52 am, Elias Salomão Helou Neto <eshn...(a)gmail.com>
wrote:

> > function Generic_Elliptic_Integral (X : Real; K : Real) return Real is
> > type Internal is digits Real'Digits * 2; -- You cannot do that!
> > begin
> > ...
>
> I don't fully understand the code, but it does seem to be very
> intuitive. What does
>
> type Real is digits <>;
>
> mean? Is "digits" a keyword of the language? I guess Ada groups
> fundamental types in categories and "digits" mean we must use some
> floating point type as the template argument, right? 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 <>".
>
> > --
> > Regards,
> > Dmitry A. Kazakovhttp://www.dmitry-kazakov.de
>
>

"Digits" is a keyword in the language, in particular it is used to
define the number of digits-of-precision in a floating-point type. The
<> in this case is telling the compiler that the type 'Real', which is
the name some floating-point type will be referred to in the generic;
the purpose is to allow the programmer the ability to write code that
is independent of the precision but dependent on the [properties of
the] class of type.