Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-11-14 15:02:27


On Fri, Nov 14, 2003 at 09:21:28AM -1000, David Abrahams wrote:
> Anyway, having never used Haskell I'm going to make a few more
> pronouncements ;->. I think the biggest problem with Haskell's
> approach is the lack of type associations that aren't declared in the
> conformance declaration. In other words, every time you make a new
> C++ iterator type, you'd need to say something like:
>
> "Foo is an iterator, int is its value_type, int& is its reference
> type, std::ptrdiff_t is its difference_type,
> std::random_access_iterator_tag is its category, and int* is its
> pointer type"
>
> Have I got that wrong? Naturally you do have to make the same
> associations in C++, but you have the advantage of conveniently
> nesting the types or deriving from some class which generates them
> because of C++'s use of structural conformance.

I am not sure what you are saying or asking here. (Haskell can deal with
the "associated types" using multi-parameter type classes--an extension
which isn't in the Haskell 98 standard, but which is supported by all the
Haskell compilers.)

Since I am guessing it may help, here is how you would encode C++
iterators in Haskell:

   -- (Note that Haskell cannot easily express "implicitly convertible",
   -- "assignment", or "const", so I am choosing not trying to deal with
   -- those issues here.)
   
   -- TrivialIterator
   class (EqualityComparable iter, -- anything inside (...)=>
          DefaultConstructible iter)=> -- is a "prior constraint"
         TrivialIterator iter value_type where
      deref :: iter -> value_type -- "deref i" means "*i"
   
   -- InputIterator
   class (TrivialIterator iter value_type,
          SignedIntegralType distance_type)=>
         InputIterator iter value_type distance_type where
      preinc :: iter -> iter -- "preinc i" means "++i"
      postinc :: iter -> iter -- "postinc i" means "i++"
      
   -- istream_iterator models InputIterator
   instance InputIterator (istream_iterator t d) t d where
      ...
   
   -- etc.

   -- Note that the iterator concepts "carry along with them" their
   -- "intrinsically associated types". "Extrinsically associated
   -- types" (like "reference type" and "pointer type", if I correctly
   -- understand C++ iterator concepts) can be declared using separate
   -- type classes and "functional dependencies" (another Haskell
   -- extension that the compilers now support).

I was trying to base that off of
   http://www.sgi.com/tech/stl/Iterators.html
as you can hopefully tell. Regardless of how well I have actually
performed the translation above, I am pretty confident that Haskell can
adequately express everything that we could express in C++, and it can
do so "roughly as succinctly" as we can in C++.

Please ask questions if things are unclear. (I know it's no fun when
you assert that "I think it's hard to do X in language L" and then some
L-programmer replies with "no it's not!" and shares a mountain of
unreadable-to-you L-code.)

-- 
-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