[Haskell-cafe] Type classes
Stefan Holdermans
stefan at cs.uu.nl
Mon Mar 20 10:06:14 EST 2006
Max,
>> class DatabaseEntry e where
>> entryLabel :: e -> String
>> formatEntry :: e -> String
>> compareEntries :: e -> e -> Ordering
>
> Then I define
>
>> data Entry = forall a. (DatabaseEntry a) => Entry a
>
>> instance DatabaseEntry Entry where
>> entryLabel (Entry e) = entryLabel e
>> formatEntry (Entry e) = formatEntry e
>
> How can I define compareEntries for this instance?
In general: you can't. The field of the Entry constructor has a
existentially quantified typed. Given two arbitrary values of type
Entry, this type may be instantiated with a different type for each
value, so you cannot easily compare the fields.
If you extend the DatabaseEntry class such that it supplies a method
that allows to produce some canonical representation for database
entries suited for comparison, then you could take that road.
Are you sure that your Entry type needs to be existentially quantified?
HTH,
Stefan
More information about the Haskell-Cafe
mailing list