Teaching new tricks to an old dog (C++ -->Ada)

  • Thread starter Turamnvia Suouriviaskimatta
  • Start date
P

Pascal Obry

Ioannis Vranos said:
How can this be done in Ada?

As it can be seen here, the bullet-proof high level facilities are used. :)

Ok, you want to play this game :) I like it...

How would you implement this (100% Ada, no external libraries as used in
your C++ example):

package API is
pragma Remote_Call_Interface;

procedure Call_Me (Str : in String);
end API;

Seriously, we are not going to build thousands of examples for C++ and Ada,
right ?

Pascal.

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
 
M

Martin Dowie

Jerry said:
The subject at hand was the use of C++ in the air traffic control
system.

As such, X running on top of Windows and/or UNIX becomes relevant ONLY
when tied to the development of air traffic control software. This page
shows exactly that. A page only about X on Windows or UNIX would not.

Well, I don't think this is *big* news to anyone! I wasn't expecting
these things to run bare-board machines. (Actually I do know a /little/
about ATC, as my brother worked for years at West Drayton, the control
centre for Heathrow).

Cheers

-- Martin
 
P

Pascal Obry

Well, it's time for me to join the 'American Dentist Association vs
C++' flame war:

You've probably not read the whole thread. It seems to me that we are far from
a language flame war. At least I found (being on the Ada side) some very
interresting messages about C++ here.

Or maybe you are trying to start one!

Pascal.

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
 
P

Pascal Obry

Alex R. Mosteo said:
Boh, the flaming here has been pretty low. In fact I'm getting very
interesting info from both fronts and both languages.

Exactly, the C++ meta-template programming seems quite interesting.

Pascal.

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
 
P

Preben Randhol

With Ada aside (I find no reason why one should not learn it), C++ is a
powerful and systems programming language, and power implies painful low
level details. However it also provides all major high level facilities,
and if you stick in high level programming it is very safe, while it
maintains the maximum space and run-time efficiency principle.


C++ programmers will claim to their death that they are infallible and
everybody else isn't. However, none of them seem to understand how ignorant
they are...

If C++ is safe, then so is a bog.

Preben
 
X

xpyttl

C++ programmers will claim to their death that they are infallible and
everybody else isn't. However, none of them seem to understand how ignorant
they are...

And this is different from Ada programmers how?

There are obviously two groups here. First, there are the Ada guys who seem
to think that modern C++ is the same language Dennis Ritchie invented 30
years ago. Then there are the C++ guys who view Ada as a sort of military
style Cobol.

The two camps are equally close to the truth.

...
 
P

Pascal Obry

xpyttl said:
There are obviously two groups here. First, there are the Ada guys who seem
to think that modern C++ is the same language Dennis Ritchie invented 30
years ago. Then there are the C++ guys who view Ada as a sort of military
style Cobol.

The two camps are equally close to the truth.

Right, both statements are certainly wrong.

Yet, Preben point was that Ada has been designed from the start to be safe
were C++ was not. As many have pointed out there is different C++, one based on
templates, STL... and is safer... but yet a programmer is a programmer. What we
are saying on the Ada side is that at some point you'll exit the safe side for
some reasons (I'm sure nobody will say this impossible, we are just human) and
the compiler will never tell. Code review will certainly help. But again this
is costly.

Most of us on the Ada side are considering that what is important in a
language is not what it permits but what it prohibits for just this reason. We
will all step on the wrong side at some point, we want the language and the
compiler to be able to warn us. Of course this can look frustrating but it is
invaluable in the long run.

I can't speak for the whole Ada community, so if you do not share this view,
feel free speak up :)

Note that it is for this very reason that Java was born (a safer C++). Java
has indeed removed lot of unsafe constructs from C++.

Pascal.

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
 
R

Robert A Duff

Alex R. Mosteo said:
Boh, the flaming here has been pretty low.

Yes. That's refreshing.
...In fact I'm getting very
interesting info from both fronts and both languages.
^^^^^^^^^^^^^^^^

"Interesting", indeed. ;-) Unfortunately, about half of this
information is complete nonsense -- about both languages! Plus some
misinformation about what optimizing compilers can and cannot do, thrown
in for good measure.

- Bob
 
P

Paul E. Bennett

xpyttl said:
And this is different from Ada programmers how?

There are obviously two groups here. First, there are the Ada guys who
seem to think that modern C++ is the same language Dennis Ritchie invented
30
years ago. Then there are the C++ guys who view Ada as a sort of military
style Cobol.

The two camps are equally close to the truth.

I fall into the third camp.

Now, I know a lot of people who do use C, C++, Ada and Java in mission
critical systems and do so successfully. Most of them use some form of
checking tools (even the Ada guys). It is certainly a long way from the "if
it compiles it is right" attitude that has seemed to have been purported by
just a very few of the Ada crowd here.

I have always held that the language used is immaterial to the safety of
final product (I certainly stated it often enough here and in c.a.e). I
acknowledge that some things can be made easier or harder by language
choice. However, it is mainly in the development process you use where the
integrity of the product is nurtured and the evidence of successful
development is produced.

--
********************************************************************
Paul E. Bennett ....................<email://[email protected]>
Forth based HIDECS Consultancy .....<http://www.amleth.demon.co.uk/>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-811095
Going Forth Safely ....EBA. http://www.electric-boat-association.org.uk/
********************************************************************
 
W

Wes Groleau

xpyttl said:
And this is different from Ada programmers how?

That's certainly an exaggeration about C++ programmers,
but it's not even close to an Ada programmer. An Ada
programmer likes Ada because he/she knows he/she is NOT
infallible.
 
R

Robert A Duff

Jim Rogers said:
Yes you can. You can always program in your own checks manually.
The drawback of doing that is that you cannot simply turn them off
with a pragma, nor can the compiler so effectively optimize those
checks out of your program when they are unneeded.

Ada compilers can optimize away manually-programmed checks as well as
built-in checks. Whether it can do so in any particular situation
depends on the code -- how much information is available to allow the
compiler to prove things -- but not so much on whether the checks are
built in.

Likewise, C++ compilers can optimize away manually-programmed checks in
some cases.

- Bob
 
R

Randy Brukardt

....
So far, no C++ advocate seems to have said anything similar to what you
claim, but in a way you're probably right. If we depended on Ada to do
the job, the Internet (for one) would be drastically more secure,
though only because nobody used it! :) (<- explicit smiley for those
who seem to need them...)

With or without the smiley, your comment is just the kind of unsupported
anti-Ada stuff that we see all the time. Why would the Internet not be used?
Ada is a fully general purpose programming language, and if the Internet was
mostly written in Ada, the applications and uses would be pretty much the
same (just with fewer trivial bugs).

The web server and search engine we run are completely written in Ada; our
mail server is partially written in Ada. As a client, you won't notice any
difference (other than the slowness of being behind a fairly slow Internet
connection).

Randy Brukardt
 
R

REH

Ed Falis said:
Fortunately, it mostly hasn't been that bad as language debates go ;-)

- Ed

I have been following this thread with great interest. C++ is by far my
favorite language, but use Ada almost exclusively at work. I love C++, but
I would not relish creating a large-scale, fault-tolerant system in it,
especially involving a large team with varying levels of skill. This thread
has been surprisingly civil with only one or two individuals using childish
attacks and half-truth to argue their side (I won't say which). Though, I
had to cringe when some in the C++ camp attempted to argue its safety vs.
Ada. I would not, myself, attempt that argument. I think its a great
language, and gives you a lot of flexibility that you can build safety upon,
but does not inherently come that way. On the other hand, it takes effort
to write an Ada program that will crash. If it compiles, it will usually
run. It may not do what you intended, of course, but it won't "core dump"
or cause a reboot, etc. Although I have to admit, I prefer C++'s OO model.
I find it easier (personally) to see the encapsulation, and Ada's
dispatching rules still confuse my. Can someone explain why they chose the
package as the encapsulation level for a class?

Ada features I would love to have in C++:
* "new types" and sub-ranges
* runtime template instantiation (I think being able to instantiate generics
dynamically is the coolest thing!)
* subprograms inside subprograms. I think it would be great for use with
the STL (i.e., using a local function with std::foreach)
* packages. namespaces are nice, but packages make modularity of very large
system a lot easier. Especially now with "use type"
* representation specifications!!!!

C++ features I would to have in Ada:
* Implicit instantiation. Yeah, I know, it can be unsafe, but that is one
thing I really like about C++: the ability to automate repetitive things.
* actually having a class object encapulating the data and methods.
* reference (in out) types in functions (without resorting to access types)
* meta-templates. very neat stuff. ugly, but neat.
* The STL!!! (though I hear something similar is coming?)
 
R

Robert A Duff

CTips said:
- Usually the checks will be added every place they are needed, and then
the usual optimizations will be used to eliminate them. In other words,
if one adds the checks manually, the compiler should eliminate them
identically.

That's true in cases where the manual checks are equivalent to the
built-in checks. But there are important cases where they're not. For
example, if you pass an integer as a parameter, and inside the called
procedure use that as an array index, it is typically natural to declare
the integer parameter as a subrange whose bounds match the array bounds
(even if those bounds are dynamic). So the array-indexing check can be
optimized away. C++ has no integer subranges, so if you did the
equivalent in C++, with a manual check, the compiler could not normally
optimize it away.

OTOH, I suppose you could program integer subranges in C++ using
templates.

You are correct that Ada's run-time checks are not entirely free in all
cases. In order to check the bounds, the bounds must be stored in
memory, which is a cost. And even if the checks are suppressed, I don't
know of any Ada compiler that will avoid storing the bounds. This is
partly because in Ada you can query those bounds (Some_Array'Length, for
example). On the other hand, if you're querying the bounds, the
equivalent in C++ would have to store the bounds, too.
How easy is it to build an arena allocator in Ada?

Pretty straightforward. You write a package containing a "storage pool
type", with operations like Allocate (which returns an Address).
You then attach your storage pool to particular pointer types,
and then the generated code will call your operations instead of the
compiler-provided ones. Inside your storage pool code, you will no
doubt have to do all kinds of low-level stuff -- address arithmetic,
perhaps. Ada has plenty of support for that level of coding.
Given a processor with load-word-locked and store-word-conditional, how
would I build an atomic increment function?

Use a machine-code insert. Same way you'd do it in any language, I
suppose. And wrap it in an abstraction layer so you can have some hope
of porting to a machine without those instructions.

- Bob
 
R

Randy Brukardt

Jim Rogers wrote:

I would have done this program somewhat differently.
with Ada.Text_Io;
with Ada.Numerics.Discrete_Random;

procedure Random_Fill is
type Rand_Values is range 0..10_000;
package My_Rand is new Ada.Numerics.Discrete_Random(Rand_Values);

Seed : My_Rand.Generator;
subtype Index_Type is Natural range 1..1000;
Vec : array(Index_Type) of Rand_Values;
Found : Natural := 0;
begin
My_Rand.Reset(Seed); -- seeds the random number generator

-- fill the array with random values in the range of 0
-- through 10,000
for I in Vec'range loop
Vec(I) := My_Rand.Random(Seed);
end loop;

Much better to use an aggregate here:

Vec := (others => My_Rand.Random(Seed));

The random function will get called once for each component. Let the
compiler write the loop; it can't do a worse job, and it might actually do
better.
-- finds first index where a value is less than 1000
for I in Vec'range loop
if Vec(I) < 1000 then
Found := I;
exit;
end if;
end loop;
if Found > 0 then
Ada.Text_Io.Put_Line("Number < 1000 found at index" &
Natural'Image(Found));
else
Ada.Text_Io.Put_Line("No number < 1000 was found");
end if;

I generally stick a test for 'Last into the loop. But that's more because
I'm lazy.

-- finds first index where a value is less than 1000
for I in Vec'range loop
if Vec(I) < 1000 then
Ada.Text_Io.Put_Line("Number < 1000 found at index" &
Natural'Image(I));
exit;
elsif I = Vec'Last then
Ada.Text_Io.Put_Line("No number < 1000 was found");
exit;
end if;
end loop;

But in this case, you really ought to not use a for loop at all:

declare
I : Index_Type'Base := Vec'First;
begin
while I <= Vec'Last loop
if Vec(I) < 1000 then
Ada.Text_Io.Put_Line("Number < 1000 found at index" &
Natural'Image(I));
exit;
end if;
I := I + 1;
end loop;
if I <= Vec'Last then
Ada.Text_Io.Put_Line("Number < 1000 found at index" &
Natural'Image(I));
else
Ada.Text_Io.Put_Line("No number < 1000 was found");
end if;
end;

But of course this is a bit more prone to errors.

The best plan is to use a container function that does the searching, so you
don't have to.

That's probably more on this topic than anyone wanted to know. :)
end Random_Fill;

Randy Brukardt
 
G

Georg Bauhaus

Martin said:
COBOL _was_ military...well Navy at least :)

The inventor of COBOL reports basket ball as a major
influence (passing indiretly). And the effect of
private account balancing on the choice of number
systems (Oops, summed the spendings in octal...).


Georg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top