From: Arjen Markus on
On 1 apr, 06:20, "Gerry Ford" <ge...(a)nowhere.ford> wrote:
> "Craig Powers" <eni...(a)hal-pc.org> wrote in message
>
> news:47f16d7d$0$1732$a726171b(a)news.hal-pc.org...
>
>
>
> > Craig Dedo wrote:
>
> >> I would like to throw out a thought question:
>
> >> What is the best way to implement generic programming and generic
> >> procedures? What are the trade-offs involved in various approaches?
> >> What languages use the best approaches? What improvements could be made
> >> in those approaches? What languages do it badly and what are the
> >> shortcomings of those approaches?
>
> >> I'm looking not only for ease and robustness of implementation, but
> >> also robustness and ease of use in using the generic programming feature
> >> for developing applications in Fortran.
>
> > My main experience with using generic programming is templates in C++.
> > Unfortunately, I haven't used them extensively enough to comment on their
> > major shortcomings, nor have I used other forms of generic programming
> > enough to comment on the relative advantages and disadvantages of C++.
> > However, if you were to ask that question in a C++ newsgroup (e.g.
> > comp.lang.c++.moderated) you would probably get some very good answers
> > about the plusses and minuses of C++ itself, and you might even get some
> > good comparitive info from anyone who has used another language with
> > generic programming features.
>
> http://www.stlport.org/resources/StepanovUSA.html
> The above is an informative link and below is an excerpt.
>
> Question:
> Could you explain to a modest C++ programmer what Generic Programming is,
> what is the relation of Generic Programming with C++ and STL, and how did
> you come to use Generic Programming in a C++ context?
>
> Answer:
> Generic programming is a programming method that is based in finding the
> most abstract representations of efficient algorithms. That is, you start
> with an algorithm and find the most general set of requirements that allows
> it to perform and to perform efficiently. The amazing thing is that many
> different algorithms need the same set of requirements and there are
> multiple implementations of these requirements. The analogous fact in
> mathematics is that many different theorems depend on the same set of axioms
> and there are many different models of the same axioms. Abstraction works!
> Generic programming assumes that there are some fundamental laws that govern
> the behavior of software components and that it is possible to design
> interoperable modules based on these laws. It is also possible to use the
> laws to guide our software design. STL is an example of generic programming.
> C++ is a language in which I was able to produce a convincing example.
>
> When I first read this a couple weeks ago, I wondered what this looks like
> in fortran:
>
> template <class StrictWeakOrdered>
> inline StrictWeakOrdered& max(StrictWeakOrdered& x,
> StrictWeakOrdered& y) {
> return x < y ? y : x;
>
> }
>
> and
> template <class StrictWeakOrdered>
> inline const StrictWeakOrdered& max(const StrictWeakOrdered& x,
> const StrictWeakOrdered& y) {
> return x < y ? y : x;
>
> }
>
> --
>
> "I am waiting for them to prove that God is really American."
>
> ~~ Lawrence Ferlinghetti

That is an easy one:

max(x,y)

Well, this Fortran equivalent is a trifle less powerful than the C++
one,
but that does not mean it is not basically true :).

Regards,

Arjen
From: Arjen Markus on
On 3 apr, 02:39, "Gerry Ford" <ge...(a)nowhere.ford> wrote:
> "Arjen Markus" <arjen.mar...(a)wldelft.nl> wrote in message
>
> news:d9078f0f-d62f-4d3b-8cea-caf6168f563d(a)e23g2000prf.googlegroups.com...
>
>
>
> > On 1 apr, 06:20, "Gerry Ford" <ge...(a)nowhere.ford> wrote:
> >> When I first read this a couple weeks ago, I wondered what this looks
> >> like
> >> in fortran:
>
> >> template <class StrictWeakOrdered>
> >> inline StrictWeakOrdered& max(StrictWeakOrdered& x,
> >> StrictWeakOrdered& y) {
> >> return x < y ? y : x;
>
> >> }
>
> >> and
> >> template <class StrictWeakOrdered>
> >> inline const StrictWeakOrdered& max(const StrictWeakOrdered& x,
> >> const StrictWeakOrdered& y) {
> >> return x < y ? y : x;
>
> >> }
>
> > That is an easy one:
>
> > max(x,y)
>
> > Well, this Fortran equivalent is a trifle less powerful than the C++
> > one,
> > but that does not mean it is not basically true :).
>
> Stepanov thought it was important that one of the above was & and the other
> was const &. The distinction is lost on me.
>
> My problem with c++ is that when you take out Microsoft and C, I'm not left
> with much. The template syntax is, e.g., beyond me.
>
> I like Stepanov's emphasis on algorithms.

I was in a somewhat frivolous mood when I wrote my answer, but indeed
Fortran
has had generic features at least since FORTRAN 77.

There are two important aspects to generic programming:
1. Notational: C++'s templates are one form, smart macros are another
2. Conceptual: creating a sort of meta-algorithm to solve a whole
class
of problems, not just a particular one (albeit one with lots of
parameters)

I like the description of the conceptual aspect, I think it captures
the
essence of what you want to achieve.

The notational aspect is just as important, but the two templates you
gave are
very idiosyncratic to C++: it has to do with passing values/objects by
reference
or by value (note: in C you _always_ pass by value, but sometimes you
pass
the address by value, resulting more or less effectively in a pass by
reference,
but it is up to you to use the asterisk in the right places).

As for generic design of algorithms: in my opinion, reverse
communication,
though it may not seem modern or even have a awkward reputation, is a
very
flexible solution on the client side to this:
You only have to fill in a few blanks in whatever way you find
necessary
and there you go.

A solution with callbacks is much more restrictive. The interface for
each callback is fixed. And it does not do much more to hide the
details,
you still need to fill the blanks.

But with the right design, either can do the job.

Regards,

Arjen
From: Craig Powers on
Gerry Ford wrote:
> "Arjen Markus" <arjen.markus(a)wldelft.nl> wrote in message
> news:d9078f0f-d62f-4d3b-8cea-caf6168f563d(a)e23g2000prf.googlegroups.com...
>> On 1 apr, 06:20, "Gerry Ford" <ge...(a)nowhere.ford> wrote:
>
>>> When I first read this a couple weeks ago, I wondered what this looks
>>> like
>>> in fortran:
>>>
>>> template <class StrictWeakOrdered>
>>> inline StrictWeakOrdered& max(StrictWeakOrdered& x,
>>> StrictWeakOrdered& y) {
>>> return x < y ? y : x;
>>>
>>> }
>>>
>>> and
>>> template <class StrictWeakOrdered>
>>> inline const StrictWeakOrdered& max(const StrictWeakOrdered& x,
>>> const StrictWeakOrdered& y) {
>>> return x < y ? y : x;
>>>
>>> }
>
>> That is an easy one:
>>
>> max(x,y)
>>
>> Well, this Fortran equivalent is a trifle less powerful than the C++
>> one,
>> but that does not mean it is not basically true :).
>
> Stepanov thought it was important that one of the above was & and the other
> was const &. The distinction is lost on me.

The distinction is that the former requires mutable parameters and
provides a mutable result, while the latter allows const parameters (at
the cost of only producing a const result).

You could write code like this with the former:
max(x, y) = max(x, y) + 10;

As a result, whichever of x or y contained the larger value gets 10
added to it.

(setting aside whether that would be appropriate -- keeping in mind even
if, at first blush, it appears to be a bad idea, there may be other
contexts where it is extremely useful for writing concise code)