comp.lang.ada
 help / color / mirror / Atom feed
From: Mikhail Terekhov <terekhov@emc.com>
Subject: Re: GCC 4.0 Ada.Containers Cursor danger.
Date: Mon, 18 Jul 2005 23:11:16 -0400
Date: 2005-07-18T23:11:16-04:00	[thread overview]
Message-ID: <gb_Ce.8815$LC6.6336@fe06.lga> (raw)
In-Reply-To: <42d6ef47$0$7644$9b4e6d93@newsread2.arcor-online.net>

Georg Bauhaus wrote:
> Mikhail Terekhov wrote:
> 
>>     - First is that all containers have an order.
>>       Again, sets and hashes in general are *not ordered*.
>>       To impose an order on them standard needs to put a
>>       restriction on implementarion algorithms.
> 
> 
> Ada.Containers require that the elements of a set say can be
> had one after the other through iteration using cursors.

That is right. That is exactly what I'm trying to say. Except one thing. 
There is no need for cursors during iterations.

> I can hardly think of this as a restriction in practice.

This is not a restriction. This is clarity. Cursors are not needed for 
iteration. Cursors are a special case, they should be marked as such.

> On the contrary, why should I be satisfied with just membership

No you shouldn't. But membership test is a most basic operation which 
has exactly the same semantics for _any_ container type. This operation 
should be present for all container types and it doesn't depend on order 
or cursors.

> test? Or with a fixed set of operations on sets? Or why should I
> abandon sets in favor of some other containers, because they
> fit better with someone's ideas of how a mathematical structure
> is sometimes defined? ;-)

No, you shouldn't be limited with a fixed set of operations on sets and 
you shouldn't abandon sets. I'm not really sure where did you get this 
idea from. This is almost in reverse of what I'm trying to say. You 
should be able to chose whatever abstraction fits your application best 
and this abstraction should be familiar to anyone who will read your 
source code. Sorted sets and hashes probably are very useful in some 
special cases, but they are just a special cases. It is not a very good 
idea to put some special cases as a _basis_ of the standard library. 
There is nothing wrong to put them into standard library, but only as an 
addition to or specialization of basic abstractions.

> 
> Say I want a subset B of A,
> 
>  B := {x: x in A | x >= 0};
> 
> This could be done using a generic algorithm which is similar
> to the Generic_Find algorithm that Matt has presented in that
> there will be a generic formal procedure that acts
> as the predicate ">= 0?".
> 
> generic
>   with function Predicate(C: Cursor) return Boolean;
> function Subset(S: Set) return Set;
> 
> function Subset(S: Set) return Set is
>   C: Cursor := First(S);
>   result: Set;
> begin
>   while Has_Element(C) loop
>      if Predicate(C) then
>         Include(result, Element(C));
>      end if;
>      Next(C);
>   end loop;
>   return result;
> end Subset;
> 
> (The example doesn't really need iteration using cursors explicitly
> because Iterate(S, Conditional_Include'access) or similar will do.
> However, that's just an ad hoc example that I could think of and I wanted
> to show cursors :-)

That is wonderful! Almost any example of using cursor doesn't really 
need cursors. Why this burden then?

> 
> 
>> You example
>>       is completely legal generic function in the current
>>       Ada.Containers framework. But if you decide at some
>>       point to add unordered containers, then you generic
>>       function became invalid because for unordered containers
>>       "first" and "last" have no sence at all.
> 
> 
> For me, First and Last (or whatever you call them) *do* make sense
> for any reasonable notion of a set in programming, sorted or not,
> even when used in computer science.
> Their purpose then is to start and end the enumeration of the elements

Try to think a little bit farther. If there is no order or sorting, what 
is the meaning of First and Last? In your example First and Last denote 
a subset where to search and this is possible (to denote a subset using 
First and Last) only for ordered containers.

> in the set. You can think of cursors as elements of a mathematical
> index set (yes!) that is associated with the elements in some set.
> Such as is used for example in proofs by induction, or when you want
> to transform a set into a sorted set, etc.

I couldn't get this part about proofs by induction, could you explain?
There is nothing wrong if some program needs a sorted container. But in 
this case (I'm afraid to use this controversal term now, but couldn't 
came up with anything better :)) it is natural to use containers that 
explicitly support sort operation, because there are probably a number 
of sort orders and Sorted_Set can support just one that is 
implementation defined. Then your algorithm will be clear, 
implementation independent, easily understandable and probably more 
efficient.

> 
> I find Cursors useful when I want to put an index set into operations
> inside a computer. When I'm can use any container,
> how could I implement index sets without Cursors?

Index sets require indirection which is provided by the core language, 
i.e. access types; they require no cursors.

> 
>>     - Second is that find/search operation makes sence only
>>       for a mapping container i.e. vector or hash;
> 
> 
> Find is a membership test, how could it not make sense for
> a set?

Find is not a membership test. Membership test is more generic, i.e it 
has _the same_ semantics for _any_ kind of container, namely "does this 
container contain this element or not?" Membership test is not a 
replacement for various kinds of search and vice versa. Find is more 
diverse and is not always make sense. For "mappings" i.e. vectors and 
hashes for example "Find" could return a key (index for vectors) of the 
element or a list/vector/set of keys whose elements satisfy some 
predicate. For ordered containers that are not mappings (lists, trees) 
"Find" could degenerate into membership test or could became "FindNext", 
"FindPrev" or "Count". For sets "Find" degenerates into membership test 
or could return a subset according to some predicate (as in your example).
So, you see that "Find" differs for diffrent container types. It is 
"less generic" than membership test.

> 
>> there is no sence to search for the element
>>       if you already have it.
> 
> 
> (I had called my procedure "silly".) Programming is not equal
> to logic ;-) Notice two uses:

Exactly, but "silly" example can't prove anything.

> 
> (1) > True generic operation for all container types can be membership
> 
>> test not find/search.
> 
> This is a membership test, but only accidentally. Think of an extended
> algorithm that gives you a set of cursors of all occurences of
> some item in a container, whatever kind of container it is.

If you need just a set of references to some elements, then you are all 
set. Ada provides access types, why cursor?

> 
>> Another problem with your example is that it is essentially a NOP.
>> If you already have a cursor for your element why to search for it?
> 
> 
> (2) The Cursor "item" designates an element in _some_ container.
> This need not be the container that is searched.
> Start and Stop can refer to a different container.
> The algorithm will find the element in that container.

Cursors could be an implementation detail, they are not needed for 
general API. Start and Stop can have no sense for some containers. 
Algorithm can use internally whatever it needs to deliver result.

> 
>> Some times cursors replace
>> collections completely! That kind of indirect interface is very
>> dangerous in my view.
> 
> 
> And Cursors enable programmers to do their work. Cursors add indirection.

Programmers are perfecly able to do their work without Cursors, at least 
most of them (I'm not counting authors of Ada.Containers ;). I would say 
Cursors add "double indirection" :) - for elements and container itself.

> Imagine programming without indirection, whatever the indirection
> mechanism looks like.

That would be a disaster :), let's say clear and loudly: "we love 
indirection", and let's use it explicitly via access types.:)

> 
> 
>> Now to answer you question, just replace "My_Container" by
>> "My_Vector_Container", then "item: My_Vector_Container.Cursor" by
>> "item: My_Vector_Container.Element_Type" and then "Cursor" by "Index"
>> in that order and you will get a "vectorized" version of you
>> function ;)
> 
> 
> Forcing every client program to use vectors...
> 
Where did you get this idea from?

Regards,
Mikhail



  parent reply	other threads:[~2005-07-19  3:11 UTC|newest]

Thread overview: 195+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-04 11:01 GCC 4.0 Ada.Containers Cursor danger Dmitriy Anisimkov
2005-07-04 18:56 ` Georg Bauhaus
2005-07-04 19:07   ` Georg Bauhaus
2005-07-05  4:27   ` Dmitriy Anisimkov
2005-07-05 15:01     ` Matthew Heaney
2005-07-06  9:10   ` Maxim Reznik
2005-07-06 10:45     ` Georg Bauhaus
2005-07-06 13:57       ` Maxim Reznik
2005-07-06 14:53         ` Georg Bauhaus
2005-07-06 15:09         ` Matthew Heaney
2005-07-06 16:37           ` Dmitriy Anisimkov
2005-07-06 16:43             ` Matthew Heaney
2005-07-06 22:24         ` Randy Brukardt
2005-07-07 10:23         ` Alex R. Mosteo
2005-07-06 12:41     ` Matthew Heaney
2005-07-06 15:37     ` Matthew Heaney
2005-07-06 21:51   ` Randy Brukardt
2005-07-05 14:51 ` Matthew Heaney
2005-07-05 17:11   ` Dmitriy Anisimkov
2005-07-05 18:02     ` Matthew Heaney
2005-07-05 19:08       ` Dmitriy Anisimkov
2005-07-05 19:26         ` Matthew Heaney
2005-07-05 19:44           ` Dmitriy Anisimkov
2005-07-05 20:06             ` Matthew Heaney
2005-07-06  2:10               ` Dmitriy Anisimkov
2005-07-06 22:44                 ` Randy Brukardt
2005-07-07  3:41                   ` Dmitriy Anisimkov
2005-07-07 19:18                     ` Randy Brukardt
2005-07-08  3:01                       ` Dmitriy Anisimkov
2005-07-09  6:17                         ` Simon Wright
2005-07-11  2:24                           ` Dmitriy Anisimkov
2005-07-11  2:24                           ` Dmitriy Anisimkov
2005-07-06  5:52     ` Martin Dowie
2005-07-06  7:02       ` Dmitriy Anisimkov
2005-07-06  8:02         ` Georg Bauhaus
2005-07-06  8:37           ` Dmitriy Anisimkov
2005-07-06  9:06             ` Pascal Obry
2005-07-06 12:14             ` Georg Bauhaus
2005-07-06  7:53       ` Pascal Obry
2005-07-06  8:44         ` Dmitriy Anisimkov
2005-07-06  9:03           ` Pascal Obry
2005-07-06  9:34             ` Dmitriy Anisimkov
2005-07-06  9:42               ` Pascal Obry
2005-07-06  9:45                 ` Dmitriy Anisimkov
2005-07-06 10:40                   ` Georg Bauhaus
2005-07-06 16:22                     ` Dmitriy Anisimkov
2005-07-06 16:42                       ` Matthew Heaney
2005-07-06 16:59                         ` Dmitriy Anisimkov
2005-07-06 17:12                           ` Matthew Heaney
2005-07-06 18:12                       ` Georg Bauhaus
2005-07-07 12:29                         ` Dmitriy Anisimkov
2005-07-07 12:46                           ` Matthew Heaney
2005-07-07 13:01                             ` Dmitriy Anisimkov
2005-07-07 13:20                               ` Matthew Heaney
2005-07-07 13:54                           ` Georg Bauhaus
2005-07-07 17:56                             ` Dmitriy Anisimkov
2005-07-07 22:12                               ` Georg Bauhaus
2005-07-15 18:03                                 ` Dmitriy Anisimkov
2005-07-16  1:45                                   ` Matthew Heaney
2005-07-17  3:55                                     ` Dmitriy Anisimkov
2005-07-17  4:29                                       ` Matthew Heaney
2005-07-07 19:29                           ` Randy Brukardt
2005-07-08  2:41                             ` Dmitriy Anisimkov
2005-07-06 22:56               ` Randy Brukardt
2005-07-06 22:51         ` Randy Brukardt
2005-07-07  0:24           ` Matthew Heaney
2005-07-07  3:20             ` Randy Brukardt
2005-07-06  7:30     ` Dmitry A. Kazakov
2005-07-06  7:50       ` Georg Bauhaus
2005-07-06  8:11         ` Dmitriy Anisimkov
2005-07-06 11:36         ` Dmitry A. Kazakov
2005-07-06 12:14           ` Georg Bauhaus
2005-07-06 23:07           ` Randy Brukardt
2005-07-07  8:01             ` Dmitry A. Kazakov
2005-07-07 10:38               ` Georg Bauhaus
2005-07-07 13:00                 ` Dmitry A. Kazakov
2005-07-07 13:41                   ` Matthew Heaney
2005-07-07 22:12                   ` Georg Bauhaus
2005-07-08  8:48                     ` Dmitry A. Kazakov
2005-07-08 10:41                       ` Georg Bauhaus
2005-07-08 13:03                         ` Dmitry A. Kazakov
2005-07-08 13:31                           ` Matthew Heaney
2005-07-10  2:12                           ` Randy Brukardt
2005-07-10  8:52                             ` Dmitry A. Kazakov
2005-07-11 10:58                               ` Georg Bauhaus
2005-07-11 12:18                                 ` Dmitry A. Kazakov
2005-07-11 13:50                                   ` Georg Bauhaus
2005-07-11 18:38                               ` Randy Brukardt
2005-07-12  8:44                                 ` Dmitry A. Kazakov
2005-07-12 10:33                                   ` Georg Bauhaus
2005-07-12 20:38                                   ` Randy Brukardt
2005-07-08 13:15                       ` Matthew Heaney
2005-07-08 14:02                         ` Dmitry A. Kazakov
2005-07-08 14:52                           ` Matthew Heaney
2005-07-11 14:57                             ` MMM
2005-07-11 18:36                               ` Georg Bauhaus
2005-07-12  2:11                                 ` MMM
2005-07-12 21:47                                   ` Randy Brukardt
2005-07-13  4:31                                     ` MMM
2005-07-13  1:15                                   ` Georg Bauhaus
2005-07-13  2:46                                     ` Matthew Heaney
2005-07-14  4:11                                     ` Mikhail Terekhov
2005-07-14 12:44                                       ` Matthew Heaney
2005-07-19  1:38                                         ` Mikhail Terekhov
2005-07-19  3:21                                           ` Matthew Heaney
2005-07-14 23:03                                       ` Georg Bauhaus
2005-07-15  8:36                                         ` Dmitry A. Kazakov
2005-07-15 10:39                                           ` Georg Bauhaus
2005-07-15 14:10                                             ` Dmitry A. Kazakov
2005-07-15 12:10                                           ` Matthew Heaney
2005-07-19  3:51                                             ` Mikhail Terekhov
2005-07-19 11:35                                               ` Matthew Heaney
2005-07-19  3:11                                         ` Mikhail Terekhov [this message]
2005-07-19 12:44                                           ` Matthew Heaney
2005-07-20  5:20                                             ` Simon Wright
2005-07-21  2:39                                               ` Matthew Heaney
2005-07-21  3:23                                               ` Randy Brukardt
2005-07-19 23:51                                           ` Randy Brukardt
2005-07-20 15:33                                             ` Robert A Duff
2005-07-11 14:56                         ` MMM
2005-07-11 23:24                           ` Matthew Heaney
2005-07-12  3:05                             ` MMM
2005-07-12  5:32                               ` Simon Wright
2005-07-13  2:41                                 ` MMM
2005-07-12 11:16                               ` Georg Bauhaus
2005-07-16 22:28                                 ` Robert A Duff
2005-07-12 13:32                               ` Marc A. Criley
2005-07-12 14:51                                 ` MMM
2005-07-12 15:35                                   ` Matthew Heaney
2005-07-12 18:40                                     ` MMM
2005-07-12 19:12                                       ` Matthew Heaney
2005-07-12 19:42                                         ` MMM
2005-07-12 20:02                                           ` Georg Bauhaus
2005-07-13  3:52                                             ` MMM
2005-07-12 20:13                                           ` Matthew Heaney
2005-07-12 21:38                                     ` Simon Wright
2005-07-12 17:44                                   ` Marc A. Criley
2005-07-12 18:51                                     ` MMM
2005-07-12 19:15                                       ` Matthew Heaney
2005-07-12 19:47                                         ` Georg Bauhaus
2005-07-13  2:20                                           ` Matthew Heaney
2005-07-12 20:00                                         ` MMM
2005-07-12 20:09                                           ` Georg Bauhaus
2005-07-12 20:15                                           ` Matthew Heaney
2005-07-12 21:01                                           ` Randy Brukardt
2005-07-13  4:16                                             ` MMM
2005-07-19 23:58                                               ` Randy Brukardt
2005-07-12 21:59                                         ` Simon Wright
2005-07-12 20:56                                       ` Randy Brukardt
2005-07-14  5:01                                         ` Mikhail Terekhov
2005-07-20  0:10                                           ` Randy Brukardt
2005-07-07 12:36               ` Matthew Heaney
2005-07-07 12:52             ` Dmitriy Anisimkov
2005-07-07 13:52               ` Georg Bauhaus
2005-07-07 17:49               ` Dmitriy Anisimkov
2005-07-07 18:35                 ` Matthew Heaney
2005-07-08 17:52                   ` Craig Carey
2005-07-07 17:50               ` Dmitriy Anisimkov
2005-07-07 19:47               ` Randy Brukardt
2005-07-08  2:28                 ` Dmitriy Anisimkov
2005-07-09 14:20                   ` Matthew Heaney
2005-07-10  1:51                   ` Randy Brukardt
2005-07-10  5:46                     ` Craig Carey
2005-07-10  6:13                       ` Craig Carey
2005-07-11 17:33                       ` OT: Greg and Colin? (was Re: GCC 4.0 Ada.Containers Cursor danger.) Marc A. Criley
2005-07-06 22:34     ` GCC 4.0 Ada.Containers Cursor danger Randy Brukardt
2005-07-07  0:22       ` Matthew Heaney
2005-07-07  3:17         ` Randy Brukardt
2005-07-08  5:34           ` Jeffrey Carter
2005-07-10  1:53             ` Randy Brukardt
2005-07-10 19:32               ` Jeffrey Carter
2005-07-07  3:24         ` Randy Brukardt
2005-07-16 23:24 ` Matthew Heaney
2005-07-17  4:04   ` Dmitriy Anisimkov
2005-07-17  5:01     ` Matthew Heaney
2005-07-17 17:13       ` Dmitriy Anisimkov
2005-07-17 17:36         ` Matthew Heaney
2005-07-17 17:49           ` Dmitriy Anisimkov
2005-07-17 18:12             ` Matthew Heaney
2005-07-17 17:40       ` Dmitriy Anisimkov
2005-07-17 17:50         ` Dmitriy Anisimkov
2005-07-17 18:08         ` Matthew Heaney
2005-07-19  4:36           ` Mikhail Terekhov
2005-07-20  1:59             ` Matthew Heaney
2005-07-20 14:00               ` Pascal Obry
2005-07-20 14:34                 ` Matthew Heaney
2005-07-20 16:51                   ` Pascal Obry
2005-07-20 16:53                   ` Pascal Obry
2005-07-21  3:18                   ` Randy Brukardt
2005-07-20  2:37             ` Robert I. Eachus
2005-08-02 16:59               ` Craig Carey
2005-08-02 20:55                 ` Simon Wright
2005-07-20  7:20             ` Georg Bauhaus
2005-07-17  9:28     ` Georg Bauhaus
2005-07-17 14:26       ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox