Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-11-15 04:31:01


On Fri, Nov 14, 2003 at 05:00:54PM -0500, Brian McNamara wrote:
> On Fri, Nov 14, 2003 at 10:38:15AM -1000, David Abrahams wrote:
> > want to ask the authors what they mean what they say:
> >
> > With respect to generics, ML and Haskell provide an informative
> > contrast to the object-oriented languages... The discussions of Java
> > generics, Generic C#, and Eiffel suggest that Haskell and ML avoid
> > some difficulties with implementing generic libraries that the
> > object-oriented languages share... Both languages have
> > disadvantages. ML lacks functions with constrained genericity aside
> > from functions nested within functors. Haskell, though quite
> > expressive, requires an awkward mechanism to express associated
> > types.
>
> I have only glanced at it so far to get a feel for their example. It
> may well be that there I've missed which makes it harder/more-awkward/
> less-scalable/something than I have thought.

Yup; they're right and I'm wrong.

For some reason, in Haskell you can say:

   class GraphTraits g e v | g -> e v

   class MyGraph1 g where
      m1src :: (GraphTraits g e v)=> e -> g -> v
      m1tgt :: (GraphTraits g e v)=> e -> g -> v

but you cannot "push the common constraints up to the top level":

   -- Not legal Haskell code!
   class (GraphTraits g e v) => MyGraph2 g where
      m2src :: e -> g -> v
      m2tgt :: e -> g -> v

without adding the constrained variables to the type class:

   -- Ok; note "e" and "v" are now "extra parameters" to MyGraph
   class (GraphTraits g e v) => MyGraph3 g e v where
      m3src :: e -> g -> v
      m3tgt :: e -> g -> v

The functional dependencies express the relationship that "given a graph
type g, we can deduce the edge/vertex types e/v", but there doesn't seem
to be any Haskell syntax to enable us to take advantage of this
knowledge. How frustrating. (It's just like the omission of "typeof" in
C++ -- the compiler _knows_ the information you want, but there's no
syntax for you to ask for that information. Grr!)

-- 
-Brian McNamara (lorgon_at_[hidden])

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk