> Lennart Augustsson wrote:
> >
> > Kevin Atkinson wrote:
> >
> > > I am sorry for the naive question but how do you fell about adding
> > > dynamic binding to Haskell to make it possible to have heterogeneous
> > > lists with out having to use a nasty union type which has a number of
> > > problems.
> > >
> >
> > What you want(?) is existential types. Most implementations support
> > this.
>
> Maybe? What exactly are they. I have not been able to find a good
> guide which explains the forall and exists qualifier concepts.
There is a short explanation and an example or two in the GHC manual
(section 5.7).
> What I want to be able to do is store a bunch of objects with a common
> base class in a list. And I also want to be able to add types to this
> heritage without having to modify a single line of the existing code.
The first is trivially possible; with existential types you can put any
object of whatever type into a list with other objects. It's not clear to me
exactly what you mean by the second. Existential types let you hide the
actual type (the witness) of an object, but they do not a priori relate to
OO or inheritance.
That said, you can, however, link the use of existential types to the
Haskell type class system by putting a context in the data declaration (at
least in GHC, and, I think HBC also):
class State a where
method :: a -> Int
data Obj = forall a. (State a) => Obj a
Note that the type variable a does not appear as a parameter of the data
declaration.
Then you can construct a heterogeneous list, all of whose "members" have
types that are instances of State (so suppose Int, String and Char are
instances of State):
xs = [Obj 1, Obj "foo", Obj 'c']
Then you can write a function:
process :: [Obj] -> Int
process [] = 0
process (x:xs) = method x + process xs
This is explained in slightly more detail in the GHC manual. There are also
papers about implementing existential types in functional languages (e.g.,
Laufer & Odersky), and these usually include motivating examples, and papers
about using existential types to represent OO-style objects (e.g., Pierce &
Turner). Mail me if you want a full reference.
--FC