> I have a base class,Organization, with name and address functions.
> I want classes Buyer and Seller from Organization.
> 
> Now if I want to create an 2 instances of Seller
> > data Yahoo = Yahoo
> > instance Organization Yahoo where
> >  name _= "Yahoo"
> >  addreess = ...
> 
> > data DoubleClick= DoubleClick
> > instance Organization DoubleClick where
> >  name _ = "DoubleClick"
>   ...
> 
> Why is [Yahoo,DoubleClick] illegal?  

It's illegal because we can't give it a type.  It hasn't got
type [Yahoo], nor [DoubleClick].  What you want is to say 
"it's a list of things in class Organization, and that's all I will
ever ask of them.  In hbc (and soon in GHC) you can say:

        data OrganizationThing = forall a. Organisation a => MkOrg a

(Some people would write "exists" instead of "forall", and that's
reasonable.  But the type of MkOrg really is

        MkOrg :: forall a. Organization a => a -> OrganizationThing
)

Notice that OrganizationThing isn't parameterised.

Now you can say

        foo :: [OrganizationThing]
        foo = [MkOrg Yahoo, MkOrg DoubleClick]

Further, you can say, for example

        toName :: OrganizationThing -> String
        toName (MkOrg o) = name o

In effect, an OrganizationThing is anything that in in class 
Organization. The MkOrg constructor is a bit annoying (like newtype)
but no more.


Does that help?  I'm about to put existentials into GHC.  It's
been delayed while I ripped its entrails out; adding existentials
and then keeping types throughout the compiler caused a non-trivial
change that I took advantage of to perform a heart-liver-and-lung
transplant.

Simon



Reply via email to