[Sorry if this appears twice.  Our mail server hung at the exact moment
I tried to send this out last night, and it doesn't appear to have made
it to the list, so I'm resending it.]

On 05-Jun-1998, Simon L Peyton Jones <[EMAIL PROTECTED]> wrote:
> 
> > 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

It seems like a bad thing to have to introduce three names for
different variations (type, class, and constructor) of the same
concept. 

At very least it would be nice to have a standard naming convention,
rather than using inconsistent names (and spellings!) for the different
variations.

But I think it would be nice to have better language support.

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

I think you can go even ahead and declare OrganizationThing to be
a member of class Organization:

        instance Organization OrganizationThing where
                name (MkOrg o) = name o
                foo (MkOrg o) = foo o
                bar (MkOrg o) = bar o
                -- similarly for all methods of class Organization
        
Then you don't need to use that annoying MkOrg constructor so often --
in fact, you only need it when constructing values, I don't think
you ever need it for deconstruction.

However, adding these instance declarations is a bit tedious.
Wouldn't it be handy if the compiler could do that automatically?

So, why not also extend the language so that it if `Foo' is a type class,
then `any Foo' is a type and `new Foo' is a constructor
for that type, and the type `any Foo' is automatically made an
instance of the class `Foo'?
This would make object-oriented programming a lot more convenient.

Also, it might make optimization easier: the compiler can optimize
the use of instance declarations such as the one above, but this
is probably slightly easier (not to mention more likely to be
implemented) if these instance declarations are builtin rather
than if the compiler has to pattern match for that particular idiom.

Of course, you might prefer to use different keywords than
`any' and `new', or even to use a different syntax altogether.

With this extension, the example above could be written

        orgs :: [any Organization]
        orgs = [new Organization Yahoo, new Organization DoubleClick]

and then if you have a function or method such as

        name :: Organization a => a -> String

you can do things like

        orgnames :: [String]
        orgnames = map name orgs

> Does that help?  I'm about to put existentials into GHC.

I'm glad to see it.  We're currently in the process of putting
existentials into Mercury :-)

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.


Reply via email to