Hello,

On 11/5/06, Bruno Oliveira <bruno.oliveira@comlab.ox.ac.uk> wrote:
Hello all,

Pablo Nogueira asked me to post this email in the mailing list since he
had some problems trying to post himself. Here is the text:

[...] 

Perhaps the entry would attract the attention of people with other definitions
of generic programming in mind, such as users of the C++ Standard Template
Library. C++ Templates are often roughly equated with a static version of
parametric polymorphism, but in the STL data types are abstract, not
algebraic. This is an important difference.

Just to make things clearer for me, what would the entry be for C++ STL?

[...]


3. Support for data abstraction is "generic" if generic programmers can write
   a single generic function definition for all (or many) abstract types. This
   is better than providing definitions *for each* abstract type. I hope you
   agree with me that the latter is not *generic* programming. I believe none
   of the existing generic programming approaches support this. Perhaps it is
   time to think about it when designing a generic library.

In Generic Haskell, support for abstract types is more complicated because,
unlike SYB, the language is designed to deal with types of arbitrary
kind. Just to pinpoint a particular problem, there is a lack of
parametrisation on type-class constraints. There follows some *pseudocode*
examples to illustrate the point.

Type-specific instances of generic functions would seem easy to define:

  gsize<Queue> :: Queue a -> Int
  gsize<Queue> = gsize<List> . QueueToList

  gsize<OrdSet> :: Ord a => OrdSet a -> Int
  gsize<OrdSet> = gsize<List> . OrdSetToList

but a more generic version for types t::*->* equipped with a, say, overloaded
"toList" operation would have to be parametric on the constraint "c", which
might be "empty":

  gsize<t::*->*> :: c a => t a -> Int
  gsize<t> = gsize<List> . toList

At the moment Generic Haskell cannot handle abstract data types because it cannot have access to their representations, which is something that the compiler needs to generate a structural representation type and later to specialize a generic function to the ADT.

However, the ADT provider could perfectly specify the things needed for GH, that is, a structural representation, and embedding projection pair. This fits nicely with ADTs because the embedding projection functions (especially the 'to' direction) can preserve the data type invariants that motivated the use of abstraction in the first place.

This also fits nicely with the idea of Generic Views because ADTs was the motivation for Views in Wadler's original paper.

Just to make things more concrete, this is what the Queue library writer would have written in order to enable generic programming on Queues:

-- snippet --
-- We use a similar structure to lists:
data repr(Queue a) = () `Sum` a `Prod` Queue a
ep(Queue a) :: EP (Queue a) (repr(Queue a))
ep (EP fromQueue toQueue)

fromQueue :: Queue a -> repr(Queue a)
fromQueue q
  = case deQueue q of
     Just (e,q') -> Inr (e :*: q')
     Nothing -> Inl ()
toQueue (Inl (e :*: q)) = e `prependToQueue` q
toQueue (Inr ()) = emptyQueue
-- end of snippet --

The gsize function would be left as it is, while still enabling generic application to Queue and other abstract data types. This is not implemented in GH, but it should not be difficult to do so.

To conclude this long email, I think data abstraction should be considered
from the start in the design of a generic programming library. On the one
hand, this should force us to see the internal representation of types as (1)
a parameter (cause we want to be generic), and (2) as something more abstract
than an encoding of the *implementation* of the type. On the other hand,
perhaps polytypic programming can then be conveyed to an object-oriented
setting without using naive encodings of algebraic data types as the
"objects".

In our framework the representation of ADTs is dictated by the view chosen (and the structure provided by the ADT implementor). For other frameworks I suppose similar techniques would be used.

To be honest, I am a bit confused as how the C++ STL programming style corresponds to generic programming as discussed in this list. In my view the STL stuff looks more related to type classes than to data type generic programming. Maybe you can clarify your point further.

Cheers,

Alexey